Accessing/Returning Data within a Closure that returns Void

Hello,

I am new to Swift and have a specific requirement needing to be implemented through the VideoToolbox API. I am using MethodChannel() from Flutter SDK to invoke Swift functions. I need access to the HW video decoder and the plan is to receive encoded frames from flutter application, decode with VideoToolbox API, then return the decoded image back through the MethodChannel().

I am having trouble checking if the session succeeds in decoding a frame. The callback given to VTDecompressionSessionDecodeFrame() via the VTDecompressionOutputCallbackRecord class may only return a void. However, this callback recieves as argument the decoded image buffer and status, which I have no way of accessing at the moment; this is essentially my problem.

I tried some method such as creating a variable at a higher level scope but I get compiler error A C function pointer cannot be formed from a closure that captures context. It was also quite frustrating that I simply cannot get the base address of a variable like in C/C++ so I can perform memcpy(), but that is another topic.

So my question is, how may I return/access data within a closure that may only return void? Are there any helpful classes to facilitate communication between the closure and function calling VTDecompressionSessionDecodeFrame(). My idea would be to have the calling function wait until the value of the image buffer or status changes. This doesn't sound like a good implementation but it's only what I can think of at the moment. Any help would be appreciated. Thank you.

Answered by capp in 740045022

Ok, I can get the status now. Just as I expected I have more debugging to do for decoding.

But what I did to fix the issue was use this override for VTDecompressionSessionDecodeFrame() instead of the other option:

func VTDecompressionSessionDecodeFrame(
    _ session: VTDecompressionSession,
    sampleBuffer: CMSampleBuffer,
    flags decodeFlags: VTDecodeFrameFlags,
    infoFlagsOut: UnsafeMutablePointer<VTDecodeInfoFlags>?,
    outputHandler: @escaping VTDecompressionOutputHandler
) -> OSStatus
Accepted Answer

Ok, I can get the status now. Just as I expected I have more debugging to do for decoding.

But what I did to fix the issue was use this override for VTDecompressionSessionDecodeFrame() instead of the other option:

func VTDecompressionSessionDecodeFrame(
    _ session: VTDecompressionSession,
    sampleBuffer: CMSampleBuffer,
    flags decodeFlags: VTDecodeFrameFlags,
    infoFlagsOut: UnsafeMutablePointer<VTDecodeInfoFlags>?,
    outputHandler: @escaping VTDecompressionOutputHandler
) -> OSStatus
Accessing/Returning Data within a Closure that returns Void
 
 
Q