Android Question How to get the current bitrate of video streaming in ExoPlayer?

JohnC

Expert
Licensed User
Longtime User
I would like to obtain the current real-time bitrate of a video that Exoplayer is playing.

I found this info, but I have no idea how to convert for use in B4A:


Any help to get this working in B4A would be greatly appreciated!
 

JohnC

Expert
Licensed User
Longtime User
I tried getting "averageBitrate", but it gave the below error:

B4X:
(RuntimeException) java.lang.RuntimeException: Field: averageBitrate not found in: com.google.android.exoplayer2.Format

I'm thinking that the above example worked because "width" and "height" are part of the "video" format:

Fields relevant to video formats
But the "averageBitrate" spec is not part of the "getVideoFormat" method:
B4X:
VideoFormat = VideoFormat.GetFieldJO("player").RunMethod("getVideoFormat", Null)

Fields commonly relevant to all formats

Do I need to call a different method other then "getVideoFormat" that will include "averageBitrate" as a valid spec?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I am using the HLS video streaming method, and after looking into this some more, and I could be wrong, but it looks like the values for "averageBitrate" and "peakBitrate" are simply read from a TAG in the video stream and is NOT a runtime-calculated value:

averageBitrate
public final int averageBitrate
The average bitrate in bits per second, or NO_VALUEif unknown or not applicable. The way in which this field is populated depends on the type of media to which the format corresponds:
  • DASH representations: Always NO_VALUE.
  • HLS variants: The AVERAGE-BANDWIDTH attribute defined on the corresponding EXT-X-STREAM-INF tag in the master playlist, or NO_VALUE if not present.
  • SmoothStreaming track elements: The Bitrate attribute defined on the corresponding TrackElement in the manifest, or NO_VALUE if not present.
  • Progressive container formats: Often NO_VALUE, but may be populated with the average bitrate of the container if known.
  • Sample formats: Often NO_VALUE, but may be populated with the average bitrate of the stream of samples with type sampleMimeType if known. Note that if sampleMimeType is a compressed format (e.g., MimeTypes.AUDIO_AAC), then this bitrate is for the stream of still compressed samples.

So, since I want to know the actual real-time bitrate and not what the media tags say the video is suppose to be, it looks like I need to use this method:


But again, I have not idea how to implement this in B4A :(
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Check this:
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Thank you for that tip, but it looks like that method gets the info from Metadata, which appears to be just the static data provided by the stream source, and not actual real-time data like the live bitrate which can be quite different from the documented (in the meta data) bitrate of the source media.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
To get the real-time/actual bitrate, it looks like I have to intercept these two events in the exoplayer:

B4X:
onLoadStarted(int sourceId, long length, int type, int trigger, Format format, int mediaStartTimeMs, int mediaEndTimeMs)
onLoadCompleted(int sourceId, long bytesLoaded, int type, int trigger, Format format, int mediaStartTimeMs, int mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs)

Then do calculations (as shown in the stackoverflow article "How to get observed bitrate from Google's ExoPlayer") to figure out how many bytes are being transferred every second.

But I do not know how to catch those two events.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
:(
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I figured out a way to get the real-time bitrate using the code from the below thread and simply calculate how many bits were received each second (using the getTotalRxBytes value):


This method assumes all the incoming bytes are related to my app, which is not a bad assumption considering my app is a video app and data usage from other apps would be minimal in comparison.
 
Last edited:
Upvote 0
Top