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