HTTP Live Streaming (HLS) is a media streaming protocol created by Apple to deliver audio and video content over the internet. Here’s a simplified overview of how HLS works:
Table of contents
Open Table of contents
What is HLS?
HLS (HTTP Live Streaming) is a streaming protocol that allows you to deliver video content over the internet. It works by breaking the video into small chunks, which are then delivered over HTTP. This approach allows for adaptive bitrate streaming, meaning the video quality can adjust in real-time based on the viewer’s internet connection speed.
Key Components of HLS:
- Media Files: The original media content (audio/video) is encoded and split into small segments (typically 2-10 seconds each). These segments are usually in MPEG-TS format.
- Playlist (M3U8) Files: These are text files that list the URLs of the media segments. There are two types of playlists:
- Media Playlist: Contains the URLs of the media segments.
- Master Playlist: References multiple media playlists, allowing for adaptive bitrate streaming.
Content Preparation
- Video Source: The original video is obtained from a source such as a camera or a video file.
- Video Encoding: The original video is encoded into multiple smaller video files (segments), each typically ranging from 2 to 10 seconds. This allows the video to be downloaded and played seamlessly without interruptions.
Video Source → Encoder → Segments (.ts files)
↓
Playlist (.m3u8)
↓
Server
↓
Player (Client)
Adaptive Bitrate
HLS supports multiple bitrates to suit different network conditions. The playlist can contain multiple versions of the video at different resolutions and bitrates.
- Monitoring the viewer’s bandwidth and CPU capacity.
- Switching to higher quality streams when the network is fast and less congested.
- Switching to lower quality streams when the network is slow or congested to avoid buffering.
- Example:
#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360
low.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1400000,RESOLUTION=854x480
medium.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1280x720
high.m3u8
And a corresponding media playlist (low.m3u8):
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10.0,
segment0.ts
#EXTINF:10.0,
segment1.ts
#EXTINF:10.0,
segment2.ts
In this example:
- The master playlist (master.m3u8) references three different media playlists (low.m3u8, medium.m3u8, and high.m3u8) with different bitrates and resolutions.
- The media playlist (low.m3u8) lists the URLs of the media segments.
Streaming
- Server: The server hosts the segmented video files and the playlist file. When a viewer requests to play a video, the server sends the playlist file and the corresponding video segments.
- Player: The video player (usually a browser or an application supporting HLS) loads the playlist file and starts downloading the video segments. The player downloads each segment and plays it immediately while downloading the next segment during playback.
- Bitrate Adjustment: The player continuously monitors network bandwidth and adjusts the download of video segments with the most suitable bitrate to ensure smooth playback.
Content Updating
- Live Streaming: For live content, the playlist file is continuously updated to add the latest video segments. The player will check the playlist file at regular intervals to update and download new video segments.
Advantages of HLS
- Adaptability: HLS can adjust video quality based on network bandwidth, helping to minimize buffering.
- High Compatibility: HLS is widely supported on many devices and operating systems, including iOS, Android, Windows, and macOS.
- Ease of Deployment: Uses HTTP for transmission, requiring no special network configuration and easy scalability.
Disadvantages of HLS
- High Latency: HLS may have higher latency compared to some other live streaming protocols, like RTMP.
- Large File Size: Due to segmenting the video, the overall size can be larger and consume more bandwidth.
How to setup hls server
- Set up HLS (HTTP Live Streaming) server involves several steps, including preparing the content, encoding the video, segmenting the video, creating playlists, and configuring the server to serve the HLS content.
- Set up HLS