[英]Call to appendPixelBuffer of AVAssetWriterInputPixelBufferAdaptor Causes EXC_BAD_ACCESS
[英]AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer: EXC_BAD_ACCESS
最近幾天我有一個使我瘋狂的問題。 關於AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer方法。 不管我做什么,它總是在此行上引起EXC_BAD_ACCESS。 我知道有幾篇關於它的文章,但是沒人能幫上忙。
這是我的情況:我正在使用OpenGLES渲染到紋理中,並且我想根據這些紋理創建視頻。 這是我的代碼:
創建FBO,附加紋理和pixelBuffer以填充渲染:
glGenFramebuffers(1, &_secondFrameBuffer);
glGenRenderbuffers(1, &_depthRenderBuffer);
CVPixelBufferPoolCreatePixelBuffer(NULL, [_videoWriter.pixelBufferInput pixelBufferPool], &_fboTexturePixelBuffer);
CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
_textureCache,
_fboTexturePixelBuffer,
NULL,
GL_TEXTURE_2D,
GL_RGBA,
(int)size.width,
(int)size.height,
GL_BGRA,
GL_UNSIGNED_BYTE,
0,
&_fboTexture);
glBindTexture(CVOpenGLESTextureGetTarget(_fboTexture), CVOpenGLESTextureGetName(_fboTexture));
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindRenderbuffer(GL_RENDERBUFFER, _depthRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size.width, size.height);
glBindFramebuffer(GL_FRAMEBUFFER, _secondFrameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, CVOpenGLESTextureGetName(_fboTexture), 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _secondFrameBuffer);
AVAssetWriter創建:
_dataQueue = dispatch_queue_create("data_queue", NULL);
_assetWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeQuickTimeMovie error:nil];
int width = [UIScreen mainScreen].applicationFrame.size.width * [UIScreen mainScreen].scale;
int height = [UIScreen mainScreen].applicationFrame.size.height * [UIScreen mainScreen].scale;
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:width], AVVideoWidthKey,
[NSNumber numberWithInt:height], AVVideoHeightKey,
nil];
_assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
_assetWriterInput.expectsMediaDataInRealTime = YES;
NSDictionary *bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
[NSNumber numberWithBool:YES], kCVPixelBufferOpenGLESCompatibilityKey,
nil];
_assetWriterInputPixelBufferAdaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:_assetWriterInput sourcePixelBufferAttributes:bufferAttributes];
[_assetWriter addInput:_assetWriterInput];
_presentationTime = kCMTimeZero;
[_assetWriter startWriting];
[_assetWriter startSessionAtSourceTime:_presentationTime];
然后,我只是嘗試附加像素Buffer ...
- (void)appendPixelBuffer:(CVPixelBufferRef)pixelBuffer
{
dispatch_async(_dataQueue, ^{
if (pixelBuffer != NULL) {
[_assetWriterInputPixelBufferAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:_presentationTime];
CMTime frameTime = CMTimeMake(1, 30);
_presentationTime = CMTimeAdd(_presentationTime, frameTime);
} else {
NSLog(@"NULL PixelBuffer !");
}
});
}
崩潰-> EXC_BAD_ACCESS
誰能幫我!
謝謝 !
我在方法- (void)appendPixelBuffer:(CVPixelBufferRef)pixelBuffer
看到了您的問題。 如果您使用ARC,請確保您的pixelBuffer沒有在異步隊列上釋放,請確保嘗試此代碼。
- (void)appendPixelBuffer:(CVPixelBufferRef)pixelBuffer
{
CVPixelBufferRetain(pixelBuffer);
dispatch_async(_dataQueue, ^{
if (pixelBuffer != NULL) {
[_assetWriterInputPixelBufferAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:_presentationTime];
CMTime frameTime = CMTimeMake(1, 30);
_presentationTime = CMTimeAdd(_presentationTime, frameTime);
CVPixelBufferRelease(pixelBuffer);
} else {
NSLog(@"NULL PixelBuffer !");
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.