Skip to content

Commit

Permalink
fix #330
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Dec 9, 2022
1 parent 438ee9a commit cf56d1c
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 34 deletions.
15 changes: 15 additions & 0 deletions Sources/KSPlayer/AVPlayer/PlayerDefines.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ public struct VideoAdaptationState {
public internal(set) var loadedCount: Int = 0
}

struct KSAudioChannelLayout {
let layout: AVAudioChannelLayout
let channelCount: AVAudioChannelCount
init(layout: AVAudioChannelLayout) {
self.layout = layout
channelCount = layout.channelCount
}

init(layout: AVAudioChannelLayout, channelCount: AVAudioChannelCount) {
self.layout = layout
self.channelCount = channelCount
}
}

open class KSOptions {
// public static let shared = KSOptions()
/// 最低缓存视频时间
Expand Down Expand Up @@ -298,6 +312,7 @@ open class KSOptions {
public internal(set) var decodeAudioTime = 0.0
public internal(set) var decodeVideoTime = 0.0
var formatCtx: UnsafeMutablePointer<AVFormatContext>?
var channelLayout = KSAudioChannelLayout(layout: AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_Stereo)!)
public init() {
formatContextOptions["auto_convert"] = 0
formatContextOptions["fps_probe_size"] = 3
Expand Down
14 changes: 6 additions & 8 deletions Sources/KSPlayer/Core/PlayerToolBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,24 @@ public class PlayerToolBar: UIStackView {
super.addArrangedSubview(view)
view.isHidden = false
}
#if canImport(UIKit)
open override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

#if canImport(UIKit)
override open func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocus(in: context, with: coordinator)
if let nextFocusedItem = context.nextFocusedItem as? UIButton {
nextFocusedItem.tintColor = nextFocusedItem.titleColor(for: .focused)
}
if let previouslyFocusedItem = context.previouslyFocusedItem as? UIButton {
if previouslyFocusedItem.isSelected {
previouslyFocusedItem.tintColor = previouslyFocusedItem.titleColor(for: .selected)
}
else if previouslyFocusedItem.isHighlighted {
} else if previouslyFocusedItem.isHighlighted {
previouslyFocusedItem.tintColor = previouslyFocusedItem.titleColor(for: .highlighted)
}
else {
} else {
previouslyFocusedItem.tintColor = previouslyFocusedItem.titleColor(for: .normal)
}
}
}
#endif
#endif

open func addTarget(_ target: AnyObject?, action: Selector) {
playButton.addTarget(target, action: action, for: .primaryActionTriggered)
Expand Down
11 changes: 6 additions & 5 deletions Sources/KSPlayer/MEPlayer/AudioEnginePlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protocol AudioPlayer: AnyObject {
var threshold: Float { get set }
var expansionRatio: Float { get set }
var overallGain: Float { get set }
func prepare(channels: UInt32)
func prepare(channels: UInt32, options: KSOptions)
func play(time: TimeInterval)
func pause()
}
Expand Down Expand Up @@ -138,18 +138,19 @@ public final class AudioEnginePlayer: AudioPlayer, FrameOutput {
engine.attach(dynamicsProcessor)
}

func prepare(channels: UInt32) {
func prepare(channels: UInt32, options: KSOptions) {
#if !os(macOS)
let channels = min(AVAudioSession.sharedInstance().maximumOutputNumberOfChannels, Int(channels))
try? AVAudioSession.sharedInstance().setPreferredOutputNumberOfChannels(channels)
#endif
var format = engine.outputNode.outputFormat(forBus: 0)
KSOptions.audioPlayerSampleRate = Int32(format.sampleRate)
if let audioUnit = engine.outputNode.audioUnit {
KSOptions.channelLayout = AVAudioChannelLayout(layout: audioUnit.channelLayout)
options.channelLayout = KSAudioChannelLayout(layout: AVAudioChannelLayout(layout: audioUnit.channelLayout))
}
if KSOptions.channelLayout != format.channelLayout || KSOptions.isAudioPlanar == format.isInterleaved {
format = AVAudioFormat(commonFormat: format.commonFormat, sampleRate: format.sampleRate, interleaved: !KSOptions.isAudioPlanar, channelLayout: KSOptions.channelLayout)
let channelLayout = options.channelLayout.layout
if channelLayout != format.channelLayout || KSOptions.isAudioPlanar == format.isInterleaved {
format = AVAudioFormat(commonFormat: format.commonFormat, sampleRate: format.sampleRate, interleaved: !KSOptions.isAudioPlanar, channelLayout: channelLayout)
}
// engine.attach(nbandEQ)
// engine.attach(distortion)
Expand Down
19 changes: 12 additions & 7 deletions Sources/KSPlayer/MEPlayer/AudioRendererPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@ public class AudioRendererPlayer: AudioPlayer, FrameOutput {
init() {
KSOptions.setAudioSession()
synchronizer.addRenderer(renderer)
if #available(tvOS 15.0, iOS 15.0, macOS 12.0, *) {
renderer.allowedAudioSpatializationFormats = .monoStereoAndMultichannel
}
}

func prepare(channels: UInt32) {
func prepare(channels: UInt32, options: KSOptions) {
if channels > 2 {
renderer.audioTimePitchAlgorithm = .spectral
}
#if os(macOS)
let channels = min(2, channels)
#else
let channels = min(UInt32(AVAudioSession.sharedInstance().maximumOutputNumberOfChannels), channels)
// let channels = min(UInt32(AVAudioSession.sharedInstance().maximumOutputNumberOfChannels), channels)
try? AVAudioSession.sharedInstance().setPreferredOutputNumberOfChannels(Int(channels))
#endif
if let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: Double(KSOptions.audioPlayerSampleRate), channels: channels, interleaved: !KSOptions.isAudioPlanar) {
Expand All @@ -75,20 +78,20 @@ public class AudioRendererPlayer: AudioPlayer, FrameOutput {
var audioStreamBasicDescription = AudioStreamBasicDescription()
let floatByteSize = UInt32(MemoryLayout<Float>.size)
audioStreamBasicDescription.mBitsPerChannel = 8 * floatByteSize
audioStreamBasicDescription.mBytesPerFrame = floatByteSize
audioStreamBasicDescription.mBytesPerFrame = floatByteSize * (KSOptions.isAudioPlanar ? 1 : channels)
audioStreamBasicDescription.mChannelsPerFrame = channels
audioStreamBasicDescription.mFormatFlags = kAudioFormatFlagIsPacked | kAudioFormatFlagIsFloat
if KSOptions.isAudioPlanar {
audioStreamBasicDescription.mFormatFlags |= kAudioFormatFlagIsNonInterleaved
}
audioStreamBasicDescription.mFormatID = kAudioFormatLinearPCM
audioStreamBasicDescription.mFramesPerPacket = 1
audioStreamBasicDescription.mBytesPerPacket = audioStreamBasicDescription.mFramesPerPacket * audioStreamBasicDescription.mBytesPerFrame * (KSOptions.isAudioPlanar ? 1 : channels)
audioStreamBasicDescription.mBytesPerPacket = audioStreamBasicDescription.mFramesPerPacket * audioStreamBasicDescription.mBytesPerFrame
audioStreamBasicDescription.mSampleRate = Float64(KSOptions.audioPlayerSampleRate)
CMAudioFormatDescriptionCreate(allocator: kCFAllocatorDefault, asbd: &audioStreamBasicDescription, layoutSize: 0, layout: nil, magicCookieSize: 0, magicCookie: nil, extensions: nil, formatDescriptionOut: &desc)
}
if let tag = desc?.audioFormatList.first?.mChannelLayoutTag, tag != kAudioChannelLayoutTag_Unknown, let layout = AVAudioChannelLayout(layoutTag: tag) {
KSOptions.channelLayout = layout
if let tag = desc?.audioFormatList.first?.mChannelLayoutTag, let layout = AVAudioChannelLayout(layoutTag: tag) {
options.channelLayout = KSAudioChannelLayout(layout: layout, channelCount: channels)
}
}

Expand Down Expand Up @@ -130,7 +133,9 @@ public class AudioRendererPlayer: AudioPlayer, FrameOutput {
for i in 0 ..< n {
var outBlockBuffer: CMBlockBuffer?
let dataByteSize = Int(render.numberOfSamples * UInt32(MemoryLayout<Float>.size) * render.channels) / n
// let dataByteSize = render.dataSize[i]
if dataByteSize > render.dataSize {
assertionFailure("dataByteSize: \(dataByteSize),render.dataSize: \(render.dataSize)")
}
CMBlockBufferCreateWithMemoryBlock(
allocator: kCFAllocatorDefault,
memoryBlock: nil,
Expand Down
2 changes: 1 addition & 1 deletion Sources/KSPlayer/MEPlayer/FFmpegDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FFmpegDecode: DecodeProtocol {
if assetTrack.mediaType == .video {
swresample = VideoSwresample()
} else {
swresample = AudioSwresample(codecpar: codecpar)
swresample = AudioSwresample(codecpar: codecpar, outChannel: options.channelLayout.channel)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/KSPlayer/MEPlayer/KSMEPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ extension KSMEPlayer: MEPlayerDelegate {
$0.audioStreamBasicDescription?.mChannelsPerFrame ?? 2
}.max() ?? 2
let fps = tracks(mediaType: .video).map(\.nominalFrameRate).max() ?? 24
audioOutput.prepare(channels: channels)
audioOutput.prepare(channels: channels, options: options)
videoOutput?.prepare(fps: fps)
runInMainqueue { [weak self] in
guard let self else { return }
Expand Down
15 changes: 7 additions & 8 deletions Sources/KSPlayer/MEPlayer/Resample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ class AudioSwresample: Swresample {
private var descriptor: AudioDescriptor
private var outChannel: AVChannelLayout
private let outSampleFmt = KSOptions.isAudioPlanar ? AV_SAMPLE_FMT_FLTP : AV_SAMPLE_FMT_FLT
init(codecpar: AVCodecParameters) {
init(codecpar: AVCodecParameters, outChannel: AVChannelLayout) {
descriptor = AudioDescriptor(codecpar: codecpar)
outChannel = KSOptions.channelLayout.channel
self.outChannel = outChannel
KSLog("out channelLayout: \(outChannel)")
_ = setup(descriptor: descriptor)
}
Expand Down Expand Up @@ -277,12 +277,11 @@ class AudioSwresample: Swresample {
}
}

extension AVAudioChannelLayout {
extension KSAudioChannelLayout {
var channel: AVChannelLayout {
let mutableLayout = UnsafeMutablePointer(mutating: layout)
let mutableLayout = UnsafeMutablePointer(mutating: layout.layout)
KSLog("KSOptions channelLayout: \(mutableLayout.pointee)")
let tag = mutableLayout.pointee.mChannelLayoutTag
let n = Int(mutableLayout.pointee.mNumberChannelDescriptions)
switch tag {
case kAudioChannelLayoutTag_Mono: return .init(nb: 1, mask: swift_AV_CH_LAYOUT_MONO)
case kAudioChannelLayoutTag_Stereo: return .init(nb: 2, mask: swift_AV_CH_LAYOUT_STEREO)
Expand All @@ -298,9 +297,9 @@ extension AVAudioChannelLayout {
case kAudioChannelLayoutTag_AAC_7_1: return .init(nb: 8, mask: swift_AV_CH_LAYOUT_7POINT1_WIDE_BACK)
case kAudioChannelLayoutTag_MPEG_7_1_C: return .init(nb: 8, mask: swift_AV_CH_LAYOUT_7POINT1)
case kAudioChannelLayoutTag_UseChannelDescriptions:
let buffers = UnsafeBufferPointer<AudioChannelDescription>(start: &mutableLayout.pointee.mChannelDescriptions, count: n)
let buffers = UnsafeBufferPointer<AudioChannelDescription>(start: &mutableLayout.pointee.mChannelDescriptions, count: Int(channelCount))
var mask = UInt64(0)
for i in 0 ..< n {
for i in 0 ..< Int(channelCount) {
let label = buffers[i].mChannelLabel
KSLog("KSOptions channelLayout label: \(label)")
let channel = label.avChannel.rawValue
Expand All @@ -316,7 +315,7 @@ extension AVAudioChannelLayout {
return outChannel
default:
var outChannel = AVChannelLayout()
av_channel_layout_default(&outChannel, Int32(max(n, 1)))
av_channel_layout_default(&outChannel, Int32(max(channelCount, 2)))
return outChannel
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/KSPlayer/Video/VideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,18 @@ open class VideoPlayerView: PlayerView {
/// Add Customize functions here
open func customizeUIComponents() {
tapGesture.addTarget(self, action: #selector(tapGestureAction(_:)))
#if canImport(UIKit)
#if canImport(UIKit)
tapGesture.allowedPressTypes = [NSNumber(value: UIPress.PressType.select.rawValue)]
#endif
#endif
tapGesture.numberOfTapsRequired = 1
controllerView.addGestureRecognizer(tapGesture)
panGesture.addTarget(self, action: #selector(panGestureAction(_:)))
controllerView.addGestureRecognizer(panGesture)
panGesture.isEnabled = false
doubleTapGesture.addTarget(self, action: #selector(doubleTapGestureAction))
#if canImport(UIKit)
#if canImport(UIKit)
doubleTapGesture.allowedPressTypes = [NSNumber(value: UIPress.PressType.select.rawValue)]
#endif
#endif
doubleTapGesture.numberOfTapsRequired = 2
tapGesture.require(toFail: doubleTapGesture)
controllerView.addGestureRecognizer(doubleTapGesture)
Expand Down

0 comments on commit cf56d1c

Please sign in to comment.