Tuesday, July 11, 2017

Introducing nginx-ts-module for HLS and MPEG-DASH live streaming

Today I'm announcing the first release 0.1.0 of nginx-ts-module. The module provides HLS and MPEG-DASH live streaming capabilities for those who want a lightweight solution based on the HTTP protocol. The stream is published in the MPEG-TS format over HTTP. This makes it possible to use all the power and flexibility of nginx HTTP configurations including SSL, access control, logging, request limiting etc. MPEG-TS is a widely adopted, well known and well documented streaming format.

Currently, the module supports three directives: ts, ts_hls, ts_dash. The first directive enables receiving the MPEG-TS stream. The following two directives enable generating HLS and MPEG-DASH files.

Example:

http {
    server {
        listen 8000;

        location /publish/ {
            # This directive sets unlimited request body size
            client_max_body_size 0;

            ts;
            ts_hls path=/var/media/hls segment=10s;
            ts_dash path=/var/media/dash segment=10s;
        }

        location /play/ {
            types {
                application/x-mpegURL m3u8;
                application/dash+xml mpd;        
                video/MP2T ts;
                video/mp4 mp4;
            }
            alias /var/media/;
        }
    }
}

To publish a stream, invoke ffmpeg like this:

$ ffmpeg -re -i ~/Movies/sintel.mp4 -bsf:v h264_mp4toannexb
         -c copy -f mpegts http://127.0.0.1:8000/publish/sintel

HLS can be played primarily in Safari and mobile devices using the following HTML:

<body>
  <video width="640" height="480" controls autoplay
         src="http://127.0.0.1:8000/play/hls/sintel/index.m3u8">
  </video>
</body>

MPEG-DASH is now supported on most browsers including Chrome, Firefox, Safari. To play the stream, the dash.js player can be used like this:

<script src="http://cdn.dashjs.org/latest/dash.all.min.js"></script>

<body>
  <video data-dashjs-player
         width="640" height="480" controls autoplay
         src="http://127.0.0.1:8000/play/dash/sintel/index.mpd">
  </video>
</body>

For more details refer to the README.rst file in the project root.

No comments:

Post a Comment