first commit

This commit is contained in:
Torma Kristóf 2020-09-19 21:46:46 +02:00
commit 0fdfb90970
5 changed files with 168 additions and 0 deletions

45
Dockerfile Normal file
View File

@ -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"]

36
README.md Normal file
View File

@ -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://<yourIP>/origin/<streamKey>`` and exposes the exact same stream at ``rtmp://<yourIP>/live``.
### Restream Mode
The input **RTMP** stream at ``rtmp://<yourIP>/origin/<streamKey>`` 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.

22
nginx_ingest.conf Normal file
View File

@ -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;
}
}
}

16
nginx_restream.conf Normal file
View File

@ -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___
}
}
}

49
startscript.sh Normal file
View File

@ -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;