保留CMSampleBufferRef会导致随机崩溃

7

我使用 captureOutput:didOutputSampleBuffer:fromConnection: 来追踪帧数。对于我的用例,我只需要存储最后一帧,并在应用程序进入后台时使用它。

这是我的代码示例:

@property (nonatomic, strong) AVCaptureVideoDataOutput *videoDataOutput;
@property (atomic) CMSampleBufferRef currentBuffer;

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
    // Get a CMSampleBuffer's Core Video image buffer for the media data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, 0);

    // Get the number of bytes per row for the pixel buffer
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);

    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef con = CGBitmapContextCreate(baseAddress, width, height, 8,
                                             bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(con);
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    // Free up the context and color space
    CGContextRelease(con);
    CGColorSpaceRelease(colorSpace);

    // Create an image object from the Quartz image
    //    UIImage *image = [UIImage imageWithCGImage:quartzImage];
    UIImage *image =  [UIImage imageWithCGImage:quartzImage scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationRight];

    // Release the Quartz image
    CGImageRelease(quartzImage);

    return (image);
}
//[self.videoDataOutput setSampleBufferDelegate:self queue:self.sessionQueue];

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    CFRetain(sampleBuffer);

    @synchronized (self) {
        if (_currentBuffer) {
            CFRelease(_currentBuffer);
        }
        self.currentBuffer = sampleBuffer;
    }
}

- (void) goingToBackground:(NSNotification *) notification
{
    UIImage *snapshot = [self imageFromSampleBuffer:_currentBuffer];

    //Doing something with snapshot...
}

问题在于,在某些情况下,我会从imageFromSampleBuffer中收到崩溃。
<Error>: copy_read_only: vm_copy failed: status 1.

CGImageRef quartzImage = CGBitmapContextCreateImage(con);这行代码上发生了崩溃。

我哪里做错了?


你能展示一下你的 imageFromSampleBuffer: 方法的代码吗? - SheffieldKevin
@SheffieldKevin,我编辑了这个问题。 - Rizon
在这里添加了如何获取sampleBuffer所有权的答案:https://dev59.com/t2XWa4cB1Zd3GeqPQtE5#36893192 - k06a
1个回答

0

我认为你需要复制缓冲区,而不仅仅是保留。从captureOutput:didOutputSampleBuffer:fromConnection:的描述中可以看出:

请注意,为了保持最佳性能,一些样本缓冲区直接引用可能需要被设备系统和其他捕获输入重用的内存池。如果多个样本缓冲区长时间引用这些内存池,输入将无法再将新样本复制到内存中,并且这些样本将被丢弃。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接