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