selkies-gstreamer-entrypoint.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. echo "# Selkies-GStreamer NGINX Configuration
  18. error_log /dev/stderr error;
  19. server {
  20. access_log /dev/stdout main;
  21. listen 8080 $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then printf \"ssl\"; fi);
  22. listen [::]:8080 $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then printf \"ssl\"; fi);
  23. ssl_certificate ${SELKIES_HTTPS_CERT-/etc/ssl/certs/ssl-cert-snakeoil.pem};
  24. ssl_certificate_key ${SELKIES_HTTPS_KEY-/etc/ssl/private/ssl-cert-snakeoil.key};
  25. location / {
  26. alias /opt/gst-web;
  27. index index.html index.htm;
  28. }
  29. location /health {
  30. proxy_http_version 1.1;
  31. proxy_read_timeout 1800s;
  32. proxy_send_timeout 1800s;
  33. proxy_connect_timeout 1800s;
  34. proxy_buffering off;
  35. client_max_body_size 10M;
  36. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then printf \"s\"; fi)://localhost:8081;
  37. }
  38. location /turn {
  39. proxy_http_version 1.1;
  40. proxy_read_timeout 1800s;
  41. proxy_send_timeout 1800s;
  42. proxy_connect_timeout 1800s;
  43. proxy_buffering off;
  44. client_max_body_size 10M;
  45. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then printf \"s\"; fi)://localhost:8081;
  46. }
  47. location /ws {
  48. proxy_set_header Upgrade \$http_upgrade;
  49. proxy_set_header Connection \"upgrade\";
  50. proxy_set_header Host \$host;
  51. proxy_set_header X-Real-IP \$remote_addr;
  52. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  53. proxy_set_header X-Forwarded-Proto \$scheme;
  54. proxy_http_version 1.1;
  55. proxy_read_timeout 1800s;
  56. proxy_send_timeout 1800s;
  57. proxy_connect_timeout 1800s;
  58. proxy_buffering off;
  59. client_max_body_size 10M;
  60. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then printf \"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-Real-IP \$remote_addr;
  67. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  68. proxy_set_header X-Forwarded-Proto \$scheme;
  69. proxy_http_version 1.1;
  70. proxy_read_timeout 1800s;
  71. proxy_send_timeout 1800s;
  72. proxy_connect_timeout 1800s;
  73. proxy_buffering off;
  74. client_max_body_size 10M;
  75. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then printf \"s\"; fi)://localhost:8081;
  76. }
  77. location /metrics {
  78. proxy_http_version 1.1;
  79. proxy_read_timeout 1800s;
  80. proxy_send_timeout 1800s;
  81. proxy_connect_timeout 1800s;
  82. proxy_buffering off;
  83. client_max_body_size 10M;
  84. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then printf \"s\"; fi)://localhost:8081;
  85. }
  86. error_page 500 502 503 504 /50x.html;
  87. location = /50x.html {
  88. alias /opt/gst-web;
  89. }
  90. }" | tee /etc/nginx/sites-available/default > /dev/null
  91. # Export environment variables required for Selkies-GStreamer
  92. export GST_DEBUG="${GST_DEBUG:-*:2}"
  93. export GSTREAMER_PATH=/opt/gstreamer
  94. # Source environment for GStreamer
  95. . /opt/gstreamer/gst-env
  96. export SELKIES_ENCODER="${SELKIES_ENCODER:-x264enc}"
  97. export SELKIES_ENABLE_RESIZE="${SELKIES_ENABLE_RESIZE:-false}"
  98. if ( [ -z "${SELKIES_TURN_USERNAME}" ] || [ -z "${SELKIES_TURN_PASSWORD}" ] ) && [ -z "${SELKIES_TURN_SHARED_SECRET}" ] || [ -z "${SELKIES_TURN_HOST}" ] || [ -z "${SELKIES_TURN_PORT}" ]; then
  99. export TURN_RANDOM_PASSWORD="$(tr -dc 'A-Za-z0-9' < /dev/urandom 2>/dev/null | head -c 24)"
  100. export SELKIES_TURN_HOST="$(curl -fsSL checkip.amazonaws.com)"
  101. export SELKIES_TURN_PORT="3478"
  102. export SELKIES_TURN_USERNAME="selkies"
  103. export SELKIES_TURN_PASSWORD="${TURN_RANDOM_PASSWORD}"
  104. /etc/start-turnserver.sh &
  105. fi
  106. export SELKIES_TURN_PROTOCOL="${SELKIES_TURN_PROTOCOL:-tcp}"
  107. # Wait for X server to start
  108. echo 'Waiting for X Socket' && until [ -S "/tmp/.X11-unix/X${DISPLAY#*:}" ]; do sleep 0.5; done && echo 'X Server is ready'
  109. # Clear the cache registry
  110. rm -rf "${HOME}/.cache/gstreamer-1.0"
  111. # Start the Selkies-GStreamer WebRTC HTML5 remote desktop application
  112. selkies-gstreamer \
  113. --addr="0.0.0.0" \
  114. --port="8081" \
  115. $@