1 /*
2  *  Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "media/base/video_adapter.h"
12 
13 #include <limits>
14 #include <memory>
15 #include <string>
16 #include <utility>
17 
18 #include "api/video/video_frame.h"
19 #include "api/video/video_source_interface.h"
20 #include "media/base/fake_frame_source.h"
21 #include "rtc_base/arraysize.h"
22 #include "rtc_base/time_utils.h"
23 #include "test/field_trial.h"
24 #include "test/gtest.h"
25 
26 namespace cricket {
27 namespace {
28 const int kWidth = 1280;
29 const int kHeight = 720;
30 const int kDefaultFps = 30;
31 
BuildSinkWants(absl::optional<int> target_pixel_count,int max_pixel_count,int max_framerate_fps,int sink_alignment=1)32 rtc::VideoSinkWants BuildSinkWants(absl::optional<int> target_pixel_count,
33                                    int max_pixel_count,
34                                    int max_framerate_fps,
35                                    int sink_alignment = 1) {
36   rtc::VideoSinkWants wants;
37   wants.target_pixel_count = target_pixel_count;
38   wants.max_pixel_count = max_pixel_count;
39   wants.max_framerate_fps = max_framerate_fps;
40   wants.resolution_alignment = sink_alignment;
41   return wants;
42 }
43 
44 }  // namespace
45 
46 class VideoAdapterTest : public ::testing::Test,
47                          public ::testing::WithParamInterface<bool> {
48  public:
VideoAdapterTest()49   VideoAdapterTest() : VideoAdapterTest("", 1) {}
VideoAdapterTest(const std::string & field_trials,int source_resolution_alignment)50   explicit VideoAdapterTest(const std::string& field_trials,
51                             int source_resolution_alignment)
52       : override_field_trials_(field_trials),
53         frame_source_(std::make_unique<FakeFrameSource>(
54             kWidth,
55             kHeight,
56             VideoFormat::FpsToInterval(kDefaultFps) /
57                 rtc::kNumNanosecsPerMicrosec)),
58         adapter_(source_resolution_alignment),
59         adapter_wrapper_(std::make_unique<VideoAdapterWrapper>(&adapter_)),
60         use_new_format_request_(GetParam()) {}
61 
62  protected:
63   // Wrap a VideoAdapter and collect stats.
64   class VideoAdapterWrapper {
65    public:
66     struct Stats {
67       int captured_frames = 0;
68       int dropped_frames = 0;
69       bool last_adapt_was_no_op = false;
70 
71       int cropped_width = 0;
72       int cropped_height = 0;
73       int out_width = 0;
74       int out_height = 0;
75     };
76 
VideoAdapterWrapper(VideoAdapter * adapter)77     explicit VideoAdapterWrapper(VideoAdapter* adapter)
78         : video_adapter_(adapter) {}
79 
AdaptFrame(const webrtc::VideoFrame & frame)80     void AdaptFrame(const webrtc::VideoFrame& frame) {
81       const int in_width = frame.width();
82       const int in_height = frame.height();
83       int cropped_width;
84       int cropped_height;
85       int out_width;
86       int out_height;
87       if (video_adapter_->AdaptFrameResolution(
88               in_width, in_height,
89               frame.timestamp_us() * rtc::kNumNanosecsPerMicrosec,
90               &cropped_width, &cropped_height, &out_width, &out_height)) {
91         stats_.cropped_width = cropped_width;
92         stats_.cropped_height = cropped_height;
93         stats_.out_width = out_width;
94         stats_.out_height = out_height;
95         stats_.last_adapt_was_no_op =
96             (in_width == cropped_width && in_height == cropped_height &&
97              in_width == out_width && in_height == out_height);
98       } else {
99         ++stats_.dropped_frames;
100       }
101       ++stats_.captured_frames;
102     }
103 
GetStats() const104     Stats GetStats() const { return stats_; }
105 
106    private:
107     VideoAdapter* video_adapter_;
108     Stats stats_;
109   };
110 
VerifyAdaptedResolution(const VideoAdapterWrapper::Stats & stats,int cropped_width,int cropped_height,int out_width,int out_height)111   void VerifyAdaptedResolution(const VideoAdapterWrapper::Stats& stats,
112                                int cropped_width,
113                                int cropped_height,
114                                int out_width,
115                                int out_height) {
116     EXPECT_EQ(cropped_width, stats.cropped_width);
117     EXPECT_EQ(cropped_height, stats.cropped_height);
118     EXPECT_EQ(out_width, stats.out_width);
119     EXPECT_EQ(out_height, stats.out_height);
120   }
121 
OnOutputFormatRequest(int width,int height,const absl::optional<int> & fps)122   void OnOutputFormatRequest(int width,
123                              int height,
124                              const absl::optional<int>& fps) {
125     if (use_new_format_request_) {
126       absl::optional<std::pair<int, int>> target_aspect_ratio =
127           std::make_pair(width, height);
128       absl::optional<int> max_pixel_count = width * height;
129       absl::optional<int> max_fps = fps;
130       adapter_.OnOutputFormatRequest(target_aspect_ratio, max_pixel_count,
131                                      max_fps);
132       return;
133     }
134     adapter_.OnOutputFormatRequest(
135         VideoFormat(width, height, fps ? VideoFormat::FpsToInterval(*fps) : 0,
136                     cricket::FOURCC_I420));
137   }
138 
139   webrtc::test::ScopedFieldTrials override_field_trials_;
140   const std::unique_ptr<FakeFrameSource> frame_source_;
141   VideoAdapter adapter_;
142   int cropped_width_;
143   int cropped_height_;
144   int out_width_;
145   int out_height_;
146   const std::unique_ptr<VideoAdapterWrapper> adapter_wrapper_;
147   const bool use_new_format_request_;
148 };
149 
150 class VideoAdapterTestVariableStartScale : public VideoAdapterTest {
151  public:
VideoAdapterTestVariableStartScale()152   VideoAdapterTestVariableStartScale()
153       : VideoAdapterTest("WebRTC-Video-VariableStartScaleFactor/Enabled/",
154                          /*source_resolution_alignment=*/1) {}
155 };
156 
157 INSTANTIATE_TEST_SUITE_P(OnOutputFormatRequests,
158                          VideoAdapterTest,
159                          ::testing::Values(true, false));
160 
161 INSTANTIATE_TEST_SUITE_P(OnOutputFormatRequests,
162                          VideoAdapterTestVariableStartScale,
163                          ::testing::Values(true, false));
164 
165 // Do not adapt the frame rate or the resolution. Expect no frame drop, no
166 // cropping, and no resolution change.
TEST_P(VideoAdapterTest,AdaptNothing)167 TEST_P(VideoAdapterTest, AdaptNothing) {
168   for (int i = 0; i < 10; ++i)
169     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
170 
171   // Verify no frame drop and no resolution change.
172   VideoAdapterWrapper::Stats stats = adapter_wrapper_->GetStats();
173   EXPECT_GE(stats.captured_frames, 10);
174   EXPECT_EQ(0, stats.dropped_frames);
175   VerifyAdaptedResolution(stats, kWidth, kHeight, kWidth, kHeight);
176   EXPECT_TRUE(stats.last_adapt_was_no_op);
177 }
178 
TEST_P(VideoAdapterTest,AdaptZeroInterval)179 TEST_P(VideoAdapterTest, AdaptZeroInterval) {
180   OnOutputFormatRequest(kWidth, kHeight, absl::nullopt);
181   for (int i = 0; i < 40; ++i)
182     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
183 
184   // Verify no crash and that frames aren't dropped.
185   VideoAdapterWrapper::Stats stats = adapter_wrapper_->GetStats();
186   EXPECT_GE(stats.captured_frames, 40);
187   EXPECT_EQ(0, stats.dropped_frames);
188   VerifyAdaptedResolution(stats, kWidth, kHeight, kWidth, kHeight);
189 }
190 
191 // Adapt the frame rate to be half of the capture rate at the beginning. Expect
192 // the number of dropped frames to be half of the number the captured frames.
TEST_P(VideoAdapterTest,AdaptFramerateToHalf)193 TEST_P(VideoAdapterTest, AdaptFramerateToHalf) {
194   OnOutputFormatRequest(kWidth, kHeight, kDefaultFps / 2);
195 
196   // Capture 10 frames and verify that every other frame is dropped. The first
197   // frame should not be dropped.
198   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
199   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 1);
200   EXPECT_EQ(0, adapter_wrapper_->GetStats().dropped_frames);
201 
202   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
203   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 2);
204   EXPECT_EQ(1, adapter_wrapper_->GetStats().dropped_frames);
205 
206   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
207   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 3);
208   EXPECT_EQ(1, adapter_wrapper_->GetStats().dropped_frames);
209 
210   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
211   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 4);
212   EXPECT_EQ(2, adapter_wrapper_->GetStats().dropped_frames);
213 
214   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
215   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 5);
216   EXPECT_EQ(2, adapter_wrapper_->GetStats().dropped_frames);
217 
218   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
219   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 6);
220   EXPECT_EQ(3, adapter_wrapper_->GetStats().dropped_frames);
221 
222   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
223   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 7);
224   EXPECT_EQ(3, adapter_wrapper_->GetStats().dropped_frames);
225 
226   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
227   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 8);
228   EXPECT_EQ(4, adapter_wrapper_->GetStats().dropped_frames);
229 
230   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
231   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 9);
232   EXPECT_EQ(4, adapter_wrapper_->GetStats().dropped_frames);
233 
234   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
235   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 10);
236   EXPECT_EQ(5, adapter_wrapper_->GetStats().dropped_frames);
237 }
238 
239 // Adapt the frame rate to be two thirds of the capture rate at the beginning.
240 // Expect the number of dropped frames to be one thirds of the number the
241 // captured frames.
TEST_P(VideoAdapterTest,AdaptFramerateToTwoThirds)242 TEST_P(VideoAdapterTest, AdaptFramerateToTwoThirds) {
243   OnOutputFormatRequest(kWidth, kHeight, kDefaultFps * 2 / 3);
244 
245   // Capture 10 frames and verify that every third frame is dropped. The first
246   // frame should not be dropped.
247   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
248   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 1);
249   EXPECT_EQ(0, adapter_wrapper_->GetStats().dropped_frames);
250 
251   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
252   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 2);
253   EXPECT_EQ(0, adapter_wrapper_->GetStats().dropped_frames);
254 
255   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
256   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 3);
257   EXPECT_EQ(1, adapter_wrapper_->GetStats().dropped_frames);
258 
259   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
260   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 4);
261   EXPECT_EQ(1, adapter_wrapper_->GetStats().dropped_frames);
262 
263   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
264   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 5);
265   EXPECT_EQ(1, adapter_wrapper_->GetStats().dropped_frames);
266 
267   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
268   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 6);
269   EXPECT_EQ(2, adapter_wrapper_->GetStats().dropped_frames);
270 
271   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
272   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 7);
273   EXPECT_EQ(2, adapter_wrapper_->GetStats().dropped_frames);
274 
275   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
276   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 8);
277   EXPECT_EQ(2, adapter_wrapper_->GetStats().dropped_frames);
278 
279   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
280   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 9);
281   EXPECT_EQ(3, adapter_wrapper_->GetStats().dropped_frames);
282 
283   adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
284   EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, 10);
285   EXPECT_EQ(3, adapter_wrapper_->GetStats().dropped_frames);
286 }
287 
288 // Request frame rate twice as high as captured frame rate. Expect no frame
289 // drop.
TEST_P(VideoAdapterTest,AdaptFramerateHighLimit)290 TEST_P(VideoAdapterTest, AdaptFramerateHighLimit) {
291   OnOutputFormatRequest(kWidth, kHeight, kDefaultFps * 2);
292 
293   for (int i = 0; i < 10; ++i)
294     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
295 
296   // Verify no frame drop.
297   EXPECT_EQ(0, adapter_wrapper_->GetStats().dropped_frames);
298 }
299 
300 // Adapt the frame rate to be half of the capture rate. No resolution limit set.
301 // Expect the number of dropped frames to be half of the number the captured
302 // frames.
TEST_P(VideoAdapterTest,AdaptFramerateToHalfWithNoPixelLimit)303 TEST_P(VideoAdapterTest, AdaptFramerateToHalfWithNoPixelLimit) {
304   adapter_.OnOutputFormatRequest(absl::nullopt, absl::nullopt, kDefaultFps / 2);
305 
306   // Capture 10 frames and verify that every other frame is dropped. The first
307   // frame should not be dropped.
308   int expected_dropped_frames = 0;
309   for (int i = 0; i < 10; ++i) {
310     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
311     EXPECT_GE(adapter_wrapper_->GetStats().captured_frames, i + 1);
312     if (i % 2 == 1)
313       ++expected_dropped_frames;
314     EXPECT_EQ(expected_dropped_frames,
315               adapter_wrapper_->GetStats().dropped_frames);
316     VerifyAdaptedResolution(adapter_wrapper_->GetStats(), kWidth, kHeight,
317                             kWidth, kHeight);
318   }
319 }
320 
321 // After the first timestamp, add a big offset to the timestamps. Expect that
322 // the adapter is conservative and resets to the new offset and does not drop
323 // any frame.
TEST_P(VideoAdapterTest,AdaptFramerateTimestampOffset)324 TEST_P(VideoAdapterTest, AdaptFramerateTimestampOffset) {
325   const int64_t capture_interval = VideoFormat::FpsToInterval(kDefaultFps);
326   OnOutputFormatRequest(640, 480, kDefaultFps);
327 
328   const int64_t first_timestamp = 0;
329   adapter_.AdaptFrameResolution(640, 480, first_timestamp, &cropped_width_,
330                                 &cropped_height_, &out_width_, &out_height_);
331   EXPECT_GT(out_width_, 0);
332   EXPECT_GT(out_height_, 0);
333 
334   const int64_t big_offset = -987654321LL * 1000;
335   const int64_t second_timestamp = big_offset;
336   adapter_.AdaptFrameResolution(640, 480, second_timestamp, &cropped_width_,
337                                 &cropped_height_, &out_width_, &out_height_);
338   EXPECT_GT(out_width_, 0);
339   EXPECT_GT(out_height_, 0);
340 
341   const int64_t third_timestamp = big_offset + capture_interval;
342   adapter_.AdaptFrameResolution(640, 480, third_timestamp, &cropped_width_,
343                                 &cropped_height_, &out_width_, &out_height_);
344   EXPECT_GT(out_width_, 0);
345   EXPECT_GT(out_height_, 0);
346 }
347 
348 // Request 30 fps and send 30 fps with jitter. Expect that no frame is dropped.
TEST_P(VideoAdapterTest,AdaptFramerateTimestampJitter)349 TEST_P(VideoAdapterTest, AdaptFramerateTimestampJitter) {
350   const int64_t capture_interval = VideoFormat::FpsToInterval(kDefaultFps);
351   OnOutputFormatRequest(640, 480, kDefaultFps);
352 
353   adapter_.AdaptFrameResolution(640, 480, capture_interval * 0 / 10,
354                                 &cropped_width_, &cropped_height_, &out_width_,
355                                 &out_height_);
356   EXPECT_GT(out_width_, 0);
357   EXPECT_GT(out_height_, 0);
358 
359   adapter_.AdaptFrameResolution(640, 480, capture_interval * 10 / 10 - 1,
360                                 &cropped_width_, &cropped_height_, &out_width_,
361                                 &out_height_);
362   EXPECT_GT(out_width_, 0);
363   EXPECT_GT(out_height_, 0);
364 
365   adapter_.AdaptFrameResolution(640, 480, capture_interval * 25 / 10,
366                                 &cropped_width_, &cropped_height_, &out_width_,
367                                 &out_height_);
368   EXPECT_GT(out_width_, 0);
369   EXPECT_GT(out_height_, 0);
370 
371   adapter_.AdaptFrameResolution(640, 480, capture_interval * 30 / 10,
372                                 &cropped_width_, &cropped_height_, &out_width_,
373                                 &out_height_);
374   EXPECT_GT(out_width_, 0);
375   EXPECT_GT(out_height_, 0);
376 
377   adapter_.AdaptFrameResolution(640, 480, capture_interval * 35 / 10,
378                                 &cropped_width_, &cropped_height_, &out_width_,
379                                 &out_height_);
380   EXPECT_GT(out_width_, 0);
381   EXPECT_GT(out_height_, 0);
382 
383   adapter_.AdaptFrameResolution(640, 480, capture_interval * 50 / 10,
384                                 &cropped_width_, &cropped_height_, &out_width_,
385                                 &out_height_);
386   EXPECT_GT(out_width_, 0);
387   EXPECT_GT(out_height_, 0);
388 }
389 
390 // Adapt the frame rate to be half of the capture rate after capturing no less
391 // than 10 frames. Expect no frame dropped before adaptation and frame dropped
392 // after adaptation.
TEST_P(VideoAdapterTest,AdaptFramerateOntheFly)393 TEST_P(VideoAdapterTest, AdaptFramerateOntheFly) {
394   OnOutputFormatRequest(kWidth, kHeight, kDefaultFps);
395   for (int i = 0; i < 10; ++i)
396     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
397 
398   // Verify no frame drop before adaptation.
399   EXPECT_EQ(0, adapter_wrapper_->GetStats().dropped_frames);
400 
401   // Adapt the frame rate.
402   OnOutputFormatRequest(kWidth, kHeight, kDefaultFps / 2);
403   for (int i = 0; i < 20; ++i)
404     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
405 
406   // Verify frame drop after adaptation.
407   EXPECT_GT(adapter_wrapper_->GetStats().dropped_frames, 0);
408 }
409 
410 // Do not adapt the frame rate or the resolution. Expect no frame drop, no
411 // cropping, and no resolution change.
TEST_P(VideoAdapterTest,AdaptFramerateRequestMax)412 TEST_P(VideoAdapterTest, AdaptFramerateRequestMax) {
413   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt,
414                                       std::numeric_limits<int>::max(),
415                                       std::numeric_limits<int>::max()));
416 
417   for (int i = 0; i < 10; ++i)
418     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
419 
420   // Verify no frame drop and no resolution change.
421   VideoAdapterWrapper::Stats stats = adapter_wrapper_->GetStats();
422   EXPECT_GE(stats.captured_frames, 10);
423   EXPECT_EQ(0, stats.dropped_frames);
424   VerifyAdaptedResolution(stats, kWidth, kHeight, kWidth, kHeight);
425   EXPECT_TRUE(stats.last_adapt_was_no_op);
426 }
427 
TEST_P(VideoAdapterTest,AdaptFramerateRequestZero)428 TEST_P(VideoAdapterTest, AdaptFramerateRequestZero) {
429   adapter_.OnSinkWants(
430       BuildSinkWants(absl::nullopt, std::numeric_limits<int>::max(), 0));
431   for (int i = 0; i < 10; ++i)
432     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
433 
434   // Verify no crash and that frames aren't dropped.
435   VideoAdapterWrapper::Stats stats = adapter_wrapper_->GetStats();
436   EXPECT_GE(stats.captured_frames, 10);
437   EXPECT_EQ(10, stats.dropped_frames);
438 }
439 
440 // Adapt the frame rate to be half of the capture rate at the beginning. Expect
441 // the number of dropped frames to be half of the number the captured frames.
TEST_P(VideoAdapterTest,AdaptFramerateRequestHalf)442 TEST_P(VideoAdapterTest, AdaptFramerateRequestHalf) {
443   adapter_.OnSinkWants(BuildSinkWants(
444       absl::nullopt, std::numeric_limits<int>::max(), kDefaultFps / 2));
445   for (int i = 0; i < 10; ++i)
446     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
447 
448   // Verify no crash and that frames aren't dropped.
449   VideoAdapterWrapper::Stats stats = adapter_wrapper_->GetStats();
450   EXPECT_GE(stats.captured_frames, 10);
451   EXPECT_EQ(5, stats.dropped_frames);
452   VerifyAdaptedResolution(stats, kWidth, kHeight, kWidth, kHeight);
453 }
454 
455 // Set a very high output pixel resolution. Expect no cropping or resolution
456 // change.
TEST_P(VideoAdapterTest,AdaptFrameResolutionHighLimit)457 TEST_P(VideoAdapterTest, AdaptFrameResolutionHighLimit) {
458   OnOutputFormatRequest(kWidth * 10, kHeight * 10, kDefaultFps);
459   EXPECT_TRUE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0, &cropped_width_,
460                                             &cropped_height_, &out_width_,
461                                             &out_height_));
462   EXPECT_EQ(kWidth, cropped_width_);
463   EXPECT_EQ(kHeight, cropped_height_);
464   EXPECT_EQ(kWidth, out_width_);
465   EXPECT_EQ(kHeight, out_height_);
466 }
467 
468 // Adapt the frame resolution to be the same as capture resolution. Expect no
469 // cropping or resolution change.
TEST_P(VideoAdapterTest,AdaptFrameResolutionIdentical)470 TEST_P(VideoAdapterTest, AdaptFrameResolutionIdentical) {
471   OnOutputFormatRequest(kWidth, kHeight, kDefaultFps);
472   EXPECT_TRUE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0, &cropped_width_,
473                                             &cropped_height_, &out_width_,
474                                             &out_height_));
475   EXPECT_EQ(kWidth, cropped_width_);
476   EXPECT_EQ(kHeight, cropped_height_);
477   EXPECT_EQ(kWidth, out_width_);
478   EXPECT_EQ(kHeight, out_height_);
479 }
480 
481 // Adapt the frame resolution to be a quarter of the capture resolution. Expect
482 // no cropping, but a resolution change.
TEST_P(VideoAdapterTest,AdaptFrameResolutionQuarter)483 TEST_P(VideoAdapterTest, AdaptFrameResolutionQuarter) {
484   OnOutputFormatRequest(kWidth / 2, kHeight / 2, kDefaultFps);
485   EXPECT_TRUE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0, &cropped_width_,
486                                             &cropped_height_, &out_width_,
487                                             &out_height_));
488   EXPECT_EQ(kWidth, cropped_width_);
489   EXPECT_EQ(kHeight, cropped_height_);
490   EXPECT_EQ(kWidth / 2, out_width_);
491   EXPECT_EQ(kHeight / 2, out_height_);
492 }
493 
494 // Adapt the pixel resolution to 0. Expect frame drop.
TEST_P(VideoAdapterTest,AdaptFrameResolutionDrop)495 TEST_P(VideoAdapterTest, AdaptFrameResolutionDrop) {
496   OnOutputFormatRequest(kWidth * 0, kHeight * 0, kDefaultFps);
497   EXPECT_FALSE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0,
498                                              &cropped_width_, &cropped_height_,
499                                              &out_width_, &out_height_));
500 }
501 
502 // Adapt the frame resolution to be a quarter of the capture resolution at the
503 // beginning. Expect no cropping but a resolution change.
TEST_P(VideoAdapterTest,AdaptResolution)504 TEST_P(VideoAdapterTest, AdaptResolution) {
505   OnOutputFormatRequest(kWidth / 2, kHeight / 2, kDefaultFps);
506   for (int i = 0; i < 10; ++i)
507     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
508 
509   // Verify no frame drop, no cropping, and resolution change.
510   VideoAdapterWrapper::Stats stats = adapter_wrapper_->GetStats();
511   EXPECT_EQ(0, stats.dropped_frames);
512   VerifyAdaptedResolution(stats, kWidth, kHeight, kWidth / 2, kHeight / 2);
513 }
514 
515 // Adapt the frame resolution to be a quarter of the capture resolution after
516 // capturing no less than 10 frames. Expect no resolution change before
517 // adaptation and resolution change after adaptation.
TEST_P(VideoAdapterTest,AdaptResolutionOnTheFly)518 TEST_P(VideoAdapterTest, AdaptResolutionOnTheFly) {
519   OnOutputFormatRequest(kWidth, kHeight, kDefaultFps);
520   for (int i = 0; i < 10; ++i)
521     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
522 
523   // Verify no resolution change before adaptation.
524   VerifyAdaptedResolution(adapter_wrapper_->GetStats(), kWidth, kHeight, kWidth,
525                           kHeight);
526 
527   // Adapt the frame resolution.
528   OnOutputFormatRequest(kWidth / 2, kHeight / 2, kDefaultFps);
529   for (int i = 0; i < 10; ++i)
530     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
531 
532   // Verify resolution change after adaptation.
533   VerifyAdaptedResolution(adapter_wrapper_->GetStats(), kWidth, kHeight,
534                           kWidth / 2, kHeight / 2);
535 }
536 
537 // Drop all frames for resolution 0x0.
TEST_P(VideoAdapterTest,DropAllFrames)538 TEST_P(VideoAdapterTest, DropAllFrames) {
539   OnOutputFormatRequest(kWidth * 0, kHeight * 0, kDefaultFps);
540   for (int i = 0; i < 10; ++i)
541     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
542 
543   // Verify all frames are dropped.
544   VideoAdapterWrapper::Stats stats = adapter_wrapper_->GetStats();
545   EXPECT_GE(stats.captured_frames, 10);
546   EXPECT_EQ(stats.captured_frames, stats.dropped_frames);
547 }
548 
TEST_P(VideoAdapterTest,TestOnOutputFormatRequest)549 TEST_P(VideoAdapterTest, TestOnOutputFormatRequest) {
550   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
551                                             &cropped_height_, &out_width_,
552                                             &out_height_));
553   EXPECT_EQ(640, cropped_width_);
554   EXPECT_EQ(400, cropped_height_);
555   EXPECT_EQ(640, out_width_);
556   EXPECT_EQ(400, out_height_);
557 
558   // Format request 640x400.
559   OnOutputFormatRequest(640, 400, absl::nullopt);
560   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
561                                             &cropped_height_, &out_width_,
562                                             &out_height_));
563   EXPECT_EQ(640, cropped_width_);
564   EXPECT_EQ(400, cropped_height_);
565   EXPECT_EQ(640, out_width_);
566   EXPECT_EQ(400, out_height_);
567 
568   // Request 1280x720, higher than input, but aspect 16:9. Expect cropping but
569   // no scaling.
570   OnOutputFormatRequest(1280, 720, absl::nullopt);
571   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
572                                             &cropped_height_, &out_width_,
573                                             &out_height_));
574   EXPECT_EQ(640, cropped_width_);
575   EXPECT_EQ(360, cropped_height_);
576   EXPECT_EQ(640, out_width_);
577   EXPECT_EQ(360, out_height_);
578 
579   // Request 0x0.
580   OnOutputFormatRequest(0, 0, absl::nullopt);
581   EXPECT_FALSE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
582                                              &cropped_height_, &out_width_,
583                                              &out_height_));
584 
585   // Request 320x200. Expect scaling, but no cropping.
586   OnOutputFormatRequest(320, 200, absl::nullopt);
587   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
588                                             &cropped_height_, &out_width_,
589                                             &out_height_));
590   EXPECT_EQ(640, cropped_width_);
591   EXPECT_EQ(400, cropped_height_);
592   EXPECT_EQ(320, out_width_);
593   EXPECT_EQ(200, out_height_);
594 
595   // Request resolution close to 2/3 scale. Expect adapt down. Scaling to 2/3
596   // is not optimized and not allowed, therefore 1/2 scaling will be used
597   // instead.
598   OnOutputFormatRequest(424, 265, absl::nullopt);
599   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
600                                             &cropped_height_, &out_width_,
601                                             &out_height_));
602   EXPECT_EQ(640, cropped_width_);
603   EXPECT_EQ(400, cropped_height_);
604   EXPECT_EQ(320, out_width_);
605   EXPECT_EQ(200, out_height_);
606 
607   // Request resolution of 3 / 8. Expect adapt down.
608   OnOutputFormatRequest(640 * 3 / 8, 400 * 3 / 8, absl::nullopt);
609   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
610                                             &cropped_height_, &out_width_,
611                                             &out_height_));
612   EXPECT_EQ(640, cropped_width_);
613   EXPECT_EQ(400, cropped_height_);
614   EXPECT_EQ(640 * 3 / 8, out_width_);
615   EXPECT_EQ(400 * 3 / 8, out_height_);
616 
617   // Switch back up. Expect adapt.
618   OnOutputFormatRequest(320, 200, absl::nullopt);
619   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
620                                             &cropped_height_, &out_width_,
621                                             &out_height_));
622   EXPECT_EQ(640, cropped_width_);
623   EXPECT_EQ(400, cropped_height_);
624   EXPECT_EQ(320, out_width_);
625   EXPECT_EQ(200, out_height_);
626 
627   // Format request 480x300.
628   OnOutputFormatRequest(480, 300, absl::nullopt);
629   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
630                                             &cropped_height_, &out_width_,
631                                             &out_height_));
632   EXPECT_EQ(640, cropped_width_);
633   EXPECT_EQ(400, cropped_height_);
634   EXPECT_EQ(480, out_width_);
635   EXPECT_EQ(300, out_height_);
636 }
637 
TEST_P(VideoAdapterTest,TestViewRequestPlusCameraSwitch)638 TEST_P(VideoAdapterTest, TestViewRequestPlusCameraSwitch) {
639   // Start at HD.
640   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
641                                             &cropped_height_, &out_width_,
642                                             &out_height_));
643   EXPECT_EQ(1280, cropped_width_);
644   EXPECT_EQ(720, cropped_height_);
645   EXPECT_EQ(1280, out_width_);
646   EXPECT_EQ(720, out_height_);
647 
648   // Format request for VGA.
649   OnOutputFormatRequest(640, 360, absl::nullopt);
650   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
651                                             &cropped_height_, &out_width_,
652                                             &out_height_));
653   EXPECT_EQ(1280, cropped_width_);
654   EXPECT_EQ(720, cropped_height_);
655   EXPECT_EQ(640, out_width_);
656   EXPECT_EQ(360, out_height_);
657 
658   // Now, the camera reopens at VGA.
659   // Both the frame and the output format should be 640x360.
660   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 360, 0, &cropped_width_,
661                                             &cropped_height_, &out_width_,
662                                             &out_height_));
663   EXPECT_EQ(640, cropped_width_);
664   EXPECT_EQ(360, cropped_height_);
665   EXPECT_EQ(640, out_width_);
666   EXPECT_EQ(360, out_height_);
667 
668   // And another view request comes in for 640x360, which should have no
669   // real impact.
670   OnOutputFormatRequest(640, 360, absl::nullopt);
671   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 360, 0, &cropped_width_,
672                                             &cropped_height_, &out_width_,
673                                             &out_height_));
674   EXPECT_EQ(640, cropped_width_);
675   EXPECT_EQ(360, cropped_height_);
676   EXPECT_EQ(640, out_width_);
677   EXPECT_EQ(360, out_height_);
678 }
679 
TEST_P(VideoAdapterTest,TestVgaWidth)680 TEST_P(VideoAdapterTest, TestVgaWidth) {
681   // Requested output format is 640x360.
682   OnOutputFormatRequest(640, 360, absl::nullopt);
683 
684   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
685                                             &cropped_height_, &out_width_,
686                                             &out_height_));
687   // Expect cropping.
688   EXPECT_EQ(640, cropped_width_);
689   EXPECT_EQ(360, cropped_height_);
690   EXPECT_EQ(640, out_width_);
691   EXPECT_EQ(360, out_height_);
692 
693   // But if frames come in at 640x360, we shouldn't adapt them down.
694   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 360, 0, &cropped_width_,
695                                             &cropped_height_, &out_width_,
696                                             &out_height_));
697   EXPECT_EQ(640, cropped_width_);
698   EXPECT_EQ(360, cropped_height_);
699   EXPECT_EQ(640, out_width_);
700   EXPECT_EQ(360, out_height_);
701 
702   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
703                                             &cropped_height_, &out_width_,
704                                             &out_height_));
705   EXPECT_EQ(640, cropped_width_);
706   EXPECT_EQ(360, cropped_height_);
707   EXPECT_EQ(640, out_width_);
708   EXPECT_EQ(360, out_height_);
709 }
710 
TEST_P(VideoAdapterTest,TestOnResolutionRequestInSmallSteps)711 TEST_P(VideoAdapterTest, TestOnResolutionRequestInSmallSteps) {
712   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
713                                             &cropped_height_, &out_width_,
714                                             &out_height_));
715   EXPECT_EQ(1280, cropped_width_);
716   EXPECT_EQ(720, cropped_height_);
717   EXPECT_EQ(1280, out_width_);
718   EXPECT_EQ(720, out_height_);
719 
720   // Adapt down one step.
721   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 1280 * 720 - 1,
722                                       std::numeric_limits<int>::max()));
723   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
724                                             &cropped_height_, &out_width_,
725                                             &out_height_));
726   EXPECT_EQ(1280, cropped_width_);
727   EXPECT_EQ(720, cropped_height_);
728   EXPECT_EQ(960, out_width_);
729   EXPECT_EQ(540, out_height_);
730 
731   // Adapt down one step more.
732   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 960 * 540 - 1,
733                                       std::numeric_limits<int>::max()));
734   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
735                                             &cropped_height_, &out_width_,
736                                             &out_height_));
737   EXPECT_EQ(1280, cropped_width_);
738   EXPECT_EQ(720, cropped_height_);
739   EXPECT_EQ(640, out_width_);
740   EXPECT_EQ(360, out_height_);
741 
742   // Adapt down one step more.
743   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 640 * 360 - 1,
744                                       std::numeric_limits<int>::max()));
745   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
746                                             &cropped_height_, &out_width_,
747                                             &out_height_));
748   EXPECT_EQ(1280, cropped_width_);
749   EXPECT_EQ(720, cropped_height_);
750   EXPECT_EQ(480, out_width_);
751   EXPECT_EQ(270, out_height_);
752 
753   // Adapt up one step.
754   adapter_.OnSinkWants(
755       BuildSinkWants(640 * 360, 960 * 540, std::numeric_limits<int>::max()));
756   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
757                                             &cropped_height_, &out_width_,
758                                             &out_height_));
759   EXPECT_EQ(1280, cropped_width_);
760   EXPECT_EQ(720, cropped_height_);
761   EXPECT_EQ(640, out_width_);
762   EXPECT_EQ(360, out_height_);
763 
764   // Adapt up one step more.
765   adapter_.OnSinkWants(
766       BuildSinkWants(960 * 540, 1280 * 720, std::numeric_limits<int>::max()));
767   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
768                                             &cropped_height_, &out_width_,
769                                             &out_height_));
770   EXPECT_EQ(1280, cropped_width_);
771   EXPECT_EQ(720, cropped_height_);
772   EXPECT_EQ(960, out_width_);
773   EXPECT_EQ(540, out_height_);
774 
775   // Adapt up one step more.
776   adapter_.OnSinkWants(
777       BuildSinkWants(1280 * 720, 1920 * 1080, std::numeric_limits<int>::max()));
778   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
779                                             &cropped_height_, &out_width_,
780                                             &out_height_));
781   EXPECT_EQ(1280, cropped_width_);
782   EXPECT_EQ(720, cropped_height_);
783   EXPECT_EQ(1280, out_width_);
784   EXPECT_EQ(720, out_height_);
785 }
786 
TEST_P(VideoAdapterTest,TestOnResolutionRequestMaxZero)787 TEST_P(VideoAdapterTest, TestOnResolutionRequestMaxZero) {
788   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
789                                             &cropped_height_, &out_width_,
790                                             &out_height_));
791   EXPECT_EQ(1280, cropped_width_);
792   EXPECT_EQ(720, cropped_height_);
793   EXPECT_EQ(1280, out_width_);
794   EXPECT_EQ(720, out_height_);
795 
796   adapter_.OnSinkWants(
797       BuildSinkWants(absl::nullopt, 0, std::numeric_limits<int>::max()));
798   EXPECT_FALSE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
799                                              &cropped_height_, &out_width_,
800                                              &out_height_));
801 }
802 
TEST_P(VideoAdapterTest,TestOnResolutionRequestInLargeSteps)803 TEST_P(VideoAdapterTest, TestOnResolutionRequestInLargeSteps) {
804   // Large step down.
805   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 640 * 360 - 1,
806                                       std::numeric_limits<int>::max()));
807   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
808                                             &cropped_height_, &out_width_,
809                                             &out_height_));
810   EXPECT_EQ(1280, cropped_width_);
811   EXPECT_EQ(720, cropped_height_);
812   EXPECT_EQ(480, out_width_);
813   EXPECT_EQ(270, out_height_);
814 
815   // Large step up.
816   adapter_.OnSinkWants(
817       BuildSinkWants(1280 * 720, 1920 * 1080, std::numeric_limits<int>::max()));
818   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
819                                             &cropped_height_, &out_width_,
820                                             &out_height_));
821   EXPECT_EQ(1280, cropped_width_);
822   EXPECT_EQ(720, cropped_height_);
823   EXPECT_EQ(1280, out_width_);
824   EXPECT_EQ(720, out_height_);
825 }
826 
TEST_P(VideoAdapterTest,TestOnOutputFormatRequestCapsMaxResolution)827 TEST_P(VideoAdapterTest, TestOnOutputFormatRequestCapsMaxResolution) {
828   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 640 * 360 - 1,
829                                       std::numeric_limits<int>::max()));
830   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
831                                             &cropped_height_, &out_width_,
832                                             &out_height_));
833   EXPECT_EQ(1280, cropped_width_);
834   EXPECT_EQ(720, cropped_height_);
835   EXPECT_EQ(480, out_width_);
836   EXPECT_EQ(270, out_height_);
837 
838   OnOutputFormatRequest(640, 360, absl::nullopt);
839   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
840                                             &cropped_height_, &out_width_,
841                                             &out_height_));
842   EXPECT_EQ(1280, cropped_width_);
843   EXPECT_EQ(720, cropped_height_);
844   EXPECT_EQ(480, out_width_);
845   EXPECT_EQ(270, out_height_);
846 
847   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 960 * 720,
848                                       std::numeric_limits<int>::max()));
849   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
850                                             &cropped_height_, &out_width_,
851                                             &out_height_));
852   EXPECT_EQ(1280, cropped_width_);
853   EXPECT_EQ(720, cropped_height_);
854   EXPECT_EQ(640, out_width_);
855   EXPECT_EQ(360, out_height_);
856 }
857 
TEST_P(VideoAdapterTest,TestOnResolutionRequestReset)858 TEST_P(VideoAdapterTest, TestOnResolutionRequestReset) {
859   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
860                                             &cropped_height_, &out_width_,
861                                             &out_height_));
862   EXPECT_EQ(1280, cropped_width_);
863   EXPECT_EQ(720, cropped_height_);
864   EXPECT_EQ(1280, out_width_);
865   EXPECT_EQ(720, out_height_);
866 
867   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 640 * 360 - 1,
868                                       std::numeric_limits<int>::max()));
869   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
870                                             &cropped_height_, &out_width_,
871                                             &out_height_));
872   EXPECT_EQ(1280, cropped_width_);
873   EXPECT_EQ(720, cropped_height_);
874   EXPECT_EQ(480, out_width_);
875   EXPECT_EQ(270, out_height_);
876 
877   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt,
878                                       std::numeric_limits<int>::max(),
879                                       std::numeric_limits<int>::max()));
880   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
881                                             &cropped_height_, &out_width_,
882                                             &out_height_));
883   EXPECT_EQ(1280, cropped_width_);
884   EXPECT_EQ(720, cropped_height_);
885   EXPECT_EQ(1280, out_width_);
886   EXPECT_EQ(720, out_height_);
887 }
888 
TEST_P(VideoAdapterTest,TestOnOutputFormatRequestResolutionReset)889 TEST_P(VideoAdapterTest, TestOnOutputFormatRequestResolutionReset) {
890   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
891                                             &cropped_height_, &out_width_,
892                                             &out_height_));
893   EXPECT_EQ(1280, cropped_width_);
894   EXPECT_EQ(720, cropped_height_);
895   EXPECT_EQ(1280, out_width_);
896   EXPECT_EQ(720, out_height_);
897 
898   adapter_.OnOutputFormatRequest(absl::nullopt, 640 * 360 - 1, absl::nullopt);
899   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
900                                             &cropped_height_, &out_width_,
901                                             &out_height_));
902   EXPECT_EQ(1280, cropped_width_);
903   EXPECT_EQ(720, cropped_height_);
904   EXPECT_EQ(480, out_width_);
905   EXPECT_EQ(270, out_height_);
906 
907   adapter_.OnOutputFormatRequest(absl::nullopt, absl::nullopt, absl::nullopt);
908   EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, &cropped_width_,
909                                             &cropped_height_, &out_width_,
910                                             &out_height_));
911   EXPECT_EQ(1280, cropped_width_);
912   EXPECT_EQ(720, cropped_height_);
913   EXPECT_EQ(1280, out_width_);
914   EXPECT_EQ(720, out_height_);
915 }
916 
TEST_P(VideoAdapterTest,TestOnOutputFormatRequestFpsReset)917 TEST_P(VideoAdapterTest, TestOnOutputFormatRequestFpsReset) {
918   OnOutputFormatRequest(kWidth, kHeight, kDefaultFps / 2);
919   for (int i = 0; i < 10; ++i)
920     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
921 
922   // Verify frame drop.
923   const int dropped_frames = adapter_wrapper_->GetStats().dropped_frames;
924   EXPECT_GT(dropped_frames, 0);
925 
926   // Reset frame rate.
927   OnOutputFormatRequest(kWidth, kHeight, absl::nullopt);
928   for (int i = 0; i < 20; ++i)
929     adapter_wrapper_->AdaptFrame(frame_source_->GetFrame());
930 
931   // Verify no frame drop after reset.
932   EXPECT_EQ(dropped_frames, adapter_wrapper_->GetStats().dropped_frames);
933 }
934 
TEST_P(VideoAdapterTest,RequestAspectRatio)935 TEST_P(VideoAdapterTest, RequestAspectRatio) {
936   // Request aspect ratio 320/180 (16:9), smaller than input, but no resolution
937   // limit. Expect cropping but no scaling.
938   adapter_.OnOutputFormatRequest(std::make_pair(320, 180), absl::nullopt,
939                                  absl::nullopt);
940   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
941                                             &cropped_height_, &out_width_,
942                                             &out_height_));
943   EXPECT_EQ(640, cropped_width_);
944   EXPECT_EQ(360, cropped_height_);
945   EXPECT_EQ(640, out_width_);
946   EXPECT_EQ(360, out_height_);
947 }
948 
TEST_P(VideoAdapterTest,RequestAspectRatioWithDifferentOrientation)949 TEST_P(VideoAdapterTest, RequestAspectRatioWithDifferentOrientation) {
950   // Request 720x1280, higher than input, but aspect 16:9. Orientation should
951   // not matter, expect cropping but no scaling.
952   OnOutputFormatRequest(720, 1280, absl::nullopt);
953   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
954                                             &cropped_height_, &out_width_,
955                                             &out_height_));
956   EXPECT_EQ(640, cropped_width_);
957   EXPECT_EQ(360, cropped_height_);
958   EXPECT_EQ(640, out_width_);
959   EXPECT_EQ(360, out_height_);
960 }
961 
TEST_P(VideoAdapterTest,InvalidAspectRatioIgnored)962 TEST_P(VideoAdapterTest, InvalidAspectRatioIgnored) {
963   // Request aspect ratio 320/0. Expect no cropping.
964   adapter_.OnOutputFormatRequest(std::make_pair(320, 0), absl::nullopt,
965                                  absl::nullopt);
966   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, &cropped_width_,
967                                             &cropped_height_, &out_width_,
968                                             &out_height_));
969   EXPECT_EQ(640, cropped_width_);
970   EXPECT_EQ(400, cropped_height_);
971   EXPECT_EQ(640, out_width_);
972   EXPECT_EQ(400, out_height_);
973 }
974 
TEST_P(VideoAdapterTest,TestCroppingWithResolutionRequest)975 TEST_P(VideoAdapterTest, TestCroppingWithResolutionRequest) {
976   // Ask for 640x360 (16:9 aspect).
977   OnOutputFormatRequest(640, 360, absl::nullopt);
978   // Send 640x480 (4:3 aspect).
979   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
980                                             &cropped_height_, &out_width_,
981                                             &out_height_));
982   // Expect cropping to 16:9 format and no scaling.
983   EXPECT_EQ(640, cropped_width_);
984   EXPECT_EQ(360, cropped_height_);
985   EXPECT_EQ(640, out_width_);
986   EXPECT_EQ(360, out_height_);
987 
988   // Adapt down one step.
989   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 640 * 360 - 1,
990                                       std::numeric_limits<int>::max()));
991   // Expect cropping to 16:9 format and 3/4 scaling.
992   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
993                                             &cropped_height_, &out_width_,
994                                             &out_height_));
995   EXPECT_EQ(640, cropped_width_);
996   EXPECT_EQ(360, cropped_height_);
997   EXPECT_EQ(480, out_width_);
998   EXPECT_EQ(270, out_height_);
999 
1000   // Adapt down one step more.
1001   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 480 * 270 - 1,
1002                                       std::numeric_limits<int>::max()));
1003   // Expect cropping to 16:9 format and 1/2 scaling.
1004   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
1005                                             &cropped_height_, &out_width_,
1006                                             &out_height_));
1007   EXPECT_EQ(640, cropped_width_);
1008   EXPECT_EQ(360, cropped_height_);
1009   EXPECT_EQ(320, out_width_);
1010   EXPECT_EQ(180, out_height_);
1011 
1012   // Adapt up one step.
1013   adapter_.OnSinkWants(
1014       BuildSinkWants(480 * 270, 640 * 360, std::numeric_limits<int>::max()));
1015   // Expect cropping to 16:9 format and 3/4 scaling.
1016   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
1017                                             &cropped_height_, &out_width_,
1018                                             &out_height_));
1019   EXPECT_EQ(640, cropped_width_);
1020   EXPECT_EQ(360, cropped_height_);
1021   EXPECT_EQ(480, out_width_);
1022   EXPECT_EQ(270, out_height_);
1023 
1024   // Adapt up one step more.
1025   adapter_.OnSinkWants(
1026       BuildSinkWants(640 * 360, 960 * 540, std::numeric_limits<int>::max()));
1027   // Expect cropping to 16:9 format and no scaling.
1028   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
1029                                             &cropped_height_, &out_width_,
1030                                             &out_height_));
1031   EXPECT_EQ(640, cropped_width_);
1032   EXPECT_EQ(360, cropped_height_);
1033   EXPECT_EQ(640, out_width_);
1034   EXPECT_EQ(360, out_height_);
1035 
1036   // Try to adapt up one step more.
1037   adapter_.OnSinkWants(
1038       BuildSinkWants(960 * 540, 1280 * 720, std::numeric_limits<int>::max()));
1039   // Expect cropping to 16:9 format and no scaling.
1040   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
1041                                             &cropped_height_, &out_width_,
1042                                             &out_height_));
1043   EXPECT_EQ(640, cropped_width_);
1044   EXPECT_EQ(360, cropped_height_);
1045   EXPECT_EQ(640, out_width_);
1046   EXPECT_EQ(360, out_height_);
1047 }
1048 
TEST_P(VideoAdapterTest,TestCroppingOddResolution)1049 TEST_P(VideoAdapterTest, TestCroppingOddResolution) {
1050   // Ask for 640x360 (16:9 aspect), with 3/16 scaling.
1051   OnOutputFormatRequest(640, 360, absl::nullopt);
1052   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt,
1053                                       640 * 360 * 3 / 16 * 3 / 16,
1054                                       std::numeric_limits<int>::max()));
1055 
1056   // Send 640x480 (4:3 aspect).
1057   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, &cropped_width_,
1058                                             &cropped_height_, &out_width_,
1059                                             &out_height_));
1060 
1061   // Instead of getting the exact aspect ratio with cropped resolution 640x360,
1062   // the resolution should be adjusted to get a perfect scale factor instead.
1063   EXPECT_EQ(640, cropped_width_);
1064   EXPECT_EQ(368, cropped_height_);
1065   EXPECT_EQ(120, out_width_);
1066   EXPECT_EQ(69, out_height_);
1067 }
1068 
TEST_P(VideoAdapterTest,TestAdaptToVerySmallResolution)1069 TEST_P(VideoAdapterTest, TestAdaptToVerySmallResolution) {
1070   // Ask for 1920x1080 (16:9 aspect), with 1/16 scaling.
1071   const int w = 1920;
1072   const int h = 1080;
1073   OnOutputFormatRequest(w, h, absl::nullopt);
1074   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, w * h * 1 / 16 * 1 / 16,
1075                                       std::numeric_limits<int>::max()));
1076 
1077   // Send 1920x1080 (16:9 aspect).
1078   EXPECT_TRUE(adapter_.AdaptFrameResolution(
1079       w, h, 0, &cropped_width_, &cropped_height_, &out_width_, &out_height_));
1080 
1081   // Instead of getting the exact aspect ratio with cropped resolution 1920x1080
1082   // the resolution should be adjusted to get a perfect scale factor instead.
1083   EXPECT_EQ(1920, cropped_width_);
1084   EXPECT_EQ(1072, cropped_height_);
1085   EXPECT_EQ(120, out_width_);
1086   EXPECT_EQ(67, out_height_);
1087 
1088   // Adapt back up one step to 3/32.
1089   adapter_.OnSinkWants(BuildSinkWants(w * h * 3 / 32 * 3 / 32,
1090                                       w * h * 1 / 8 * 1 / 8,
1091                                       std::numeric_limits<int>::max()));
1092 
1093   // Send 1920x1080 (16:9 aspect).
1094   EXPECT_TRUE(adapter_.AdaptFrameResolution(
1095       w, h, 0, &cropped_width_, &cropped_height_, &out_width_, &out_height_));
1096 
1097   EXPECT_EQ(180, out_width_);
1098   EXPECT_EQ(99, out_height_);
1099 }
1100 
TEST_P(VideoAdapterTest,AdaptFrameResolutionDropWithResolutionRequest)1101 TEST_P(VideoAdapterTest, AdaptFrameResolutionDropWithResolutionRequest) {
1102   OnOutputFormatRequest(0, 0, kDefaultFps);
1103   EXPECT_FALSE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0,
1104                                              &cropped_width_, &cropped_height_,
1105                                              &out_width_, &out_height_));
1106 
1107   adapter_.OnSinkWants(BuildSinkWants(960 * 540,
1108                                       std::numeric_limits<int>::max(),
1109                                       std::numeric_limits<int>::max()));
1110 
1111   // Still expect all frames to be dropped
1112   EXPECT_FALSE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0,
1113                                              &cropped_width_, &cropped_height_,
1114                                              &out_width_, &out_height_));
1115 
1116   adapter_.OnSinkWants(BuildSinkWants(absl::nullopt, 640 * 480 - 1,
1117                                       std::numeric_limits<int>::max()));
1118 
1119   // Still expect all frames to be dropped
1120   EXPECT_FALSE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0,
1121                                              &cropped_width_, &cropped_height_,
1122                                              &out_width_, &out_height_));
1123 }
1124 
1125 // Test that we will adapt to max given a target pixel count close to max.
TEST_P(VideoAdapterTest,TestAdaptToMax)1126 TEST_P(VideoAdapterTest, TestAdaptToMax) {
1127   OnOutputFormatRequest(640, 360, kDefaultFps);
1128   adapter_.OnSinkWants(BuildSinkWants(640 * 360 - 1 /* target */,
1129                                       std::numeric_limits<int>::max(),
1130                                       std::numeric_limits<int>::max()));
1131 
1132   EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 360, 0, &cropped_width_,
1133                                             &cropped_height_, &out_width_,
1134                                             &out_height_));
1135   EXPECT_EQ(640, out_width_);
1136   EXPECT_EQ(360, out_height_);
1137 }
1138 
1139 // Test adjusting to 16:9 in landscape, and 9:16 in portrait.
TEST(VideoAdapterTestMultipleOrientation,TestNormal)1140 TEST(VideoAdapterTestMultipleOrientation, TestNormal) {
1141   VideoAdapter video_adapter;
1142   video_adapter.OnOutputFormatRequest(std::make_pair(640, 360), 640 * 360,
1143                                       std::make_pair(360, 640), 360 * 640, 30);
1144 
1145   int cropped_width;
1146   int cropped_height;
1147   int out_width;
1148   int out_height;
1149   EXPECT_TRUE(video_adapter.AdaptFrameResolution(
1150       /* in_width= */ 640, /* in_height= */ 480, /* in_timestamp_ns= */ 0,
1151       &cropped_width, &cropped_height, &out_width, &out_height));
1152   EXPECT_EQ(640, cropped_width);
1153   EXPECT_EQ(360, cropped_height);
1154   EXPECT_EQ(640, out_width);
1155   EXPECT_EQ(360, out_height);
1156 
1157   EXPECT_TRUE(video_adapter.AdaptFrameResolution(
1158       /* in_width= */ 480, /* in_height= */ 640,
1159       /* in_timestamp_ns= */ rtc::kNumNanosecsPerSec / 30, &cropped_width,
1160       &cropped_height, &out_width, &out_height));
1161   EXPECT_EQ(360, cropped_width);
1162   EXPECT_EQ(640, cropped_height);
1163   EXPECT_EQ(360, out_width);
1164   EXPECT_EQ(640, out_height);
1165 }
1166 
1167 // Force output to be 9:16, even for landscape input.
TEST(VideoAdapterTestMultipleOrientation,TestForcePortrait)1168 TEST(VideoAdapterTestMultipleOrientation, TestForcePortrait) {
1169   VideoAdapter video_adapter;
1170   video_adapter.OnOutputFormatRequest(std::make_pair(360, 640), 640 * 360,
1171                                       std::make_pair(360, 640), 360 * 640, 30);
1172 
1173   int cropped_width;
1174   int cropped_height;
1175   int out_width;
1176   int out_height;
1177   EXPECT_TRUE(video_adapter.AdaptFrameResolution(
1178       /* in_width= */ 640, /* in_height= */ 480, /* in_timestamp_ns= */ 0,
1179       &cropped_width, &cropped_height, &out_width, &out_height));
1180   EXPECT_EQ(270, cropped_width);
1181   EXPECT_EQ(480, cropped_height);
1182   EXPECT_EQ(270, out_width);
1183   EXPECT_EQ(480, out_height);
1184 
1185   EXPECT_TRUE(video_adapter.AdaptFrameResolution(
1186       /* in_width= */ 480, /* in_height= */ 640,
1187       /* in_timestamp_ns= */ rtc::kNumNanosecsPerSec / 30, &cropped_width,
1188       &cropped_height, &out_width, &out_height));
1189   EXPECT_EQ(360, cropped_width);
1190   EXPECT_EQ(640, cropped_height);
1191   EXPECT_EQ(360, out_width);
1192   EXPECT_EQ(640, out_height);
1193 }
1194 
TEST_P(VideoAdapterTest,AdaptResolutionInSteps)1195 TEST_P(VideoAdapterTest, AdaptResolutionInSteps) {
1196   const int kWidth = 1280;
1197   const int kHeight = 720;
1198   OnOutputFormatRequest(kWidth, kHeight, absl::nullopt);  // 16:9 aspect.
1199 
1200   // Scale factors: 3/4, 2/3, 3/4, 2/3, ...
1201   // Scale        : 3/4, 1/2, 3/8, 1/4, 3/16, 1/8.
1202   const int kExpectedWidths[] = {960, 640, 480, 320, 240, 160};
1203   const int kExpectedHeights[] = {540, 360, 270, 180, 135, 90};
1204 
1205   int request_width = kWidth;
1206   int request_height = kHeight;
1207 
1208   for (size_t i = 0; i < arraysize(kExpectedWidths); ++i) {
1209     // Adapt down one step.
1210     adapter_.OnSinkWants(BuildSinkWants(absl::nullopt,
1211                                         request_width * request_height - 1,
1212                                         std::numeric_limits<int>::max()));
1213     EXPECT_TRUE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0,
1214                                               &cropped_width_, &cropped_height_,
1215                                               &out_width_, &out_height_));
1216     EXPECT_EQ(kExpectedWidths[i], out_width_);
1217     EXPECT_EQ(kExpectedHeights[i], out_height_);
1218     request_width = out_width_;
1219     request_height = out_height_;
1220   }
1221 }
1222 
1223 // Scale factors are 3/4, 2/3, 3/4, 2/3, ... (see test above).
1224 // In VideoAdapterTestVariableStartScale, first scale factor depends on
1225 // resolution. May start with:
1226 // - 2/3 (if width/height multiple of 3) or
1227 // - 2/3, 2/3 (if width/height multiple of 9).
TEST_P(VideoAdapterTestVariableStartScale,AdaptResolutionInStepsFirst3_4)1228 TEST_P(VideoAdapterTestVariableStartScale, AdaptResolutionInStepsFirst3_4) {
1229   const int kWidth = 1280;
1230   const int kHeight = 720;
1231   OnOutputFormatRequest(kWidth, kHeight, absl::nullopt);  // 16:9 aspect.
1232 
1233   // Scale factors: 3/4, 2/3, 3/4, 2/3, ...
1234   // Scale        : 3/4, 1/2, 3/8, 1/4, 3/16, 1/8.
1235   const int kExpectedWidths[] = {960, 640, 480, 320, 240, 160};
1236   const int kExpectedHeights[] = {540, 360, 270, 180, 135, 90};
1237 
1238   int request_width = kWidth;
1239   int request_height = kHeight;
1240 
1241   for (size_t i = 0; i < arraysize(kExpectedWidths); ++i) {
1242     // Adapt down one step.
1243     adapter_.OnSinkWants(BuildSinkWants(absl::nullopt,
1244                                         request_width * request_height - 1,
1245                                         std::numeric_limits<int>::max()));
1246     EXPECT_TRUE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0,
1247                                               &cropped_width_, &cropped_height_,
1248                                               &out_width_, &out_height_));
1249     EXPECT_EQ(kExpectedWidths[i], out_width_);
1250     EXPECT_EQ(kExpectedHeights[i], out_height_);
1251     request_width = out_width_;
1252     request_height = out_height_;
1253   }
1254 }
1255 
TEST_P(VideoAdapterTestVariableStartScale,AdaptResolutionInStepsFirst2_3)1256 TEST_P(VideoAdapterTestVariableStartScale, AdaptResolutionInStepsFirst2_3) {
1257   const int kWidth = 1920;
1258   const int kHeight = 1080;
1259   OnOutputFormatRequest(kWidth, kHeight, absl::nullopt);  // 16:9 aspect.
1260 
1261   // Scale factors: 2/3, 3/4, 2/3, 3/4, ...
1262   // Scale:         2/3, 1/2, 1/3, 1/4, 1/6, 1/8, 1/12.
1263   const int kExpectedWidths[] = {1280, 960, 640, 480, 320, 240, 160};
1264   const int kExpectedHeights[] = {720, 540, 360, 270, 180, 135, 90};
1265 
1266   int request_width = kWidth;
1267   int request_height = kHeight;
1268 
1269   for (size_t i = 0; i < arraysize(kExpectedWidths); ++i) {
1270     // Adapt down one step.
1271     adapter_.OnSinkWants(BuildSinkWants(absl::nullopt,
1272                                         request_width * request_height - 1,
1273                                         std::numeric_limits<int>::max()));
1274     EXPECT_TRUE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0,
1275                                               &cropped_width_, &cropped_height_,
1276                                               &out_width_, &out_height_));
1277     EXPECT_EQ(kExpectedWidths[i], out_width_);
1278     EXPECT_EQ(kExpectedHeights[i], out_height_);
1279     request_width = out_width_;
1280     request_height = out_height_;
1281   }
1282 }
1283 
TEST_P(VideoAdapterTestVariableStartScale,AdaptResolutionInStepsFirst2x2_3)1284 TEST_P(VideoAdapterTestVariableStartScale, AdaptResolutionInStepsFirst2x2_3) {
1285   const int kWidth = 1440;
1286   const int kHeight = 1080;
1287   OnOutputFormatRequest(kWidth, kHeight, absl::nullopt);  // 4:3 aspect.
1288 
1289   // Scale factors: 2/3, 2/3, 3/4, 2/3, 3/4, ...
1290   // Scale        : 2/3, 4/9, 1/3, 2/9, 1/6, 1/9, 1/12, 1/18, 1/24, 1/36.
1291   const int kExpectedWidths[] = {960, 640, 480, 320, 240, 160, 120, 80, 60, 40};
1292   const int kExpectedHeights[] = {720, 480, 360, 240, 180, 120, 90, 60, 45, 30};
1293 
1294   int request_width = kWidth;
1295   int request_height = kHeight;
1296 
1297   for (size_t i = 0; i < arraysize(kExpectedWidths); ++i) {
1298     // Adapt down one step.
1299     adapter_.OnSinkWants(BuildSinkWants(absl::nullopt,
1300                                         request_width * request_height - 1,
1301                                         std::numeric_limits<int>::max()));
1302     EXPECT_TRUE(adapter_.AdaptFrameResolution(kWidth, kHeight, 0,
1303                                               &cropped_width_, &cropped_height_,
1304                                               &out_width_, &out_height_));
1305     EXPECT_EQ(kExpectedWidths[i], out_width_);
1306     EXPECT_EQ(kExpectedHeights[i], out_height_);
1307     request_width = out_width_;
1308     request_height = out_height_;
1309   }
1310 }
1311 
TEST_P(VideoAdapterTest,AdaptResolutionWithSinkAlignment)1312 TEST_P(VideoAdapterTest, AdaptResolutionWithSinkAlignment) {
1313   constexpr int kSourceWidth = 1280;
1314   constexpr int kSourceHeight = 720;
1315   constexpr int kSourceFramerate = 30;
1316   constexpr int kRequestedWidth = 480;
1317   constexpr int kRequestedHeight = 270;
1318   constexpr int kRequestedFramerate = 30;
1319 
1320   OnOutputFormatRequest(kRequestedWidth, kRequestedHeight, kRequestedFramerate);
1321 
1322   int frame_num = 1;
1323   for (const int sink_alignment : {2, 3, 4, 5}) {
1324     adapter_.OnSinkWants(
1325         BuildSinkWants(absl::nullopt, std::numeric_limits<int>::max(),
1326                        std::numeric_limits<int>::max(), sink_alignment));
1327     EXPECT_TRUE(adapter_.AdaptFrameResolution(
1328         kSourceWidth, kSourceHeight,
1329         frame_num * rtc::kNumNanosecsPerSec / kSourceFramerate, &cropped_width_,
1330         &cropped_height_, &out_width_, &out_height_));
1331     EXPECT_EQ(out_width_ % sink_alignment, 0);
1332     EXPECT_EQ(out_height_ % sink_alignment, 0);
1333 
1334     ++frame_num;
1335   }
1336 }
1337 
1338 class VideoAdapterWithSourceAlignmentTest : public VideoAdapterTest {
1339  protected:
1340   static constexpr int kSourceResolutionAlignment = 7;
1341 
VideoAdapterWithSourceAlignmentTest()1342   VideoAdapterWithSourceAlignmentTest()
1343       : VideoAdapterTest(/*field_trials=*/"", kSourceResolutionAlignment) {}
1344 };
1345 
TEST_P(VideoAdapterWithSourceAlignmentTest,AdaptResolution)1346 TEST_P(VideoAdapterWithSourceAlignmentTest, AdaptResolution) {
1347   constexpr int kSourceWidth = 1280;
1348   constexpr int kSourceHeight = 720;
1349   constexpr int kRequestedWidth = 480;
1350   constexpr int kRequestedHeight = 270;
1351   constexpr int kRequestedFramerate = 30;
1352 
1353   OnOutputFormatRequest(kRequestedWidth, kRequestedHeight, kRequestedFramerate);
1354 
1355   EXPECT_TRUE(adapter_.AdaptFrameResolution(
1356       kSourceWidth, kSourceHeight, /*in_timestamp_ns=*/0, &cropped_width_,
1357       &cropped_height_, &out_width_, &out_height_));
1358   EXPECT_EQ(out_width_ % kSourceResolutionAlignment, 0);
1359   EXPECT_EQ(out_height_ % kSourceResolutionAlignment, 0);
1360 }
1361 
TEST_P(VideoAdapterWithSourceAlignmentTest,AdaptResolutionWithSinkAlignment)1362 TEST_P(VideoAdapterWithSourceAlignmentTest, AdaptResolutionWithSinkAlignment) {
1363   constexpr int kSourceWidth = 1280;
1364   constexpr int kSourceHeight = 720;
1365   // 7 and 8 neither divide 480 nor 270.
1366   constexpr int kRequestedWidth = 480;
1367   constexpr int kRequestedHeight = 270;
1368   constexpr int kRequestedFramerate = 30;
1369   constexpr int kSinkResolutionAlignment = 8;
1370 
1371   OnOutputFormatRequest(kRequestedWidth, kRequestedHeight, kRequestedFramerate);
1372 
1373   adapter_.OnSinkWants(BuildSinkWants(
1374       absl::nullopt, std::numeric_limits<int>::max(),
1375       std::numeric_limits<int>::max(), kSinkResolutionAlignment));
1376   EXPECT_TRUE(adapter_.AdaptFrameResolution(
1377       kSourceWidth, kSourceHeight, /*in_timestamp_ns=*/0, &cropped_width_,
1378       &cropped_height_, &out_width_, &out_height_));
1379   EXPECT_EQ(out_width_ % kSourceResolutionAlignment, 0);
1380   EXPECT_EQ(out_height_ % kSourceResolutionAlignment, 0);
1381   EXPECT_EQ(out_width_ % kSinkResolutionAlignment, 0);
1382   EXPECT_EQ(out_height_ % kSinkResolutionAlignment, 0);
1383 }
1384 
1385 INSTANTIATE_TEST_SUITE_P(OnOutputFormatRequests,
1386                          VideoAdapterWithSourceAlignmentTest,
1387                          ::testing::Values(true, false));
1388 
1389 }  // namespace cricket
1390