Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to throttle video capture frame rate in AVCapture framework

I am trying to throttle my video capture framerate for my application, as I have found that it is impacting VoiceOver performance.

At the moment, it captures frames from the video camera, and then processes the frames using OpenGL routines as quickly as possible. I would like to set a specific framerate in the capture process.

I was expecting to be able to do this by using videoMinFrameDuration or minFrameDuration, but this seems to make no difference to performance. Any ideas?

    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];

Any ideas? Am I missing something? Is this the best way to throttle?

AVCaptureConnection *conn = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
if (conn.supportsVideoMinFrameDuration)
    conn.videoMinFrameDuration = CMTimeMake(1,2);
else
    videoOutput.minFrameDuration = CMTimeMake(1,2);
like image 286
Mike Ullrich Avatar asked Feb 29 '12 11:02

Mike Ullrich


1 Answers

Mike Ullrich's answer worked up until ios 7. These two methods are unfortunately deprecated in ios7. You have to set the activeVideo{Min|Max}FrameDuration on the AVCaptureDevice itself. Something like:

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];
like image 161
Mr. T Avatar answered Sep 21 '22 12:09

Mr. T