Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a copy of CMSampleBuffer in Swift 2.0

This has been asked before, but something must have changed in Swift since it was asked. I am trying to store CMSampleBuffer objects returned from an AVCaptureSession to be processed later. After some experimentation I discovered that AVCaptureSession must be reusing its CMSampleBuffer references. When I try to keep more than 15 the session hangs. So I thought I would make copies of the sample buffers. But I can't seem to get it to work. Here is what I have written:

var allocator: Unmanaged<CFAllocator>! = CFAllocatorGetDefault()
var bufferCopy: UnsafeMutablePointer<CMSampleBuffer?>
let err = CMSampleBufferCreateCopy(allocator.takeRetainedValue(), sampleBuffer, bufferCopy)
if err == noErr {
    bufferArray.append(bufferCopy.memory!)
} else {
    NSLog("Failed to copy buffer. Error: \(err)")
}

This won't compile because it says that Variable 'bufferCopy' used before being initialized. I've looked at many examples and they'll either compile and not work or they won't compile.

Anyone see what I'm doing wrong here?

like image 339
Rob Avatar asked Feb 17 '16 21:02

Rob


1 Answers

You can simply pass a CMSampleBuffer? variable (which, as an optional, is implicitly initialized with nil) as inout argument with &:

var bufferCopy : CMSampleBuffer?
let err = CMSampleBufferCreateCopy(kCFAllocatorDefault, buffer, &bufferCopy)
if err == noErr {
    // ...
}
like image 154
Martin R Avatar answered Oct 06 '22 22:10

Martin R