Basic concepts of HLS and AVPlayer

Nabil
3 min readJul 14, 2018

--

In this article I want to explore what is HLS, the AVFoundation family and how to build a simple native iOS player with an HLS URL.

What is HLS

HLS stands for HTTP Live Streaming. It is a media streaming protocol used for delivering audio and visual media over the internet. The major difference that brought faster streaming over the internet with this protocol was the idea of cropping into chunks one single video instead of downloading the entire media file. You can even work with multiple qualities depending on some things such as bandwidth because those chunks are encoded and available in different quality settings in the playlist.

It looks a bit complex in the first time, but it is simply several tags following the HLS protocol where players adapt and use all these provided data to deliver the best streaming quality possible:

#EXTM3U#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="stereo",LANGUAGE="en",NAME="English",DEFAULT=YES,AUTOSELECT=YES,URI="audio/stereo/en/128kbit.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="stereo",LANGUAGE="dubbing",NAME="Dubbing",DEFAULT=NO,AUTOSELECT=YES,URI="audio/stereo/none/128kbit.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="surround",LANGUAGE="en",NAME="English",DEFAULT=YES,AUTOSELECT=YES,URI="audio/surround/en/320kbit.m3u8"#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="Deutsch",DEFAULT=NO,AUTOSELECT=YES,FORCED=NO,LANGUAGE="de",URI="subtitles_de.m3u8"
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="English",DEFAULT=YES,AUTOSELECT=YES,FORCED=NO,LANGUAGE="en",URI="subtitles_en.m3u8"
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=258157,CODECS="avc1.4d400d,mp4a.40.2",AUDIO="stereo",RESOLUTION=422x180,SUBTITLES="subs"
video/250kbit.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=520929,CODECS="avc1.4d4015,mp4a.40.2",AUDIO="stereo",RESOLUTION=638x272,SUBTITLES="subs"
video/500kbit.m3u8

There is a lot to understand about the playlist, but keep in mind that by providing all the information possible this will increase the quality delivery of AVPlayer.

I suggest to keep this link with you as well, where you can take a look into the meaning of each tag and how it should be used based on the HLS protocol version.

What is AVFoundation

AVFoundation is Apple’s Foundational Objective-C framework and it sits upon C frameworks CoreAudio, CoreMedia and CoreAnimation. as you can see in the image.

AVFoundation Family

AVFoundation offers mechanisms for reading and writing media, it helps to compose independent clips into new assets and it gives us an interface for playback and editing of audio and video for our iOS and MacOS apps.

What is AVPlayer

AVPlayer is part of AVFoundation framework of the iOS family. And it provides the interface to control the playback and timing of a media asset.

AVFoundation was not the first solution from Apple to make life easier for developers across MacOS and iOS to work with media, before that there were some frameworks such as QTKit.

How to play videos using AVPlayer

There are many ways to play a media in the AVPlayer, you can start with an AVPlayerItem, or you can give an URL directly to the AVPlayer. In this example I will keep the code as simple as possible just to show how to build it in your ViewController.

With this, you will have the native Media Control of the AVPlayer, that you can’t change the layout of its controls. If you want to have your own custom controls you will need to build an AVPlayerLayer and watch the events, we will talk about this in another post.

What happens behind the scenes?

When the player receives the playlist it is parsed and the bitrate chosen to start to request and download chunks until it has enough buffer to play and keep up downloading other chunks without going to the stalled state.

Anatomy of an HLS Playback Session

In this image we can see a time line of an HLS Playback Session where we start downloading a master playlist in a bit rate of 2 Mb, after checkpoint 5 the bit rate changes to 1 Mb until it gets in a stalled state then it recovers to 1 Mb bit rate and only plays again when it has enough buffer.

In the next post I intend to talk about the AVKit and show how to design a new Media Control for the AVPlayer.

WWDC talks to watch about what we talked here

--

--

Nabil

Software Engineer, big fan of Quality and Agile methodologies.