OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/media/media_stream_constraints_util_video_source.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cmath> | |
9 #include <limits> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "content/renderer/media/media_stream_video_source.h" | |
14 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | |
15 #include "third_party/WebKit/public/platform/WebString.h" | |
16 | |
17 namespace content { | |
18 | |
19 namespace { | |
20 | |
21 // Number of default settings to be used as final tie-breaking criteria for | |
22 // settings that are equally good at satisfying constraints: | |
23 // device ID, power-line frequency, resolution and frame rate. | |
24 const int kNumDefaultDistanceEntries = 4; | |
25 | |
26 // The default resolution to be preferred as tie-breaking criterion. | |
27 const int kDefaultResolutionArea = MediaStreamVideoSource::kDefaultWidth * | |
28 MediaStreamVideoSource::kDefaultHeight; | |
29 | |
30 // The minimum aspect ratio to be supported by sources. | |
31 const double kMinSourceAspectRatio = 0.05; | |
32 | |
33 blink::WebString ToWebString(::mojom::FacingMode facing_mode) { | |
34 switch (facing_mode) { | |
35 case ::mojom::FacingMode::USER: | |
36 return blink::WebString::fromASCII("user"); | |
37 case ::mojom::FacingMode::ENVIRONMENT: | |
38 return blink::WebString::fromASCII("environment"); | |
39 case ::mojom::FacingMode::LEFT: | |
40 return blink::WebString::fromASCII("left"); | |
41 case ::mojom::FacingMode::RIGHT: | |
42 return blink::WebString::fromASCII("right"); | |
43 default: | |
44 return blink::WebString::fromASCII(""); | |
45 } | |
46 } | |
47 | |
48 template <typename ConstraintType> | |
49 bool ConstraintHasMax(const ConstraintType& constraint) { | |
50 return constraint.hasMax() || constraint.hasExact(); | |
51 } | |
52 | |
53 template <typename ConstraintType> | |
54 bool ConstraintHasMin(const ConstraintType& constraint) { | |
55 return constraint.hasMin() || constraint.hasExact(); | |
56 } | |
57 | |
58 template <typename ConstraintType> | |
59 auto ConstraintMax(const ConstraintType& constraint) | |
60 -> decltype(constraint.max()) { | |
61 DCHECK(ConstraintHasMax(constraint)); | |
62 return constraint.hasExact() ? constraint.exact() : constraint.max(); | |
63 } | |
64 | |
65 template <typename ConstraintType> | |
66 auto ConstraintMin(const ConstraintType& constraint) | |
67 -> decltype(constraint.min()) { | |
68 DCHECK(ConstraintHasMin(constraint)); | |
69 return constraint.hasExact() ? constraint.exact() : constraint.min(); | |
70 } | |
71 | |
72 // Generic distance function between two numeric values. Based on the fitness | |
73 // distance function described in | |
74 // https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
75 double Distance(double value1, double value2) { | |
hbos_chromium
2017/02/08 09:45:00
Here and other places there may be more precise li
Guido Urdaneta
2017/02/08 10:50:42
Done.
| |
76 if (std::fabs(value1 - value2) <= blink::DoubleConstraint::kConstraintEpsilon) | |
77 return 0.0; | |
78 | |
79 return std::fabs(value1 - value2) / | |
80 std::max(std::fabs(value1), std::fabs(value2)); | |
81 } | |
82 | |
83 // Returns a pair with the minimum and maximum aspect ratios supported by the | |
84 // source resolution settings |source_height| and |source_width|, subject to | |
85 // given width and height constraints. | |
86 void GetSourceAspectRatioRange(int source_height, | |
87 int source_width, | |
88 const blink::LongConstraint& height_constraint, | |
89 const blink::LongConstraint& width_constraint, | |
90 double* min_source_aspect_ratio, | |
91 double* max_source_aspect_ratio) { | |
92 DCHECK_GE(source_height, 1); | |
93 DCHECK_GE(source_width, 1); | |
94 long min_height = 1; | |
95 if (ConstraintHasMin(height_constraint)) | |
96 min_height = std::max(min_height, ConstraintMin(height_constraint)); | |
97 | |
98 long max_height = source_height; | |
99 if (ConstraintHasMax(height_constraint)) | |
100 max_height = std::min(max_height, ConstraintMax(height_constraint)); | |
101 | |
102 long min_width = 1; | |
103 if (ConstraintHasMin(width_constraint)) | |
104 min_width = std::max(min_width, ConstraintMin(width_constraint)); | |
105 | |
106 long max_width = source_width; | |
107 if (ConstraintHasMax(width_constraint)) | |
108 max_width = std::min(max_width, ConstraintMax(width_constraint)); | |
109 | |
110 *min_source_aspect_ratio = | |
111 std::max(static_cast<double>(min_width) / static_cast<double>(max_height), | |
112 kMinSourceAspectRatio); | |
113 *max_source_aspect_ratio = | |
114 std::max(static_cast<double>(max_width) / static_cast<double>(min_height), | |
115 kMinSourceAspectRatio); | |
116 } | |
117 | |
118 // Returns a custom distance between a string and a string constraint. | |
119 // Returns 0 if |value| satisfies |constraint|. HUGE_VAL otherwise. | |
120 double StringConstraintSourceDistance(const blink::WebString& value, | |
121 const blink::StringConstraint& constraint, | |
122 const char** failed_constraint_name) { | |
123 if (constraint.matches(value)) | |
124 return 0.0; | |
125 | |
126 if (failed_constraint_name) | |
127 *failed_constraint_name = constraint.name(); | |
128 return HUGE_VAL; | |
129 } | |
130 | |
131 // Returns a custom distance function suitable for screen dimensions, given | |
132 // a |constraint| (e.g. width or height) and a candidate value |source_value|. | |
133 // A source can support track resolutions in the range [1, |source_value|], | |
134 // using cropping if necessary. | |
135 // If the source range and the constraint range are disjoint, return HUGE_VAL. | |
136 // If the constraint has maximum, penalize sources that exceed the maximum | |
137 // by returning Distance(|source_value|, maximum). This is intended to prefer, | |
138 // among sources that satisfy the constraint, those that have lower resource | |
139 // usage. Otherwise, return zero. | |
140 double ResolutionConstraintSourceDistance( | |
141 int source_value, | |
142 const blink::LongConstraint& constraint, | |
143 const char** failed_constraint_name) { | |
144 DCHECK_GE(source_value, 1); | |
145 bool constraint_has_max = ConstraintHasMax(constraint); | |
146 long constraint_max = constraint_has_max ? ConstraintMax(constraint) : -1; | |
147 | |
148 // If the intersection between the source range and the constraint range is | |
149 // empty, return HUGE_VAL. | |
150 if ((constraint_has_max && constraint_max < 1) || | |
151 (ConstraintHasMin(constraint) && | |
152 source_value < ConstraintMin(constraint))) { | |
153 if (failed_constraint_name) | |
154 *failed_constraint_name = constraint.name(); | |
155 return HUGE_VAL; | |
156 } | |
157 | |
158 // If the source value exceeds the maximum requested, penalize. | |
159 if (constraint_has_max && source_value > constraint_max) | |
160 return Distance(source_value, constraint_max); | |
161 | |
162 return 0.0; | |
163 } | |
164 | |
165 // Returns a custom distance function suitable for frame rate, given | |
166 // a |constraint| and a candidate value. | |
167 // A source can support track frame rates in the interval (0.0, |source_value|], | |
168 // using frame-rate adjustments if necessary. | |
169 // If the source range and the constraint range are disjoint, return HUGE_VAL. | |
170 // If the constraint has maximum, penalize source frame rates that exceed the | |
171 // maximum by returning Distance(|source_value|, maximum). This is intended to | |
172 // prefer, among sources that satisfy the constraint, those that have lower | |
173 // resource usage. Otherwise, return zero. | |
174 double FrameRateConstraintSourceDistance( | |
175 double source_value, | |
176 const blink::DoubleConstraint& constraint, | |
177 const char** failed_constraint_name) { | |
178 DCHECK_GT(source_value, 0.0); | |
179 bool constraint_has_max = ConstraintHasMax(constraint); | |
180 double constraint_max = constraint_has_max ? ConstraintMax(constraint) : -1.0; | |
181 | |
182 if ((constraint_has_max && constraint_max <= 0.0) || | |
183 (ConstraintHasMin(constraint) && | |
184 source_value < ConstraintMin(constraint) - | |
185 blink::DoubleConstraint::kConstraintEpsilon)) { | |
186 if (failed_constraint_name) | |
187 *failed_constraint_name = constraint.name(); | |
188 return HUGE_VAL; | |
189 } | |
190 | |
191 if (constraint_has_max && source_value > constraint_max) | |
192 return Distance(source_value, constraint_max); | |
193 | |
194 return 0.0; | |
195 } | |
196 | |
197 // Returns a custom distance function suitable for aspect ratio, given | |
198 // the values for the aspect_ratio, width and height constraints, and candidate | |
199 // source values for width and height. | |
200 // A source can support track resolutions that range from | |
201 // min_width x min_height to max_width x max_height | |
202 // where | |
203 // min_width = max(1, width_constraint.min) | |
204 // min_height = max(1, height_constraint.min) | |
205 // max_width = min(source_width, width_constraint.max) | |
206 // max_height = min(source_height, height_constraint.max) | |
207 // The aspect-ratio range supported by the source is determined by the extremes | |
208 // of those resolutions. | |
209 // min_ar = min_width / max_height. | |
210 // max_ar = max_width / min_height. | |
211 // | |
212 // If the supported range [min_ar, max_ar] and the range specified by the | |
213 // aspectRatio constraint are disjoint, return HUGE_VAL. Otherwise, return zero. | |
214 double AspectRatioConstraintSourceDistance( | |
215 int source_height, | |
216 int source_width, | |
217 const blink::LongConstraint& height_constraint, | |
218 const blink::LongConstraint& width_constraint, | |
219 const blink::DoubleConstraint& aspect_ratio_constraint, | |
220 const char** failed_constraint_name) { | |
221 DCHECK_GT(source_height, 1); | |
222 DCHECK_GT(source_width, 1); | |
223 | |
224 bool ar_constraint_has_min = ConstraintHasMin(aspect_ratio_constraint); | |
225 double ar_constraint_min = | |
226 ar_constraint_has_min ? ConstraintMin(aspect_ratio_constraint) : -1.0; | |
227 bool ar_constraint_has_max = ConstraintHasMax(aspect_ratio_constraint); | |
228 double ar_constraint_max = | |
229 ar_constraint_has_max ? ConstraintMax(aspect_ratio_constraint) : -1.0; | |
230 | |
231 double min_source_aspect_ratio; | |
232 double max_source_aspect_ratio; | |
233 GetSourceAspectRatioRange(source_height, source_width, height_constraint, | |
234 width_constraint, &min_source_aspect_ratio, | |
235 &max_source_aspect_ratio); | |
236 | |
237 // If the supported range and the constraint rage are disjoint, return | |
238 // HUGE_VAL. | |
239 if ((ar_constraint_has_min && | |
240 max_source_aspect_ratio < | |
241 ar_constraint_min - blink::DoubleConstraint::kConstraintEpsilon) || | |
242 (ar_constraint_has_max && | |
243 min_source_aspect_ratio > | |
244 ar_constraint_max + blink::DoubleConstraint::kConstraintEpsilon)) { | |
245 if (failed_constraint_name) | |
246 *failed_constraint_name = aspect_ratio_constraint.name(); | |
247 return HUGE_VAL; | |
248 } | |
249 | |
250 return 0.0; | |
251 } | |
252 | |
253 // Returns a custom distance function suitable for the googPowerLineFrequency | |
254 // constraint, given a |constraint| and a candidate value |source_value|. | |
255 // The distance is HUGE_VAL if |source_value| cannot satisfy |constraint|. | |
256 // Otherwise, the distance is zero. | |
257 double PowerLineFrequencyConstraintSourceDistance( | |
258 const blink::LongConstraint& constraint, | |
259 media::PowerLineFrequency source_value, | |
260 const char** failed_constraint_name) { | |
261 bool constraint_has_min = ConstraintHasMin(constraint); | |
262 bool constraint_has_max = ConstraintHasMax(constraint); | |
263 long constraint_min = constraint_has_min ? ConstraintMin(constraint) : -1L; | |
264 long constraint_max = constraint_has_max ? ConstraintMax(constraint) : -1L; | |
265 long source_value_long = static_cast<long>(source_value); | |
266 | |
267 if ((constraint_has_max && source_value_long > constraint_max) || | |
268 (constraint_has_min && source_value_long < constraint_min)) { | |
269 if (failed_constraint_name) | |
270 *failed_constraint_name = constraint.name(); | |
271 return HUGE_VAL; | |
272 } | |
273 | |
274 return 0.0; | |
275 } | |
276 | |
277 // Returns a custom distance for constraints that depend on the device | |
278 // characteristics that have a fixed value. | |
279 double DeviceSourceDistance( | |
280 const std::string& device_id, | |
281 ::mojom::FacingMode facing_mode, | |
282 const blink::WebMediaTrackConstraintSet& constraint_set, | |
283 const char** failed_constraint_name) { | |
284 return StringConstraintSourceDistance(blink::WebString::fromASCII(device_id), | |
285 constraint_set.deviceId, | |
286 failed_constraint_name) + | |
287 StringConstraintSourceDistance(ToWebString(facing_mode), | |
288 constraint_set.facingMode, | |
289 failed_constraint_name); | |
290 } | |
291 | |
292 // Returns a custom distance for constraints that depend on a video-capture | |
293 // format. | |
294 double FormatSourceDistance( | |
295 const media::VideoCaptureFormat& format, | |
296 const blink::WebMediaTrackConstraintSet& constraint_set, | |
297 const char** failed_constraint_name) { | |
298 return ResolutionConstraintSourceDistance(format.frame_size.height(), | |
299 constraint_set.height, | |
300 failed_constraint_name) + | |
301 ResolutionConstraintSourceDistance(format.frame_size.width(), | |
302 constraint_set.width, | |
303 failed_constraint_name) + | |
304 AspectRatioConstraintSourceDistance( | |
305 format.frame_size.height(), format.frame_size.width(), | |
306 constraint_set.height, constraint_set.width, | |
307 constraint_set.aspectRatio, failed_constraint_name) + | |
308 FrameRateConstraintSourceDistance(format.frame_rate, | |
309 constraint_set.frameRate, | |
310 failed_constraint_name); | |
311 } | |
312 | |
313 // Returns a custom distance between a set of candidate settings and a | |
314 // constraint set. It is simply the sum of the distances for each individual | |
315 // setting in |candidate|. | |
316 // If |candidate| cannot satisfy constraint, the distance is HUGE_VAL. | |
317 // Otherwise the distance is a finite value. Candidates with lower distance | |
318 // satisfy |constraint_set| in a "better" way. | |
319 double CandidateSourceDistance( | |
320 const VideoCaptureSourceSettings& candidate, | |
321 const blink::WebMediaTrackConstraintSet& constraint_set, | |
322 const char** failed_constraint_name) { | |
323 return DeviceSourceDistance(candidate.device_id(), candidate.facing_mode(), | |
324 constraint_set, failed_constraint_name) + | |
325 FormatSourceDistance(candidate.format(), constraint_set, | |
326 failed_constraint_name) + | |
327 PowerLineFrequencyConstraintSourceDistance( | |
328 constraint_set.googPowerLineFrequency, | |
329 candidate.power_line_frequency(), failed_constraint_name); | |
330 } | |
331 | |
332 // Returns the fitness distance between |value| and |constraint|. | |
333 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
334 double StringConstraintFitnessDistance( | |
335 const blink::WebString& value, | |
336 const blink::StringConstraint& constraint) { | |
337 if (!constraint.hasIdeal()) | |
338 return 0.0; | |
339 | |
340 for (auto& ideal_value : constraint.ideal()) { | |
341 if (value == ideal_value) | |
342 return 0.0; | |
343 } | |
344 | |
345 return 1.0; | |
346 } | |
347 | |
348 // Returns the fitness distance between |value| and |constraint| for | |
349 // resolution constraints (i.e., width and height). | |
350 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
351 double ResolutionConstraintFitnessDistance( | |
352 long value, | |
353 const blink::LongConstraint& constraint) { | |
354 if (!constraint.hasIdeal()) | |
355 return 0.0; | |
356 | |
357 // Source resolutions greater than ideal support the ideal value with | |
358 // cropping. | |
359 if (value >= constraint.ideal()) | |
360 return 0.0; | |
361 | |
362 return Distance(value, constraint.ideal()); | |
363 } | |
364 | |
365 // Returns the fitness distance between |value| and |constraint| for | |
366 // resolution constraints (i.e., width and height), ignoring cropping. | |
367 // This measures how well a native resolution supports the idea value. | |
368 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
369 double ResolutionConstraintNativeFitnessDistance( | |
370 long value, | |
371 const blink::LongConstraint& constraint) { | |
372 return constraint.hasIdeal() ? Distance(value, constraint.ideal()) : 0.0; | |
373 } | |
374 | |
375 // Returns the fitness distance between a source resolution settings | |
376 // and the aspectRatio constraint, taking into account resolution restrictions | |
377 // on the source imposed by the width and height constraints. | |
378 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
379 double AspectRatioConstraintFitnessDistance( | |
380 long source_height, | |
381 long source_width, | |
382 const blink::LongConstraint& height_constraint, | |
383 const blink::LongConstraint& width_constraint, | |
384 const blink::DoubleConstraint& aspect_ratio_constraint) { | |
385 DCHECK_GT(source_height, 1); | |
386 DCHECK_GT(source_width, 1); | |
387 | |
388 if (!aspect_ratio_constraint.hasIdeal()) | |
389 return 0.0; | |
390 | |
391 double min_source_aspect_ratio; | |
392 double max_source_aspect_ratio; | |
393 GetSourceAspectRatioRange(source_height, source_width, height_constraint, | |
394 width_constraint, &min_source_aspect_ratio, | |
395 &max_source_aspect_ratio); | |
396 | |
397 // If the supported aspect ratio range does not include the ideal aspect | |
398 // ratio, compute fitness using the spec formula. | |
399 if (max_source_aspect_ratio < | |
400 aspect_ratio_constraint.ideal() - | |
401 blink::DoubleConstraint::kConstraintEpsilon) { | |
402 return Distance(max_source_aspect_ratio, aspect_ratio_constraint.ideal()); | |
403 } | |
404 | |
405 if (min_source_aspect_ratio > | |
406 aspect_ratio_constraint.ideal() + | |
407 blink::DoubleConstraint::kConstraintEpsilon) { | |
408 return Distance(min_source_aspect_ratio, aspect_ratio_constraint.ideal()); | |
409 } | |
410 | |
411 // Otherwise, the ideal aspect ratio can be supported and the fitness is 0. | |
412 return 0.0; | |
413 } | |
414 | |
415 // Returns the fitness distance between |value| and |constraint| for the | |
416 // frameRate constraint. | |
417 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
418 double FrameRateConstraintFitnessDistance( | |
419 double value, | |
420 const blink::DoubleConstraint& constraint) { | |
421 if (!constraint.hasIdeal()) | |
422 return 0.0; | |
423 | |
424 // Source frame rates greater than ideal support the ideal value using | |
425 // frame-rate adjustment. | |
426 if (value >= | |
427 constraint.ideal() - blink::DoubleConstraint::kConstraintEpsilon) { | |
428 return 0.0; | |
429 } | |
430 | |
431 return Distance(value, constraint.ideal()); | |
432 } | |
433 | |
434 // Returns the fitness distance between |value| and |constraint| for the | |
435 // frameRate constraint, ignoring frame-rate adjustment. | |
436 // It measures how well the native frame rate supports the ideal value. | |
437 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
438 double FrameRateConstraintNativeFitnessDistance( | |
439 double value, | |
440 const blink::DoubleConstraint& constraint) { | |
441 return constraint.hasIdeal() ? Distance(value, constraint.ideal()) : 0.0; | |
442 } | |
443 | |
444 // Returns the fitness distance between |value| and |constraint| for the | |
445 // googPowerLineFrequency constraint. | |
446 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
447 double PowerLineFrequencyConstraintFitnessDistance( | |
448 long value, | |
449 const blink::LongConstraint& constraint) { | |
450 if (!constraint.hasIdeal()) | |
451 return 0.0; | |
452 | |
453 // This constraint is of type long, but it behaves as an enum. Thus, values | |
454 // equal to ideal have fitness 0.0 and any other values have fitness 1.0. | |
455 if (value == constraint.ideal()) | |
456 return 0.0; | |
457 | |
458 return 1.0; | |
459 } | |
460 | |
461 // Returns the fitness distance between a settings candidate and a constraint | |
462 // set. The returned value is the sum of the fitness distances between each | |
463 // setting in |candidate| and the corresponding constraint in |constraint_set|. | |
464 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
465 double CandidateFitnessDistance( | |
466 const VideoCaptureSourceSettings& candidate, | |
467 const blink::WebMediaTrackConstraintSet& constraint_set) { | |
468 DCHECK(std::isfinite( | |
469 CandidateSourceDistance(candidate, constraint_set, nullptr))); | |
470 double fitness = 0.0; | |
471 fitness += AspectRatioConstraintFitnessDistance( | |
472 candidate.GetHeight(), candidate.GetWidth(), constraint_set.height, | |
473 constraint_set.width, constraint_set.aspectRatio); | |
474 fitness += StringConstraintFitnessDistance(candidate.GetDeviceId(), | |
475 constraint_set.deviceId); | |
476 fitness += StringConstraintFitnessDistance(candidate.GetFacingMode(), | |
477 constraint_set.facingMode); | |
478 fitness += FrameRateConstraintFitnessDistance(candidate.GetFrameRate(), | |
479 constraint_set.frameRate); | |
480 fitness += PowerLineFrequencyConstraintFitnessDistance( | |
481 candidate.GetPowerLineFrequency(), constraint_set.googPowerLineFrequency); | |
482 fitness += ResolutionConstraintFitnessDistance(candidate.GetHeight(), | |
483 constraint_set.height); | |
484 fitness += ResolutionConstraintFitnessDistance(candidate.GetWidth(), | |
485 constraint_set.width); | |
486 | |
487 return fitness; | |
488 } | |
489 | |
490 // Returns the native fitness distance between a settings candidate and a | |
491 // constraint set. The returned value is the sum of the fitness distances for | |
492 // the native values of settings that support a range of values (i.e., width, | |
493 // height and frame rate). | |
494 // Based on https://w3c.github.io/mediacapture-main/#dfn-applyconstraints. | |
495 double CandidateNativeFitnessDistance( | |
496 const VideoCaptureSourceSettings& candidate, | |
497 const blink::WebMediaTrackConstraintSet& constraint_set) { | |
498 DCHECK(std::isfinite( | |
499 CandidateSourceDistance(candidate, constraint_set, nullptr))); | |
500 double fitness = 0.0; | |
501 fitness += FrameRateConstraintNativeFitnessDistance(candidate.GetFrameRate(), | |
502 constraint_set.frameRate); | |
503 fitness += ResolutionConstraintNativeFitnessDistance(candidate.GetHeight(), | |
504 constraint_set.height); | |
505 fitness += ResolutionConstraintNativeFitnessDistance(candidate.GetWidth(), | |
506 constraint_set.width); | |
507 | |
508 return fitness; | |
509 } | |
510 | |
511 using DistanceVector = std::vector<double>; | |
512 | |
513 // This function appends additional entries to |distance_vector| based on | |
514 // custom distance metrics between |candidate| and some default settings. | |
515 // These entries are to be used as the final tie breaker for candidates that | |
516 // are equally good according to the spec and the custom distance functions | |
517 // between candidates and constraints. | |
518 void AppendDistanceFromDefault(const VideoCaptureSourceSettings& candidate, | |
519 const VideoCaptureCapabilities& capabilities, | |
520 DistanceVector* distance_vector) { | |
521 // Favor IDs that appear first in the enumeration. | |
522 for (size_t i = 0; i < capabilities.device_capabilities.size(); ++i) { | |
523 if (candidate.device_id() == | |
524 capabilities.device_capabilities[i]->device_id) { | |
525 distance_vector->push_back(i); | |
526 break; | |
527 } | |
528 } | |
529 | |
530 // Prefer default power-line frequency. | |
531 double power_line_frequency_distance = | |
532 candidate.power_line_frequency() == | |
533 media::PowerLineFrequency::FREQUENCY_DEFAULT | |
534 ? 0.0 | |
535 : HUGE_VAL; | |
536 distance_vector->push_back(power_line_frequency_distance); | |
537 | |
538 // Prefer a resolution with area close to the default. | |
539 int candidate_area = candidate.format().frame_size.GetArea(); | |
540 double resolution_distance = | |
541 candidate_area == kDefaultResolutionArea | |
542 ? 0.0 | |
543 : Distance(candidate_area, kDefaultResolutionArea); | |
544 distance_vector->push_back(resolution_distance); | |
545 | |
546 // Prefer a frame rate close to the default. | |
547 double frame_rate_distance = | |
548 candidate.format().frame_rate == MediaStreamVideoSource::kDefaultFrameRate | |
549 ? 0.0 | |
550 : Distance(candidate.format().frame_rate, | |
551 MediaStreamVideoSource::kDefaultFrameRate); | |
552 distance_vector->push_back(frame_rate_distance); | |
553 } | |
554 | |
555 } // namespace | |
556 | |
557 VideoCaptureCapabilities::VideoCaptureCapabilities() = default; | |
558 VideoCaptureCapabilities::VideoCaptureCapabilities( | |
559 VideoCaptureCapabilities&& other) = default; | |
560 VideoCaptureCapabilities::~VideoCaptureCapabilities() = default; | |
561 VideoCaptureCapabilities& VideoCaptureCapabilities::operator=( | |
562 VideoCaptureCapabilities&& other) = default; | |
563 | |
564 VideoCaptureSourceSettings::VideoCaptureSourceSettings( | |
565 const VideoCaptureSourceSettings& other) = default; | |
566 VideoCaptureSourceSettings::VideoCaptureSourceSettings( | |
567 VideoCaptureSourceSettings&& other) = default; | |
568 VideoCaptureSourceSettings::~VideoCaptureSourceSettings() = default; | |
569 VideoCaptureSourceSettings& VideoCaptureSourceSettings::operator=( | |
570 const VideoCaptureSourceSettings& other) = default; | |
571 VideoCaptureSourceSettings& VideoCaptureSourceSettings::operator=( | |
572 VideoCaptureSourceSettings&& other) = default; | |
573 | |
574 VideoCaptureSourceSettings::VideoCaptureSourceSettings() | |
575 : facing_mode_(::mojom::FacingMode::NONE), | |
576 power_line_frequency_(media::PowerLineFrequency::FREQUENCY_DEFAULT) {} | |
577 | |
578 VideoCaptureSourceSettings::VideoCaptureSourceSettings( | |
579 const std::string& device_id, | |
580 const media::VideoCaptureFormat& format, | |
581 ::mojom::FacingMode facing_mode, | |
582 media::PowerLineFrequency power_line_frequency) | |
583 : device_id_(device_id), | |
584 format_(format), | |
585 facing_mode_(facing_mode), | |
586 power_line_frequency_(power_line_frequency) {} | |
587 | |
588 blink::WebString VideoCaptureSourceSettings::GetFacingMode() const { | |
589 return ToWebString(facing_mode_); | |
590 } | |
591 | |
592 long VideoCaptureSourceSettings::GetPowerLineFrequency() const { | |
593 return static_cast<long>(power_line_frequency_); | |
594 } | |
595 | |
596 long VideoCaptureSourceSettings::GetWidth() const { | |
597 return format_.frame_size.width(); | |
598 } | |
599 | |
600 long VideoCaptureSourceSettings::GetHeight() const { | |
601 return format_.frame_size.height(); | |
602 } | |
603 | |
604 double VideoCaptureSourceSettings::GetFrameRate() const { | |
605 return format_.frame_rate; | |
606 } | |
607 | |
608 blink::WebString VideoCaptureSourceSettings::GetDeviceId() const { | |
609 return blink::WebString::fromASCII(device_id_.data()); | |
610 } | |
611 | |
612 const char kDefaultFailedConstraintName[] = ""; | |
613 | |
614 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult() | |
615 : failed_constraint_name(kDefaultFailedConstraintName) {} | |
616 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult( | |
617 const VideoCaptureSourceSelectionResult& other) = default; | |
618 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult( | |
619 VideoCaptureSourceSelectionResult&& other) = default; | |
620 VideoCaptureSourceSelectionResult::~VideoCaptureSourceSelectionResult() = | |
621 default; | |
622 VideoCaptureSourceSelectionResult& VideoCaptureSourceSelectionResult::operator=( | |
623 const VideoCaptureSourceSelectionResult& other) = default; | |
624 VideoCaptureSourceSelectionResult& VideoCaptureSourceSelectionResult::operator=( | |
625 VideoCaptureSourceSelectionResult&& other) = default; | |
626 | |
627 VideoCaptureSourceSelectionResult SelectVideoCaptureSourceSettings( | |
628 const VideoCaptureCapabilities& capabilities, | |
629 const blink::WebMediaConstraints& constraints) { | |
630 // This function works only if infinity is defined for the double type. | |
631 DCHECK(std::numeric_limits<double>::has_infinity); | |
632 | |
633 // A distance vector contains: | |
634 // a) For each advanced constraint set, a 0/1 value indicating if the | |
635 // candidate satisfies the corresponding constraint set. | |
636 // b) Fitness distance for the candidate based on support for the ideal values | |
637 // of the basic constraint set. | |
638 // c) A custom distance value based on how "well" a candidate satisfies each | |
639 // constraint set, including basic and advanced sets. | |
640 // d) Native fitness distance for the candidate based on support for the | |
641 // ideal values of the basic constraint set using native values for | |
642 // settings that can support a range of values. | |
643 // e) A custom distance value based on how close the candidate is to default | |
644 // settings. | |
645 // Parts (a) and (b) are according to spec. Parts (c) to (e) are | |
646 // implementation specific and used to break ties. | |
647 DistanceVector best_distance(2 * constraints.advanced().size() + 3 + | |
648 kNumDefaultDistanceEntries); | |
649 std::fill(best_distance.begin(), best_distance.end(), HUGE_VAL); | |
650 VideoCaptureSourceSelectionResult result; | |
651 const char* failed_constraint_name = result.failed_constraint_name; | |
652 | |
653 for (auto& device : capabilities.device_capabilities) { | |
654 double basic_device_distance = | |
655 DeviceSourceDistance(device->device_id, device->facing_mode, | |
656 constraints.basic(), &failed_constraint_name); | |
657 if (!std::isfinite(basic_device_distance)) | |
658 continue; | |
659 | |
660 for (auto& format : device->formats) { | |
661 double basic_format_distance = FormatSourceDistance( | |
662 format, constraints.basic(), &failed_constraint_name); | |
663 if (!std::isfinite(basic_format_distance)) | |
664 continue; | |
665 | |
666 for (auto& power_line_frequency : capabilities.power_line_capabilities) { | |
667 double basic_power_line_frequency_distance = | |
668 PowerLineFrequencyConstraintSourceDistance( | |
669 constraints.basic().googPowerLineFrequency, | |
670 power_line_frequency, &failed_constraint_name); | |
671 if (!std::isfinite(basic_power_line_frequency_distance)) | |
672 continue; | |
673 | |
674 // The candidate satisfies the basic constraint set. | |
675 double candidate_basic_custom_distance = | |
676 basic_device_distance + basic_format_distance + | |
677 basic_power_line_frequency_distance; | |
678 DCHECK(std::isfinite(candidate_basic_custom_distance)); | |
679 | |
680 // Temporary vector to save custom distances for advanced constraints. | |
681 // Custom distances must be added to the candidate distance vector after | |
682 // all the spec-mandated values. | |
683 DistanceVector advanced_custom_distance_vector; | |
684 VideoCaptureSourceSettings candidate(device->device_id, format, | |
685 device->facing_mode, | |
686 power_line_frequency); | |
687 DistanceVector candidate_distance_vector; | |
688 // First criteria for valid candidates is satisfaction of advanced | |
689 // constraint sets. | |
690 for (const auto& advanced : constraints.advanced()) { | |
691 double custom_distance = | |
692 CandidateSourceDistance(candidate, advanced, nullptr); | |
693 advanced_custom_distance_vector.push_back(custom_distance); | |
694 double spec_distance = std::isfinite(custom_distance) ? 0 : 1; | |
695 candidate_distance_vector.push_back(spec_distance); | |
696 } | |
697 | |
698 // Second criterion is fitness distance. | |
699 candidate_distance_vector.push_back( | |
700 CandidateFitnessDistance(candidate, constraints.basic())); | |
701 | |
702 // Third criteria are custom distances to constraint sets. | |
703 candidate_distance_vector.push_back(candidate_basic_custom_distance); | |
704 std::copy(advanced_custom_distance_vector.begin(), | |
705 advanced_custom_distance_vector.end(), | |
706 std::back_inserter(candidate_distance_vector)); | |
707 | |
708 // Fourth criteria is native fitness distance. | |
709 candidate_distance_vector.push_back( | |
710 CandidateNativeFitnessDistance(candidate, constraints.basic())); | |
711 | |
712 // Final criteria are custom distances to default settings. | |
713 AppendDistanceFromDefault(candidate, capabilities, | |
714 &candidate_distance_vector); | |
715 | |
716 DCHECK_EQ(best_distance.size(), candidate_distance_vector.size()); | |
717 if (candidate_distance_vector < best_distance) { | |
718 best_distance = candidate_distance_vector; | |
719 result.settings = std::move(candidate); | |
720 result.failed_constraint_name = nullptr; | |
721 } | |
722 } | |
723 } | |
724 } | |
725 | |
726 if (!result.has_value()) | |
727 result.failed_constraint_name = failed_constraint_name; | |
728 | |
729 return result; | |
730 } | |
731 | |
732 } // namespace content | |
OLD | NEW |