sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #import "ARDCaptureController.h" |
| 12 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 13 | #import <WebRTC/RTCLogging.h> |
| 14 | |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 15 | #import "ARDSettingsModel.h" |
| 16 | |
Kári Tristan Helgason | 293865c | 2018-05-30 09:59:38 +0200 | [diff] [blame] | 17 | const Float64 kFramerateLimit = 30.0; |
| 18 | |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 19 | @implementation ARDCaptureController { |
| 20 | RTCCameraVideoCapturer *_capturer; |
| 21 | ARDSettingsModel *_settings; |
| 22 | BOOL _usingFrontCamera; |
| 23 | } |
| 24 | |
| 25 | - (instancetype)initWithCapturer:(RTCCameraVideoCapturer *)capturer |
| 26 | settings:(ARDSettingsModel *)settings { |
Peter Hanspers | d9b64cd | 2018-01-12 16:16:18 +0100 | [diff] [blame] | 27 | if (self = [super init]) { |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 28 | _capturer = capturer; |
| 29 | _settings = settings; |
| 30 | _usingFrontCamera = YES; |
| 31 | } |
| 32 | |
| 33 | return self; |
| 34 | } |
| 35 | |
| 36 | - (void)startCapture { |
| 37 | AVCaptureDevicePosition position = |
| 38 | _usingFrontCamera ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack; |
| 39 | AVCaptureDevice *device = [self findDeviceForPosition:position]; |
| 40 | AVCaptureDeviceFormat *format = [self selectFormatForDevice:device]; |
Peter Hanspers | d92e0b5 | 2017-10-23 14:30:25 +0200 | [diff] [blame] | 41 | |
| 42 | if (format == nil) { |
| 43 | RTCLogError(@"No valid formats for device %@", device); |
| 44 | NSAssert(NO, @""); |
| 45 | |
| 46 | return; |
| 47 | } |
| 48 | |
sakal | ea12f4c | 2017-05-05 00:45:30 -0700 | [diff] [blame] | 49 | NSInteger fps = [self selectFpsForFormat:format]; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 50 | |
| 51 | [_capturer startCaptureWithDevice:device format:format fps:fps]; |
| 52 | } |
| 53 | |
| 54 | - (void)stopCapture { |
| 55 | [_capturer stopCapture]; |
| 56 | } |
| 57 | |
| 58 | - (void)switchCamera { |
| 59 | _usingFrontCamera = !_usingFrontCamera; |
| 60 | [self startCapture]; |
| 61 | } |
| 62 | |
| 63 | #pragma mark - Private |
| 64 | |
| 65 | - (AVCaptureDevice *)findDeviceForPosition:(AVCaptureDevicePosition)position { |
| 66 | NSArray<AVCaptureDevice *> *captureDevices = [RTCCameraVideoCapturer captureDevices]; |
| 67 | for (AVCaptureDevice *device in captureDevices) { |
| 68 | if (device.position == position) { |
| 69 | return device; |
| 70 | } |
| 71 | } |
| 72 | return captureDevices[0]; |
| 73 | } |
| 74 | |
| 75 | - (AVCaptureDeviceFormat *)selectFormatForDevice:(AVCaptureDevice *)device { |
| 76 | NSArray<AVCaptureDeviceFormat *> *formats = |
| 77 | [RTCCameraVideoCapturer supportedFormatsForDevice:device]; |
| 78 | int targetWidth = [_settings currentVideoResolutionWidthFromStore]; |
| 79 | int targetHeight = [_settings currentVideoResolutionHeightFromStore]; |
| 80 | AVCaptureDeviceFormat *selectedFormat = nil; |
| 81 | int currentDiff = INT_MAX; |
| 82 | |
| 83 | for (AVCaptureDeviceFormat *format in formats) { |
| 84 | CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription); |
Anders Carlsson | 7dad255 | 2017-11-15 15:59:50 +0100 | [diff] [blame] | 85 | FourCharCode pixelFormat = CMFormatDescriptionGetMediaSubType(format.formatDescription); |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 86 | int diff = abs(targetWidth - dimension.width) + abs(targetHeight - dimension.height); |
| 87 | if (diff < currentDiff) { |
| 88 | selectedFormat = format; |
| 89 | currentDiff = diff; |
Anders Carlsson | 7dad255 | 2017-11-15 15:59:50 +0100 | [diff] [blame] | 90 | } else if (diff == currentDiff && pixelFormat == [_capturer preferredOutputPixelFormat]) { |
| 91 | selectedFormat = format; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 95 | return selectedFormat; |
| 96 | } |
| 97 | |
sakal | ea12f4c | 2017-05-05 00:45:30 -0700 | [diff] [blame] | 98 | - (NSInteger)selectFpsForFormat:(AVCaptureDeviceFormat *)format { |
Kári Tristan Helgason | 293865c | 2018-05-30 09:59:38 +0200 | [diff] [blame] | 99 | Float64 maxSupportedFramerate = 0; |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 100 | for (AVFrameRateRange *fpsRange in format.videoSupportedFrameRateRanges) { |
Kári Tristan Helgason | 293865c | 2018-05-30 09:59:38 +0200 | [diff] [blame] | 101 | maxSupportedFramerate = fmax(maxSupportedFramerate, fpsRange.maxFrameRate); |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 102 | } |
Kári Tristan Helgason | 293865c | 2018-05-30 09:59:38 +0200 | [diff] [blame] | 103 | return fmin(maxSupportedFramerate, kFramerateLimit); |
sakal | c522e75 | 2017-04-05 12:17:48 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @end |