sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
fischman@webrtc.org | e68102e | 2014-03-25 05:15:44 +0000 | [diff] [blame] | 11 | #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 | #error "This file requires ARC support." |
| 13 | #endif |
| 14 | |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 15 | #include <AVFoundation/AVFoundation.h> |
| 16 | |
| 17 | #include <string> |
| 18 | |
kthelgason | 20a52e1 | 2016-09-30 00:43:12 -0700 | [diff] [blame] | 19 | #include "webrtc/modules/video_capture/objc/device_info.h" |
| 20 | #include "webrtc/modules/video_capture/objc/device_info_objc.h" |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/video_capture/video_capture_impl.h" |
Sam Zackrisson | dcbb66f | 2017-08-08 12:00:28 +0200 | [diff] [blame] | 22 | #include "webrtc/rtc_base/logging.h" |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace webrtc; |
| 25 | using namespace videocapturemodule; |
| 26 | |
kthelgason | 20a52e1 | 2016-09-30 00:43:12 -0700 | [diff] [blame] | 27 | static NSArray* camera_presets = @[ |
| 28 | AVCaptureSessionPreset352x288, AVCaptureSessionPreset640x480, |
| 29 | AVCaptureSessionPreset1280x720 |
| 30 | ]; |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 31 | |
kthelgason | 20a52e1 | 2016-09-30 00:43:12 -0700 | [diff] [blame] | 32 | #define IOS_UNSUPPORTED() \ |
Sam Zackrisson | dcbb66f | 2017-08-08 12:00:28 +0200 | [diff] [blame] | 33 | LOG(LS_ERROR) << __FUNCTION__ << " is not supported on the iOS platform."; \ |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 34 | return -1; |
| 35 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 36 | VideoCaptureModule::DeviceInfo* VideoCaptureImpl::CreateDeviceInfo() { |
| 37 | return new DeviceInfoIos(); |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 38 | } |
| 39 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 40 | DeviceInfoIos::DeviceInfoIos() { |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 41 | this->Init(); |
| 42 | } |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 43 | |
| 44 | DeviceInfoIos::~DeviceInfoIos() {} |
| 45 | |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 46 | int32_t DeviceInfoIos::Init() { |
| 47 | // Fill in all device capabilities. |
| 48 | |
| 49 | int deviceCount = [DeviceInfoIosObjC captureDeviceCount]; |
| 50 | |
| 51 | for (int i = 0; i < deviceCount; i++) { |
kthelgason | 20a52e1 | 2016-09-30 00:43:12 -0700 | [diff] [blame] | 52 | AVCaptureDevice* avDevice = [DeviceInfoIosObjC captureDeviceForIndex:i]; |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 53 | VideoCaptureCapabilities capabilityVector; |
| 54 | |
kthelgason | 20a52e1 | 2016-09-30 00:43:12 -0700 | [diff] [blame] | 55 | for (NSString* preset in camera_presets) { |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 56 | BOOL support = [avDevice supportsAVCaptureSessionPreset:preset]; |
| 57 | if (support) { |
| 58 | VideoCaptureCapability capability = |
| 59 | [DeviceInfoIosObjC capabilityForPreset:preset]; |
| 60 | capabilityVector.push_back(capability); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | char deviceNameUTF8[256]; |
| 65 | char deviceId[256]; |
| 66 | this->GetDeviceName(i, deviceNameUTF8, 256, deviceId, 256); |
| 67 | std::string deviceIdCopy(deviceId); |
| 68 | std::pair<std::string, VideoCaptureCapabilities> mapPair = |
kthelgason | 20a52e1 | 2016-09-30 00:43:12 -0700 | [diff] [blame] | 69 | std::pair<std::string, VideoCaptureCapabilities>(deviceIdCopy, |
| 70 | capabilityVector); |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 71 | _capabilitiesMap.insert(mapPair); |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 76 | |
| 77 | uint32_t DeviceInfoIos::NumberOfDevices() { |
| 78 | return [DeviceInfoIosObjC captureDeviceCount]; |
| 79 | } |
| 80 | |
| 81 | int32_t DeviceInfoIos::GetDeviceName(uint32_t deviceNumber, |
| 82 | char* deviceNameUTF8, |
| 83 | uint32_t deviceNameUTF8Length, |
| 84 | char* deviceUniqueIdUTF8, |
| 85 | uint32_t deviceUniqueIdUTF8Length, |
| 86 | char* productUniqueIdUTF8, |
| 87 | uint32_t productUniqueIdUTF8Length) { |
| 88 | NSString* deviceName = [DeviceInfoIosObjC deviceNameForIndex:deviceNumber]; |
| 89 | |
| 90 | NSString* deviceUniqueId = |
| 91 | [DeviceInfoIosObjC deviceUniqueIdForIndex:deviceNumber]; |
| 92 | |
| 93 | strncpy(deviceNameUTF8, [deviceName UTF8String], deviceNameUTF8Length); |
| 94 | deviceNameUTF8[deviceNameUTF8Length - 1] = '\0'; |
| 95 | |
kthelgason | 20a52e1 | 2016-09-30 00:43:12 -0700 | [diff] [blame] | 96 | strncpy(deviceUniqueIdUTF8, deviceUniqueId.UTF8String, deviceUniqueIdUTF8Length); |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 97 | deviceUniqueIdUTF8[deviceUniqueIdUTF8Length - 1] = '\0'; |
| 98 | |
| 99 | if (productUniqueIdUTF8) { |
| 100 | productUniqueIdUTF8[0] = '\0'; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | int32_t DeviceInfoIos::NumberOfCapabilities(const char* deviceUniqueIdUTF8) { |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 107 | int32_t numberOfCapabilities = 0; |
| 108 | std::string deviceUniqueId(deviceUniqueIdUTF8); |
| 109 | std::map<std::string, VideoCaptureCapabilities>::iterator it = |
| 110 | _capabilitiesMap.find(deviceUniqueId); |
| 111 | |
| 112 | if (it != _capabilitiesMap.end()) { |
| 113 | numberOfCapabilities = it->second.size(); |
| 114 | } |
| 115 | return numberOfCapabilities; |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | int32_t DeviceInfoIos::GetCapability(const char* deviceUniqueIdUTF8, |
| 119 | const uint32_t deviceCapabilityNumber, |
| 120 | VideoCaptureCapability& capability) { |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 121 | std::string deviceUniqueId(deviceUniqueIdUTF8); |
| 122 | std::map<std::string, VideoCaptureCapabilities>::iterator it = |
| 123 | _capabilitiesMap.find(deviceUniqueId); |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 124 | |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 125 | if (it != _capabilitiesMap.end()) { |
| 126 | VideoCaptureCapabilities deviceCapabilities = it->second; |
| 127 | |
| 128 | if (deviceCapabilityNumber < deviceCapabilities.size()) { |
| 129 | VideoCaptureCapability cap; |
| 130 | cap = deviceCapabilities[deviceCapabilityNumber]; |
| 131 | capability = cap; |
| 132 | return 0; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return -1; |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int32_t DeviceInfoIos::DisplayCaptureSettingsDialogBox( |
| 140 | const char* deviceUniqueIdUTF8, |
| 141 | const char* dialogTitleUTF8, |
| 142 | void* parentWindow, |
| 143 | uint32_t positionX, |
| 144 | uint32_t positionY) { |
| 145 | IOS_UNSUPPORTED(); |
| 146 | } |
| 147 | |
| 148 | int32_t DeviceInfoIos::GetOrientation(const char* deviceUniqueIdUTF8, |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 149 | VideoRotation& orientation) { |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 150 | if (strcmp(deviceUniqueIdUTF8, "Front Camera") == 0) { |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 151 | orientation = kVideoRotation_0; |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 152 | } else { |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 153 | orientation = kVideoRotation_90; |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 154 | } |
| 155 | return orientation; |
| 156 | } |
| 157 | |
| 158 | int32_t DeviceInfoIos::CreateCapabilityMap(const char* deviceUniqueIdUTF8) { |
Yuriy Shevchuk | 39f2b0c | 2015-05-14 14:16:13 -0700 | [diff] [blame] | 159 | std::string deviceName(deviceUniqueIdUTF8); |
| 160 | std::map<std::string, std::vector<VideoCaptureCapability>>::iterator it = |
| 161 | _capabilitiesMap.find(deviceName); |
| 162 | VideoCaptureCapabilities deviceCapabilities; |
| 163 | if (it != _capabilitiesMap.end()) { |
| 164 | _captureCapabilities = it->second; |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | return -1; |
sjlee@webrtc.org | d690eab | 2013-08-14 22:07:04 +0000 | [diff] [blame] | 169 | } |