I'm trying to set the kCMSampleAttachmentKey_DisplayImmediately for each of my samples in the CMSampleBuffer.
So far I've tried getting dictionaries (Dictionary<NSObject, AnyObject>, NSDictionary, CFDictionary), and invoked CMSetAttachment on both CMSampleBuffer and its attachments.
For retrieving attachments I use CMSampleBufferGetSampleAttachmentsArray.
Any ideas how can I set those flags in Swift?
Here's the solution (probably far from perfect) that works with Swift 4 (should work with 3).
func setSampleBufferAttachments(_ sampleBuffer: CMSampleBuffer) {
    let attachments: CFArray! = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true)
    let dictionary = unsafeBitCast(CFArrayGetValueAtIndex(attachments, 0),
        to: CFMutableDictionary.self)
    let key = Unmanaged.passUnretained(kCMSampleAttachmentKey_DisplayImmediately).toOpaque()
    let value = Unmanaged.passUnretained(kCFBooleanTrue).toOpaque()
    CFDictionarySetValue(dictionary, key, value)
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With