Video Streaming with NGINX

  • Published on

  • View

  • Download

Embed Size (px)


<p>PowerPoint Presentation</p> <p>Video Streaming with NGINXIntroduced by Andrew AlexeevPresented by Rick NelsonNginx Inc</p> <p>May 13, 20141</p> <p>About this webinarThe demand for Internet Video continues to grow at a fast pace and from all types of devices. In this webinar, you'll learn about the different methods for streaming video and how some of the advanced features of NGINX and NGINX Plus can be used to meet your video streaming needs.Thank you Andrew. Over the years different methods have been devised to deliver video content. In this webinar we will take a look at some of these methods and then I will talk about how you can use NGINX for video delivery. 2Video Streaming MethodsFile DownloadProgressive DownloadPseudo-StreamingAdaptive Bitrate StreamingRTMP</p> <p>Today the methods I will be talking about are file download, progressive download, pseudo-streaming, adaptive bitrate streaming and RTMP. 3The Evolution of HTTP Video StreamingPseudo-StreamingAdaptive Bitrate StreamingFile DownloadProgressive DownloadIm going to start by discussing how these methods have evolved over time, focusing on HTTP and so for that I will cover file download, progressive download, pseudo streaming and adaptive bitrate streaming.4File Download</p> <p>N</p> <p>InternetVideo ServerContent downloaded to diskPlayback starts after the download completesDownload stored on disk</p> <p>We will start with the simplest way of delivering video content and that is to download the complete media file to your local hard drive and then playing the video. The process is very straight forward. The file is downloaded from the video server to the local machine and once the download completes the video can be played. This means that you have to wait for the entire file to be downloaded before playback can begin and if the file is large or your link is slow this can take a very long time and if you are unlucky and run into connection errors, the download may never complete. So there is obviously room for improvement.</p> <p>5Progressive Download</p> <p>NVideo ServerProgressive DownloadContent is streamed and played as it is downloadedMETAPlayback starts before download completesMeta data at the beginning of the fileDownload stored on diskTrick-play and media flow control not easyProblematic with low bandwidth connections</p> <p>An improvement over simply downloading a file is progressive download. In this scenario, the meta data for the media file (that is the information needed by the video player to know how to play the video) is downloaded first, then the download of the rest of the file begins and after a certain amount of data is buffered by the player it is able to start playing the video. The file will continue to download and as long as the file downloads faster then the video plays, you will experience smooth playback. If your link is slow, then the playback may stall while the player waits for more data to be buffered. Im sure you have all encountered this phenomena. So this method does not handle slow connections well and you arent able to use trick play features such as fast forward, jumping to a certain part of the video, etc. </p> <p>The key advantage to this approach over file download is that you dont have to wait for the download to complete before playing the video so the video can be played while the file is still being downloaded. This is made possible by moving the meta data to the front of the file so the player has the information it needs to play the file. So this a step in the right direction, but still is not ideal.</p> <p>6Pseudo-StreamingVideo ServerPseudo-streamingClient can abort stream and request new byte-rangeMETAxx</p> <p>NAdds trick-play to progressive downloadClient can request partial contentContent can be buffered by player </p> <p>Pseudo-streaming is an advance on simple progressive download in that the player can ask for specific byte ranges, thus enabling some trick play features and also the player can buffer the data without necessarily having to write the data to disk.</p> <p>The download no longer has to be continuous from the start to the end of the file. The player can now stop the stream and move to a new point in the data.7Adaptive Bitrate StreamingVideo ServerAdaptive StreamingClient downloads manifest, then video chunks of appropriate bitrate MANIFEST</p> <p>NData is sent as a continuous stream of small chunksSupports trick-play capabilitiesSupports variable bitrates supportedEasily change bitrates while streaming Less buffering on the clientBetter restoration from network interruptions</p> <p>A big step forward came with the advent of adaptive bitrate streaming. This delivers the media in a different way then the progressive download and pseudo-streaming methods we have been talking about. Here a media file is repackaged as a series of smaller segments, each with just a few seconds of video. A playlist or manifest file is then created that acts as a table of contents for the segments. What adaptive bitrate refers to is that there can be different versions of the same video, each encoded with a different bitrate. Think of a having a high definition and low definition version of the same video and even versions in between. The high definition version would be appropriate for a high bandwidth link and the low definition version appropriate for a low bandwidth link. With adaptive bitrate streaming the player can switch seamlessly between bitrates depending on the speed of the connection and can easily skip forward or backward through the segment files. By being able to adapt to network conditions using different bitrates the client can see a better quality of service, and the player can also more easily recover from network interruptions.</p> <p>8Adaptive Bitrate StreamingHTTP Live Streaming (HLS)From ApplePackage MP4 encoded file to:Playlist (.m3u8)Segment files (.ts)HTTP Dynamic Streaming (HDS)From AdobePackage MP4 encoded file to:Manifest (.f4m)Index (.f4x)Fragment(.f4f)</p> <p>Now lets look at two popular ways of doing adaptive bitrate streaming over HTTP and how they work. HTTP Live Streaming, or HLS, was developed by Apple. With HLS, the mp4 file is re-packaged into a playlist with the extension of m3u8 and a series of short segment files with an extension of ts. The playlist is used to identify the segments. A player first requests the playlist and then streams the segment files. HTTP Dynamic Streaming, or HDS, comes from Adobe and also requires that the content to be re-packaged, but in this case into a manifest file, an index file and a fragment file. If you have encoded the video with multiple bitrates, then you will have a master playlist or manifest file that points to a playlist or manifest file for each bitrate.9HLS File StructureHopefully this diagram will clarify things. This shows the file structure used by HLS, assuming there are 2 bitrates available. The master playlist points to a playlist for each bitrate and these playlists point to the segment files. I only show the first 2 segment files. The number of segment files will depend on the length of the segments and the total length of the video. If you only have a single bitrate, then there would be no master playlist.10HDS File StructureHere we see the file structure for HDS. Again I have assumed 2 bitrates. So there is a master manifest that points to the manifest for each bitrate. These manifest then point to an index which points to the fragment file. Again if you have only a single bitrate there will not be a master manifest.11Real Time Messaging Protocol (RTMP) TCP-based streaming protocolDeveloped by MacroMedia Now AdobeLive and VOD streamingWide desktop browser support via Flash pluginSupported by a variety of software</p> <p>Another way of doing video streaming that doesnt use HTTP, is to use the Real Time Messaging Protocol or RTMP. This can support both live video and video on demand and is supported across various desktop browsers by means of a Flash plugin, and it is also supported by many other software programs.12Introducing NGINXNow that we covered a bit about video streaming in general, lets take a look at how you can use NGINX to support your video streaming requirements.13What is NGINX?Internet</p> <p>N</p> <p>Web ServerServe content from disk</p> <p>Application ServerFastCGI, uWSGI, Passenger</p> <p>ProxyCaching, Load BalancingHTTP trafficApplication AccelerationSSL and SPDY terminationPerformance MonitoringHigh AvailabilityAdvanced Features:Bandwidth ManagementContent-based RoutingRequest ManipulationResponse RewritingAuthenticationMail ProxyGeo LocationHLSHDSRTMPMP4/FLVMP4 BandwidthFirst off, what is NGINX? NGINX is a high performance web server of static content, including video and also a full reverse proxy or Application Delivery Controller with many advanced features. For dynamic content it can connect to application servers over HTTP, FastCGI, uWSGI, Passenger and other methods.14146,000,000WebsitesNGINX AcceleratesStarted as on open source project about 10 years ago, NGINX has gained a justified reputation as a high performance web and proxy server and is seeing widespread adoption, now powering over 140 million websites.1523%Top 1 million websites38%Top 1,000 websitesIncluding over 20% of the top 1 million busiest websites and almost 40% of the top 1,000 busiest websites, which now makes NGINX the most used web server for the top 1,000 websites.16NGINX and NGINX PlusNGINX F/OSS</p> <p>nginx.org3rd party modulesLarge community of &gt;100 modulesAs I said, NGINX started out as an open source project and that continues today, but in August of last year we released the first commercial version of NGINX, NGINX Plus, built on top of the open source version.17NGINX and NGINX PlusNGINX F/OSS</p> <p>nginx.org3rd party modulesLarge community of &gt;100 modulesNGINX Plus</p> <p>Advanced load balancing featuresEase-of-managementCommercial supportNGINX Plus brings advanced features, mainly around enterprise level load balancing and video streaming and also comes with support. As we go through this presentation, we will talk about some of the NGINX Plus features specific to video streaming.18NGINX PlusNGINX Plus builds:NGINX PlusNGINX Plus LuaNGINX Plus ExtrasNGINX Plus Streaming Media Server </p> <p>There are four builds of NGINX Plus that are available. NGINX Plus is the core build and all the code in this build has been developed by the Nginx team in Moscow, the same team that develops the open source version, and is fully supported. The NGINX Plus Lua build adds the popular 3rd party module that allows you to do scripting in the Lua programming language. The NGINX Plus Extras build adds several additional 3rd party modules. These 3rd party modules are supported on a best effort basis. And finally there is the NGINX Plus Streaming Media Server. This is a stripped down version of NGINX Plus, with just those features needed for video streaming.19Video streaming with NgINX PlusSo now lets talk about how you can use NGINX to do video streaming.20NGINX Plus and HTTP Live Streaming (HLS)ngx_http_hls_moduleNGINX creates playlist and segment files on-the-flyAllows for existing mp4 files to be served by HLSNo need to re-package contentSave time and storage space</p> <p>location /hls { hls;}Well start with HLS. NGINX Plus includes a HLS module. You may remember that when I talked about HLS previously I mentioned that the content needs to be re-packaged into a playlist and segment files. One of the nice features of the NGINX Plus HLS module is that it can do this re-packaging for you, on-the-fly. So you can take your existing mp4 files and start serving them with HLS without having to do anything to the files. Just point NGINX Plus at your mp4 files and NGINX Plus will do the rest. If you want to support multiple bitrates, then all you need to do is create a master playlist pointing to the playlist for each bitrate. These playlist files wont actually exist, but NGINX will create them automatically at run time.21Demo: NGINX HLSNow I will start showing demos using NGINX to deliver video using some of the methods I have been talking about. First, lets see HLS in action. You will see that if we look in the /mp4 directory, we just have the file catanddog.mp4, but if we go to our browser and enter catanddog.mp4.m3u8, which is the URI of the playlist, we see our video even though that file doesnt exist. This is because NGINX created it on the fly. If we look in the access log, we will see the request for the m3u8 file along with the segment files, which were also created on-the-fly by NGINX.</p> <p>Show /mp4Browser http://vidtest/mp4/catanddog.mp4.m4u8Show /etc/nginx/logs/hls.access.log</p> <p>22NGINX Plus and HTTP Dynamic Streaming (HDS)ngx_http_f4f_moduleManifest, index and segment files created by Adobe tools such as f4fpackager</p> <p>location /hds { f4f;}Now lets move on to HDS. HDS is a different then HLS in that the mp4 encoded files must be re-packaged with an Adobe tool, like f4fpackager. Once this is done the f4f module that comes with NGINX Plus will handle the HDS streaming requests.23Demo: NGINX HDSIf we look in the hds directory where I have put the HDS files, we will see the manifest, index and segment file. To use HDS we need to embed a flash player in a webpage, which is what we have done here and we see the same video we saw during the HLS demo. If we look at the access log for these requests we will see many request for cat1-Frag1, cat1-Frag2 and so on and the HDS module has the logic to know how to handle those requests.</p> <p>Show /hdsBrowser http://test-f4f.htmlShow /etc/nginx/logs/hds.access.log</p> <p>24NGINX and HTTP Pseudo-Streamingngx_http_mp4_moduleBandwidth can be controlled (NGINX Plus)location /video { mp4; mp4_limit_rate 1.2; mp4_limit_rate_after 15s;}Now, adaptive bitrate streaming is great and Ive talked about some of its advantages, but not all client devices may support it, so you may still want to support pseudo-streaming and that is something supported in NGINX open source, but NGINX Plus adds the ability to control the bandwidth used by a client over a connection. mp4_limit_rate is multiplied by the bitrate, so in this example we will allow 1.2 times the encoded bitrate for each connection. mp4_limit_rate_after tells NGINX Plus to delay starting to limit the bandwidth for the specified time after the download begins. So in this example, 15 seconds. This allows the client to buffer an initial amount of video before before the limit kicks in.25Demo: NGINX Pseudo-StreamingAs we saw when we looking in the /mp4 directory, we had just the one mp4 file. For the previous demo, we requested the playlist, but if request the file directly, then the video will be played using pseudo-streaming.</p> <p>Browser http://vidtest/mp4/catanddog.mp4</p> <p>26NGINX Plus and RTMP Streamingnginx-rtmp-moduleRTMP live and VOD streamingStream transcoding with external software (ffmpeg, avconv)HTTP callbacks on streaming eventsStream relay: push and pullOne alternati...</p>