试图将 a 转换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
被扔了。
谁能帮我弄清楚发生了什么?
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)