GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Crashes when setting 60fps using setFrameRate: in GPUImageVideoCamera

Open ngelo opened this issue 12 years ago • 7 comments

On the iPhone 5 and later models, the camera can capture at 1280x720 and 60 fps. However, this cannot be set using the method setFrameRate: in the GPUImageVideoCamera class. I found that only a max value of 30 can be used with the setFrameRate:. When you use 60 with setFrameRate:, the application crashes at line 547 of GPUImageVideoCamera:

[_inputCamera setActiveVideoMinFrameDuration:CMTimeMake(1, _frameRate)];

I have been using a workaround to achieve 1280x720 and 60fps with GPUImageVideoCamera. Below is the code that I have been using:

GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionBack];
// videoCamera.frameRate = 60; /* CANNOT be set this way */

/* This is where 60 fps is set instead */
AVCaptureDevice *inputCamera = videoCamera.inputCamera;
for (AVCaptureDeviceFormat *videoFormat in [inputCamera formats]) {
    CMFormatDescriptionRef videoFormatDescriptionRef = videoFormat.formatDescription;
    Float64 maxFrameRate = ((AVFrameRateRange *) [videoFormat.videoSupportedFrameRateRanges objectAtIndex:0]).maxFrameRate;

    if (maxFrameRate == 60 && CMFormatDescriptionGetMediaSubType(videoFormatDescriptionRef) == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
        if ([inputCamera lockForConfiguration:nil]) {
            inputCamera.activeFormat = videoFormat;
            [inputCamera setActiveVideoMinFrameDuration:CMTimeMake(10, 600)];
            [inputCamera setActiveVideoMaxFrameDuration:CMTimeMake(10, 600)];
            [inputCamera unlockForConfiguration];
        }
    }
}

The main difference between this method of setting the frame rate and the one that is used in setFrameRate: is that my workaround reconfigures the AVCaptureDevice instance of the video camera by changing its active format to one that supports 60 fps.

To achieve 1280x720 and 60 fps in GPUImageVideoCamera, I see 2 possible solutions: 1.) Use my implementation of iterating through formats on the AVCaptureDevice of GPUImageVideoCamera. 2.) Create a new initializer for GPUImageVideoCamera called initWithSessionPreset:cameraPosition:frameRate: with a default value of 30 that would be passed in through the original initializer while still using the solution I described above.

Brad, if you think my solution will work, I can modify the GPUImageVideoCamera class to support this.

Thank you.

ngelo avatar Oct 08 '14 10:10 ngelo

Odd. Other people have been able to set the framerate to 60 FPS: https://github.com/BradLarson/GPUImage/issues/1207

BradLarson avatar Oct 08 '14 16:10 BradLarson

I got same issue. It crashes on 5s with reason *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureVideoDevice setActiveVideoMinFrameDuration:] - the passed activeFrameDuration 1:60 is not supported by the device. Use -activeFormat.videoSupportedFrameRateRanges to discover valid ranges.'

VitalyTimofeev avatar Oct 10 '14 09:10 VitalyTimofeev

@VitalyTimofeev Yes, thats the same error I receive on my iPhone 5. By using activeFormat.videoSupportedFrameRateRanges as the error suggests I was able to get around this error as my code above shows.

ngelo avatar Oct 10 '14 20:10 ngelo

This sounds like a bug in AV Foundation, not GPUImage. Please file a bug report with Apple about this.

BradLarson avatar Oct 12 '14 21:10 BradLarson

@ngelo I got same problem. Did you find any solutions?

sjtujulian avatar May 22 '15 14:05 sjtujulian

@sjtujulian I was able to get the frame rate changes working by subclassing GPUImageVideoCamera and using the following implementation for setFrameRate::

- (void)setFrameRate:(int32_t)frameRate
{
    _frameRate = frameRate;

    // To avoid the video camera freaking out, use the superclass's method of
    // setting the frame rate.
    //
    // TODO: Implement this myself without the superclass.
    if (frameRate == 30) {
        [super setFrameRate:frameRate];
        return;
    }

    AVCaptureDevice *videoDevice = self.inputCamera;
    for(AVCaptureDeviceFormat *vFormat in [videoDevice formats] ) {
        CMFormatDescriptionRef description= vFormat.formatDescription;
        float maxRate = ((AVFrameRateRange*) [vFormat.videoSupportedFrameRateRanges objectAtIndex:0]).maxFrameRate;
        if (maxRate > frameRate - 1 &&
           CMFormatDescriptionGetMediaSubType(description)==kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
            if ([videoDevice lockForConfiguration:nil]) {
                videoDevice.activeFormat = vFormat;
                [videoDevice setActiveVideoMinFrameDuration:CMTimeMake(10, frameRate * 10)];
                [videoDevice setActiveVideoMaxFrameDuration:CMTimeMake(10, frameRate * 10)];
                [videoDevice unlockForConfiguration];
                break;
            }
        }
    }

    [self resetBenchmarkAverage];
}

As you can see I didn't completely finish it as noted with the TODO comment. I'm not sure if this is be best way to do it, but it seems to work for me. Let me know if it fixes your problem.

ngelo avatar May 22 '15 22:05 ngelo

Apple provides code for this here https://developer.apple.com/documentation/avfoundation/avcapturedevice

+ (void)configureCamera:(AVCaptureDevice *)device forFrameRate:(int)frameRate
{
    AVCaptureDeviceFormat *bestFormat = nil;
    AVFrameRateRange *bestFrameRateRange = nil;
    for ( AVCaptureDeviceFormat *format in [device formats] ) {
        for ( AVFrameRateRange *range in format.videoSupportedFrameRateRanges ) {
            //NSLog(@"Format %@",format);
            if ( range.maxFrameRate == frameRate ) {
                bestFormat = format;
                bestFrameRateRange = range;
            }
        }
    }
    
    //NSLog(@"Format %@",bestFormat);
    
    if ( bestFormat ) {
        if ( [device lockForConfiguration:NULL] == YES ) {
            device.activeFormat = bestFormat;
            device.activeVideoMinFrameDuration = bestFrameRateRange.minFrameDuration;
            device.activeVideoMaxFrameDuration = bestFrameRateRange.minFrameDuration;
            [device unlockForConfiguration];
        }
    }
}

joshbernfeld avatar Nov 28 '17 06:11 joshbernfeld