Creating CMSampleBufferRef from the data

时间秒杀一切 提交于 2019-12-04 08:37:16

问题


I am trying to create a CMSampleBuffer Ref from the data and trying to feed it to AVAssetWriter. But asset writer is failing to create the movie from the data. Following is the code to create the CMSampleBufferRef.

CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(cvimgRef,0);

uint8_t *buf=(uint8_t *)CVPixelBufferGetBaseAddress(cvimgRef);

int width = 480;
int height = 360;
int bitmapBytesPerRow   = width*4;
int bitmapByteCount     = bitmapBytesPerRow*height;


CVPixelBufferRef pixelBufRef = NULL;
CMSampleBufferRef newSampleBuffer = NULL;
CMSampleTimingInfo timimgInfo = kCMTimingInfoInvalid;
CMSampleBufferGetSampleTimingInfo(sampleBuffer, 0, &timimgInfo);

OSStatus result = 0;

OSType pixFmt = CVPixelBufferGetPixelFormatType(cvimgRef);

CVPixelBufferCreateWithBytes(kCFAllocatorDefault, width, height, pixFmt, buf, bitmapBytesPerRow, NULL, NULL, NULL, &pixelBufRef);

CMVideoFormatDescriptionRef videoInfo = NULL;

result = CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBufRef, &videoInfo);

CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBufRef, true, NULL, NULL, videoInfo, &timimgInfo, &newSampleBuffer);

Movie creation works fine when we use the original CMSampleBufferRef obtained from the AVFoundation data output callback method.

But the same fails when I try to create the movie using the custom CMSampleBufferRef. Asset writer throws the following error:

The operation couldn’t be completed. (AVFoundationErrorDomain error -11800.)

Please help me out in resolving this issue.


回答1:


You should look into AVAssetWriterInputPixelBufferAdaptor - it accepts CVPixelBuffers so you don't need to convert the CVPixelBuffer in a CMSampleBuffer.

here is a link to a thread about it on the apple dev forum -> https://devforums.apple.com/thread/70258?tstart=0

Also - any chance you could post your project file or sample code of the capture movie working -- I am using the default CMSampleBuffer from the AVFoundation data output callback method - but when I save it to camera roll it is all black except the last 5 frames which I have to manually scrub to :S

any help in regards to my issue would be greatly appreciated.

Cheers,

Michael




回答2:


    The operation couldn’t be completed. (AVFoundationErrorDomain error -11800.)

For this error, it always occur when timingInfo is invalid. it need set it to valid values with PTS and Duration.

CMSampleTimingInfo timingInfo = kCMTimingInfoInvalid;
timingInfo.presentationTimeStamp = pts; 
timingInfo.duration = duration;


来源:https://stackoverflow.com/questions/3788231/creating-cmsamplebufferref-from-the-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!