Chapter 9. Sophisticated Media Streaming

Introduction

This chapter covers streaming media with NGINX in MPEG-4 or Flash Video formats. NGINX is widely used to distribute and stream content to the masses. NGINX supports industry-standard formats and streaming technologies, which will be covered in this chapter. NGINX Plus enables the ability to fragment content on the fly with the HTTP Live Stream module, as well as the ability to deliver HTTP Dynamic Streaming of already fragmented media. NGINX natively allows for bandwidth limits, and NGINX Plus’s advanced features offers bitrate limiting, enabling your content to be delivered in the most efficient manner while reserving the servers’ resources to reach the most users.

Serving MP4 and FLV

Problem

You need to stream digital media, originating in MPEG-4 (MP4) or Flash Video (FLV).

Solution

Designate an HTTP location block as .mp4 or .flv. NGINX will stream the media using progressive downloads or HTTP pseudostreaming and support seeking:

http {
    server {
        ...
        
        location /videos/ {
            mp4;
        }
        location ~ .flv$ {
            flv;
        }
    }
} 

The example location block tells NGINX that files in the videos directory are in MP4 format type and can be streamed with progressive download support. The second location block instructs NGINX that any files ending in .flv are in FLV format and can be streamed with HTTP pseudostreaming support.

Discussion

Streaming video or audio files in NGINX is as simple as a single directive. Progressive download enables the client to initiate playback of the media before the file has finished downloading. NGINX supports seeking to an undownloaded portion of the media in both formats.

Streaming with HLS

Problem

You need to support HTTP Live Streaming (HLS) for H.264/AAC-encoded content packaged in MP4 files.

Solution

Utilize NGINX Plus’s HLS module with real-time segmentation, packetization, and multiplexing, with control over fragmentation buffering and more, like forwarding HLS arguments:

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

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

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

The location block demonstrated directs NGINX to stream HLS media out of the /var/www/video directory, fragmenting the media into four-second segments. The number of HLS buffers is set to 10 with a size of 10 megabytes. The initial MP4 buffer size is set to 1 MB with a maximum of 5 MB.

Discussion

The HLS module available in NGINX Plus provides the ability to transmultiplex MP4 media files on the fly. There are many directives that give you control over how your media is fragmented and buffered. The location block must be configured to serve the media as an HLS stream with the HLS handler. The HLS fragmentation is set in number of seconds, instructing NGINX to fragment the media by time length. The amount of buffered data is set with the hls_buffers directive specifying the number of buffers and the size. The client is allowed to start playback of the media after a certain amount of buffering has accrued specified by the hls_mp4_buffer_size. However, a larger buffer may be necessary as metadata about the video may exceed the initial buffer size. This amount is capped by the hls_mp4_max_buffer_size. These buffering variables allow NGINX to optimize the end-user experience; choosing the right values for these directives requires knowing the target audience and your media. For instance, if the bulk of your media is large video files, and your target audience has high bandwidth, you may opt for a larger max buffer size and longer length fragmentation. This will allow for the metadata about the content to be downloaded initially without error and your users to receive larger fragments.

Streaming with HDS

Problem

You need to support Adobe’s HTTP Dynamic Streaming (HDS) that has already been fragmented and separated from the metadata.

Solution

Use NGINX Plus’s support for fragmented FLV files (F4F) module to offer Adobe Adaptive Streaming to your users:

location /video/ {
    alias /var/www/transformed_video;
    f4f;
    f4f_buffer_size 512k;
}

The example instructs NGINX Plus to serve previously fragmented media from a location on disk to the client using the NGINX Plus F4F module. The buffer size for the index file (.f4x) is set to 512 kilobytes.

Discussion

The NGINX Plus F4F module enables NGINX to serve previously fragmented media to end users. The configuration of such is as simple as using the f4f handler inside of an HTTP location block. The f4f_buffer_size directive configures the buffer size for the index file of this type of media.

Bandwidth Limits

Problem

You need to limit bandwidth to downstream media streaming clients without impacting the viewing experience.

Solution

Utilize NGINX Plus’s bitrate-limiting support for MP4 media files:

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

This configuration allows the downstream client to download for 15 seconds before applying a bitrate limit. After 15 seconds, the client is allowed to download media at a rate of 120% of the bitrate, which enables the client to always download faster than they play.

Discussion

NGINX Plus’s bitrate limiting allows your streaming server to limit bandwidth dynamically based on the media being served, allowing clients to download just as much as they need to ensure a seamless user experience. The MP4 handler described in “Serving MP4 and FLV” designates this location block to stream MP4 media formats. The rate-limiting directives, such as mp4_limit_rate_after, tell NGINX to only rate-limit traffic after a specified amount of time, in seconds. The other directive involved in MP4 rate limiting is mp4_limit_rate, which specifies the bitrate at which clients are allowed to download in relation to the bitrate of the media. A value of 1 provided to the mp4_limit_rate directive specifies that NGINX is to limit bandwidth (1-to-1) to the bitrate of the media. Providing a value of more than one to the mp4_limit_rate directive will allow users to download faster than they watch so they can buffer the media and watch seamlessly while they download.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.173.227