HTTP Live Streaming (HLS) is an HTTP-based adaptive bitrate streaming communications protocol developed by Apple Inc.
Adaptive bitrate streaming means that the client media software dynamically detects the Internet speed of the user, and then adjusts video quality in response.
Support for this protocol is widespread in media players, web browsers, mobile devices, and streaming media servers.
Technical Overview Of HLS Streaming
With normal HTTP streaming the HLS protocol separates the video file into smaller segments with the “.ts” file extension (MPEG2 Transport Stream).
Since the HLS is an adaptive bitrate protocol, the bitrates must accommodate and adapt to the channel bandwidth and other parameters of the media player.
The video delivery starts by delivering a single file called Manifest File (master.m3u8) that contains links to the index files. The index files contain a playlist with references to the actual video segments.
Based on the bandwidth of the currently available connection of the user, the HLS allows the media player to adjust the video to the best possible quality.
HLS streaming format
The video files need to be encoded with H.264(AVC) or with the H.265(HEVC) compression algorithms.
Implementation
The implementation of the HLS requires creating a CDN zone for big objects (they are such by default) and generating a .m3u8 index file that contains links to the corresponding .ts segments of the file.
This is done by prefixing ‘/hls’ to the path and appending ‘/index.m3u8’ at the end. Please note that you need to upload only your full .mp4 files and segmentation is done by the CDN.
Example:
mp4_file: /video/012345.mp4
m3u8_path: /hls/video/012345.mp4/index.m3u8
The returned .m3u8 index file contains the corresponding segments for the .mp4 file. The default values set up for the first segment and the other segments are 5 and 10 seconds respectively. You can change them by using parameters bsd (first few segments) and sd (other segments) in the URL.
Example:
mp4_file: /video/012345.mp4
m3u8_path: /hls/bsd/10000-10000-15000/sd/20000/video/012345.mp4/index.m3u8
In the example above the value set up for the bsd parameter of the initial segment is 10 seconds, the second is set up again to 10 seconds, and the third is set up to be 15 seconds. The rest of the segments are set up with the sd parameter to be 20 seconds. Note that the values should be in milliseconds.
It is important to specify the relative paths of a segment as per the specifications on the documentation at https://tools.ietf.org/html/draft-pantos-http-live-streaming-17. Meaning that the absolute path for segment ‘seg-5-v1-a1.ts’ is /hls/video/012345.mp4/seg-5-v1-a1.ts, not /seg-5-v1-a1.ts
URLSET
Furthermore, there is functionality to build a playlist from multiple files. It is used usually when for a given video source there are multiple resolutions and you would like the users to be able to switch between them without changing the page.
Example:
Let’s say that we have the previous file /video/012345.mp4 in 4 resolutions 144p, 240p, 360p and 480p and we have named them /012345_144p.mp4, /012345_240p.mp4, /012345_360p.mp4 and /012345_480p.mp4 respectively.
The URI must be set up as shown below:
/hls/video/012345_,144,240,360,480,720,p.mp4/urlset/master.m3u8
Where the two common parts are “012345_” and “p.mp4” and the bitrates – 144,240,360,480 are divided by commas:
/hls/video/012345_,24,36,48,0p.mp4/urlset/master.m3u8
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=368981,RESOLUTION=426x240,FRAME-RATE=24.000,CODECS="avc1.4d4015,mp4a.40.2"
/hls/012345_144p.mp4/index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=368981,RESOLUTION=426x240,FRAME-RATE=24.000,CODECS="avc1.4d4015,mp4a.40.2"
/hls/012345_240p.mp4/index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=460061,RESOLUTION=640x360,FRAME-RATE=24.000,CODECS="avc1.4d401e,mp4a.40.2"
/hls/012345_360p.mp4/index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=789049,RESOLUTION=854x480,FRAME-RATE=24.000,CODECS="avc1.4d401e,mp4a.40.2"
/hls/012345_480p.mp4/index.m3u8
The Manifest File (master.m3u8) contains playlists (index.m3u8) for each of the provided files along with additional information for each stream included in playlists.
The Manifest File ends with master.m3u8, not with index.m3u8 as it is with the playlist for a single file.
It should be also noted that the playlist inside the master playlist has the same URI as the one that can be used for the individual URIs.
VTT Subtitles
Universal CDN supports multi-language subtitles in VTT format for HLS streaming.
To demonstrate the feature, we will use the example videos /video/012345.mp4 and /video/12345.mp4 and we assume that the subtitles are in the same folder /video or in subfolder /video/subs/.
Example:
/hls/video/, 012345.mp4, 123456.mp4, lang/ger/012345_ger.vtt , lang/eng/12345_eng.vtt, /urlset/master.m3u8
/hls/video/, 012345.mp4, 123456.mp4, subs/lang/ger/012345_ger.vtt , subs/lang/eng/12345_eng.vtt, /urlset/master.m3u8
There should be noted several things for the above request path:
1. We use urlset and master.m3u8 to load the subtitles in the playlist, not the standard playlist index.m3u8
2. We specify the common path first, which in the example is /video
3. Required part for the URL is lang/ISO 639-2, which is a three-letter country code. You can find all codes on this page: https://www.loc.gov/standards/iso639-2/php/code_list.php
4. If the subtitles are in the subfolder, the folder should be before the required part.
5. All commas are intentional.
Limit access using a secret key
The HLS functionality can be combined with our security token feature to block any hotlinking attempts. For detailed information, please go to Combining Transmuxing to HLS with Limit Access Using a Secret Key Feature.
To combine all features of the secret key that limit access, you must include the following part:
/video/bsd/10000-10000-15000/sd/20000/lang/eng/012345_eng.vtt, lang/ger/012345_ger.vtt, 012345_,24,36,48, 0p.mp4
Note that the prefix /hls and suffixes /urlset/master.m3u8 and /index.m3u8 are excluded.
All the segments have security tokens attached to them when returned inside the .m3u8 file. For this reason, there is no need to recalculate it for each of them before requesting it.
Since the security token is the same (including all parameters) for the .m3u8 file, it’s important to note that for longer videos it’s possible that the security token can expire before a request for the given segment is made.
Purge HLS Cache
UCDN provides a fast and efficient solution to purge the HLS cache, whether it’s the index.m3u8 or the master.m3u8 file, as well as individual segments. Below are brief descriptions of the different HLS objects that can be set for purging:
- Purging the index.m3u8 file allows you to remove the specific HLS variant, including its associated segments and index files, from the cache. This method is useful when you want to refresh a particular resolution or variant of the video while keeping other variants intact.
- Purging the master.m3u8 file enables you to remove all variants and resolutions associated with the video from the cache. This comprehensive purge removes the master file itself, index files, segments, and other related elements. It ensures a complete refresh of the HLS content, but keep in mind that it will impact all variants of the video.
- In addition, you can also purge individual segments. This granular approach enables you to refresh specific segments while keeping the rest of the content intact.
Purge jobs can be initiated through the UCDN’s API. Example purge command:
curl --include --request POST "https://api.ucdn.com/v3/purge/YourZone/?token=YourApiToken" --header "Content-Type: application/json" -d '{"files":"/hls/012345_360p.mp4/index.m3u8"}'
Or from the UCDN Panel, by following the below step-by-step instructions:
1. Access the UCDN Control Panel.
2. Navigate to the Purge Files page.
3. Under the ‘Purge by filename‘ section, you can choose between:
– Specifying the zone’s host name and relative path.
Example:
I will purge files from: subdomain.domain.com
Purge files from this list: /hls/012345_360p.mp4/index.m3u8
– Providing the full path without specifying the zone’s host name.
Example:
I will purge files from: guess zone from hostnames
Purge files from this list: https://subdomain.domain.com/hls/012345_360p.mp4/index.m3u8
4. Choose ‘Purge Files’.
Allow up to 15 minutes for purge job completion, depending on the amount of objects.
Combining Transmuxing to HLS with Limit Access Using a Secret Key
Combining Transmuxing to MPEG-DASH with Limit Access Using a Secret Key