1

Trying to convert a CMSampleBuffer to H264 encoded data

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;
        }
     });
}

The above method is continuously called. It seems to throw EXC_BAD_ACCESS.

I tried using NSZombie objects but still couldn't figure out what is causing it.

I tried creating a copy of the CMSampleBufferRef and passing that to make sure it is not getting deallocated. Still EXC_BAD_ACCESS is thrown.

Can anyone help me in figuring out what is happening?

P.S - The CMSampleBuffer is created from 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)
Aravind G S
  • 392
  • 3
  • 17

0 Answers0