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