2

I've been banging my head on the wall for hours trying to create a MTAudioProcessingTapCallbacks from the MediaToolbox library, using Swift 2. I found this great article with an implementation in Objective-C, so I thought I would try to re-write it in Swift, so that I can understand it better, however that is proving a bit beyond me at the moment.

The main issue is that I can't find a way to cast the return value of MTAudioProcessingTapGetStorage(tap) (which is an UnsafeMutablePointer<Void>) to an RMStreamer, which appears to be a feature of the original implementation:

RMStreamer *streamer = (__bridge RMStreamer *) MTAudioProcessingTapGetStorage(tap);

If I attempt this in the most obvious way:

var streamer = MTAudioProcessingTapGetStorage(tap) as! RMStreamer

I get a warning saying:

Cast from UnsafeMutablePointer (aka 'UnsafeMutablePointer<()> to unrelated type RMStreamer always fails)"

So, I found out that the __bridge is somehow equivalent to takeUnretainedValue() but that method does not seem to be available anywhere in this context. I suspect I'm being rather ignorant, but does anyone know if it is possible to implement this kind of thing in Swift 2, or am I better off just learning Objective-C and using that for MediaToolbox stuff in future?

Sam Salisbury
  • 1,066
  • 11
  • 19
  • Hey Sam! How did you manage to create the actual tap callbacks? I'm stuck at that point... for example `var callbacks: MTAudioProcessingTapCallbacks ... callbacks.prepare = ???` – Praveen Sharma Nov 17 '15 at 19:35

1 Answers1

2

Well, I eventually figured out how to get this little piece of the puzzle compiling (I still haven't tested it at runtime), using:

let streamer = Unmanaged<RMStreamer>.fromOpaque(COpaquePointer(MTAudioProcessingTapGetStorage(tap))).takeUnretainedValue()

All I found this out from from https://stackoverflow.com/a/30788165/73237

UPDATE To make this a bit easier to use, wrote the following swift class, mimicking the obj-c nomenclature..

class __bridge<T:AnyObject> {
    class func from(p: UnsafeMutablePointer<()>) -> T {
        return Unmanaged<T>.fromOpaque(COpaquePointer(p)).takeUnretainedValue()
    }
}

Which can be invoked like this, for example:

let streamer = __bridge<RMStreamer>.from(MTAudioProcessingTapGetStorage(tap))
Community
  • 1
  • 1
Sam Salisbury
  • 1,066
  • 11
  • 19