【问题标题】:Dispatch_async in captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer, EXC_BAD_ACCESS error on sample buffercaptureOutput 中的 Dispatch_async:(AVCaptureOutput*)captureOutput didOutputSampleBuffer,样本缓冲区上的 EXC_BAD_ACCESS 错误
【发布时间】:2022-01-04 17:22:58
【问题描述】:

我正在尝试从 captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer 获取样本缓冲区,对其进行处理,然后将其附加到 AVAssetWriter。整个代码都可以工作,但是它变得非常慢,而且我在旧设备上的 fps 很低。
我想把它放在一个 dispatch_async 中以提高性能,但是一旦访问样本缓冲区就会导致 EXC_BAD_ACCESS 错误。
我该如何修复它,同时将代码保留在后台?

queue1 = dispatch_queue_create("testqueue", DISPATCH_QUEUE_SERIAL);
...

-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection{
    
dispatch_async(queue1, ^{
...
if(captureOutput == videoOutput){
//I process the buffer by appending an image to an adaptor
if([writerVideoInput isReadyForMoreMediaData] && recordingAssetWriter.status == 1)
    [adaptor appendPixelBuffer:pxBuffer withPresentationTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)]) //<-- here I get EXC_BAD_ACCESS
}
if(captureOutput == audioOutput){
...
// I then append the audio buffer
if([assetWriterAudioInput isReadyForMoreMediaData] && recordingAssetWriter.status == 1)
    [assetWriterAudioInput appendSampleBuffer:sampleBuffer];
...
}
});

}

【问题讨论】:

    标签: ios objective-c cmsamplebuffer cmsamplebufferref


    【解决方案1】:

    来自AVCaptureVideoDataOutput.h 头文件中的captureOutput:didOutputSampleBuffer:fromConnection: 讨论:

    需要在此方法范围之外引用CMSampleBuffer 对象的客户端必须先CFRetain 它,然后在完成后CFRelease 它。

    所以看起来您需要保留这些样本缓冲区,因为它们超出了范围!不要忘记稍后释放它们,否则会泄漏大量内存。

    我忘记了 Objective-C 中的 ARC 不管理 CoreFoundation 对象。

    然后头文件继续警告不要将样本缓冲区保持太久以免丢帧。

    【讨论】:

    • 谢谢,在 dispatch_async 之前添加 CFRetain(samplebuffer) 然后在其中添加 CFRelease(samplebuffer) 解决了这个问题。
    猜你喜欢
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 2021-12-14
    • 2019-09-14
    • 1970-01-01
    相关资源
    最近更新 更多