buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 11 | #include <limits.h> // For INT_MAX |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 12 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 13 | #include <memory> |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 17 | #include "webrtc/media/base/fakevideocapturer.h" |
| 18 | #include "webrtc/media/base/mediachannel.h" |
| 19 | #include "webrtc/media/base/testutils.h" |
| 20 | #include "webrtc/media/base/videoadapter.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 21 | #include "webrtc/rtc_base/gunit.h" |
| 22 | #include "webrtc/rtc_base/logging.h" |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 23 | |
| 24 | namespace cricket { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | const int kDefaultFps = 30; |
| 27 | } // namespace |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 28 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 29 | class VideoAdapterTest : public testing::Test { |
| 30 | public: |
| 31 | virtual void SetUp() { |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 32 | capturer_.reset(new FakeVideoCapturer); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 33 | capture_format_ = capturer_->GetSupportedFormats()->at(0); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 34 | capture_format_.interval = VideoFormat::FpsToInterval(kDefaultFps); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 35 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 36 | listener_.reset(new VideoCapturerListener(&adapter_)); |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 37 | capturer_->AddOrUpdateSink(listener_.get(), rtc::VideoSinkWants()); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 38 | } |
| 39 | |
pbos@webrtc.org | 75c3ec1 | 2014-08-27 18:16:13 +0000 | [diff] [blame] | 40 | virtual void TearDown() { |
| 41 | // Explicitly disconnect the VideoCapturer before to avoid data races |
| 42 | // (frames delivered to VideoCapturerListener while it's being destructed). |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 43 | capturer_->RemoveSink(listener_.get()); |
pbos@webrtc.org | 75c3ec1 | 2014-08-27 18:16:13 +0000 | [diff] [blame] | 44 | } |
| 45 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 46 | protected: |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 47 | class VideoCapturerListener |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 48 | : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 49 | public: |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 50 | struct Stats { |
| 51 | int captured_frames; |
| 52 | int dropped_frames; |
| 53 | bool last_adapt_was_no_op; |
| 54 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 55 | int cropped_width; |
| 56 | int cropped_height; |
| 57 | int out_width; |
| 58 | int out_height; |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 61 | explicit VideoCapturerListener(VideoAdapter* adapter) |
| 62 | : video_adapter_(adapter), |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 63 | cropped_width_(0), |
| 64 | cropped_height_(0), |
| 65 | out_width_(0), |
| 66 | out_height_(0), |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 67 | captured_frames_(0), |
| 68 | dropped_frames_(0), |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 69 | last_adapt_was_no_op_(false) {} |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 70 | |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 71 | void OnFrame(const webrtc::VideoFrame& frame) { |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 72 | rtc::CritScope lock(&crit_); |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 73 | const int in_width = frame.width(); |
| 74 | const int in_height = frame.height(); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 75 | int cropped_width; |
| 76 | int cropped_height; |
| 77 | int out_width; |
| 78 | int out_height; |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 79 | if (video_adapter_->AdaptFrameResolution( |
| 80 | in_width, in_height, |
| 81 | frame.timestamp_us() * rtc::kNumNanosecsPerMicrosec, |
| 82 | &cropped_width, &cropped_height, &out_width, &out_height)) { |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 83 | cropped_width_ = cropped_width; |
| 84 | cropped_height_ = cropped_height; |
| 85 | out_width_ = out_width; |
| 86 | out_height_ = out_height; |
| 87 | last_adapt_was_no_op_ = |
| 88 | (in_width == cropped_width && in_height == cropped_height && |
| 89 | in_width == out_width && in_height == out_height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 90 | } else { |
| 91 | ++dropped_frames_; |
| 92 | } |
| 93 | ++captured_frames_; |
| 94 | } |
| 95 | |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 96 | Stats GetStats() { |
| 97 | rtc::CritScope lock(&crit_); |
| 98 | Stats stats; |
| 99 | stats.captured_frames = captured_frames_; |
| 100 | stats.dropped_frames = dropped_frames_; |
| 101 | stats.last_adapt_was_no_op = last_adapt_was_no_op_; |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 102 | stats.cropped_width = cropped_width_; |
| 103 | stats.cropped_height = cropped_height_; |
| 104 | stats.out_width = out_width_; |
| 105 | stats.out_height = out_height_; |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 106 | return stats; |
| 107 | } |
| 108 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 109 | private: |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 110 | rtc::CriticalSection crit_; |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 111 | VideoAdapter* video_adapter_; |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 112 | int cropped_width_; |
| 113 | int cropped_height_; |
| 114 | int out_width_; |
| 115 | int out_height_; |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 116 | int captured_frames_; |
| 117 | int dropped_frames_; |
| 118 | bool last_adapt_was_no_op_; |
| 119 | }; |
| 120 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 121 | |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 122 | void VerifyAdaptedResolution(const VideoCapturerListener::Stats& stats, |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 123 | int cropped_width, |
| 124 | int cropped_height, |
| 125 | int out_width, |
| 126 | int out_height) { |
| 127 | EXPECT_EQ(cropped_width, stats.cropped_width); |
| 128 | EXPECT_EQ(cropped_height, stats.cropped_height); |
| 129 | EXPECT_EQ(out_width, stats.out_width); |
| 130 | EXPECT_EQ(out_height, stats.out_height); |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 131 | } |
| 132 | |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 133 | std::unique_ptr<FakeVideoCapturer> capturer_; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 134 | VideoAdapter adapter_; |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 135 | int cropped_width_; |
| 136 | int cropped_height_; |
| 137 | int out_width_; |
| 138 | int out_height_; |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 139 | std::unique_ptr<VideoCapturerListener> listener_; |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 140 | VideoFormat capture_format_; |
| 141 | }; |
| 142 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 143 | // Do not adapt the frame rate or the resolution. Expect no frame drop, no |
| 144 | // cropping, and no resolution change. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 145 | TEST_F(VideoAdapterTest, AdaptNothing) { |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 146 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 147 | for (int i = 0; i < 10; ++i) |
| 148 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 149 | |
| 150 | // Verify no frame drop and no resolution change. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 151 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 152 | EXPECT_GE(stats.captured_frames, 10); |
| 153 | EXPECT_EQ(0, stats.dropped_frames); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 154 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height, |
| 155 | capture_format_.width, capture_format_.height); |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 156 | EXPECT_TRUE(stats.last_adapt_was_no_op); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST_F(VideoAdapterTest, AdaptZeroInterval) { |
| 160 | VideoFormat format = capturer_->GetSupportedFormats()->at(0); |
| 161 | format.interval = 0; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 162 | adapter_.OnOutputFormatRequest(format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 163 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 164 | for (int i = 0; i < 10; ++i) |
| 165 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 166 | |
| 167 | // Verify no crash and that frames aren't dropped. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 168 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 169 | EXPECT_GE(stats.captured_frames, 10); |
| 170 | EXPECT_EQ(0, stats.dropped_frames); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 171 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height, |
| 172 | capture_format_.width, capture_format_.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Adapt the frame rate to be half of the capture rate at the beginning. Expect |
| 176 | // the number of dropped frames to be half of the number the captured frames. |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 177 | TEST_F(VideoAdapterTest, AdaptFramerateToHalf) { |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 178 | VideoFormat request_format = capture_format_; |
| 179 | request_format.interval *= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 180 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 181 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 182 | |
| 183 | // Capture 10 frames and verify that every other frame is dropped. The first |
| 184 | // frame should not be dropped. |
| 185 | capturer_->CaptureFrame(); |
| 186 | EXPECT_GE(listener_->GetStats().captured_frames, 1); |
| 187 | EXPECT_EQ(0, listener_->GetStats().dropped_frames); |
| 188 | |
| 189 | capturer_->CaptureFrame(); |
| 190 | EXPECT_GE(listener_->GetStats().captured_frames, 2); |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 191 | EXPECT_EQ(1, listener_->GetStats().dropped_frames); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 192 | |
| 193 | capturer_->CaptureFrame(); |
| 194 | EXPECT_GE(listener_->GetStats().captured_frames, 3); |
| 195 | EXPECT_EQ(1, listener_->GetStats().dropped_frames); |
| 196 | |
| 197 | capturer_->CaptureFrame(); |
| 198 | EXPECT_GE(listener_->GetStats().captured_frames, 4); |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 199 | EXPECT_EQ(2, listener_->GetStats().dropped_frames); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 200 | |
| 201 | capturer_->CaptureFrame(); |
| 202 | EXPECT_GE(listener_->GetStats().captured_frames, 5); |
| 203 | EXPECT_EQ(2, listener_->GetStats().dropped_frames); |
| 204 | |
| 205 | capturer_->CaptureFrame(); |
| 206 | EXPECT_GE(listener_->GetStats().captured_frames, 6); |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 207 | EXPECT_EQ(3, listener_->GetStats().dropped_frames); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 208 | |
| 209 | capturer_->CaptureFrame(); |
| 210 | EXPECT_GE(listener_->GetStats().captured_frames, 7); |
| 211 | EXPECT_EQ(3, listener_->GetStats().dropped_frames); |
| 212 | |
| 213 | capturer_->CaptureFrame(); |
| 214 | EXPECT_GE(listener_->GetStats().captured_frames, 8); |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 215 | EXPECT_EQ(4, listener_->GetStats().dropped_frames); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 216 | |
| 217 | capturer_->CaptureFrame(); |
| 218 | EXPECT_GE(listener_->GetStats().captured_frames, 9); |
| 219 | EXPECT_EQ(4, listener_->GetStats().dropped_frames); |
| 220 | |
| 221 | capturer_->CaptureFrame(); |
| 222 | EXPECT_GE(listener_->GetStats().captured_frames, 10); |
nisse | f5297a0 | 2016-09-30 01:34:27 -0700 | [diff] [blame] | 223 | EXPECT_EQ(5, listener_->GetStats().dropped_frames); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Adapt the frame rate to be two thirds of the capture rate at the beginning. |
| 227 | // Expect the number of dropped frames to be one thirds of the number the |
| 228 | // captured frames. |
| 229 | TEST_F(VideoAdapterTest, AdaptFramerateToTwoThirds) { |
| 230 | VideoFormat request_format = capture_format_; |
| 231 | request_format.interval = request_format.interval * 3 / 2; |
| 232 | adapter_.OnOutputFormatRequest(request_format); |
| 233 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
| 234 | |
| 235 | // Capture 10 frames and verify that every third frame is dropped. The first |
| 236 | // frame should not be dropped. |
| 237 | capturer_->CaptureFrame(); |
| 238 | EXPECT_GE(listener_->GetStats().captured_frames, 1); |
| 239 | EXPECT_EQ(0, listener_->GetStats().dropped_frames); |
| 240 | |
| 241 | capturer_->CaptureFrame(); |
| 242 | EXPECT_GE(listener_->GetStats().captured_frames, 2); |
| 243 | EXPECT_EQ(0, listener_->GetStats().dropped_frames); |
| 244 | |
| 245 | capturer_->CaptureFrame(); |
| 246 | EXPECT_GE(listener_->GetStats().captured_frames, 3); |
| 247 | EXPECT_EQ(1, listener_->GetStats().dropped_frames); |
| 248 | |
| 249 | capturer_->CaptureFrame(); |
| 250 | EXPECT_GE(listener_->GetStats().captured_frames, 4); |
| 251 | EXPECT_EQ(1, listener_->GetStats().dropped_frames); |
| 252 | |
| 253 | capturer_->CaptureFrame(); |
| 254 | EXPECT_GE(listener_->GetStats().captured_frames, 5); |
| 255 | EXPECT_EQ(1, listener_->GetStats().dropped_frames); |
| 256 | |
| 257 | capturer_->CaptureFrame(); |
| 258 | EXPECT_GE(listener_->GetStats().captured_frames, 6); |
| 259 | EXPECT_EQ(2, listener_->GetStats().dropped_frames); |
| 260 | |
| 261 | capturer_->CaptureFrame(); |
| 262 | EXPECT_GE(listener_->GetStats().captured_frames, 7); |
| 263 | EXPECT_EQ(2, listener_->GetStats().dropped_frames); |
| 264 | |
| 265 | capturer_->CaptureFrame(); |
| 266 | EXPECT_GE(listener_->GetStats().captured_frames, 8); |
| 267 | EXPECT_EQ(2, listener_->GetStats().dropped_frames); |
| 268 | |
| 269 | capturer_->CaptureFrame(); |
| 270 | EXPECT_GE(listener_->GetStats().captured_frames, 9); |
| 271 | EXPECT_EQ(3, listener_->GetStats().dropped_frames); |
| 272 | |
| 273 | capturer_->CaptureFrame(); |
| 274 | EXPECT_GE(listener_->GetStats().captured_frames, 10); |
| 275 | EXPECT_EQ(3, listener_->GetStats().dropped_frames); |
| 276 | } |
| 277 | |
| 278 | // Request frame rate twice as high as captured frame rate. Expect no frame |
| 279 | // drop. |
| 280 | TEST_F(VideoAdapterTest, AdaptFramerateHighLimit) { |
| 281 | VideoFormat request_format = capture_format_; |
| 282 | request_format.interval /= 2; |
| 283 | adapter_.OnOutputFormatRequest(request_format); |
| 284 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 285 | for (int i = 0; i < 10; ++i) |
| 286 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 287 | |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 288 | // Verify no frame drop. |
| 289 | EXPECT_EQ(0, listener_->GetStats().dropped_frames); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 290 | } |
| 291 | |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 292 | // After the first timestamp, add a big offset to the timestamps. Expect that |
| 293 | // the adapter is conservative and resets to the new offset and does not drop |
| 294 | // any frame. |
| 295 | TEST_F(VideoAdapterTest, AdaptFramerateTimestampOffset) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 296 | const int64_t capture_interval = VideoFormat::FpsToInterval(kDefaultFps); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 297 | adapter_.OnOutputFormatRequest( |
| 298 | VideoFormat(640, 480, capture_interval, cricket::FOURCC_ANY)); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 299 | |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 300 | const int64_t first_timestamp = 0; |
| 301 | adapter_.AdaptFrameResolution(640, 480, first_timestamp, |
| 302 | &cropped_width_, &cropped_height_, |
| 303 | &out_width_, &out_height_); |
| 304 | EXPECT_GT(out_width_, 0); |
| 305 | EXPECT_GT(out_height_, 0); |
| 306 | |
| 307 | const int64_t big_offset = -987654321LL * 1000; |
| 308 | const int64_t second_timestamp = big_offset; |
| 309 | adapter_.AdaptFrameResolution(640, 480, second_timestamp, |
| 310 | &cropped_width_, &cropped_height_, |
| 311 | &out_width_, &out_height_); |
| 312 | EXPECT_GT(out_width_, 0); |
| 313 | EXPECT_GT(out_height_, 0); |
| 314 | |
| 315 | const int64_t third_timestamp = big_offset + capture_interval; |
| 316 | adapter_.AdaptFrameResolution(640, 480, third_timestamp, |
| 317 | &cropped_width_, &cropped_height_, |
| 318 | &out_width_, &out_height_); |
| 319 | EXPECT_GT(out_width_, 0); |
| 320 | EXPECT_GT(out_height_, 0); |
| 321 | } |
| 322 | |
| 323 | // Request 30 fps and send 30 fps with jitter. Expect that no frame is dropped. |
| 324 | TEST_F(VideoAdapterTest, AdaptFramerateTimestampJitter) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 325 | const int64_t capture_interval = VideoFormat::FpsToInterval(kDefaultFps); |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 326 | adapter_.OnOutputFormatRequest( |
| 327 | VideoFormat(640, 480, capture_interval, cricket::FOURCC_ANY)); |
| 328 | |
| 329 | adapter_.AdaptFrameResolution(640, 480, capture_interval * 0 / 10, |
| 330 | &cropped_width_, &cropped_height_, |
| 331 | &out_width_, &out_height_); |
| 332 | EXPECT_GT(out_width_, 0); |
| 333 | EXPECT_GT(out_height_, 0); |
| 334 | |
| 335 | adapter_.AdaptFrameResolution(640, 480, capture_interval * 10 / 10 - 1, |
| 336 | &cropped_width_, &cropped_height_, |
| 337 | &out_width_, &out_height_); |
| 338 | EXPECT_GT(out_width_, 0); |
| 339 | EXPECT_GT(out_height_, 0); |
| 340 | |
| 341 | adapter_.AdaptFrameResolution(640, 480, capture_interval * 25 / 10, |
| 342 | &cropped_width_, &cropped_height_, |
| 343 | &out_width_, &out_height_); |
| 344 | EXPECT_GT(out_width_, 0); |
| 345 | EXPECT_GT(out_height_, 0); |
| 346 | |
| 347 | adapter_.AdaptFrameResolution(640, 480, capture_interval * 30 / 10, |
| 348 | &cropped_width_, &cropped_height_, |
| 349 | &out_width_, &out_height_); |
| 350 | EXPECT_GT(out_width_, 0); |
| 351 | EXPECT_GT(out_height_, 0); |
| 352 | |
| 353 | adapter_.AdaptFrameResolution(640, 480, capture_interval * 35 / 10, |
| 354 | &cropped_width_, &cropped_height_, |
| 355 | &out_width_, &out_height_); |
| 356 | EXPECT_GT(out_width_, 0); |
| 357 | EXPECT_GT(out_height_, 0); |
| 358 | |
| 359 | adapter_.AdaptFrameResolution(640, 480, capture_interval * 50 / 10, |
| 360 | &cropped_width_, &cropped_height_, |
| 361 | &out_width_, &out_height_); |
| 362 | EXPECT_GT(out_width_, 0); |
| 363 | EXPECT_GT(out_height_, 0); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // Adapt the frame rate to be half of the capture rate after capturing no less |
| 367 | // than 10 frames. Expect no frame dropped before adaptation and frame dropped |
| 368 | // after adaptation. |
| 369 | TEST_F(VideoAdapterTest, AdaptFramerateOntheFly) { |
| 370 | VideoFormat request_format = capture_format_; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 371 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 372 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 373 | for (int i = 0; i < 10; ++i) |
| 374 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 375 | |
| 376 | // Verify no frame drop before adaptation. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 377 | EXPECT_EQ(0, listener_->GetStats().dropped_frames); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 378 | |
| 379 | // Adapat the frame rate. |
| 380 | request_format.interval *= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 381 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 382 | |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 383 | for (int i = 0; i < 20; ++i) |
| 384 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 385 | |
| 386 | // Verify frame drop after adaptation. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 387 | EXPECT_GT(listener_->GetStats().dropped_frames, 0); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 388 | } |
| 389 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 390 | // Do not adapt the frame rate or the resolution. Expect no frame drop, no |
| 391 | // cropping, and no resolution change. |
| 392 | TEST_F(VideoAdapterTest, OnFramerateRequestMax) { |
| 393 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), |
| 394 | std::numeric_limits<int>::max(), |
| 395 | std::numeric_limits<int>::max()); |
| 396 | |
| 397 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
| 398 | for (int i = 0; i < 10; ++i) |
| 399 | capturer_->CaptureFrame(); |
| 400 | |
| 401 | // Verify no frame drop and no resolution change. |
| 402 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 403 | EXPECT_GE(stats.captured_frames, 10); |
| 404 | EXPECT_EQ(0, stats.dropped_frames); |
| 405 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height, |
| 406 | capture_format_.width, capture_format_.height); |
| 407 | EXPECT_TRUE(stats.last_adapt_was_no_op); |
| 408 | } |
| 409 | |
| 410 | TEST_F(VideoAdapterTest, OnFramerateRequestZero) { |
| 411 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), |
| 412 | std::numeric_limits<int>::max(), 0); |
| 413 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
| 414 | for (int i = 0; i < 10; ++i) |
| 415 | capturer_->CaptureFrame(); |
| 416 | |
| 417 | // Verify no crash and that frames aren't dropped. |
| 418 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 419 | EXPECT_GE(stats.captured_frames, 10); |
| 420 | EXPECT_EQ(10, stats.dropped_frames); |
| 421 | } |
| 422 | |
| 423 | // Adapt the frame rate to be half of the capture rate at the beginning. Expect |
| 424 | // the number of dropped frames to be half of the number the captured frames. |
| 425 | TEST_F(VideoAdapterTest, OnFramerateRequestHalf) { |
| 426 | adapter_.OnResolutionFramerateRequest( |
| 427 | rtc::Optional<int>(), std::numeric_limits<int>::max(), kDefaultFps / 2); |
| 428 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
| 429 | for (int i = 0; i < 10; ++i) |
| 430 | capturer_->CaptureFrame(); |
| 431 | |
| 432 | // Verify no crash and that frames aren't dropped. |
| 433 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 434 | EXPECT_GE(stats.captured_frames, 10); |
| 435 | EXPECT_EQ(5, stats.dropped_frames); |
| 436 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height, |
| 437 | capture_format_.width, capture_format_.height); |
| 438 | } |
| 439 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 440 | // Set a very high output pixel resolution. Expect no cropping or resolution |
| 441 | // change. |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 442 | TEST_F(VideoAdapterTest, AdaptFrameResolutionHighLimit) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 443 | VideoFormat output_format = capture_format_; |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 444 | output_format.width *= 10; |
| 445 | output_format.height *= 10; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 446 | adapter_.OnOutputFormatRequest(output_format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 447 | EXPECT_TRUE(adapter_.AdaptFrameResolution( |
| 448 | capture_format_.width, capture_format_.height, 0, |
| 449 | &cropped_width_, &cropped_height_, |
| 450 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 451 | EXPECT_EQ(capture_format_.width, cropped_width_); |
| 452 | EXPECT_EQ(capture_format_.height, cropped_height_); |
| 453 | EXPECT_EQ(capture_format_.width, out_width_); |
| 454 | EXPECT_EQ(capture_format_.height, out_height_); |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // Adapt the frame resolution to be the same as capture resolution. Expect no |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 458 | // cropping or resolution change. |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 459 | TEST_F(VideoAdapterTest, AdaptFrameResolutionIdentical) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 460 | adapter_.OnOutputFormatRequest(capture_format_); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 461 | EXPECT_TRUE(adapter_.AdaptFrameResolution( |
| 462 | capture_format_.width, capture_format_.height, 0, |
| 463 | &cropped_width_, &cropped_height_, |
| 464 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 465 | EXPECT_EQ(capture_format_.width, cropped_width_); |
| 466 | EXPECT_EQ(capture_format_.height, cropped_height_); |
| 467 | EXPECT_EQ(capture_format_.width, out_width_); |
| 468 | EXPECT_EQ(capture_format_.height, out_height_); |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // Adapt the frame resolution to be a quarter of the capture resolution. Expect |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 472 | // no cropping, but a resolution change. |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 473 | TEST_F(VideoAdapterTest, AdaptFrameResolutionQuarter) { |
| 474 | VideoFormat request_format = capture_format_; |
| 475 | request_format.width /= 2; |
| 476 | request_format.height /= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 477 | adapter_.OnOutputFormatRequest(request_format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 478 | EXPECT_TRUE(adapter_.AdaptFrameResolution( |
| 479 | capture_format_.width, capture_format_.height, 0, |
| 480 | &cropped_width_, &cropped_height_, |
| 481 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 482 | EXPECT_EQ(capture_format_.width, cropped_width_); |
| 483 | EXPECT_EQ(capture_format_.height, cropped_height_); |
| 484 | EXPECT_EQ(request_format.width, out_width_); |
| 485 | EXPECT_EQ(request_format.height, out_height_); |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // Adapt the pixel resolution to 0. Expect frame drop. |
| 489 | TEST_F(VideoAdapterTest, AdaptFrameResolutionDrop) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 490 | VideoFormat output_format = capture_format_; |
| 491 | output_format.width = 0; |
| 492 | output_format.height = 0; |
| 493 | adapter_.OnOutputFormatRequest(output_format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 494 | EXPECT_FALSE(adapter_.AdaptFrameResolution( |
| 495 | capture_format_.width, capture_format_.height, 0, |
| 496 | &cropped_width_, &cropped_height_, |
| 497 | &out_width_, &out_height_)); |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 500 | // Adapt the frame resolution to be a quarter of the capture resolution at the |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 501 | // beginning. Expect no cropping but a resolution change. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 502 | TEST_F(VideoAdapterTest, AdaptResolution) { |
| 503 | VideoFormat request_format = capture_format_; |
| 504 | request_format.width /= 2; |
| 505 | request_format.height /= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 506 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 507 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 508 | for (int i = 0; i < 10; ++i) |
| 509 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 510 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 511 | // Verify no frame drop, no cropping, and resolution change. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 512 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 513 | EXPECT_EQ(0, stats.dropped_frames); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 514 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height, |
| 515 | request_format.width, request_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 516 | } |
| 517 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 518 | // Adapt the frame resolution to be a quarter of the capture resolution after |
| 519 | // capturing no less than 10 frames. Expect no resolution change before |
| 520 | // adaptation and resolution change after adaptation. |
| 521 | TEST_F(VideoAdapterTest, AdaptResolutionOnTheFly) { |
| 522 | VideoFormat request_format = capture_format_; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 523 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 524 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 525 | for (int i = 0; i < 10; ++i) |
| 526 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 527 | |
| 528 | // Verify no resolution change before adaptation. |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 529 | VerifyAdaptedResolution(listener_->GetStats(), |
| 530 | capture_format_.width, capture_format_.height, |
| 531 | request_format.width, request_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 532 | |
| 533 | // Adapt the frame resolution. |
| 534 | request_format.width /= 2; |
| 535 | request_format.height /= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 536 | adapter_.OnOutputFormatRequest(request_format); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 537 | for (int i = 0; i < 10; ++i) |
| 538 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 539 | |
| 540 | // Verify resolution change after adaptation. |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 541 | VerifyAdaptedResolution(listener_->GetStats(), |
| 542 | capture_format_.width, capture_format_.height, |
| 543 | request_format.width, request_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 544 | } |
| 545 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 546 | // Drop all frames. |
| 547 | TEST_F(VideoAdapterTest, DropAllFrames) { |
| 548 | VideoFormat format; // with resolution 0x0. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 549 | adapter_.OnOutputFormatRequest(format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 550 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 551 | for (int i = 0; i < 10; ++i) |
| 552 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 553 | |
| 554 | // Verify all frames are dropped. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 555 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 556 | EXPECT_GE(stats.captured_frames, 10); |
| 557 | EXPECT_EQ(stats.captured_frames, stats.dropped_frames); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 560 | TEST_F(VideoAdapterTest, TestOnOutputFormatRequest) { |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 561 | VideoFormat format(640, 400, 0, 0); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 562 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 563 | &cropped_width_, &cropped_height_, |
| 564 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 565 | EXPECT_EQ(640, cropped_width_); |
| 566 | EXPECT_EQ(400, cropped_height_); |
| 567 | EXPECT_EQ(640, out_width_); |
| 568 | EXPECT_EQ(400, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 569 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 570 | // Format request 640x400. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 571 | format.height = 400; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 572 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 573 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 574 | &cropped_width_, &cropped_height_, |
| 575 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 576 | EXPECT_EQ(640, cropped_width_); |
| 577 | EXPECT_EQ(400, cropped_height_); |
| 578 | EXPECT_EQ(640, out_width_); |
| 579 | EXPECT_EQ(400, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 580 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 581 | // Request 1280x720, higher than input, but aspect 16:9. Expect cropping but |
| 582 | // no scaling. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 583 | format.width = 1280; |
| 584 | format.height = 720; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 585 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 586 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 587 | &cropped_width_, &cropped_height_, |
| 588 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 589 | EXPECT_EQ(640, cropped_width_); |
| 590 | EXPECT_EQ(360, cropped_height_); |
| 591 | EXPECT_EQ(640, out_width_); |
| 592 | EXPECT_EQ(360, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 593 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 594 | // Request 0x0. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 595 | format.width = 0; |
| 596 | format.height = 0; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 597 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 598 | EXPECT_FALSE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 599 | &cropped_width_, &cropped_height_, |
| 600 | &out_width_, &out_height_)); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 601 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 602 | // Request 320x200. Expect scaling, but no cropping. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 603 | format.width = 320; |
| 604 | format.height = 200; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 605 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 606 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 607 | &cropped_width_, &cropped_height_, |
| 608 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 609 | EXPECT_EQ(640, cropped_width_); |
| 610 | EXPECT_EQ(400, cropped_height_); |
| 611 | EXPECT_EQ(320, out_width_); |
| 612 | EXPECT_EQ(200, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 613 | |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 614 | // Request resolution close to 2/3 scale. Expect adapt down. Scaling to 2/3 |
| 615 | // is not optimized and not allowed, therefore 1/2 scaling will be used |
| 616 | // instead. |
| 617 | format.width = 424; |
| 618 | format.height = 265; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 619 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 620 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 621 | &cropped_width_, &cropped_height_, |
| 622 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 623 | EXPECT_EQ(640, cropped_width_); |
| 624 | EXPECT_EQ(400, cropped_height_); |
| 625 | EXPECT_EQ(320, out_width_); |
| 626 | EXPECT_EQ(200, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 627 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 628 | // Request resolution of 3 / 8. Expect adapt down. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 629 | format.width = 640 * 3 / 8; |
| 630 | format.height = 400 * 3 / 8; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 631 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 632 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 633 | &cropped_width_, &cropped_height_, |
| 634 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 635 | EXPECT_EQ(640, cropped_width_); |
| 636 | EXPECT_EQ(400, cropped_height_); |
| 637 | EXPECT_EQ(640 * 3 / 8, out_width_); |
| 638 | EXPECT_EQ(400 * 3 / 8, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 639 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 640 | // Switch back up. Expect adapt. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 641 | format.width = 320; |
| 642 | format.height = 200; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 643 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 644 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 645 | &cropped_width_, &cropped_height_, |
| 646 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 647 | EXPECT_EQ(640, cropped_width_); |
| 648 | EXPECT_EQ(400, cropped_height_); |
| 649 | EXPECT_EQ(320, out_width_); |
| 650 | EXPECT_EQ(200, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 651 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 652 | // Format request 480x300. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 653 | format.width = 480; |
| 654 | format.height = 300; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 655 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 656 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 400, 0, |
| 657 | &cropped_width_, &cropped_height_, |
| 658 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 659 | EXPECT_EQ(640, cropped_width_); |
| 660 | EXPECT_EQ(400, cropped_height_); |
| 661 | EXPECT_EQ(480, out_width_); |
| 662 | EXPECT_EQ(300, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 665 | TEST_F(VideoAdapterTest, TestViewRequestPlusCameraSwitch) { |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 666 | // Start at HD. |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 667 | VideoFormat format(1280, 720, 0, 0); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 668 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 669 | &cropped_width_, &cropped_height_, |
| 670 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 671 | EXPECT_EQ(1280, cropped_width_); |
| 672 | EXPECT_EQ(720, cropped_height_); |
| 673 | EXPECT_EQ(1280, out_width_); |
| 674 | EXPECT_EQ(720, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 675 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 676 | // Format request for VGA. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 677 | format.width = 640; |
| 678 | format.height = 360; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 679 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 680 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 681 | &cropped_width_, &cropped_height_, |
| 682 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 683 | EXPECT_EQ(1280, cropped_width_); |
| 684 | EXPECT_EQ(720, cropped_height_); |
| 685 | EXPECT_EQ(640, out_width_); |
| 686 | EXPECT_EQ(360, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 687 | |
| 688 | // Now, the camera reopens at VGA. |
| 689 | // Both the frame and the output format should be 640x360. |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 690 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 360, 0, |
| 691 | &cropped_width_, &cropped_height_, |
| 692 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 693 | EXPECT_EQ(640, cropped_width_); |
| 694 | EXPECT_EQ(360, cropped_height_); |
| 695 | EXPECT_EQ(640, out_width_); |
| 696 | EXPECT_EQ(360, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 697 | |
| 698 | // And another view request comes in for 640x360, which should have no |
| 699 | // real impact. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 700 | adapter_.OnOutputFormatRequest(format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 701 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 360, 0, |
| 702 | &cropped_width_, &cropped_height_, |
| 703 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 704 | EXPECT_EQ(640, cropped_width_); |
| 705 | EXPECT_EQ(360, cropped_height_); |
| 706 | EXPECT_EQ(640, out_width_); |
| 707 | EXPECT_EQ(360, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 710 | TEST_F(VideoAdapterTest, TestVGAWidth) { |
| 711 | // Reqeuested Output format is 640x360. |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 712 | VideoFormat format(640, 360, 0, FOURCC_I420); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 713 | adapter_.OnOutputFormatRequest(format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 714 | |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 715 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 716 | &cropped_width_, &cropped_height_, |
| 717 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 718 | // Expect cropping. |
| 719 | EXPECT_EQ(640, cropped_width_); |
| 720 | EXPECT_EQ(360, cropped_height_); |
| 721 | EXPECT_EQ(640, out_width_); |
| 722 | EXPECT_EQ(360, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 723 | |
| 724 | // But if frames come in at 640x360, we shouldn't adapt them down. |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 725 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 360, 0, |
| 726 | &cropped_width_, &cropped_height_, |
| 727 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 728 | EXPECT_EQ(640, cropped_width_); |
| 729 | EXPECT_EQ(360, cropped_height_); |
| 730 | EXPECT_EQ(640, out_width_); |
| 731 | EXPECT_EQ(360, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 732 | |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 733 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 734 | &cropped_width_, &cropped_height_, |
| 735 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 736 | EXPECT_EQ(640, cropped_width_); |
| 737 | EXPECT_EQ(360, cropped_height_); |
| 738 | EXPECT_EQ(640, out_width_); |
| 739 | EXPECT_EQ(360, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 740 | } |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 741 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 742 | TEST_F(VideoAdapterTest, TestOnResolutionRequestInSmallSteps) { |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 743 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 744 | &cropped_width_, &cropped_height_, |
| 745 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 746 | EXPECT_EQ(1280, cropped_width_); |
| 747 | EXPECT_EQ(720, cropped_height_); |
| 748 | EXPECT_EQ(1280, out_width_); |
| 749 | EXPECT_EQ(720, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 750 | |
| 751 | // Adapt down one step. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 752 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 1280 * 720 - 1, |
| 753 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 754 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 755 | &cropped_width_, &cropped_height_, |
| 756 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 757 | EXPECT_EQ(1280, cropped_width_); |
| 758 | EXPECT_EQ(720, cropped_height_); |
| 759 | EXPECT_EQ(960, out_width_); |
| 760 | EXPECT_EQ(540, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 761 | |
| 762 | // Adapt down one step more. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 763 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 960 * 540 - 1, |
| 764 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 765 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 766 | &cropped_width_, &cropped_height_, |
| 767 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 768 | EXPECT_EQ(1280, cropped_width_); |
| 769 | EXPECT_EQ(720, cropped_height_); |
| 770 | EXPECT_EQ(640, out_width_); |
| 771 | EXPECT_EQ(360, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 772 | |
| 773 | // Adapt down one step more. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 774 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 640 * 360 - 1, |
| 775 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 776 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 777 | &cropped_width_, &cropped_height_, |
| 778 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 779 | EXPECT_EQ(1280, cropped_width_); |
| 780 | EXPECT_EQ(720, cropped_height_); |
| 781 | EXPECT_EQ(480, out_width_); |
| 782 | EXPECT_EQ(270, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 783 | |
| 784 | // Adapt up one step. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 785 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(640 * 360), |
| 786 | 960 * 540, |
| 787 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 788 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 789 | &cropped_width_, &cropped_height_, |
| 790 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 791 | EXPECT_EQ(1280, cropped_width_); |
| 792 | EXPECT_EQ(720, cropped_height_); |
| 793 | EXPECT_EQ(640, out_width_); |
| 794 | EXPECT_EQ(360, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 795 | |
| 796 | // Adapt up one step more. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 797 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(960 * 540), |
| 798 | 1280 * 720, |
| 799 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 800 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 801 | &cropped_width_, &cropped_height_, |
| 802 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 803 | EXPECT_EQ(1280, cropped_width_); |
| 804 | EXPECT_EQ(720, cropped_height_); |
| 805 | EXPECT_EQ(960, out_width_); |
| 806 | EXPECT_EQ(540, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 807 | |
| 808 | // Adapt up one step more. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 809 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(1280 * 720), |
| 810 | 1920 * 1080, |
| 811 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 812 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 813 | &cropped_width_, &cropped_height_, |
| 814 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 815 | EXPECT_EQ(1280, cropped_width_); |
| 816 | EXPECT_EQ(720, cropped_height_); |
| 817 | EXPECT_EQ(1280, out_width_); |
| 818 | EXPECT_EQ(720, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | TEST_F(VideoAdapterTest, TestOnResolutionRequestMaxZero) { |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 822 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 823 | &cropped_width_, &cropped_height_, |
| 824 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 825 | EXPECT_EQ(1280, cropped_width_); |
| 826 | EXPECT_EQ(720, cropped_height_); |
| 827 | EXPECT_EQ(1280, out_width_); |
| 828 | EXPECT_EQ(720, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 829 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 830 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 0, |
| 831 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 832 | EXPECT_FALSE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 833 | &cropped_width_, &cropped_height_, |
| 834 | &out_width_, &out_height_)); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | TEST_F(VideoAdapterTest, TestOnResolutionRequestInLargeSteps) { |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 838 | // Large step down. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 839 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 640 * 360 - 1, |
| 840 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 841 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 842 | &cropped_width_, &cropped_height_, |
| 843 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 844 | EXPECT_EQ(1280, cropped_width_); |
| 845 | EXPECT_EQ(720, cropped_height_); |
| 846 | EXPECT_EQ(480, out_width_); |
| 847 | EXPECT_EQ(270, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 848 | |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 849 | // Large step up. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 850 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(1280 * 720), |
| 851 | 1920 * 1080, |
| 852 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 853 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 854 | &cropped_width_, &cropped_height_, |
| 855 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 856 | EXPECT_EQ(1280, cropped_width_); |
| 857 | EXPECT_EQ(720, cropped_height_); |
| 858 | EXPECT_EQ(1280, out_width_); |
| 859 | EXPECT_EQ(720, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | TEST_F(VideoAdapterTest, TestOnOutputFormatRequestCapsMaxResolution) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 863 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 640 * 360 - 1, |
| 864 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 865 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 866 | &cropped_width_, &cropped_height_, |
| 867 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 868 | EXPECT_EQ(1280, cropped_width_); |
| 869 | EXPECT_EQ(720, cropped_height_); |
| 870 | EXPECT_EQ(480, out_width_); |
| 871 | EXPECT_EQ(270, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 872 | |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 873 | VideoFormat new_format(640, 360, 0, FOURCC_I420); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 874 | adapter_.OnOutputFormatRequest(new_format); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 875 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 876 | &cropped_width_, &cropped_height_, |
| 877 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 878 | EXPECT_EQ(1280, cropped_width_); |
| 879 | EXPECT_EQ(720, cropped_height_); |
| 880 | EXPECT_EQ(480, out_width_); |
| 881 | EXPECT_EQ(270, out_height_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 882 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 883 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 960 * 720, |
| 884 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 885 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 886 | &cropped_width_, &cropped_height_, |
| 887 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 888 | EXPECT_EQ(1280, cropped_width_); |
| 889 | EXPECT_EQ(720, cropped_height_); |
| 890 | EXPECT_EQ(640, out_width_); |
| 891 | EXPECT_EQ(360, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 894 | TEST_F(VideoAdapterTest, TestOnResolutionRequestReset) { |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 895 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 896 | &cropped_width_, &cropped_height_, |
| 897 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 898 | EXPECT_EQ(1280, cropped_width_); |
| 899 | EXPECT_EQ(720, cropped_height_); |
| 900 | EXPECT_EQ(1280, out_width_); |
| 901 | EXPECT_EQ(720, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 902 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 903 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 640 * 360 - 1, |
| 904 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 905 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 906 | &cropped_width_, &cropped_height_, |
| 907 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 908 | EXPECT_EQ(1280, cropped_width_); |
| 909 | EXPECT_EQ(720, cropped_height_); |
| 910 | EXPECT_EQ(480, out_width_); |
| 911 | EXPECT_EQ(270, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 912 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 913 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), |
| 914 | std::numeric_limits<int>::max(), |
| 915 | std::numeric_limits<int>::max()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 916 | EXPECT_TRUE(adapter_.AdaptFrameResolution(1280, 720, 0, |
| 917 | &cropped_width_, &cropped_height_, |
| 918 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 919 | EXPECT_EQ(1280, cropped_width_); |
| 920 | EXPECT_EQ(720, cropped_height_); |
| 921 | EXPECT_EQ(1280, out_width_); |
| 922 | EXPECT_EQ(720, out_height_); |
| 923 | } |
| 924 | |
| 925 | TEST_F(VideoAdapterTest, TestCroppingWithResolutionRequest) { |
| 926 | // Ask for 640x360 (16:9 aspect). |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 927 | adapter_.OnOutputFormatRequest(VideoFormat(640, 360, 0, FOURCC_I420)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 928 | // Send 640x480 (4:3 aspect). |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 929 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 930 | &cropped_width_, &cropped_height_, |
| 931 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 932 | // Expect cropping to 16:9 format and no scaling. |
| 933 | EXPECT_EQ(640, cropped_width_); |
| 934 | EXPECT_EQ(360, cropped_height_); |
| 935 | EXPECT_EQ(640, out_width_); |
| 936 | EXPECT_EQ(360, out_height_); |
| 937 | |
| 938 | // Adapt down one step. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 939 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 640 * 360 - 1, |
| 940 | std::numeric_limits<int>::max()); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 941 | // Expect cropping to 16:9 format and 3/4 scaling. |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 942 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 943 | &cropped_width_, &cropped_height_, |
| 944 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 945 | EXPECT_EQ(640, cropped_width_); |
| 946 | EXPECT_EQ(360, cropped_height_); |
| 947 | EXPECT_EQ(480, out_width_); |
| 948 | EXPECT_EQ(270, out_height_); |
| 949 | |
| 950 | // Adapt down one step more. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 951 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 480 * 270 - 1, |
| 952 | std::numeric_limits<int>::max()); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 953 | // Expect cropping to 16:9 format and 1/2 scaling. |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 954 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 955 | &cropped_width_, &cropped_height_, |
| 956 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 957 | EXPECT_EQ(640, cropped_width_); |
| 958 | EXPECT_EQ(360, cropped_height_); |
| 959 | EXPECT_EQ(320, out_width_); |
| 960 | EXPECT_EQ(180, out_height_); |
| 961 | |
| 962 | // Adapt up one step. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 963 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(480 * 270), |
| 964 | 640 * 360, |
| 965 | std::numeric_limits<int>::max()); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 966 | // Expect cropping to 16:9 format and 3/4 scaling. |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 967 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 968 | &cropped_width_, &cropped_height_, |
| 969 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 970 | EXPECT_EQ(640, cropped_width_); |
| 971 | EXPECT_EQ(360, cropped_height_); |
| 972 | EXPECT_EQ(480, out_width_); |
| 973 | EXPECT_EQ(270, out_height_); |
| 974 | |
| 975 | // Adapt up one step more. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 976 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(640 * 360), |
| 977 | 960 * 540, |
| 978 | std::numeric_limits<int>::max()); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 979 | // Expect cropping to 16:9 format and no scaling. |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 980 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 981 | &cropped_width_, &cropped_height_, |
| 982 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 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 | // Try to adapt up one step more. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 989 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(960 * 540), |
| 990 | 1280 * 720, |
| 991 | std::numeric_limits<int>::max()); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 992 | // Expect cropping to 16:9 format and no scaling. |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 993 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 994 | &cropped_width_, &cropped_height_, |
| 995 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 996 | EXPECT_EQ(640, cropped_width_); |
| 997 | EXPECT_EQ(360, cropped_height_); |
| 998 | EXPECT_EQ(640, out_width_); |
| 999 | EXPECT_EQ(360, out_height_); |
| 1000 | } |
| 1001 | |
| 1002 | TEST_F(VideoAdapterTest, TestCroppingOddResolution) { |
| 1003 | // Ask for 640x360 (16:9 aspect), with 3/16 scaling. |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 1004 | adapter_.OnOutputFormatRequest( |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 1005 | VideoFormat(640, 360, 0, FOURCC_I420)); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1006 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), |
| 1007 | 640 * 360 * 3 / 16 * 3 / 16, |
| 1008 | std::numeric_limits<int>::max()); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 1009 | |
| 1010 | // Send 640x480 (4:3 aspect). |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 1011 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 480, 0, |
| 1012 | &cropped_width_, &cropped_height_, |
| 1013 | &out_width_, &out_height_)); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 1014 | |
| 1015 | // Instead of getting the exact aspect ratio with cropped resolution 640x360, |
| 1016 | // the resolution should be adjusted to get a perfect scale factor instead. |
| 1017 | EXPECT_EQ(640, cropped_width_); |
| 1018 | EXPECT_EQ(368, cropped_height_); |
| 1019 | EXPECT_EQ(120, out_width_); |
| 1020 | EXPECT_EQ(69, out_height_); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 1023 | TEST_F(VideoAdapterTest, TestAdaptToVerySmallResolution) { |
| 1024 | // Ask for 1920x1080 (16:9 aspect), with 1/16 scaling. |
| 1025 | const int w = 1920; |
| 1026 | const int h = 1080; |
| 1027 | adapter_.OnOutputFormatRequest(VideoFormat(w, h, 0, FOURCC_I420)); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1028 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), |
| 1029 | w * h * 1 / 16 * 1 / 16, |
| 1030 | std::numeric_limits<int>::max()); |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 1031 | |
| 1032 | // Send 1920x1080 (16:9 aspect). |
| 1033 | EXPECT_TRUE(adapter_.AdaptFrameResolution( |
| 1034 | w, h, 0, &cropped_width_, &cropped_height_, &out_width_, &out_height_)); |
| 1035 | |
| 1036 | // Instead of getting the exact aspect ratio with cropped resolution 1920x1080 |
| 1037 | // the resolution should be adjusted to get a perfect scale factor instead. |
| 1038 | EXPECT_EQ(1920, cropped_width_); |
| 1039 | EXPECT_EQ(1072, cropped_height_); |
| 1040 | EXPECT_EQ(120, out_width_); |
| 1041 | EXPECT_EQ(67, out_height_); |
| 1042 | |
| 1043 | // Adapt back up one step to 3/32. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1044 | adapter_.OnResolutionFramerateRequest( |
| 1045 | rtc::Optional<int>(w * h * 3 / 32 * 3 / 32), w * h * 1 / 8 * 1 / 8, |
| 1046 | std::numeric_limits<int>::max()); |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 1047 | |
| 1048 | // Send 1920x1080 (16:9 aspect). |
| 1049 | EXPECT_TRUE(adapter_.AdaptFrameResolution( |
| 1050 | w, h, 0, &cropped_width_, &cropped_height_, &out_width_, &out_height_)); |
| 1051 | |
| 1052 | EXPECT_EQ(180, out_width_); |
| 1053 | EXPECT_EQ(99, out_height_); |
| 1054 | } |
| 1055 | |
| 1056 | TEST_F(VideoAdapterTest, AdaptFrameResolutionDropWithResolutionRequest) { |
| 1057 | VideoFormat output_format = capture_format_; |
| 1058 | output_format.width = 0; |
| 1059 | output_format.height = 0; |
| 1060 | adapter_.OnOutputFormatRequest(output_format); |
| 1061 | EXPECT_FALSE(adapter_.AdaptFrameResolution( |
| 1062 | capture_format_.width, capture_format_.height, 0, |
| 1063 | &cropped_width_, &cropped_height_, |
| 1064 | &out_width_, &out_height_)); |
| 1065 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1066 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(960 * 540), |
| 1067 | std::numeric_limits<int>::max(), |
| 1068 | std::numeric_limits<int>::max()); |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 1069 | |
| 1070 | // Still expect all frames to be dropped |
| 1071 | EXPECT_FALSE(adapter_.AdaptFrameResolution( |
| 1072 | capture_format_.width, capture_format_.height, 0, |
| 1073 | &cropped_width_, &cropped_height_, |
| 1074 | &out_width_, &out_height_)); |
| 1075 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1076 | adapter_.OnResolutionFramerateRequest(rtc::Optional<int>(), 640 * 480 - 1, |
| 1077 | std::numeric_limits<int>::max()); |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 1078 | |
| 1079 | // Still expect all frames to be dropped |
| 1080 | EXPECT_FALSE(adapter_.AdaptFrameResolution( |
| 1081 | capture_format_.width, capture_format_.height, 0, |
| 1082 | &cropped_width_, &cropped_height_, |
| 1083 | &out_width_, &out_height_)); |
| 1084 | } |
| 1085 | |
Magnus Jedvert | 6d230d7 | 2017-02-22 18:30:27 +0100 | [diff] [blame] | 1086 | // Test that we will adapt to max given a target pixel count close to max. |
| 1087 | TEST_F(VideoAdapterTest, TestAdaptToMax) { |
| 1088 | adapter_.OnOutputFormatRequest(VideoFormat(640, 360, 0, FOURCC_I420)); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1089 | adapter_.OnResolutionFramerateRequest( |
| 1090 | rtc::Optional<int>(640 * 360 - 1) /* target */, |
| 1091 | std::numeric_limits<int>::max(), std::numeric_limits<int>::max()); |
Magnus Jedvert | 6d230d7 | 2017-02-22 18:30:27 +0100 | [diff] [blame] | 1092 | |
| 1093 | EXPECT_TRUE(adapter_.AdaptFrameResolution(640, 360, 0, &cropped_width_, |
| 1094 | &cropped_height_, &out_width_, |
| 1095 | &out_height_)); |
| 1096 | EXPECT_EQ(640, out_width_); |
| 1097 | EXPECT_EQ(360, out_height_); |
| 1098 | } |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 1099 | } // namespace cricket |