Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capping the bit rate of an HLS stream programmatically on iOS devices

I have an HD video that I am streaming to an iOS app. I want to allow the user the ability to cap the max stream quality (low, medium, high) considering the video is several GBs when streaming at the max bit rate. Along the same lines, I would like to automatically choose a setting based on cellular vs wifi connection, for the obvious data-cap reasons.

I have no problem getting the current bit rate by accessing the AVPlayerItemAccessLogEvent, but am lost when it comes to forcing a lower quality stream.

Is this even possible with HLS? Thanks!

like image 780
timgcarlson Avatar asked Mar 21 '23 19:03

timgcarlson


2 Answers

If you are using AVPlayer, the right way should be

preferredPeakBitRate

From Apple doc here, The desired limit, in bits per second, of network bandwidth consumption for this item.

like image 109
Wizard Of iOS Avatar answered Mar 23 '23 13:03

Wizard Of iOS


It's not exactly dynamic, but I did solve this problem by creating four different m3u8 playlists. I labeled each playlist to represent a stream quality (low, medium, high, extreme). The user would select one based on the desired max quality. The extreme playlist includes the URLs of all qualities. The high playlist has less URLs than the extreme, the medium less URLs than the high, and the low less URLs than the medium. Whenever the user selects a different quality, I would just switch the base stream playlist to the respective quality playlist URL.

Here is a simple example of the four different playlists.

HLS_Movie_Extreme.m3u8

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=64000
stream-0-64000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
stream-1-350000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=800000
stream-2-800000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1200000
stream-3-1200000/index prog_index.m3u8 m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1800000
stream-4-1800000/prog_index.m3u8

HLS_Movie_High.m3u8

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=64000
stream-0-64000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
stream-1-350000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=800000
stream-2-800000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1200000
stream-3-1200000/index prog_index.m3u8 m3u8

HLS_Movie_Medium.m3u8

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=64000
stream-0-64000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
stream-1-350000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=800000
stream-2-800000/prog_index.m3u8

HLS_Movie_Low.m3u8

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=64000
stream-0-64000/prog_index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
stream-1-350000/prog_index.m3u8

Like I said, it's not dynamic, but you could use various techniques to get the users network connection and point to the desired quality playlist if needed. For me, it was sufficient to get the user's preference, and adjust the stream accordingly.

like image 27
timgcarlson Avatar answered Mar 23 '23 13:03

timgcarlson