I tried to save the CMSampleBuffer from didOutputSampleBuffer, as the iOS developer docs, I copy it in CFArrayRetainCallBack like blow:
static const void * ObjectRetainCallBack(CFAllocatorRef allocator, const void *value) {
CMSampleBufferRef buffer = (CMSampleBufferRef)value;
if (buffer) {
CMSampleBufferRef new = NULL;
OSStatus status = CMSampleBufferCreateCopy(kCFAllocatorDefault, buffer, &new);
if (status == noErr) {
return new;
}
return NULL;
}
return NULL;
}
static void ObjectReleaseCallBack(CFAllocatorRef allocator, const void *value) {
if (value) {
CFRelease(value);
}
}
CFMutableArrayRef CreateDispatchHoldingArray() {
CFArrayCallBacks callBacks = {
0,
ObjectRetainCallBack,
ObjectReleaseCallBack,
NULL,
NULL
};
return CFArrayCreateMutable(kCFAllocatorDefault, 0, &callBacks);
}
I use the array like below:
- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CFArrayAppendValue(_pixelArray, sampleBuffer);
}
Somebody has better way to do this or any suggestion?
Thanks a lot.
CMSampleBufferCreateCopy is shallow copy, according to the answer from how to deep copy pixelbuffer
recreate the samplebuffer with a new pixelbuffer. then I could cache sample buffers.
But this way cost resource, I can't hold too many CMSampleBufferRefs in memory(each of them takes about 3MB), and the process of deep copy is waste of time.