blob: c360cf359343723ade7acc700f08106a92985c48 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mallinath@webrtc.org12984f02012-02-16 18:18:21 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
niklase@google.com470e71d2011-07-07 08:21:25 +000011#include <stdlib.h>
12
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000013#include "webrtc/modules/video_capture/device_info_impl.h"
14#include "webrtc/modules/video_capture/video_capture_config.h"
15#include "webrtc/system_wrappers/interface/trace.h"
16
niklase@google.com470e71d2011-07-07 08:21:25 +000017#ifndef abs
18#define abs(a) (a>=0?a:-a)
19#endif
20
21namespace webrtc
22{
23namespace videocapturemodule
24{
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000025DeviceInfoImpl::DeviceInfoImpl(const int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +000026 : _id(id), _apiLock(*RWLockWrapper::CreateRWLock()), _lastUsedDeviceName(NULL),
27 _lastUsedDeviceNameLength(0)
28{
29}
30
31DeviceInfoImpl::~DeviceInfoImpl(void)
32{
33 _apiLock.AcquireLockExclusive();
34 // Reset old capability list
35 MapItem* item = NULL;
36 while ((item = _captureCapabilities.Last()))
37 {
38 delete (VideoCaptureCapability*) item->GetItem();
39 _captureCapabilities.Erase(item);
40 }
41 free(_lastUsedDeviceName);
42 _apiLock.ReleaseLockExclusive();
43
44 delete &_apiLock;
45}
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000046int32_t DeviceInfoImpl::NumberOfCapabilities(
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000047 const char* deviceUniqueIdUTF8)
niklase@google.com470e71d2011-07-07 08:21:25 +000048{
niklase@google.com470e71d2011-07-07 08:21:25 +000049
50 if (!deviceUniqueIdUTF8)
51 return -1;
52
53 _apiLock.AcquireLockShared();
54
55 if (_lastUsedDeviceNameLength == strlen((char*) deviceUniqueIdUTF8))
56 {
57 // Is it the same device that is asked for again.
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +000058#if defined(WEBRTC_MAC) || defined(WEBRTC_LINUX)
niklase@google.com470e71d2011-07-07 08:21:25 +000059 if(strncasecmp((char*)_lastUsedDeviceName,
60 (char*) deviceUniqueIdUTF8,
61 _lastUsedDeviceNameLength)==0)
62#else
63 if (_strnicmp((char*) _lastUsedDeviceName,
64 (char*) deviceUniqueIdUTF8,
65 _lastUsedDeviceNameLength) == 0)
66#endif
67 {
68 //yes
69 _apiLock.ReleaseLockShared();
70 return _captureCapabilities.Size();
71 }
72 }
73 // Need to get exclusive rights to create the new capability map.
74 _apiLock.ReleaseLockShared();
75 WriteLockScoped cs2(_apiLock);
76
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000077 int32_t ret = CreateCapabilityMap(deviceUniqueIdUTF8);
niklase@google.com470e71d2011-07-07 08:21:25 +000078 return ret;
79}
80
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000081int32_t DeviceInfoImpl::GetCapability(const char* deviceUniqueIdUTF8,
82 const uint32_t deviceCapabilityNumber,
83 VideoCaptureCapability& capability)
niklase@google.com470e71d2011-07-07 08:21:25 +000084{
niklase@google.com470e71d2011-07-07 08:21:25 +000085
86 if (!deviceUniqueIdUTF8)
87 {
88 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
89 "deviceUniqueIdUTF8 parameter not set in call to GetCapability");
90 return -1;
91 }
92 ReadLockScoped cs(_apiLock);
93
94 if ((_lastUsedDeviceNameLength != strlen((char*) deviceUniqueIdUTF8))
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +000095#if defined(WEBRTC_MAC) || defined(WEBRTC_LINUX)
niklase@google.com470e71d2011-07-07 08:21:25 +000096 || (strncasecmp((char*)_lastUsedDeviceName,
97 (char*) deviceUniqueIdUTF8,
98 _lastUsedDeviceNameLength)!=0))
99#else
100 || (_strnicmp((char*) _lastUsedDeviceName,
101 (char*) deviceUniqueIdUTF8,
102 _lastUsedDeviceNameLength) != 0))
103#endif
104
105 {
106 _apiLock.ReleaseLockShared();
107 _apiLock.AcquireLockExclusive();
108 if (-1 == CreateCapabilityMap(deviceUniqueIdUTF8))
109 {
110 _apiLock.ReleaseLockExclusive();
111 _apiLock.AcquireLockShared();
112 return -1;
113 }
114 _apiLock.ReleaseLockExclusive();
115 _apiLock.AcquireLockShared();
116 }
117
118 // Make sure the number is valid
119 if (deviceCapabilityNumber >= (unsigned int) _captureCapabilities.Size())
120 {
121 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
122 "deviceCapabilityNumber %d is invalid in call to GetCapability",
123 deviceCapabilityNumber);
124 return -1;
125 }
126
127 MapItem* item = _captureCapabilities.Find(deviceCapabilityNumber);
128 if (!item)
129 {
130 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
131 "Failed to find capability number %d of %d possible",
132 deviceCapabilityNumber, _captureCapabilities.Size());
133 return -1;
134 }
135
136 VideoCaptureCapability* capPointer = static_cast<VideoCaptureCapability*>
137 (item->GetItem());
138 if (!capPointer)
139 {
140 return -1;
141 }
142
143 capability = *capPointer;
144 return 0;
145}
146
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000147int32_t DeviceInfoImpl::GetBestMatchedCapability(
leozwang@webrtc.org1745e932012-03-01 16:30:40 +0000148 const char*deviceUniqueIdUTF8,
mallinath@webrtc.org12984f02012-02-16 18:18:21 +0000149 const VideoCaptureCapability& requested,
niklase@google.com470e71d2011-07-07 08:21:25 +0000150 VideoCaptureCapability& resulting)
151{
152
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
154 if (!deviceUniqueIdUTF8)
155 return -1;
156
157 ReadLockScoped cs(_apiLock);
158 if ((_lastUsedDeviceNameLength != strlen((char*) deviceUniqueIdUTF8))
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +0000159#if defined(WEBRTC_MAC) || defined(WEBRTC_LINUX)
niklase@google.com470e71d2011-07-07 08:21:25 +0000160 || (strncasecmp((char*)_lastUsedDeviceName,
161 (char*) deviceUniqueIdUTF8,
162 _lastUsedDeviceNameLength)!=0))
163#else
164 || (_strnicmp((char*) _lastUsedDeviceName,
165 (char*) deviceUniqueIdUTF8,
166 _lastUsedDeviceNameLength) != 0))
167#endif
168 {
169 _apiLock.ReleaseLockShared();
170 _apiLock.AcquireLockExclusive();
171 if (-1 == CreateCapabilityMap(deviceUniqueIdUTF8))
172 {
173 return -1;
174 }
175 _apiLock.ReleaseLockExclusive();
176 _apiLock.AcquireLockShared();
177 }
178
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000179 int32_t bestformatIndex = -1;
180 int32_t bestWidth = 0;
181 int32_t bestHeight = 0;
182 int32_t bestFrameRate = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183 RawVideoType bestRawType = kVideoUnknown;
184 webrtc::VideoCodecType bestCodecType = webrtc::kVideoCodecUnknown;
185
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000186 const int32_t numberOfCapabilies = _captureCapabilities.Size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000188 for (int32_t tmp = 0; tmp < numberOfCapabilies; ++tmp) // Loop through all capabilities
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 {
190 MapItem* item = _captureCapabilities.Find(tmp);
191 if (!item)
192 return -1;
193
194 VideoCaptureCapability& capability = *static_cast<VideoCaptureCapability*>
195 (item->GetItem());
196
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000197 const int32_t diffWidth = capability.width - requested.width;
198 const int32_t diffHeight = capability.height - requested.height;
199 const int32_t diffFrameRate = capability.maxFPS - requested.maxFPS;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000201 const int32_t currentbestDiffWith = bestWidth - requested.width;
202 const int32_t currentbestDiffHeight = bestHeight - requested.height;
203 const int32_t currentbestDiffFrameRate = bestFrameRate - requested.maxFPS;
niklase@google.com470e71d2011-07-07 08:21:25 +0000204
205 if ((diffHeight >= 0 && diffHeight <= abs(currentbestDiffHeight)) // Height better or equalt that previouse.
206 || (currentbestDiffHeight < 0 && diffHeight >= currentbestDiffHeight))
207 {
208
209 if (diffHeight == currentbestDiffHeight) // Found best height. Care about the width)
210 {
211 if ((diffWidth >= 0 && diffWidth <= abs(currentbestDiffWith)) // Width better or equal
212 || (currentbestDiffWith < 0 && diffWidth >= currentbestDiffWith))
213 {
214 if (diffWidth == currentbestDiffWith && diffHeight
215 == currentbestDiffHeight) // Same size as previously
216 {
217 //Also check the best frame rate if the diff is the same as previouse
218 if (((diffFrameRate >= 0 &&
219 diffFrameRate <= currentbestDiffFrameRate) // Frame rate to high but better match than previouse and we have not selected IUV
220 ||
221 (currentbestDiffFrameRate < 0 &&
222 diffFrameRate >= currentbestDiffFrameRate)) // Current frame rate is lower than requested. This is better.
223 )
224 {
225 if ((currentbestDiffFrameRate == diffFrameRate) // Same frame rate as previous or frame rate allready good enough
226 || (currentbestDiffFrameRate >= 0))
227 {
228 if (bestRawType != requested.rawType
229 && requested.rawType != kVideoUnknown
230 && (capability.rawType == requested.rawType
231 || capability.rawType == kVideoI420
232 || capability.rawType == kVideoYUY2
233 || capability.rawType == kVideoYV12))
234 {
235 bestCodecType = capability.codecType;
236 bestRawType = capability.rawType;
237 bestformatIndex = tmp;
238 }
239 // If width height and frame rate is full filled we can use the camera for encoding if it is supported.
240 if (capability.height == requested.height
241 && capability.width == requested.width
242 && capability.maxFPS >= requested.maxFPS)
243 {
244 if (capability.codecType == requested.codecType
245 && bestCodecType != requested.codecType)
246 {
247 bestCodecType = capability.codecType;
248 bestformatIndex = tmp;
249 }
250 }
251 }
252 else // Better frame rate
253 {
254 if (requested.codecType == capability.codecType)
255 {
256
257 bestWidth = capability.width;
258 bestHeight = capability.height;
259 bestFrameRate = capability.maxFPS;
260 bestCodecType = capability.codecType;
261 bestRawType = capability.rawType;
262 bestformatIndex = tmp;
263 }
264 }
265 }
266 }
267 else // Better width than previously
268 {
269 if (requested.codecType == capability.codecType)
270 {
271 bestWidth = capability.width;
272 bestHeight = capability.height;
273 bestFrameRate = capability.maxFPS;
274 bestCodecType = capability.codecType;
275 bestRawType = capability.rawType;
276 bestformatIndex = tmp;
277 }
278 }
279 }// else width no good
280 }
281 else // Better height
282 {
283 if (requested.codecType == capability.codecType)
284 {
285 bestWidth = capability.width;
286 bestHeight = capability.height;
287 bestFrameRate = capability.maxFPS;
288 bestCodecType = capability.codecType;
289 bestRawType = capability.rawType;
290 bestformatIndex = tmp;
291 }
292 }
293 }// else height not good
294 }//end for
295
296 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id,
297 "Best camera format: Width %d, Height %d, Frame rate %d, Color format %d",
298 bestWidth, bestHeight, bestFrameRate, bestRawType);
299
300 // Copy the capability
301 MapItem* item = _captureCapabilities.Find(bestformatIndex);
302 if (!item)
303 return -1;
304 VideoCaptureCapability* capPointer =
305 static_cast<VideoCaptureCapability*> (item->GetItem());
306 if (!capPointer)
307 return -1;
308
309 resulting = *capPointer;
310
311 return bestformatIndex;
312}
313
314/* Returns the expected Capture delay*/
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000315int32_t DeviceInfoImpl::GetExpectedCaptureDelay(
niklase@google.com470e71d2011-07-07 08:21:25 +0000316 const DelayValues delayValues[],
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000317 const uint32_t sizeOfDelayValues,
leozwang@webrtc.org1745e932012-03-01 16:30:40 +0000318 const char* productId,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000319 const uint32_t width,
320 const uint32_t height)
niklase@google.com470e71d2011-07-07 08:21:25 +0000321{
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000322 int32_t bestDelay = kDefaultCaptureDelay;
niklase@google.com470e71d2011-07-07 08:21:25 +0000323
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000324 for (uint32_t device = 0; device < sizeOfDelayValues; ++device)
niklase@google.com470e71d2011-07-07 08:21:25 +0000325 {
326 if (delayValues[device].productId && strncmp((char*) productId,
327 (char*) delayValues[device].productId,
328 kVideoCaptureProductIdLength) == 0)
329 {
330 // We have found the camera
331
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000332 int32_t bestWidth = 0;
333 int32_t bestHeight = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000334
335 //Loop through all tested sizes and find one that seems fitting
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000336 for (uint32_t delayIndex = 0; delayIndex < NoOfDelayValues; ++delayIndex)
niklase@google.com470e71d2011-07-07 08:21:25 +0000337 {
338 const DelayValue& currentValue = delayValues[device].delayValues[delayIndex];
339
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000340 const int32_t diffWidth = currentValue.width - width;
341 const int32_t diffHeight = currentValue.height - height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000342
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000343 const int32_t currentbestDiffWith = bestWidth - width;
344 const int32_t currentbestDiffHeight = bestHeight - height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000345
346 if ((diffHeight >= 0 && diffHeight <= abs(currentbestDiffHeight)) // Height better or equal than previous.
347 || (currentbestDiffHeight < 0 && diffHeight >= currentbestDiffHeight))
348 {
349
350 if (diffHeight == currentbestDiffHeight) // Found best height. Care about the width)
351 {
352 if ((diffWidth >= 0 && diffWidth <= abs(currentbestDiffWith)) // Width better or equal
353 || (currentbestDiffWith < 0 && diffWidth >= currentbestDiffWith))
354 {
355 if (diffWidth == currentbestDiffWith && diffHeight
356 == currentbestDiffHeight) // Same size as previous
357 {
358 }
359 else // Better width than previously
360 {
361 bestWidth = currentValue.width;
362 bestHeight = currentValue.height;
363 bestDelay = currentValue.delay;
364 }
365 }// else width no good
366 }
367 else // Better height
368 {
369 bestWidth = currentValue.width;
370 bestHeight = currentValue.height;
371 bestDelay = currentValue.delay;
372 }
373 }// else height not good
374 }//end for
375 break;
376 }
377 }
378 if (bestDelay > kMaxCaptureDelay)
379 {
380 WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, _id,
381 "Expected capture delay too high. %dms, will use %d", bestDelay,
382 kMaxCaptureDelay);
383 bestDelay = kMaxCaptureDelay;
384
385 }
386
387 return bestDelay;
388
389}
390
391//Default implementation. This should be overridden by Mobile implementations.
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000392int32_t DeviceInfoImpl::GetOrientation(const char* deviceUniqueIdUTF8,
393 VideoCaptureRotation& orientation)
niklase@google.com470e71d2011-07-07 08:21:25 +0000394{
395 orientation = kCameraRotate0;
396 return -1;
397}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000398} // namespace videocapturemodule
399} // namespace webrtc