Skip to content

Commit

Permalink
Wrap flag check into static function
Browse files Browse the repository at this point in the history
  • Loading branch information
danielebogo committed Nov 14, 2024
1 parent de4b916 commit 22c2c8d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public enum FeatureFlag: String, CaseIterable {

/// Replace Subscribe/Unsubscribe with Follow/Unfollow
case useFollowNaming

/// Use a cookie to manage `MTAudioProcessingTap` deallocation
case useDefaultPlayerTapCookie

Expand Down
49 changes: 20 additions & 29 deletions podcasts/DefaultPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
}
}

private static func unretainedDefaultPlayer(for tap: MTAudioProcessingTap) -> DefaultPlayer? {
if FeatureFlag.useDefaultPlayerTapCookie.enabled {
let cookie = Unmanaged<AudioProcessingTapProxy>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
guard let player = cookie.input else { return nil }
return player
} else {
return Unmanaged<DefaultPlayer>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
}
}

private func createAudioMix() {
guard audioMix == nil else { return }

Expand Down Expand Up @@ -319,13 +329,8 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
let tapInit: MTAudioProcessingTapInitCallback = { tap, clientInfo, tapStorageOut in
tapStorageOut.pointee = clientInfo

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 referenceToSelf = DefaultPlayer.unretainedDefaultPlayer(for: tap) else {
return
}

referenceToSelf.peakLimiter = nil
Expand All @@ -341,16 +346,11 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
}

let tapPrepare: MTAudioProcessingTapPrepareCallback = { tap, maxFrames, processingFormat in
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 referenceToSelf = DefaultPlayer.unretainedDefaultPlayer(for: tap) else {
return
}

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

let tapUnprepare: MTAudioProcessingTapUnprepareCallback = { tap in
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 referenceToSelf = DefaultPlayer.unretainedDefaultPlayer(for: tap) else {
return
}

if let peakLimiter = referenceToSelf.peakLimiter {
AudioUnitUninitialize(peakLimiter)
AudioComponentInstanceDispose(peakLimiter)
Expand All @@ -386,13 +382,8 @@ class DefaultPlayer: PlaybackProtocol, Hashable {
}

let tapProcess: MTAudioProcessingTapProcessCallback = { tap, numberFrames, _, bufferListInOut, numberFramesOut, flagsOut in
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 referenceToSelf = DefaultPlayer.unretainedDefaultPlayer(for: tap) else {
return
}

let currentSampleCount = referenceToSelf.sampleCount
Expand Down

0 comments on commit 22c2c8d

Please sign in to comment.