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

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

Issue 2707203006: Minor refactoring of support classes for video-device constraints. (Closed)
Patch Set: hbos comment + fix in accessor Created 3 years, 10 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/media/media_stream_constraints_util_video_source.h" 5 #include "content/renderer/media/media_stream_constraints_util_video_device.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "content/renderer/media/media_stream_constraints_util.h"
13 #include "content/renderer/media/media_stream_video_source.h" 14 #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/WebMediaConstraints.h"
15 #include "third_party/WebKit/public/platform/WebString.h" 16 #include "third_party/WebKit/public/platform/WebString.h"
16 17
17 namespace content { 18 namespace content {
18 19
19 namespace { 20 namespace {
20 21
21 // Number of default settings to be used as final tie-breaking criteria for 22 // Number of default settings to be used as final tie-breaking criteria for
22 // settings that are equally good at satisfying constraints: 23 // settings that are equally good at satisfying constraints:
(...skipping 19 matching lines...) Expand all
42 return blink::WebString::fromASCII("environment"); 43 return blink::WebString::fromASCII("environment");
43 case ::mojom::FacingMode::LEFT: 44 case ::mojom::FacingMode::LEFT:
44 return blink::WebString::fromASCII("left"); 45 return blink::WebString::fromASCII("left");
45 case ::mojom::FacingMode::RIGHT: 46 case ::mojom::FacingMode::RIGHT:
46 return blink::WebString::fromASCII("right"); 47 return blink::WebString::fromASCII("right");
47 default: 48 default:
48 return blink::WebString::fromASCII(""); 49 return blink::WebString::fromASCII("");
49 } 50 }
50 } 51 }
51 52
52 template <typename ConstraintType> 53 struct VideoDeviceCaptureSourceSettings {
53 bool ConstraintHasMax(const ConstraintType& constraint) { 54 public:
54 return constraint.hasMax() || constraint.hasExact(); 55 VideoDeviceCaptureSourceSettings()
55 } 56 : facing_mode_(::mojom::FacingMode::NONE),
57 power_line_frequency_(media::PowerLineFrequency::FREQUENCY_DEFAULT) {}
56 58
57 template <typename ConstraintType> 59 VideoDeviceCaptureSourceSettings(
58 bool ConstraintHasMin(const ConstraintType& constraint) { 60 const std::string& device_id,
59 return constraint.hasMin() || constraint.hasExact(); 61 const media::VideoCaptureFormat& format,
60 } 62 ::mojom::FacingMode facing_mode,
63 media::PowerLineFrequency power_line_frequency)
64 : device_id_(device_id),
65 format_(format),
66 facing_mode_(facing_mode),
67 power_line_frequency_(power_line_frequency) {}
61 68
62 template <typename ConstraintType> 69 VideoDeviceCaptureSourceSettings(
63 auto ConstraintMax(const ConstraintType& constraint) 70 const VideoDeviceCaptureSourceSettings& other) = default;
64 -> decltype(constraint.max()) { 71 VideoDeviceCaptureSourceSettings& operator=(
65 DCHECK(ConstraintHasMax(constraint)); 72 const VideoDeviceCaptureSourceSettings& other) = default;
66 return constraint.hasExact() ? constraint.exact() : constraint.max(); 73 VideoDeviceCaptureSourceSettings(VideoDeviceCaptureSourceSettings&& other) =
67 } 74 default;
75 VideoDeviceCaptureSourceSettings& operator=(
76 VideoDeviceCaptureSourceSettings&& other) = default;
77 ~VideoDeviceCaptureSourceSettings() = default;
68 78
69 template <typename ConstraintType> 79 // These accessor-like methods transform types to what Blink constraint
70 auto ConstraintMin(const ConstraintType& constraint) 80 // classes expect.
71 -> decltype(constraint.min()) { 81 blink::WebString GetFacingMode() const { return ToWebString(facing_mode_); }
72 DCHECK(ConstraintHasMin(constraint)); 82 long GetPowerLineFrequency() const {
73 return constraint.hasExact() ? constraint.exact() : constraint.min(); 83 return static_cast<long>(power_line_frequency_);
84 }
85 long GetWidth() const { return format_.frame_size.width(); }
86 long GetHeight() const { return format_.frame_size.height(); }
87 double GetFrameRate() const { return format_.frame_rate; }
88 blink::WebString GetDeviceId() const {
89 return blink::WebString::fromASCII(device_id_.data());
90 }
91 blink::WebString GetVideoKind() const {
92 return GetVideoKindForFormat(format_);
93 }
94
95 // Accessors.
96 const media::VideoCaptureFormat& format() const { return format_; }
97 const std::string& device_id() const { return device_id_; }
98 ::mojom::FacingMode facing_mode() const { return facing_mode_; }
99 media::PowerLineFrequency power_line_frequency() const {
100 return power_line_frequency_;
101 }
102
103 private:
104 std::string device_id_;
105 media::VideoCaptureFormat format_;
106 ::mojom::FacingMode facing_mode_;
107 media::PowerLineFrequency power_line_frequency_;
108 };
109
110 VideoDeviceCaptureSourceSelectionResult ResultFromSettings(
111 const VideoDeviceCaptureSourceSettings& settings) {
112 VideoDeviceCaptureSourceSelectionResult result;
113 result.capture_params.power_line_frequency = settings.power_line_frequency();
114 result.capture_params.requested_format = settings.format();
115 result.device_id = settings.device_id();
116 result.facing_mode = settings.facing_mode();
117 result.failed_constraint_name = nullptr;
118
119 return result;
74 } 120 }
75 121
76 // Generic distance function between two numeric values. Based on the fitness 122 // Generic distance function between two numeric values. Based on the fitness
77 // distance function described in 123 // distance function described in
78 // https://w3c.github.io/mediacapture-main/#dfn-fitness-distance 124 // https://w3c.github.io/mediacapture-main/#dfn-fitness-distance
79 double Distance(double value1, double value2) { 125 double Distance(double value1, double value2) {
80 if (std::fabs(value1 - value2) <= blink::DoubleConstraint::kConstraintEpsilon) 126 if (std::fabs(value1 - value2) <= blink::DoubleConstraint::kConstraintEpsilon)
81 return 0.0; 127 return 0.0;
82 128
83 return std::fabs(value1 - value2) / 129 return std::fabs(value1 - value2) /
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 failed_constraint_name); 363 failed_constraint_name);
318 } 364 }
319 365
320 // Returns a custom distance between a set of candidate settings and a 366 // 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 367 // constraint set. It is simply the sum of the distances for each individual
322 // setting in |candidate|. 368 // setting in |candidate|.
323 // If |candidate| cannot satisfy constraint, the distance is HUGE_VAL. 369 // If |candidate| cannot satisfy constraint, the distance is HUGE_VAL.
324 // Otherwise the distance is a finite value. Candidates with lower distance 370 // Otherwise the distance is a finite value. Candidates with lower distance
325 // satisfy |constraint_set| in a "better" way. 371 // satisfy |constraint_set| in a "better" way.
326 double CandidateSourceDistance( 372 double CandidateSourceDistance(
327 const VideoCaptureSourceSettings& candidate, 373 const VideoDeviceCaptureSourceSettings& candidate,
328 const blink::WebMediaTrackConstraintSet& constraint_set, 374 const blink::WebMediaTrackConstraintSet& constraint_set,
329 const char** failed_constraint_name) { 375 const char** failed_constraint_name) {
330 return DeviceSourceDistance(candidate.device_id(), candidate.facing_mode(), 376 return DeviceSourceDistance(candidate.device_id(), candidate.facing_mode(),
331 constraint_set, failed_constraint_name) + 377 constraint_set, failed_constraint_name) +
332 FormatSourceDistance(candidate.format(), constraint_set, 378 FormatSourceDistance(candidate.format(), constraint_set,
333 failed_constraint_name) + 379 failed_constraint_name) +
334 PowerLineFrequencyConstraintSourceDistance( 380 PowerLineFrequencyConstraintSourceDistance(
335 constraint_set.googPowerLineFrequency, 381 constraint_set.googPowerLineFrequency,
336 candidate.power_line_frequency(), failed_constraint_name); 382 candidate.power_line_frequency(), failed_constraint_name);
337 } 383 }
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 return 0.0; 509 return 0.0;
464 510
465 return 1.0; 511 return 1.0;
466 } 512 }
467 513
468 // Returns the fitness distance between a settings candidate and a constraint 514 // 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 515 // set. The returned value is the sum of the fitness distances between each
470 // setting in |candidate| and the corresponding constraint in |constraint_set|. 516 // setting in |candidate| and the corresponding constraint in |constraint_set|.
471 // Based on https://w3c.github.io/mediacapture-main/#dfn-fitness-distance. 517 // Based on https://w3c.github.io/mediacapture-main/#dfn-fitness-distance.
472 double CandidateFitnessDistance( 518 double CandidateFitnessDistance(
473 const VideoCaptureSourceSettings& candidate, 519 const VideoDeviceCaptureSourceSettings& candidate,
474 const blink::WebMediaTrackConstraintSet& constraint_set) { 520 const blink::WebMediaTrackConstraintSet& constraint_set) {
475 DCHECK(std::isfinite( 521 DCHECK(std::isfinite(
476 CandidateSourceDistance(candidate, constraint_set, nullptr))); 522 CandidateSourceDistance(candidate, constraint_set, nullptr)));
477 double fitness = 0.0; 523 double fitness = 0.0;
478 fitness += AspectRatioConstraintFitnessDistance( 524 fitness += AspectRatioConstraintFitnessDistance(
479 candidate.GetHeight(), candidate.GetWidth(), constraint_set.height, 525 candidate.GetHeight(), candidate.GetWidth(), constraint_set.height,
480 constraint_set.width, constraint_set.aspectRatio); 526 constraint_set.width, constraint_set.aspectRatio);
481 fitness += StringConstraintFitnessDistance(candidate.GetDeviceId(), 527 fitness += StringConstraintFitnessDistance(candidate.GetDeviceId(),
482 constraint_set.deviceId); 528 constraint_set.deviceId);
483 fitness += StringConstraintFitnessDistance(candidate.GetFacingMode(), 529 fitness += StringConstraintFitnessDistance(candidate.GetFacingMode(),
(...skipping 11 matching lines...) Expand all
495 541
496 return fitness; 542 return fitness;
497 } 543 }
498 544
499 // Returns the native fitness distance between a settings candidate and a 545 // 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 546 // 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, 547 // the native values of settings that support a range of values (i.e., width,
502 // height and frame rate). 548 // height and frame rate).
503 // Based on https://w3c.github.io/mediacapture-main/#dfn-fitness-distance. 549 // Based on https://w3c.github.io/mediacapture-main/#dfn-fitness-distance.
504 double CandidateNativeFitnessDistance( 550 double CandidateNativeFitnessDistance(
505 const VideoCaptureSourceSettings& candidate, 551 const VideoDeviceCaptureSourceSettings& candidate,
506 const blink::WebMediaTrackConstraintSet& constraint_set) { 552 const blink::WebMediaTrackConstraintSet& constraint_set) {
507 DCHECK(std::isfinite( 553 DCHECK(std::isfinite(
508 CandidateSourceDistance(candidate, constraint_set, nullptr))); 554 CandidateSourceDistance(candidate, constraint_set, nullptr)));
509 double fitness = 0.0; 555 double fitness = 0.0;
510 fitness += FrameRateConstraintNativeFitnessDistance(candidate.GetFrameRate(), 556 fitness += FrameRateConstraintNativeFitnessDistance(candidate.GetFrameRate(),
511 constraint_set.frameRate); 557 constraint_set.frameRate);
512 fitness += ResolutionConstraintNativeFitnessDistance(candidate.GetHeight(), 558 fitness += ResolutionConstraintNativeFitnessDistance(candidate.GetHeight(),
513 constraint_set.height); 559 constraint_set.height);
514 fitness += ResolutionConstraintNativeFitnessDistance(candidate.GetWidth(), 560 fitness += ResolutionConstraintNativeFitnessDistance(candidate.GetWidth(),
515 constraint_set.width); 561 constraint_set.width);
516 562
517 return fitness; 563 return fitness;
518 } 564 }
519 565
520 using DistanceVector = std::vector<double>; 566 using DistanceVector = std::vector<double>;
521 567
522 // This function appends additional entries to |distance_vector| based on 568 // This function appends additional entries to |distance_vector| based on
523 // custom distance metrics between |candidate| and some default settings. 569 // custom distance metrics between |candidate| and some default settings.
524 // These entries are to be used as the final tie breaker for candidates that 570 // 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 571 // are equally good according to the spec and the custom distance functions
526 // between candidates and constraints. 572 // between candidates and constraints.
527 void AppendDistanceFromDefault(const VideoCaptureSourceSettings& candidate, 573 void AppendDistanceFromDefault(
528 const VideoCaptureCapabilities& capabilities, 574 const VideoDeviceCaptureSourceSettings& candidate,
529 DistanceVector* distance_vector) { 575 const VideoDeviceCaptureCapabilities& capabilities,
576 DistanceVector* distance_vector) {
530 // Favor IDs that appear first in the enumeration. 577 // Favor IDs that appear first in the enumeration.
531 for (size_t i = 0; i < capabilities.device_capabilities.size(); ++i) { 578 for (size_t i = 0; i < capabilities.device_capabilities.size(); ++i) {
532 if (candidate.device_id() == 579 if (candidate.device_id() ==
533 capabilities.device_capabilities[i]->device_id) { 580 capabilities.device_capabilities[i]->device_id) {
534 distance_vector->push_back(i); 581 distance_vector->push_back(i);
535 break; 582 break;
536 } 583 }
537 } 584 }
538 585
539 // Prefer default power-line frequency. 586 // Prefer default power-line frequency.
(...skipping 23 matching lines...) Expand all
563 610
564 } // namespace 611 } // namespace
565 612
566 blink::WebString GetVideoKindForFormat( 613 blink::WebString GetVideoKindForFormat(
567 const media::VideoCaptureFormat& format) { 614 const media::VideoCaptureFormat& format) {
568 return (format.pixel_format == media::PIXEL_FORMAT_Y16) 615 return (format.pixel_format == media::PIXEL_FORMAT_Y16)
569 ? blink::WebString::fromASCII(kVideoKindDepth) 616 ? blink::WebString::fromASCII(kVideoKindDepth)
570 : blink::WebString::fromASCII(kVideoKindColor); 617 : blink::WebString::fromASCII(kVideoKindColor);
571 } 618 }
572 619
573 VideoCaptureCapabilities::VideoCaptureCapabilities() = default; 620 VideoDeviceCaptureCapabilities::VideoDeviceCaptureCapabilities() = default;
574 VideoCaptureCapabilities::VideoCaptureCapabilities( 621 VideoDeviceCaptureCapabilities::VideoDeviceCaptureCapabilities(
575 VideoCaptureCapabilities&& other) = default; 622 VideoDeviceCaptureCapabilities&& other) = default;
576 VideoCaptureCapabilities::~VideoCaptureCapabilities() = default; 623 VideoDeviceCaptureCapabilities::~VideoDeviceCaptureCapabilities() = default;
577 VideoCaptureCapabilities& VideoCaptureCapabilities::operator=( 624 VideoDeviceCaptureCapabilities& VideoDeviceCaptureCapabilities::operator=(
578 VideoCaptureCapabilities&& other) = default; 625 VideoDeviceCaptureCapabilities&& 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 626
632 const char kDefaultFailedConstraintName[] = ""; 627 const char kDefaultFailedConstraintName[] = "";
633 628
634 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult() 629 VideoDeviceCaptureSourceSelectionResult::
635 : failed_constraint_name(kDefaultFailedConstraintName) {} 630 VideoDeviceCaptureSourceSelectionResult()
636 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult( 631 : failed_constraint_name(kDefaultFailedConstraintName),
637 const VideoCaptureSourceSelectionResult& other) = default; 632 facing_mode(::mojom::FacingMode::NONE) {}
638 VideoCaptureSourceSelectionResult::VideoCaptureSourceSelectionResult( 633 VideoDeviceCaptureSourceSelectionResult::
639 VideoCaptureSourceSelectionResult&& other) = default; 634 VideoDeviceCaptureSourceSelectionResult(
640 VideoCaptureSourceSelectionResult::~VideoCaptureSourceSelectionResult() = 635 const VideoDeviceCaptureSourceSelectionResult& other) = default;
641 default; 636 VideoDeviceCaptureSourceSelectionResult::
642 VideoCaptureSourceSelectionResult& VideoCaptureSourceSelectionResult::operator=( 637 VideoDeviceCaptureSourceSelectionResult(
643 const VideoCaptureSourceSelectionResult& other) = default; 638 VideoDeviceCaptureSourceSelectionResult&& other) = default;
644 VideoCaptureSourceSelectionResult& VideoCaptureSourceSelectionResult::operator=( 639 VideoDeviceCaptureSourceSelectionResult::
645 VideoCaptureSourceSelectionResult&& other) = default; 640 ~VideoDeviceCaptureSourceSelectionResult() = default;
641 VideoDeviceCaptureSourceSelectionResult&
642 VideoDeviceCaptureSourceSelectionResult::operator=(
643 const VideoDeviceCaptureSourceSelectionResult& other) = default;
644 VideoDeviceCaptureSourceSelectionResult&
645 VideoDeviceCaptureSourceSelectionResult::operator=(
646 VideoDeviceCaptureSourceSelectionResult&& other) = default;
646 647
647 VideoCaptureSourceSelectionResult SelectVideoCaptureSourceSettings( 648 VideoDeviceCaptureSourceSelectionResult SelectVideoDeviceCaptureSourceSettings(
648 const VideoCaptureCapabilities& capabilities, 649 const VideoDeviceCaptureCapabilities& capabilities,
649 const blink::WebMediaConstraints& constraints) { 650 const blink::WebMediaConstraints& constraints) {
650 // This function works only if infinity is defined for the double type. 651 // This function works only if infinity is defined for the double type.
651 DCHECK(std::numeric_limits<double>::has_infinity); 652 DCHECK(std::numeric_limits<double>::has_infinity);
652 653
653 // A distance vector contains: 654 // A distance vector contains:
654 // a) For each advanced constraint set, a 0/1 value indicating if the 655 // a) For each advanced constraint set, a 0/1 value indicating if the
655 // candidate satisfies the corresponding constraint set. 656 // candidate satisfies the corresponding constraint set.
656 // b) Fitness distance for the candidate based on support for the ideal values 657 // b) Fitness distance for the candidate based on support for the ideal values
657 // of the basic constraint set. 658 // of the basic constraint set.
658 // c) A custom distance value based on how "well" a candidate satisfies each 659 // c) A custom distance value based on how "well" a candidate satisfies each
659 // constraint set, including basic and advanced sets. 660 // constraint set, including basic and advanced sets.
660 // d) Native fitness distance for the candidate based on support for the 661 // 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 // ideal values of the basic constraint set using native values for
662 // settings that can support a range of values. 663 // settings that can support a range of values.
663 // e) A custom distance value based on how close the candidate is to default 664 // e) A custom distance value based on how close the candidate is to default
664 // settings. 665 // settings.
665 // Parts (a) and (b) are according to spec. Parts (c) to (e) are 666 // Parts (a) and (b) are according to spec. Parts (c) to (e) are
666 // implementation specific and used to break ties. 667 // implementation specific and used to break ties.
667 DistanceVector best_distance(2 * constraints.advanced().size() + 3 + 668 DistanceVector best_distance(2 * constraints.advanced().size() + 3 +
668 kNumDefaultDistanceEntries); 669 kNumDefaultDistanceEntries);
669 std::fill(best_distance.begin(), best_distance.end(), HUGE_VAL); 670 std::fill(best_distance.begin(), best_distance.end(), HUGE_VAL);
670 VideoCaptureSourceSelectionResult result; 671 VideoDeviceCaptureSourceSelectionResult result;
671 const char* failed_constraint_name = result.failed_constraint_name; 672 const char* failed_constraint_name = result.failed_constraint_name;
672 673
673 for (auto& device : capabilities.device_capabilities) { 674 for (auto& device : capabilities.device_capabilities) {
674 double basic_device_distance = 675 double basic_device_distance =
675 DeviceSourceDistance(device->device_id, device->facing_mode, 676 DeviceSourceDistance(device->device_id, device->facing_mode,
676 constraints.basic(), &failed_constraint_name); 677 constraints.basic(), &failed_constraint_name);
677 if (!std::isfinite(basic_device_distance)) 678 if (!std::isfinite(basic_device_distance))
678 continue; 679 continue;
679 680
680 for (auto& format : device->formats) { 681 for (auto& format : device->formats) {
(...skipping 13 matching lines...) Expand all
694 // The candidate satisfies the basic constraint set. 695 // The candidate satisfies the basic constraint set.
695 double candidate_basic_custom_distance = 696 double candidate_basic_custom_distance =
696 basic_device_distance + basic_format_distance + 697 basic_device_distance + basic_format_distance +
697 basic_power_line_frequency_distance; 698 basic_power_line_frequency_distance;
698 DCHECK(std::isfinite(candidate_basic_custom_distance)); 699 DCHECK(std::isfinite(candidate_basic_custom_distance));
699 700
700 // Temporary vector to save custom distances for advanced constraints. 701 // Temporary vector to save custom distances for advanced constraints.
701 // Custom distances must be added to the candidate distance vector after 702 // Custom distances must be added to the candidate distance vector after
702 // all the spec-mandated values. 703 // all the spec-mandated values.
703 DistanceVector advanced_custom_distance_vector; 704 DistanceVector advanced_custom_distance_vector;
704 VideoCaptureSourceSettings candidate(device->device_id, format, 705 VideoDeviceCaptureSourceSettings candidate(device->device_id, format,
705 device->facing_mode, 706 device->facing_mode,
706 power_line_frequency); 707 power_line_frequency);
707 DistanceVector candidate_distance_vector; 708 DistanceVector candidate_distance_vector;
708 // First criteria for valid candidates is satisfaction of advanced 709 // First criteria for valid candidates is satisfaction of advanced
709 // constraint sets. 710 // constraint sets.
710 for (const auto& advanced : constraints.advanced()) { 711 for (const auto& advanced : constraints.advanced()) {
711 double custom_distance = 712 double custom_distance =
712 CandidateSourceDistance(candidate, advanced, nullptr); 713 CandidateSourceDistance(candidate, advanced, nullptr);
713 advanced_custom_distance_vector.push_back(custom_distance); 714 advanced_custom_distance_vector.push_back(custom_distance);
714 double spec_distance = std::isfinite(custom_distance) ? 0 : 1; 715 double spec_distance = std::isfinite(custom_distance) ? 0 : 1;
715 candidate_distance_vector.push_back(spec_distance); 716 candidate_distance_vector.push_back(spec_distance);
716 } 717 }
(...skipping 12 matching lines...) Expand all
729 candidate_distance_vector.push_back( 730 candidate_distance_vector.push_back(
730 CandidateNativeFitnessDistance(candidate, constraints.basic())); 731 CandidateNativeFitnessDistance(candidate, constraints.basic()));
731 732
732 // Final criteria are custom distances to default settings. 733 // Final criteria are custom distances to default settings.
733 AppendDistanceFromDefault(candidate, capabilities, 734 AppendDistanceFromDefault(candidate, capabilities,
734 &candidate_distance_vector); 735 &candidate_distance_vector);
735 736
736 DCHECK_EQ(best_distance.size(), candidate_distance_vector.size()); 737 DCHECK_EQ(best_distance.size(), candidate_distance_vector.size());
737 if (candidate_distance_vector < best_distance) { 738 if (candidate_distance_vector < best_distance) {
738 best_distance = candidate_distance_vector; 739 best_distance = candidate_distance_vector;
739 result.settings = std::move(candidate); 740 result = ResultFromSettings(candidate);
740 result.failed_constraint_name = nullptr;
741 } 741 }
742 } 742 }
743 } 743 }
744 } 744 }
745 745
746 if (!result.has_value()) 746 if (!result.HasValue())
747 result.failed_constraint_name = failed_constraint_name; 747 result.failed_constraint_name = failed_constraint_name;
748 748
749 return result; 749 return result;
750 } 750 }
751 751
752 } // namespace content 752 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698