nginx-streamer/startscript.sh
Torma Kristof 9e8b872200
All checks were successful
continuous-integration/drone/push Build is passing
change pid directory
2020-09-20 03:35:04 +02:00

70 lines
2.2 KiB
Bash

#!/usr/bin/env bash
NGINX_CONF=/etc/nginx/nginx.conf
NGINX_INGEST_CONF=/etc/nginx/nginx_ingest.conf
NGINX_RESTREAM_CONF=/etc/nginx/nginx_restream.conf
NGINX_ENCODE_CONF=/etc/nginx/nginx_encode.conf
function mfcb { local val="$4"; "$1"; eval "$2[$3]=\$val;"; };
function val_ltrim { if [[ "$val" =~ ^[[:space:]]+ ]]; then val="${val:${#BASH_REMATCH[0]}}"; fi; };
function val_rtrim { if [[ "$val" =~ [[:space:]]+$ ]]; then val="${val:0:${#val}-${#BASH_REMATCH[0]}}"; fi; };
function val_trim { val_ltrim; val_rtrim; };
if [[ -z "${TYPE}" ]]; then
NGINX_TYPE="ingest"
else
NGINX_TYPE="${TYPE}"
fi
case $NGINX_TYPE in
"ingest")
if [[ -z "${STREAM_KEY}" ]]; then
NGINX_STREAM_KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
echo -e "Stream Key: $NGINX_STREAM_KEY"
else
NGINX_STREAM_KEY="${STREAM_KEY}"
fi
sed 's@___STREAMKEY___@'"$NGINX_STREAM_KEY"'@' $NGINX_INGEST_CONF > $NGINX_CONF
;;
"restream")
if [[ -z "${PUSH_URLS}" ]]; then
echo -e "Envvar PUSH_URLS required"
exit 1
else
readarray -c1 -C 'mfcb val_trim a' -td, <<<"$PUSH_URLS,"; unset 'a[-1]'; declare -a a;
NGINX_PUSH_URLS=""
for i in "${a[@]}"
do
:
NGINX_PUSH_URLS=$NGINX_PUSH_URLS$'push '$i$'\n'
done
sed 's@___PUSH_DIRECTIVES___@'"$NGINX_PUSH_URLS"'@' $NGINX_RESTREAM_CONF > $NGINX_CONF
fi
;;
"encode")
if [[ -z "${STREAM_KEY}" ]]; then
NGINX_STREAM_KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
echo -e "Stream Key: $NGINX_STREAM_KEY"
else
NGINX_STREAM_KEY="${STREAM_KEY}"
fi
if [[ -z "${ENCODE_PUSH_URL}" ]]; then
echo -e "Environment variable ENCODE_PUSH_URL must be set."
exit 1
else
NGINX_ENCODE_PUSH_URL="${STREAM_KEY}"
fi
if [[ -z "${FFMPEG_ARGS}" ]]; then
echo -e "Environment variable FFMPEG_ARGS must be set."
exit 1
else
NGINX_FFMPEG_ARGS="${FFMPEG_ARGS}"
fi
sed 's@___STREAMKEY___@'"$NGINX_STREAM_KEY"'@' $NGINX_ENCODET_CONF | sed 's@___ENCOE_PUSH_URL___@'"$NGINX_ENCODE_PUSH_URL"'@' | sed 's@___FFMPEG_ARGS___@'"$NGINX_FFMPEG_ARGS"'@' > $NGINX_CONF
;;
*)
echo -e "Environment variable TYPE must be either encode, restream or ingest".
;;
esac
nginx -g 'pid /tmp/nginx.pid;'