I am trying to retrieve a CVPixelBufferRef from CMSampleBufferRef in-order to alter the CVPixelBufferRef to overlay a watermark on the fly.
I am using CMSampleBufferGetImageBuffer(sampleBuffer)
in-order to achieve that. I am printing the result of the returned CVPixelBufferRef, but its always null.
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
NSLog(@"PixelBuffer %@",pixelBuffer);
...
}
I there anything I am missing?
After hours of debugging, it turns out that the sample might be a video or audio sample. So trying to get CVPixelBufferRef from an audio buffer returns null.
I solved it by checking the sample type before proceeding. Since I am not interested in the audio samples I simply return when its an audio sample.
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CMFormatDescriptionRef formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer);
CMMediaType mediaType = CMFormatDescriptionGetMediaType(formatDesc);
//Checking sample type before proceeding
if (mediaType == kCMMediaType_Audio)
{return;}
//Processing the sample...
}