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