Streaming Media delivery using NGINX Plus

Streaming Media delivery using NGINX Plus

NGINX is widely used to deliver MP4 and FLV video content using progressive downloads or HTTP pseudo-streaming. Progressive downloads rely on the efficiency of the streaming server to handle disk I/O and concurrent connections, while MP4 or FLV pseudo-streaming capabilities also allow the clients to use a simple “trick-play” technique to seek to the desired position in a video stream without needing to download the entire resource.

NGINX Plus and NGINX Media Server extend this capability to support adaptive streaming for video-on-demand (VOD) applications with Apple HLS and Adobe HDS, and Flash-based services using RTMP. Among other benefits, adaptive streaming allows the video player to select the most appropriate bit-rate in real-time.

NGINX Plus and NGINX Media Server can also impose smart bandwidth limits on individual MP4 streams to prevent fast clients and download accelerators from using unnecessary resources, and the enhanced session logging capabilities of NGINX Plus reduce the overhead of the standard HTTP request logging with HTTP streaming media.

Delivering Streaming Media with NGINX Plus

HLS video on demand

The HLS/VOD module in NGINX Plus provides HLS support for H.264/AAC encoded content packaged in MP4 file container (filename extensions of .mp4, m4v, or m4a). With the HLS/VOD module in NGINX Plus, there’s no need to re-package the already existing MP4 content when introducing adaptive streaming to the users. The same content can be “transformed” or “transmultiplexed” on-the-fly from the MP4 file container to HLS. The NGINX Plus module performs real-time segmentation, packetization and multiplexing from the MP4 file container to HLS/MPEG-TS without re-coding the content.

Before clients begin downloading media segments, they first request an m3u8 manifest. The HLS module in NGINX Plus generates the playlist on-the-fly, so you do not need to manually describe the segments structure.

You can configure NGINX to serve HLS streams from a particular location using the following configuration:

location /hls/ {
    hls;  # Use the HLS handler to manage requests

    # serve content from the following location
    alias /var/www/video;

    # HLS parameters
    hls_fragment  	        8s;
    hls_buffers         	10 10m;
    hls_mp4_buffer_size 	1m;
    hls_mp4_max_buffer_size 5m;

HLS clients will first request the m3u8 manifest for a file in the location, and will then begin downloading the segments of video specified in the file. Again, NGINX Plus will handle the task of segmenting MP4-packaged content on-the-fly for streaming with HLS.

In addition, the HLS functionality can be used along with NGINX’ secure link module to generate authorized, time-limited links based on unique client data such as a cookie or source IP address. This provides a strong degree of protection against misuse of the video service.

HDS adaptive streaming

Adobe’s HTTP Dynamic Streaming (HDS) provides an alternative method to delivering adaptive streaming media to your users. It functions in a similar fashion to Apple’s HLS, but uses different file formats.

Video is first prepared using Adobe’s f4fpackager tool, to generate f4m (manifest), f4f (fragment) and f4x (index) files. These files are then published to the webserver, and NGINX Plus’ f4f handler is used to deliver them.

location /hds/ {
    f4f;  # Use the HDS handler to manage requests

    # serve content from the following location
    alias /var/www/video;

Session Log Aggregation

Video clients that access content using HDS or HLS will issue a great many HTTP requests for the video fragments. The resulting request log will be very verbose and difficult to analyse.

If you wish to log streaming media downloads, NGINX Plus’ Session Log Aggregation feature can be used to generate more concise logs that combine multiple HTTP requests into a single log file entry. All of the requests in a single time-limited session are combined, and the total data transferred is logged. All other parameters are taken from the first request in the session.

You can enable session logging for requests to the media location and use standard logging for all other client requests to preserve the detail in your request logs.

MP4 bandwidth control

NGINX Plus can apply limits to HTTP traffic downloads using the limit_rate and limit_rate_after directives. These directives define limits in terms of bandwidth (bytes per second).

NGINX Plus also offers smart limits for MP4 content. The mp4_limit_rate and mp4_limit_rate_after directives define bandwidth limits in terms of the bit-rate of the downloaded file, and the time after which to apply the bandwidth limit.

For example the following configuration will allow MP4 files to stream without limit for 15 seconds to let clients read-ahead, and will then apply a bandwidth limit of 120% the bit-rate of the MP4 file.

location /video/ {
    mp4_limit_rate        1.2;
    mp4_limit_rate_after  15s;

This helps to avoid excessive wastage of bandwidth resources when clients use ‘trick-play’ capabilities to skip forwards through video files, and it reduces the impact of video download accelerators.