Search code examples
iosswiftavfoundationcore-media

CMSampleBufferGetImageBuffer(sampleBuffer) return nil


I use this code to capture video from camera, but the CMSampleBufferGetImageBuffer(sampleBuffer) always return nil. What is the problem?. Here is the code, I modify the code from this source to adapt for Swift 4 https://github.com/FlexMonkey/CoreImageHelpers/blob/master/CoreImageHelpers/coreImageHelpers/CameraCaptureHelper.swift

import AVFoundation
import CoreMedia
import CoreImage
import UIKit


class CameraCaptureHelper: NSObject
{
let captureSession = AVCaptureSession()
let cameraPosition: AVCaptureDevice.Position

weak var delegate: CameraCaptureHelperDelegate?

required init(cameraPosition: AVCaptureDevice.Position)
{
    self.cameraPosition = cameraPosition

    super.init()

    initialiseCaptureSession()
}

fileprivate func initialiseCaptureSession()
{
    captureSession.sessionPreset = AVCaptureSession.Preset.photo

    guard let camera = AVCaptureDevice.default(.builtInWideAngleCamera,
                                               for: .video, position: cameraPosition)
        else {
            fatalError("Unable to access camera")
    }
    do
    {
        let input = try AVCaptureDeviceInput(device: camera)

        captureSession.addInput(input)
    }
    catch
    {
        fatalError("Unable to access back camera")
    }

    let videoOutput = AVCaptureVideoDataOutput()

    videoOutput.setSampleBufferDelegate(self,
                                        queue: DispatchQueue(label: "sample buffer delegate", attributes: []))

    if captureSession.canAddOutput(videoOutput)
    {
        captureSession.addOutput(videoOutput)
    }

    captureSession.startRunning()
}
}

extension CameraCaptureHelper: AVCaptureVideoDataOutputSampleBufferDelegate
{

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

    connection.videoOrientation = .landscapeRight //AVCaptureVideoOrientation(rawValue: UIApplication.shared.statusBarOrientation.rawValue)!

    guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else
    {
        return
    }

    DispatchQueue.main.async
        {
            self.delegate?.newCameraImage(self,
                                          image: CIImage(cvPixelBuffer: pixelBuffer))
    }
}

}

protocol CameraCaptureHelperDelegate: class
{
func newCameraImage(_ cameraCaptureHelper: CameraCaptureHelper, image: CIImage)
}

Solution

  • You're trying to access the pixel buffer from the "just dropped a sample buffer" callback. The header file says:

    CMSampleBuffer object passed to this delegate method will contain metadata about the dropped video frame, such as its duration and presentation time stamp, but will contain no actual video data.

    You should be doing that from the didOutputSampleBuffer: delegate callback.