OLD | NEW |
(Empty) | |
| 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 |
| 13 #import "ARDSettingsModel.h" |
| 14 |
| 15 @implementation ARDCaptureController { |
| 16 RTCCameraVideoCapturer *_capturer; |
| 17 ARDSettingsModel *_settings; |
| 18 BOOL _usingFrontCamera; |
| 19 } |
| 20 |
| 21 - (instancetype)initWithCapturer:(RTCCameraVideoCapturer *)capturer |
| 22 settings:(ARDSettingsModel *)settings { |
| 23 if ([super init]) { |
| 24 _capturer = capturer; |
| 25 _settings = settings; |
| 26 _usingFrontCamera = YES; |
| 27 } |
| 28 |
| 29 return self; |
| 30 } |
| 31 |
| 32 - (void)startCapture { |
| 33 AVCaptureDevicePosition position = |
| 34 _usingFrontCamera ? AVCaptureDevicePositionFront : AVCaptureDevicePosition
Back; |
| 35 AVCaptureDevice *device = [self findDeviceForPosition:position]; |
| 36 AVCaptureDeviceFormat *format = [self selectFormatForDevice:device]; |
| 37 int fps = [self selectFpsForFormat:format]; |
| 38 |
| 39 [_capturer startCaptureWithDevice:device format:format fps:fps]; |
| 40 } |
| 41 |
| 42 - (void)stopCapture { |
| 43 [_capturer stopCapture]; |
| 44 } |
| 45 |
| 46 - (void)switchCamera { |
| 47 _usingFrontCamera = !_usingFrontCamera; |
| 48 [self startCapture]; |
| 49 } |
| 50 |
| 51 #pragma mark - Private |
| 52 |
| 53 - (AVCaptureDevice *)findDeviceForPosition:(AVCaptureDevicePosition)position { |
| 54 NSArray<AVCaptureDevice *> *captureDevices = [RTCCameraVideoCapturer captureDe
vices]; |
| 55 for (AVCaptureDevice *device in captureDevices) { |
| 56 if (device.position == position) { |
| 57 return device; |
| 58 } |
| 59 } |
| 60 return captureDevices[0]; |
| 61 } |
| 62 |
| 63 - (AVCaptureDeviceFormat *)selectFormatForDevice:(AVCaptureDevice *)device { |
| 64 NSArray<AVCaptureDeviceFormat *> *formats = |
| 65 [RTCCameraVideoCapturer supportedFormatsForDevice:device]; |
| 66 int targetWidth = [_settings currentVideoResolutionWidthFromStore]; |
| 67 int targetHeight = [_settings currentVideoResolutionHeightFromStore]; |
| 68 AVCaptureDeviceFormat *selectedFormat = nil; |
| 69 int currentDiff = INT_MAX; |
| 70 |
| 71 for (AVCaptureDeviceFormat *format in formats) { |
| 72 CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.f
ormatDescription); |
| 73 int diff = abs(targetWidth - dimension.width) + abs(targetHeight - dimension
.height); |
| 74 if (diff < currentDiff) { |
| 75 selectedFormat = format; |
| 76 currentDiff = diff; |
| 77 } |
| 78 } |
| 79 |
| 80 NSAssert(selectedFormat != nil, @"No suitable capture format found."); |
| 81 return selectedFormat; |
| 82 } |
| 83 |
| 84 - (int)selectFpsForFormat:(AVCaptureDeviceFormat *)format { |
| 85 Float64 maxFramerate = 0; |
| 86 for (AVFrameRateRange *fpsRange in format.videoSupportedFrameRateRanges) { |
| 87 maxFramerate = fmax(maxFramerate, fpsRange.maxFrameRate); |
| 88 } |
| 89 return maxFramerate; |
| 90 } |
| 91 |
| 92 @end |
OLD | NEW |