• 1140
  • More

Speir Live Streaming App NGINX Set Up

Streams 

The streams module allows to creation of live video streams for a wide audience. This module requires a separate media server to be set up, currently 

Nginx and 

OvenMediaEngine are supported

Configuration 

Streaming section 

  • Server Software - live streaming server, for now only 
  • Nginx and 
  • OvenMediaEngine are supported. Nginx doesn't support WebRTC, so you can't broadcast from the browser, for now only OvenMediaEngine supports WebRTC and streaming from the browser.
  • Server Hostname - server hostname without protocol, for example live.example.com
  • Application Name - application name defined in your streaming server configuration
  • Sources Pattern - JSON string with possible sources suitable for 
  • OvenMediaPlayer, there are the following substitution markers are supported:
  • {host} - Server Hostname
  • {app} - Application Name
  • {key} - Streaming Key
  • {params} - Additional Params
  • Enable MPEG-DASH streaming - enable this option if MPEG-DASH sources are specified in the Sources Pattern
  • Enable HLS streaming - enable this option if HLS sources are specified in the Sources Pattern
  • Base URL for recordings (experimental) - when recording is enabled set the base URL where recorded videos can be accessed

NGINX 

Configuration for 

NGINX RTMP module

  • NGINX Stats URL - URL to get statistics about current streaming such as the number of viewers
  • NGINX Authentication - authentication for publishing the stream, 
  • on_publish setting needs to be configured in NGINX, this setting doesn't support 
  • https URLs.
  • NGINX recording folder path - when recording is enabled, set an absolute path to the folder where recorded videos are stored, 
  • on_record_done setting needs to be configured in NGINX, this setting doesn't support 
  • https URLs.

Sample NGINX configuration for HLS streaming (note this configuration will expose your streaming service for everyone):

rtmp {
    server {
        listen 1935;

        application app {
            live on;

            record off; # disable recording

            exec ffmpeg -i rtmp://localhost/app/$name
              -preset ultrafast -c:a aac -s 1280:720  -b:a 128k -c:v libx264 -x264-params keyint=24:no-scenecut=1 -r 24 -b:v 1536k -f flv rtmp://localhost/hls/$name_mid
              -preset ultrafast -c:a aac -s 1920:1080 -b:a 128k -c:v libx264 -x264-params keyint=24:no-scenecut=1 -r 24 -b:v 2048K -f flv rtmp://localhost/hls/$name_hi;

            deny play all;
        }

        application hls {
            live on;

            on_publish http://example.com/m/stream/nginx_on_publish/;

            hls on;
            hls_path /tmp/hls;
            hls_nested on;
            hls_fragment 2s;
            hls_playlist_length 10s;

            hls_variant _mid BANDWIDTH=1664000;
            hls_variant _hi  BANDWIDTH=2176000;
        }
    }
}

To enable recording replace 

record off; with the following:

record all; 
record_path /path/to/recorded/videos;
record_unique on;
on_record_done http://example.com/m/stream/on_record_done/;

Then add a virtual host:

        server {
            listen 80;
            server_name  live.example.com;
            index index.html index.htm;

            root /var/www/html;

            location /app {
                types {
                    application/vnd.apple.mpegurl m3u8;
                    video/mp2t ts;
                }
                add_header Access-Control-Allow-Origin *;
                add_header Cache-Control no-cache;
                alias /tmp/hls;
            }
            location /stat {
                rtmp_stat all;
            }
        }

Sources pattern for the above configuration:

[

    {

        type: "hls"
,

        file: "http://{host}/{app}/{key}_hi/index.m3u8{params}"
,

        label: "HD 1080"

    }
,

    {

        type: "hls"
,

        file: "http://{host}/{app}/{key}_mid/index.m3u8{params}"
,

        label: "HD 720"

    }

]

Optional reverse proxy for 

on_publish and 

on_record_done URLs, in case it needs to be enabled on 

http address and/or other subdomain:

    location /on_publish/ { # or /on_record_done/
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;

        proxy_pass https://example.com/m/stream/nginx_on_publish/; # or https://example.com/m/stream/on_record_done/
        proxy_redirect   off;
    }

for example, if you configure reverse proxy on some.example.com domain then need to change 

on_publish setting:

on_publish http://some.example.com/on_publish/;

and

on_record_done http://some.example.com/on_record_done/;

Additional notes regarding recording actual for both NGINX RTMP module and OvenMediaEngine

  • The recording folder need to be configured in your web server to be publicly accessible
  • Recorded videos need to be pruned manually since videos are copied to UNA so it can be deleted shortly after recording, you can set the following cronjob to delete recordings older than 1 day:
45 * * * * find /path/to/recorded/videos/ -mtime +0 -exec rm -f {} \+
Comments (0)
Login or Join to comment.