Detect When AvPlayer Bit Rate

In my application, I use AVPlayer to read some streams (m3u8 file) with HLS protocol. I need to know how many times, during a streaming session, the client switches the bit rate .

Assume that client throughput is increasing. Thus, the client will switch to a higher bitrate segment. Can AVPlayer detect this switch?

Thanks.

+6
source share
2 answers

I have had a similar problem lately. The solution turned out to be a bit hacky, but it worked as far as I saw. First I created an observer for new access log notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAVPlayerAccess:) name:AVPlayerItemNewAccessLogEntryNotification object:nil]; 

What causes this function. It can probably be optimized, but here is the main idea:

 - (void)handleAVPlayerAccess:(NSNotification *)notif { AVPlayerItemAccessLog *accessLog = [((AVPlayerItem *)notif.object) accessLog]; AVPlayerItemAccessLogEvent *lastEvent = accessLog.events.lastObject; float lastEventNumber = lastEvent.indicatedBitrate; if (lastEventNumber != self.lastBitRate) { //Here is where you can increment a variable to keep track of the number of times you switch your bit rate. NSLog(@"Switch indicatedBitrate from: %f to: %f", self.lastBitRate, lastEventNumber); self.lastBitRate = lastEventNumber; } } 

Each time a new entry appears in the access log, it checks the last specified bitrate from the most recent entry (the last object in the access log for the player element). It compares the specified bitrate with the property that kept the bitrate from the last change.

+9
source

BoardProgrammer Solution Works Great! In my case, I needed the specified bitrate to determine when the quality of the content switched from SD to HD. Here is the version of Swift 3.

 // Add observer. NotificationCenter.default.addObserver(self, selector: #selector(handleAVPlayerAccess), name: NSNotification.Name.AVPlayerItemNewAccessLogEntry, object: nil) // Handle notification. func handleAVPlayerAccess(notification: Notification) { guard let playerItem = notification.object as? AVPlayerItem, let lastEvent = playerItem.accessLog()?.events.last else { return } let indicatedBitrate = lastEvent.indicatedBitrate // Use bitrate to determine bandwidth decrease or increase. } 
+4
source

Source: https://habr.com/ru/post/983594/


All Articles