selkies-gstreamer-entrypoint.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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-Real-IP \$remote_addr;
  54. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  55. proxy_set_header X-Forwarded-Proto \$scheme;
  56. proxy_http_version 1.1;
  57. proxy_read_timeout 3600s;
  58. proxy_send_timeout 3600s;
  59. proxy_connect_timeout 3600s;
  60. proxy_buffering off;
  61. client_max_body_size 10M;
  62. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:8081;
  63. }
  64. location /webrtc/signalling {
  65. proxy_set_header Upgrade \$http_upgrade;
  66. proxy_set_header Connection \"upgrade\";
  67. proxy_set_header Host \$host;
  68. proxy_set_header X-Real-IP \$remote_addr;
  69. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  70. proxy_set_header X-Forwarded-Proto \$scheme;
  71. proxy_http_version 1.1;
  72. proxy_read_timeout 3600s;
  73. proxy_send_timeout 3600s;
  74. proxy_connect_timeout 3600s;
  75. proxy_buffering off;
  76. client_max_body_size 10M;
  77. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:8081;
  78. }
  79. location /metrics {
  80. proxy_http_version 1.1;
  81. proxy_read_timeout 3600s;
  82. proxy_send_timeout 3600s;
  83. proxy_connect_timeout 3600s;
  84. proxy_buffering off;
  85. client_max_body_size 10M;
  86. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:9081;
  87. }
  88. error_page 500 502 503 504 /50x.html;
  89. location = /50x.html {
  90. root /opt/gst-web/;
  91. }
  92. }" | tee /etc/nginx/sites-available/default > /dev/null
  93. # Export environment variables required for Selkies-GStreamer
  94. export GST_DEBUG="${GST_DEBUG:-*:2}"
  95. export GSTREAMER_PATH=/opt/gstreamer
  96. # Source environment for GStreamer
  97. . /opt/gstreamer/gst-env
  98. export SELKIES_ENCODER="${SELKIES_ENCODER:-x264enc}"
  99. export SELKIES_ENABLE_RESIZE="${SELKIES_ENABLE_RESIZE:-false}"
  100. if ( [ -z "${SELKIES_TURN_USERNAME}" ] || [ -z "${SELKIES_TURN_PASSWORD}" ] ) && [ -z "${SELKIES_TURN_SHARED_SECRET}" ] || [ -z "${SELKIES_TURN_HOST}" ] || [ -z "${SELKIES_TURN_PORT}" ]; then
  101. export TURN_RANDOM_PASSWORD="$(tr -dc 'A-Za-z0-9' < /dev/urandom 2>/dev/null | head -c 24)"
  102. export SELKIES_TURN_HOST="$(curl -fsSL checkip.amazonaws.com)"
  103. export SELKIES_TURN_PORT="3478"
  104. export SELKIES_TURN_USERNAME="selkies"
  105. export SELKIES_TURN_PASSWORD="${TURN_RANDOM_PASSWORD}"
  106. /etc/start-turnserver.sh &
  107. fi
  108. export SELKIES_TURN_PROTOCOL="${SELKIES_TURN_PROTOCOL:-tcp}"
  109. # Wait for X server to start
  110. echo 'Waiting for X Socket' && until [ -S "/tmp/.X11-unix/X${DISPLAY#*:}" ]; do sleep 0.5; done && echo 'X Server is ready'
  111. # Clear the cache registry
  112. rm -rf "${HOME}/.cache/gstreamer-1.0"
  113. # Start the Selkies-GStreamer WebRTC HTML5 remote desktop application
  114. selkies-gstreamer \
  115. --addr="0.0.0.0" \
  116. --port="8081" \
  117. --enable_basic_auth="false" \
  118. --enable_metrics_http="true" \
  119. --metrics_http_port="9081" \
  120. $@