selkies-gstreamer-entrypoint.sh 6.4 KB

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