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