From e0a298da5bba566e10f4ac68292c6050081f6b5d Mon Sep 17 00:00:00 2001 From: Torma Kristof Date: Sun, 20 Sep 2020 02:49:49 +0200 Subject: [PATCH] add encode mode --- Dockerfile | 5 +-- README.md | 18 +++++--- config/nginx_encode.conf | 17 +++++++ nginx_ingest.conf => config/nginx_ingest.conf | 0 .../nginx_restream.conf | 0 startscript.sh | 45 ++++++++++++++----- 6 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 config/nginx_encode.conf rename nginx_ingest.conf => config/nginx_ingest.conf (100%) rename nginx_restream.conf => config/nginx_restream.conf (100%) diff --git a/Dockerfile b/Dockerfile index db42daa..b40866c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,9 +40,8 @@ RUN apt-get update && \ ln -sf /dev/stdout /var/log/nginx/access.log && \ ln -sf /dev/stderr /var/log/nginx/error.log -COPY startscript.sh /usr/sbin/startscript.sh -COPY nginx_ingest.conf /etc/nginx/nginx_ingest.conf -COPY nginx_restream.conf /etc/nginx/nginx_restream.conf +COPY ./startscript.sh /usr/sbin/startscript.sh +COPY ./config/ /etc/nginx/ EXPOSE 1935 diff --git a/README.md b/README.md index a7265f8..e09a43a 100644 --- a/README.md +++ b/README.md @@ -13,19 +13,25 @@ For example typing ``docker run registry.kmlabz.com/videon/nginx-streamer If there are no environment variables present, NGINX Streamer stars in ingest mode and generates a stream key for you. - - TYPE - May be ``ingest`` or ``restream`` - - STREAM_KEY - May be an alphanumeric string - - PUSH_URLS - May be a list of rtmp endpoints to restream to. For example: ``rtmp://live-fra02.twitch.tv/app/{stream_key}, rtmp://a.rtmp.youtube.com/live2``. This environment variable is only relevant in restream mode. + - *TYPE* - May be ``ingest``, ``restream`` or ``encode`` + - *STREAM_KEY* - May be an alphanumeric string + - *PUSH_URLS* - May be a list of rtmp endpoints to restream to. For example: ``rtmp://live-fra02.twitch.tv/app/{stream_key}, rtmp://a.rtmp.youtube.com/live2``. This environment variable is only relevant in restream mode. + - *FFMPEG_ARGS* - Arguments passed to the internal FFMPEG encoder. + - *ENCODE_PUSH_URL* - **RTMP** endpoint to push the stream to. ## Operating Modes ### Ingest Mode -Takes an **RTMP** stream at ``rtmp:///origin/`` and exposes the exact same stream at ``rtmp:///live``. +Takes an **RTMP** stream at ``rtmp:///origin/`` and exposes the exact same stream at ``rtmp:///live``. It is active if *TYPE* is set to ``ingest``. ### Restream Mode -The input **RTMP** stream at ``rtmp:///origin/`` will be pushed to the specified **RTMP** endpoints. +The input **RTMP** stream at ``rtmp:///origin/`` will be pushed to the **RTMP** endpoints specified in *PUSH_URLS*. It is active if *TYPE* is set to ``restream``. + +### Encode Mode + +The input **RTMP** stream at ``rtmp:///origin/`` gets encoded according to the environment variable ``FFMPEG_ARGS`` and then gets pushed to pushed to the **RTMP** endpoints specified in *ENCODE_PUSH_URL*. It is active if *TYPE* is set to ``encode``. ## Ports @@ -34,3 +40,5 @@ NGINX Streamer exposes the default **RTMP** port, 1935. You may forward this to ## TODO Currently, NGINX Streamer does not validate stream keys. This may be implemented in Django, for example. + +An API to get the current stream key would be nice as well. diff --git a/config/nginx_encode.conf b/config/nginx_encode.conf new file mode 100644 index 0000000..fa34a64 --- /dev/null +++ b/config/nginx_encode.conf @@ -0,0 +1,17 @@ +daemon off; +worker_processes 1; +events { + worker_connections 1024; +} +rtmp { + server { + listen 1935; + chunk_size 8192; + application origin { + live on; + record off; + meta copy; + exec ffmpeg -i rtmp://localhost:1935/origin/___STREAMKEY___ ___FFMPEG_ARGS___ ___ENCOE_PUSH_URL___; + } + } +} \ No newline at end of file diff --git a/nginx_ingest.conf b/config/nginx_ingest.conf similarity index 100% rename from nginx_ingest.conf rename to config/nginx_ingest.conf diff --git a/nginx_restream.conf b/config/nginx_restream.conf similarity index 100% rename from nginx_restream.conf rename to config/nginx_restream.conf diff --git a/startscript.sh b/startscript.sh index 8e3d00c..a4060f2 100644 --- a/startscript.sh +++ b/startscript.sh @@ -3,6 +3,7 @@ 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; }; @@ -11,23 +12,20 @@ function val_trim { val_ltrim; val_rtrim; }; if [[ -z "${TYPE}" ]]; then NGINX_TYPE="ingest" else - if [[ $TYPE == "ingest" ]] || [[ $TYPE == "restream" ]]; then - NGINX_TYPE="${TYPE}" - else - echo -e "Envvar TYPE must be either ingest or restream" - exit 1 - fi + NGINX_TYPE="${TYPE}" fi -if [[ $NGINX_TYPE == "ingest" ]]; then - if [[ -z "${STREAM_KEY}" ]]; then +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 -else +;; +"restream") if [[ -z "${PUSH_URLS}" ]]; then echo -e "Envvar PUSH_URLS required" exit 1 @@ -41,6 +39,31 @@ else done sed 's@___PUSH_DIRECTIVES___@'"$NGINX_PUSH_URLS"'@' $NGINX_RESTREAM_CONF > $NGINX_CONF fi -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 \ No newline at end of file +nginx