我正在嘗試從 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];
...
}
});
}
uj5u.com熱心網友回復:
從頭檔案中的captureOutput:didOutputSampleBuffer:fromConnection:討論AVCaptureVideoDataOutput.h:
需要參考
CMSampleBuffer此方法范圍之外的物件的客戶端必須先參考CFRetain,然后CFRelease在完成后參考。
所以看起來你需要保留那些樣本緩沖區,因為它們超出了范圍!不要忘記稍后釋放它們,否則會泄漏大量記憶體。
我忘記了 Objective-C 中的 ARC 不管理CoreFoundation物件。
然后,頭檔案繼續警告不要將樣本緩沖區保留太久,以免丟幀。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367817.html
標籤:ios 目标-c 厘米采样缓冲区 cmsamplebufferref
