我在AVAssetWriterInputPixelBufferAdaptor中使用pixelBufferPool来创建与append方法一起使用的像素缓冲区。创建4个缓冲区后,pixelBufferPool属性变为NULL;
我这样设置我的编写器,输入和适配器:
- (BOOL) setupRecorder {
NSError *error = nil;
if([[NSFileManager defaultManager] fileExistsAtPath:[[self tempFileURL] path]])
[[NSFileManager defaultManager] removeItemAtURL:[self tempFileURL] error:&error];
assetWriter = [[AVAssetWriter alloc] initWithURL: [self tempFileURL]
fileType:AVFileTypeQuickTimeMovie
error:&error];
if (error) {
NSLog(@"Error creating asset writer: %@", error);
[assetWriter release];
return NO;
}
// writer
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:videoWidth], AVVideoWidthKey,
[NSNumber numberWithInt:videoHeight], AVVideoHeightKey,
nil];
assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
NSDictionary *bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
nil];
adaptor = [[AVAssetWriterInputPixelBufferAdaptor alloc] initWithAssetWriterInput:assetWriterInput sourcePixelBufferAttributes:bufferAttributes];
[adaptor retain];
assetWriterInput.expectsMediaDataInRealTime = YES;
[assetWriter addInput:assetWriterInput];
return YES;
}
我用这个来分发像素缓冲区:
- (CVPixelBufferRef) createPixelBufferRef {
CVPixelBufferPoolRef pixelBufferPool = adaptor.pixelBufferPool;
CVPixelBufferRef pixelBuffer = NULL;
CVReturn cvReturn = CVPixelBufferPoolCreatePixelBuffer(NULL, pixelBufferPool, &pixelBuffer);
if(cvReturn != kCVReturnSuccess)
NSLog(@"CVPixelBuffePoolCreatePixelBuffer: %d", cvReturn);
bufferCreatedCount++;
return pixelBuffer;
}
当我完成将像素缓冲区传递给appendPixelBuffer时,我使用CVPixelBufferRelease释放了像素缓冲区。在此变为NULL之前,我永远不会调用markAsFinished,endSessionAtSourceTime或finishWriting。此外,适配器本身不会变为NULL。
我读过的大多数帖子都谈到由于适配器配置错误而导致池从一开始就不存在,但是我的存在在那里,但是只是很短的时间。有人看到过这种行为吗?
最佳答案
如果发生像素缓冲区适配器错误,则可能会发生这种情况。像素缓冲器适配器可能由于帧顺序困惑或显示时间相同而进入错误状态。
关于ios - AVAssetWriterInputPixelBufferAdaptor pixelBufferPool在一段时间后变为NULL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6414138/