Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: webrtc/examples/objc/AppRTCMobile/ARDCaptureController.m

Issue 2778163005: Use new RTCCameraVideoCapturer in AppRTCMobile. (Closed)
Patch Set: Rename remaining media constraint references. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)switchCamera {
43 _usingFrontCamera = !_usingFrontCamera;
44 [self startCapture];
45 }
46
47 #pragma mark - Private
48
49 - (AVCaptureDevice *)findDeviceForPosition:(AVCaptureDevicePosition)position {
50 NSArray<AVCaptureDevice *> *captureDevices = [RTCCameraVideoCapturer captureDe vices];
51 for (AVCaptureDevice *device in captureDevices) {
52 if (device.position == position) {
53 return device;
54 }
55 }
56 return captureDevices[0];
57 }
58
59 - (AVCaptureDeviceFormat *)selectFormatForDevice:(AVCaptureDevice *)device {
60 NSArray<AVCaptureDeviceFormat *> *formats =
61 [RTCCameraVideoCapturer supportedFormatsForDevice:device];
62 int targetWidth = [_settings currentVideoResolutionWidthFromStore];
63 int targetHeight = [_settings currentVideoResolutionHeightFromStore];
64 AVCaptureDeviceFormat *selectedFormat = nil;
65 int currentDiff = INT_MAX;
66
67 for (AVCaptureDeviceFormat *format in formats) {
68 CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.f ormatDescription);
69 int diff = abs(targetWidth - dimension.width) + abs(targetHeight - dimension .height);
70 if (diff < currentDiff) {
71 selectedFormat = format;
72 currentDiff = diff;
73 }
74 }
75
76 NSAssert(selectedFormat != nil, @"No suitable capture format found.");
77 return selectedFormat;
78 }
79
80 - (int)selectFpsForFormat:(AVCaptureDeviceFormat *)format {
81 Float64 maxFramerate = 0;
82 for (AVFrameRateRange *fpsRange in format.videoSupportedFrameRateRanges) {
83 maxFramerate = fmax(maxFramerate, fpsRange.maxFrameRate);
84 }
85 return maxFramerate;
86 }
87
88 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698