HLS video streaming
Nowadays, video streaming is very much used by people over the internet. They use it for watching movies online, musical video clips or live TV programs on their computer or smartphones. They also use it for sharing their own home videos of themselves, for example teaching something, playing video games, etc.
What is HLS?
Apple’s HTTP Live Streaming (HLS) is a protocol for streaming video using an ordinary web server. It supports live and on-demand streaming, as well as the ability to automatically adapt the stream quality to device and network conditions. The protocol works as follows:
- An existing file or live stream is segmented before delivery into short files.
- These segments are stored on a standard web server and individually requested by a video player.
- During playback, the player seamlessly stitches the segments together.
If multiple qualities of a stream are available, the player will continuously monitor the current bandwidth and pick the next fragment from the highest quality it can load. This is called Adaptive Streaming.
The main purpose of adaptive streaming is to make the video play smoothly and with the less quantity of freezes. Its benefits are that the video is not going to freeze due to the low current bandwidth. But you may probably watch a loss of quality in the video if your internet bandwidth is poor.
However, there is another new protocol called Mpeg Dash and it is a standard. Apple devices like Iphones or MacBook computers do not support the mpeg dash protocol yet.
So, if you want people to watch your videos using any computer OS or smartphones including Apple devices, you should use HLS.
How HLS Works
At a high level, HLS works like all adaptive streaming technologies; you create multiple files for distribution to the player, which can adaptively change streams to optimize the playback experience.
As an HTTP-based technology, no streaming server is required, so all the switching logic resides on the player.
To distribute to HLS clients, you encode the source video into multiple files at different data rates and divide them into short chunks, usually between 5-10 seconds long. These are loaded onto an HTTP server along with a text-based manifest file with a .M3U8 extension that directs the player to additional manifest files for each of the encoded streams.
Figure 1. HLS uses multiple encoded files with index files directing the player to different streams and chunks of audio/video data within those streams.
The player monitors changing bandwidth conditions. If this determines a stream change, the player checks the original manifest file for the location of additional streams, and then the CHECK THIS stream-specific manifest file for the URL of the next chunk of video data. Stream switching is generally seamless to the viewer.
How to serve an HLS video
HLS File Preparation
HLS currently supports H.264 video. Audio can be HE-AAC or AAC-LC up to 48 khz, stereo. The individual manifest files detail the profile used during encoding so the player will only select and retrieve compatible streams.
Audio/video streams must be segmented into chunks in a MPEG-2 transport stream with a .ts extension. All files are then uploaded to an HTTP server for deployment. In a live scenario, the .ts chunks are continuously added and the .M3U8 manifest files continually updated with the locations of alternative streams and file chunks.
Deploying HLS Streams
Delivery via HTTP has CHEC advantages: no streaming server is required and the audio/video chunks should leverage HTTP caching servers located in the premises of internet service providers, cellular providers, and other organizations, which should improve video quality for viewers served from these caches. HTTP content should also pass through most firewalls.
Apple recommends using the HTML5 video tag for deploying HLS video on a website.
On the Playback Side
On computers and iPad devices, the Safari browser can play HLS streams within a web page, with Safari launching a full-screen media player on iPhones and iPod touch devices. Starting with version 2, all Apple TV devices include an HTTP Live Streaming client.
For Other OS and Android devices you can use an HLS player in your web page. There are a lot of them for you to add to your website, like JWPlayer or Viblast player.
The HLS protocol has two components; a set of chunked files in .ts format and the required manifest files. In an on-demand environment, you can encode the alternative files using any standalone H.264 encoding tool, like ffmpeg or any other. Cloud encoding services like those provided by Encoding.com can also typically produce HLS-compatible files.
Once you have the encoded streams, you can use Apple tools to create the chunked files and playlists. Here are the HLS tools available for download from Apple
For live HLS distribution, you need an encoding tool that can encode the files into H.264 format, create the MPEG-2 transport stream chunks and create and update the manifest files. Now most vendors of encoding hardware also offer live HLS-compatible products, including Digital Rapids,Elemental Technologies, Haivision, Seawell Networks, and ViewCast.
Real Time Transmuxing
The other approach to live or on-demand streaming to HLS-compatible players is via transmuxing, which is offered by multiple streaming server vendors and CDNs. Specifically, these servers input an H.264-stream originally compatible with Flash or Silverlight (or other formats) and then dynamically re-wrap the file into the required MPEG-2 transport stream chunks and create the required manifest files.
Server-based implementations include:
Akamai also offers “in the network” repackaging of H.264 input files for HLS deployment.
Nowadays the iOS, Android, Linux and Windows platforms are the critical targets for virtually all streaming publishers, and HLS can deliver the best possible experience to those platforms, and others that support HLS playback. Fortunately, the streaming industry has several HLS tools and technologies that make this very simple and affordable.