EXC_BAD_ACCESS发生在H264编码CMSampleBuffer上

时间:2019-06-19 11:56:47

标签: ios h.264 cvpixelbuffer cmsamplebuffer video-toolbox

尝试将CMSampleBuffer转换为H264编码的数据

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

有人可以帮我弄清楚发生了什么事吗?

P.S-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)

0 个答案:

没有答案