selkies-gstreamer-entrypoint.sh 5.5 KB

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