niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mallinath@webrtc.org | 12984f0 | 2012-02-16 18:18:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 3d5cb33 | 2014-05-14 08:42:07 +0000 | [diff] [blame] | 11 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/video_capture/device_info_impl.h" |
| 15 | #include "modules/video_capture/video_capture_config.h" |
| 16 | #include "rtc_base/logging.h" |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame] | 17 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | #ifndef abs |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 19 | #define abs(a) (a >= 0 ? a : -a) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 22 | namespace webrtc { |
| 23 | namespace videocapturemodule { |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 24 | DeviceInfoImpl::DeviceInfoImpl() |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 25 | : _apiLock(*RWLockWrapper::CreateRWLock()), |
| 26 | _lastUsedDeviceName(NULL), |
| 27 | _lastUsedDeviceNameLength(0) {} |
| 28 | |
| 29 | DeviceInfoImpl::~DeviceInfoImpl(void) { |
| 30 | _apiLock.AcquireLockExclusive(); |
| 31 | free(_lastUsedDeviceName); |
| 32 | _apiLock.ReleaseLockExclusive(); |
| 33 | |
| 34 | delete &_apiLock; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 36 | int32_t DeviceInfoImpl::NumberOfCapabilities(const char* deviceUniqueIdUTF8) { |
| 37 | if (!deviceUniqueIdUTF8) |
| 38 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 40 | _apiLock.AcquireLockShared(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 42 | if (_lastUsedDeviceNameLength == strlen((char*)deviceUniqueIdUTF8)) { |
| 43 | // Is it the same device that is asked for again. |
andrew@webrtc.org | f3b65db | 2012-09-06 18:17:00 +0000 | [diff] [blame] | 44 | #if defined(WEBRTC_MAC) || defined(WEBRTC_LINUX) |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 45 | if (strncasecmp((char*)_lastUsedDeviceName, (char*)deviceUniqueIdUTF8, |
| 46 | _lastUsedDeviceNameLength) == 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | #else |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 48 | if (_strnicmp((char*)_lastUsedDeviceName, (char*)deviceUniqueIdUTF8, |
| 49 | _lastUsedDeviceNameLength) == 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | #endif |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 51 | { |
| 52 | // yes |
| 53 | _apiLock.ReleaseLockShared(); |
| 54 | return static_cast<int32_t>(_captureCapabilities.size()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 56 | } |
| 57 | // Need to get exclusive rights to create the new capability map. |
| 58 | _apiLock.ReleaseLockShared(); |
| 59 | WriteLockScoped cs2(_apiLock); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 61 | int32_t ret = CreateCapabilityMap(deviceUniqueIdUTF8); |
| 62 | return ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 65 | int32_t DeviceInfoImpl::GetCapability(const char* deviceUniqueIdUTF8, |
| 66 | const uint32_t deviceCapabilityNumber, |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 67 | VideoCaptureCapability& capability) { |
| 68 | assert(deviceUniqueIdUTF8 != NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 70 | ReadLockScoped cs(_apiLock); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 72 | if ((_lastUsedDeviceNameLength != strlen((char*)deviceUniqueIdUTF8)) |
andrew@webrtc.org | f3b65db | 2012-09-06 18:17:00 +0000 | [diff] [blame] | 73 | #if defined(WEBRTC_MAC) || defined(WEBRTC_LINUX) |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 74 | || (strncasecmp((char*)_lastUsedDeviceName, (char*)deviceUniqueIdUTF8, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | _lastUsedDeviceNameLength) != 0)) |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 76 | #else |
| 77 | || (_strnicmp((char*)_lastUsedDeviceName, (char*)deviceUniqueIdUTF8, |
| 78 | _lastUsedDeviceNameLength) != 0)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 81 | { |
| 82 | _apiLock.ReleaseLockShared(); |
| 83 | _apiLock.AcquireLockExclusive(); |
| 84 | if (-1 == CreateCapabilityMap(deviceUniqueIdUTF8)) { |
| 85 | _apiLock.ReleaseLockExclusive(); |
| 86 | _apiLock.AcquireLockShared(); |
| 87 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 89 | _apiLock.ReleaseLockExclusive(); |
| 90 | _apiLock.AcquireLockShared(); |
| 91 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 93 | // Make sure the number is valid |
| 94 | if (deviceCapabilityNumber >= (unsigned int)_captureCapabilities.size()) { |
| 95 | LOG(LS_ERROR) << "Invalid deviceCapabilityNumber " << deviceCapabilityNumber |
| 96 | << ">= number of capabilities (" |
| 97 | << _captureCapabilities.size() << ")."; |
| 98 | return -1; |
| 99 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 101 | capability = _captureCapabilities[deviceCapabilityNumber]; |
| 102 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 105 | int32_t DeviceInfoImpl::GetBestMatchedCapability( |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 106 | const char* deviceUniqueIdUTF8, |
| 107 | const VideoCaptureCapability& requested, |
| 108 | VideoCaptureCapability& resulting) { |
| 109 | if (!deviceUniqueIdUTF8) |
| 110 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 112 | ReadLockScoped cs(_apiLock); |
| 113 | if ((_lastUsedDeviceNameLength != strlen((char*)deviceUniqueIdUTF8)) |
andrew@webrtc.org | f3b65db | 2012-09-06 18:17:00 +0000 | [diff] [blame] | 114 | #if defined(WEBRTC_MAC) || defined(WEBRTC_LINUX) |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 115 | || (strncasecmp((char*)_lastUsedDeviceName, (char*)deviceUniqueIdUTF8, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | _lastUsedDeviceNameLength) != 0)) |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 117 | #else |
| 118 | || (_strnicmp((char*)_lastUsedDeviceName, (char*)deviceUniqueIdUTF8, |
| 119 | _lastUsedDeviceNameLength) != 0)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | #endif |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 121 | { |
| 122 | _apiLock.ReleaseLockShared(); |
| 123 | _apiLock.AcquireLockExclusive(); |
| 124 | if (-1 == CreateCapabilityMap(deviceUniqueIdUTF8)) { |
| 125 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 127 | _apiLock.ReleaseLockExclusive(); |
| 128 | _apiLock.AcquireLockShared(); |
| 129 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 131 | int32_t bestformatIndex = -1; |
| 132 | int32_t bestWidth = 0; |
| 133 | int32_t bestHeight = 0; |
| 134 | int32_t bestFrameRate = 0; |
| 135 | VideoType bestVideoType = VideoType::kUnknown; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 137 | const int32_t numberOfCapabilies = |
| 138 | static_cast<int32_t>(_captureCapabilities.size()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 140 | for (int32_t tmp = 0; tmp < numberOfCapabilies; |
| 141 | ++tmp) // Loop through all capabilities |
| 142 | { |
| 143 | VideoCaptureCapability& capability = _captureCapabilities[tmp]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 145 | const int32_t diffWidth = capability.width - requested.width; |
| 146 | const int32_t diffHeight = capability.height - requested.height; |
| 147 | const int32_t diffFrameRate = capability.maxFPS - requested.maxFPS; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 149 | const int32_t currentbestDiffWith = bestWidth - requested.width; |
| 150 | const int32_t currentbestDiffHeight = bestHeight - requested.height; |
| 151 | const int32_t currentbestDiffFrameRate = bestFrameRate - requested.maxFPS; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 153 | if ((diffHeight >= 0 && |
| 154 | diffHeight <= abs(currentbestDiffHeight)) // Height better or equalt |
| 155 | // that previouse. |
| 156 | || (currentbestDiffHeight < 0 && diffHeight >= currentbestDiffHeight)) { |
| 157 | if (diffHeight == |
| 158 | currentbestDiffHeight) // Found best height. Care about the width) |
| 159 | { |
| 160 | if ((diffWidth >= 0 && |
| 161 | diffWidth <= abs(currentbestDiffWith)) // Width better or equal |
| 162 | || (currentbestDiffWith < 0 && diffWidth >= currentbestDiffWith)) { |
| 163 | if (diffWidth == currentbestDiffWith && |
| 164 | diffHeight == currentbestDiffHeight) // Same size as previously |
| 165 | { |
| 166 | // Also check the best frame rate if the diff is the same as |
| 167 | // previouse |
| 168 | if (((diffFrameRate >= 0 && |
| 169 | diffFrameRate <= |
| 170 | currentbestDiffFrameRate) // Frame rate to high but |
| 171 | // better match than previouse |
| 172 | // and we have not selected IUV |
| 173 | || (currentbestDiffFrameRate < 0 && |
| 174 | diffFrameRate >= |
| 175 | currentbestDiffFrameRate)) // Current frame rate is |
| 176 | // lower than requested. |
| 177 | // This is better. |
| 178 | ) { |
| 179 | if ((currentbestDiffFrameRate == |
| 180 | diffFrameRate) // Same frame rate as previous or frame rate |
| 181 | // allready good enough |
| 182 | || (currentbestDiffFrameRate >= 0)) { |
| 183 | if (bestVideoType != requested.videoType && |
| 184 | requested.videoType != VideoType::kUnknown && |
| 185 | (capability.videoType == requested.videoType || |
| 186 | capability.videoType == VideoType::kI420 || |
| 187 | capability.videoType == VideoType::kYUY2 || |
| 188 | capability.videoType == VideoType::kYV12)) { |
| 189 | bestVideoType = capability.videoType; |
| 190 | bestformatIndex = tmp; |
| 191 | } |
| 192 | // If width height and frame rate is full filled we can use the |
| 193 | // camera for encoding if it is supported. |
| 194 | if (capability.height == requested.height && |
| 195 | capability.width == requested.width && |
| 196 | capability.maxFPS >= requested.maxFPS) { |
| 197 | bestformatIndex = tmp; |
| 198 | } |
| 199 | } else // Better frame rate |
| 200 | { |
nisse | 1e32122 | 2017-02-20 23:27:37 -0800 | [diff] [blame] | 201 | bestWidth = capability.width; |
| 202 | bestHeight = capability.height; |
| 203 | bestFrameRate = capability.maxFPS; |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 204 | bestVideoType = capability.videoType; |
nisse | 1e32122 | 2017-02-20 23:27:37 -0800 | [diff] [blame] | 205 | bestformatIndex = tmp; |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 206 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 208 | } else // Better width than previously |
| 209 | { |
| 210 | bestWidth = capability.width; |
| 211 | bestHeight = capability.height; |
| 212 | bestFrameRate = capability.maxFPS; |
| 213 | bestVideoType = capability.videoType; |
| 214 | bestformatIndex = tmp; |
| 215 | } |
| 216 | } // else width no good |
| 217 | } else // Better height |
| 218 | { |
| 219 | bestWidth = capability.width; |
| 220 | bestHeight = capability.height; |
| 221 | bestFrameRate = capability.maxFPS; |
| 222 | bestVideoType = capability.videoType; |
| 223 | bestformatIndex = tmp; |
| 224 | } |
| 225 | } // else height not good |
| 226 | } // end for |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 228 | LOG(LS_VERBOSE) << "Best camera format: " << bestWidth << "x" << bestHeight |
| 229 | << "@" << bestFrameRate |
| 230 | << "fps, color format: " << static_cast<int>(bestVideoType); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 231 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 232 | // Copy the capability |
| 233 | if (bestformatIndex < 0) |
| 234 | return -1; |
| 235 | resulting = _captureCapabilities[bestformatIndex]; |
| 236 | return bestformatIndex; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 239 | // Default implementation. This should be overridden by Mobile implementations. |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 240 | int32_t DeviceInfoImpl::GetOrientation(const char* deviceUniqueIdUTF8, |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 241 | VideoRotation& orientation) { |
| 242 | orientation = kVideoRotation_0; |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame^] | 243 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 244 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 245 | } // namespace videocapturemodule |
| 246 | } // namespace webrtc |