How to Release an Mtaudioprocessingtap

How do you release an MTAudioProcessingTap?

I had the same problem. To make it work, I had to reset the player item's audioMix, the tap processor (MYAudioTapProcessor in Apple's sample project) and manually release the MTAudioProcessingTapRef:

MTAudioProcessingTapRef tap = ((AVMutableAudioMixInputParameters *)_audioTapProcessor.audioMix.inputParameters[0]).audioTapProcessor;
_player.currentItem.audioMix = nil;
_audioTapProcessor = nil;
CFRelease(tap);

This causes the finalize callback to be invoked.

Edit: Seems like the CFRelease is not required, see comments.

How to get unprepare and finalize callbacks to fire for multiple MTAudioProcessingTaps?

It looks like the referenced post is the proper way.

In my case, it was an unrelated issue with tearing down the player. It turns out that having strong references to periodicTimeObservers and boundaryTimeObservers still on the AVPlayer caused the player to hang around for longer than expected.

When leaving the view controller (the time at which I want to destroy the AVPlayer) I removed any AVPlayerLayers created from the AVPlayer from their superview and nil-ed them, then took the same steps as stated above, except I did not release the individual taps with CFRelease. This was taken care of by nil-ing _audioMix and _playerItem.

MTAudioProcessingTap EXC_BAD_ACCESS , doesnt always fire the finalize callback. how to Release it?

If VideoMediaInput is deallocated before the tap is deallocated (which can happen as there seems to be no way to synchronously stop a tap), then the tap callback can choke on a reference to your deallocated class.

You can fix this by passing (a wrapped, I guess) weak reference to your class. You can do it like this:

First delete your tap instance variable, and any references to it - it's not needed. Then make these changes:

class VideoMediaInput: NSObject {

class TapCookie {
weak var input: VideoMediaInput?

deinit {
print("TapCookie deinit")
}
}
...

func setupProcessingTap(){
let cookie = TapCookie()
cookie.input = self

var callbacks = MTAudioProcessingTapCallbacks(
version: kMTAudioProcessingTapCallbacksVersion_0,
clientInfo: UnsafeMutableRawPointer(Unmanaged.passRetained(cookie).toOpaque()),
init: tapInit,
finalize: tapFinalize,
prepare: tapPrepare,
unprepare: tapUnprepare,
process: tapProcess)
...

let tapFinalize: MTAudioProcessingTapFinalizeCallback = {
(tap) in
print("finalize \(tap)\n")

// release cookie
Unmanaged<TapCookie>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).release()
}

let tapPrepare: MTAudioProcessingTapPrepareCallback = {
(tap, itemCount, basicDescription) in
print("prepare: \(tap, itemCount, basicDescription)\n")
let cookie = Unmanaged<TapCookie>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()
let selfMediaInput = cookie.input!
...

let tapProcess: MTAudioProcessingTapProcessCallback = {
(tap, numberFrames, flags, bufferListInOut, numberFramesOut, flagsOut) in
print("callback \(bufferListInOut)\n")

let cookie = Unmanaged<TapCookie>.fromOpaque(MTAudioProcessingTapGetStorage(tap)).takeUnretainedValue()

guard let selfMediaInput = cookie.input else {
print("Tap callback: VideoMediaInput was deallocated!")
return
}
...

I'm not sure if the cookie class is necessary, it exists only to wrap the weak reference. Cutting edge Swift experts may know how to mash the weakness through all the teenage mutant ninja raw pointers, but I don't.

Issue with MTAudioProcessingTap on device

This was apparently a bug in 6.0 (which my device was still on). Simulator was on 6.1

Upgraded device to 6.1.2 and errors disappeared.



Related Topics



Leave a reply



Submit