streaming

How to stream to multiple servers simultaneously

Server

Install Docker

To install Docker, you need to add Docker’s official GPG key and set up the Docker stable repository.

install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo   "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" |   tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt update --allow-insecure-repositories
apt --allow-unauthenticated install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Set up Nginx + RTMP Module + Stunnel

Create a configuration file for the RTMP server (rtmp.conf) and a script to run the Docker container (run.sh).

Dockerfile

# Ubuntu is the base OS
FROM ubuntu:latest

# Install dependencies
RUN apt-get update && \
    apt-get install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev wget unzip stunnel4 gettext

# Download and unpack the Nginx source code
RUN wget http://nginx.org/download/nginx-1.19.0.tar.gz && \
    tar -zxvf nginx-1.19.0.tar.gz && \
    rm nginx-1.19.0.tar.gz

# Download and unpack the RTMP module source code
RUN wget https://github.com/arut/nginx-rtmp-module/archive/master.zip && \
    unzip master.zip && \
    rm master.zip

# Build Nginx with the RTMP module
WORKDIR nginx-1.19.0
RUN CFLAGS=-Wno-error ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master && \
    make && \
    make install

# Expose the ports
EXPOSE 1935 8080

# Start the Nginx service and stunnel
CMD ["/bin/bash", "-c", "envsubst < /etc/stunnel/stunnel.conf.template > /etc/stunnel/stunnel.conf && /usr/local/nginx/sbin/nginx && stunnel /etc/stunnel/stunnel.conf"]

rtmp.conf

worker_processes  1;

events {
    worker_connections 1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;

            # Push to an output with a stream key
            push rtmp://localhost:19350/rtmp/xxxxxxxxxxxxxxxxx?s_bl=1&s_fbp=lhr6-1&s_prp=ams2-1&s_sw=0&s_tids=1&s_vt=ig&a=xxxxxxxxxxxxxxxxxxxxxxxx;
        }
    }
}


http {
    sendfile off;
    tcp_nopush on;
    directio 512;
    default_type application/octet-stream;

    server {
        listen 8080;

        location / {
            # Disable cache
            add_header 'Cache-Control' 'no-cache';

            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';

            # allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            types {
                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /mnt/;
        }
    }

    error_log /dev/stderr;
    access_log /dev/stdout;
}

run.sh

iptables -A INPUT -p tcp --dport 1935 -j ACCEPT

docker build -t my_nginx_rtmp .

docker run -p 1935:1935 -p 19350:19350 -v ./rtmp.conf:/usr/local/nginx/conf/nginx.conf -v ./stunnel.conf.template:/etc/stunnel/stunnel.conf.template -e RTMPS_SERVER=edgetee-upload-ams2-1.xx.fbcdn.net -e RTMPS_PORT=443 my_nginx_rtmp

stunnel.conf.template

foreground = yes
pid = /stunnel.pid

[rtmps]
client = yes
accept = 19350
connect = ${RTMPS_SERVER}:${RTMPS_PORT}

Step 4: Run the contianer

chmod +x run.sh
./run.sh

Client

Install necessary packages

First, you need to install FFmpeg and Docker. FFmpeg is a tool that can capture your desktop and stream it to an RTMP server. Docker is used to run the RTMP server in a container.

sudo apt update
sudo apt install ffmpeg
sudo apt install ca-certificates curl gnupg lsb-release

Stream your desktop

Before you start streaming, you need to find out your screen resolution. You can do this with the xdpyinfo command.

xdpyinfo | grep dimensions

Then, you can use FFmpeg to capture your desktop and stream it to the RTMP server. Replace 1366x768 with your actual screen resolution, and replace rtmps://163.5.182.40:1935/rtmp with your actual RTMP server URL.

ffmpeg -f x11grab -s 1366x768 -framerate 5 -i :0.0 -f flv rtmp://163.5.182.40:1935/live

That’s it! You’re now streaming your desktop to multiple servers simultaneously.


More detailed docs and contribute:

This repo is powered by Gordarg’s docs generator.