Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set CMSampleBuffer samples attachment (Swift)

Tags:

ios

swift

h.264

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?

like image 649
Tomasz Wójcik Avatar asked Apr 08 '15 14:04

Tomasz Wójcik


1 Answers

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)
}
like image 162
Tomasz Wójcik Avatar answered Oct 18 '22 16:10

Tomasz Wójcik