首页 > 解决方案 > EXC_BAD_ACCESS 发生在 H264 编码 CMSampleBuffer 上

问题描述

试图将 a 转换CMSampleBufferH264编码数据

aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

- (void) encode:(CMSampleBufferRef )sampleBuffer
{
     dispatch_sync(aQueue, ^{
         self->frameCount++;
        CVImageBufferRef imageBuffer = (CVImageBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);

        CMTime presentationTimeStamp = CMTimeMake(self->frameCount, 1);
        VTEncodeInfoFlags flags;

        OSStatus statusCode = VTCompressionSessionEncodeFrame(self->EncodingSession,
                                                         imageBuffer,
                                                         presentationTimeStamp,
                                                         kCMTimeInvalid,
                                                         NULL, NULL, &flags);
        if (statusCode != noErr) {
            self->error = @"H264: VTCompressionSessionEncodeFrame failed "; 
            VTCompressionSessionInvalidate(self->EncodingSession);
            self->EncodingSession = NULL;
            self->error = NULL;
            return;
        }
     });
}

上述方法被连续调用。好像扔了EXC_BAD_ACCESS

我尝试使用NSZombie对象,但仍然无法弄清楚是什么原因造成的。

我尝试创建一个副本CMSampleBufferRef并传递它以确保它不会被释放。还是EXC_BAD_ACCESS被扔了。

谁能帮我弄清楚发生了什么?

PS -CMSampleBuffer创建自CVPixelBuffer

var sampleBfr:CMSampleBuffer?
let scale = CMTimeScale(USEC_PER_SEC)
let pts = CMTime(value: CMTimeValue(NSDate().timeIntervalSince1970 * Double(scale)), timescale: scale)
var timingInfo = CMSampleTimingInfo(duration: CMTime.invalid,
                       presentationTimeStamp: pts,
                             decodeTimeStamp: CMTime.invalid)
var videoDesc:CMVideoFormatDescription? = nil
let _:OSStatus = CMVideoFormatDescriptionCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: pxBuffer!, formatDescriptionOut: &videoDesc)
let _:OSStatus = CMSampleBufferCreateReadyWithImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: pxBuffer!, formatDescription: videoDesc!, sampleTiming: &timingInfo, sampleBufferOut: &sampleBfr)

标签: iosh.264cvpixelbuffercmsamplebuffervideo-toolbox

解决方案


推荐阅读