Skip to content

Commit

Permalink
Apply tap proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
danielebogo committed Nov 13, 2024
1 parent 485affc commit de4b916
Showing 1 changed file with 58 additions and 8 deletions.
66 changes: 58 additions & 8 deletions podcasts/DefaultPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,34 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
}

// MARK: - Audio Mix
#if !os(watchOS)
private class AudioProcessingTapProxy {
weak var input: DefaultPlayer?

init(input: DefaultPlayer) {
self.input = input
}

deinit {
FileLog.shared.console("[AudioProcessingTapProxy] Deinit proxy")
}
}

#if !os(watchOS)
private func createAudioMix() {
guard audioMix == nil else { return }

let mutableMix = AVMutableAudioMix()
let audioMixInputParameters = AVMutableAudioMixInputParameters(track: assetTrack)

var clientInfo = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
if FeatureFlag.useDefaultPlayerTapCookie.enabled {
let tapCookie = AudioProcessingTapProxy(input: self)
clientInfo = UnsafeMutableRawPointer(Unmanaged.passRetained(tapCookie).toOpaque())
}

var callbacks = MTAudioProcessingTapCallbacks(
version: kMTAudioProcessingTapCallbacksVersion_0,
clientInfo: UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()),
clientInfo: clientInfo,
init: tapInit,
finalize: tapFinalize,
prepare: tapPrepare,
Expand All @@ -302,16 +319,36 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
let tapInit: MTAudioProcessingTapInitCallback = { tap, clientInfo, tapStorageOut in
tapStorageOut.pointee = clientInfo

let referenceToSelf = Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
let referenceToSelf: DefaultPlayer
if FeatureFlag.useDefaultPlayerTapCookie.enabled {
let cookie = Unmanaged<AudioProcessingTapProxy>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
guard let player = cookie.input else { return }
referenceToSelf = player
} else {
referenceToSelf = Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
}

referenceToSelf.peakLimiter = nil
referenceToSelf.highPassFilter = nil
referenceToSelf.sampleCount = 0
}

let tapFinalize: MTAudioProcessingTapFinalizeCallback = { _ in }
let tapFinalize: MTAudioProcessingTapFinalizeCallback = { tap in
if FeatureFlag.useDefaultPlayerTapCookie.enabled {
FileLog.shared.console("[AudioProcessingTapProxy] Finalize tap: \(tap)\n")
Unmanaged<AudioProcessingTapProxy>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).release()
}
}

let tapPrepare: MTAudioProcessingTapPrepareCallback = { tap, maxFrames, processingFormat in
var referenceToSelf = Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
let referenceToSelf: DefaultPlayer
if FeatureFlag.useDefaultPlayerTapCookie.enabled {
let cookie = Unmanaged<AudioProcessingTapProxy>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
guard let player = cookie.input else { return }
referenceToSelf = player
} else {
referenceToSelf = Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
}

guard let filter = referenceToSelf.createHighPassFilter(maxFrames: maxFrames, processingFormat: processingFormat.pointee) else {
referenceToSelf.handlePlaybackError("Setup high pass filter failed")
Expand All @@ -327,7 +364,14 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
}

let tapUnprepare: MTAudioProcessingTapUnprepareCallback = { tap in
var referenceToSelf = Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
let referenceToSelf: DefaultPlayer
if FeatureFlag.useDefaultPlayerTapCookie.enabled {
let cookie = Unmanaged<AudioProcessingTapProxy>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
guard let player = cookie.input else { return }
referenceToSelf = player
} else {
referenceToSelf = Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
}
if let peakLimiter = referenceToSelf.peakLimiter {
AudioUnitUninitialize(peakLimiter)
AudioComponentInstanceDispose(peakLimiter)
Expand All @@ -342,7 +386,14 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
}

let tapProcess: MTAudioProcessingTapProcessCallback = { tap, numberFrames, _, bufferListInOut, numberFramesOut, flagsOut in
var referenceToSelf = Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
let referenceToSelf: DefaultPlayer
if FeatureFlag.useDefaultPlayerTapCookie.enabled {
let cookie = Unmanaged<AudioProcessingTapProxy>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
guard let player = cookie.input else { return }
referenceToSelf = player
} else {
referenceToSelf = Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
}

let currentSampleCount = referenceToSelf.sampleCount
referenceToSelf.sampleCount += Float64(numberFrames)
Expand All @@ -352,7 +403,6 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
referenceToSelf.handlePlaybackError("MTAudioProcessingTapGetSourceAudio failed")
return
}

return
}

Expand Down

0 comments on commit de4b916

Please sign in to comment.