Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

avfoundation - How to get initial buffer size of AVPlayer

In order to measure the quality of service of video playback in my App, I need to measure the initial buffer size of AVPlayer when the content actually start playing.

To do so, I am adding a one-time periodic time observer to the first millisecond of the playback and inspecting from there the property segmentsDownloadedDuration of the last AVPlayerItemAccessLogEvent.

Sadly the values that I am getting with this approach don't match the values that I see server-side.

Server-side, if I multiply the numbers of segments initially requested by segments duration, the value that I get mismatch by up to 6 seconds compared with the reports from my client App.

Is there any better approach for getting this data? Is there something we might be missing or doing wrong?

You can find here below an example of the client approach:

import UIKit
import AVFoundation
class PlaygroundViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let videoURL = URL(string: "https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8")!
      
        // Setup Player Layer
        playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = view.bounds
        view.layer.addSublayer(playerLayer)

        // Setup Player Item
        let asset = AVAsset(url: videoURL)
        playerItem = AVPlayerItem(asset: asset)

        // Setup AVPlayer
        player = AVPlayer(playerItem: playerItem)

        // Setup one time observer
        var contentStartedObserver: Any?
        contentStartedObserver = player?.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: 1000), queue: DispatchQueue.main, using: {
            [weak self] _ in

            print("Content Started Playing")

            guard let accessLog = self?.playerItem.accessLog() else { return }
            guard let lastEvent = accessLog.events.last else { return }

            print(">> Buffered Duration:")
            print(lastEvent.segmentsDownloadedDuration)

            // Ok, we are done. Remove the observer
            self?.player?.removeTimeObserver(contentStartedObserver)
        })

        // Play
        playerLayer.player = player
        player.play()
    }

    // MARK: - Private

    private var playerLayer: AVPlayerLayer!
    private var player: AVPlayer!
    private var playerItem: AVPlayerItem!
}
question from:https://stackoverflow.com/questions/65940712/how-to-get-initial-buffer-size-of-avplayer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...