2020-09-19 21:46:46 +02:00
#!/usr/bin/env bash
2020-09-19 22:57:16 +02:00
NGINX_CONF = /etc/nginx/nginx.conf
NGINX_INGEST_CONF = /etc/nginx/nginx_ingest.conf
NGINX_RESTREAM_CONF = /etc/nginx/nginx_restream.conf
2020-09-20 02:49:49 +02:00
NGINX_ENCODE_CONF = /etc/nginx/nginx_encode.conf
2020-09-19 21:46:46 +02:00
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
2020-09-20 02:49:49 +02:00
NGINX_TYPE = " ${ TYPE } "
2020-09-19 21:46:46 +02:00
fi
2020-09-20 02:49:49 +02:00
case $NGINX_TYPE in
"ingest" )
if [ [ -z " ${ STREAM_KEY } " ] ] ; then
2020-09-19 21:46:46 +02:00
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
2020-09-19 22:49:07 +02:00
sed 's@___STREAMKEY___@' " $NGINX_STREAM_KEY " '@' $NGINX_INGEST_CONF > $NGINX_CONF
2020-09-20 02:49:49 +02:00
; ;
"restream" )
2020-09-19 21:46:46 +02:00
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
2020-09-19 22:49:07 +02:00
sed 's@___PUSH_DIRECTIVES___@' " $NGINX_PUSH_URLS " '@' $NGINX_RESTREAM_CONF > $NGINX_CONF
2020-09-19 21:46:46 +02:00
fi
2020-09-20 02:49:49 +02:00
; ;
"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
2020-09-19 21:46:46 +02:00
2020-09-20 03:35:04 +02:00
nginx -g 'pid /tmp/nginx.pid;'