我正在尝试从 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];
...
}
});
}