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