I want to throttle video capture frame rate in AVCapture framework

有些话、适合烂在心里 提交于 2020-01-12 08:47:29

问题


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

回答1:


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



回答2:


Turns out you need to set both videoMinFrameDuration and videoMaxFrameDuration for either one to work.

eg:

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


来源:https://stackoverflow.com/questions/9497959/i-want-to-throttle-video-capture-frame-rate-in-avcapture-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!