iosobjective-cavfoundationavplayeravplayeritem

how to get bitrate of video streaming on iPhone/iPad devices?


I have an iOS app that streams video on iPhone/iPad devices.

Now I need to get the bitrate on video streaming on the iPhone/iPad device. ObservedBitrate and IndicatedBitrate does not help with this.

I also came across this below post but few comments mention it works for mpeg4 videos only. How to check Resolution, bitrate of video in iOS

I'm looking for the bitrate of video streaming on iPhone/iPad and for any video format.

Thanks!


Solution

  • You can listen for AVPlayerItemNewAccessLogEntry notification to get current bitrates for your stream such as:

    Adaptive video playback with AVPlayer can change a current stream to a stream with high or low bandwidth back and forth by network conditions but these changes do not appears on every stalls, network issues etc. because your steam can be buffered, m3u8 doesn't have stream with lower bandwidth to play etc. So if you need to detect a current realtime playback state look to stalls, FPS.

    There is sample code in swift but it's easy convertible to objc if you need:

    let url = URL(string: "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8")!
    let playerItem = AVPlayerItem(url: url)
    self.player = AVPlayer(playerItem: playerItem)
    
    NotificationCenter.default.addObserver(forName: .AVPlayerItemNewAccessLogEntry, object: playerItem, queue: nil) { notification in
        if let event = playerItem.accessLog()?.events.last {
            let bitrates = [event.observedBitrate,
                            event.indicatedBitrate,
                            event.averageVideoBitrate,
            ]
            print(">", bitrates)
        }
    }
    
    player?.play()
    
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = view.bounds
    view.layer.addSublayer(playerLayer)
    
    Outputs:
    
    > [16375938.865617642, 628000.0, 307568.0]
    > [nan, 1728000.0, 0.0]
    > [9830221.39078689, 2528000.0, 1422032.0]