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 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 16 | #include "webrtc/base/gunit.h" |
| 17 | #include "webrtc/base/logging.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 18 | #include "webrtc/media/base/fakevideocapturer.h" |
| 19 | #include "webrtc/media/base/mediachannel.h" |
| 20 | #include "webrtc/media/base/testutils.h" |
| 21 | #include "webrtc/media/base/videoadapter.h" |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 22 | |
| 23 | namespace cricket { |
| 24 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 25 | class VideoAdapterTest : public testing::Test { |
| 26 | public: |
| 27 | virtual void SetUp() { |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 28 | capturer_.reset(new FakeVideoCapturer); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 29 | capture_format_ = capturer_->GetSupportedFormats()->at(0); |
| 30 | capture_format_.interval = VideoFormat::FpsToInterval(50); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 31 | adapter_.SetExpectedInputFrameInterval(capture_format_.interval); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 32 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 33 | listener_.reset(new VideoCapturerListener(&adapter_)); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 34 | capturer_->SignalFrameCaptured.connect( |
| 35 | listener_.get(), &VideoCapturerListener::OnFrameCaptured); |
| 36 | } |
| 37 | |
pbos@webrtc.org | 75c3ec1 | 2014-08-27 18:16:13 +0000 | [diff] [blame] | 38 | virtual void TearDown() { |
| 39 | // Explicitly disconnect the VideoCapturer before to avoid data races |
| 40 | // (frames delivered to VideoCapturerListener while it's being destructed). |
| 41 | capturer_->SignalFrameCaptured.disconnect_all(); |
| 42 | } |
| 43 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 44 | protected: |
| 45 | class VideoCapturerListener: public sigslot::has_slots<> { |
| 46 | public: |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 47 | struct Stats { |
| 48 | int captured_frames; |
| 49 | int dropped_frames; |
| 50 | bool last_adapt_was_no_op; |
| 51 | |
| 52 | int adapted_width; |
| 53 | int adapted_height; |
| 54 | }; |
| 55 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 56 | explicit VideoCapturerListener(VideoAdapter* adapter) |
| 57 | : video_adapter_(adapter), |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 58 | captured_frames_(0), |
| 59 | dropped_frames_(0), |
| 60 | last_adapt_was_no_op_(false) { |
| 61 | } |
| 62 | |
| 63 | void OnFrameCaptured(VideoCapturer* capturer, |
| 64 | const CapturedFrame* captured_frame) { |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 65 | rtc::CritScope lock(&crit_); |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 66 | const int in_width = captured_frame->width; |
| 67 | const int in_height = abs(captured_frame->height); |
| 68 | const VideoFormat adapted_format = |
| 69 | video_adapter_->AdaptFrameResolution(in_width, in_height); |
| 70 | if (!adapted_format.IsSize0x0()) { |
| 71 | adapted_format_ = adapted_format; |
| 72 | last_adapt_was_no_op_ = (in_width == adapted_format.width && |
| 73 | in_height == adapted_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 74 | } else { |
| 75 | ++dropped_frames_; |
| 76 | } |
| 77 | ++captured_frames_; |
| 78 | } |
| 79 | |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 80 | Stats GetStats() { |
| 81 | rtc::CritScope lock(&crit_); |
| 82 | Stats stats; |
| 83 | stats.captured_frames = captured_frames_; |
| 84 | stats.dropped_frames = dropped_frames_; |
| 85 | stats.last_adapt_was_no_op = last_adapt_was_no_op_; |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 86 | if (!adapted_format_.IsSize0x0()) { |
| 87 | stats.adapted_width = adapted_format_.width; |
| 88 | stats.adapted_height = adapted_format_.height; |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 89 | } else { |
| 90 | stats.adapted_width = stats.adapted_height = -1; |
| 91 | } |
| 92 | |
| 93 | return stats; |
| 94 | } |
| 95 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 96 | private: |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 97 | rtc::CriticalSection crit_; |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 98 | VideoAdapter* video_adapter_; |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 99 | VideoFormat adapted_format_; |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 100 | int captured_frames_; |
| 101 | int dropped_frames_; |
| 102 | bool last_adapt_was_no_op_; |
| 103 | }; |
| 104 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 105 | |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 106 | void VerifyAdaptedResolution(const VideoCapturerListener::Stats& stats, |
| 107 | int width, |
| 108 | int height) { |
| 109 | EXPECT_EQ(width, stats.adapted_width); |
| 110 | EXPECT_EQ(height, stats.adapted_height); |
| 111 | } |
| 112 | |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 113 | std::unique_ptr<FakeVideoCapturer> capturer_; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 114 | VideoAdapter adapter_; |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 115 | std::unique_ptr<VideoCapturerListener> listener_; |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 116 | VideoFormat capture_format_; |
| 117 | }; |
| 118 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 119 | // Do not adapt the frame rate or the resolution. Expect no frame drop and no |
| 120 | // resolution change. |
| 121 | TEST_F(VideoAdapterTest, AdaptNothing) { |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 122 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 123 | for (int i = 0; i < 10; ++i) |
| 124 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 125 | |
| 126 | // Verify no frame drop and no resolution change. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 127 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 128 | EXPECT_GE(stats.captured_frames, 10); |
| 129 | EXPECT_EQ(0, stats.dropped_frames); |
| 130 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height); |
| 131 | EXPECT_TRUE(stats.last_adapt_was_no_op); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST_F(VideoAdapterTest, AdaptZeroInterval) { |
| 135 | VideoFormat format = capturer_->GetSupportedFormats()->at(0); |
| 136 | format.interval = 0; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 137 | adapter_.OnOutputFormatRequest(format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 138 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 139 | for (int i = 0; i < 10; ++i) |
| 140 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 141 | |
| 142 | // Verify no crash and that frames aren't dropped. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 143 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 144 | EXPECT_GE(stats.captured_frames, 10); |
| 145 | EXPECT_EQ(0, stats.dropped_frames); |
| 146 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Adapt the frame rate to be half of the capture rate at the beginning. Expect |
| 150 | // the number of dropped frames to be half of the number the captured frames. |
| 151 | TEST_F(VideoAdapterTest, AdaptFramerate) { |
| 152 | VideoFormat request_format = capture_format_; |
| 153 | request_format.interval *= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 154 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 155 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 156 | for (int i = 0; i < 10; ++i) |
| 157 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 158 | |
| 159 | // Verify frame drop and no resolution change. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 160 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 161 | EXPECT_GE(stats.captured_frames, 10); |
| 162 | EXPECT_EQ(stats.captured_frames / 2, stats.dropped_frames); |
| 163 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Adapt the frame rate to be half of the capture rate at the beginning. Expect |
| 167 | // the number of dropped frames to be half of the number the captured frames. |
| 168 | TEST_F(VideoAdapterTest, AdaptFramerateVariable) { |
| 169 | VideoFormat request_format = capture_format_; |
| 170 | request_format.interval = request_format.interval * 3 / 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 171 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 172 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 173 | for (int i = 0; i < 30; ++i) |
| 174 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 175 | |
| 176 | // Verify frame drop and no resolution change. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 177 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 178 | EXPECT_GE(stats.captured_frames, 30); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 179 | // Verify 2 / 3 kept (20) and 1 / 3 dropped (10). |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 180 | EXPECT_EQ(stats.captured_frames * 1 / 3, stats.dropped_frames); |
| 181 | VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Adapt the frame rate to be half of the capture rate after capturing no less |
| 185 | // than 10 frames. Expect no frame dropped before adaptation and frame dropped |
| 186 | // after adaptation. |
| 187 | TEST_F(VideoAdapterTest, AdaptFramerateOntheFly) { |
| 188 | VideoFormat request_format = capture_format_; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 189 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 190 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 191 | for (int i = 0; i < 10; ++i) |
| 192 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 193 | |
| 194 | // Verify no frame drop before adaptation. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 195 | EXPECT_EQ(0, listener_->GetStats().dropped_frames); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 196 | |
| 197 | // Adapat the frame rate. |
| 198 | request_format.interval *= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 199 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 200 | |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 201 | for (int i = 0; i < 20; ++i) |
| 202 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 203 | |
| 204 | // Verify frame drop after adaptation. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 205 | EXPECT_GT(listener_->GetStats().dropped_frames, 0); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 206 | } |
| 207 | |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 208 | // Set a very high output pixel resolution. Expect no resolution change. |
| 209 | TEST_F(VideoAdapterTest, AdaptFrameResolutionHighLimit) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 210 | VideoFormat output_format = capture_format_; |
| 211 | output_format.width = 2560; |
| 212 | output_format.height = 2560; |
| 213 | adapter_.OnOutputFormatRequest(output_format); |
| 214 | VideoFormat adapted_format = adapter_.AdaptFrameResolution( |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 215 | capture_format_.width, capture_format_.height); |
| 216 | EXPECT_EQ(capture_format_.width, adapted_format.width); |
| 217 | EXPECT_EQ(capture_format_.height, adapted_format.height); |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Adapt the frame resolution to be the same as capture resolution. Expect no |
| 221 | // resolution change. |
| 222 | TEST_F(VideoAdapterTest, AdaptFrameResolutionIdentical) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 223 | adapter_.OnOutputFormatRequest(capture_format_); |
| 224 | const VideoFormat adapted_format = adapter_.AdaptFrameResolution( |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 225 | capture_format_.width, capture_format_.height); |
| 226 | EXPECT_EQ(capture_format_.width, adapted_format.width); |
| 227 | EXPECT_EQ(capture_format_.height, adapted_format.height); |
| 228 | } |
| 229 | |
| 230 | // Adapt the frame resolution to be a quarter of the capture resolution. Expect |
| 231 | // resolution change. |
| 232 | TEST_F(VideoAdapterTest, AdaptFrameResolutionQuarter) { |
| 233 | VideoFormat request_format = capture_format_; |
| 234 | request_format.width /= 2; |
| 235 | request_format.height /= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 236 | adapter_.OnOutputFormatRequest(request_format); |
| 237 | const VideoFormat adapted_format = adapter_.AdaptFrameResolution( |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 238 | request_format.width, request_format.height); |
| 239 | EXPECT_EQ(request_format.width, adapted_format.width); |
| 240 | EXPECT_EQ(request_format.height, adapted_format.height); |
| 241 | } |
| 242 | |
| 243 | // Adapt the pixel resolution to 0. Expect frame drop. |
| 244 | TEST_F(VideoAdapterTest, AdaptFrameResolutionDrop) { |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 245 | VideoFormat output_format = capture_format_; |
| 246 | output_format.width = 0; |
| 247 | output_format.height = 0; |
| 248 | adapter_.OnOutputFormatRequest(output_format); |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 249 | EXPECT_TRUE( |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 250 | adapter_ |
| 251 | .AdaptFrameResolution(capture_format_.width, capture_format_.height) |
| 252 | .IsSize0x0()); |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 253 | } |
| 254 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 255 | // Adapt the frame resolution to be a quarter of the capture resolution at the |
| 256 | // beginning. Expect resolution change. |
| 257 | TEST_F(VideoAdapterTest, AdaptResolution) { |
| 258 | VideoFormat request_format = capture_format_; |
| 259 | request_format.width /= 2; |
| 260 | request_format.height /= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 261 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 262 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 263 | for (int i = 0; i < 10; ++i) |
| 264 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 265 | |
| 266 | // Verify no frame drop and resolution change. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 267 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 268 | EXPECT_EQ(0, stats.dropped_frames); |
| 269 | VerifyAdaptedResolution(stats, request_format.width, request_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 270 | } |
| 271 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 272 | // Adapt the frame resolution to be a quarter of the capture resolution after |
| 273 | // capturing no less than 10 frames. Expect no resolution change before |
| 274 | // adaptation and resolution change after adaptation. |
| 275 | TEST_F(VideoAdapterTest, AdaptResolutionOnTheFly) { |
| 276 | VideoFormat request_format = capture_format_; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 277 | adapter_.OnOutputFormatRequest(request_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 278 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 279 | for (int i = 0; i < 10; ++i) |
| 280 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 281 | |
| 282 | // Verify no resolution change before adaptation. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 283 | VerifyAdaptedResolution( |
| 284 | listener_->GetStats(), request_format.width, request_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 285 | |
| 286 | // Adapt the frame resolution. |
| 287 | request_format.width /= 2; |
| 288 | request_format.height /= 2; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 289 | adapter_.OnOutputFormatRequest(request_format); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 290 | for (int i = 0; i < 10; ++i) |
| 291 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 292 | |
| 293 | // Verify resolution change after adaptation. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 294 | VerifyAdaptedResolution( |
| 295 | listener_->GetStats(), request_format.width, request_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 296 | } |
| 297 | |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 298 | // Drop all frames. |
| 299 | TEST_F(VideoAdapterTest, DropAllFrames) { |
| 300 | VideoFormat format; // with resolution 0x0. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 301 | adapter_.OnOutputFormatRequest(format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 302 | EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); |
Magnus Jedvert | 0184057 | 2015-04-10 11:18:39 +0200 | [diff] [blame] | 303 | for (int i = 0; i < 10; ++i) |
| 304 | capturer_->CaptureFrame(); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 305 | |
| 306 | // Verify all frames are dropped. |
pbos@webrtc.org | c9b3f77 | 2014-08-26 12:33:18 +0000 | [diff] [blame] | 307 | VideoCapturerListener::Stats stats = listener_->GetStats(); |
| 308 | EXPECT_GE(stats.captured_frames, 10); |
| 309 | EXPECT_EQ(stats.captured_frames, stats.dropped_frames); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 312 | TEST_F(VideoAdapterTest, TestOnOutputFormatRequest) { |
| 313 | VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), 0); |
| 314 | adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30)); |
| 315 | VideoFormat out_format = |
| 316 | adapter_.AdaptFrameResolution(format.width, format.height); |
| 317 | EXPECT_EQ(format, adapter_.input_format()); |
| 318 | EXPECT_EQ(format, out_format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 319 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 320 | // Format request 640x400. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 321 | format.height = 400; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 322 | adapter_.OnOutputFormatRequest(format); |
| 323 | out_format = adapter_.AdaptFrameResolution(640, 400); |
| 324 | EXPECT_EQ(640, out_format.width); |
| 325 | EXPECT_EQ(400, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 326 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 327 | // Request 1280x720, higher than input. Adapt nothing. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 328 | format.width = 1280; |
| 329 | format.height = 720; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 330 | adapter_.OnOutputFormatRequest(format); |
| 331 | out_format = adapter_.AdaptFrameResolution(640, 400); |
| 332 | EXPECT_EQ(640, out_format.width); |
| 333 | EXPECT_EQ(400, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 334 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 335 | // Request 0x0. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 336 | format.width = 0; |
| 337 | format.height = 0; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 338 | adapter_.OnOutputFormatRequest(format); |
| 339 | out_format = adapter_.AdaptFrameResolution(640, 400); |
| 340 | EXPECT_TRUE(out_format.IsSize0x0()); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 341 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 342 | // Request 320x200. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 343 | format.width = 320; |
| 344 | format.height = 200; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 345 | adapter_.OnOutputFormatRequest(format); |
| 346 | out_format = adapter_.AdaptFrameResolution(640, 400); |
| 347 | EXPECT_EQ(320, out_format.width); |
| 348 | EXPECT_EQ(200, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 349 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 350 | // Request resolution of 2 / 3. Expect adapt down. Scaling to 1/3 is not |
| 351 | // optimized and not allowed. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 352 | format.width = (640 * 2 + 1) / 3; |
| 353 | format.height = (400 * 2 + 1) / 3; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 354 | adapter_.OnOutputFormatRequest(format); |
| 355 | out_format = adapter_.AdaptFrameResolution(640, 400); |
| 356 | EXPECT_EQ(320, out_format.width); |
| 357 | EXPECT_EQ(200, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 358 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 359 | // Request resolution of 3 / 8. Expect adapt down. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 360 | format.width = 640 * 3 / 8; |
| 361 | format.height = 400 * 3 / 8; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 362 | adapter_.OnOutputFormatRequest(format); |
| 363 | out_format = adapter_.AdaptFrameResolution(640, 400); |
| 364 | EXPECT_EQ(640 * 3 / 8, out_format.width); |
| 365 | EXPECT_EQ(400 * 3 / 8, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 366 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 367 | // Switch back up. Expect adapt. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 368 | format.width = 320; |
| 369 | format.height = 200; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 370 | adapter_.OnOutputFormatRequest(format); |
| 371 | out_format = adapter_.AdaptFrameResolution(640, 400); |
| 372 | EXPECT_EQ(320, out_format.width); |
| 373 | EXPECT_EQ(200, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 374 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 375 | // Format request 480x300. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 376 | format.width = 480; |
| 377 | format.height = 300; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 378 | adapter_.OnOutputFormatRequest(format); |
| 379 | out_format = adapter_.AdaptFrameResolution(640, 400); |
| 380 | EXPECT_EQ(480, out_format.width); |
| 381 | EXPECT_EQ(300, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 384 | TEST_F(VideoAdapterTest, TestViewRequestPlusCameraSwitch) { |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 385 | // Start at HD. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 386 | VideoFormat format(1280, 720, VideoFormat::FpsToInterval(30), 0); |
| 387 | adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30)); |
| 388 | VideoFormat out_format = |
| 389 | adapter_.AdaptFrameResolution(format.width, format.height); |
| 390 | EXPECT_EQ(format, adapter_.input_format()); |
| 391 | EXPECT_EQ(out_format, adapter_.input_format()); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 392 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 393 | // Format request for VGA. |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 394 | format.width = 640; |
| 395 | format.height = 360; |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 396 | adapter_.OnOutputFormatRequest(format); |
| 397 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 398 | EXPECT_EQ(640, out_format.width); |
| 399 | EXPECT_EQ(360, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 400 | |
| 401 | // Now, the camera reopens at VGA. |
| 402 | // Both the frame and the output format should be 640x360. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 403 | out_format = adapter_.AdaptFrameResolution(640, 360); |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 404 | EXPECT_EQ(640, out_format.width); |
| 405 | EXPECT_EQ(360, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 406 | |
| 407 | // And another view request comes in for 640x360, which should have no |
| 408 | // real impact. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 409 | adapter_.OnOutputFormatRequest(format); |
| 410 | out_format = adapter_.AdaptFrameResolution(640, 360); |
| 411 | EXPECT_EQ(640, out_format.width); |
| 412 | EXPECT_EQ(360, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 415 | TEST_F(VideoAdapterTest, TestVGAWidth) { |
| 416 | // Reqeuested Output format is 640x360. |
| 417 | VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420); |
| 418 | adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30)); |
| 419 | adapter_.OnOutputFormatRequest(format); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 420 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 421 | VideoFormat out_format = adapter_.AdaptFrameResolution(640, 480); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 422 | // At this point, we have to adapt down to something lower. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 423 | EXPECT_EQ(480, out_format.width); |
| 424 | EXPECT_EQ(360, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 425 | |
| 426 | // But if frames come in at 640x360, we shouldn't adapt them down. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 427 | out_format = adapter_.AdaptFrameResolution(640, 360); |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 428 | EXPECT_EQ(640, out_format.width); |
| 429 | EXPECT_EQ(360, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 430 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 431 | out_format = adapter_.AdaptFrameResolution(640, 480); |
| 432 | EXPECT_EQ(480, out_format.width); |
| 433 | EXPECT_EQ(360, out_format.height); |
| 434 | } |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 435 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 436 | TEST_F(VideoAdapterTest, TestOnResolutionRequestInSmallSteps) { |
| 437 | VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 438 | EXPECT_EQ(1280, out_format.width); |
| 439 | EXPECT_EQ(720, out_format.height); |
| 440 | |
| 441 | // Adapt down one step. |
| 442 | adapter_.OnResolutionRequest(rtc::Optional<int>(1280 * 720 - 1), |
| 443 | rtc::Optional<int>()); |
| 444 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 445 | EXPECT_EQ(960, out_format.width); |
| 446 | EXPECT_EQ(540, out_format.height); |
| 447 | |
| 448 | // Adapt down one step more. |
| 449 | adapter_.OnResolutionRequest(rtc::Optional<int>(960 * 540 - 1), |
| 450 | rtc::Optional<int>()); |
| 451 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 452 | EXPECT_EQ(640, out_format.width); |
| 453 | EXPECT_EQ(360, out_format.height); |
| 454 | |
| 455 | // Adapt down one step more. |
| 456 | adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1), |
| 457 | rtc::Optional<int>()); |
| 458 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 459 | EXPECT_EQ(480, out_format.width); |
| 460 | EXPECT_EQ(270, out_format.height); |
| 461 | |
| 462 | // Adapt up one step. |
| 463 | adapter_.OnResolutionRequest(rtc::Optional<int>(), |
| 464 | rtc::Optional<int>(480 * 270)); |
| 465 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 466 | EXPECT_EQ(640, out_format.width); |
| 467 | EXPECT_EQ(360, out_format.height); |
| 468 | |
| 469 | // Adapt up one step more. |
| 470 | adapter_.OnResolutionRequest(rtc::Optional<int>(), |
| 471 | rtc::Optional<int>(640 * 360)); |
| 472 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 473 | EXPECT_EQ(960, out_format.width); |
| 474 | EXPECT_EQ(540, out_format.height); |
| 475 | |
| 476 | // Adapt up one step more. |
| 477 | adapter_.OnResolutionRequest(rtc::Optional<int>(), |
| 478 | rtc::Optional<int>(960 * 720)); |
| 479 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 480 | EXPECT_EQ(1280, out_format.width); |
| 481 | EXPECT_EQ(720, out_format.height); |
| 482 | } |
| 483 | |
| 484 | TEST_F(VideoAdapterTest, TestOnResolutionRequestMaxZero) { |
| 485 | VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 486 | EXPECT_EQ(1280, out_format.width); |
| 487 | EXPECT_EQ(720, out_format.height); |
| 488 | |
| 489 | adapter_.OnResolutionRequest(rtc::Optional<int>(0), rtc::Optional<int>()); |
| 490 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 491 | EXPECT_EQ(0, out_format.width); |
| 492 | EXPECT_EQ(0, out_format.height); |
| 493 | } |
| 494 | |
| 495 | TEST_F(VideoAdapterTest, TestOnResolutionRequestInLargeSteps) { |
| 496 | adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1), |
| 497 | rtc::Optional<int>()); |
| 498 | VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 499 | EXPECT_EQ(480, out_format.width); |
| 500 | EXPECT_EQ(270, out_format.height); |
| 501 | |
| 502 | adapter_.OnResolutionRequest(rtc::Optional<int>(), |
| 503 | rtc::Optional<int>(960 * 720)); |
| 504 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 505 | EXPECT_EQ(1280, out_format.width); |
| 506 | EXPECT_EQ(720, out_format.height); |
| 507 | } |
| 508 | |
| 509 | TEST_F(VideoAdapterTest, TestOnOutputFormatRequestCapsMaxResolution) { |
| 510 | adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1), |
| 511 | rtc::Optional<int>()); |
| 512 | VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 513 | EXPECT_EQ(480, out_format.width); |
| 514 | EXPECT_EQ(270, out_format.height); |
| 515 | |
| 516 | VideoFormat new_format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420); |
| 517 | adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30)); |
| 518 | adapter_.OnOutputFormatRequest(new_format); |
| 519 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 520 | EXPECT_EQ(480, out_format.width); |
| 521 | EXPECT_EQ(270, out_format.height); |
| 522 | |
| 523 | adapter_.OnResolutionRequest(rtc::Optional<int>(), |
| 524 | rtc::Optional<int>(960 * 720)); |
| 525 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 526 | EXPECT_EQ(640, out_format.width); |
| 527 | EXPECT_EQ(360, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 530 | TEST_F(VideoAdapterTest, TestOnResolutionRequestReset) { |
| 531 | VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 532 | EXPECT_EQ(1280, out_format.width); |
| 533 | EXPECT_EQ(720, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 534 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 535 | adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1), |
| 536 | rtc::Optional<int>()); |
| 537 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 538 | EXPECT_EQ(480, out_format.width); |
| 539 | EXPECT_EQ(270, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 540 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 541 | adapter_.OnResolutionRequest(rtc::Optional<int>(), rtc::Optional<int>()); |
| 542 | out_format = adapter_.AdaptFrameResolution(1280, 720); |
| 543 | EXPECT_EQ(1280, out_format.width); |
| 544 | EXPECT_EQ(720, out_format.height); |
buildbot@webrtc.org | 4f0d401 | 2014-08-07 04:47:36 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | } // namespace cricket |