CMSampleBufferGetDataBuffer() returns nil value - Cocoa Swift

家住魔仙堡 提交于 2020-04-30 08:23:06

问题


I am trying to capture my system's screen and process the data. But I get nil value for CMSampleBufferGetDataBuffer for the sample buffer I get in captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) delegate method. Any idea? below is my code:

import Cocoa
import AVFoundation

class ViewController: NSViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

    private lazy var sampleBufferDelegateQueue = DispatchQueue(label: "VideoCapture")
    private lazy var captureSession: AVCaptureSession = {
        let session = AVCaptureSession()
        session.sessionPreset = .hd1280x720

        if let input = AVCaptureScreenInput.init(displayID: CGMainDisplayID()) {
            session.addInput(input)
        }


        let output = AVCaptureVideoDataOutput()
        output.videoSettings = [
            kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
            kCVPixelBufferMetalCompatibilityKey as String: true
        ]
        output.setSampleBufferDelegate(self, queue: self.sampleBufferDelegateQueue)
        session.addOutput(output)

        return session
    }()


    @IBAction func startAction(_ sender: Any) {
        self.start()
    }
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func start() {
        guard !self.captureSession.isRunning else {
            return
        }

        self.captureSession.startRunning()
    }

    func stop() {
        guard self.captureSession.isRunning else {
            return
        }

        self.captureSession.stopRunning()
    }


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


         let blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
        print(blockBuffer ?? "nil")

    }
}

来源:https://stackoverflow.com/questions/61078336/cmsamplebuffergetdatabuffer-returns-nil-value-cocoa-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!