Lets Dive into Exo-Player (Part II): Adding Quality Control

In the last article (Lets Dive into ExoPlayer ) we started with the basic setup of ExoPlayer using Styled Player View.
Now in the article, i tried penning down the part in which we will set up the configuration in ExoPlayer to extract multiple qualities from a video so that the user can switch between them
What is a Bitrate
The basic definition of bitrate is “the number of bits that are conveyed or processed in a given unit of time”
Now let's think of it this way we have a video file now it has certain bits of data embedded inside it. These bits of data may or may not be available in multiple bitrates
In case it doesn’t have multiple bitrates (MP4 File)
In case it has Multiple Bitrates (HLS, DASH)
Now there can be many cases but we are considering these specifically
ExoPlayer provide support to play all such different kind of video format

HLS: HTTP Live Streaming
HTTP-based adaptive bitrate streaming communications protocol was developed by Apple Inc. and released in 2009. It works by breaking the overall stream into a sequence of small HTTP-based files i.e short chunk of a whole file. A list of available streams encoded at different bit rates and then can be used to play as the m3u8 file format.
DASH: Dynamic Adaptive Streaming over HTTP
This is a kind of media streaming protocol that works similarly to the way HLS works by breaking the content into a sequence of small segments, which are served over HTTP. It can be a movie or the live broadcast of a sports event.
DASH was developed by MPEG (Moving Pictures Expert Group), the preeminent international authority for media compression.
The format for a dash file is MPD and because it is open source and supports Encrypted Media Extensions (EME) and Media Source Extension (MSE) it is widely used for DRM Support too
The content made available by both protocols has a variety of different bit rates. and we can use a bit rate adaptation (ABR) algorithm to automatically select the segment with the highest bit rate possible based on bandwidth and other factors
ExoPlayer has support for the ABR Algorithm which is basically one of the bandwidth-based bitrate selection Algorithm that automatically chooses the best bitrate to select and play
There are various other algorithms: Adaptive, Random, and Fixed Track Selection.
Similarly, we have different quality of the same video embedded in it using bitrate there can be embedded subtitles, and audio just as we have in dubbed/subbed videos

Implementation
Basic Setup :
Demo Repo :
Since we have already set up our player here will be skipping that
In ExoPlayer everything is a track whether it is a Video, Audio,Text
TrackSelector: We will be using ExoPlayer Default TrackSelector this will help us get the currently selected track and extract all it info
A single track can have multiple embedded sub-tracks inside it i.e track-groups
Player Setup
val trackSelector = DefaultTrackSelector(/* context= */this, AdaptiveTrackSelection.Factory())val player = ExoPlayer.Builder(this)
.setTrackSelector(trackSelector)
.build()
Basically, this will help us with extracting all the defined Sub Track that are embedded in the main parent track can be follow type TRACK_TYPE_VIDEO, TRACK_TYPE_AUDIO, TRACK_TYPE_TEXT, TRACK_TYPE_METADATA, TRACK_TYPE_IMAGE .etc
Since Using the Styled Player Viewer we get subtitle, and audio selection by default we will focus on video quality only here
Generate Quality List is a Kotlin extension function that generates all Video Quality Levels Here we have a
Render Track that defines all the track info of the current media source
Now Render Track have various track group of type Audio, Video, Text ..etc
These Track Groups consist of multiple video quality tracks which we will check one by one for support i.e whether they are supported or not
If that video quality track is supported then we will extract the label from it and use that track further with our player to set that quality
Here we are using TrackSelectionOverrides that can be used with Track Selector to select that particular track
Now this function will return us a list where we have each element paired up to its SelectionOverride we can parse that list anywhere and show quality
Here is the function which is being used above to check for support
What's Next?
Some Additional thoughts?
ExoPlayer has support for many extensive features like DRM, Caching, and Offline Download. Do let me know what you would like to know more.
Thank you for reading, stay tuned for amazing articles!
Catch me up at: https://twitter.com/its_pra_tick
Do not forget to give claps !! Happy Coding :)