9

我在 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。

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

4

2 回答 2

6

我有同样的问题。正如我所知道的,如果CMTime您放入的某些 sappendPixelBuffer:withPresentationTime:相等,就会发生这种情况。例如,如果您使用CMTimeMakeWithSeconds太粗略的时间刻度,就会发生这种情况。

于 2011-12-15T06:56:40.890 回答
4

它可能发生在像素缓冲区适配器错误的情况下。像素缓冲区适配器可能会因帧被乱序或呈现时间相同而进入错误状态。

于 2014-03-14T06:19:35.697 回答