selkies-gstreamer-entrypoint.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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="128/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. # Export environment variables required for Selkies
  21. export GST_DEBUG="${GST_DEBUG:-*:2}"
  22. export GSTREAMER_PATH=/opt/gstreamer
  23. # Source environment for GStreamer
  24. . /opt/gstreamer/gst-env
  25. export SELKIES_ENCODER="${SELKIES_ENCODER:-x264enc}"
  26. export SELKIES_ENABLE_RESIZE="${SELKIES_ENABLE_RESIZE:-false}"
  27. if [ -z "${SELKIES_TURN_REST_URI}" ] && { { [ -z "${SELKIES_TURN_USERNAME}" ] || [ -z "${SELKIES_TURN_PASSWORD}" ]; } && [ -z "${SELKIES_TURN_SHARED_SECRET}" ] || [ -z "${SELKIES_TURN_HOST}" ] || [ -z "${SELKIES_TURN_PORT}" ]; }; then
  28. export TURN_RANDOM_PASSWORD="$(tr -dc 'A-Za-z0-9' < /dev/urandom 2>/dev/null | head -c 24)"
  29. export SELKIES_TURN_HOST="${SELKIES_TURN_HOST:-$(dig -4 TXT +short @ns1.google.com o-o.myaddr.l.google.com 2>/dev/null | { read output; if [ -z "$output" ] || echo "$output" | grep -q '^;;'; then exit 1; else echo "$(echo $output | sed 's,\",,g')"; fi } || dig -6 TXT +short @ns1.google.com o-o.myaddr.l.google.com 2>/dev/null | { read output; if [ -z "$output" ] || echo "$output" | grep -q '^;;'; then exit 1; else echo "[$(echo $output | sed 's,\",,g')]"; fi } || hostname -I 2>/dev/null | awk '{print $1; exit}' || echo '127.0.0.1')}"
  30. export TURN_EXTERNAL_IP="${TURN_EXTERNAL_IP:-$(getent ahostsv4 $(echo ${SELKIES_TURN_HOST} | tr -d '[]') 2>/dev/null | awk '{print $1; exit}' || getent ahostsv6 $(echo ${SELKIES_TURN_HOST} | tr -d '[]') 2>/dev/null | awk '{print "[" $1 "]"; exit}')}"
  31. export SELKIES_TURN_PORT="${SELKIES_TURN_PORT:-3478}"
  32. export SELKIES_TURN_USERNAME="selkies"
  33. export SELKIES_TURN_PASSWORD="${TURN_RANDOM_PASSWORD}"
  34. export SELKIES_TURN_PROTOCOL="${SELKIES_TURN_PROTOCOL:-tcp}"
  35. export SELKIES_STUN_HOST="${SELKIES_STUN_HOST:-stun.l.google.com}"
  36. export SELKIES_STUN_PORT="${SELKIES_STUN_PORT:-19302}"
  37. /etc/start-turnserver.sh &
  38. fi
  39. # Extract NVRTC dependency, https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvrtc/LICENSE.txt
  40. if command -v nvidia-smi &> /dev/null && nvidia-smi >/dev/null 2>&1; then
  41. CUDA_DRIVER_SYSTEM="$(nvidia-smi | sed -n 's/.*CUDA Version: \([0-9]\+\.[0-9]\+\).*/\1/p')"
  42. NVRTC_ARCH="${NVRTC_ARCH-$(dpkg --print-architecture | sed -e 's/arm64/sbsa/' -e 's/ppc64el/ppc64le/' -e 's/i.*86/x86/' -e 's/amd64/x86_64/' -e 's/unknown/x86_64/')}"
  43. NVRTC_URL="https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvrtc/linux-${NVRTC_ARCH}/"
  44. NVRTC_ARCHIVE="$(curl -fsSL "${NVRTC_URL}" | grep -oP "(?<=href=')cuda_nvrtc-linux-${NVRTC_ARCH}-${CUDA_DRIVER_SYSTEM}\.[0-9]+-archive\.tar\.xz" | sort -V | tail -n 1)"
  45. if [ -z "${NVRTC_ARCHIVE}" ]; then
  46. FALLBACK_VERSION="${CUDA_DRIVER_SYSTEM}.0"
  47. NVRTC_ARCHIVE=$((curl -fsSL "${NVRTC_URL}" | grep -oP "(?<=href=')cuda_nvrtc-linux-${NVRTC_ARCH}-.*?\.tar\.xz" ; \
  48. echo "cuda_nvrtc-linux-${NVRTC_ARCH}-${FALLBACK_VERSION}-archive.tar.xz") | \
  49. sort -V | grep -B 1 --fixed-strings "${FALLBACK_VERSION}" | head -n 1)
  50. fi
  51. if [ -z "${NVRTC_ARCHIVE}" ]; then
  52. echo "ERROR: Could not find a compatible NVRTC archive." >&2
  53. fi
  54. echo "Selected NVRTC archive: ${NVRTC_ARCHIVE}"
  55. GST_ARCH="$(dpkg --print-architecture | sed -e 's/arm64/aarch64-linux-gnu/' -e 's/armhf/arm-linux-gnueabihf/' -e 's/riscv64/riscv64-linux-gnu/' -e 's/ppc64el/powerpc64le-linux-gnu/' -e 's/s390x/s390x-linux-gnu/' -e 's/i.*86/i386-linux-gnu/' -e 's/amd64/x86_64-linux-gnu/' -e 's/unknown/x86_64-linux-gnu/')"
  56. cd /tmp && curl -fsSL "${NVRTC_URL}${NVRTC_ARCHIVE}" | tar -xJf - -C /tmp && mv -f cuda_nvrtc* cuda_nvrtc && cd cuda_nvrtc/lib && chmod -f 755 libnvrtc* && rm -f "/opt/gstreamer/lib/${GST_ARCH}/"libnvrtc* && mv -f libnvrtc* "/opt/gstreamer/lib/${GST_ARCH}/" && cd /tmp && rm -rf /tmp/cuda_nvrtc
  57. fi
  58. # Wait for X server to start
  59. echo 'Waiting for X Socket' && until [ -S "/tmp/.X11-unix/X${DISPLAY#*:}" ]; do sleep 0.5; done && echo 'X Server is ready'
  60. # Configure NGINX
  61. 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
  62. echo "# Selkies NGINX Configuration
  63. server {
  64. access_log /dev/stdout;
  65. error_log /dev/stderr;
  66. listen ${NGINX_PORT:-8080} $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "ssl"; fi);
  67. listen [::]:${NGINX_PORT:-8080} $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "ssl"; fi);
  68. ssl_certificate ${SELKIES_HTTPS_CERT-/etc/ssl/certs/ssl-cert-snakeoil.pem};
  69. ssl_certificate_key ${SELKIES_HTTPS_KEY-/etc/ssl/private/ssl-cert-snakeoil.key};
  70. $(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)
  71. location / {
  72. root /opt/gst-web/;
  73. index index.html index.htm;
  74. }
  75. location /health {
  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:${SELKIES_PORT:-8081};
  83. }
  84. location /turn {
  85. proxy_http_version 1.1;
  86. proxy_read_timeout 3600s;
  87. proxy_send_timeout 3600s;
  88. proxy_connect_timeout 3600s;
  89. proxy_buffering off;
  90. client_max_body_size 10M;
  91. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_PORT:-8081};
  92. }
  93. location /ws {
  94. proxy_set_header Upgrade \$http_upgrade;
  95. proxy_set_header Connection \"upgrade\";
  96. proxy_set_header Host \$host;
  97. proxy_set_header X-Real-IP \$remote_addr;
  98. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  99. proxy_set_header X-Forwarded-Proto \$scheme;
  100. proxy_http_version 1.1;
  101. proxy_read_timeout 3600s;
  102. proxy_send_timeout 3600s;
  103. proxy_connect_timeout 3600s;
  104. proxy_buffering off;
  105. client_max_body_size 10M;
  106. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_PORT:-8081};
  107. }
  108. location /webrtc/signalling {
  109. proxy_set_header Upgrade \$http_upgrade;
  110. proxy_set_header Connection \"upgrade\";
  111. proxy_set_header Host \$host;
  112. proxy_set_header X-Real-IP \$remote_addr;
  113. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  114. proxy_set_header X-Forwarded-Proto \$scheme;
  115. proxy_http_version 1.1;
  116. proxy_read_timeout 3600s;
  117. proxy_send_timeout 3600s;
  118. proxy_connect_timeout 3600s;
  119. proxy_buffering off;
  120. client_max_body_size 10M;
  121. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_PORT:-8081};
  122. }
  123. location /metrics {
  124. proxy_http_version 1.1;
  125. proxy_read_timeout 3600s;
  126. proxy_send_timeout 3600s;
  127. proxy_connect_timeout 3600s;
  128. proxy_buffering off;
  129. client_max_body_size 10M;
  130. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_METRICS_HTTP_PORT:-9081};
  131. }
  132. error_page 500 502 503 504 /50x.html;
  133. location = /50x.html {
  134. root /opt/gst-web/;
  135. }
  136. }" | tee /etc/nginx/sites-available/default > /dev/null
  137. # Clear the cache registry
  138. rm -rf "${HOME}/.cache/gstreamer-1.0"
  139. # Start the Selkies WebRTC HTML5 remote desktop application
  140. selkies-gstreamer \
  141. --addr="localhost" \
  142. --port="${SELKIES_PORT:-8081}" \
  143. --enable_basic_auth="false" \
  144. --enable_metrics_http="true" \
  145. --metrics_http_port="${SELKIES_METRICS_HTTP_PORT:-9081}" \
  146. $@