调用AVAssetWriterInputPixelBufferAdaptor的appendPixelBuffer导致EXC_BAD_ACCESS

时间:2011-06-15 21:29:23

标签: ios iphone opengl-es avassetwriter

我正在从这个

的OpenGL层生成一个CVPixelBufferRef
- (CVPixelBufferRef) getGLPixelBuf {
    int s = 1;
    UIScreen * screen = [UIScreen mainScreen];
    if ([screen respondsToSelector:@selector(scale)]){
        s = (int)[screen scale];
    }
    const int w = self.frame.size.width/2;
    const int h = self.frame.size.height/2;
    const NSInteger my_data_length = 4 * w * h * s * s;
    // allocate array and read pixels into it.
    GLubyte * buffer = malloc(my_data_length);

    GLint readType;
    GLint readFormat;
    glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &readType);
    glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &readFormat);

    glReadPixels(0, 0, w * s, h * s, readFormat, readType, buffer);

    // gl renders "upside down" so swap top to bottom into new array.
    GLubyte * buffer2 = malloc(my_data_length);

    for(int y = 0; y < h*s; y++){
        memcpy(buffer2 + (h * s - 1 - y) * 4 * w * s, buffer + (4 * y * w * s), 4 * w * s);
    }

    free(buffer);
    CVPixelBufferRef pixel_buffer = NULL;
    CVPixelBufferCreateWithBytes (NULL, w * 2, h * 2, kCVPixelFormatType_32BGRA, buffer2 , 4 * w * s, NULL, 0, NULL, &pixel_buffer);
    free(buffer2);

    return pixel_buffer;
}

然后将像素缓冲区传递给我的辅助类中的AVAssetWriterInputPixelBufferAdaptor,如下所示:

- (void)recordFrame {
    if([recorder isRecording]){
        CVPixelBufferRef pixelBuffer = [self getGLPixelBuf];
        CVPixelBufferLockBaseAddress(pixelBuffer, 0);

        [recorder appendPixelBuffer:pixelBuffer withPresentationTime:camera.lastSampleTime];

        CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
        CVPixelBufferRelease(pixelBuffer);
    }
}

但是在帮助程序类中,以下是

- (BOOL)appendPixelBuffer:(CVPixelBufferRef)pixelBuffer withPresentationTime:(CMTime)presentationTime {
    if (writerInput.readyForMoreMediaData)     
        return [adaptor appendPixelBuffer:pixelBuffer withPresentationTime:presentationTime];  
    return NO;
}
当我进行appendPixelBuffer调用时,

会导致EXC_BAD_ACCESS。我启用了NSZombieEnabled,但它没有向我提供任何信息。记录仪初始化为与背景高度和OpenGL层宽度相同的高度和宽度。适配器配置为kCVPixelFormatType_32BGRA像素格式以及像素缓冲区。

任何帮助表示赞赏!谢谢!

1 个答案:

答案 0 :(得分:0)

将附加到适配器时不要锁定像素缓冲区,并且不保留方法- (CVPixelBufferRef) getGLPixelBuf;返回的pixel_buffer,要在CVPixelBufferRelease(pixelBuffer);方法中使用- (void)recordFrame;,之前需要保留pixel_buffer从- (CVPixelBufferRef) getGLPixelBuf;

返回