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

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

Issue 2777703002: Introduce SelectSettings algorithm for MediaStream video tracks. (Closed)
Patch Set: merge Content and Device result classes Created 3 years, 8 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_device.h" 5 #include "content/renderer/media/media_stream_constraints_util_video_device.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/optional.h" 10 #include "base/optional.h"
11 #include "content/renderer/media/media_stream_video_source.h" 11 #include "content/renderer/media/media_stream_video_source.h"
12 #include "content/renderer/media/mock_constraint_factory.h" 12 #include "content/renderer/media/mock_constraint_factory.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" 14 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
15 15
16 namespace content { 16 namespace content {
17 17
18 namespace { 18 namespace {
19 19
20 const char kDeviceID1[] = "fake_device_1"; 20 const char kDeviceID1[] = "fake_device_1";
21 const char kDeviceID2[] = "fake_device_2"; 21 const char kDeviceID2[] = "fake_device_2";
22 const char kDeviceID3[] = "fake_device_3"; 22 const char kDeviceID3[] = "fake_device_3";
23 const char kDeviceID4[] = "fake_device_4"; 23 const char kDeviceID4[] = "fake_device_4";
24
25 void CheckTrackAdapterSettingsEqualsResolution(
26 const VideoCaptureSettings& settings) {
27 EXPECT_EQ(settings.Format().frame_size.width(),
28 settings.track_adapter_settings().max_width);
29 EXPECT_EQ(settings.Format().frame_size.height(),
30 settings.track_adapter_settings().max_height);
31 EXPECT_EQ(1.0 / settings.Format().frame_size.height(),
32 settings.track_adapter_settings().min_aspect_ratio);
33 EXPECT_EQ(settings.Format().frame_size.width(),
34 settings.track_adapter_settings().max_aspect_ratio);
24 } 35 }
25 36
37 void CheckTrackAdapterSettingsEqualsFrameRate(
38 const VideoCaptureSettings& settings,
39 double value = 0.0) {
40 if (value >= settings.FrameRate())
41 value = 0.0;
42 EXPECT_EQ(value, settings.track_adapter_settings().max_frame_rate);
43 }
44
45 void CheckTrackAdapterSettingsEqualsFormat(
46 const VideoCaptureSettings& settings) {
47 CheckTrackAdapterSettingsEqualsResolution(settings);
48 CheckTrackAdapterSettingsEqualsFrameRate(settings);
49 }
50
51 double AspectRatio(const media::VideoCaptureFormat& format) {
52 return static_cast<double>(format.frame_size.width()) /
53 static_cast<double>(format.frame_size.height());
54 }
55
56 } // namespace
57
26 class MediaStreamConstraintsUtilVideoDeviceTest : public testing::Test { 58 class MediaStreamConstraintsUtilVideoDeviceTest : public testing::Test {
27 public: 59 public:
28 void SetUp() override { 60 void SetUp() override {
29 // Default device. It is default because it is the first in the enumeration. 61 // Default device. It is default because it is the first in the enumeration.
30 ::mojom::VideoInputDeviceCapabilitiesPtr device = 62 ::mojom::VideoInputDeviceCapabilitiesPtr device =
31 ::mojom::VideoInputDeviceCapabilities::New(); 63 ::mojom::VideoInputDeviceCapabilities::New();
32 device->device_id = kDeviceID1; 64 device->device_id = kDeviceID1;
33 device->facing_mode = ::mojom::FacingMode::NONE; 65 device->facing_mode = ::mojom::FacingMode::NONE;
34 device->formats = { 66 device->formats = {
35 media::VideoCaptureFormat(gfx::Size(200, 200), 40.0f, 67 media::VideoCaptureFormat(gfx::Size(200, 200), 40.0f,
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 media::PIXEL_FORMAT_Y16)}; 126 media::PIXEL_FORMAT_Y16)};
95 capabilities_.device_capabilities.push_back(std::move(device)); 127 capabilities_.device_capabilities.push_back(std::move(device));
96 128
97 capabilities_.power_line_capabilities = { 129 capabilities_.power_line_capabilities = {
98 media::PowerLineFrequency::FREQUENCY_DEFAULT, 130 media::PowerLineFrequency::FREQUENCY_DEFAULT,
99 media::PowerLineFrequency::FREQUENCY_50HZ, 131 media::PowerLineFrequency::FREQUENCY_50HZ,
100 media::PowerLineFrequency::FREQUENCY_60HZ, 132 media::PowerLineFrequency::FREQUENCY_60HZ,
101 }; 133 };
102 134
103 capabilities_.noise_reduction_capabilities = { 135 capabilities_.noise_reduction_capabilities = {
104 rtc::Optional<bool>(), rtc::Optional<bool>(true), 136 base::Optional<bool>(), base::Optional<bool>(true),
105 rtc::Optional<bool>(false), 137 base::Optional<bool>(false),
106 }; 138 };
107 139
108 default_device_ = capabilities_.device_capabilities[0].get(); 140 default_device_ = capabilities_.device_capabilities[0].get();
109 low_res_device_ = capabilities_.device_capabilities[1].get(); 141 low_res_device_ = capabilities_.device_capabilities[1].get();
110 high_res_device_ = capabilities_.device_capabilities[2].get(); 142 high_res_device_ = capabilities_.device_capabilities[2].get();
111 default_closest_format_ = &default_device_->formats[1]; 143 default_closest_format_ = &default_device_->formats[1];
112 low_res_closest_format_ = &low_res_device_->formats[2]; 144 low_res_closest_format_ = &low_res_device_->formats[2];
113 high_res_closest_format_ = &high_res_device_->formats[2]; 145 high_res_closest_format_ = &high_res_device_->formats[2];
114 high_res_highest_format_ = &high_res_device_->formats[5]; 146 high_res_highest_format_ = &high_res_device_->formats[5];
115 } 147 }
116 148
117 protected: 149 protected:
118 VideoDeviceCaptureSourceSelectionResult SelectSettings() { 150 VideoCaptureSettings SelectSettings() {
119 blink::WebMediaConstraints constraints = 151 blink::WebMediaConstraints constraints =
120 constraint_factory_.CreateWebMediaConstraints(); 152 constraint_factory_.CreateWebMediaConstraints();
121 return SelectVideoDeviceCaptureSourceSettings(capabilities_, constraints); 153 return SelectSettingsVideoDeviceCapture(capabilities_, constraints);
122 } 154 }
123 155
124 VideoDeviceCaptureCapabilities capabilities_; 156 VideoDeviceCaptureCapabilities capabilities_;
125 const mojom::VideoInputDeviceCapabilities* default_device_; 157 const mojom::VideoInputDeviceCapabilities* default_device_;
126 const mojom::VideoInputDeviceCapabilities* low_res_device_; 158 const mojom::VideoInputDeviceCapabilities* low_res_device_;
127 const mojom::VideoInputDeviceCapabilities* high_res_device_; 159 const mojom::VideoInputDeviceCapabilities* high_res_device_;
128 // Closest formats to the default settings. 160 // Closest formats to the default settings.
129 const media::VideoCaptureFormat* default_closest_format_; 161 const media::VideoCaptureFormat* default_closest_format_;
130 const media::VideoCaptureFormat* low_res_closest_format_; 162 const media::VideoCaptureFormat* low_res_closest_format_;
131 const media::VideoCaptureFormat* high_res_closest_format_; 163 const media::VideoCaptureFormat* high_res_closest_format_;
132 const media::VideoCaptureFormat* high_res_highest_format_; 164 const media::VideoCaptureFormat* high_res_highest_format_;
133 165
134 MockConstraintFactory constraint_factory_; 166 MockConstraintFactory constraint_factory_;
135 }; 167 };
136 168
137 // The Unconstrained test checks the default selection criteria. 169 // The Unconstrained test checks the default selection criteria.
138 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, Unconstrained) { 170 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, Unconstrained) {
139 constraint_factory_.Reset(); 171 constraint_factory_.Reset();
140 auto result = SelectSettings(); 172 auto result = SelectSettings();
141 EXPECT_TRUE(result.HasValue()); 173 EXPECT_TRUE(result.HasValue());
142 // Should select the default device with closest-to-default settings. 174 // Should select the default device with closest-to-default settings.
143 EXPECT_EQ(default_device_->device_id, result.device_id()); 175 EXPECT_EQ(default_device_->device_id, result.device_id());
144 EXPECT_EQ(default_device_->facing_mode, result.facing_mode()); 176 EXPECT_EQ(default_device_->facing_mode, result.facing_mode());
145 EXPECT_EQ(*default_closest_format_, result.Format()); 177 EXPECT_EQ(*default_closest_format_, result.Format());
146 // Should select default settings for other constraints. 178 // Should select default settings for other constraints.
147 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT, 179 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT,
148 result.PowerLineFrequency()); 180 result.PowerLineFrequency());
149 EXPECT_EQ(rtc::Optional<bool>(), result.noise_reduction()); 181 EXPECT_EQ(base::Optional<bool>(), result.noise_reduction());
150 } 182 }
151 183
152 // The "Overconstrained" tests verify that failure of any single required 184 // The "Overconstrained" tests verify that failure of any single required
153 // constraint results in failure to select a candidate. 185 // constraint results in failure to select a candidate.
154 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, OverconstrainedOnDeviceID) { 186 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, OverconstrainedOnDeviceID) {
155 constraint_factory_.Reset(); 187 constraint_factory_.Reset();
156 constraint_factory_.basic().deviceId.setExact( 188 constraint_factory_.basic().deviceId.setExact(
157 blink::WebString::fromASCII("NONEXISTING")); 189 blink::WebString::fromASCII("NONEXISTING"));
158 auto result = SelectSettings(); 190 auto result = SelectSettings();
159 EXPECT_FALSE(result.HasValue()); 191 EXPECT_FALSE(result.HasValue());
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 ::mojom::VideoInputDeviceCapabilitiesPtr device = 343 ::mojom::VideoInputDeviceCapabilitiesPtr device =
312 ::mojom::VideoInputDeviceCapabilities::New(); 344 ::mojom::VideoInputDeviceCapabilities::New();
313 device->device_id = kDeviceID1; 345 device->device_id = kDeviceID1;
314 device->facing_mode = ::mojom::FacingMode::NONE; 346 device->facing_mode = ::mojom::FacingMode::NONE;
315 device->formats = { 347 device->formats = {
316 media::VideoCaptureFormat(gfx::Size(200, 200), 40.0f, 348 media::VideoCaptureFormat(gfx::Size(200, 200), 40.0f,
317 media::PIXEL_FORMAT_I420), 349 media::PIXEL_FORMAT_I420),
318 }; 350 };
319 capabilities.device_capabilities.push_back(std::move(device)); 351 capabilities.device_capabilities.push_back(std::move(device));
320 capabilities.power_line_capabilities = capabilities_.power_line_capabilities; 352 capabilities.power_line_capabilities = capabilities_.power_line_capabilities;
321 capabilities.noise_reduction_capabilities = {rtc::Optional<bool>(false)}; 353 capabilities.noise_reduction_capabilities = {base::Optional<bool>(false)};
322 354
323 constraint_factory_.Reset(); 355 constraint_factory_.Reset();
324 constraint_factory_.basic().googNoiseReduction.setExact(true); 356 constraint_factory_.basic().googNoiseReduction.setExact(true);
325 auto constraints = constraint_factory_.CreateWebMediaConstraints(); 357 auto constraints = constraint_factory_.CreateWebMediaConstraints();
326 auto result = 358 auto result = SelectSettingsVideoDeviceCapture(capabilities, constraints);
327 SelectVideoDeviceCaptureSourceSettings(capabilities, constraints);
328 EXPECT_FALSE(result.HasValue()); 359 EXPECT_FALSE(result.HasValue());
329 EXPECT_EQ(constraint_factory_.basic().googNoiseReduction.name(), 360 EXPECT_EQ(constraint_factory_.basic().googNoiseReduction.name(),
330 result.failed_constraint_name()); 361 result.failed_constraint_name());
331 } 362 }
332 363
333 // The "Mandatory" and "Ideal" tests check that various selection criteria work 364 // The "Mandatory" and "Ideal" tests check that various selection criteria work
334 // for each individual constraint in the basic constraint set. 365 // for each individual constraint in the basic constraint set.
335 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryDeviceID) { 366 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryDeviceID) {
336 constraint_factory_.Reset(); 367 constraint_factory_.Reset();
337 constraint_factory_.basic().deviceId.setExact( 368 constraint_factory_.basic().deviceId.setExact(
338 blink::WebString::fromASCII(default_device_->device_id)); 369 blink::WebString::fromASCII(default_device_->device_id));
339 auto result = SelectSettings(); 370 auto result = SelectSettings();
340 EXPECT_TRUE(result.HasValue()); 371 EXPECT_TRUE(result.HasValue());
341 EXPECT_EQ(default_device_->device_id, result.device_id()); 372 EXPECT_EQ(default_device_->device_id, result.device_id());
342 EXPECT_EQ(*default_closest_format_, result.Format()); 373 EXPECT_EQ(*default_closest_format_, result.Format());
343 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT, 374 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT,
344 result.PowerLineFrequency()); 375 result.PowerLineFrequency());
376 CheckTrackAdapterSettingsEqualsFormat(result);
345 377
346 constraint_factory_.basic().deviceId.setExact( 378 constraint_factory_.basic().deviceId.setExact(
347 blink::WebString::fromASCII(low_res_device_->device_id)); 379 blink::WebString::fromASCII(low_res_device_->device_id));
348 result = SelectSettings(); 380 result = SelectSettings();
349 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 381 EXPECT_EQ(low_res_device_->device_id, result.device_id());
350 EXPECT_EQ(*low_res_closest_format_, result.Format()); 382 EXPECT_EQ(*low_res_closest_format_, result.Format());
351 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT, 383 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT,
352 result.PowerLineFrequency()); 384 result.PowerLineFrequency());
385 CheckTrackAdapterSettingsEqualsFormat(result);
353 386
354 constraint_factory_.basic().deviceId.setExact( 387 constraint_factory_.basic().deviceId.setExact(
355 blink::WebString::fromASCII(high_res_device_->device_id)); 388 blink::WebString::fromASCII(high_res_device_->device_id));
356 result = SelectSettings(); 389 result = SelectSettings();
357 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 390 EXPECT_EQ(high_res_device_->device_id, result.device_id());
358 EXPECT_EQ(*high_res_closest_format_, result.Format()); 391 EXPECT_EQ(*high_res_closest_format_, result.Format());
359 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT, 392 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT,
360 result.PowerLineFrequency()); 393 result.PowerLineFrequency());
394 CheckTrackAdapterSettingsEqualsFormat(result);
361 } 395 }
362 396
363 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryFacingMode) { 397 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryFacingMode) {
364 constraint_factory_.Reset(); 398 constraint_factory_.Reset();
365 constraint_factory_.basic().facingMode.setExact( 399 constraint_factory_.basic().facingMode.setExact(
366 blink::WebString::fromASCII("environment")); 400 blink::WebString::fromASCII("environment"));
367 auto result = SelectSettings(); 401 auto result = SelectSettings();
368 EXPECT_TRUE(result.HasValue()); 402 EXPECT_TRUE(result.HasValue());
369 EXPECT_EQ(::mojom::FacingMode::ENVIRONMENT, result.facing_mode()); 403 EXPECT_EQ(::mojom::FacingMode::ENVIRONMENT, result.facing_mode());
370 // Only the low-res device supports environment facing mode. Should select 404 // Only the low-res device supports environment facing mode. Should select
371 // default settings for everything else. 405 // default settings for everything else.
372 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 406 EXPECT_EQ(low_res_device_->device_id, result.device_id());
373 EXPECT_EQ(*low_res_closest_format_, result.Format()); 407 EXPECT_EQ(*low_res_closest_format_, result.Format());
374 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT, 408 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT,
375 result.PowerLineFrequency()); 409 result.PowerLineFrequency());
410 CheckTrackAdapterSettingsEqualsFormat(result);
376 411
377 constraint_factory_.basic().facingMode.setExact( 412 constraint_factory_.basic().facingMode.setExact(
378 blink::WebString::fromASCII("user")); 413 blink::WebString::fromASCII("user"));
379 result = SelectSettings(); 414 result = SelectSettings();
380 EXPECT_TRUE(result.HasValue()); 415 EXPECT_TRUE(result.HasValue());
381 EXPECT_EQ(::mojom::FacingMode::USER, result.facing_mode()); 416 EXPECT_EQ(::mojom::FacingMode::USER, result.facing_mode());
382 // Only the high-res device supports user facing mode. Should select default 417 // Only the high-res device supports user facing mode. Should select default
383 // settings for everything else. 418 // settings for everything else.
384 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 419 EXPECT_EQ(high_res_device_->device_id, result.device_id());
385 EXPECT_EQ(*high_res_closest_format_, result.Format()); 420 EXPECT_EQ(*high_res_closest_format_, result.Format());
386 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT, 421 EXPECT_EQ(media::PowerLineFrequency::FREQUENCY_DEFAULT,
387 result.PowerLineFrequency()); 422 result.PowerLineFrequency());
423 CheckTrackAdapterSettingsEqualsFormat(result);
388 } 424 }
389 425
390 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryVideoKind) { 426 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryVideoKind) {
391 constraint_factory_.Reset(); 427 constraint_factory_.Reset();
392 constraint_factory_.basic().videoKind.setExact( 428 constraint_factory_.basic().videoKind.setExact(
393 blink::WebString::fromASCII("depth")); 429 blink::WebString::fromASCII("depth"));
394 auto result = SelectSettings(); 430 auto result = SelectSettings();
395 EXPECT_TRUE(result.HasValue()); 431 EXPECT_TRUE(result.HasValue());
396 EXPECT_EQ(kDeviceID4, result.device_id()); 432 EXPECT_EQ(kDeviceID4, result.device_id());
397 EXPECT_EQ(media::PIXEL_FORMAT_Y16, result.Format().pixel_format); 433 EXPECT_EQ(media::PIXEL_FORMAT_Y16, result.Format().pixel_format);
434 CheckTrackAdapterSettingsEqualsFormat(result);
398 435
399 constraint_factory_.basic().videoKind.setExact( 436 constraint_factory_.basic().videoKind.setExact(
400 blink::WebString::fromASCII("color")); 437 blink::WebString::fromASCII("color"));
401 result = SelectSettings(); 438 result = SelectSettings();
402 EXPECT_TRUE(result.HasValue()); 439 EXPECT_TRUE(result.HasValue());
403 EXPECT_EQ(default_device_->device_id, result.device_id()); 440 EXPECT_EQ(default_device_->device_id, result.device_id());
441 CheckTrackAdapterSettingsEqualsFormat(result);
404 } 442 }
405 443
406 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryPowerLineFrequency) { 444 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryPowerLineFrequency) {
407 constraint_factory_.Reset(); 445 constraint_factory_.Reset();
408 const media::PowerLineFrequency kPowerLineFrequencies[] = { 446 const media::PowerLineFrequency kPowerLineFrequencies[] = {
409 media::PowerLineFrequency::FREQUENCY_50HZ, 447 media::PowerLineFrequency::FREQUENCY_50HZ,
410 media::PowerLineFrequency::FREQUENCY_60HZ}; 448 media::PowerLineFrequency::FREQUENCY_60HZ};
411 for (auto power_line_frequency : kPowerLineFrequencies) { 449 for (auto power_line_frequency : kPowerLineFrequencies) {
412 constraint_factory_.basic().googPowerLineFrequency.setExact( 450 constraint_factory_.basic().googPowerLineFrequency.setExact(
413 static_cast<long>(power_line_frequency)); 451 static_cast<long>(power_line_frequency));
414 auto result = SelectSettings(); 452 auto result = SelectSettings();
415 EXPECT_TRUE(result.HasValue()); 453 EXPECT_TRUE(result.HasValue());
416 EXPECT_EQ(power_line_frequency, result.PowerLineFrequency()); 454 EXPECT_EQ(power_line_frequency, result.PowerLineFrequency());
417 // The default device and settings closest to the default should be 455 // The default device and settings closest to the default should be
418 // selected. 456 // selected.
419 EXPECT_EQ(default_device_->device_id, result.device_id()); 457 EXPECT_EQ(default_device_->device_id, result.device_id());
420 EXPECT_EQ(default_device_->facing_mode, result.facing_mode()); 458 EXPECT_EQ(default_device_->facing_mode, result.facing_mode());
421 EXPECT_EQ(*default_closest_format_, result.Format()); 459 EXPECT_EQ(*default_closest_format_, result.Format());
460 CheckTrackAdapterSettingsEqualsFormat(result);
422 } 461 }
423 } 462 }
424 463
425 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryNoiseReduction) { 464 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryNoiseReduction) {
426 constraint_factory_.Reset(); 465 constraint_factory_.Reset();
427 const bool kNoiseReductionValues[] = {true, false}; 466 const bool kNoiseReductionValues[] = {true, false};
428 for (auto noise_reduction : kNoiseReductionValues) { 467 for (auto noise_reduction : kNoiseReductionValues) {
429 constraint_factory_.basic().googNoiseReduction.setExact(noise_reduction); 468 constraint_factory_.basic().googNoiseReduction.setExact(noise_reduction);
430 auto result = SelectSettings(); 469 auto result = SelectSettings();
431 EXPECT_TRUE(result.HasValue()); 470 EXPECT_TRUE(result.HasValue());
432 EXPECT_EQ(noise_reduction, result.noise_reduction()); 471 EXPECT_EQ(noise_reduction, result.noise_reduction());
433 // The default device and settings closest to the default should be 472 // The default device and settings closest to the default should be
434 // selected. 473 // selected.
435 EXPECT_EQ(default_device_->device_id, result.device_id()); 474 EXPECT_EQ(default_device_->device_id, result.device_id());
436 EXPECT_EQ(default_device_->facing_mode, result.facing_mode()); 475 EXPECT_EQ(default_device_->facing_mode, result.facing_mode());
437 EXPECT_EQ(*default_closest_format_, result.Format()); 476 EXPECT_EQ(*default_closest_format_, result.Format());
477 CheckTrackAdapterSettingsEqualsFormat(result);
438 } 478 }
439 } 479 }
440 480
441 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryExactHeight) { 481 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryExactHeight) {
442 constraint_factory_.Reset(); 482 constraint_factory_.Reset();
443 const int kHeight = MediaStreamVideoSource::kDefaultHeight; 483 const int kHeight = MediaStreamVideoSource::kDefaultHeight;
444 constraint_factory_.basic().height.setExact(kHeight); 484 constraint_factory_.basic().height.setExact(kHeight);
445 auto result = SelectSettings(); 485 auto result = SelectSettings();
446 EXPECT_TRUE(result.HasValue()); 486 EXPECT_TRUE(result.HasValue());
447 // All devices in |capabilities_| support the requested height. The algorithm 487 // All devices in |capabilities_| support the requested height. The algorithm
448 // should prefer the first device that supports the requested height natively, 488 // should prefer the first device that supports the requested height natively,
449 // which is the low-res device. 489 // which is the low-res device.
450 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 490 EXPECT_EQ(low_res_device_->device_id, result.device_id());
451 EXPECT_EQ(kHeight, result.Height()); 491 EXPECT_EQ(kHeight, result.Height());
492 EXPECT_EQ(kHeight, result.track_adapter_settings().max_height);
452 493
453 const int kLargeHeight = 1500; 494 const int kLargeHeight = 1500;
454 constraint_factory_.basic().height.setExact(kLargeHeight); 495 constraint_factory_.basic().height.setExact(kLargeHeight);
455 result = SelectSettings(); 496 result = SelectSettings();
456 EXPECT_TRUE(result.HasValue()); 497 EXPECT_TRUE(result.HasValue());
457 // Only the high-res device at the highest resolution supports the requested 498 // Only the high-res device at the highest resolution supports the requested
458 // height, even if not natively. 499 // height, even if not natively.
459 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 500 EXPECT_EQ(high_res_device_->device_id, result.device_id());
460 EXPECT_EQ(*high_res_highest_format_, result.Format()); 501 EXPECT_EQ(*high_res_highest_format_, result.Format());
502 EXPECT_EQ(kLargeHeight, result.track_adapter_settings().max_height);
503 EXPECT_EQ(std::round(kLargeHeight * AspectRatio(*high_res_highest_format_)),
504 result.track_adapter_settings().max_width);
461 } 505 }
462 506
463 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMinHeight) { 507 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMinHeight) {
464 constraint_factory_.Reset(); 508 constraint_factory_.Reset();
465 const int kHeight = MediaStreamVideoSource::kDefaultHeight; 509 const int kHeight = MediaStreamVideoSource::kDefaultHeight;
466 constraint_factory_.basic().height.setMin(kHeight); 510 constraint_factory_.basic().height.setMin(kHeight);
467 auto result = SelectSettings(); 511 auto result = SelectSettings();
468 EXPECT_TRUE(result.HasValue()); 512 EXPECT_TRUE(result.HasValue());
469 // All devices in |capabilities_| support the requested height range. The 513 // All devices in |capabilities_| support the requested height range. The
470 // algorithm should prefer the default device. 514 // algorithm should prefer the default device.
471 EXPECT_EQ(default_device_->device_id, result.device_id()); 515 EXPECT_EQ(default_device_->device_id, result.device_id());
472 EXPECT_LE(kHeight, result.Height()); 516 EXPECT_LE(kHeight, result.Height());
517 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
518 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
519 EXPECT_EQ(static_cast<double>(result.Width()) / kHeight,
520 result.track_adapter_settings().max_aspect_ratio);
521 EXPECT_EQ(1.0 / result.Height(),
522 result.track_adapter_settings().min_aspect_ratio);
523 CheckTrackAdapterSettingsEqualsFrameRate(result);
473 524
474 const int kLargeHeight = 1500; 525 const int kLargeHeight = 1500;
475 constraint_factory_.basic().height.setMin(kLargeHeight); 526 constraint_factory_.basic().height.setMin(kLargeHeight);
476 result = SelectSettings(); 527 result = SelectSettings();
477 EXPECT_TRUE(result.HasValue()); 528 EXPECT_TRUE(result.HasValue());
478 // Only the high-res device at the highest resolution supports the requested 529 // Only the high-res device at the highest resolution supports the requested
479 // height range. 530 // height range.
480 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 531 EXPECT_EQ(high_res_device_->device_id, result.device_id());
481 EXPECT_EQ(*high_res_highest_format_, result.Format()); 532 EXPECT_EQ(*high_res_highest_format_, result.Format());
533 EXPECT_LE(kHeight, result.Height());
534 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
535 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
536 EXPECT_EQ(static_cast<double>(result.Width()) / kLargeHeight,
537 result.track_adapter_settings().max_aspect_ratio);
538 EXPECT_EQ(1.0 / result.Height(),
539 result.track_adapter_settings().min_aspect_ratio);
540 CheckTrackAdapterSettingsEqualsFrameRate(result);
482 } 541 }
483 542
484 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMaxHeight) { 543 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMaxHeight) {
485 constraint_factory_.Reset(); 544 constraint_factory_.Reset();
486 const int kLowHeight = 20; 545 const int kLowHeight = 20;
487 constraint_factory_.basic().height.setMax(kLowHeight); 546 constraint_factory_.basic().height.setMax(kLowHeight);
488 auto result = SelectSettings(); 547 auto result = SelectSettings();
489 EXPECT_TRUE(result.HasValue()); 548 EXPECT_TRUE(result.HasValue());
490 // All devices in |capabilities_| support the requested height range. The 549 // All devices in |capabilities_| support the requested height range. The
491 // algorithm should prefer the settings that natively exceed the requested 550 // algorithm should prefer the settings that natively exceed the requested
492 // maximum by the lowest amount. In this case it is the low-res device. 551 // maximum by the lowest amount. In this case it is the low-res device.
493 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 552 EXPECT_EQ(low_res_device_->device_id, result.device_id());
494 EXPECT_EQ(low_res_device_->formats[0], result.Format()); 553 EXPECT_EQ(low_res_device_->formats[0], result.Format());
554 EXPECT_EQ(kLowHeight, result.track_adapter_settings().max_height);
555 EXPECT_EQ(std::round(kLowHeight * AspectRatio(result.Format())),
556 result.track_adapter_settings().max_width);
557 EXPECT_EQ(static_cast<double>(result.Width()),
558 result.track_adapter_settings().max_aspect_ratio);
559 EXPECT_EQ(1.0 / kLowHeight, result.track_adapter_settings().min_aspect_ratio);
560 CheckTrackAdapterSettingsEqualsFrameRate(result);
495 } 561 }
496 562
497 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryHeightRange) { 563 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryHeightRange) {
498 constraint_factory_.Reset(); 564 constraint_factory_.Reset();
499 { 565 {
500 const int kMinHeight = 480; 566 const int kMinHeight = 480;
501 const int kMaxHeight = 720; 567 const int kMaxHeight = 720;
502 constraint_factory_.basic().height.setMin(kMinHeight); 568 constraint_factory_.basic().height.setMin(kMinHeight);
503 constraint_factory_.basic().height.setMax(kMaxHeight); 569 constraint_factory_.basic().height.setMax(kMaxHeight);
504 auto result = SelectSettings(); 570 auto result = SelectSettings();
505 EXPECT_TRUE(result.HasValue()); 571 EXPECT_TRUE(result.HasValue());
506 EXPECT_GE(result.Height(), kMinHeight); 572 EXPECT_GE(result.Height(), kMinHeight);
507 EXPECT_LE(result.Height(), kMaxHeight); 573 EXPECT_LE(result.Height(), kMaxHeight);
508 // All devices in |capabilities_| support the constraint range. The 574 // All devices in |capabilities_| support the constraint range. The
509 // algorithm should prefer the default device since it has at least one 575 // algorithm should prefer the default device since it has at least one
510 // native format (the closest-to-default format) included in the requested 576 // native format (the closest-to-default format) included in the requested
511 // range. 577 // range.
512 EXPECT_EQ(default_device_->device_id, result.device_id()); 578 EXPECT_EQ(default_device_->device_id, result.device_id());
513 EXPECT_EQ(*default_closest_format_, result.Format()); 579 EXPECT_EQ(*default_closest_format_, result.Format());
580 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
581 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
582 EXPECT_EQ(static_cast<double>(result.Width()) / kMinHeight,
583 result.track_adapter_settings().max_aspect_ratio);
584 EXPECT_EQ(1.0 / result.Height(),
585 result.track_adapter_settings().min_aspect_ratio);
586 CheckTrackAdapterSettingsEqualsFrameRate(result);
514 } 587 }
515 588
516 { 589 {
517 const int kMinHeight = 550; 590 const int kMinHeight = 550;
518 const int kMaxHeight = 650; 591 const int kMaxHeight = 650;
519 constraint_factory_.basic().height.setMin(kMinHeight); 592 constraint_factory_.basic().height.setMin(kMinHeight);
520 constraint_factory_.basic().height.setMax(kMaxHeight); 593 constraint_factory_.basic().height.setMax(kMaxHeight);
521 auto result = SelectSettings(); 594 auto result = SelectSettings();
522 EXPECT_TRUE(result.HasValue()); 595 EXPECT_TRUE(result.HasValue());
523 EXPECT_GE(result.Height(), kMinHeight); 596 EXPECT_GE(result.Height(), kMinHeight);
524 EXPECT_LE(result.Height(), kMaxHeight); 597 EXPECT_LE(result.Height(), kMaxHeight);
525 // In this case, the algorithm should prefer the low-res device since it is 598 // In this case, the algorithm should prefer the low-res device since it is
526 // the first device with a native format (800x600) included in the requested 599 // the first device with a native format (800x600) included in the requested
527 // range. 600 // range.
528 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 601 EXPECT_EQ(low_res_device_->device_id, result.device_id());
529 EXPECT_EQ(800, result.Width()); 602 EXPECT_EQ(800, result.Width());
530 EXPECT_EQ(600, result.Height()); 603 EXPECT_EQ(600, result.Height());
604 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
605 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
606 EXPECT_EQ(static_cast<double>(result.Width()) / kMinHeight,
607 result.track_adapter_settings().max_aspect_ratio);
608 EXPECT_EQ(1.0 / result.Height(),
609 result.track_adapter_settings().min_aspect_ratio);
610 CheckTrackAdapterSettingsEqualsFrameRate(result);
531 } 611 }
532 612
533 { 613 {
534 const int kMinHeight = 700; 614 const int kMinHeight = 700;
535 const int kMaxHeight = 800; 615 const int kMaxHeight = 800;
536 constraint_factory_.basic().height.setMin(kMinHeight); 616 constraint_factory_.basic().height.setMin(kMinHeight);
537 constraint_factory_.basic().height.setMax(kMaxHeight); 617 constraint_factory_.basic().height.setMax(kMaxHeight);
538 auto result = SelectSettings(); 618 auto result = SelectSettings();
539 EXPECT_TRUE(result.HasValue()); 619 EXPECT_TRUE(result.HasValue());
540 EXPECT_GE(result.Height(), kMinHeight); 620 EXPECT_GE(result.Height(), kMinHeight);
541 EXPECT_LE(result.Height(), kMaxHeight); 621 EXPECT_LE(result.Height(), kMaxHeight);
542 // In this case, the algorithm should prefer the high-res device since it is 622 // In this case, the algorithm should prefer the high-res device since it is
543 // the only device with a native format (1280x720) included in the requested 623 // the only device with a native format (1280x720) included in the requested
544 // range. 624 // range.
545 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 625 EXPECT_EQ(high_res_device_->device_id, result.device_id());
546 EXPECT_EQ(1280, result.Width()); 626 EXPECT_EQ(1280, result.Width());
547 EXPECT_EQ(720, result.Height()); 627 EXPECT_EQ(720, result.Height());
628 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
629 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
630 EXPECT_EQ(static_cast<double>(result.Width()) / kMinHeight,
631 result.track_adapter_settings().max_aspect_ratio);
632 EXPECT_EQ(1.0 / result.Height(),
633 result.track_adapter_settings().min_aspect_ratio);
634 CheckTrackAdapterSettingsEqualsFrameRate(result);
548 } 635 }
549 } 636 }
550 637
551 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, IdealHeight) { 638 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, IdealHeight) {
552 constraint_factory_.Reset(); 639 constraint_factory_.Reset();
553 { 640 {
554 const int kIdealHeight = 480; 641 const int kIdealHeight = 480;
555 constraint_factory_.basic().height.setIdeal(kIdealHeight); 642 constraint_factory_.basic().height.setIdeal(kIdealHeight);
556 auto result = SelectSettings(); 643 auto result = SelectSettings();
557 EXPECT_TRUE(result.HasValue()); 644 EXPECT_TRUE(result.HasValue());
558 // The algorithm should select the first device that supports the ideal 645 // The algorithm should select the first device that supports the ideal
559 // height natively. 646 // height natively.
560 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 647 EXPECT_EQ(low_res_device_->device_id, result.device_id());
561 EXPECT_EQ(kIdealHeight, result.Height()); 648 EXPECT_EQ(kIdealHeight, result.Height());
649 CheckTrackAdapterSettingsEqualsFormat(result);
562 } 650 }
563 651
564 { 652 {
565 const int kIdealHeight = 481; 653 const int kIdealHeight = 481;
566 constraint_factory_.basic().height.setIdeal(kIdealHeight); 654 constraint_factory_.basic().height.setIdeal(kIdealHeight);
567 auto result = SelectSettings(); 655 auto result = SelectSettings();
568 EXPECT_TRUE(result.HasValue()); 656 EXPECT_TRUE(result.HasValue());
569 // In this case, the default device is selected because it can satisfy the 657 // In this case, the default device is selected because it can satisfy the
570 // ideal at a lower cost than the other devices (500 vs 600 or 720). 658 // ideal at a lower cost than the other devices (500 vs 600 or 720).
571 // Note that a native resolution of 480 is further from the ideal than 659 // Note that a native resolution of 480 is further from the ideal than
572 // 500 cropped to 480. 660 // 500 cropped to 480.
573 EXPECT_EQ(default_device_->device_id, result.device_id()); 661 EXPECT_EQ(default_device_->device_id, result.device_id());
574 EXPECT_EQ(*default_closest_format_, result.Format()); 662 EXPECT_EQ(*default_closest_format_, result.Format());
663 // The track is cropped to the ideal height, maintaining the source aspect
664 // ratio.
665 EXPECT_EQ(kIdealHeight, result.track_adapter_settings().max_height);
666 EXPECT_EQ(std::round(kIdealHeight * AspectRatio(result.Format())),
667 result.track_adapter_settings().max_width);
668 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
669 EXPECT_EQ(1.0 / result.Height(),
670 result.track_adapter_settings().min_aspect_ratio);
671 CheckTrackAdapterSettingsEqualsFrameRate(result);
575 } 672 }
576 673
577 { 674 {
578 const int kIdealHeight = 1079; 675 const int kIdealHeight = 1079;
579 constraint_factory_.basic().height.setIdeal(kIdealHeight); 676 constraint_factory_.basic().height.setIdeal(kIdealHeight);
580 auto result = SelectSettings(); 677 auto result = SelectSettings();
581 EXPECT_TRUE(result.HasValue()); 678 EXPECT_TRUE(result.HasValue());
582 // In this case, the high-res device has two configurations that satisfy 679 // In this case, the high-res device has two configurations that satisfy
583 // the ideal value (1920x1080 and 2304x1536). Select the one with shortest 680 // the ideal value (1920x1080 and 2304x1536). Select the one with shortest
584 // native distance to the ideal value (1920x1080). 681 // native distance to the ideal value (1920x1080).
585 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 682 EXPECT_EQ(high_res_device_->device_id, result.device_id());
586 EXPECT_EQ(1920, result.Width()); 683 EXPECT_EQ(1920, result.Width());
587 EXPECT_EQ(1080, result.Height()); 684 EXPECT_EQ(1080, result.Height());
685 EXPECT_EQ(kIdealHeight, result.track_adapter_settings().max_height);
686 EXPECT_EQ(std::round(kIdealHeight * AspectRatio(result.Format())),
687 result.track_adapter_settings().max_width);
688 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
689 EXPECT_EQ(1.0 / result.Height(),
690 result.track_adapter_settings().min_aspect_ratio);
691 CheckTrackAdapterSettingsEqualsFrameRate(result);
588 } 692 }
589 693
590 { 694 {
591 const int kIdealHeight = 1200; 695 const int kIdealHeight = 1200;
592 constraint_factory_.basic().height.setIdeal(kIdealHeight); 696 constraint_factory_.basic().height.setIdeal(kIdealHeight);
593 auto result = SelectSettings(); 697 auto result = SelectSettings();
594 EXPECT_TRUE(result.HasValue()); 698 EXPECT_TRUE(result.HasValue());
595 // The algorithm must the select the only device that can satisfy the ideal, 699 // The algorithm must the select the only device that can satisfy the ideal,
596 // which is the high-res device at the highest resolution. 700 // which is the high-res device at the highest resolution.
597 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 701 EXPECT_EQ(high_res_device_->device_id, result.device_id());
598 EXPECT_EQ(*high_res_highest_format_, result.Format()); 702 EXPECT_EQ(*high_res_highest_format_, result.Format());
703 EXPECT_EQ(kIdealHeight, result.track_adapter_settings().max_height);
704 EXPECT_EQ(std::round(kIdealHeight * AspectRatio(result.Format())),
705 result.track_adapter_settings().max_width);
706 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
707 EXPECT_EQ(1.0 / result.Height(),
708 result.track_adapter_settings().min_aspect_ratio);
709 CheckTrackAdapterSettingsEqualsFrameRate(result);
599 } 710 }
600 } 711 }
601 712
602 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryExactWidth) { 713 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryExactWidth) {
603 constraint_factory_.Reset(); 714 constraint_factory_.Reset();
604 const int kWidth = 640; 715 const int kWidth = 640;
605 constraint_factory_.basic().width.setExact(kWidth); 716 constraint_factory_.basic().width.setExact(kWidth);
606 auto result = SelectSettings(); 717 auto result = SelectSettings();
607 EXPECT_TRUE(result.HasValue()); 718 EXPECT_TRUE(result.HasValue());
608 // All devices in |capabilities_| support the requested width. The algorithm 719 // All devices in |capabilities_| support the requested width. The algorithm
609 // should prefer the first device that supports the requested width natively, 720 // should prefer the first device that supports the requested width natively,
610 // which is the low-res device. 721 // which is the low-res device.
611 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 722 EXPECT_EQ(low_res_device_->device_id, result.device_id());
612 EXPECT_EQ(kWidth, result.Width()); 723 EXPECT_EQ(kWidth, result.Width());
724 EXPECT_EQ(std::round(kWidth / AspectRatio(result.Format())),
725 result.track_adapter_settings().max_height);
726 EXPECT_EQ(kWidth, result.track_adapter_settings().max_width);
727 EXPECT_EQ(kWidth, result.track_adapter_settings().max_aspect_ratio);
728 EXPECT_EQ(static_cast<double>(kWidth) / result.Height(),
729 result.track_adapter_settings().min_aspect_ratio);
730 CheckTrackAdapterSettingsEqualsFrameRate(result);
613 731
614 const int kLargeWidth = 2000; 732 const int kLargeWidth = 2000;
615 constraint_factory_.basic().width.setExact(kLargeWidth); 733 constraint_factory_.basic().width.setExact(kLargeWidth);
616 result = SelectSettings(); 734 result = SelectSettings();
617 EXPECT_TRUE(result.HasValue()); 735 EXPECT_TRUE(result.HasValue());
618 EXPECT_LE(kLargeWidth, result.Width()); 736 EXPECT_LE(kLargeWidth, result.Width());
619 // Only the high-res device at the highest resolution supports the requested 737 // Only the high-res device at the highest resolution supports the requested
620 // width, even if not natively. 738 // width, even if not natively.
621 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 739 EXPECT_EQ(high_res_device_->device_id, result.device_id());
622 EXPECT_EQ(*high_res_highest_format_, result.Format()); 740 EXPECT_EQ(*high_res_highest_format_, result.Format());
741 EXPECT_EQ(std::round(kLargeWidth / AspectRatio(result.Format())),
742 result.track_adapter_settings().max_height);
743 EXPECT_EQ(kLargeWidth, result.track_adapter_settings().max_width);
744 EXPECT_EQ(kLargeWidth, result.track_adapter_settings().max_aspect_ratio);
745 EXPECT_EQ(static_cast<double>(kLargeWidth) / result.Height(),
746 result.track_adapter_settings().min_aspect_ratio);
747 CheckTrackAdapterSettingsEqualsFrameRate(result);
623 } 748 }
624 749
625 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMinWidth) { 750 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMinWidth) {
626 constraint_factory_.Reset(); 751 constraint_factory_.Reset();
627 const int kWidth = 640; 752 const int kWidth = 640;
628 constraint_factory_.basic().width.setMin(kWidth); 753 constraint_factory_.basic().width.setMin(kWidth);
629 auto result = SelectSettings(); 754 auto result = SelectSettings();
630 EXPECT_TRUE(result.HasValue()); 755 EXPECT_TRUE(result.HasValue());
631 // All devices in |capabilities_| support the requested width range. The 756 // All devices in |capabilities_| support the requested width range. The
632 // algorithm should prefer the default device at 1000x1000, which is the 757 // algorithm should prefer the default device at 1000x1000, which is the
633 // first configuration that satisfies the minimum width. 758 // first configuration that satisfies the minimum width.
634 EXPECT_EQ(default_device_->device_id, result.device_id()); 759 EXPECT_EQ(default_device_->device_id, result.device_id());
635 EXPECT_LE(kWidth, result.Width()); 760 EXPECT_LE(kWidth, result.Width());
636 EXPECT_EQ(1000, result.Width()); 761 EXPECT_EQ(1000, result.Width());
637 EXPECT_EQ(1000, result.Height()); 762 EXPECT_EQ(1000, result.Height());
763 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
764 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
765 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
766 EXPECT_EQ(static_cast<double>(kWidth) / result.Height(),
767 result.track_adapter_settings().min_aspect_ratio);
768 CheckTrackAdapterSettingsEqualsFrameRate(result);
638 769
639 const int kLargeWidth = 2000; 770 const int kLargeWidth = 2000;
640 constraint_factory_.basic().width.setMin(kLargeWidth); 771 constraint_factory_.basic().width.setMin(kLargeWidth);
641 result = SelectSettings(); 772 result = SelectSettings();
642 EXPECT_TRUE(result.HasValue()); 773 EXPECT_TRUE(result.HasValue());
643 // Only the high-res device at the highest resolution supports the requested 774 // Only the high-res device at the highest resolution supports the requested
644 // minimum width. 775 // minimum width.
645 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 776 EXPECT_EQ(high_res_device_->device_id, result.device_id());
646 EXPECT_LE(kLargeWidth, result.Width()); 777 EXPECT_LE(kLargeWidth, result.Width());
647 EXPECT_EQ(*high_res_highest_format_, result.Format()); 778 EXPECT_EQ(*high_res_highest_format_, result.Format());
779 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
780 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
781 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
782 EXPECT_EQ(static_cast<double>(kLargeWidth) / result.Height(),
783 result.track_adapter_settings().min_aspect_ratio);
784 CheckTrackAdapterSettingsEqualsFrameRate(result);
648 } 785 }
649 786
650 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMaxWidth) { 787 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMaxWidth) {
651 constraint_factory_.Reset(); 788 constraint_factory_.Reset();
652 const int kLowWidth = 30; 789 const int kLowWidth = 30;
653 constraint_factory_.basic().width.setMax(kLowWidth); 790 constraint_factory_.basic().width.setMax(kLowWidth);
654 auto result = SelectSettings(); 791 auto result = SelectSettings();
655 EXPECT_TRUE(result.HasValue()); 792 EXPECT_TRUE(result.HasValue());
656 // All devices in |capabilities_| support the requested width range. The 793 // All devices in |capabilities_| support the requested width range. The
657 // algorithm should prefer the settings that natively exceed the requested 794 // algorithm should prefer the settings that natively exceed the requested
658 // maximum by the lowest amount. In this case it is the low-res device at its 795 // maximum by the lowest amount. In this case it is the low-res device at its
659 // lowest resolution. 796 // lowest resolution.
660 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 797 EXPECT_EQ(low_res_device_->device_id, result.device_id());
661 EXPECT_EQ(low_res_device_->formats[0], result.Format()); 798 EXPECT_EQ(low_res_device_->formats[0], result.Format());
799 // The track is cropped to kLowWidth and keeps the source aspect ratio.
800 EXPECT_EQ(std::round(kLowWidth / AspectRatio(result.Format())),
801 result.track_adapter_settings().max_height);
802 EXPECT_EQ(kLowWidth, result.track_adapter_settings().max_width);
803 EXPECT_EQ(kLowWidth, result.track_adapter_settings().max_aspect_ratio);
804 EXPECT_EQ(1.0 / result.Height(),
805 result.track_adapter_settings().min_aspect_ratio);
806 CheckTrackAdapterSettingsEqualsFrameRate(result);
662 } 807 }
663 808
664 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryWidthRange) { 809 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryWidthRange) {
665 constraint_factory_.Reset(); 810 constraint_factory_.Reset();
666 { 811 {
667 const int kMinWidth = 640; 812 const int kMinWidth = 640;
668 const int kMaxWidth = 1280; 813 const int kMaxWidth = 1280;
669 constraint_factory_.basic().width.setMin(kMinWidth); 814 constraint_factory_.basic().width.setMin(kMinWidth);
670 constraint_factory_.basic().width.setMax(kMaxWidth); 815 constraint_factory_.basic().width.setMax(kMaxWidth);
671 auto result = SelectSettings(); 816 auto result = SelectSettings();
672 EXPECT_TRUE(result.HasValue()); 817 EXPECT_TRUE(result.HasValue());
673 EXPECT_GE(result.Width(), kMinWidth); 818 EXPECT_GE(result.Width(), kMinWidth);
674 EXPECT_LE(result.Width(), kMaxWidth); 819 EXPECT_LE(result.Width(), kMaxWidth);
675 // All devices in |capabilities_| support the constraint range. The 820 // All devices in |capabilities_| support the constraint range. The
676 // algorithm should prefer the default device since it has at least one 821 // algorithm should prefer the default device since it has at least one
677 // native format (1000x1000) included in the requested range. 822 // native format (1000x1000) included in the requested range.
678 EXPECT_EQ(default_device_->device_id, result.device_id()); 823 EXPECT_EQ(default_device_->device_id, result.device_id());
679 EXPECT_EQ(1000, result.Width()); 824 EXPECT_EQ(1000, result.Width());
680 EXPECT_EQ(1000, result.Height()); 825 EXPECT_EQ(1000, result.Height());
826 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
827 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
828 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
829 EXPECT_EQ(static_cast<double>(kMinWidth) / result.Height(),
830 result.track_adapter_settings().min_aspect_ratio);
831 CheckTrackAdapterSettingsEqualsFrameRate(result);
681 } 832 }
682 833
683 { 834 {
684 const int kMinWidth = 750; 835 const int kMinWidth = 750;
685 const int kMaxWidth = 850; 836 const int kMaxWidth = 850;
686 constraint_factory_.basic().width.setMin(kMinWidth); 837 constraint_factory_.basic().width.setMin(kMinWidth);
687 constraint_factory_.basic().width.setMax(kMaxWidth); 838 constraint_factory_.basic().width.setMax(kMaxWidth);
688 auto result = SelectSettings(); 839 auto result = SelectSettings();
689 EXPECT_TRUE(result.HasValue()); 840 EXPECT_TRUE(result.HasValue());
690 EXPECT_GE(result.Width(), kMinWidth); 841 EXPECT_GE(result.Width(), kMinWidth);
691 EXPECT_LE(result.Width(), kMaxWidth); 842 EXPECT_LE(result.Width(), kMaxWidth);
692 // In this case, the algorithm should prefer the low-res device since it is 843 // In this case, the algorithm should prefer the low-res device since it is
693 // the first device with a native format (800x600) included in the requested 844 // the first device with a native format (800x600) included in the requested
694 // range. 845 // range.
695 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 846 EXPECT_EQ(low_res_device_->device_id, result.device_id());
696 EXPECT_EQ(800, result.Width()); 847 EXPECT_EQ(800, result.Width());
697 EXPECT_EQ(600, result.Height()); 848 EXPECT_EQ(600, result.Height());
849 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
850 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
851 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
852 EXPECT_EQ(static_cast<double>(kMinWidth) / result.Height(),
853 result.track_adapter_settings().min_aspect_ratio);
854 CheckTrackAdapterSettingsEqualsFrameRate(result);
698 } 855 }
699 856
700 { 857 {
701 const int kMinWidth = 1900; 858 const int kMinWidth = 1900;
702 const int kMaxWidth = 2000; 859 const int kMaxWidth = 2000;
703 constraint_factory_.basic().width.setMin(kMinWidth); 860 constraint_factory_.basic().width.setMin(kMinWidth);
704 constraint_factory_.basic().width.setMax(kMaxWidth); 861 constraint_factory_.basic().width.setMax(kMaxWidth);
705 auto result = SelectSettings(); 862 auto result = SelectSettings();
706 EXPECT_TRUE(result.HasValue()); 863 EXPECT_TRUE(result.HasValue());
707 EXPECT_GE(result.Width(), kMinWidth); 864 EXPECT_GE(result.Width(), kMinWidth);
708 EXPECT_LE(result.Width(), kMaxWidth); 865 EXPECT_LE(result.Width(), kMaxWidth);
709 // In this case, the algorithm should prefer the high-res device since it is 866 // In this case, the algorithm should prefer the high-res device since it is
710 // the only device with a native format (1920x1080) included in the 867 // the only device with a native format (1920x1080) included in the
711 // requested range. 868 // requested range.
712 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 869 EXPECT_EQ(high_res_device_->device_id, result.device_id());
713 EXPECT_EQ(1920, result.Width()); 870 EXPECT_EQ(1920, result.Width());
714 EXPECT_EQ(1080, result.Height()); 871 EXPECT_EQ(1080, result.Height());
872 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
873 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
874 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
875 EXPECT_EQ(static_cast<double>(kMinWidth) / result.Height(),
876 result.track_adapter_settings().min_aspect_ratio);
877 CheckTrackAdapterSettingsEqualsFrameRate(result);
715 } 878 }
716 } 879 }
717 880
718 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, IdealWidth) { 881 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, IdealWidth) {
719 constraint_factory_.Reset(); 882 constraint_factory_.Reset();
720 { 883 {
721 const int kIdealWidth = 320; 884 const int kIdealWidth = 320;
722 constraint_factory_.basic().width.setIdeal(kIdealWidth); 885 constraint_factory_.basic().width.setIdeal(kIdealWidth);
723 auto result = SelectSettings(); 886 auto result = SelectSettings();
724 EXPECT_TRUE(result.HasValue()); 887 EXPECT_TRUE(result.HasValue());
725 // The algorithm should select the first device that supports the ideal 888 // The algorithm should select the first device that supports the ideal
726 // width natively, which is the low-res device at 320x240. 889 // width natively, which is the low-res device at 320x240.
727 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 890 EXPECT_EQ(low_res_device_->device_id, result.device_id());
728 EXPECT_EQ(kIdealWidth, result.Width()); 891 EXPECT_EQ(kIdealWidth, result.Width());
892 EXPECT_EQ(std::round(kIdealWidth / AspectRatio(result.Format())),
893 result.track_adapter_settings().max_height);
894 EXPECT_EQ(kIdealWidth, result.track_adapter_settings().max_width);
895 EXPECT_EQ(kIdealWidth, result.track_adapter_settings().max_aspect_ratio);
896 EXPECT_EQ(1.0 / result.Height(),
897 result.track_adapter_settings().min_aspect_ratio);
898 CheckTrackAdapterSettingsEqualsFrameRate(result);
729 } 899 }
730 900
731 { 901 {
732 const int kIdealWidth = 321; 902 const int kIdealWidth = 321;
733 constraint_factory_.basic().width.setIdeal(kIdealWidth); 903 constraint_factory_.basic().width.setIdeal(kIdealWidth);
734 auto result = SelectSettings(); 904 auto result = SelectSettings();
735 EXPECT_TRUE(result.HasValue()); 905 EXPECT_TRUE(result.HasValue());
736 // In this case, the default device is selected because it can satisfy the 906 // In this case, the default device is selected because it can satisfy the
737 // ideal at a lower cost than the other devices (500 vs 640). 907 // ideal at a lower cost than the other devices (500 vs 640).
738 // Note that a native resolution of 320 is further from the ideal value of 908 // Note that a native resolution of 320 is further from the ideal value of
739 // 321 than 500 cropped to 321. 909 // 321 than 500 cropped to 321.
740 EXPECT_EQ(default_device_->device_id, result.device_id()); 910 EXPECT_EQ(default_device_->device_id, result.device_id());
741 EXPECT_EQ(*default_closest_format_, result.Format()); 911 EXPECT_EQ(*default_closest_format_, result.Format());
912 // The track is cropped to kIdealWidth and keeps the source aspect ratio.
913 EXPECT_EQ(std::round(kIdealWidth / AspectRatio(result.Format())),
914 result.track_adapter_settings().max_height);
915 EXPECT_EQ(kIdealWidth, result.track_adapter_settings().max_width);
916 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
917 EXPECT_EQ(1.0 / result.Height(),
918 result.track_adapter_settings().min_aspect_ratio);
919 CheckTrackAdapterSettingsEqualsFrameRate(result);
742 } 920 }
743 921
744 { 922 {
745 const int kIdealWidth = 2000; 923 const int kIdealWidth = 2000;
746 constraint_factory_.basic().width.setIdeal(kIdealWidth); 924 constraint_factory_.basic().width.setIdeal(kIdealWidth);
747 auto result = SelectSettings(); 925 auto result = SelectSettings();
748 EXPECT_TRUE(result.HasValue()); 926 EXPECT_TRUE(result.HasValue());
749 // The algorithm must the select the only device that can satisfy the ideal. 927 // The algorithm must the select the only device that can satisfy the ideal.
750 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 928 EXPECT_EQ(high_res_device_->device_id, result.device_id());
751 EXPECT_EQ(*high_res_highest_format_, result.Format()); 929 EXPECT_EQ(*high_res_highest_format_, result.Format());
930 // The track is cropped to kIdealWidth and keeps the source aspect ratio.
931 EXPECT_EQ(std::round(kIdealWidth / AspectRatio(result.Format())),
932 result.track_adapter_settings().max_height);
933 EXPECT_EQ(kIdealWidth, result.track_adapter_settings().max_width);
934 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
935 EXPECT_EQ(1.0 / result.Height(),
936 result.track_adapter_settings().min_aspect_ratio);
937 CheckTrackAdapterSettingsEqualsFrameRate(result);
752 } 938 }
753 939
754 { 940 {
755 const int kIdealWidth = 3000; 941 const int kIdealWidth = 3000;
756 constraint_factory_.basic().width.setIdeal(kIdealWidth); 942 constraint_factory_.basic().width.setIdeal(kIdealWidth);
757 auto result = SelectSettings(); 943 auto result = SelectSettings();
758 EXPECT_TRUE(result.HasValue()); 944 EXPECT_TRUE(result.HasValue());
759 // The algorithm must the select the device and setting with less distance 945 // The algorithm must the select the device and setting with less distance
760 // to the ideal. 946 // to the ideal.
761 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 947 EXPECT_EQ(high_res_device_->device_id, result.device_id());
762 EXPECT_EQ(*high_res_highest_format_, result.Format()); 948 EXPECT_EQ(*high_res_highest_format_, result.Format());
949 CheckTrackAdapterSettingsEqualsFormat(result);
763 } 950 }
764 } 951 }
765 952
766 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryExactFrameRate) { 953 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryExactFrameRate) {
767 constraint_factory_.Reset(); 954 constraint_factory_.Reset();
768 const double kFrameRate = MediaStreamVideoSource::kDefaultFrameRate; 955 const double kFrameRate = MediaStreamVideoSource::kDefaultFrameRate;
769 constraint_factory_.basic().frameRate.setExact(kFrameRate); 956 constraint_factory_.basic().frameRate.setExact(kFrameRate);
770 auto result = SelectSettings(); 957 auto result = SelectSettings();
771 EXPECT_TRUE(result.HasValue()); 958 EXPECT_TRUE(result.HasValue());
772 // All devices in |capabilities_| support the requested frame rate. The 959 // All devices in |capabilities_| support the requested frame rate. The
773 // algorithm should prefer the first device that supports the requested frame 960 // algorithm should prefer the first device that supports the requested frame
774 // rate natively, which is the low-res device at 640x480x30Hz. 961 // rate natively, which is the low-res device at 640x480x30Hz.
775 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 962 EXPECT_EQ(low_res_device_->device_id, result.device_id());
776 EXPECT_EQ(kFrameRate, result.FrameRate()); 963 EXPECT_EQ(kFrameRate, result.FrameRate());
777 EXPECT_EQ(640, result.Width()); 964 EXPECT_EQ(640, result.Width());
778 EXPECT_EQ(480, result.Height()); 965 EXPECT_EQ(480, result.Height());
966 CheckTrackAdapterSettingsEqualsResolution(result);
967 CheckTrackAdapterSettingsEqualsFrameRate(result, kFrameRate);
779 968
780 const double kLargeFrameRate = 50; 969 const double kLargeFrameRate = 50;
781 constraint_factory_.basic().frameRate.setExact(kLargeFrameRate); 970 constraint_factory_.basic().frameRate.setExact(kLargeFrameRate);
782 result = SelectSettings(); 971 result = SelectSettings();
783 EXPECT_TRUE(result.HasValue()); 972 EXPECT_TRUE(result.HasValue());
784 // Only the high-res device supports the requested frame rate, even if not 973 // Only the high-res device supports the requested frame rate, even if not
785 // natively. The least expensive configuration that supports the requested 974 // natively. The least expensive configuration that supports the requested
786 // frame rate is 1280x720x60Hz. 975 // frame rate is 1280x720x60Hz.
787 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 976 EXPECT_EQ(high_res_device_->device_id, result.device_id());
788 EXPECT_EQ(60.0, result.FrameRate()); 977 EXPECT_EQ(60.0, result.FrameRate());
789 EXPECT_EQ(1280, result.Width()); 978 EXPECT_EQ(1280, result.Width());
790 EXPECT_EQ(720, result.Height()); 979 EXPECT_EQ(720, result.Height());
980 CheckTrackAdapterSettingsEqualsResolution(result);
981 CheckTrackAdapterSettingsEqualsFrameRate(result, kLargeFrameRate);
791 } 982 }
792 983
793 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMinFrameRate) { 984 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMinFrameRate) {
794 constraint_factory_.Reset(); 985 constraint_factory_.Reset();
795 const double kFrameRate = MediaStreamVideoSource::kDefaultFrameRate; 986 const double kFrameRate = MediaStreamVideoSource::kDefaultFrameRate;
796 constraint_factory_.basic().frameRate.setMin(kFrameRate); 987 constraint_factory_.basic().frameRate.setMin(kFrameRate);
797 auto result = SelectSettings(); 988 auto result = SelectSettings();
798 EXPECT_TRUE(result.HasValue()); 989 EXPECT_TRUE(result.HasValue());
799 // All devices in |capabilities_| support the requested frame-rate range. The 990 // All devices in |capabilities_| support the requested frame-rate range. The
800 // algorithm should prefer the default device. 991 // algorithm should prefer the default device.
801 EXPECT_EQ(default_device_->device_id, result.device_id()); 992 EXPECT_EQ(default_device_->device_id, result.device_id());
802 // The format closest to the default satisfies the constraint. 993 // The format closest to the default satisfies the constraint.
803 EXPECT_EQ(*default_closest_format_, result.Format()); 994 EXPECT_EQ(*default_closest_format_, result.Format());
995 CheckTrackAdapterSettingsEqualsFormat(result);
804 996
805 const double kLargeFrameRate = 50; 997 const double kLargeFrameRate = 50;
806 constraint_factory_.basic().frameRate.setMin(kLargeFrameRate); 998 constraint_factory_.basic().frameRate.setMin(kLargeFrameRate);
807 result = SelectSettings(); 999 result = SelectSettings();
808 EXPECT_TRUE(result.HasValue()); 1000 EXPECT_TRUE(result.HasValue());
809 // Only the high-res device supports the requested frame-rate range. 1001 // Only the high-res device supports the requested frame-rate range.
810 // The least expensive configuration is 1280x720x60Hz. 1002 // The least expensive configuration is 1280x720x60Hz.
811 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1003 EXPECT_EQ(high_res_device_->device_id, result.device_id());
812 EXPECT_LE(kLargeFrameRate, result.FrameRate()); 1004 EXPECT_LE(kLargeFrameRate, result.FrameRate());
813 EXPECT_EQ(1280, result.Width()); 1005 EXPECT_EQ(1280, result.Width());
814 EXPECT_EQ(720, result.Height()); 1006 EXPECT_EQ(720, result.Height());
1007 CheckTrackAdapterSettingsEqualsFormat(result);
815 } 1008 }
816 1009
817 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMaxFrameRate) { 1010 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMaxFrameRate) {
818 constraint_factory_.Reset(); 1011 constraint_factory_.Reset();
819 const double kLowFrameRate = 10; 1012 const double kLowFrameRate = 10;
820 constraint_factory_.basic().frameRate.setMax(kLowFrameRate); 1013 constraint_factory_.basic().frameRate.setMax(kLowFrameRate);
821 auto result = SelectSettings(); 1014 auto result = SelectSettings();
822 EXPECT_TRUE(result.HasValue()); 1015 EXPECT_TRUE(result.HasValue());
823 // All devices in |capabilities_| support the requested frame-rate range. The 1016 // All devices in |capabilities_| support the requested frame-rate range. The
824 // algorithm should prefer the settings that natively exceed the requested 1017 // algorithm should prefer the settings that natively exceed the requested
825 // maximum by the lowest amount. In this case it is the high-res device with 1018 // maximum by the lowest amount. In this case it is the high-res device with
826 // default resolution . 1019 // default resolution .
827 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1020 EXPECT_EQ(high_res_device_->device_id, result.device_id());
828 EXPECT_EQ(kLowFrameRate, result.FrameRate()); 1021 EXPECT_EQ(kLowFrameRate, result.FrameRate());
829 EXPECT_EQ(MediaStreamVideoSource::kDefaultHeight, result.Height()); 1022 EXPECT_EQ(MediaStreamVideoSource::kDefaultHeight, result.Height());
830 EXPECT_EQ(MediaStreamVideoSource::kDefaultWidth, result.Width()); 1023 EXPECT_EQ(MediaStreamVideoSource::kDefaultWidth, result.Width());
1024 CheckTrackAdapterSettingsEqualsResolution(result);
1025 CheckTrackAdapterSettingsEqualsFrameRate(result, kLowFrameRate);
831 } 1026 }
832 1027
833 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryFrameRateRange) { 1028 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryFrameRateRange) {
834 constraint_factory_.Reset(); 1029 constraint_factory_.Reset();
835 { 1030 {
836 const double kMinFrameRate = 10; 1031 const double kMinFrameRate = 10;
837 const double kMaxFrameRate = 40; 1032 const double kMaxFrameRate = 40;
838 constraint_factory_.basic().frameRate.setMin(kMinFrameRate); 1033 constraint_factory_.basic().frameRate.setMin(kMinFrameRate);
839 constraint_factory_.basic().frameRate.setMax(kMaxFrameRate); 1034 constraint_factory_.basic().frameRate.setMax(kMaxFrameRate);
840 auto result = SelectSettings(); 1035 auto result = SelectSettings();
841 EXPECT_TRUE(result.HasValue()); 1036 EXPECT_TRUE(result.HasValue());
842 EXPECT_LE(kMinFrameRate, result.FrameRate()); 1037 EXPECT_LE(kMinFrameRate, result.FrameRate());
843 EXPECT_GE(kMaxFrameRate, result.FrameRate()); 1038 EXPECT_GE(kMaxFrameRate, result.FrameRate());
844 // All devices in |capabilities_| support the constraint range. The 1039 // All devices in |capabilities_| support the constraint range. The
845 // algorithm should prefer the default device since its closest-to-default 1040 // algorithm should prefer the default device since its closest-to-default
846 // format has a frame rate included in the requested range. 1041 // format has a frame rate included in the requested range.
847 EXPECT_EQ(default_device_->device_id, result.device_id()); 1042 EXPECT_EQ(default_device_->device_id, result.device_id());
848 EXPECT_EQ(*default_closest_format_, result.Format()); 1043 EXPECT_EQ(*default_closest_format_, result.Format());
1044 CheckTrackAdapterSettingsEqualsFormat(result);
849 } 1045 }
850 1046
851 { 1047 {
852 const double kMinFrameRate = 25; 1048 const double kMinFrameRate = 25;
853 const double kMaxFrameRate = 35; 1049 const double kMaxFrameRate = 35;
854 constraint_factory_.basic().frameRate.setMin(kMinFrameRate); 1050 constraint_factory_.basic().frameRate.setMin(kMinFrameRate);
855 constraint_factory_.basic().frameRate.setMax(kMaxFrameRate); 1051 constraint_factory_.basic().frameRate.setMax(kMaxFrameRate);
856 auto result = SelectSettings(); 1052 auto result = SelectSettings();
857 EXPECT_TRUE(result.HasValue()); 1053 EXPECT_TRUE(result.HasValue());
858 EXPECT_GE(result.FrameRate(), kMinFrameRate); 1054 EXPECT_GE(result.FrameRate(), kMinFrameRate);
859 EXPECT_LE(result.FrameRate(), kMaxFrameRate); 1055 EXPECT_LE(result.FrameRate(), kMaxFrameRate);
860 // In this case, the algorithm should prefer the low-res device since it is 1056 // In this case, the algorithm should prefer the low-res device since it is
861 // the first device with a native frame rate included in the requested 1057 // the first device with a native frame rate included in the requested
862 // range. The default resolution should be preferred as secondary criterion. 1058 // range. The default resolution should be preferred as secondary criterion.
863 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 1059 EXPECT_EQ(low_res_device_->device_id, result.device_id());
864 EXPECT_EQ(*low_res_closest_format_, result.Format()); 1060 EXPECT_EQ(*low_res_closest_format_, result.Format());
1061 CheckTrackAdapterSettingsEqualsFormat(result);
865 } 1062 }
866 1063
867 { 1064 {
868 const double kMinFrameRate = 50; 1065 const double kMinFrameRate = 50;
869 const double kMaxFrameRate = 70; 1066 const double kMaxFrameRate = 70;
870 constraint_factory_.basic().frameRate.setMin(kMinFrameRate); 1067 constraint_factory_.basic().frameRate.setMin(kMinFrameRate);
871 constraint_factory_.basic().frameRate.setMax(kMaxFrameRate); 1068 constraint_factory_.basic().frameRate.setMax(kMaxFrameRate);
872 auto result = SelectSettings(); 1069 auto result = SelectSettings();
873 EXPECT_TRUE(result.HasValue()); 1070 EXPECT_TRUE(result.HasValue());
874 EXPECT_GE(result.FrameRate(), kMinFrameRate); 1071 EXPECT_GE(result.FrameRate(), kMinFrameRate);
875 EXPECT_LE(result.FrameRate(), kMaxFrameRate); 1072 EXPECT_LE(result.FrameRate(), kMaxFrameRate);
876 // In this case, the algorithm should prefer the high-res device since it is 1073 // In this case, the algorithm should prefer the high-res device since it is
877 // the only device with a native format included in the requested range. 1074 // the only device with a native format included in the requested range.
878 // The 1280x720 resolution should be selected due to closeness to default 1075 // The 1280x720 resolution should be selected due to closeness to default
879 // settings, which is the second tie-breaker criterion that applies. 1076 // settings, which is the second tie-breaker criterion that applies.
880 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1077 EXPECT_EQ(high_res_device_->device_id, result.device_id());
881 EXPECT_EQ(1280, result.Width()); 1078 EXPECT_EQ(1280, result.Width());
882 EXPECT_EQ(720, result.Height()); 1079 EXPECT_EQ(720, result.Height());
1080 CheckTrackAdapterSettingsEqualsFormat(result);
883 } 1081 }
884 } 1082 }
885 1083
886 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, IdealFrameRate) { 1084 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, IdealFrameRate) {
887 constraint_factory_.Reset(); 1085 constraint_factory_.Reset();
888 { 1086 {
889 const double kIdealFrameRate = MediaStreamVideoSource::kDefaultFrameRate; 1087 const double kIdealFrameRate = MediaStreamVideoSource::kDefaultFrameRate;
890 constraint_factory_.basic().frameRate.setIdeal(kIdealFrameRate); 1088 constraint_factory_.basic().frameRate.setIdeal(kIdealFrameRate);
891 auto result = SelectSettings(); 1089 auto result = SelectSettings();
892 EXPECT_TRUE(result.HasValue()); 1090 EXPECT_TRUE(result.HasValue());
893 // The algorithm should select the first configuration that supports the 1091 // The algorithm should select the first configuration that supports the
894 // ideal frame rate natively, which is the low-res device. Default 1092 // ideal frame rate natively, which is the low-res device. Default
895 // resolution should be selected as secondary criterion. 1093 // resolution should be selected as secondary criterion.
896 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 1094 EXPECT_EQ(low_res_device_->device_id, result.device_id());
897 EXPECT_EQ(*low_res_closest_format_, result.Format()); 1095 EXPECT_EQ(*low_res_closest_format_, result.Format());
1096 CheckTrackAdapterSettingsEqualsResolution(result);
1097 CheckTrackAdapterSettingsEqualsFrameRate(result, kIdealFrameRate);
898 } 1098 }
899 1099
900 { 1100 {
901 const double kIdealFrameRate = 31; 1101 const double kIdealFrameRate = 31;
902 constraint_factory_.basic().frameRate.setIdeal(kIdealFrameRate); 1102 constraint_factory_.basic().frameRate.setIdeal(kIdealFrameRate);
903 auto result = SelectSettings(); 1103 auto result = SelectSettings();
904 EXPECT_TRUE(result.HasValue()); 1104 EXPECT_TRUE(result.HasValue());
905 // In this case, the default device is selected because it can satisfy the 1105 // In this case, the default device is selected because it can satisfy the
906 // ideal at a lower cost than the other devices (40 vs 60). 1106 // ideal at a lower cost than the other devices (40 vs 60).
907 // Note that a native frame rate of 30 is further from the ideal than 1107 // Note that a native frame rate of 30 is further from the ideal than
908 // 31 adjusted to 30. 1108 // 31 adjusted to 30.
909 EXPECT_EQ(default_device_->device_id, result.device_id()); 1109 EXPECT_EQ(default_device_->device_id, result.device_id());
910 EXPECT_EQ(*default_closest_format_, result.Format()); 1110 EXPECT_EQ(*default_closest_format_, result.Format());
1111 CheckTrackAdapterSettingsEqualsResolution(result);
1112 CheckTrackAdapterSettingsEqualsFrameRate(result, kIdealFrameRate);
911 } 1113 }
912 1114
913 { 1115 {
914 const double kIdealFrameRate = 55; 1116 const double kIdealFrameRate = 55;
915 constraint_factory_.basic().frameRate.setIdeal(kIdealFrameRate); 1117 constraint_factory_.basic().frameRate.setIdeal(kIdealFrameRate);
916 auto result = SelectSettings(); 1118 auto result = SelectSettings();
917 EXPECT_TRUE(result.HasValue()); 1119 EXPECT_TRUE(result.HasValue());
918 // The high-res device format 1280x720x60.0 must be selected because its 1120 // The high-res device format 1280x720x60.0 must be selected because its
919 // frame rate can satisfy the ideal frame rate and has resolution closest 1121 // frame rate can satisfy the ideal frame rate and has resolution closest
920 // to the default. 1122 // to the default.
921 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1123 EXPECT_EQ(high_res_device_->device_id, result.device_id());
922 EXPECT_EQ(1280, result.Width()); 1124 EXPECT_EQ(1280, result.Width());
923 EXPECT_EQ(720, result.Height()); 1125 EXPECT_EQ(720, result.Height());
924 EXPECT_EQ(60, result.FrameRate()); 1126 EXPECT_EQ(60, result.FrameRate());
1127 CheckTrackAdapterSettingsEqualsResolution(result);
1128 CheckTrackAdapterSettingsEqualsFrameRate(result, kIdealFrameRate);
925 } 1129 }
926 1130
927 { 1131 {
928 const double kIdealFrameRate = 100; 1132 const double kIdealFrameRate = 100;
929 constraint_factory_.basic().frameRate.setIdeal(kIdealFrameRate); 1133 constraint_factory_.basic().frameRate.setIdeal(kIdealFrameRate);
930 auto result = SelectSettings(); 1134 auto result = SelectSettings();
931 EXPECT_TRUE(result.HasValue()); 1135 EXPECT_TRUE(result.HasValue());
932 // The algorithm must select settings with frame rate closest to the ideal. 1136 // The algorithm must select settings with frame rate closest to the ideal.
933 // The high-res device format 1280x720x60.0 must be selected because its 1137 // The high-res device format 1280x720x60.0 must be selected because its
934 // frame rate it closest to the ideal value and it has resolution closest to 1138 // frame rate it closest to the ideal value and it has resolution closest to
935 // the default. 1139 // the default.
936 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1140 EXPECT_EQ(high_res_device_->device_id, result.device_id());
937 EXPECT_EQ(1280, result.Width()); 1141 EXPECT_EQ(1280, result.Width());
938 EXPECT_EQ(720, result.Height()); 1142 EXPECT_EQ(720, result.Height());
939 EXPECT_EQ(60, result.FrameRate()); 1143 EXPECT_EQ(60, result.FrameRate());
1144 CheckTrackAdapterSettingsEqualsFormat(result);
940 } 1145 }
941 } 1146 }
942 1147
943 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryExactAspectRatio) { 1148 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryExactAspectRatio) {
944 constraint_factory_.Reset(); 1149 constraint_factory_.Reset();
945 const double kAspectRatio = 4.0 / 3.0; 1150 const double kAspectRatio = 4.0 / 3.0;
946 constraint_factory_.basic().aspectRatio.setExact(kAspectRatio); 1151 constraint_factory_.basic().aspectRatio.setExact(kAspectRatio);
947 auto result = SelectSettings(); 1152 auto result = SelectSettings();
948 EXPECT_TRUE(result.HasValue()); 1153 EXPECT_TRUE(result.HasValue());
949 double min_width = 1.0; 1154 double min_width = 1.0;
950 double max_width = result.Width(); 1155 double max_width = result.Width();
951 double min_height = 1.0; 1156 double min_height = 1.0;
952 double max_height = result.Height(); 1157 double max_height = result.Height();
953 double min_aspect_ratio = min_width / max_height; 1158 double min_aspect_ratio = min_width / max_height;
954 double max_aspect_ratio = max_width / min_height; 1159 double max_aspect_ratio = max_width / min_height;
955 // The requested aspect ratio must be within the supported range. 1160 // The requested aspect ratio must be within the supported range.
956 EXPECT_GE(kAspectRatio, min_aspect_ratio); 1161 EXPECT_GE(kAspectRatio, min_aspect_ratio);
957 EXPECT_LE(kAspectRatio, max_aspect_ratio); 1162 EXPECT_LE(kAspectRatio, max_aspect_ratio);
958 // All devices in |capabilities_| support the requested aspect ratio. 1163 // All devices in |capabilities_| support the requested aspect ratio.
959 // The algorithm should prefer the first device that supports the requested 1164 // The algorithm should prefer the first device that supports the requested
960 // aspect ratio. 1165 // aspect ratio.
961 EXPECT_EQ(default_device_->device_id, result.device_id()); 1166 EXPECT_EQ(default_device_->device_id, result.device_id());
962 EXPECT_EQ(*default_closest_format_, result.Format()); 1167 EXPECT_EQ(*default_closest_format_, result.Format());
1168 EXPECT_EQ(std::round(result.Width() / kAspectRatio),
1169 result.track_adapter_settings().max_height);
1170 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1171 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().min_aspect_ratio);
1172 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().max_aspect_ratio);
1173 CheckTrackAdapterSettingsEqualsFrameRate(result);
963 1174
964 const int kMinWidth = 500; 1175 const int kMinWidth = 500;
965 const int kMaxWidth = 1000; 1176 const int kMaxWidth = 1000;
966 const int kMaxHeight = 500; 1177 const int kMaxHeight = 500;
967 constraint_factory_.basic().height.setMax(kMaxHeight); 1178 constraint_factory_.basic().height.setMax(kMaxHeight);
968 constraint_factory_.basic().width.setMin(kMinWidth); 1179 constraint_factory_.basic().width.setMin(kMinWidth);
969 constraint_factory_.basic().width.setMax(kMaxWidth); 1180 constraint_factory_.basic().width.setMax(kMaxWidth);
970 constraint_factory_.basic().aspectRatio.setExact(kAspectRatio); 1181 constraint_factory_.basic().aspectRatio.setExact(kAspectRatio);
971 result = SelectSettings(); 1182 result = SelectSettings();
972 EXPECT_TRUE(result.HasValue()); 1183 EXPECT_TRUE(result.HasValue());
973 min_width = std::max(1, kMinWidth); 1184 min_width = std::max(1, kMinWidth);
974 max_width = std::min(result.Width(), kMaxWidth); 1185 max_width = std::min(result.Width(), kMaxWidth);
975 min_height = 1.0; 1186 min_height = 1.0;
976 max_height = std::min(result.Height(), kMaxHeight); 1187 max_height = std::min(result.Height(), kMaxHeight);
977 min_aspect_ratio = min_width / max_height; 1188 min_aspect_ratio = min_width / max_height;
978 max_aspect_ratio = max_width / min_height; 1189 max_aspect_ratio = max_width / min_height;
979 // The requested aspect ratio must be within the supported range. 1190 // The requested aspect ratio must be within the supported range.
980 EXPECT_GE(kAspectRatio, min_aspect_ratio); 1191 EXPECT_GE(kAspectRatio, min_aspect_ratio);
981 EXPECT_LE(kAspectRatio, max_aspect_ratio); 1192 EXPECT_LE(kAspectRatio, max_aspect_ratio);
982 // The default device can support the requested aspect ratio with the default 1193 // The default device can support the requested aspect ratio with the default
983 // settings (500x500) using cropping. 1194 // settings (500x500) using cropping.
984 EXPECT_EQ(default_device_->device_id, result.device_id()); 1195 EXPECT_EQ(default_device_->device_id, result.device_id());
985 EXPECT_EQ(*default_closest_format_, result.Format()); 1196 EXPECT_EQ(*default_closest_format_, result.Format());
1197 EXPECT_EQ(std::round(result.Width() / kAspectRatio),
1198 result.track_adapter_settings().max_height);
1199 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1200 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().min_aspect_ratio);
1201 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().max_aspect_ratio);
1202 CheckTrackAdapterSettingsEqualsFrameRate(result);
986 1203
987 const int kMinHeight = 480; 1204 const int kMinHeight = 480;
988 constraint_factory_.basic().height.setMin(kMinHeight); 1205 constraint_factory_.basic().height.setMin(kMinHeight);
989 constraint_factory_.basic().height.setMax(kMaxHeight); 1206 constraint_factory_.basic().height.setMax(kMaxHeight);
990 constraint_factory_.basic().width.setMin(kMinWidth); 1207 constraint_factory_.basic().width.setMin(kMinWidth);
991 constraint_factory_.basic().width.setMax(kMaxWidth); 1208 constraint_factory_.basic().width.setMax(kMaxWidth);
992 constraint_factory_.basic().aspectRatio.setExact(kAspectRatio); 1209 constraint_factory_.basic().aspectRatio.setExact(kAspectRatio);
993 result = SelectSettings(); 1210 result = SelectSettings();
994 EXPECT_TRUE(result.HasValue()); 1211 EXPECT_TRUE(result.HasValue());
995 min_width = std::max(1, kMinWidth); 1212 min_width = std::max(1, kMinWidth);
996 max_width = std::min(result.Width(), kMaxWidth); 1213 max_width = std::min(result.Width(), kMaxWidth);
997 min_height = std::max(1, kMinHeight); 1214 min_height = std::max(1, kMinHeight);
998 max_height = std::min(result.Height(), kMaxHeight); 1215 max_height = std::min(result.Height(), kMaxHeight);
999 min_aspect_ratio = min_width / max_height; 1216 min_aspect_ratio = min_width / max_height;
1000 max_aspect_ratio = max_width / min_height; 1217 max_aspect_ratio = max_width / min_height;
1001 // The requested aspect ratio must be within the supported range. 1218 // The requested aspect ratio must be within the supported range.
1002 EXPECT_GE(kAspectRatio, min_aspect_ratio); 1219 EXPECT_GE(kAspectRatio, min_aspect_ratio);
1003 EXPECT_LE(kAspectRatio, max_aspect_ratio); 1220 EXPECT_LE(kAspectRatio, max_aspect_ratio);
1004 // Given resolution constraints, the default device with closest-to-default 1221 // Given resolution constraints, the default device with closest-to-default
1005 // settings cannot satisfy the required aspect ratio. 1222 // settings cannot satisfy the required aspect ratio.
1006 // The first device that can do it is the low-res device with a native 1223 // The first device that can do it is the low-res device with a native
1007 // resolution of 640x480. Higher resolutions for the default device are more 1224 // resolution of 640x480. Higher resolutions for the default device are more
1008 // penalized by the constraints than the default native resolution of the 1225 // penalized by the constraints than the default native resolution of the
1009 // low-res device. 1226 // low-res device.
1010 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 1227 EXPECT_EQ(low_res_device_->device_id, result.device_id());
1011 EXPECT_EQ(*low_res_closest_format_, result.Format()); 1228 EXPECT_EQ(*low_res_closest_format_, result.Format());
1229 EXPECT_EQ(std::round(result.Width() / kAspectRatio),
1230 result.track_adapter_settings().max_height);
1231 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1232 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().min_aspect_ratio);
1233 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().max_aspect_ratio);
1234 CheckTrackAdapterSettingsEqualsFrameRate(result);
1012 } 1235 }
1013 1236
1014 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMinAspectRatio) { 1237 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMinAspectRatio) {
1015 constraint_factory_.Reset(); 1238 constraint_factory_.Reset();
1016 const double kAspectRatio = 4.0 / 3.0; 1239 const double kAspectRatio = 4.0 / 3.0;
1017 constraint_factory_.basic().aspectRatio.setMin(kAspectRatio); 1240 constraint_factory_.basic().aspectRatio.setMin(kAspectRatio);
1018 auto result = SelectSettings(); 1241 auto result = SelectSettings();
1019 EXPECT_TRUE(result.HasValue()); 1242 EXPECT_TRUE(result.HasValue());
1020 double max_width = result.Width(); 1243 double max_width = result.Width();
1021 double min_height = 1.0; 1244 double min_height = 1.0;
1022 double max_aspect_ratio = max_width / min_height; 1245 double max_aspect_ratio = max_width / min_height;
1023 // Minimum constraint aspect ratio must be less than or equal to the maximum 1246 // Minimum constraint aspect ratio must be less than or equal to the maximum
1024 // supported by the source. 1247 // supported by the source.
1025 EXPECT_LE(kAspectRatio, max_aspect_ratio); 1248 EXPECT_LE(kAspectRatio, max_aspect_ratio);
1026 // All devices in |capabilities_| support the requested aspect-ratio range. 1249 // All devices in |capabilities_| support the requested aspect-ratio range.
1027 // The algorithm should prefer the first device that supports the requested 1250 // The algorithm should prefer the first device that supports the requested
1028 // aspect-ratio range, which in this case is the default device. 1251 // aspect-ratio range, which in this case is the default device.
1029 EXPECT_EQ(default_device_->device_id, result.device_id()); 1252 EXPECT_EQ(default_device_->device_id, result.device_id());
1030 EXPECT_EQ(*default_closest_format_, result.Format()); 1253 EXPECT_EQ(*default_closest_format_, result.Format());
1254 // Adjust the track resolution to use the minimum aspect ratio, which is
1255 // greater than the source's aspect ratio.
1256 EXPECT_EQ(std::round(result.Width() / kAspectRatio),
1257 result.track_adapter_settings().max_height);
1258 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1259 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().min_aspect_ratio);
1260 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
1261 CheckTrackAdapterSettingsEqualsFrameRate(result);
1031 1262
1032 const int kMinWidth = 500; 1263 const int kMinWidth = 500;
1033 const int kMaxWidth = 1000; 1264 const int kMaxWidth = 1000;
1034 const int kMinHeight = 480; 1265 const int kMinHeight = 480;
1035 const int kMaxHeight = 500; 1266 const int kMaxHeight = 500;
1036 constraint_factory_.basic().width.setMin(kMinWidth); 1267 constraint_factory_.basic().width.setMin(kMinWidth);
1037 constraint_factory_.basic().width.setMax(kMaxWidth); 1268 constraint_factory_.basic().width.setMax(kMaxWidth);
1038 constraint_factory_.basic().height.setMin(kMinHeight); 1269 constraint_factory_.basic().height.setMin(kMinHeight);
1039 constraint_factory_.basic().height.setMax(kMaxHeight); 1270 constraint_factory_.basic().height.setMax(kMaxHeight);
1040 constraint_factory_.basic().aspectRatio.setMin(kAspectRatio); 1271 constraint_factory_.basic().aspectRatio.setMin(kAspectRatio);
1041 result = SelectSettings(); 1272 result = SelectSettings();
1042 EXPECT_TRUE(result.HasValue()); 1273 EXPECT_TRUE(result.HasValue());
1043 max_width = std::min(result.Width(), kMaxWidth); 1274 max_width = std::min(result.Width(), kMaxWidth);
1044 min_height = std::max(1, kMinHeight); 1275 min_height = std::max(1, kMinHeight);
1045 max_aspect_ratio = max_width / min_height; 1276 max_aspect_ratio = max_width / min_height;
1046 // Minimum constraint aspect ratio must be less than or equal to the minimum 1277 // Minimum constraint aspect ratio must be less than or equal to the minimum
1047 // supported by the source. 1278 // supported by the source.
1048 EXPECT_LE(kAspectRatio, max_aspect_ratio); 1279 EXPECT_LE(kAspectRatio, max_aspect_ratio);
1049 // Given resolution constraints, the default device with closest-to-default 1280 // Given resolution constraints, the default device with closest-to-default
1050 // settings cannot satisfy the required minimum aspect ratio (maximum would 1281 // settings cannot satisfy the required minimum aspect ratio (maximum would
1051 // be 500/480). The first device that can is the low-res device with a native 1282 // be 500/480). The first device that can is the low-res device with a native
1052 // resolution of 640x480. 1283 // resolution of 640x480.
1053 // Higher resolutions for the default device are more penalized by the 1284 // Higher resolutions for the default device are more penalized by the
1054 // constraints than the default native resolution of the low-res device. 1285 // constraints than the default native resolution of the low-res device.
1055 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 1286 EXPECT_EQ(low_res_device_->device_id, result.device_id());
1056 EXPECT_EQ(*low_res_closest_format_, result.Format()); 1287 EXPECT_EQ(*low_res_closest_format_, result.Format());
1288 // The source's native aspect ratio equals the minimum aspect ratio.
1289 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1290 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1291 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().min_aspect_ratio);
1292 EXPECT_EQ(max_aspect_ratio, result.track_adapter_settings().max_aspect_ratio);
1293 CheckTrackAdapterSettingsEqualsFrameRate(result);
1057 } 1294 }
1058 1295
1059 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMaxAspectRatio) { 1296 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryMaxAspectRatio) {
1060 constraint_factory_.Reset(); 1297 constraint_factory_.Reset();
1061 const double kAspectRatio = 0.5; 1298 const double kAspectRatio = 0.5;
1062 constraint_factory_.basic().aspectRatio.setMax(kAspectRatio); 1299 constraint_factory_.basic().aspectRatio.setMax(kAspectRatio);
1063 auto result = SelectSettings(); 1300 auto result = SelectSettings();
1064 EXPECT_TRUE(result.HasValue()); 1301 EXPECT_TRUE(result.HasValue());
1065 double min_width = 1.0; 1302 double min_width = 1.0;
1066 double max_height = result.Height(); 1303 double max_height = result.Height();
1067 double min_aspect_ratio = min_width / max_height; 1304 double min_aspect_ratio = min_width / max_height;
1068 // Minimum constraint aspect ratio must be less than or equal to the maximum 1305 // Minimum constraint aspect ratio must be less than or equal to the maximum
1069 // supported by the source. 1306 // supported by the source.
1070 EXPECT_GE(kAspectRatio, min_aspect_ratio); 1307 EXPECT_GE(kAspectRatio, min_aspect_ratio);
1071 // All devices in |capabilities_| support the requested aspect-ratio range. 1308 // All devices in |capabilities_| support the requested aspect-ratio range.
1072 // The algorithm should prefer the first device that supports the requested 1309 // The algorithm should prefer the first device that supports the requested
1073 // aspect-ratio range, which in this case is the default device. 1310 // aspect-ratio range, which in this case is the default device.
1074 EXPECT_EQ(default_device_->device_id, result.device_id()); 1311 EXPECT_EQ(default_device_->device_id, result.device_id());
1075 EXPECT_EQ(*default_closest_format_, result.Format()); 1312 EXPECT_EQ(*default_closest_format_, result.Format());
1313 // The track's aspect ratio is adjusted to the maximum, which is lower than
1314 // the source's native aspect ratio.
1315 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1316 EXPECT_EQ(std::round(result.Height() * kAspectRatio),
1317 result.track_adapter_settings().max_width);
1318 EXPECT_EQ(min_aspect_ratio, result.track_adapter_settings().min_aspect_ratio);
1319 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().max_aspect_ratio);
1320 CheckTrackAdapterSettingsEqualsFrameRate(result);
1076 1321
1077 const int kExactWidth = 360; 1322 const int kExactWidth = 360;
1078 const int kMinHeight = 360; 1323 const int kMinHeight = 360;
1079 const int kMaxHeight = 720; 1324 const int kMaxHeight = 720;
1080 constraint_factory_.basic().width.setExact(kExactWidth); 1325 constraint_factory_.basic().width.setExact(kExactWidth);
1081 constraint_factory_.basic().height.setMin(kMinHeight); 1326 constraint_factory_.basic().height.setMin(kMinHeight);
1082 constraint_factory_.basic().height.setMax(kMaxHeight); 1327 constraint_factory_.basic().height.setMax(kMaxHeight);
1083 constraint_factory_.basic().aspectRatio.setMax(kAspectRatio); 1328 constraint_factory_.basic().aspectRatio.setMax(kAspectRatio);
1084 result = SelectSettings(); 1329 result = SelectSettings();
1085 EXPECT_TRUE(result.HasValue()); 1330 EXPECT_TRUE(result.HasValue());
1086 min_width = std::max(1, kExactWidth); 1331 min_width = std::max(1, kExactWidth);
1087 max_height = std::min(result.Height(), kMaxHeight); 1332 max_height = std::min(result.Height(), kMaxHeight);
1088 min_aspect_ratio = min_width / max_height; 1333 min_aspect_ratio = min_width / max_height;
1089 // Minimum constraint aspect ratio must be less than or equal to the minimum 1334 // Minimum constraint aspect ratio must be less than or equal to the minimum
1090 // supported by the source. 1335 // supported by the source.
1091 EXPECT_GE(kAspectRatio, min_aspect_ratio); 1336 EXPECT_GE(kAspectRatio, min_aspect_ratio);
1092 // Given resolution constraints, the default device with closest-to-default 1337 // Given resolution constraints, the default device with closest-to-default
1093 // settings cannot satisfy the required maximum aspect ratio (maximum would 1338 // settings cannot satisfy the required maximum aspect ratio (maximum would
1094 // be 360/500). 1339 // be 360/500).
1095 // The high-res device with a native resolution of 1280x720 can support 1340 // The high-res device with a native resolution of 1280x720 can support
1096 // 360x720 with cropping with less penalty than the default device at 1341 // 360x720 with cropping with less penalty than the default device at
1097 // 1000x1000. 1342 // 1000x1000.
1098 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1343 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1099 EXPECT_EQ(1280, result.Width()); 1344 EXPECT_EQ(1280, result.Width());
1100 EXPECT_EQ(720, result.Height()); 1345 EXPECT_EQ(720, result.Height());
1346 // The track's aspect ratio is adjusted to the maximum, which is lower than
1347 // the source's native aspect ratio.
1348 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1349 EXPECT_EQ(std::round(result.Height() * kAspectRatio),
1350 result.track_adapter_settings().max_width);
1351 EXPECT_EQ(min_aspect_ratio, result.track_adapter_settings().min_aspect_ratio);
1352 EXPECT_EQ(kAspectRatio, result.track_adapter_settings().max_aspect_ratio);
1353 CheckTrackAdapterSettingsEqualsFrameRate(result);
1101 } 1354 }
1102 1355
1103 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryAspectRatioRange) { 1356 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, MandatoryAspectRatioRange) {
1104 constraint_factory_.Reset(); 1357 constraint_factory_.Reset();
1105 { 1358 {
1106 const double kMinAspectRatio = 0.5; 1359 const double kMinAspectRatio = 0.5;
1107 const double kMaxAspectRatio = 1.0; 1360 const double kMaxAspectRatio = 1.0;
1108 1361
1109 constraint_factory_.basic().aspectRatio.setMin(kMinAspectRatio); 1362 constraint_factory_.basic().aspectRatio.setMin(kMinAspectRatio);
1110 constraint_factory_.basic().aspectRatio.setMax(kMaxAspectRatio); 1363 constraint_factory_.basic().aspectRatio.setMax(kMaxAspectRatio);
1111 auto result = SelectSettings(); 1364 auto result = SelectSettings();
1112 EXPECT_TRUE(result.HasValue()); 1365 EXPECT_TRUE(result.HasValue());
1113 double min_width = 1.0; 1366 double min_width = 1.0;
1114 double max_width = result.Width(); 1367 double max_width = result.Width();
1115 double min_height = 1.0; 1368 double min_height = 1.0;
1116 double max_height = result.Height(); 1369 double max_height = result.Height();
1117 double min_aspect_ratio = min_width / max_height; 1370 double min_aspect_ratio = min_width / max_height;
1118 double max_aspect_ratio = max_width / min_height; 1371 double max_aspect_ratio = max_width / min_height;
1119 // Constraint aspect-ratio range must have nonempty intersection with 1372 // Constraint aspect-ratio range must have nonempty intersection with
1120 // supported range. 1373 // supported range.
1121 EXPECT_LE(kMinAspectRatio, max_aspect_ratio); 1374 EXPECT_LE(kMinAspectRatio, max_aspect_ratio);
1122 EXPECT_GE(kMaxAspectRatio, min_aspect_ratio); 1375 EXPECT_GE(kMaxAspectRatio, min_aspect_ratio);
1123 // All devices in |capabilities_| support the requested aspect-ratio range. 1376 // All devices in |capabilities_| support the requested aspect-ratio range.
1124 // The algorithm should prefer the first device that supports the requested 1377 // The algorithm should prefer the first device that supports the requested
1125 // aspect-ratio range, which in this case is the default device. 1378 // aspect-ratio range, which in this case is the default device.
1126 EXPECT_EQ(default_device_->device_id, result.device_id()); 1379 EXPECT_EQ(default_device_->device_id, result.device_id());
1127 EXPECT_EQ(*default_closest_format_, result.Format()); 1380 EXPECT_EQ(*default_closest_format_, result.Format());
1381 // The source's aspect ratio matches the maximum aspect ratio. No adjustment
1382 // is required.
1383 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1384 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1385 EXPECT_EQ(kMinAspectRatio,
1386 result.track_adapter_settings().min_aspect_ratio);
1387 EXPECT_EQ(kMaxAspectRatio,
1388 result.track_adapter_settings().max_aspect_ratio);
1389 CheckTrackAdapterSettingsEqualsFrameRate(result);
1128 } 1390 }
1129 1391
1130 { 1392 {
1131 const double kMinAspectRatio = 3.0; 1393 const double kMinAspectRatio = 3.0;
1132 const double kMaxAspectRatio = 4.0; 1394 const double kMaxAspectRatio = 4.0;
1133 1395
1134 const long kExactHeight = 600; 1396 const long kMinHeight = 600;
1135 constraint_factory_.Reset(); 1397 constraint_factory_.Reset();
1136 constraint_factory_.basic().height.setMin(kExactHeight); 1398 constraint_factory_.basic().height.setMin(kMinHeight);
1137 constraint_factory_.basic().aspectRatio.setMin(kMinAspectRatio); 1399 constraint_factory_.basic().aspectRatio.setMin(kMinAspectRatio);
1138 constraint_factory_.basic().aspectRatio.setMax(kMaxAspectRatio); 1400 constraint_factory_.basic().aspectRatio.setMax(kMaxAspectRatio);
1139 auto result = SelectSettings(); 1401 auto result = SelectSettings();
1140 EXPECT_TRUE(result.HasValue()); 1402 EXPECT_TRUE(result.HasValue());
1141 double min_width = 1.0; 1403 double min_width = 1.0;
1142 double max_width = result.Width(); 1404 double max_width = result.Width();
1143 double min_height = 1.0; 1405 double min_height = 1.0;
1144 double max_height = result.Height(); 1406 double max_height = result.Height();
1145 double min_aspect_ratio = min_width / max_height; 1407 double min_aspect_ratio = min_width / max_height;
1146 double max_aspect_ratio = max_width / min_height; 1408 double max_aspect_ratio = max_width / min_height;
1147 // Constraint aspect-ratio range must have nonempty intersection with 1409 // Constraint aspect-ratio range must have nonempty intersection with
1148 // supported range. 1410 // supported range.
1149 EXPECT_LE(kMinAspectRatio, max_aspect_ratio); 1411 EXPECT_LE(kMinAspectRatio, max_aspect_ratio);
1150 EXPECT_GE(kMaxAspectRatio, min_aspect_ratio); 1412 EXPECT_GE(kMaxAspectRatio, min_aspect_ratio);
1151 // The only device that supports the resolution and aspect ratio constraint 1413 // The only device that supports the resolution and aspect ratio constraint
1152 // is the high-res device. The 1920x1080 is the least expensive format. 1414 // is the high-res device. The 1920x1080 is the least expensive format.
1153 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1415 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1154 EXPECT_EQ(1920, result.Width()); 1416 EXPECT_EQ(1920, result.Width());
1155 EXPECT_EQ(1080, result.Height()); 1417 EXPECT_EQ(1080, result.Height());
1418 // The track is cropped to support the minimum aspect ratio.
1419 EXPECT_EQ(std::round(result.Width() / kMinAspectRatio),
1420 result.track_adapter_settings().max_height);
1421 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1422 EXPECT_EQ(kMinAspectRatio,
1423 result.track_adapter_settings().min_aspect_ratio);
1424 EXPECT_EQ(static_cast<double>(result.Width()) / kMinHeight,
1425 result.track_adapter_settings().max_aspect_ratio);
1426 CheckTrackAdapterSettingsEqualsFrameRate(result);
1156 } 1427 }
1157 } 1428 }
1158 1429
1159 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, IdealAspectRatio) { 1430 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, IdealAspectRatio) {
1160 constraint_factory_.Reset(); 1431 constraint_factory_.Reset();
1161 { 1432 {
1162 const double kIdealAspectRatio = 0.5; 1433 const double kIdealAspectRatio = 0.5;
1163 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio); 1434 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
1164 auto result = SelectSettings(); 1435 auto result = SelectSettings();
1165 EXPECT_TRUE(result.HasValue()); 1436 EXPECT_TRUE(result.HasValue());
1166 double min_width = 1.0; 1437 double min_width = 1.0;
1167 double max_width = result.Width(); 1438 double max_width = result.Width();
1168 double min_height = 1.0; 1439 double min_height = 1.0;
1169 double max_height = result.Height(); 1440 double max_height = result.Height();
1170 double min_aspect_ratio = min_width / max_height; 1441 double min_aspect_ratio = min_width / max_height;
1171 double max_aspect_ratio = max_width / min_height; 1442 double max_aspect_ratio = max_width / min_height;
1172 // All devices in |capabilities_| support the ideal aspect-ratio. 1443 // All devices in |capabilities_| support the ideal aspect-ratio.
1173 // The algorithm should prefer the default device with closest-to-default 1444 // The algorithm should prefer the default device with closest-to-default
1174 // settings. 1445 // settings.
1175 EXPECT_LE(kIdealAspectRatio, max_aspect_ratio); 1446 EXPECT_LE(kIdealAspectRatio, max_aspect_ratio);
1176 EXPECT_GE(kIdealAspectRatio, min_aspect_ratio); 1447 EXPECT_GE(kIdealAspectRatio, min_aspect_ratio);
1177 EXPECT_EQ(default_device_->device_id, result.device_id()); 1448 EXPECT_EQ(default_device_->device_id, result.device_id());
1178 EXPECT_EQ(*default_closest_format_, result.Format()); 1449 EXPECT_EQ(*default_closest_format_, result.Format());
1450 // The track is cropped to support the ideal aspect ratio.
1451 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1452 EXPECT_EQ(std::round(result.Height() * kIdealAspectRatio),
1453 result.track_adapter_settings().max_width);
1454 EXPECT_EQ(min_aspect_ratio,
1455 result.track_adapter_settings().min_aspect_ratio);
1456 EXPECT_EQ(max_aspect_ratio,
1457 result.track_adapter_settings().max_aspect_ratio);
1458 CheckTrackAdapterSettingsEqualsFrameRate(result);
1179 } 1459 }
1180 1460
1181 { 1461 {
1182 const double kIdealAspectRatio = 1500.0; 1462 const double kIdealAspectRatio = 1500.0;
1183 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio); 1463 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
1184 auto result = SelectSettings(); 1464 auto result = SelectSettings();
1185 EXPECT_TRUE(result.HasValue()); 1465 EXPECT_TRUE(result.HasValue());
1186 // The only device that supports the ideal aspect ratio is the high-res 1466 // The only device that supports the ideal aspect ratio is the high-res
1187 // device. The least expensive way to support it with the 1920x1080 format 1467 // device.
1188 // cropped to 1500x1.
1189 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1468 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1190 EXPECT_EQ(1920, result.Width()); 1469 EXPECT_EQ(1920, result.Width());
1191 EXPECT_EQ(1080, result.Height()); 1470 EXPECT_EQ(1080, result.Height());
1471 // The most exact way to support the ideal aspect ratio would be to crop to
1472 // 1500x1. However, the algorithm tries to crop to 1920x1.28 and rounds.
1473 // In this case, the effect of rounding is noticeable because of the
1474 // resulting low value for height. For more typical resolution values,
1475 // the at-most 1-pixel error caused by rounding is not an issue.
1476 EXPECT_EQ(std::round(result.Width() / kIdealAspectRatio),
1477 result.track_adapter_settings().max_height);
1478 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1479 EXPECT_EQ(1.0 / result.Height(),
1480 result.track_adapter_settings().min_aspect_ratio);
1481 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
1482 CheckTrackAdapterSettingsEqualsFrameRate(result);
1192 } 1483 }
1193 1484
1194 { 1485 {
1195 const double kIdealAspectRatio = 2000.0; 1486 const double kIdealAspectRatio = 2000.0;
1196 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio); 1487 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
1197 auto result = SelectSettings(); 1488 auto result = SelectSettings();
1198 EXPECT_TRUE(result.HasValue()); 1489 EXPECT_TRUE(result.HasValue());
1199 // The only device that supports the ideal aspect ratio is the high-res 1490 // The only device that supports the ideal aspect ratio is the high-res
1200 // device with its highest resolution, cropped to 2000x1. 1491 // device with its highest resolution.
1201 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1492 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1202 EXPECT_EQ(*high_res_highest_format_, result.Format()); 1493 EXPECT_EQ(*high_res_highest_format_, result.Format());
1494 EXPECT_EQ(std::round(result.Width() / kIdealAspectRatio),
1495 result.track_adapter_settings().max_height);
1496 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1497 EXPECT_EQ(1.0 / result.Height(),
1498 result.track_adapter_settings().min_aspect_ratio);
1499 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
1500 CheckTrackAdapterSettingsEqualsFrameRate(result);
1203 } 1501 }
1204 1502
1205 { 1503 {
1206 const double kIdealAspectRatio = 4000.0; 1504 const double kIdealAspectRatio = 4000.0;
1207 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio); 1505 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
1208 auto result = SelectSettings(); 1506 auto result = SelectSettings();
1209 EXPECT_TRUE(result.HasValue()); 1507 EXPECT_TRUE(result.HasValue());
1210 // The configuration closest to the ideal aspect ratio is is the high-res 1508 // The configuration closest to the ideal aspect ratio is is the high-res
1211 // device with its highest resolution, cropped to 2304x1. 1509 // device with its highest resolution, cropped to 2304x1.
1212 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1510 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1213 EXPECT_EQ(*high_res_highest_format_, result.Format()); 1511 EXPECT_EQ(*high_res_highest_format_, result.Format());
1512 // In this case there is no rounding error.
1513 EXPECT_EQ(1, result.track_adapter_settings().max_height);
1514 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1515 EXPECT_EQ(1.0 / result.Height(),
1516 result.track_adapter_settings().min_aspect_ratio);
1517 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
1518 CheckTrackAdapterSettingsEqualsFrameRate(result);
1214 } 1519 }
1215 1520
1216 { 1521 {
1217 const double kIdealAspectRatio = 2.0; 1522 const double kIdealAspectRatio = 2.0;
1523 const int kExactHeight = 400;
1218 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio); 1524 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
1219 constraint_factory_.basic().height.setExact(400); 1525 constraint_factory_.basic().height.setExact(kExactHeight);
1220 auto result = SelectSettings(); 1526 auto result = SelectSettings();
1221 EXPECT_TRUE(result.HasValue()); 1527 EXPECT_TRUE(result.HasValue());
1222 // The first device to support the ideal aspect ratio and the resolution 1528 // The first device to support the ideal aspect ratio and the resolution
1223 // constraint is the low-res device. The 800x600 format cropped to 800x400 1529 // constraint is the low-res device. The 800x600 format cropped to 800x400
1224 // is the lest expensive way to achieve it. 1530 // is the lest expensive way to achieve it.
1225 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 1531 EXPECT_EQ(low_res_device_->device_id, result.device_id());
1226 EXPECT_EQ(800, result.Width()); 1532 EXPECT_EQ(800, result.Width());
1227 EXPECT_EQ(600, result.Height()); 1533 EXPECT_EQ(600, result.Height());
1534 EXPECT_EQ(kExactHeight, result.track_adapter_settings().max_height);
1535 EXPECT_EQ(kExactHeight * kIdealAspectRatio,
1536 result.track_adapter_settings().max_width);
1537 EXPECT_EQ(1.0 / kExactHeight,
1538 result.track_adapter_settings().min_aspect_ratio);
1539 EXPECT_EQ(static_cast<double>(result.Width()) / kExactHeight,
1540 result.track_adapter_settings().max_aspect_ratio);
1541 CheckTrackAdapterSettingsEqualsFrameRate(result);
1228 } 1542 }
1229 1543
1230 { 1544 {
1231 const double kIdealAspectRatio = 3.0; 1545 const double kIdealAspectRatio = 3.0;
1546 const int kExactHeight = 400;
1232 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio); 1547 constraint_factory_.basic().aspectRatio.setIdeal(kIdealAspectRatio);
1233 constraint_factory_.basic().height.setExact(400); 1548 constraint_factory_.basic().height.setExact(kExactHeight);
1234 auto result = SelectSettings(); 1549 auto result = SelectSettings();
1235 EXPECT_TRUE(result.HasValue()); 1550 EXPECT_TRUE(result.HasValue());
1236 // The only device that supports the ideal aspect ratio and the resolution 1551 // The only device that supports the ideal aspect ratio and the resolution
1237 // constraint is the high-res device. The 1280x720 cropped to 1200x400 is 1552 // constraint is the high-res device. The 1280x720 cropped to 1200x400 is
1238 // the lest expensive way to achieve it. 1553 // the lest expensive way to achieve it.
1239 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1554 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1240 EXPECT_EQ(1280, result.Width()); 1555 EXPECT_EQ(1280, result.Width());
1241 EXPECT_EQ(720, result.Height()); 1556 EXPECT_EQ(720, result.Height());
1557 EXPECT_EQ(kExactHeight, result.track_adapter_settings().max_height);
1558 EXPECT_EQ(kExactHeight * kIdealAspectRatio,
1559 result.track_adapter_settings().max_width);
1560 EXPECT_EQ(1.0 / kExactHeight,
1561 result.track_adapter_settings().min_aspect_ratio);
1562 EXPECT_EQ(static_cast<double>(result.Width()) / kExactHeight,
1563 result.track_adapter_settings().max_aspect_ratio);
1564 CheckTrackAdapterSettingsEqualsFrameRate(result);
1242 } 1565 }
1243 } 1566 }
1244 1567
1245 // The "Advanced" tests check selection criteria involving advanced constraint 1568 // The "Advanced" tests check selection criteria involving advanced constraint
1246 // sets. 1569 // sets.
1247 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1570 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1248 AdvancedMinMaxResolutionFrameRate) { 1571 AdvancedMinMaxResolutionFrameRate) {
1249 constraint_factory_.Reset(); 1572 constraint_factory_.Reset();
1250 blink::WebMediaTrackConstraintSet& advanced1 = 1573 blink::WebMediaTrackConstraintSet& advanced1 =
1251 constraint_factory_.AddAdvanced(); 1574 constraint_factory_.AddAdvanced();
1252 advanced1.width.setMin(4000); 1575 advanced1.width.setMin(4000);
1253 advanced1.height.setMin(4000); 1576 advanced1.height.setMin(4000);
1254 // No device supports the first advanced set. This first advanced constraint 1577 // No device supports the first advanced set. This first advanced constraint
1255 // set is therefore ignored in all calls to SelectSettings(). 1578 // set is therefore ignored in all calls to SelectSettings().
1256 // Tie-breaker rule that applies is closeness to default settings. 1579 // Tie-breaker rule that applies is closeness to default settings.
1257 auto result = SelectSettings(); 1580 auto result = SelectSettings();
1258 EXPECT_EQ(default_device_->device_id, result.device_id()); 1581 EXPECT_EQ(default_device_->device_id, result.device_id());
1259 EXPECT_EQ(*default_closest_format_, result.Format()); 1582 EXPECT_EQ(*default_closest_format_, result.Format());
1583 CheckTrackAdapterSettingsEqualsFormat(result);
1260 1584
1261 blink::WebMediaTrackConstraintSet& advanced2 = 1585 blink::WebMediaTrackConstraintSet& advanced2 =
1262 constraint_factory_.AddAdvanced(); 1586 constraint_factory_.AddAdvanced();
1263 advanced2.width.setMin(320); 1587 advanced2.width.setMin(320);
1264 advanced2.height.setMin(240); 1588 advanced2.height.setMin(240);
1265 advanced2.width.setMax(640); 1589 advanced2.width.setMax(640);
1266 advanced2.height.setMax(480); 1590 advanced2.height.setMax(480);
1267 result = SelectSettings(); 1591 result = SelectSettings();
1268 // The device that best supports this advanced set is the low-res device, 1592 // The device that best supports this advanced set is the low-res device,
1269 // which natively supports the maximum resolution. 1593 // which natively supports the maximum resolution.
1270 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 1594 EXPECT_EQ(low_res_device_->device_id, result.device_id());
1271 EXPECT_EQ(640, result.Width()); 1595 EXPECT_EQ(640, result.Width());
1272 EXPECT_EQ(480, result.Height()); 1596 EXPECT_EQ(480, result.Height());
1597 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1598 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1599 EXPECT_EQ(320.0 / 480.0, result.track_adapter_settings().min_aspect_ratio);
1600 EXPECT_EQ(640.0 / 240.0, result.track_adapter_settings().max_aspect_ratio);
1601 CheckTrackAdapterSettingsEqualsFrameRate(result);
1273 1602
1274 blink::WebMediaTrackConstraintSet& advanced3 = 1603 blink::WebMediaTrackConstraintSet& advanced3 =
1275 constraint_factory_.AddAdvanced(); 1604 constraint_factory_.AddAdvanced();
1276 advanced3.frameRate.setMax(10.0); 1605 advanced3.frameRate.setMax(10.0);
1277 result = SelectSettings(); 1606 result = SelectSettings();
1278 EXPECT_TRUE(result.HasValue()); 1607 EXPECT_TRUE(result.HasValue());
1279 // The high-res device natively supports the third advanced set in addition 1608 // The high-res device natively supports the third advanced set in addition
1280 // to the previous set and should be selected. 1609 // to the previous set and should be selected.
1281 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1610 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1282 EXPECT_EQ(640, result.Width()); 1611 EXPECT_EQ(640, result.Width());
1283 EXPECT_EQ(480, result.Height()); 1612 EXPECT_EQ(480, result.Height());
1613 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1614 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1615 EXPECT_EQ(320.0 / 480.0, result.track_adapter_settings().min_aspect_ratio);
1616 EXPECT_EQ(640.0 / 240.0, result.track_adapter_settings().max_aspect_ratio);
1617 CheckTrackAdapterSettingsEqualsFrameRate(result, 10.0);
1284 1618
1285 blink::WebMediaTrackConstraintSet& advanced4 = 1619 blink::WebMediaTrackConstraintSet& advanced4 =
1286 constraint_factory_.AddAdvanced(); 1620 constraint_factory_.AddAdvanced();
1287 advanced4.width.setMax(1000); 1621 advanced4.width.setMax(1000);
1288 advanced4.height.setMax(1000); 1622 advanced4.height.setMax(1000);
1289 result = SelectSettings(); 1623 result = SelectSettings();
1290 // Even though the default device supports the resolution in the fourth 1624 // Even though the default device supports the resolution in the fourth
1291 // advanced natively, having better support for the previous sets has 1625 // advanced natively, having better support for the previous sets has
1292 // precedence, so the high-res device is selected. 1626 // precedence, so the high-res device is selected.
1293 EXPECT_TRUE(result.HasValue()); 1627 EXPECT_TRUE(result.HasValue());
1294 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1628 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1295 EXPECT_EQ(640, result.Width()); 1629 EXPECT_EQ(640, result.Width());
1296 EXPECT_EQ(480, result.Height()); 1630 EXPECT_EQ(480, result.Height());
1631 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1632 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1633 EXPECT_EQ(320.0 / 480.0, result.track_adapter_settings().min_aspect_ratio);
1634 EXPECT_EQ(640.0 / 240.0, result.track_adapter_settings().max_aspect_ratio);
1635 CheckTrackAdapterSettingsEqualsFrameRate(result, 10.0);
1297 1636
1298 constraint_factory_.basic().width.setIdeal(100); 1637 constraint_factory_.basic().width.setIdeal(100);
1299 constraint_factory_.basic().height.setIdeal(100); 1638 constraint_factory_.basic().height.setIdeal(100);
1300 result = SelectSettings(); 1639 result = SelectSettings();
1301 EXPECT_TRUE(result.HasValue()); 1640 EXPECT_TRUE(result.HasValue());
1302 // The high-res device at 600x400@10Hz supports all advanced sets and is 1641 // The high-res device at 600x400@10Hz supports all advanced sets and is
1303 // better at supporting the ideal value. 1642 // better at supporting the ideal value.
1304 // It beats 320x240@30Hz because the penalty for the native frame rate takes 1643 // It beats 320x240@30Hz because the penalty for the native frame rate takes
1305 // precedence over the native fitness distance. 1644 // precedence over the native fitness distance.
1306 // Both support standard fitness distance equally, since 600x400 can be 1645 // Both support standard fitness distance equally, since 600x400 can be
1307 // cropped to 320x240. 1646 // cropped to 320x240.
1308 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1647 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1309 EXPECT_EQ(600, result.Width()); 1648 EXPECT_EQ(600, result.Width());
1310 EXPECT_EQ(400, result.Height()); 1649 EXPECT_EQ(400, result.Height());
1650 EXPECT_EQ(320, result.track_adapter_settings().max_width);
1651 EXPECT_EQ(240, result.track_adapter_settings().max_height);
1652 EXPECT_EQ(320.0 / 400.0, result.track_adapter_settings().min_aspect_ratio);
1653 EXPECT_EQ(600.0 / 240.0, result.track_adapter_settings().max_aspect_ratio);
1654 CheckTrackAdapterSettingsEqualsFrameRate(result, 10.0);
1311 1655
1312 constraint_factory_.basic().width.setIdeal(2000); 1656 constraint_factory_.basic().width.setIdeal(2000);
1313 constraint_factory_.basic().height.setIdeal(1500); 1657 constraint_factory_.basic().height.setIdeal(1500);
1314 result = SelectSettings(); 1658 result = SelectSettings();
1315 EXPECT_TRUE(result.HasValue()); 1659 EXPECT_TRUE(result.HasValue());
1316 // The high-res device at 640x480@10Hz is closer to the large ideal 1660 // The high-res device at 640x480@10Hz is closer to the large ideal
1317 // resolution. 1661 // resolution.
1318 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1662 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1319 EXPECT_EQ(640, result.Width()); 1663 EXPECT_EQ(640, result.Width());
1320 EXPECT_EQ(480, result.Height()); 1664 EXPECT_EQ(480, result.Height());
1665 EXPECT_EQ(640, result.track_adapter_settings().max_width);
1666 EXPECT_EQ(480, result.track_adapter_settings().max_height);
1667 EXPECT_EQ(320.0 / 480.0, result.track_adapter_settings().min_aspect_ratio);
1668 EXPECT_EQ(640.0 / 240.0, result.track_adapter_settings().max_aspect_ratio);
1669 CheckTrackAdapterSettingsEqualsFrameRate(result, 10.0);
1321 } 1670 }
1322 1671
1323 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1672 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1324 AdvancedResolutionAndFrameRate) { 1673 AdvancedResolutionAndFrameRate) {
1325 constraint_factory_.Reset(); 1674 constraint_factory_.Reset();
1326 blink::WebMediaTrackConstraintSet& advanced1 = 1675 blink::WebMediaTrackConstraintSet& advanced1 =
1327 constraint_factory_.AddAdvanced(); 1676 constraint_factory_.AddAdvanced();
1328 advanced1.width.setExact(1920); 1677 advanced1.width.setExact(1920);
1329 advanced1.height.setExact(1080); 1678 advanced1.height.setExact(1080);
1330 blink::WebMediaTrackConstraintSet& advanced2 = 1679 blink::WebMediaTrackConstraintSet& advanced2 =
1331 constraint_factory_.AddAdvanced(); 1680 constraint_factory_.AddAdvanced();
1332 advanced2.frameRate.setExact(60.0); 1681 advanced2.frameRate.setExact(60.0);
1333 blink::WebMediaTrackConstraintSet& advanced3 = 1682 blink::WebMediaTrackConstraintSet& advanced3 =
1334 constraint_factory_.AddAdvanced(); 1683 constraint_factory_.AddAdvanced();
1335 advanced3.width.setExact(2304); 1684 advanced3.width.setExact(2304);
1336 advanced3.height.setExact(1536); 1685 advanced3.height.setExact(1536);
1337 auto result = SelectSettings(); 1686 auto result = SelectSettings();
1338 EXPECT_TRUE(result.HasValue()); 1687 EXPECT_TRUE(result.HasValue());
1339 // The high-res device is the only one that satisfies the first advanced 1688 // The high-res device is the only one that satisfies the first advanced
1340 // set. 2304x1536x10.0 satisfies sets 1 and 3, while 1920x1080x60.0 1689 // set. 2304x1536x10.0 satisfies sets 1 and 3, while 1920x1080x60.0
1341 // satisfies sets 1, and 2. The latter must be selected, regardless of 1690 // satisfies sets 1, and 2. The latter must be selected, regardless of
1342 // any other criteria. 1691 // any other criteria.
1343 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1692 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1344 EXPECT_EQ(1920, result.Width()); 1693 EXPECT_EQ(1920, result.Width());
1345 EXPECT_EQ(1080, result.Height()); 1694 EXPECT_EQ(1080, result.Height());
1346 EXPECT_EQ(60.0, result.FrameRate()); 1695 EXPECT_EQ(60.0, result.FrameRate());
1696 EXPECT_EQ(1920, result.track_adapter_settings().max_width);
1697 EXPECT_EQ(1080, result.track_adapter_settings().max_height);
1698 EXPECT_EQ(1920.0 / 1080.0, result.track_adapter_settings().min_aspect_ratio);
1699 EXPECT_EQ(1920.0 / 1080.0, result.track_adapter_settings().max_aspect_ratio);
1700 CheckTrackAdapterSettingsEqualsFrameRate(result, 60.0);
1347 } 1701 }
1348 1702
1349 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, AdvancedNoiseReduction) { 1703 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, AdvancedNoiseReduction) {
1350 constraint_factory_.Reset(); 1704 constraint_factory_.Reset();
1351 blink::WebMediaTrackConstraintSet& advanced1 = 1705 blink::WebMediaTrackConstraintSet& advanced1 =
1352 constraint_factory_.AddAdvanced(); 1706 constraint_factory_.AddAdvanced();
1353 advanced1.width.setMin(640); 1707 advanced1.width.setMin(640);
1354 advanced1.height.setMin(480); 1708 advanced1.height.setMin(480);
1355 blink::WebMediaTrackConstraintSet& advanced2 = 1709 blink::WebMediaTrackConstraintSet& advanced2 =
1356 constraint_factory_.AddAdvanced(); 1710 constraint_factory_.AddAdvanced();
1357 advanced2.width.setMin(1920); 1711 advanced2.width.setMin(1920);
1358 advanced2.height.setMin(1080); 1712 advanced2.height.setMin(1080);
1359 advanced2.googNoiseReduction.setExact(false); 1713 advanced2.googNoiseReduction.setExact(false);
1360 auto result = SelectSettings(); 1714 auto result = SelectSettings();
1361 EXPECT_TRUE(result.HasValue()); 1715 EXPECT_TRUE(result.HasValue());
1362 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1716 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1363 EXPECT_LE(1920, result.Width()); 1717 EXPECT_LE(1920, result.Width());
1364 EXPECT_LE(1080, result.Height()); 1718 EXPECT_LE(1080, result.Height());
1365 EXPECT_TRUE(result.noise_reduction() && !*result.noise_reduction()); 1719 EXPECT_TRUE(result.noise_reduction() && !*result.noise_reduction());
1720 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1721 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1722 EXPECT_EQ(1920.0 / result.Height(),
1723 result.track_adapter_settings().min_aspect_ratio);
1724 EXPECT_EQ(result.Width() / 1080.0,
1725 result.track_adapter_settings().max_aspect_ratio);
1726 CheckTrackAdapterSettingsEqualsFrameRate(result);
1366 } 1727 }
1367 1728
1368 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1729 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1369 AdvancedContradictoryNoiseReduction) { 1730 AdvancedContradictoryNoiseReduction) {
1370 { 1731 {
1371 constraint_factory_.Reset(); 1732 constraint_factory_.Reset();
1372 blink::WebMediaTrackConstraintSet& advanced1 = 1733 blink::WebMediaTrackConstraintSet& advanced1 =
1373 constraint_factory_.AddAdvanced(); 1734 constraint_factory_.AddAdvanced();
1374 advanced1.width.setMin(640); 1735 advanced1.width.setMin(640);
1375 advanced1.height.setMin(480); 1736 advanced1.height.setMin(480);
1376 advanced1.googNoiseReduction.setExact(true); 1737 advanced1.googNoiseReduction.setExact(true);
1377 blink::WebMediaTrackConstraintSet& advanced2 = 1738 blink::WebMediaTrackConstraintSet& advanced2 =
1378 constraint_factory_.AddAdvanced(); 1739 constraint_factory_.AddAdvanced();
1379 advanced2.width.setMin(1920); 1740 advanced2.width.setMin(1920);
1380 advanced2.height.setMin(1080); 1741 advanced2.height.setMin(1080);
1381 advanced2.googNoiseReduction.setExact(false); 1742 advanced2.googNoiseReduction.setExact(false);
1382 auto result = SelectSettings(); 1743 auto result = SelectSettings();
1383 EXPECT_TRUE(result.HasValue()); 1744 EXPECT_TRUE(result.HasValue());
1384 // The second advanced set cannot be satisfied because it contradicts the 1745 // The second advanced set cannot be satisfied because it contradicts the
1385 // first set. The default device supports the first set and should be 1746 // first set. The default device supports the first set and should be
1386 // selected. 1747 // selected.
1387 EXPECT_EQ(default_device_->device_id, result.device_id()); 1748 EXPECT_EQ(default_device_->device_id, result.device_id());
1388 EXPECT_LE(640, result.Width()); 1749 EXPECT_LE(640, result.Width());
1389 EXPECT_LE(480, result.Height()); 1750 EXPECT_LE(480, result.Height());
1390 EXPECT_TRUE(result.noise_reduction() && *result.noise_reduction()); 1751 EXPECT_TRUE(result.noise_reduction() && *result.noise_reduction());
1752 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1753 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1754 EXPECT_EQ(640.0 / result.Height(),
1755 result.track_adapter_settings().min_aspect_ratio);
1756 EXPECT_EQ(result.Width() / 480.0,
1757 result.track_adapter_settings().max_aspect_ratio);
1758 CheckTrackAdapterSettingsEqualsFrameRate(result);
1391 } 1759 }
1392 1760
1393 // Same test without noise reduction 1761 // Same test without noise reduction
1394 { 1762 {
1395 constraint_factory_.Reset(); 1763 constraint_factory_.Reset();
1396 blink::WebMediaTrackConstraintSet& advanced1 = 1764 blink::WebMediaTrackConstraintSet& advanced1 =
1397 constraint_factory_.AddAdvanced(); 1765 constraint_factory_.AddAdvanced();
1398 advanced1.width.setMin(640); 1766 advanced1.width.setMin(640);
1399 advanced1.height.setMin(480); 1767 advanced1.height.setMin(480);
1400 blink::WebMediaTrackConstraintSet& advanced2 = 1768 blink::WebMediaTrackConstraintSet& advanced2 =
1401 constraint_factory_.AddAdvanced(); 1769 constraint_factory_.AddAdvanced();
1402 advanced2.width.setMin(1920); 1770 advanced2.width.setMin(1920);
1403 advanced2.height.setMin(1080); 1771 advanced2.height.setMin(1080);
1404 auto result = SelectSettings(); 1772 auto result = SelectSettings();
1405 EXPECT_TRUE(result.HasValue()); 1773 EXPECT_TRUE(result.HasValue());
1406 // Only the high-res device can satisfy the second advanced set. 1774 // Only the high-res device can satisfy the second advanced set.
1407 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1775 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1408 EXPECT_LE(1920, result.Width()); 1776 EXPECT_LE(1920, result.Width());
1409 EXPECT_LE(1080, result.Height()); 1777 EXPECT_LE(1080, result.Height());
1410 // Should select default noise reduction setting. 1778 // Should select default noise reduction setting.
1411 EXPECT_TRUE(!result.noise_reduction()); 1779 EXPECT_TRUE(!result.noise_reduction());
1780 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1781 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1782 EXPECT_EQ(1920.0 / result.Height(),
1783 result.track_adapter_settings().min_aspect_ratio);
1784 EXPECT_EQ(result.Width() / 1080.0,
1785 result.track_adapter_settings().max_aspect_ratio);
1786 CheckTrackAdapterSettingsEqualsFrameRate(result);
1412 } 1787 }
1413 } 1788 }
1414 1789
1415 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1790 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1416 AdvancedContradictoryExactResolution) { 1791 AdvancedContradictoryExactResolution) {
1417 constraint_factory_.Reset(); 1792 constraint_factory_.Reset();
1418 blink::WebMediaTrackConstraintSet& advanced1 = 1793 blink::WebMediaTrackConstraintSet& advanced1 =
1419 constraint_factory_.AddAdvanced(); 1794 constraint_factory_.AddAdvanced();
1420 advanced1.width.setExact(640); 1795 advanced1.width.setExact(640);
1421 advanced1.height.setExact(480); 1796 advanced1.height.setExact(480);
1422 blink::WebMediaTrackConstraintSet& advanced2 = 1797 blink::WebMediaTrackConstraintSet& advanced2 =
1423 constraint_factory_.AddAdvanced(); 1798 constraint_factory_.AddAdvanced();
1424 advanced2.width.setExact(1920); 1799 advanced2.width.setExact(1920);
1425 advanced2.height.setExact(1080); 1800 advanced2.height.setExact(1080);
1426 auto result = SelectSettings(); 1801 auto result = SelectSettings();
1427 EXPECT_TRUE(result.HasValue()); 1802 EXPECT_TRUE(result.HasValue());
1428 // The second advanced set must be ignored because it contradicts the first 1803 // The second advanced set must be ignored because it contradicts the first
1429 // set. The low-res device is the one that best supports the requested 1804 // set. The low-res device is the one that best supports the requested
1430 // resolution. 1805 // resolution.
1431 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 1806 EXPECT_EQ(low_res_device_->device_id, result.device_id());
1432 EXPECT_EQ(640, result.Width()); 1807 EXPECT_EQ(640, result.Width());
1433 EXPECT_EQ(480, result.Height()); 1808 EXPECT_EQ(480, result.Height());
1809 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1810 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1811 EXPECT_EQ(640.0 / 480.0, result.track_adapter_settings().min_aspect_ratio);
1812 EXPECT_EQ(640.0 / 480.0, result.track_adapter_settings().max_aspect_ratio);
1813 CheckTrackAdapterSettingsEqualsFrameRate(result);
1434 } 1814 }
1435 1815
1436 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1816 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1437 AdvancedContradictoryMaxMinResolutionFrameRate) { 1817 AdvancedContradictoryMaxMinResolutionFrameRate) {
1438 constraint_factory_.Reset(); 1818 constraint_factory_.Reset();
1439 blink::WebMediaTrackConstraintSet& advanced1 = 1819 blink::WebMediaTrackConstraintSet& advanced1 =
1440 constraint_factory_.AddAdvanced(); 1820 constraint_factory_.AddAdvanced();
1441 advanced1.width.setMax(640); 1821 advanced1.width.setMax(640);
1442 advanced1.height.setMax(480); 1822 advanced1.height.setMax(480);
1443 blink::WebMediaTrackConstraintSet& advanced2 = 1823 blink::WebMediaTrackConstraintSet& advanced2 =
1444 constraint_factory_.AddAdvanced(); 1824 constraint_factory_.AddAdvanced();
1445 advanced2.width.setMin(1920); 1825 advanced2.width.setMin(1920);
1446 advanced2.height.setMin(1080); 1826 advanced2.height.setMin(1080);
1447 advanced2.frameRate.setExact(60.0); 1827 advanced2.frameRate.setExact(60.0);
1448 auto result = SelectSettings(); 1828 auto result = SelectSettings();
1449 EXPECT_TRUE(result.HasValue()); 1829 EXPECT_TRUE(result.HasValue());
1450 // The second advanced set must be ignored because it contradicts the first 1830 // The second advanced set must be ignored because it contradicts the first
1451 // set. The default device with the 200x200@40Hz format should be selected. 1831 // set. The default device with the 200x200@40Hz format should be selected.
1452 // That format satisfies the first advanced set as well as any other, so the 1832 // That format satisfies the first advanced set as well as any other, so the
1453 // tie breaker rule that applies is default device ID. 1833 // tie breaker rule that applies is default device ID.
1454 EXPECT_EQ(default_device_->device_id, result.device_id()); 1834 EXPECT_EQ(default_device_->device_id, result.device_id());
1455 EXPECT_EQ(200, result.Width()); 1835 EXPECT_EQ(200, result.Width());
1456 EXPECT_EQ(200, result.Height()); 1836 EXPECT_EQ(200, result.Height());
1457 EXPECT_EQ(40, result.FrameRate()); 1837 EXPECT_EQ(40, result.FrameRate());
1838 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1839 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1840 EXPECT_EQ(1.0 / result.Height(),
1841 result.track_adapter_settings().min_aspect_ratio);
1842 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
1843 CheckTrackAdapterSettingsEqualsFrameRate(result);
1458 } 1844 }
1459 1845
1460 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1846 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1461 AdvancedContradictoryMinMaxResolutionFrameRate) { 1847 AdvancedContradictoryMinMaxResolutionFrameRate) {
1462 constraint_factory_.Reset(); 1848 constraint_factory_.Reset();
1463 blink::WebMediaTrackConstraintSet& advanced1 = 1849 blink::WebMediaTrackConstraintSet& advanced1 =
1464 constraint_factory_.AddAdvanced(); 1850 constraint_factory_.AddAdvanced();
1465 advanced1.width.setMin(800); 1851 advanced1.width.setMin(800);
1466 advanced1.height.setMin(600); 1852 advanced1.height.setMin(600);
1467 blink::WebMediaTrackConstraintSet& advanced2 = 1853 blink::WebMediaTrackConstraintSet& advanced2 =
1468 constraint_factory_.AddAdvanced(); 1854 constraint_factory_.AddAdvanced();
1469 advanced2.width.setMax(640); 1855 advanced2.width.setMax(640);
1470 advanced2.height.setMax(480); 1856 advanced2.height.setMax(480);
1471 advanced2.frameRate.setExact(60.0); 1857 advanced2.frameRate.setExact(60.0);
1472 auto result = SelectSettings(); 1858 auto result = SelectSettings();
1473 EXPECT_TRUE(result.HasValue()); 1859 EXPECT_TRUE(result.HasValue());
1474 // The second advanced set must be ignored because it contradicts the first 1860 // The second advanced set must be ignored because it contradicts the first
1475 // set. The default device with the 1000x1000@20Hz format should be selected. 1861 // set. The default device with the 1000x1000@20Hz format should be selected.
1476 // That format satisfies the first advanced set as well as any other, so the 1862 // That format satisfies the first advanced set as well as any other, so the
1477 // tie breaker rule that applies is default device ID. 1863 // tie breaker rule that applies is default device ID.
1478 EXPECT_EQ(default_device_->device_id, result.device_id()); 1864 EXPECT_EQ(default_device_->device_id, result.device_id());
1479 EXPECT_EQ(1000, result.Width()); 1865 EXPECT_EQ(1000, result.Width());
1480 EXPECT_EQ(1000, result.Height()); 1866 EXPECT_EQ(1000, result.Height());
1481 EXPECT_EQ(20, result.FrameRate()); 1867 EXPECT_EQ(20, result.FrameRate());
1868 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1869 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
1870 EXPECT_EQ(800.0 / result.Height(),
1871 result.track_adapter_settings().min_aspect_ratio);
1872 EXPECT_EQ(result.Width() / 600.0,
1873 result.track_adapter_settings().max_aspect_ratio);
1874 CheckTrackAdapterSettingsEqualsFrameRate(result);
1482 } 1875 }
1483 1876
1484 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1877 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1485 AdvancedContradictoryExactAspectRatio) { 1878 AdvancedContradictoryExactAspectRatio) {
1486 constraint_factory_.Reset(); 1879 constraint_factory_.Reset();
1487 blink::WebMediaTrackConstraintSet& advanced1 = 1880 blink::WebMediaTrackConstraintSet& advanced1 =
1488 constraint_factory_.AddAdvanced(); 1881 constraint_factory_.AddAdvanced();
1489 advanced1.aspectRatio.setExact(2300.0); 1882 advanced1.aspectRatio.setExact(2300.0);
1490 blink::WebMediaTrackConstraintSet& advanced2 = 1883 blink::WebMediaTrackConstraintSet& advanced2 =
1491 constraint_factory_.AddAdvanced(); 1884 constraint_factory_.AddAdvanced();
1492 advanced2.aspectRatio.setExact(3.0); 1885 advanced2.aspectRatio.setExact(3.0);
1493 auto result = SelectSettings(); 1886 auto result = SelectSettings();
1494 EXPECT_TRUE(result.HasValue()); 1887 EXPECT_TRUE(result.HasValue());
1495 // The second advanced set must be ignored because it contradicts the first 1888 // The second advanced set must be ignored because it contradicts the first
1496 // set. Only the high-res device in the highest-resolution format supports the 1889 // set. Only the high-res device in the highest-resolution format supports the
1497 // requested aspect ratio. 1890 // requested aspect ratio.
1498 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1891 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1499 EXPECT_EQ(*high_res_highest_format_, result.Format()); 1892 EXPECT_EQ(*high_res_highest_format_, result.Format());
1893 // The track is cropped to support the exact aspect ratio.
1894 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1895 EXPECT_EQ(std::round(result.Height() / 2300.0),
1896 result.track_adapter_settings().max_height);
1897 EXPECT_EQ(2300.0, result.track_adapter_settings().min_aspect_ratio);
1898 EXPECT_EQ(2300.0, result.track_adapter_settings().max_aspect_ratio);
1899 CheckTrackAdapterSettingsEqualsFrameRate(result);
1500 } 1900 }
1501 1901
1502 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1902 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1503 AdvancedContradictoryAspectRatioRange) { 1903 AdvancedContradictoryAspectRatioRange) {
1504 constraint_factory_.Reset(); 1904 constraint_factory_.Reset();
1505 blink::WebMediaTrackConstraintSet& advanced1 = 1905 blink::WebMediaTrackConstraintSet& advanced1 =
1506 constraint_factory_.AddAdvanced(); 1906 constraint_factory_.AddAdvanced();
1507 advanced1.aspectRatio.setMin(2300.0); 1907 advanced1.aspectRatio.setMin(2300.0);
1508 blink::WebMediaTrackConstraintSet& advanced2 = 1908 blink::WebMediaTrackConstraintSet& advanced2 =
1509 constraint_factory_.AddAdvanced(); 1909 constraint_factory_.AddAdvanced();
1510 advanced2.aspectRatio.setMax(3.0); 1910 advanced2.aspectRatio.setMax(3.0);
1511 auto result = SelectSettings(); 1911 auto result = SelectSettings();
1512 EXPECT_TRUE(result.HasValue()); 1912 EXPECT_TRUE(result.HasValue());
1513 // The second advanced set must be ignored because it contradicts the first 1913 // The second advanced set must be ignored because it contradicts the first
1514 // set. Only the high-res device in the highest-resolution format supports the 1914 // set. Only the high-res device in the highest-resolution format supports the
1515 // requested aspect ratio. 1915 // requested aspect ratio.
1516 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 1916 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1517 EXPECT_EQ(*high_res_highest_format_, result.Format()); 1917 EXPECT_EQ(*high_res_highest_format_, result.Format());
1918 // The track is cropped to support the min aspect ratio.
1919 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
1920 EXPECT_EQ(std::round(result.Height() / 2300.0),
1921 result.track_adapter_settings().max_height);
1922 EXPECT_EQ(2300.0, result.track_adapter_settings().min_aspect_ratio);
1923 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_aspect_ratio);
1924 CheckTrackAdapterSettingsEqualsFrameRate(result);
1518 } 1925 }
1519 1926
1520 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1927 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1521 AdvancedContradictoryExactFrameRate) { 1928 AdvancedContradictoryExactFrameRate) {
1522 constraint_factory_.Reset(); 1929 constraint_factory_.Reset();
1523 blink::WebMediaTrackConstraintSet& advanced1 = 1930 blink::WebMediaTrackConstraintSet& advanced1 =
1524 constraint_factory_.AddAdvanced(); 1931 constraint_factory_.AddAdvanced();
1525 advanced1.frameRate.setExact(40.0); 1932 advanced1.frameRate.setExact(40.0);
1526 blink::WebMediaTrackConstraintSet& advanced2 = 1933 blink::WebMediaTrackConstraintSet& advanced2 =
1527 constraint_factory_.AddAdvanced(); 1934 constraint_factory_.AddAdvanced();
1528 advanced2.frameRate.setExact(45.0); 1935 advanced2.frameRate.setExact(45.0);
1529 auto result = SelectSettings(); 1936 auto result = SelectSettings();
1530 EXPECT_TRUE(result.HasValue()); 1937 EXPECT_TRUE(result.HasValue());
1531 // The second advanced set must be ignored because it contradicts the first 1938 // The second advanced set must be ignored because it contradicts the first
1532 // set. 1939 // set.
1533 EXPECT_EQ(40.0, result.FrameRate()); 1940 EXPECT_EQ(40.0, result.FrameRate());
1941 CheckTrackAdapterSettingsEqualsResolution(result);
1942 CheckTrackAdapterSettingsEqualsFrameRate(result);
1534 } 1943 }
1535 1944
1536 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1945 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1537 AdvancedContradictoryFrameRateRange) { 1946 AdvancedContradictoryFrameRateRange) {
1538 constraint_factory_.Reset(); 1947 constraint_factory_.Reset();
1539 blink::WebMediaTrackConstraintSet& advanced1 = 1948 blink::WebMediaTrackConstraintSet& advanced1 =
1540 constraint_factory_.AddAdvanced(); 1949 constraint_factory_.AddAdvanced();
1541 advanced1.frameRate.setMin(40.0); 1950 advanced1.frameRate.setMin(40.0);
1542 blink::WebMediaTrackConstraintSet& advanced2 = 1951 blink::WebMediaTrackConstraintSet& advanced2 =
1543 constraint_factory_.AddAdvanced(); 1952 constraint_factory_.AddAdvanced();
1544 advanced2.frameRate.setMax(35.0); 1953 advanced2.frameRate.setMax(35.0);
1545 auto result = SelectSettings(); 1954 auto result = SelectSettings();
1546 EXPECT_TRUE(result.HasValue()); 1955 EXPECT_TRUE(result.HasValue());
1547 // The second advanced set must be ignored because it contradicts the first 1956 // The second advanced set must be ignored because it contradicts the first
1548 // set. 1957 // set.
1549 EXPECT_LE(40.0, result.FrameRate()); 1958 EXPECT_LE(40.0, result.FrameRate());
1959 CheckTrackAdapterSettingsEqualsResolution(result);
1960 CheckTrackAdapterSettingsEqualsFrameRate(result);
1550 } 1961 }
1551 1962
1552 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1963 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1553 AdvancedContradictoryWidthFrameRate) { 1964 AdvancedContradictoryWidthFrameRate) {
1554 constraint_factory_.Reset(); 1965 constraint_factory_.Reset();
1555 blink::WebMediaTrackConstraintSet& advanced1 = 1966 blink::WebMediaTrackConstraintSet& advanced1 =
1556 constraint_factory_.AddAdvanced(); 1967 constraint_factory_.AddAdvanced();
1557 advanced1.width.setMax(1920); 1968 advanced1.width.setMax(1920);
1558 blink::WebMediaTrackConstraintSet& advanced2 = 1969 blink::WebMediaTrackConstraintSet& advanced2 =
1559 constraint_factory_.AddAdvanced(); 1970 constraint_factory_.AddAdvanced();
1560 advanced2.width.setMin(2000); 1971 advanced2.width.setMin(2000);
1561 advanced2.frameRate.setExact(10.0); 1972 advanced2.frameRate.setExact(10.0);
1562 blink::WebMediaTrackConstraintSet& advanced3 = 1973 blink::WebMediaTrackConstraintSet& advanced3 =
1563 constraint_factory_.AddAdvanced(); 1974 constraint_factory_.AddAdvanced();
1564 advanced3.frameRate.setExact(30.0); 1975 advanced3.frameRate.setExact(30.0);
1565 auto result = SelectSettings(); 1976 auto result = SelectSettings();
1566 EXPECT_TRUE(result.HasValue()); 1977 EXPECT_TRUE(result.HasValue());
1567 // The low-res device at 320x240@30Hz satisfies advanced sets 1 and 3. 1978 // The low-res device at 320x240@30Hz satisfies advanced sets 1 and 3.
1568 // The high-res device at 2304x1536@10.0f can satisfy sets 1 and 2, but not 1979 // The high-res device at 2304x1536@10.0f can satisfy sets 1 and 2, but not
1569 // both at the same time. Thus, low-res device must be preferred. 1980 // both at the same time. Thus, low-res device must be preferred.
1570 EXPECT_EQ(low_res_device_->device_id, result.device_id()); 1981 EXPECT_EQ(low_res_device_->device_id, result.device_id());
1571 EXPECT_EQ(30.0, result.FrameRate()); 1982 EXPECT_EQ(30.0, result.FrameRate());
1572 EXPECT_GE(1920, result.Width()); 1983 EXPECT_GE(1920, result.Width());
1984 CheckTrackAdapterSettingsEqualsFormat(result);
1573 } 1985 }
1574 1986
1575 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 1987 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1576 AdvancedContradictoryHeightFrameRate) { 1988 AdvancedContradictoryHeightFrameRate) {
1577 constraint_factory_.Reset(); 1989 constraint_factory_.Reset();
1578 blink::WebMediaTrackConstraintSet& advanced1 = 1990 blink::WebMediaTrackConstraintSet& advanced1 =
1579 constraint_factory_.AddAdvanced(); 1991 constraint_factory_.AddAdvanced();
1580 advanced1.height.setMax(1080); 1992 advanced1.height.setMax(1080);
1581 blink::WebMediaTrackConstraintSet& advanced2 = 1993 blink::WebMediaTrackConstraintSet& advanced2 =
1582 constraint_factory_.AddAdvanced(); 1994 constraint_factory_.AddAdvanced();
1583 advanced2.height.setMin(1500); 1995 advanced2.height.setMin(1500);
1584 advanced2.frameRate.setExact(10.0); 1996 advanced2.frameRate.setExact(10.0);
1585 blink::WebMediaTrackConstraintSet& advanced3 = 1997 blink::WebMediaTrackConstraintSet& advanced3 =
1586 constraint_factory_.AddAdvanced(); 1998 constraint_factory_.AddAdvanced();
1587 advanced3.frameRate.setExact(60.0); 1999 advanced3.frameRate.setExact(60.0);
1588 auto result = SelectSettings(); 2000 auto result = SelectSettings();
1589 EXPECT_TRUE(result.HasValue()); 2001 EXPECT_TRUE(result.HasValue());
1590 // The high-res device at 1280x768@60Hz and 1920x1080@60Hz satisfies advanced 2002 // The high-res device at 1280x768@60Hz and 1920x1080@60Hz satisfies advanced
1591 // sets 1 and 3. The same device at 2304x1536@10.0f can satisfy sets 1 and 2, 2003 // sets 1 and 3. The same device at 2304x1536@10.0f can satisfy sets 1 and 2,
1592 // but not both at the same time. Thus, the format closest to default that 2004 // but not both at the same time. Thus, the format closest to default that
1593 // satisfies sets 1 and 3 must be chosen. 2005 // satisfies sets 1 and 3 must be chosen.
1594 EXPECT_EQ(high_res_device_->device_id, result.device_id()); 2006 EXPECT_EQ(high_res_device_->device_id, result.device_id());
1595 EXPECT_EQ(60.0, result.FrameRate()); 2007 EXPECT_EQ(60.0, result.FrameRate());
1596 EXPECT_GE(1080, result.Height()); 2008 EXPECT_GE(1080, result.Height());
2009 CheckTrackAdapterSettingsEqualsFormat(result);
1597 } 2010 }
1598 2011
1599 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, AdvancedDeviceID) { 2012 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, AdvancedDeviceID) {
1600 constraint_factory_.Reset(); 2013 constraint_factory_.Reset();
1601 blink::WebMediaTrackConstraintSet& advanced1 = 2014 blink::WebMediaTrackConstraintSet& advanced1 =
1602 constraint_factory_.AddAdvanced(); 2015 constraint_factory_.AddAdvanced();
1603 blink::WebString id_vector1[] = {blink::WebString::fromASCII(kDeviceID1), 2016 blink::WebString id_vector1[] = {blink::WebString::fromASCII(kDeviceID1),
1604 blink::WebString::fromASCII(kDeviceID2)}; 2017 blink::WebString::fromASCII(kDeviceID2)};
1605 advanced1.deviceId.setExact( 2018 advanced1.deviceId.setExact(
1606 blink::WebVector<blink::WebString>(id_vector1, arraysize(id_vector1))); 2019 blink::WebVector<blink::WebString>(id_vector1, arraysize(id_vector1)));
1607 blink::WebString id_vector2[] = {blink::WebString::fromASCII(kDeviceID2), 2020 blink::WebString id_vector2[] = {blink::WebString::fromASCII(kDeviceID2),
1608 blink::WebString::fromASCII(kDeviceID3)}; 2021 blink::WebString::fromASCII(kDeviceID3)};
1609 blink::WebMediaTrackConstraintSet& advanced2 = 2022 blink::WebMediaTrackConstraintSet& advanced2 =
1610 constraint_factory_.AddAdvanced(); 2023 constraint_factory_.AddAdvanced();
1611 advanced2.deviceId.setExact( 2024 advanced2.deviceId.setExact(
1612 blink::WebVector<blink::WebString>(id_vector2, arraysize(id_vector2))); 2025 blink::WebVector<blink::WebString>(id_vector2, arraysize(id_vector2)));
1613 auto result = SelectSettings(); 2026 auto result = SelectSettings();
1614 EXPECT_TRUE(result.HasValue()); 2027 EXPECT_TRUE(result.HasValue());
1615 // kDeviceID2 must be selected because it is the only one that satisfies both 2028 // kDeviceID2 must be selected because it is the only one that satisfies both
1616 // advanced sets. 2029 // advanced sets.
1617 EXPECT_EQ(std::string(kDeviceID2), result.device_id()); 2030 EXPECT_EQ(std::string(kDeviceID2), result.device_id());
2031 CheckTrackAdapterSettingsEqualsFormat(result);
1618 } 2032 }
1619 2033
1620 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 2034 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1621 AdvancedContradictoryDeviceID) { 2035 AdvancedContradictoryDeviceID) {
1622 constraint_factory_.Reset(); 2036 constraint_factory_.Reset();
1623 blink::WebMediaTrackConstraintSet& advanced1 = 2037 blink::WebMediaTrackConstraintSet& advanced1 =
1624 constraint_factory_.AddAdvanced(); 2038 constraint_factory_.AddAdvanced();
1625 blink::WebString id_vector1[] = {blink::WebString::fromASCII(kDeviceID1), 2039 blink::WebString id_vector1[] = {blink::WebString::fromASCII(kDeviceID1),
1626 blink::WebString::fromASCII(kDeviceID2)}; 2040 blink::WebString::fromASCII(kDeviceID2)};
1627 advanced1.deviceId.setExact( 2041 advanced1.deviceId.setExact(
1628 blink::WebVector<blink::WebString>(id_vector1, arraysize(id_vector1))); 2042 blink::WebVector<blink::WebString>(id_vector1, arraysize(id_vector1)));
1629 blink::WebString id_vector2[] = {blink::WebString::fromASCII(kDeviceID3), 2043 blink::WebString id_vector2[] = {blink::WebString::fromASCII(kDeviceID3),
1630 blink::WebString::fromASCII(kDeviceID4)}; 2044 blink::WebString::fromASCII(kDeviceID4)};
1631 blink::WebMediaTrackConstraintSet& advanced2 = 2045 blink::WebMediaTrackConstraintSet& advanced2 =
1632 constraint_factory_.AddAdvanced(); 2046 constraint_factory_.AddAdvanced();
1633 advanced2.deviceId.setExact( 2047 advanced2.deviceId.setExact(
1634 blink::WebVector<blink::WebString>(id_vector2, arraysize(id_vector2))); 2048 blink::WebVector<blink::WebString>(id_vector2, arraysize(id_vector2)));
1635 auto result = SelectSettings(); 2049 auto result = SelectSettings();
1636 EXPECT_TRUE(result.HasValue()); 2050 EXPECT_TRUE(result.HasValue());
1637 // The second advanced set must be ignored because it contradicts the first 2051 // The second advanced set must be ignored because it contradicts the first
1638 // set. 2052 // set.
1639 EXPECT_EQ(std::string(kDeviceID1), result.device_id()); 2053 EXPECT_EQ(std::string(kDeviceID1), result.device_id());
2054 CheckTrackAdapterSettingsEqualsFormat(result);
1640 } 2055 }
1641 2056
1642 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, 2057 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest,
1643 AdvancedContradictoryPowerLineFrequency) { 2058 AdvancedContradictoryPowerLineFrequency) {
1644 { 2059 {
1645 constraint_factory_.Reset(); 2060 constraint_factory_.Reset();
1646 blink::WebMediaTrackConstraintSet& advanced1 = 2061 blink::WebMediaTrackConstraintSet& advanced1 =
1647 constraint_factory_.AddAdvanced(); 2062 constraint_factory_.AddAdvanced();
1648 advanced1.width.setMin(640); 2063 advanced1.width.setMin(640);
1649 advanced1.height.setMin(480); 2064 advanced1.height.setMin(480);
1650 advanced1.googPowerLineFrequency.setExact(50); 2065 advanced1.googPowerLineFrequency.setExact(50);
1651 blink::WebMediaTrackConstraintSet& advanced2 = 2066 blink::WebMediaTrackConstraintSet& advanced2 =
1652 constraint_factory_.AddAdvanced(); 2067 constraint_factory_.AddAdvanced();
1653 advanced2.width.setMin(1920); 2068 advanced2.width.setMin(1920);
1654 advanced2.height.setMin(1080); 2069 advanced2.height.setMin(1080);
1655 advanced2.googPowerLineFrequency.setExact(60); 2070 advanced2.googPowerLineFrequency.setExact(60);
1656 auto result = SelectSettings(); 2071 auto result = SelectSettings();
1657 EXPECT_TRUE(result.HasValue()); 2072 EXPECT_TRUE(result.HasValue());
1658 // The second advanced set cannot be satisfied because it contradicts the 2073 // The second advanced set cannot be satisfied because it contradicts the
1659 // first set. The default device supports the first set and should be 2074 // first set. The default device supports the first set and should be
1660 // selected. 2075 // selected.
1661 EXPECT_EQ(default_device_->device_id, result.device_id()); 2076 EXPECT_EQ(default_device_->device_id, result.device_id());
1662 EXPECT_LE(640, result.Width()); 2077 EXPECT_LE(640, result.Width());
1663 EXPECT_LE(480, result.Height()); 2078 EXPECT_LE(480, result.Height());
1664 EXPECT_EQ(50, static_cast<int>(result.PowerLineFrequency())); 2079 EXPECT_EQ(50, static_cast<int>(result.PowerLineFrequency()));
2080 EXPECT_EQ(result.Width(), result.track_adapter_settings().max_width);
2081 EXPECT_EQ(result.Height(), result.track_adapter_settings().max_height);
2082 EXPECT_EQ(640.0 / result.Height(),
2083 result.track_adapter_settings().min_aspect_ratio);
2084 EXPECT_EQ(result.Width() / 480.0,
2085 result.track_adapter_settings().max_aspect_ratio);
2086 CheckTrackAdapterSettingsEqualsFrameRate(result);
1665 } 2087 }
1666 } 2088 }
1667 2089
1668 // The "NoDevices" tests verify that the algorithm returns the expected result 2090 // The "NoDevices" tests verify that the algorithm returns the expected result
1669 // when there are no candidates to choose from. 2091 // when there are no candidates to choose from.
1670 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, NoDevicesNoConstraints) { 2092 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, NoDevicesNoConstraints) {
1671 constraint_factory_.Reset(); 2093 constraint_factory_.Reset();
1672 VideoDeviceCaptureCapabilities capabilities; 2094 VideoDeviceCaptureCapabilities capabilities;
1673 auto result = SelectVideoDeviceCaptureSourceSettings( 2095 auto result = SelectSettingsVideoDeviceCapture(
1674 capabilities, constraint_factory_.CreateWebMediaConstraints()); 2096 capabilities, constraint_factory_.CreateWebMediaConstraints());
1675 EXPECT_FALSE(result.HasValue()); 2097 EXPECT_FALSE(result.HasValue());
1676 EXPECT_TRUE(std::string(result.failed_constraint_name()).empty()); 2098 EXPECT_TRUE(std::string(result.failed_constraint_name()).empty());
1677 } 2099 }
1678 2100
1679 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, NoDevicesWithConstraints) { 2101 TEST_F(MediaStreamConstraintsUtilVideoDeviceTest, NoDevicesWithConstraints) {
1680 constraint_factory_.Reset(); 2102 constraint_factory_.Reset();
1681 constraint_factory_.basic().height.setExact(100); 2103 constraint_factory_.basic().height.setExact(100);
1682 VideoDeviceCaptureCapabilities capabilities; 2104 VideoDeviceCaptureCapabilities capabilities;
1683 auto result = SelectVideoDeviceCaptureSourceSettings( 2105 auto result = SelectSettingsVideoDeviceCapture(
1684 capabilities, constraint_factory_.CreateWebMediaConstraints()); 2106 capabilities, constraint_factory_.CreateWebMediaConstraints());
1685 EXPECT_FALSE(result.HasValue()); 2107 EXPECT_FALSE(result.HasValue());
1686 EXPECT_TRUE(std::string(result.failed_constraint_name()).empty()); 2108 EXPECT_TRUE(std::string(result.failed_constraint_name()).empty());
1687 } 2109 }
1688 2110
1689 } // namespace content 2111 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698