blob: 32bf9c3e6366d8b55fc2da072665e354eb311268 [file] [log] [blame]
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +00001/*
2 * libjingle
3 * Copyright 2010 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// If we don't have a WebRtcVideoFrame, just skip all of these tests.
29#if defined(HAVE_WEBRTC_VIDEO)
30#include <limits.h> // For INT_MAX
31#include <string>
32#include <vector>
33
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +000034#include "talk/media/base/mediachannel.h"
35#include "talk/media/base/testutils.h"
36#include "talk/media/base/videoadapter.h"
37#include "talk/media/devices/filevideocapturer.h"
38#include "talk/media/webrtc/webrtcvideoframe.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000039#include "webrtc/base/gunit.h"
40#include "webrtc/base/logging.h"
41#include "webrtc/base/sigslot.h"
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +000042
43namespace cricket {
44
45namespace {
46 static const uint32 kWaitTimeout = 3000U; // 3 seconds.
47 static const uint32 kShortWaitTimeout = 1000U; // 1 second.
48 void UpdateCpuLoad(CoordinatedVideoAdapter* adapter,
49 int current_cpus, int max_cpus, float process_load, float system_load) {
50 adapter->set_cpu_load_min_samples(1);
51 adapter->OnCpuLoadUpdated(current_cpus, max_cpus,
52 process_load, system_load);
53 }
54}
55
56class VideoAdapterTest : public testing::Test {
57 public:
58 virtual void SetUp() {
59 capturer_.reset(new FileVideoCapturer);
60 EXPECT_TRUE(capturer_->Init(GetTestFilePath(
61 "captured-320x240-2s-48.frames")));
62 capture_format_ = capturer_->GetSupportedFormats()->at(0);
63 capture_format_.interval = VideoFormat::FpsToInterval(50);
64 adapter_.reset(new VideoAdapter());
65 adapter_->SetInputFormat(capture_format_);
66
67 listener_.reset(new VideoCapturerListener(adapter_.get()));
68 capturer_->SignalFrameCaptured.connect(
69 listener_.get(), &VideoCapturerListener::OnFrameCaptured);
70 }
71
pbos@webrtc.org75c3ec12014-08-27 18:16:13 +000072 virtual void TearDown() {
73 // Explicitly disconnect the VideoCapturer before to avoid data races
74 // (frames delivered to VideoCapturerListener while it's being destructed).
75 capturer_->SignalFrameCaptured.disconnect_all();
76 }
77
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +000078 protected:
79 class VideoCapturerListener: public sigslot::has_slots<> {
80 public:
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +000081 struct Stats {
82 int captured_frames;
83 int dropped_frames;
84 bool last_adapt_was_no_op;
85
86 int adapted_width;
87 int adapted_height;
88 };
89
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +000090 explicit VideoCapturerListener(VideoAdapter* adapter)
91 : video_adapter_(adapter),
92 adapted_frame_(NULL),
93 copied_output_frame_(),
94 captured_frames_(0),
95 dropped_frames_(0),
96 last_adapt_was_no_op_(false) {
97 }
98
99 void OnFrameCaptured(VideoCapturer* capturer,
100 const CapturedFrame* captured_frame) {
101 WebRtcVideoFrame temp_i420;
102 EXPECT_TRUE(temp_i420.Init(captured_frame,
perkj@webrtc.org1d828132015-03-03 06:44:06 +0000103 captured_frame->width, abs(captured_frame->height), true));
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000104 VideoFrame* out_frame = NULL;
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000105 rtc::CritScope lock(&crit_);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000106 EXPECT_TRUE(video_adapter_->AdaptFrame(&temp_i420, &out_frame));
107 if (out_frame) {
108 if (out_frame == &temp_i420) {
109 last_adapt_was_no_op_ = true;
110 copied_output_frame_.reset(temp_i420.Copy());
111 adapted_frame_ = copied_output_frame_.get();
112 } else {
113 last_adapt_was_no_op_ = false;
114 adapted_frame_ = out_frame;
115 copied_output_frame_.reset();
116 }
117 } else {
118 ++dropped_frames_;
119 }
120 ++captured_frames_;
121 }
122
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000123 Stats GetStats() {
124 rtc::CritScope lock(&crit_);
125 Stats stats;
126 stats.captured_frames = captured_frames_;
127 stats.dropped_frames = dropped_frames_;
128 stats.last_adapt_was_no_op = last_adapt_was_no_op_;
129 if (adapted_frame_ != NULL) {
pbos@webrtc.orgf21ac1f2014-08-26 12:46:57 +0000130 stats.adapted_width = static_cast<int>(adapted_frame_->GetWidth());
131 stats.adapted_height = static_cast<int>(adapted_frame_->GetHeight());
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000132 } else {
133 stats.adapted_width = stats.adapted_height = -1;
134 }
135
136 return stats;
137 }
138
139 VideoFrame* CopyAdaptedFrame() {
140 rtc::CritScope lock(&crit_);
141 if (adapted_frame_ == NULL) {
142 return NULL;
143 }
144 return adapted_frame_->Copy();
145 }
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000146
147 private:
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000148 rtc::CriticalSection crit_;
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000149 VideoAdapter* video_adapter_;
150 const VideoFrame* adapted_frame_;
151 rtc::scoped_ptr<VideoFrame> copied_output_frame_;
152 int captured_frames_;
153 int dropped_frames_;
154 bool last_adapt_was_no_op_;
155 };
156
157 class CpuAdapterListener: public sigslot::has_slots<> {
158 public:
159 CpuAdapterListener() : received_cpu_signal_(false) {}
160 void OnCpuAdaptationSignalled() { received_cpu_signal_ = true; }
161 bool received_cpu_signal() { return received_cpu_signal_; }
162 private:
163 bool received_cpu_signal_;
164 };
165
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000166 void VerifyAdaptedResolution(const VideoCapturerListener::Stats& stats,
167 int width,
168 int height) {
169 EXPECT_EQ(width, stats.adapted_width);
170 EXPECT_EQ(height, stats.adapted_height);
171 }
172
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000173 rtc::scoped_ptr<FileVideoCapturer> capturer_;
174 rtc::scoped_ptr<VideoAdapter> adapter_;
175 rtc::scoped_ptr<VideoCapturerListener> listener_;
176 VideoFormat capture_format_;
177};
178
179
180// Test adapter remembers exact pixel count
181TEST_F(VideoAdapterTest, AdaptNumPixels) {
182 adapter_->SetOutputNumPixels(123456);
183 EXPECT_EQ(123456, adapter_->GetOutputNumPixels());
184}
185
186// Test adapter is constructed but not activated. Expect no frame drop and no
187// resolution change.
188TEST_F(VideoAdapterTest, AdaptInactive) {
189 // Output resolution is not set.
190 EXPECT_EQ(INT_MAX, adapter_->GetOutputNumPixels());
191
192 // Call Adapter with some frames.
193 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
194 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000195 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000196
197 // Verify no frame drop and no resolution change.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000198 VideoCapturerListener::Stats stats = listener_->GetStats();
199 EXPECT_GE(stats.captured_frames, 10);
200 EXPECT_EQ(0, stats.dropped_frames);
201 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000202}
203
204// Do not adapt the frame rate or the resolution. Expect no frame drop and no
205// resolution change.
206TEST_F(VideoAdapterTest, AdaptNothing) {
207 adapter_->SetOutputFormat(capture_format_);
208 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
209 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000210 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000211
212 // Verify no frame drop and no resolution change.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000213 VideoCapturerListener::Stats stats = listener_->GetStats();
214 EXPECT_GE(stats.captured_frames, 10);
215 EXPECT_EQ(0, stats.dropped_frames);
216 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height);
217 EXPECT_TRUE(stats.last_adapt_was_no_op);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000218}
219
220TEST_F(VideoAdapterTest, AdaptZeroInterval) {
221 VideoFormat format = capturer_->GetSupportedFormats()->at(0);
222 format.interval = 0;
223 adapter_->SetInputFormat(format);
224 adapter_->SetOutputFormat(format);
225 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
226 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000227 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000228
229 // Verify no crash and that frames aren't dropped.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000230 VideoCapturerListener::Stats stats = listener_->GetStats();
231 EXPECT_GE(stats.captured_frames, 10);
232 EXPECT_EQ(0, stats.dropped_frames);
233 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000234}
235
236// Adapt the frame rate to be half of the capture rate at the beginning. Expect
237// the number of dropped frames to be half of the number the captured frames.
238TEST_F(VideoAdapterTest, AdaptFramerate) {
239 VideoFormat request_format = capture_format_;
240 request_format.interval *= 2;
241 adapter_->SetOutputFormat(request_format);
242 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
243 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000244 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000245
246 // Verify frame drop and no resolution change.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000247 VideoCapturerListener::Stats stats = listener_->GetStats();
248 EXPECT_GE(stats.captured_frames, 10);
249 EXPECT_EQ(stats.captured_frames / 2, stats.dropped_frames);
250 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000251}
252
253// Adapt the frame rate to be half of the capture rate at the beginning. Expect
254// the number of dropped frames to be half of the number the captured frames.
255TEST_F(VideoAdapterTest, AdaptFramerateVariable) {
256 VideoFormat request_format = capture_format_;
257 request_format.interval = request_format.interval * 3 / 2;
258 adapter_->SetOutputFormat(request_format);
259 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
260 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000261 listener_->GetStats().captured_frames >= 30, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000262
263 // Verify frame drop and no resolution change.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000264 VideoCapturerListener::Stats stats = listener_->GetStats();
265 EXPECT_GE(stats.captured_frames, 30);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000266 // Verify 2 / 3 kept (20) and 1 / 3 dropped (10).
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000267 EXPECT_EQ(stats.captured_frames * 1 / 3, stats.dropped_frames);
268 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000269}
270
271// Adapt the frame rate to be half of the capture rate after capturing no less
272// than 10 frames. Expect no frame dropped before adaptation and frame dropped
273// after adaptation.
274TEST_F(VideoAdapterTest, AdaptFramerateOntheFly) {
275 VideoFormat request_format = capture_format_;
276 adapter_->SetOutputFormat(request_format);
277 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
278 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000279 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000280
281 // Verify no frame drop before adaptation.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000282 EXPECT_EQ(0, listener_->GetStats().dropped_frames);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000283
284 // Adapat the frame rate.
285 request_format.interval *= 2;
286 adapter_->SetOutputFormat(request_format);
287
288 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000289 listener_->GetStats().captured_frames >= 20, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000290
291 // Verify frame drop after adaptation.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000292 EXPECT_GT(listener_->GetStats().dropped_frames, 0);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000293}
294
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +0000295// Set a very high output pixel resolution. Expect no resolution change.
296TEST_F(VideoAdapterTest, AdaptFrameResolutionHighLimit) {
297 adapter_->SetOutputNumPixels(INT_MAX);
298 VideoFormat adapted_format = adapter_->AdaptFrameResolution(
299 capture_format_.width, capture_format_.height);
300 EXPECT_EQ(capture_format_.width, adapted_format.width);
301 EXPECT_EQ(capture_format_.height, adapted_format.height);
302
303 adapter_->SetOutputNumPixels(987654321);
304 adapted_format = capture_format_,
305 adapter_->AdaptFrameResolution(capture_format_.width, capture_format_.height);
306 EXPECT_EQ(capture_format_.width, adapted_format.width);
307 EXPECT_EQ(capture_format_.height, adapted_format.height);
308}
309
310// Adapt the frame resolution to be the same as capture resolution. Expect no
311// resolution change.
312TEST_F(VideoAdapterTest, AdaptFrameResolutionIdentical) {
313 adapter_->SetOutputFormat(capture_format_);
314 const VideoFormat adapted_format = adapter_->AdaptFrameResolution(
315 capture_format_.width, capture_format_.height);
316 EXPECT_EQ(capture_format_.width, adapted_format.width);
317 EXPECT_EQ(capture_format_.height, adapted_format.height);
318}
319
320// Adapt the frame resolution to be a quarter of the capture resolution. Expect
321// resolution change.
322TEST_F(VideoAdapterTest, AdaptFrameResolutionQuarter) {
323 VideoFormat request_format = capture_format_;
324 request_format.width /= 2;
325 request_format.height /= 2;
326 adapter_->SetOutputFormat(request_format);
327 const VideoFormat adapted_format = adapter_->AdaptFrameResolution(
328 request_format.width, request_format.height);
329 EXPECT_EQ(request_format.width, adapted_format.width);
330 EXPECT_EQ(request_format.height, adapted_format.height);
331}
332
333// Adapt the pixel resolution to 0. Expect frame drop.
334TEST_F(VideoAdapterTest, AdaptFrameResolutionDrop) {
335 adapter_->SetOutputNumPixels(0);
336 EXPECT_TRUE(
337 adapter_->AdaptFrameResolution(capture_format_.width,
338 capture_format_.height).IsSize0x0());
339}
340
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000341// Adapt the frame resolution to be a quarter of the capture resolution at the
342// beginning. Expect resolution change.
343TEST_F(VideoAdapterTest, AdaptResolution) {
344 VideoFormat request_format = capture_format_;
345 request_format.width /= 2;
346 request_format.height /= 2;
347 adapter_->SetOutputFormat(request_format);
348 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
349 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000350 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000351
352 // Verify no frame drop and resolution change.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000353 VideoCapturerListener::Stats stats = listener_->GetStats();
354 EXPECT_EQ(0, stats.dropped_frames);
355 VerifyAdaptedResolution(stats, request_format.width, request_format.height);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000356}
357
358// Adapt the frame resolution to half width. Expect resolution change.
359TEST_F(VideoAdapterTest, AdaptResolutionNarrow) {
360 VideoFormat request_format = capture_format_;
361 request_format.width /= 2;
362 adapter_->set_scale_third(true);
363 adapter_->SetOutputFormat(request_format);
364 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
365 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000366 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000367
368 // Verify resolution change.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000369 VerifyAdaptedResolution(listener_->GetStats(), 213, 160);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000370}
371
372// Adapt the frame resolution to half height. Expect resolution change.
373TEST_F(VideoAdapterTest, AdaptResolutionWide) {
374 VideoFormat request_format = capture_format_;
375 request_format.height /= 2;
376 adapter_->set_scale_third(true);
377 adapter_->SetOutputFormat(request_format);
378 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
379 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000380 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000381
382 // Verify resolution change.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000383 VerifyAdaptedResolution(listener_->GetStats(), 213, 160);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000384}
385
386// Adapt the frame resolution to be a quarter of the capture resolution after
387// capturing no less than 10 frames. Expect no resolution change before
388// adaptation and resolution change after adaptation.
389TEST_F(VideoAdapterTest, AdaptResolutionOnTheFly) {
390 VideoFormat request_format = capture_format_;
391 adapter_->SetOutputFormat(request_format);
392 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
393 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000394 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000395
396 // Verify no resolution change before adaptation.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000397 VerifyAdaptedResolution(
398 listener_->GetStats(), request_format.width, request_format.height);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000399
400 // Adapt the frame resolution.
401 request_format.width /= 2;
402 request_format.height /= 2;
403 adapter_->SetOutputFormat(request_format);
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000404 int captured_frames = listener_->GetStats().captured_frames;
405 EXPECT_TRUE_WAIT(
406 !capturer_->IsRunning() ||
407 listener_->GetStats().captured_frames >= captured_frames + 10,
408 kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000409
410 // Verify resolution change after adaptation.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000411 VerifyAdaptedResolution(
412 listener_->GetStats(), request_format.width, request_format.height);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000413}
414
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000415// Drop all frames.
416TEST_F(VideoAdapterTest, DropAllFrames) {
417 VideoFormat format; // with resolution 0x0.
418 adapter_->SetOutputFormat(format);
419 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
420 EXPECT_TRUE_WAIT(!capturer_->IsRunning() ||
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000421 listener_->GetStats().captured_frames >= 10, kWaitTimeout);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000422
423 // Verify all frames are dropped.
pbos@webrtc.orgc9b3f772014-08-26 12:33:18 +0000424 VideoCapturerListener::Stats stats = listener_->GetStats();
425 EXPECT_GE(stats.captured_frames, 10);
426 EXPECT_EQ(stats.captured_frames, stats.dropped_frames);
buildbot@webrtc.org4f0d4012014-08-07 04:47:36 +0000427}
428
429TEST(CoordinatedVideoAdapterTest, TestCoordinatedWithoutCpuAdaptation) {
430 CoordinatedVideoAdapter adapter;
431 adapter.set_cpu_adaptation(false);
432
433 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420);
434 adapter.SetInputFormat(format);
435 adapter.set_scale_third(true);
436 EXPECT_EQ(format, adapter.input_format());
437 EXPECT_TRUE(adapter.output_format().IsSize0x0());
438
439 // Server format request 640x400.
440 format.height = 400;
441 adapter.OnOutputFormatRequest(format);
442 EXPECT_EQ(640, adapter.output_format().width);
443 EXPECT_EQ(400, adapter.output_format().height);
444
445 // Server format request 1280x720, higher than input. Adapt nothing.
446 format.width = 1280;
447 format.height = 720;
448 adapter.OnOutputFormatRequest(format);
449 EXPECT_EQ(640, adapter.output_format().width);
450 EXPECT_EQ(400, adapter.output_format().height);
451
452 // Cpu load is high, but cpu adaptation is disabled. Adapt nothing.
453 adapter.OnCpuLoadUpdated(1, 1, 0.99f, 0.99f);
454 EXPECT_EQ(640, adapter.output_format().width);
455 EXPECT_EQ(400, adapter.output_format().height);
456
457 // Encoder resolution request: downgrade with different size. Adapt nothing.
458 adapter.OnEncoderResolutionRequest(320, 200,
459 CoordinatedVideoAdapter::DOWNGRADE);
460 EXPECT_EQ(640, adapter.output_format().width);
461 EXPECT_EQ(400, adapter.output_format().height);
462
463 // Encoder resolution request: downgrade.
464 adapter.OnEncoderResolutionRequest(640, 400,
465 CoordinatedVideoAdapter::DOWNGRADE);
466 EXPECT_EQ(480, adapter.output_format().width);
467 EXPECT_EQ(300, adapter.output_format().height);
468
469 // Encoder resolution request: downgrade. But GD off. Adapt nothing.
470 adapter.set_gd_adaptation(false);
471 adapter.OnEncoderResolutionRequest(480, 300,
472 CoordinatedVideoAdapter::DOWNGRADE);
473 EXPECT_EQ(480, adapter.output_format().width);
474 EXPECT_EQ(300, adapter.output_format().height);
475 adapter.set_gd_adaptation(true);
476
477 // Encoder resolution request: downgrade.
478 adapter.OnEncoderResolutionRequest(480, 300,
479 CoordinatedVideoAdapter::DOWNGRADE);
480 EXPECT_EQ(320, adapter.output_format().width);
481 EXPECT_EQ(200, adapter.output_format().height);
482
483 // Encoder resolution request: keep. Adapt nothing.
484 adapter.OnEncoderResolutionRequest(320, 200,
485 CoordinatedVideoAdapter::KEEP);
486 EXPECT_EQ(320, adapter.output_format().width);
487 EXPECT_EQ(200, adapter.output_format().height);
488
489 // Encoder resolution request: upgrade.
490 adapter.OnEncoderResolutionRequest(320, 200,
491 CoordinatedVideoAdapter::UPGRADE);
492 EXPECT_EQ(480, adapter.output_format().width);
493 EXPECT_EQ(300, adapter.output_format().height);
494
495 // Server format request 0x0.
496 format.width = 0;
497 format.height = 0;
498 adapter.OnOutputFormatRequest(format);
499 EXPECT_TRUE(adapter.output_format().IsSize0x0());
500
501 // Server format request 320x200.
502 format.width = 320;
503 format.height = 200;
504 adapter.OnOutputFormatRequest(format);
505 EXPECT_EQ(320, adapter.output_format().width);
506 EXPECT_EQ(200, adapter.output_format().height);
507
508 // Server format request 160x100. But view disabled. Adapt nothing.
509 adapter.set_view_adaptation(false);
510 format.width = 160;
511 format.height = 100;
512 adapter.OnOutputFormatRequest(format);
513 EXPECT_EQ(320, adapter.output_format().width);
514 EXPECT_EQ(200, adapter.output_format().height);
515 adapter.set_view_adaptation(true);
516
517 // Enable View Switch. Expect adapt down.
518 adapter.set_view_switch(true);
519 format.width = 160;
520 format.height = 100;
521 adapter.OnOutputFormatRequest(format);
522 EXPECT_EQ(160, adapter.output_format().width);
523 EXPECT_EQ(100, adapter.output_format().height);
524
525 // Encoder resolution request: upgrade. Adapt nothing.
526 adapter.OnEncoderResolutionRequest(160, 100,
527 CoordinatedVideoAdapter::UPGRADE);
528 EXPECT_EQ(160, adapter.output_format().width);
529 EXPECT_EQ(100, adapter.output_format().height);
530
531 // Request View of 2 / 3. Expect adapt down.
532 adapter.set_view_switch(true);
533 format.width = (640 * 2 + 1) / 3;
534 format.height = (400 * 2 + 1) / 3;
535 adapter.OnOutputFormatRequest(format);
536 EXPECT_EQ((640 * 2 + 1) / 3, adapter.output_format().width);
537 EXPECT_EQ((400 * 2 + 1) / 3, adapter.output_format().height);
538
539
540 // Request View of 3 / 8. Expect adapt down.
541 adapter.set_view_switch(true);
542 format.width = 640 * 3 / 8;
543 format.height = 400 * 3 / 8;
544 adapter.OnOutputFormatRequest(format);
545 EXPECT_EQ(640 * 3 / 8, adapter.output_format().width);
546 EXPECT_EQ(400 * 3 / 8, adapter.output_format().height);
547
548 // View Switch back up. Expect adapt.
549 format.width = 320;
550 format.height = 200;
551 adapter.OnOutputFormatRequest(format);
552 EXPECT_EQ(320, adapter.output_format().width);
553 EXPECT_EQ(200, adapter.output_format().height);
554
555 adapter.set_view_switch(false);
556
557 // Encoder resolution request: upgrade. Constrained by server request.
558 adapter.OnEncoderResolutionRequest(320, 200,
559 CoordinatedVideoAdapter::UPGRADE);
560 EXPECT_EQ(320, adapter.output_format().width);
561 EXPECT_EQ(200, adapter.output_format().height);
562
563 // Server format request 480x300.
564 format.width = 480;
565 format.height = 300;
566 adapter.OnOutputFormatRequest(format);
567 EXPECT_EQ(480, adapter.output_format().width);
568 EXPECT_EQ(300, adapter.output_format().height);
569}
570
571TEST(CoordinatedVideoAdapterTest, TestCoordinatedWithCpuAdaptation) {
572 CoordinatedVideoAdapter adapter;
573 adapter.set_cpu_adaptation(true);
574 EXPECT_FALSE(adapter.cpu_smoothing());
575 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420);
576 adapter.SetInputFormat(format);
577
578 // Server format request 640x400.
579 format.height = 400;
580 adapter.OnOutputFormatRequest(format);
581 EXPECT_EQ(640, adapter.output_format().width);
582 EXPECT_EQ(400, adapter.output_format().height);
583
584 // Process load is medium, but system load is high. Downgrade.
585 UpdateCpuLoad(&adapter, 1, 1, 0.55f, 0.98f);
586 EXPECT_EQ(480, adapter.output_format().width);
587 EXPECT_EQ(300, adapter.output_format().height);
588
589 // CPU high, but cpu adaptation disabled. Adapt nothing.
590 adapter.set_cpu_adaptation(false);
591 adapter.OnCpuLoadUpdated(1, 1, 0.55f, 0.98f);
592 EXPECT_EQ(480, adapter.output_format().width);
593 EXPECT_EQ(300, adapter.output_format().height);
594 adapter.set_cpu_adaptation(true);
595
596 // System load is high, but time has not elaspsed. Adapt nothing.
597 adapter.set_cpu_load_min_samples(2);
598 adapter.OnCpuLoadUpdated(1, 1, 0.55f, 0.98f);
599 EXPECT_EQ(480, adapter.output_format().width);
600 EXPECT_EQ(300, adapter.output_format().height);
601
602 // Process load is medium, but system load is high. Downgrade.
603 UpdateCpuLoad(&adapter, 1, 1, 0.55f, 0.98f);
604 EXPECT_EQ(320, adapter.output_format().width);
605 EXPECT_EQ(200, adapter.output_format().height);
606
607 // Test reason for adapting is CPU.
608 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU,
609 adapter.adapt_reason());
610
611 // Server format request 320x200. Same as CPU. Do nothing.
612 format.width = 320;
613 format.height = 200;
614 adapter.OnOutputFormatRequest(format);
615 EXPECT_EQ(320, adapter.output_format().width);
616 EXPECT_EQ(200, adapter.output_format().height);
617
618 // Test reason for adapting is CPU and VIEW.
619 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU +
620 CoordinatedVideoAdapter::ADAPTREASON_VIEW,
621 adapter.adapt_reason());
622
623 // Process load and system load are normal. Adapt nothing.
624 UpdateCpuLoad(&adapter, 1, 1, 0.5f, 0.8f);
625 EXPECT_EQ(320, adapter.output_format().width);
626 EXPECT_EQ(200, adapter.output_format().height);
627
628 // Process load and system load are low, but view is still low. Adapt nothing.
629 UpdateCpuLoad(&adapter, 1, 1, 0.2f, 0.3f);
630 EXPECT_EQ(320, adapter.output_format().width);
631 EXPECT_EQ(200, adapter.output_format().height);
632
633 // Test reason for adapting is VIEW.
634 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW,
635 adapter.adapt_reason());
636
637 // Server format request 640x400. Cpu is still low. Upgrade.
638 format.width = 640;
639 format.height = 400;
640 adapter.OnOutputFormatRequest(format);
641 EXPECT_EQ(480, adapter.output_format().width);
642 EXPECT_EQ(300, adapter.output_format().height);
643
644 // Test reason for adapting is CPU.
645 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU,
646 adapter.adapt_reason());
647
648 // Encoder resolution request: downgrade.
649 adapter.OnEncoderResolutionRequest(480, 300,
650 CoordinatedVideoAdapter::DOWNGRADE);
651 EXPECT_EQ(320, adapter.output_format().width);
652 EXPECT_EQ(200, adapter.output_format().height);
653
654 // Test reason for adapting is BANDWIDTH.
655 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_BANDWIDTH,
656 adapter.adapt_reason());
657
658 // Process load and system load are low. Constrained by GD. Adapt nothing
659 adapter.OnCpuLoadUpdated(1, 1, 0.2f, 0.3f);
660 EXPECT_EQ(320, adapter.output_format().width);
661 EXPECT_EQ(200, adapter.output_format().height);
662
663 // Encoder resolution request: upgrade.
664 adapter.OnEncoderResolutionRequest(320, 200,
665 CoordinatedVideoAdapter::UPGRADE);
666 EXPECT_EQ(480, adapter.output_format().width);
667 EXPECT_EQ(300, adapter.output_format().height);
668
669 // Encoder resolution request: upgrade. Constrained by CPU.
670 adapter.OnEncoderResolutionRequest(480, 300,
671 CoordinatedVideoAdapter::UPGRADE);
672 EXPECT_EQ(480, adapter.output_format().width);
673 EXPECT_EQ(300, adapter.output_format().height);
674
675 // Server format request 640x400. Constrained by CPU.
676 format.width = 640;
677 format.height = 400;
678 adapter.OnOutputFormatRequest(format);
679 EXPECT_EQ(480, adapter.output_format().width);
680 EXPECT_EQ(300, adapter.output_format().height);
681}
682
683TEST(CoordinatedVideoAdapterTest, TestCoordinatedWithCpuRequest) {
684 CoordinatedVideoAdapter adapter;
685 adapter.set_cpu_adaptation(true);
686 EXPECT_FALSE(adapter.cpu_smoothing());
687 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420);
688 adapter.SetInputFormat(format);
689
690 // Server format request 640x400.
691 format.height = 400;
692 adapter.OnOutputFormatRequest(format);
693 EXPECT_EQ(640, adapter.output_format().width);
694 EXPECT_EQ(400, adapter.output_format().height);
695
696 // CPU resolution request: downgrade. Adapt down.
697 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
698 EXPECT_EQ(480, adapter.output_format().width);
699 EXPECT_EQ(300, adapter.output_format().height);
700
701 // CPU resolution request: keep. Do nothing.
702 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::KEEP);
703 EXPECT_EQ(480, adapter.output_format().width);
704 EXPECT_EQ(300, adapter.output_format().height);
705
706 // CPU resolution request: downgrade, but cpu adaptation disabled.
707 // Adapt nothing.
708 adapter.set_cpu_adaptation(false);
709 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
710 EXPECT_EQ(480, adapter.output_format().width);
711 EXPECT_EQ(300, adapter.output_format().height);
712
713 // CPU resolution request: downgrade. Adapt down.
714 adapter.set_cpu_adaptation(true);
715 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
716 EXPECT_EQ(320, adapter.output_format().width);
717 EXPECT_EQ(200, adapter.output_format().height);
718
719 // Test reason for adapting is CPU.
720 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU,
721 adapter.adapt_reason());
722
723 // CPU resolution request: downgrade, but already at minimum. Do nothing.
724 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
725 EXPECT_EQ(320, adapter.output_format().width);
726 EXPECT_EQ(200, adapter.output_format().height);
727
728 // Server format request 320x200. Same as CPU. Do nothing.
729 format.width = 320;
730 format.height = 200;
731 adapter.OnOutputFormatRequest(format);
732 EXPECT_EQ(320, adapter.output_format().width);
733 EXPECT_EQ(200, adapter.output_format().height);
734
735 // Test reason for adapting is CPU and VIEW.
736 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU +
737 CoordinatedVideoAdapter::ADAPTREASON_VIEW,
738 adapter.adapt_reason());
739
740 // CPU resolution request: upgrade, but view request still low. Do nothing.
741 adapter.OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
742 EXPECT_EQ(320, adapter.output_format().width);
743 EXPECT_EQ(200, adapter.output_format().height);
744
745 // Test reason for adapting is VIEW.
746 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW,
747 adapter.adapt_reason());
748
749 // Server format request 640x400. Cpu is still low. Upgrade.
750 format.width = 640;
751 format.height = 400;
752 adapter.OnOutputFormatRequest(format);
753 EXPECT_EQ(480, adapter.output_format().width);
754 EXPECT_EQ(300, adapter.output_format().height);
755
756 // Test reason for adapting is CPU.
757 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU,
758 adapter.adapt_reason());
759
760 // Encoder resolution request: downgrade.
761 adapter.OnEncoderResolutionRequest(480, 300,
762 CoordinatedVideoAdapter::DOWNGRADE);
763 EXPECT_EQ(320, adapter.output_format().width);
764 EXPECT_EQ(200, adapter.output_format().height);
765
766 // Test reason for adapting is BANDWIDTH.
767 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_BANDWIDTH,
768 adapter.adapt_reason());
769
770 // Process load and system load are low. Constrained by GD. Adapt nothing
771 adapter.OnCpuLoadUpdated(1, 1, 0.2f, 0.3f);
772 EXPECT_EQ(320, adapter.output_format().width);
773 EXPECT_EQ(200, adapter.output_format().height);
774
775 // Encoder resolution request: upgrade.
776 adapter.OnEncoderResolutionRequest(320, 200,
777 CoordinatedVideoAdapter::UPGRADE);
778 EXPECT_EQ(480, adapter.output_format().width);
779 EXPECT_EQ(300, adapter.output_format().height);
780
781 // Encoder resolution request: upgrade. Constrained by CPU.
782 adapter.OnEncoderResolutionRequest(480, 300,
783 CoordinatedVideoAdapter::UPGRADE);
784 EXPECT_EQ(480, adapter.output_format().width);
785 EXPECT_EQ(300, adapter.output_format().height);
786
787 // Server format request 640x400. Constrained by CPU.
788 format.width = 640;
789 format.height = 400;
790 adapter.OnOutputFormatRequest(format);
791 EXPECT_EQ(480, adapter.output_format().width);
792 EXPECT_EQ(300, adapter.output_format().height);
793}
794
795TEST(CoordinatedVideoAdapterTest, TestViewRequestPlusCameraSwitch) {
796 CoordinatedVideoAdapter adapter;
797 adapter.set_view_switch(true);
798
799 // Start at HD.
800 VideoFormat format(1280, 720, VideoFormat::FpsToInterval(30), FOURCC_I420);
801 adapter.SetInputFormat(format);
802 EXPECT_EQ(format, adapter.input_format());
803 EXPECT_TRUE(adapter.output_format().IsSize0x0());
804
805 // View request for VGA.
806 format.width = 640;
807 format.height = 360;
808 adapter.OnOutputFormatRequest(format);
809 EXPECT_EQ(640, adapter.output_format().width);
810 EXPECT_EQ(360, adapter.output_format().height);
811 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW, adapter.adapt_reason());
812
813 // Now, the camera reopens at VGA.
814 // Both the frame and the output format should be 640x360.
815 WebRtcVideoFrame in_frame;
816 in_frame.InitToBlack(640, 360, 1, 1, 33, 33);
817 VideoFrame* out_frame;
818 adapter.AdaptFrame(&in_frame, &out_frame);
819 EXPECT_EQ(640u, out_frame->GetWidth());
820 EXPECT_EQ(360u, out_frame->GetHeight());
821 // At this point, the view is no longer adapted, since the input has resized
822 // small enough to fit the last view request.
823 EXPECT_EQ(0, adapter.adapt_reason());
824
825 // And another view request comes in for 640x360, which should have no
826 // real impact.
827 adapter.OnOutputFormatRequest(format);
828 EXPECT_EQ(640, adapter.output_format().width);
829 EXPECT_EQ(360, adapter.output_format().height);
830 EXPECT_EQ(0, adapter.adapt_reason());
831}
832
833TEST(CoordinatedVideoAdapterTest, TestVGAWidth) {
834 CoordinatedVideoAdapter adapter;
835 adapter.set_view_switch(true);
836
837 // Start at 640x480, for cameras that don't support 640x360.
838 VideoFormat format(640, 480, VideoFormat::FpsToInterval(30), FOURCC_I420);
839 adapter.SetInputFormat(format);
840 EXPECT_EQ(format, adapter.input_format());
841 EXPECT_TRUE(adapter.output_format().IsSize0x0());
842
843 // Output format is 640x360, though.
844 format.width = 640;
845 format.height = 360;
846 adapter.SetOutputFormat(format);
847
848 // And also a view request comes for 640x360.
849 adapter.OnOutputFormatRequest(format);
850 // At this point, we have to adapt down to something lower.
851 EXPECT_EQ(480, adapter.output_format().width);
852 EXPECT_EQ(360, adapter.output_format().height);
853
854 // But if frames come in at 640x360, we shouldn't adapt them down.
855 // Fake a 640x360 frame.
856 WebRtcVideoFrame in_frame;
857 in_frame.InitToBlack(640, 360, 1, 1, 33, 33);
858 VideoFrame* out_frame;
859 adapter.AdaptFrame(&in_frame, &out_frame);
860
861 EXPECT_EQ(640u, out_frame->GetWidth());
862 EXPECT_EQ(360u, out_frame->GetHeight());
863
864 // Similarly, no-op adapt requests for other reasons shouldn't change
865 // adaptation state (before a previous bug, the previous EXPECTs would
866 // fail and the following would succeed, as the no-op CPU request would
867 // fix the adaptation state).
868 adapter.set_cpu_adaptation(true);
869 UpdateCpuLoad(&adapter, 1, 1, 0.7f, 0.7f);
870 adapter.AdaptFrame(&in_frame, &out_frame);
871
872 EXPECT_EQ(640u, out_frame->GetWidth());
873 EXPECT_EQ(360u, out_frame->GetHeight());
874}
875
876// When adapting resolution for CPU or GD, the quantity of pixels that the
877// request is based on is reduced to half or double, and then an actual
878// resolution is snapped to, rounding to the closest actual resolution.
879// This works well for some tolerance to 3/4, odd widths and aspect ratios
880// that dont exactly match, but is not best behavior for ViewRequests which
881// need to be be strictly respected to avoid going over the resolution budget
882// given to the codec - 854x480 total pixels.
883// ViewRequest must find a lower resolution.
884TEST(CoordinatedVideoAdapterTest, TestCoordinatedViewRequestDown) {
885 CoordinatedVideoAdapter adapter;
886 adapter.set_cpu_adaptation(false);
887
888 VideoFormat format(960, 540, VideoFormat::FpsToInterval(30), FOURCC_I420);
889 adapter.SetInputFormat(format);
890 adapter.set_scale_third(true);
891 EXPECT_EQ(format, adapter.input_format());
892 EXPECT_TRUE(adapter.output_format().IsSize0x0());
893
894 // Server format request 640x400. Expect HVGA.
895 format.width = 640;
896 format.height = 400;
897 adapter.OnOutputFormatRequest(format);
898 EXPECT_EQ(640, adapter.output_format().width);
899 EXPECT_EQ(360, adapter.output_format().height);
900
901 // Test reason for adapting is VIEW.
902 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW, adapter.adapt_reason());
903}
904
905// Test that we downgrade video for cpu up to two times.
906TEST(CoordinatedVideoAdapterTest, TestCpuDowngradeTimes) {
907 CoordinatedVideoAdapter adapter;
908 adapter.set_cpu_adaptation(true);
909 EXPECT_FALSE(adapter.cpu_smoothing());
910 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420);
911 adapter.SetInputFormat(format);
912
913 // Server format request 640x400.
914 format.height = 400;
915 adapter.OnOutputFormatRequest(format);
916 EXPECT_EQ(640, adapter.output_format().width);
917 EXPECT_EQ(400, adapter.output_format().height);
918
919 // Process load and system load are low. Do not change the cpu desired format
920 // and do not adapt.
921 adapter.OnCpuLoadUpdated(1, 1, 0.2f, 0.3f);
922 EXPECT_EQ(640, adapter.output_format().width);
923 EXPECT_EQ(400, adapter.output_format().height);
924
925 // System load is high. Downgrade.
926 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
927 EXPECT_EQ(480, adapter.output_format().width);
928 EXPECT_EQ(300, adapter.output_format().height);
929
930 // System load is high. Downgrade again.
931 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
932 EXPECT_EQ(320, adapter.output_format().width);
933 EXPECT_EQ(200, adapter.output_format().height);
934
935 // System load is still high. Do not downgrade any more.
936 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
937 EXPECT_EQ(320, adapter.output_format().width);
938 EXPECT_EQ(200, adapter.output_format().height);
939
940 // Process load and system load are low. Upgrade.
941 UpdateCpuLoad(&adapter, 1, 1, 0.2f, 0.3f);
942 EXPECT_EQ(480, adapter.output_format().width);
943 EXPECT_EQ(300, adapter.output_format().height);
944
945 // System load is high. Downgrade.
946 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
947 EXPECT_EQ(320, adapter.output_format().width);
948 EXPECT_EQ(200, adapter.output_format().height);
949
950 // System load is still high. Do not downgrade any more.
951 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
952 EXPECT_EQ(320, adapter.output_format().width);
953 EXPECT_EQ(200, adapter.output_format().height);
954}
955
956// Test that we respect CPU adapter threshold values.
957TEST(CoordinatedVideoAdapterTest, TestAdapterCpuThreshold) {
958 CoordinatedVideoAdapter adapter;
959 adapter.set_cpu_adaptation(true);
960 EXPECT_FALSE(adapter.cpu_smoothing());
961 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420);
962 adapter.SetInputFormat(format);
963
964 // Server format request 640x400.
965 format.height = 400;
966 adapter.OnOutputFormatRequest(format);
967 EXPECT_EQ(640, adapter.output_format().width);
968 EXPECT_EQ(400, adapter.output_format().height);
969
970 // Process load and system load are low. Do not change the cpu desired format
971 // and do not adapt.
972 adapter.OnCpuLoadUpdated(1, 1, 0.2f, 0.3f);
973 EXPECT_EQ(640, adapter.output_format().width);
974 EXPECT_EQ(400, adapter.output_format().height);
975
976 // System load is high. Downgrade.
977 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
978 EXPECT_EQ(480, adapter.output_format().width);
979 EXPECT_EQ(300, adapter.output_format().height);
980
981 // Test reason for adapting is CPU.
982 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_CPU, adapter.adapt_reason());
983
984 // System load is high. Normally downgrade but threshold is high. Do nothing.
985 adapter.set_high_system_threshold(0.98f); // Set threshold high.
986 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
987 EXPECT_EQ(480, adapter.output_format().width);
988 EXPECT_EQ(300, adapter.output_format().height);
989
990 // System load is medium. Normally do nothing, threshold is low. Adapt down.
991 adapter.set_high_system_threshold(0.75f); // Set threshold low.
992 UpdateCpuLoad(&adapter, 1, 1, 0.8f, 0.8f);
993 EXPECT_EQ(320, adapter.output_format().width);
994 EXPECT_EQ(200, adapter.output_format().height);
995}
996
997
998// Test that for an upgrade cpu request, we actually upgrade the desired format;
999// for a downgrade request, we downgrade from the output format.
1000TEST(CoordinatedVideoAdapterTest, TestRealCpuUpgrade) {
1001 CoordinatedVideoAdapter adapter;
1002 adapter.set_cpu_adaptation(true);
1003 adapter.set_cpu_smoothing(true);
1004 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420);
1005 adapter.SetInputFormat(format);
1006
1007 // Server format request 640x400.
1008 format.width = 640;
1009 format.height = 400;
1010 adapter.OnOutputFormatRequest(format);
1011 EXPECT_EQ(640, adapter.output_format().width);
1012 EXPECT_EQ(400, adapter.output_format().height);
1013
1014 // Process load and system load are low. Do not change the cpu desired format
1015 // and do not adapt.
1016 UpdateCpuLoad(&adapter, 1, 1, 0.2f, 0.3f);
1017 EXPECT_EQ(640, adapter.output_format().width);
1018 EXPECT_EQ(400, adapter.output_format().height);
1019
1020 // Server format request 320x200.
1021 format.width = 320;
1022 format.height = 200;
1023 adapter.OnOutputFormatRequest(format);
1024 EXPECT_EQ(320, adapter.output_format().width);
1025 EXPECT_EQ(200, adapter.output_format().height);
1026
1027 // Process load and system load are low. Do not change the cpu desired format
1028 // and do not adapt.
1029 UpdateCpuLoad(&adapter, 1, 1, 0.2f, 0.3f);
1030 EXPECT_EQ(320, adapter.output_format().width);
1031 EXPECT_EQ(200, adapter.output_format().height);
1032
1033 // Server format request 640x400. Set to 640x400 immediately.
1034 format.width = 640;
1035 format.height = 400;
1036 adapter.OnOutputFormatRequest(format);
1037 EXPECT_EQ(640, adapter.output_format().width);
1038 EXPECT_EQ(400, adapter.output_format().height);
1039
1040 // Server format request 320x200.
1041 format.width = 320;
1042 format.height = 200;
1043 adapter.OnOutputFormatRequest(format);
1044 EXPECT_EQ(320, adapter.output_format().width);
1045 EXPECT_EQ(200, adapter.output_format().height);
1046
1047 // Process load is high, but system is not. Do not change the cpu desired
1048 // format and do not adapt.
1049 for (size_t i = 0; i < 10; ++i) {
1050 UpdateCpuLoad(&adapter, 1, 1, 0.75f, 0.8f);
1051 }
1052 EXPECT_EQ(320, adapter.output_format().width);
1053 EXPECT_EQ(200, adapter.output_format().height);
1054}
1055
1056// Test that for an upgrade encoder request, we actually upgrade the desired
1057// format; for a downgrade request, we downgrade from the output format.
1058TEST(CoordinatedVideoAdapterTest, TestRealEncoderUpgrade) {
1059 CoordinatedVideoAdapter adapter;
1060 adapter.set_cpu_adaptation(true);
1061 adapter.set_cpu_smoothing(true);
1062 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), FOURCC_I420);
1063 adapter.SetInputFormat(format);
1064
1065 // Server format request 640x400.
1066 format.width = 640;
1067 format.height = 400;
1068 adapter.OnOutputFormatRequest(format);
1069 EXPECT_EQ(640, adapter.output_format().width);
1070 EXPECT_EQ(400, adapter.output_format().height);
1071
1072 // Encoder resolution request. Do not change the encoder desired format and
1073 // do not adapt.
1074 adapter.OnEncoderResolutionRequest(640, 400,
1075 CoordinatedVideoAdapter::UPGRADE);
1076 EXPECT_EQ(640, adapter.output_format().width);
1077 EXPECT_EQ(400, adapter.output_format().height);
1078
1079 // Server format request 320x200.
1080 format.width = 320;
1081 format.height = 200;
1082 adapter.OnOutputFormatRequest(format);
1083 EXPECT_EQ(320, adapter.output_format().width);
1084 EXPECT_EQ(200, adapter.output_format().height);
1085
1086 // Encoder resolution request. Do not change the encoder desired format and
1087 // do not adapt.
1088 adapter.OnEncoderResolutionRequest(320, 200,
1089 CoordinatedVideoAdapter::UPGRADE);
1090 EXPECT_EQ(320, adapter.output_format().width);
1091 EXPECT_EQ(200, adapter.output_format().height);
1092
1093 // Server format request 640x400. Set to 640x400 immediately.
1094 format.width = 640;
1095 format.height = 400;
1096 adapter.OnOutputFormatRequest(format);
1097 EXPECT_EQ(480, adapter.output_format().width);
1098 EXPECT_EQ(300, adapter.output_format().height);
1099
1100 // Test reason for adapting is BANDWIDTH.
1101 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_BANDWIDTH,
1102 adapter.adapt_reason());
1103
1104 // Server format request 320x200.
1105 format.width = 320;
1106 format.height = 200;
1107 adapter.OnOutputFormatRequest(format);
1108 EXPECT_EQ(320, adapter.output_format().width);
1109 EXPECT_EQ(200, adapter.output_format().height);
1110
1111 // Encoder resolution request. Downgrade from 320x200.
1112 adapter.OnEncoderResolutionRequest(320, 200,
1113 CoordinatedVideoAdapter::DOWNGRADE);
1114 EXPECT_EQ(240, adapter.output_format().width);
1115 EXPECT_EQ(150, adapter.output_format().height);
1116}
1117
1118TEST(CoordinatedVideoAdapterTest, TestNormalizeOutputFormat) {
1119 CoordinatedVideoAdapter adapter;
1120 // The input format is 640x360 and the output is limited to 16:9.
1121 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420);
1122 adapter.SetInputFormat(format);
1123
1124 format.width = 320;
1125 format.height = 180;
1126 format.interval = VideoFormat::FpsToInterval(15);
1127 adapter.OnOutputFormatRequest(format);
1128 EXPECT_EQ(320, adapter.output_format().width);
1129 EXPECT_EQ(180, adapter.output_format().height);
1130 EXPECT_EQ(VideoFormat::FpsToInterval(15), adapter.output_format().interval);
1131
1132 format.width = 320;
1133 format.height = 200;
1134 format.interval = VideoFormat::FpsToInterval(40);
1135 adapter.OnOutputFormatRequest(format);
1136 EXPECT_EQ(320, adapter.output_format().width);
1137 EXPECT_EQ(180, adapter.output_format().height);
1138 EXPECT_EQ(VideoFormat::FpsToInterval(30), adapter.output_format().interval);
1139
1140 // Test reason for adapting is VIEW. Should work even with normalization.
1141 EXPECT_EQ(CoordinatedVideoAdapter::ADAPTREASON_VIEW,
1142 adapter.adapt_reason());
1143
1144 format.width = 320;
1145 format.height = 240;
1146 adapter.OnOutputFormatRequest(format);
1147 EXPECT_EQ(320, adapter.output_format().width);
1148 EXPECT_EQ(180, adapter.output_format().height);
1149
1150 // The input format is 640x480 and the output will be 4:3.
1151 format.width = 640;
1152 format.height = 480;
1153 adapter.SetInputFormat(format);
1154 EXPECT_EQ(320, adapter.output_format().width);
1155 EXPECT_EQ(240, adapter.output_format().height);
1156
1157 format.width = 320;
1158 format.height = 240;
1159 adapter.OnOutputFormatRequest(format);
1160 EXPECT_EQ(320, adapter.output_format().width);
1161 EXPECT_EQ(240, adapter.output_format().height);
1162
1163 // The input format is initialized after the output. At that time, the output
1164 // height is adjusted.
1165 format.width = 0;
1166 format.height = 0;
1167 adapter.SetInputFormat(format);
1168
1169 format.width = 320;
1170 format.height = 240;
1171 format.interval = VideoFormat::FpsToInterval(30);
1172 adapter.OnOutputFormatRequest(format);
1173 EXPECT_EQ(320, adapter.output_format().width);
1174 EXPECT_EQ(240, adapter.output_format().height);
1175 EXPECT_EQ(VideoFormat::FpsToInterval(30), adapter.output_format().interval);
1176
1177 format.width = 640;
1178 format.height = 480;
1179 format.interval = VideoFormat::FpsToInterval(15);
1180 adapter.SetInputFormat(format);
1181 EXPECT_EQ(320, adapter.output_format().width);
1182 EXPECT_EQ(240, adapter.output_format().height);
1183 EXPECT_EQ(VideoFormat::FpsToInterval(15), adapter.output_format().interval);
1184}
1185
1186// Test that we downgrade video for cpu up to two times.
1187TEST_F(VideoAdapterTest, CpuDowngradeAndSignal) {
1188 CoordinatedVideoAdapter adapter;
1189 CpuAdapterListener cpu_listener;
1190 adapter.SignalCpuAdaptationUnable.connect(
1191 &cpu_listener, &CpuAdapterListener::OnCpuAdaptationSignalled);
1192
1193 adapter.set_cpu_adaptation(true);
1194 EXPECT_FALSE(adapter.cpu_smoothing());
1195 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420);
1196 adapter.SetInputFormat(format);
1197 adapter.OnOutputFormatRequest(format);
1198
1199 // System load is high. Downgrade.
1200 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
1201
1202 // System load is high. Downgrade again.
1203 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
1204
1205 // System load is still high. Do not downgrade any more. Ensure we have not
1206 // signalled until after the cpu warning though.
1207 EXPECT_TRUE(!cpu_listener.received_cpu_signal());
1208 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
1209 EXPECT_TRUE_WAIT(cpu_listener.received_cpu_signal(), kWaitTimeout);
1210}
1211
1212// Test that we downgrade video for cpu up to two times.
1213TEST_F(VideoAdapterTest, CpuDowngradeAndDontSignal) {
1214 CoordinatedVideoAdapter adapter;
1215 CpuAdapterListener cpu_listener;
1216 adapter.SignalCpuAdaptationUnable.connect(
1217 &cpu_listener, &CpuAdapterListener::OnCpuAdaptationSignalled);
1218
1219 adapter.set_cpu_adaptation(true);
1220 adapter.set_cpu_smoothing(true);
1221 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420);
1222 adapter.SetInputFormat(format);
1223 adapter.OnOutputFormatRequest(format);
1224
1225 // System load is high. Downgrade.
1226 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
1227
1228 // System load is high, process is not, Do not downgrade again.
1229 UpdateCpuLoad(&adapter, 1, 1, 0.25f, 0.95f);
1230
1231 // System load is high, process is not, Do not downgrade again and do not
1232 // signal.
1233 adapter.set_cpu_adaptation(false);
1234 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
1235 rtc::Thread::Current()->ProcessMessages(kShortWaitTimeout);
1236 EXPECT_TRUE(!cpu_listener.received_cpu_signal());
1237 adapter.set_cpu_adaptation(true);
1238}
1239
1240// Test that we require enough time before we downgrade.
1241TEST_F(VideoAdapterTest, CpuMinTimeRequirement) {
1242 CoordinatedVideoAdapter adapter;
1243 CpuAdapterListener cpu_listener;
1244 adapter.SignalCpuAdaptationUnable.connect(
1245 &cpu_listener, &CpuAdapterListener::OnCpuAdaptationSignalled);
1246
1247 adapter.set_cpu_adaptation(true);
1248 adapter.set_cpu_smoothing(true);
1249 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420);
1250 adapter.SetInputFormat(format);
1251 adapter.OnOutputFormatRequest(format);
1252
1253 EXPECT_EQ(3, adapter.cpu_load_min_samples());
1254 adapter.set_cpu_load_min_samples(5);
1255
1256 for (size_t i = 0; i < 4; ++i) {
1257 adapter.OnCpuLoadUpdated(1, 1, 1.0f, 1.0f);
1258 EXPECT_EQ(640, adapter.output_format().width);
1259 EXPECT_EQ(360, adapter.output_format().height);
1260 }
1261 // The computed cpu load should now be around 93.5%, with the coefficient of
1262 // 0.4 and a seed value of 0.5. That should be high enough to adapt, but it
1263 // isn't enough samples, so we shouldn't have adapted on any of the previous
1264 // samples.
1265
1266 // One more sample is enough, though, once enough time has passed.
1267 adapter.OnCpuLoadUpdated(1, 1, 1.0f, 1.0f);
1268 EXPECT_EQ(480, adapter.output_format().width);
1269 EXPECT_EQ(270, adapter.output_format().height);
1270
1271 // Now the cpu is lower, but we still need enough samples to upgrade.
1272 for (size_t i = 0; i < 4; ++i) {
1273 adapter.OnCpuLoadUpdated(1, 1, 0.1f, 0.1f);
1274 EXPECT_EQ(480, adapter.output_format().width);
1275 EXPECT_EQ(270, adapter.output_format().height);
1276 }
1277
1278 // One more sample is enough, once time has elapsed.
1279 adapter.OnCpuLoadUpdated(1, 1, 1.0f, 1.0f);
1280 EXPECT_EQ(640, adapter.output_format().width);
1281 EXPECT_EQ(360, adapter.output_format().height);
1282}
1283
1284TEST_F(VideoAdapterTest, CpuIgnoresSpikes) {
1285 CoordinatedVideoAdapter adapter;
1286 CpuAdapterListener cpu_listener;
1287 adapter.SignalCpuAdaptationUnable.connect(
1288 &cpu_listener, &CpuAdapterListener::OnCpuAdaptationSignalled);
1289
1290 adapter.set_cpu_adaptation(true);
1291 adapter.set_cpu_smoothing(true);
1292 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420);
1293 adapter.SetInputFormat(format);
1294 adapter.OnOutputFormatRequest(format);
1295
1296 // System load is high. Downgrade.
1297 for (size_t i = 0; i < 5; ++i) {
1298 UpdateCpuLoad(&adapter, 1, 1, 0.95f, 0.95f);
1299 }
1300 EXPECT_EQ(480, adapter.output_format().width);
1301 EXPECT_EQ(270, adapter.output_format().height);
1302
1303 // Now we're in a state where we could upgrade or downgrade, so get to a
1304 // steady state of about 75% cpu usage.
1305 for (size_t i = 0; i < 5; ++i) {
1306 UpdateCpuLoad(&adapter, 1, 1, 0.75f, 0.75f);
1307 EXPECT_EQ(480, adapter.output_format().width);
1308 EXPECT_EQ(270, adapter.output_format().height);
1309 }
1310
1311 // Now, the cpu spikes for two samples, but then goes back to
1312 // normal. This shouldn't cause adaptation.
1313 UpdateCpuLoad(&adapter, 1, 1, 0.90f, 0.90f);
1314 UpdateCpuLoad(&adapter, 1, 1, 0.90f, 0.90f);
1315 EXPECT_EQ(480, adapter.output_format().width);
1316 EXPECT_EQ(270, adapter.output_format().height);
1317 // Back to the steady state for awhile.
1318 for (size_t i = 0; i < 5; ++i) {
1319 UpdateCpuLoad(&adapter, 1, 1, 0.75, 0.75);
1320 EXPECT_EQ(480, adapter.output_format().width);
1321 EXPECT_EQ(270, adapter.output_format().height);
1322 }
1323
1324 // Now, system cpu usage is starting to drop down. But it takes a bit before
1325 // it gets all the way there.
1326 for (size_t i = 0; i < 10; ++i) {
1327 UpdateCpuLoad(&adapter, 1, 1, 0.5f, 0.5f);
1328 }
1329 EXPECT_EQ(640, adapter.output_format().width);
1330 EXPECT_EQ(360, adapter.output_format().height);
1331}
1332
1333} // namespace cricket
1334#endif // HAVE_WEBRTC_VIDEO