blob: 85733d143f7a0bccf4365e7cfb6fee168340063a [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"
Sam Zackrissondcbb66f2017-08-08 12:00:28 +020022#include "webrtc/rtc_base/logging.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() \
Sam Zackrissondcbb66f2017-08-08 12:00:28 +020033 LOG(LS_ERROR) << __FUNCTION__ << " is not supported on the iOS platform."; \
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000034 return -1;
35
nisseb29b9c82016-12-12 00:22:56 -080036VideoCaptureModule::DeviceInfo* VideoCaptureImpl::CreateDeviceInfo() {
37 return new DeviceInfoIos();
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000038}
39
nisseb29b9c82016-12-12 00:22:56 -080040DeviceInfoIos::DeviceInfoIos() {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070041 this->Init();
42}
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000043
44DeviceInfoIos::~DeviceInfoIos() {}
45
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070046int32_t DeviceInfoIos::Init() {
47 // Fill in all device capabilities.
48
49 int deviceCount = [DeviceInfoIosObjC captureDeviceCount];
50
51 for (int i = 0; i < deviceCount; i++) {
kthelgason20a52e12016-09-30 00:43:12 -070052 AVCaptureDevice* avDevice = [DeviceInfoIosObjC captureDeviceForIndex:i];
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070053 VideoCaptureCapabilities capabilityVector;
54
kthelgason20a52e12016-09-30 00:43:12 -070055 for (NSString* preset in camera_presets) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070056 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 =
kthelgason20a52e12016-09-30 00:43:12 -070069 std::pair<std::string, VideoCaptureCapabilities>(deviceIdCopy,
70 capabilityVector);
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -070071 _capabilitiesMap.insert(mapPair);
72 }
73
74 return 0;
75}
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000076
77uint32_t DeviceInfoIos::NumberOfDevices() {
78 return [DeviceInfoIosObjC captureDeviceCount];
79}
80
81int32_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
kthelgason20a52e12016-09-30 00:43:12 -070096 strncpy(deviceUniqueIdUTF8, deviceUniqueId.UTF8String, deviceUniqueIdUTF8Length);
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +000097 deviceUniqueIdUTF8[deviceUniqueIdUTF8Length - 1] = '\0';
98
99 if (productUniqueIdUTF8) {
100 productUniqueIdUTF8[0] = '\0';
101 }
102
103 return 0;
104}
105
106int32_t DeviceInfoIos::NumberOfCapabilities(const char* deviceUniqueIdUTF8) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700107 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.orgd690eab2013-08-14 22:07:04 +0000116}
117
118int32_t DeviceInfoIos::GetCapability(const char* deviceUniqueIdUTF8,
119 const uint32_t deviceCapabilityNumber,
120 VideoCaptureCapability& capability) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700121 std::string deviceUniqueId(deviceUniqueIdUTF8);
122 std::map<std::string, VideoCaptureCapabilities>::iterator it =
123 _capabilitiesMap.find(deviceUniqueId);
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000124
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700125 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.orgd690eab2013-08-14 22:07:04 +0000137}
138
139int32_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
148int32_t DeviceInfoIos::GetOrientation(const char* deviceUniqueIdUTF8,
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000149 VideoRotation& orientation) {
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000150 if (strcmp(deviceUniqueIdUTF8, "Front Camera") == 0) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000151 orientation = kVideoRotation_0;
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000152 } else {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000153 orientation = kVideoRotation_90;
sjlee@webrtc.orgd690eab2013-08-14 22:07:04 +0000154 }
155 return orientation;
156}
157
158int32_t DeviceInfoIos::CreateCapabilityMap(const char* deviceUniqueIdUTF8) {
Yuriy Shevchuk39f2b0c2015-05-14 14:16:13 -0700159 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.orgd690eab2013-08-14 22:07:04 +0000169}