Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: content/renderer/media/media_stream_constraints_util_video_source.cc

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

Powered by Google App Engine
This is Rietveld 408576698