So in my app, I am recording video. I want to cap my fps of the recorded video to 15 fps because any faster and I have issues with processing every frame. The app is targeted at iOS 5.0+, so I don't have to worry about older versions of iOS than that.
I know that to set a max fps, I can use AVCaptureConnection's setVideoMinFrameDuration. I also know that to get it to actually work, I also have to setVideoMaxFrameDuration. However, it appears that on my iPad, the AVCaptureConnection's isVideoMinFrameDurationSupported always returns false, and thus I never set the videoMinFrameDuration, and end up having to fall back to setting the AVCAptureVideoDataOutput's minFrameDuration (which is a deprecated call, and causes warnings and so on. Can anyone explain why I can't set the videoMinFrameDuration?
Code:
AVCaptureVideoDataOutput *videoDataOut = [[AVCaptureVideoDataOutput alloc] init];
NSDictionary *settings = [[NSDictionary alloc]
initWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange],
(id)kCVPixelBufferPixelFormatTypeKey, nil];
videoDataOut.videoSettings = settings;
captureQueue = dispatch_queue_create("videoCaptureQueue", NULL);
[videoDataOut setSampleBufferDelegate:self queue:captureQueue];
videoDataOut.alwaysDiscardsLateVideoFrames = YES;
AVCaptureConnection *conn = [videoDataOut connectionWithMediaType:AVMediaTypeVideo];
// This if block is failing for some reason even though I'm running iOS 5.0+
if ([conn isVideoMinFrameDurationSupported] && [conn isVideoMaxFrameDurationSupported]){
[conn setVideoMinFrameDuration:CMTimeMake(1, pParams->fps)];
[conn setVideoMaxFrameDuration:CMTimeMake(1, pParams->fps)];
}
else {
videoDataOut.minFrameDuration = CMTimeMake(1, pParams->fps);
}
[captureSession addOutput:videoDataOut];
dispatch_release(captureQueue);
I figured out my issue.You must add the output to the capture session BEFORE trying to set the minFrameDuration (or, for that matter, pulling out the AVCaptureConnection).
So, new code looks like this:
AVCaptureVideoDataOutput *videoDataOut = [[AVCaptureVideoDataOutput alloc] init];
NSDictionary *settings = [[NSDictionary alloc]
initWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange],
(id)kCVPixelBufferPixelFormatTypeKey, nil];
videoDataOut.videoSettings = settings;
captureQueue = dispatch_queue_create("videoCaptureQueue", NULL);
[videoDataOut setSampleBufferDelegate:self queue:captureQueue];
videoDataOut.alwaysDiscardsLateVideoFrames = YES;
[captureSession addOutput:videoDataOut];
AVCaptureConnection *conn = [videoDataOut connectionWithMediaType:AVMediaTypeVideo];
if ([conn isVideoMinFrameDurationSupported] && [conn isVideoMaxFrameDurationSupported]){
[conn setVideoMinFrameDuration:CMTimeMake(1, pParams->fps)];
[conn setVideoMaxFrameDuration:CMTimeMake(1, pParams->fps)];
}
dispatch_release(captureQueue);