From 0fdfb9097047c7a5dc40f52977a9d2c79d72e910 Mon Sep 17 00:00:00 2001 From: Torma Kristof Date: Sat, 19 Sep 2020 21:46:46 +0200 Subject: [PATCH] first commit --- Dockerfile | 45 +++++++++++++++++++++++++++++++++++++++++ README.md | 36 +++++++++++++++++++++++++++++++++ nginx_ingest.conf | 22 ++++++++++++++++++++ nginx_restream.conf | 16 +++++++++++++++ startscript.sh | 49 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 nginx_ingest.conf create mode 100644 nginx_restream.conf create mode 100644 startscript.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0709cd4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,45 @@ +FROM buildpack-deps:focal + +ENV NGINX_VERSION nginx-1.19.2 +ENV NGINX_RTMP_MODULE_VERSION 1.2.1 + +RUN apt-get update && \ + apt-get install -y ca-certificates openssl libssl-dev ffmpeg && \ + apt-get clean && rm -rf /var/lib/apt/lists/* + +RUN mkdir -p /tmp/build/nginx && \ + cd /tmp/build/nginx && \ + wget -O ${NGINX_VERSION}.tar.gz https://nginx.org/download/${NGINX_VERSION}.tar.gz && \ + tar -zxf ${NGINX_VERSION}.tar.gz + +RUN mkdir -p /tmp/build/nginx-rtmp-module && \ + cd /tmp/build/nginx-rtmp-module && \ + wget -O nginx-rtmp-module-${NGINX_RTMP_MODULE_VERSION}.tar.gz https://github.com/arut/nginx-rtmp-module/archive/v${NGINX_RTMP_MODULE_VERSION}.tar.gz && \ + tar -zxf nginx-rtmp-module-${NGINX_RTMP_MODULE_VERSION}.tar.gz && \ + cd nginx-rtmp-module-${NGINX_RTMP_MODULE_VERSION} + +RUN cd /tmp/build/nginx/${NGINX_VERSION} && \ + ./configure \ + --sbin-path=/usr/local/sbin/nginx \ + --conf-path=/etc/nginx/nginx.conf \ + --error-log-path=/var/log/nginx/error.log \ + --pid-path=/var/run/nginx/nginx.pid \ + --lock-path=/var/lock/nginx/nginx.lock \ + --http-log-path=/var/log/nginx/access.log \ + --http-client-body-temp-path=/tmp/nginx-client-body \ + --with-http_ssl_module \ + --with-threads \ + --with-ipv6 \ + --add-module=/tmp/build/nginx-rtmp-module/nginx-rtmp-module-${NGINX_RTMP_MODULE_VERSION} && \ + make -j $(getconf _NPROCESSORS_ONLN) && \ + make install && \ + mkdir /var/lock/nginx && \ + rm -rf /tmp/build + +RUN 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 + +EXPOSE 1935 +CMD ["bash", "/usr/sbin/startscript.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7265f8 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# NGINX Streamer + +NGINX Streamer is a simple to use and lightweight **RTMP** ingest or restream tool. It can distribute streams to multiple proviers or copy a stream to another local endpoint you can then view live. + +## Requirements + +NGINX Streamer only requires an OCI compatible container runtime to run. + +For example typing ``docker run registry.kmlabz.com/videon/nginx-streamer +`` into your terminal should do the trick. + +## Environment Variables + +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. + +## Operating Modes + +### Ingest Mode + +Takes an **RTMP** stream at ``rtmp:///origin/`` and exposes the exact same stream at ``rtmp:///live``. + +### Restream Mode + +The input **RTMP** stream at ``rtmp:///origin/`` will be pushed to the specified **RTMP** endpoints. + +## Ports + +NGINX Streamer exposes the default **RTMP** port, 1935. You may forward this to any port on your machine. + +## TODO + +Currently, NGINX Streamer does not validate stream keys. This may be implemented in Django, for example. diff --git a/nginx_ingest.conf b/nginx_ingest.conf new file mode 100644 index 0000000..8311f80 --- /dev/null +++ b/nginx_ingest.conf @@ -0,0 +1,22 @@ +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___ -c copy -f flv rtmp://127.0.0.1:1935/live; + } + application live { + allow publish 127.0.0.1; + deny publish all; + live on; + record off; + } + } +} \ No newline at end of file diff --git a/nginx_restream.conf b/nginx_restream.conf new file mode 100644 index 0000000..c4df272 --- /dev/null +++ b/nginx_restream.conf @@ -0,0 +1,16 @@ +worker_processes 1; +events { + worker_connections 1024; +} +rtmp { + server { + listen 1935; + chunk_size 8192; + application origin { + live on; + record off; + meta copy; + ___PUSH_DIRECTIVES___ + } + } +} \ No newline at end of file diff --git a/startscript.sh b/startscript.sh new file mode 100644 index 0000000..11e77fe --- /dev/null +++ b/startscript.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +NGINX_CONF=/etc/nginx/nginx.conf +if test -f "$NGINX_CONF"; then + nginx -g daemon off; + exit 0 +fi + +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 + if [[ $TYPE == "ingest" ]] || [[ $TYPE == "restream" ]]; then + NGINX_TYPE="${TYPE}" + else + echo -e "Envvar TYPE must be either ingest or restream" + exit 1 + fi +fi + +if [[ $NGINX_TYPE == "ingest" ]]; then + 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 -i 's@___STREAMKEY___@'"$NGINX_STREAM_KEY"'@' nginx_ingest.conf > $NGINX_CONF +else + 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 -i 's@___PUSH_DIRECTIVES___@'"$NGINX_PUSH_URLS"'@' nginx_restream.conf > $NGINX_CONF + fi +fi + +nginx -g daemon off; \ No newline at end of file