【发布时间】:2017-01-16 09:19:00
【问题描述】:
我遇到了AVAssetWritter 的问题。有时会发生我的视频写作会话只是挂起。在检查了当前在我的设备上运行的线程后,我发现整个视频处理都在等待copyNextSampleBuffer 返回。我不知道是什么导致了这个问题。有谁成功克服了这个问题?
以下是从仪器捕获的线程转储。它以mach_msg_trap 结束。
视频处理循环
while ([self.assetWriterVideoInput isReadyForMoreMediaData] && !(*completedOrFailed) && !self.cancelled)
{
@autoreleasepool {
CMSampleBufferRef sampleBuffer = [self.assetReaderVideoOutput copyNextSampleBuffer];
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferRef croppedBuffer = NULL;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
CVPixelBufferCreate(kCFAllocatorDefault, self.outputSize.width, self.outputSize.height, CVPixelBufferGetPixelFormatType(pixelBuffer), (__bridge CFDictionaryRef) options, &croppedBuffer);
CIImage *img = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer];
// img processing
[self.context render:img toCVPixelBuffer:croppedBuffer];
if (sampleBuffer != NULL)
{
BOOL success = [self.avPixelAdaptor appendPixelBuffer:croppedBuffer withPresentationTime:sampleTime];
CFRelease(sampleBuffer);
sampleBuffer = NULL;
*completedOrFailed = !success;
}
else
{
*completedOrFailed = YES;
}
CVPixelBufferRelease(croppedBuffer);
}
}
}
更新
资产阅读器的源资产是AVMutableComposition,它由几个指向照片库的AVURLAsset组成(即url = "assets-library://asset/asset.MOV?id=4CA9A2C6-F2D4- 4FDF-AAEC-6335B9BD840A&ext=MOV")。每个源资产需要 2 秒,在源资产 0.6 秒后开始。如果所有源资产都从 0 开始,则视频处理永远不会挂起。
总结
主要问题是:什么情况会导致copyNextSampleBuffer 永远等待退出。文档没有提到这种情况。
【问题讨论】:
-
你在 github 上有一个可以重现问题的小项目吗?
-
不,我没有这样的项目 :(
-
如果你能做一个就好了
-
你有没有在这里取得进步?我有同样的问题,只是读取样本缓冲区以显示在 AVSampleBufferDisplayLayer 中(无写入)
标签: ios avfoundation