selkies-gstreamer-entrypoint.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/bash
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. set -e
  6. # Wait for XDG_RUNTIME_DIR
  7. until [ -d "${XDG_RUNTIME_DIR}" ]; do sleep 0.5; done
  8. # Configure joystick interposer
  9. export SELKIES_INTERPOSER='/usr/$LIB/selkies_joystick_interposer.so'
  10. export LD_PRELOAD="${SELKIES_INTERPOSER}${LD_PRELOAD:+:${LD_PRELOAD}}"
  11. export SDL_JOYSTICK_DEVICE=/dev/input/js0
  12. # Set default display
  13. export DISPLAY="${DISPLAY:-:20}"
  14. # PipeWire-Pulse server socket path
  15. export PIPEWIRE_LATENCY="32/48000"
  16. export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp}"
  17. export PIPEWIRE_RUNTIME_DIR="${PIPEWIRE_RUNTIME_DIR:-${XDG_RUNTIME_DIR:-/tmp}}"
  18. export PULSE_RUNTIME_PATH="${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}"
  19. export PULSE_SERVER="${PULSE_SERVER:-unix:${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}/native}"
  20. # Configure NGINX
  21. if [ "$(echo ${SELKIES_ENABLE_BASIC_AUTH} | tr '[:upper:]' '[:lower:]')" != "false" ]; then htpasswd -bcm "${XDG_RUNTIME_DIR}/.htpasswd" "${SELKIES_BASIC_AUTH_USER:-${USER}}" "${SELKIES_BASIC_AUTH_PASSWORD:-${PASSWD}}"; fi
  22. echo "# Selkies-GStreamer NGINX Configuration
  23. server {
  24. access_log /dev/stdout;
  25. error_log /dev/stderr;
  26. listen 8080 $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "ssl"; fi);
  27. listen [::]:8080 $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "ssl"; fi);
  28. ssl_certificate ${SELKIES_HTTPS_CERT-/etc/ssl/certs/ssl-cert-snakeoil.pem};
  29. ssl_certificate_key ${SELKIES_HTTPS_KEY-/etc/ssl/private/ssl-cert-snakeoil.key};
  30. $(if [ \"$(echo ${SELKIES_ENABLE_BASIC_AUTH} | tr '[:upper:]' '[:lower:]')\" != \"false\" ]; then echo "auth_basic \"Selkies\";"; echo -n " auth_basic_user_file ${XDG_RUNTIME_DIR}/.htpasswd;"; fi)
  31. location / {
  32. root /opt/gst-web/;
  33. index index.html index.htm;
  34. }
  35. location /health {
  36. proxy_http_version 1.1;
  37. proxy_read_timeout 3600s;
  38. proxy_send_timeout 3600s;
  39. proxy_connect_timeout 3600s;
  40. proxy_buffering off;
  41. client_max_body_size 10M;
  42. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:8081;
  43. }
  44. location /turn {
  45. proxy_http_version 1.1;
  46. proxy_read_timeout 3600s;
  47. proxy_send_timeout 3600s;
  48. proxy_connect_timeout 3600s;
  49. proxy_buffering off;
  50. client_max_body_size 10M;
  51. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:8081;
  52. }
  53. location /ws {
  54. proxy_set_header Upgrade \$http_upgrade;
  55. proxy_set_header Connection \"upgrade\";
  56. proxy_set_header Host \$host;
  57. proxy_set_header X-Real-IP \$remote_addr;
  58. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  59. proxy_set_header X-Forwarded-Proto \$scheme;
  60. proxy_http_version 1.1;
  61. proxy_read_timeout 3600s;
  62. proxy_send_timeout 3600s;
  63. proxy_connect_timeout 3600s;
  64. proxy_buffering off;
  65. client_max_body_size 10M;
  66. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:8081;
  67. }
  68. location /webrtc/signalling {
  69. proxy_set_header Upgrade \$http_upgrade;
  70. proxy_set_header Connection \"upgrade\";
  71. proxy_set_header Host \$host;
  72. proxy_set_header X-Real-IP \$remote_addr;
  73. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  74. proxy_set_header X-Forwarded-Proto \$scheme;
  75. proxy_http_version 1.1;
  76. proxy_read_timeout 3600s;
  77. proxy_send_timeout 3600s;
  78. proxy_connect_timeout 3600s;
  79. proxy_buffering off;
  80. client_max_body_size 10M;
  81. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:8081;
  82. }
  83. location /metrics {
  84. proxy_http_version 1.1;
  85. proxy_read_timeout 3600s;
  86. proxy_send_timeout 3600s;
  87. proxy_connect_timeout 3600s;
  88. proxy_buffering off;
  89. client_max_body_size 10M;
  90. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:9081;
  91. }
  92. error_page 500 502 503 504 /50x.html;
  93. location = /50x.html {
  94. root /opt/gst-web/;
  95. }
  96. }" | tee /etc/nginx/sites-available/default > /dev/null
  97. # Export environment variables required for Selkies-GStreamer
  98. export GST_DEBUG="${GST_DEBUG:-*:2}"
  99. export GSTREAMER_PATH=/opt/gstreamer
  100. # Source environment for GStreamer
  101. . /opt/gstreamer/gst-env
  102. export SELKIES_ENCODER="${SELKIES_ENCODER:-x264enc}"
  103. export SELKIES_ENABLE_RESIZE="${SELKIES_ENABLE_RESIZE:-false}"
  104. if ( [ -z "${SELKIES_TURN_USERNAME}" ] || [ -z "${SELKIES_TURN_PASSWORD}" ] ) && [ -z "${SELKIES_TURN_SHARED_SECRET}" ] || [ -z "${SELKIES_TURN_HOST}" ] || [ -z "${SELKIES_TURN_PORT}" ]; then
  105. export TURN_RANDOM_PASSWORD="$(tr -dc 'A-Za-z0-9' < /dev/urandom 2>/dev/null | head -c 24)"
  106. export SELKIES_TURN_HOST="$(curl -fsSL checkip.amazonaws.com)"
  107. export SELKIES_TURN_PORT="3478"
  108. export SELKIES_TURN_USERNAME="selkies"
  109. export SELKIES_TURN_PASSWORD="${TURN_RANDOM_PASSWORD}"
  110. export SELKIES_TURN_PROTOCOL="${SELKIES_TURN_PROTOCOL:-tcp}"
  111. /etc/start-turnserver.sh &
  112. fi
  113. # Wait for X server to start
  114. echo 'Waiting for X Socket' && until [ -S "/tmp/.X11-unix/X${DISPLAY#*:}" ]; do sleep 0.5; done && echo 'X Server is ready'
  115. # Clear the cache registry
  116. rm -rf "${HOME}/.cache/gstreamer-1.0"
  117. # Start the Selkies-GStreamer WebRTC HTML5 remote desktop application
  118. selkies-gstreamer \
  119. --addr="localhost" \
  120. --port="8081" \
  121. --enable_basic_auth="false" \
  122. --enable_metrics_http="true" \
  123. --metrics_http_port="9081" \
  124. $@