我想在AVCapture框架中限制视频捕获帧速率

时间:2012-02-29 11:09:17

标签: ios accessibility avcapturesession

我正试图为我的应用程序限制我的视频捕获帧速率,因为我发现它正在影响VoiceOver的性能。

目前,它从摄像机捕获帧,然后尽快使用OpenGL例程处理帧。我想在捕获过程中设置一个特定的帧速率。

我希望能够通过使用videoMinFrameDuration或minFrameDuration来实现这一点,但这似乎对性能没有任何影响。有什么想法吗?

    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) 
{
    if ([device position] == AVCaptureDevicePositionBack) 
    {
        backFacingCamera = device;
                    //  SET SOME OTHER PROPERTIES
    }
}


// Create the capture session
captureSession = [[AVCaptureSession alloc] init];

// Add the video input  
NSError *error = nil;
videoInput = [[[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error] autorelease];

// Add the video frame output   
videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoOutput setAlwaysDiscardsLateVideoFrames:YES];

[videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
[videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];



// Start capturing
if([backFacingCamera supportsAVCaptureSessionPreset:AVCaptureSessionPreset1920x1080])
{
    [captureSession setSessionPreset:AVCaptureSessionPreset1920x1080]; 
    captureDeviceWidth = 1920; 
    captureDeviceHeight = 1080;
    #if defined(VA_DEBUG)
    NSLog(@"Video AVCaptureSessionPreset1920x1080");
    #endif
}
else  do some fall back stuff

// If you wish to cap the frame rate to a known value, such as 15 fps, set 
// minFrameDuration.
AVCaptureConnection *conn = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
if (conn.supportsVideoMinFrameDuration)
    conn.videoMinFrameDuration = CMTimeMake(1,2);
else
    videoOutput.minFrameDuration = CMTimeMake(1,2);


if ([captureSession canAddInput:videoInput]) 
    [captureSession addInput:videoInput];


if ([captureSession canAddOutput:videoOutput])
    [captureSession addOutput:videoOutput];

if (![captureSession isRunning])
    [captureSession startRunning];

有什么想法吗?我错过了什么吗?这是节流的最好方法吗?

AVCaptureConnection *conn = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
if (conn.supportsVideoMinFrameDuration)
    conn.videoMinFrameDuration = CMTimeMake(1,2);
else
    videoOutput.minFrameDuration = CMTimeMake(1,2);

2 个答案:

答案 0 :(得分:6)

Mike Ullrich的回答直到ios 7.这两种方法在ios7中不幸被弃用了。您必须在AVCaptureDevice本身上设置activeVideo{Min|Max}FrameDuration。类似的东西:

int fps                             = 30;  // Change this value
AVCaptureDevice *device             = ...; // Get the active capture device
[device lockForConfiguration:nil];
[device setActiveVideoMinFrameDuration:CMTimeMake(1, fps)];
[device setActiveVideoMaxFrameDuration:CMTimeMake(1, fps)];
[device unlockForConfiguration];

答案 1 :(得分:2)

原来你需要设置videoMinFrameDuration和videoMaxFrameDuration才能使其中任何一个工作。

例如:

[conn setVideoMinFrameDuration:CMTimeMake(1,1)];
[conn setVideoMaxFrameDuration:CMTimeMake(1,1)];