add encode mode
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Torma Kristóf 2020-09-20 02:49:49 +02:00
parent 0fc0ae1ff7
commit e0a298da5b
6 changed files with 66 additions and 19 deletions

View File

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

View File

@ -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://<yourIP>/origin/<streamKey>`` and exposes the exact same stream at ``rtmp://<yourIP>/live``.
Takes an **RTMP** stream at ``rtmp://<yourIP>/origin/<streamKey>`` and exposes the exact same stream at ``rtmp://<yourIP>/live``. It is active if *TYPE* is set to ``ingest``.
### Restream Mode
The input **RTMP** stream at ``rtmp://<yourIP>/origin/<streamKey>`` will be pushed to the specified **RTMP** endpoints.
The input **RTMP** stream at ``rtmp://<yourIP>/origin/<streamKey>`` 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://<yourIP>/origin/<streamKey>`` 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.

17
config/nginx_encode.conf Normal file
View File

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

View File

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