Skip to content

Commit

Permalink
add videoBitrate and audioBitrate
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Nov 5, 2023
1 parent 5a3c14f commit 6c24a94
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Sources/KSPlayer/AVPlayer/KSAVPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public class KSAVPlayer {
public private(set) var duration: TimeInterval = 0
public private(set) var fileSize: Double = 0
public private(set) var playableTime: TimeInterval = 0

public var audioBitrate: Int = 0
public var videoBitrate: Int = 0
public var playbackRate: Float = 1 {
didSet {
if playbackState == .playing {
Expand Down
2 changes: 2 additions & 0 deletions Sources/KSPlayer/AVPlayer/MediaPlayerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public protocol MediaPlayback: AnyObject {
var fileSize: Double { get }
var metadata: [String: String] { get }
var naturalSize: CGSize { get }
var audioBitrate: Int { get }
var videoBitrate: Int { get }
var currentPlaybackTime: TimeInterval { get }
func prepareToPlay()
func shutdown()
Expand Down
4 changes: 4 additions & 0 deletions Sources/KSPlayer/MEPlayer/KSMEPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ extension KSMEPlayer: MediaPlayerProtocol {

public var seekable: Bool { playerItem.seekable }

public var videoBitrate: Int { playerItem.videoBitrate }

public var audioBitrate: Int { playerItem.audioBitrate }

public func seek(time: TimeInterval, completion: @escaping ((Bool) -> Void)) {
let time = max(time, 0)
playbackState = .seeking
Expand Down
20 changes: 10 additions & 10 deletions Sources/KSPlayer/MEPlayer/MEPlayerItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,14 @@ extension MEPlayerItem {
// MARK: MediaPlayback

extension MEPlayerItem: MediaPlayback {
var videoBitrate: Int {
Int(8 * (videoTrack?.bitrate ?? 0))
}

var audioBitrate: Int {
Int(8 * (audioTrack?.bitrate ?? 0))
}

var seekable: Bool {
guard let formatCtx else {
return false
Expand Down Expand Up @@ -766,23 +774,15 @@ extension MEPlayerItem: OutputRenderSourceDelegate {
case .dropNextPacket:
if let videoTrack = videoTrack as? AsyncPlayerItemTrack {
_ = videoTrack.packetQueue.pop { item, _ -> Bool in
if let corePacket = item.corePacket {
return corePacket.pointee.flags & AV_PKT_FLAG_KEY != AV_PKT_FLAG_KEY
} else {
return false
}
!item.isKeyFrame
}
}
case .dropGOPPacket:
if let videoTrack = videoTrack as? AsyncPlayerItemTrack {
var packet: Packet? = nil
repeat {
packet = videoTrack.packetQueue.pop { item, _ -> Bool in
if let corePacket = item.corePacket {
return corePacket.pointee.flags & AV_PKT_FLAG_KEY != AV_PKT_FLAG_KEY
} else {
return false
}
!item.isKeyFrame
}
} while packet != nil
}
Expand Down
17 changes: 17 additions & 0 deletions Sources/KSPlayer/MEPlayer/MEPlayerItemTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,24 @@ class SyncPlayerItemTrack<Frame: MEFrame>: PlayerItemTrackProtocol, CustomString
outputRenderQueue.shutdown()
}

private var lastPacketBytes = Int32(0)
private var lastPacketSeconds = Double(-1)
var bitrate = Double(0)
fileprivate func doDecode(packet: Packet) {
if packet.isKeyFrame, packet.assetTrack.mediaType != .subtitle {
let seconds = packet.seconds
let diff = seconds - lastPacketSeconds
if lastPacketSeconds < 0 || diff < 0 {
bitrate = 0
lastPacketBytes = 0
lastPacketSeconds = seconds
} else if diff > 0.5 {
bitrate = Double(lastPacketBytes) / diff
lastPacketBytes = 0
lastPacketSeconds = seconds
}
}
lastPacketBytes += packet.size
let decoder = decoderMap.value(for: packet.assetTrack.trackID, default: makeDecode(assetTrack: packet.assetTrack))
decoder.decodeFrame(from: packet) { [weak self] result in
guard let self else {
Expand Down
12 changes: 12 additions & 0 deletions Sources/KSPlayer/MEPlayer/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ final class Packet: ObjectQueueItem {
var size: Int32 = 0
var assetTrack: FFmpegAssetTrack!
private(set) var corePacket = av_packet_alloc()
var isKeyFrame: Bool {
if let corePacket {
return corePacket.pointee.flags & AV_PKT_FLAG_KEY == AV_PKT_FLAG_KEY
} else {
return false
}
}

var seconds: Double {
assetTrack.timebase.cmtime(for: position).seconds
}

func fill() {
guard let corePacket else {
return
Expand Down
6 changes: 3 additions & 3 deletions Sources/KSPlayer/MEPlayer/VideoToolboxDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class VideoToolboxDecode: DecodeProtocol {
}
guard status == noErr else {
if status == kVTInvalidSessionErr || status == kVTVideoDecoderMalfunctionErr || status == kVTVideoDecoderBadDataErr {
if corePacket.flags & AV_PKT_FLAG_KEY == 1 {
if packet.isKeyFrame {
completionHandler(.failure(NSError(errorCode: .codecVideoReceiveFrame, avErrorCode: status)))
}
}
Expand All @@ -59,7 +59,7 @@ class VideoToolboxDecode: DecodeProtocol {
let frame = VideoVTBFrame(fps: session.assetTrack.nominalFrameRate)
frame.corePixelBuffer = imageBuffer
frame.timebase = session.assetTrack.timebase
if packetFlags & AV_PKT_FLAG_KEY == 1, packetFlags & AV_PKT_FLAG_DISCARD != 0, self.lastPosition > 0 {
if packet.isKeyFrame, packetFlags & AV_PKT_FLAG_DISCARD != 0, self.lastPosition > 0 {
self.startTime = self.lastPosition - pts
}
self.lastPosition = max(self.lastPosition, pts)
Expand All @@ -72,7 +72,7 @@ class VideoToolboxDecode: DecodeProtocol {
if status == noErr {
VTDecompressionSessionWaitForAsynchronousFrames(session.decompressionSession)
} else if status == kVTInvalidSessionErr || status == kVTVideoDecoderMalfunctionErr || status == kVTVideoDecoderBadDataErr {
if corePacket.flags & AV_PKT_FLAG_KEY == 1 {
if packet.isKeyFrame {
throw NSError(errorCode: .codecVideoReceiveFrame, avErrorCode: status)
} else {
// 解决从后台切换到前台,解码失败的问题
Expand Down
2 changes: 2 additions & 0 deletions Sources/KSPlayer/SwiftUI/KSVideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ struct VideoSettingView: View {
subtitleModel.searchSubtitle(query: subtitleTitle, languages: ["zh-cn"])
}
Text("Stream Type: \((videoTracks?.first { $0.isEnabled }?.fieldOrder ?? .progressive).description)")
Text("Audio bitrate: \(config.playerLayer?.player.audioBitrate ?? 0) b/s")
Text("Video bitrate: \(config.playerLayer?.player.videoBitrate ?? 0) b/s")
if let fileSize = config.playerLayer?.player.fileSize, fileSize > 0 {
Text("File Size: \(String(format: "%.1f", fileSize / 1_000_000))MB")
}
Expand Down

0 comments on commit 6c24a94

Please sign in to comment.