Search code examples
nginxstreamhttp-live-streamingrtmpnode-media-server

Retrieve HLS data from another stream and push it to rtmp server


I have been working on a project where I need to be able to push dynamically to 2 or 3 other channels. I tested a couple of scenarios, my last attempt is as following:

Stream Server

worker_processes auto;
rtmp_auto_push on;
events {}
rtmp {
    server {
        listen 1935; # Listen on standard RTMP port

        application live {
            live on;
            hls on;
            hls_path /www/tmp/hls;
            hls_fragment 10s; # default is 5s
            hls_playlist_length 5m; # default is 30s
            # once playlist length is reached it deletes the oldest fragments

            # authentication
            # on_publish => some other server
            # on_done => some other server
        }
    }
}

http {
    server {
        listen 8080;

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                application/octet-stream ts;
            }
            root /www/tmp;
            add_header Cache-Control no-cache;

            # To avoid issues with cross-domain HTTP requests (e.g. during development)
            add_header Access-Control-Allow-Origin *;
        }
    }
}

and then force another server to read from the hls content created by this server. I tried with node-media-server and nginx but neither had a real solution on this, here is my attempt on the nginx one:

events {}
rtmp {
    server {
        listen 1935; # Listen on standard RTMP port
        chunk_size 4000; 
        # ping 30s;
        # notify_method get;

        # This application is to accept incoming stream
        application live {
            live on; # Allows live input
            deny play all; # disable consuming the stream from nginx as rtmp

            hls on; # Enable HTTP Live Streaming
            hls_fragment 3;
            hls_playlist_length 10;
            hls_path /www/tmp/hls;  # hls fragments path
                        
            # MPEG-DASH
            dash on;
            dash_path /mnt/dash/;  # dash fragments path
            dash_fragment 3;
            dash_playlist_length 10;    
            push => another rtmp server;
        }
    }
}

http {
    server {
        listen 8080;

        location / {
            root /www;
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                application/octet-stream ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;

            # To avoid issues with cross-domain HTTP requests (e.g. during development)
            add_header Access-Control-Allow-Origin *;
        }
    }
}

If you know any other options please let me know or have any other suggestions, thank you


Solution

  • Do you want to do this as bellow:

    Client -----> Nginx -------> Server
             (Stream 1/2/3)    (Stream 2)
    

    You should never let another server to read Stream 2 from Nginx, intead you should use a FFmpeg as a client to do this:

    Client -----> Nginx -----> FFmpeg  --> Server
             (Stream 1/2/3)             (Stream 2)
    

    To do this, please run command like this:

    ffmpeg -f flv -i rtmp://ip/live/stream2 -c copy -f flv rtmp://server/live/stream2
    

    So you can read any stream from Nginx to another server.

    If you want the Server to do this, you can use SRS/Ingest, which fork a FFmpeg process to pull the stream to SRS