You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #import "ARDCaptureController.h"
  11. #import <WebRTC/RTCLogging.h>
  12. #import "ARDSettingsModel.h"
  13. const Float64 kFramerateLimit = 30.0;
  14. @implementation ARDCaptureController {
  15. RTCCameraVideoCapturer *_capturer;
  16. ARDSettingsModel *_settings;
  17. BOOL _usingFrontCamera;
  18. }
  19. - (instancetype)initWithCapturer:(RTCCameraVideoCapturer *)capturer
  20. settings:(ARDSettingsModel *)settings {
  21. if (self = [super init]) {
  22. _capturer = capturer;
  23. _settings = settings;
  24. _usingFrontCamera = YES;
  25. }
  26. return self;
  27. }
  28. - (void)startCapture {
  29. AVCaptureDevicePosition position =
  30. _usingFrontCamera ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;
  31. AVCaptureDevice *device = [self findDeviceForPosition:position];
  32. AVCaptureDeviceFormat *format = [self selectFormatForDevice:device];
  33. if (format == nil) {
  34. RTCLogError(@"No valid formats for device %@", device);
  35. NSAssert(NO, @"");
  36. return;
  37. }
  38. NSInteger fps = [self selectFpsForFormat:format];
  39. [_capturer startCaptureWithDevice:device format:format fps:fps];
  40. }
  41. - (void)stopCapture {
  42. [_capturer stopCapture];
  43. }
  44. - (void)switchCamera {
  45. _usingFrontCamera = !_usingFrontCamera;
  46. [self startCapture];
  47. }
  48. #pragma mark - Private
  49. - (AVCaptureDevice *)findDeviceForPosition:(AVCaptureDevicePosition)position {
  50. NSArray<AVCaptureDevice *> *captureDevices = [RTCCameraVideoCapturer captureDevices];
  51. for (AVCaptureDevice *device in captureDevices) {
  52. if (device.position == position) {
  53. return device;
  54. }
  55. }
  56. return captureDevices[0];
  57. }
  58. - (AVCaptureDeviceFormat *)selectFormatForDevice:(AVCaptureDevice *)device {
  59. NSArray<AVCaptureDeviceFormat *> *formats =
  60. [RTCCameraVideoCapturer supportedFormatsForDevice:device];
  61. int targetWidth = [_settings currentVideoResolutionWidthFromStore];
  62. int targetHeight = [_settings currentVideoResolutionHeightFromStore];
  63. AVCaptureDeviceFormat *selectedFormat = nil;
  64. int currentDiff = INT_MAX;
  65. for (AVCaptureDeviceFormat *format in formats) {
  66. CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
  67. FourCharCode pixelFormat = CMFormatDescriptionGetMediaSubType(format.formatDescription);
  68. int diff = abs(targetWidth - dimension.width) + abs(targetHeight - dimension.height);
  69. if (diff < currentDiff) {
  70. selectedFormat = format;
  71. currentDiff = diff;
  72. } else if (diff == currentDiff && pixelFormat == [_capturer preferredOutputPixelFormat]) {
  73. selectedFormat = format;
  74. }
  75. }
  76. return selectedFormat;
  77. }
  78. - (NSInteger)selectFpsForFormat:(AVCaptureDeviceFormat *)format {
  79. Float64 maxSupportedFramerate = 0;
  80. for (AVFrameRateRange *fpsRange in format.videoSupportedFrameRateRanges) {
  81. maxSupportedFramerate = fmax(maxSupportedFramerate, fpsRange.maxFrameRate);
  82. }
  83. return fmin(maxSupportedFramerate, kFramerateLimit);
  84. }
  85. @end