blob: 876dbb21717dbf4c07aefa61c9bfcfb548d1f0a2 [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() \
nisseb29b9c82016-12-12 00:22:56 -080033 WEBRTC_TRACE(kTraceError, kTraceVideoCapture, 0, \
kthelgason20a52e12016-09-30 00:43:12 -070034 "%s is not supported on the iOS platform.", __FUNCTION__); \
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000035 return -1;
36
nisseb29b9c82016-12-12 00:22:56 -080037VideoCaptureModule::DeviceInfo* VideoCaptureImpl::CreateDeviceInfo() {
38 return new DeviceInfoIos();
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000039}
40
nisseb29b9c82016-12-12 00:22:56 -080041DeviceInfoIos::DeviceInfoIos() {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070042 this->Init();
43}
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000044
45DeviceInfoIos::~DeviceInfoIos() {}
46
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070047int32_t DeviceInfoIos::Init() {
48 // Fill in all device capabilities.
49
50 int deviceCount = [DeviceInfoIosObjC captureDeviceCount];
51
52 for (int i = 0; i < deviceCount; i++) {
kthelgason20a52e12016-09-30 00:43:12 -070053 AVCaptureDevice* avDevice = [DeviceInfoIosObjC captureDeviceForIndex:i];
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070054 VideoCaptureCapabilities capabilityVector;
55
kthelgason20a52e12016-09-30 00:43:12 -070056 for (NSString* preset in camera_presets) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070057 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 =
kthelgason20a52e12016-09-30 00:43:12 -070070 std::pair<std::string, VideoCaptureCapabilities>(deviceIdCopy,
71 capabilityVector);
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070072 _capabilitiesMap.insert(mapPair);
73 }
74
75 return 0;
76}
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000077
78uint32_t DeviceInfoIos::NumberOfDevices() {
79 return [DeviceInfoIosObjC captureDeviceCount];
80}
81
82int32_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
kthelgason20a52e12016-09-30 00:43:12 -070097 strncpy(deviceUniqueIdUTF8, deviceUniqueId.UTF8String, deviceUniqueIdUTF8Length);
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000098 deviceUniqueIdUTF8[deviceUniqueIdUTF8Length - 1] = '\0';
99
100 if (productUniqueIdUTF8) {
101 productUniqueIdUTF8[0] = '\0';
102 }
103
104 return 0;
105}
106
107int32_t DeviceInfoIos::NumberOfCapabilities(const char* deviceUniqueIdUTF8) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700108 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.orgd690eab2013-08-14 22:07:04 +0000117}
118
119int32_t DeviceInfoIos::GetCapability(const char* deviceUniqueIdUTF8,
120 const uint32_t deviceCapabilityNumber,
121 VideoCaptureCapability& capability) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700122 std::string deviceUniqueId(deviceUniqueIdUTF8);
123 std::map<std::string, VideoCaptureCapabilities>::iterator it =
124 _capabilitiesMap.find(deviceUniqueId);
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000125
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700126 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.orgd690eab2013-08-14 22:07:04 +0000138}
139
140int32_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
149int32_t DeviceInfoIos::GetOrientation(const char* deviceUniqueIdUTF8,
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000150 VideoRotation& orientation) {
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000151 if (strcmp(deviceUniqueIdUTF8, "Front Camera") == 0) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000152 orientation = kVideoRotation_0;
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000153 } else {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000154 orientation = kVideoRotation_90;
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000155 }
156 return orientation;
157}
158
159int32_t DeviceInfoIos::CreateCapabilityMap(const char* deviceUniqueIdUTF8) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700160 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.orgd690eab2013-08-14 22:07:04 +0000170}