blob: 2535f1c69e32cb6b661dcaedf470792cd8246784 [file] [log] [blame]
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +00001/*
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.orge68102e2014-03-25 05:15:44 +000011#if !defined(__has_feature) || !__has_feature(objc_arc)
12#error "This file requires ARC support."
13#endif
14
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070015#include <AVFoundation/AVFoundation.h>
16
17#include <string>
18
kthelgason20a52e12016-09-30 00:43:12 -070019#include "webrtc/modules/video_capture/objc/device_info.h"
20#include "webrtc/modules/video_capture/objc/device_info_objc.h"
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000021#include "webrtc/modules/video_capture/video_capture_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/system_wrappers/include/trace.h"
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000023
24using namespace webrtc;
25using namespace videocapturemodule;
26
kthelgason20a52e12016-09-30 00:43:12 -070027static NSArray* camera_presets = @[
28 AVCaptureSessionPreset352x288, AVCaptureSessionPreset640x480,
29 AVCaptureSessionPreset1280x720
30];
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070031
kthelgason20a52e12016-09-30 00:43:12 -070032#define IOS_UNSUPPORTED() \
33 WEBRTC_TRACE(kTraceError, kTraceVideoCapture, _id, \
34 "%s is not supported on the iOS platform.", __FUNCTION__); \
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000035 return -1;
36
37VideoCaptureModule::DeviceInfo* VideoCaptureImpl::CreateDeviceInfo(
38 const int32_t device_id) {
39 return new DeviceInfoIos(device_id);
40}
41
42DeviceInfoIos::DeviceInfoIos(const int32_t device_id)
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070043 : DeviceInfoImpl(device_id) {
44 this->Init();
45}
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000046
47DeviceInfoIos::~DeviceInfoIos() {}
48
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070049int32_t DeviceInfoIos::Init() {
50 // Fill in all device capabilities.
51
52 int deviceCount = [DeviceInfoIosObjC captureDeviceCount];
53
54 for (int i = 0; i < deviceCount; i++) {
kthelgason20a52e12016-09-30 00:43:12 -070055 AVCaptureDevice* avDevice = [DeviceInfoIosObjC captureDeviceForIndex:i];
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070056 VideoCaptureCapabilities capabilityVector;
57
kthelgason20a52e12016-09-30 00:43:12 -070058 for (NSString* preset in camera_presets) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070059 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 =
kthelgason20a52e12016-09-30 00:43:12 -070072 std::pair<std::string, VideoCaptureCapabilities>(deviceIdCopy,
73 capabilityVector);
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070074 _capabilitiesMap.insert(mapPair);
75 }
76
77 return 0;
78}
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000079
80uint32_t DeviceInfoIos::NumberOfDevices() {
81 return [DeviceInfoIosObjC captureDeviceCount];
82}
83
84int32_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
kthelgason20a52e12016-09-30 00:43:12 -070099 strncpy(deviceUniqueIdUTF8, deviceUniqueId.UTF8String, deviceUniqueIdUTF8Length);
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000100 deviceUniqueIdUTF8[deviceUniqueIdUTF8Length - 1] = '\0';
101
102 if (productUniqueIdUTF8) {
103 productUniqueIdUTF8[0] = '\0';
104 }
105
106 return 0;
107}
108
109int32_t DeviceInfoIos::NumberOfCapabilities(const char* deviceUniqueIdUTF8) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700110 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.orgd690eab2013-08-14 22:07:04 +0000119}
120
121int32_t DeviceInfoIos::GetCapability(const char* deviceUniqueIdUTF8,
122 const uint32_t deviceCapabilityNumber,
123 VideoCaptureCapability& capability) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700124 std::string deviceUniqueId(deviceUniqueIdUTF8);
125 std::map<std::string, VideoCaptureCapabilities>::iterator it =
126 _capabilitiesMap.find(deviceUniqueId);
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000127
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700128 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.orgd690eab2013-08-14 22:07:04 +0000140}
141
142int32_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
151int32_t DeviceInfoIos::GetOrientation(const char* deviceUniqueIdUTF8,
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000152 VideoRotation& orientation) {
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000153 if (strcmp(deviceUniqueIdUTF8, "Front Camera") == 0) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000154 orientation = kVideoRotation_0;
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000155 } else {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000156 orientation = kVideoRotation_90;
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000157 }
158 return orientation;
159}
160
161int32_t DeviceInfoIos::CreateCapabilityMap(const char* deviceUniqueIdUTF8) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700162 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.orgd690eab2013-08-14 22:07:04 +0000172}