我在 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。
我读过的大多数帖子都谈到由于适配器配置错误而导致池从一开始就没有,但是我的在那里,但只是很短的时间。其他人看到这种行为吗?