3

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?

Basel JD
  • 275
  • 3
  • 17

2 Answers2

7

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...

}
Basel JD
  • 275
  • 3
  • 17
-1

Basel JD's answer in Swift 4.0. It just worked for me

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

    guard let formatDescription: CMFormatDescription = CMSampleBufferGetFormatDescription(sampleBuffer) else { return }

    let mediaType: CMMediaType = CMFormatDescriptionGetMediaType(formatDescription)

    if mediaType == kCMMediaType_Audio {
        print("this was an audio sample....")
        return
    }

}
Chrishon Wyllie
  • 209
  • 1
  • 2
  • 11