Basel JD
Basel JD

Reputation: 275

CMSampleBufferGetImageBuffer returning null

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?

Upvotes: 3

Views: 2323

Answers (2)

Chrishon Wyllie
Chrishon Wyllie

Reputation: 209

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
    }

}

Upvotes: -1

Basel JD
Basel JD

Reputation: 275

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

}

Upvotes: 7

Related Questions