1

Currently I have an AVCaptureSession with an AVCaptureVideoDataOutput. It is my understanding that the captureOutput delegate function is called every time the camera captures a frame. Which by default means that the captureOutput function is called at 30fps.

I also have a CMMotionManager storing data as the phone moves. The motion manager is running at about 90-100fps.

Is there anyway I could increase the fps the camera takes in frames so that the captureOutput function is called at roughly the same speed as the motionManager?

Any help would be appreciated.

NFarrell
  • 255
  • 1
  • 17

2 Answers2

1

From Apple docs

The following code example illustrates how to select an iOS device’s highest possible frame rate

- (void)configureCameraForHighestFrameRate:(AVCaptureDevice *)device
{
    AVCaptureDeviceFormat *bestFormat = nil;
    AVFrameRateRange *bestFrameRateRange = nil;
    for ( AVCaptureDeviceFormat *format in [device formats] ) {
        for ( AVFrameRateRange *range in format.videoSupportedFrameRateRanges ) {
            if ( range.maxFrameRate > bestFrameRateRange.maxFrameRate ) {
                bestFormat = format;
                bestFrameRateRange = range;
            }
        }
    }
    if ( bestFormat ) {
        if ( [device lockForConfiguration:NULL] == YES ) {
            device.activeFormat = bestFormat;
            device.activeVideoMinFrameDuration = bestFrameRateRange.minFrameDuration;
            device.activeVideoMaxFrameDuration = bestFrameRateRange.minFrameDuration;
            [device unlockForConfiguration];
        }
    }
}
Tiko
  • 485
  • 4
  • 10
  • Thanks for the response Tiko. I actually did something exactly like this and successfully changed the frame rate to make it faster but for some reason the captureOutput function still only is called at 30fps. Do you know why this could be? – NFarrell Jun 22 '17 at 19:15
  • The camera is at 120fps to be exact. – NFarrell Jun 22 '17 at 20:38
1

To solve this problem I changed the AVCaptureDevice's frame rate just like the last answer in this post AVCapture capturing and getting framebuffer at 60 fps in iOS 7

In order to get the captureOutput function to run at the same speed as the camera you have to add the AVCaptureDeviceInput before you change the AVCaptureDevice's frame rate.

Here's what the code ended up looking like for Swift 3:

do{

        input = try AVCaptureDeviceInput(device: backCamera)
        if (error == nil && (captureSession?.canAddInput(input))!)
        {
            captureSession?.addInput(input)

        }

        var finalFormat = AVCaptureDeviceFormat()
        var maxFPS: Double = 0
        for vformat in (backCamera?.formats)!{
            var ranges = (vformat as AnyObject).videoSupportedFrameRateRanges as! [AVFrameRateRange]
            let frameRates = ranges[0]

            if frameRates.maxFrameRate >= maxFPS && frameRates.maxFrameRate <= 120.0{
                maxFPS = frameRates.maxFrameRate
                finalFormat = vformat as! AVCaptureDeviceFormat
            }


        }
        if maxFPS != 0{

            let timeValue = Int64(1200.0 / maxFPS)
            let timeScale: Int64 = 1200
            try backCamera!.lockForConfiguration()

            backCamera!.activeFormat = finalFormat
            //print("CAMER FPS: \(backCamera?.activeFormat.videoSupportedFrameRateRanges.description)\n")
            backCamera!.activeVideoMinFrameDuration = CMTimeMake(timeValue, Int32(timeScale))
            backCamera!.activeVideoMaxFrameDuration = CMTimeMake(timeValue, Int32(timeScale))
            backCamera!.focusMode = AVCaptureFocusMode.autoFocus
            backCamera!.unlockForConfiguration()
        }
    }catch{
        print("Problem setting FPS\n")
        exit(0)
    }
NFarrell
  • 255
  • 1
  • 17