Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: webrtc/video/vie_encoder_unittest.cc

Issue 2716643002: Add framerate to VideoSinkWants and ability to signal on overuse (Closed)
Patch Set: windows warning Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <limits> 11 #include <limits>
12 #include <utility> 12 #include <utility>
13 13
14 #include "webrtc/api/video/i420_buffer.h" 14 #include "webrtc/api/video/i420_buffer.h"
15 #include "webrtc/base/fakeclock.h"
15 #include "webrtc/base/logging.h" 16 #include "webrtc/base/logging.h"
16 #include "webrtc/media/base/videoadapter.h" 17 #include "webrtc/media/base/videoadapter.h"
17 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h" 18 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h"
18 #include "webrtc/system_wrappers/include/metrics_default.h" 19 #include "webrtc/system_wrappers/include/metrics_default.h"
19 #include "webrtc/system_wrappers/include/sleep.h" 20 #include "webrtc/system_wrappers/include/sleep.h"
20 #include "webrtc/test/encoder_settings.h" 21 #include "webrtc/test/encoder_settings.h"
21 #include "webrtc/test/fake_encoder.h" 22 #include "webrtc/test/fake_encoder.h"
22 #include "webrtc/test/frame_generator.h" 23 #include "webrtc/test/frame_generator.h"
23 #include "webrtc/test/gmock.h" 24 #include "webrtc/test/gmock.h"
24 #include "webrtc/test/gtest.h" 25 #include "webrtc/test/gtest.h"
25 #include "webrtc/video/send_statistics_proxy.h" 26 #include "webrtc/video/send_statistics_proxy.h"
26 #include "webrtc/video/vie_encoder.h" 27 #include "webrtc/video/vie_encoder.h"
27 28
28 namespace { 29 namespace {
29 #if defined(WEBRTC_ANDROID) 30 #if defined(WEBRTC_ANDROID)
30 // TODO(kthelgason): Lower this limit when better testing 31 // TODO(kthelgason): Lower this limit when better testing
31 // on MediaCodec and fallback implementations are in place. 32 // on MediaCodec and fallback implementations are in place.
32 const int kMinPixelsPerFrame = 320 * 180; 33 const int kMinPixelsPerFrame = 320 * 180;
33 #else 34 #else
34 const int kMinPixelsPerFrame = 120 * 90; 35 const int kMinPixelsPerFrame = 120 * 90;
35 #endif 36 #endif
36 } 37 const int kMinFramerateFps = 2;
38 const int64_t kFrameTimeoutMs = 100;
39 } // namespace
37 40
38 namespace webrtc { 41 namespace webrtc {
39 42
40 using DegredationPreference = VideoSendStream::DegradationPreference; 43 using DegredationPreference = VideoSendStream::DegradationPreference;
41 using ScaleReason = AdaptationObserverInterface::AdaptReason; 44 using ScaleReason = AdaptationObserverInterface::AdaptReason;
42 using ::testing::_; 45 using ::testing::_;
43 using ::testing::Return; 46 using ::testing::Return;
44 47
45 namespace { 48 namespace {
46 const size_t kMaxPayloadLength = 1440; 49 const size_t kMaxPayloadLength = 1440;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 bool adaption_enabled() { 129 bool adaption_enabled() {
127 rtc::CritScope cs(&crit_); 130 rtc::CritScope cs(&crit_);
128 return adaptation_enabled_; 131 return adaptation_enabled_;
129 } 132 }
130 133
131 void IncomingCapturedFrame(const VideoFrame& video_frame) override { 134 void IncomingCapturedFrame(const VideoFrame& video_frame) override {
132 int cropped_width = 0; 135 int cropped_width = 0;
133 int cropped_height = 0; 136 int cropped_height = 0;
134 int out_width = 0; 137 int out_width = 0;
135 int out_height = 0; 138 int out_height = 0;
136 if (adaption_enabled() && 139 if (adaption_enabled()) {
137 adapter_.AdaptFrameResolution(video_frame.width(), video_frame.height(), 140 if (adapter_.AdaptFrameResolution(
138 video_frame.timestamp_us() * 1000, 141 video_frame.width(), video_frame.height(),
139 &cropped_width, &cropped_height, 142 video_frame.timestamp_us() * 1000, &cropped_width,
140 &out_width, &out_height)) { 143 &cropped_height, &out_width, &out_height)) {
141 VideoFrame adapted_frame( 144 VideoFrame adapted_frame(new rtc::RefCountedObject<TestBuffer>(
142 new rtc::RefCountedObject<TestBuffer>(nullptr, out_width, out_height), 145 nullptr, out_width, out_height),
143 99, 99, kVideoRotation_0); 146 99, 99, kVideoRotation_0);
144 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms()); 147 adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms());
145 test::FrameForwarder::IncomingCapturedFrame(adapted_frame); 148 test::FrameForwarder::IncomingCapturedFrame(adapted_frame);
149 }
146 } else { 150 } else {
147 test::FrameForwarder::IncomingCapturedFrame(video_frame); 151 test::FrameForwarder::IncomingCapturedFrame(video_frame);
148 } 152 }
149 } 153 }
150 154
151 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, 155 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
152 const rtc::VideoSinkWants& wants) override { 156 const rtc::VideoSinkWants& wants) override {
153 rtc::CritScope cs(&crit_); 157 rtc::CritScope cs(&crit_);
154 adapter_.OnResolutionRequest(wants.target_pixel_count, 158 adapter_.OnResolutionFramerateRequest(wants.target_pixel_count,
155 wants.max_pixel_count); 159 wants.max_pixel_count,
160 wants.max_framerate_fps);
156 test::FrameForwarder::AddOrUpdateSink(sink, wants); 161 test::FrameForwarder::AddOrUpdateSink(sink, wants);
157 } 162 }
158 163
159 cricket::VideoAdapter adapter_; 164 cricket::VideoAdapter adapter_;
160 bool adaptation_enabled_ GUARDED_BY(crit_); 165 bool adaptation_enabled_ GUARDED_BY(crit_);
161 }; 166 };
167
168 class MockableSendStatisticsProxy : public SendStatisticsProxy {
169 public:
170 MockableSendStatisticsProxy(Clock* clock,
171 const VideoSendStream::Config& config,
172 VideoEncoderConfig::ContentType content_type)
173 : SendStatisticsProxy(clock, config, content_type) {}
174
175 VideoSendStream::Stats GetStats() override {
176 rtc::CritScope cs(&lock_);
177 if (mock_stats_)
178 return *mock_stats_;
179 return SendStatisticsProxy::GetStats();
180 }
181
182 void SetMockStats(const VideoSendStream::Stats& stats) {
183 rtc::CritScope cs(&lock_);
184 mock_stats_.emplace(stats);
185 }
186
187 void ResetMockStats() {
188 rtc::CritScope cs(&lock_);
189 mock_stats_.reset();
190 }
191
192 private:
193 rtc::CriticalSection lock_;
194 rtc::Optional<VideoSendStream::Stats> mock_stats_ GUARDED_BY(lock_);
195 };
196
162 } // namespace 197 } // namespace
163 198
164 class ViEEncoderTest : public ::testing::Test { 199 class ViEEncoderTest : public ::testing::Test {
165 public: 200 public:
166 static const int kDefaultTimeoutMs = 30 * 1000; 201 static const int kDefaultTimeoutMs = 30 * 1000;
167 202
168 ViEEncoderTest() 203 ViEEncoderTest()
169 : video_send_config_(VideoSendStream::Config(nullptr)), 204 : video_send_config_(VideoSendStream::Config(nullptr)),
170 codec_width_(320), 205 codec_width_(320),
171 codec_height_(240), 206 codec_height_(240),
172 fake_encoder_(), 207 fake_encoder_(),
173 stats_proxy_(new SendStatisticsProxy( 208 stats_proxy_(new MockableSendStatisticsProxy(
174 Clock::GetRealTimeClock(), 209 Clock::GetRealTimeClock(),
175 video_send_config_, 210 video_send_config_,
176 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)), 211 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)),
177 sink_(&fake_encoder_) {} 212 sink_(&fake_encoder_) {}
178 213
179 void SetUp() override { 214 void SetUp() override {
180 metrics::Reset(); 215 metrics::Reset();
181 video_send_config_ = VideoSendStream::Config(nullptr); 216 video_send_config_ = VideoSendStream::Config(nullptr);
182 video_send_config_.encoder_settings.encoder = &fake_encoder_; 217 video_send_config_.encoder_settings.encoder = &fake_encoder_;
183 video_send_config_.encoder_settings.payload_name = "FAKE"; 218 video_send_config_.encoder_settings.payload_name = "FAKE";
184 video_send_config_.encoder_settings.payload_type = 125; 219 video_send_config_.encoder_settings.payload_type = 125;
185 220
186 VideoEncoderConfig video_encoder_config; 221 VideoEncoderConfig video_encoder_config;
187 test::FillEncoderConfiguration(1, &video_encoder_config); 222 test::FillEncoderConfiguration(1, &video_encoder_config);
188 video_encoder_config_ = video_encoder_config.Copy(); 223 video_encoder_config_ = video_encoder_config.Copy();
189 ConfigureEncoder(std::move(video_encoder_config), true /* nack_enabled */); 224 ConfigureEncoder(std::move(video_encoder_config), true /* nack_enabled */);
190 } 225 }
191 226
192 void ConfigureEncoder(VideoEncoderConfig video_encoder_config, 227 void ConfigureEncoder(VideoEncoderConfig video_encoder_config,
193 bool nack_enabled) { 228 bool nack_enabled) {
194 if (vie_encoder_) 229 if (vie_encoder_)
195 vie_encoder_->Stop(); 230 vie_encoder_->Stop();
196 vie_encoder_.reset(new ViEEncoderUnderTest( 231 vie_encoder_.reset(new ViEEncoderUnderTest(
197 stats_proxy_.get(), video_send_config_.encoder_settings)); 232 stats_proxy_.get(), video_send_config_.encoder_settings));
198 vie_encoder_->SetSink(&sink_, false /* rotation_applied */); 233 vie_encoder_->SetSink(&sink_, false /* rotation_applied */);
199 vie_encoder_->SetSource(&video_source_, 234 vie_encoder_->SetSource(
200 VideoSendStream::DegradationPreference::kBalanced); 235 &video_source_,
236 VideoSendStream::DegradationPreference::kMaintainFramerate);
201 vie_encoder_->SetStartBitrate(kTargetBitrateBps); 237 vie_encoder_->SetStartBitrate(kTargetBitrateBps);
202 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 238 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config),
203 kMaxPayloadLength, nack_enabled); 239 kMaxPayloadLength, nack_enabled);
204 } 240 }
205 241
206 void ResetEncoder(const std::string& payload_name, 242 void ResetEncoder(const std::string& payload_name,
207 size_t num_streams, 243 size_t num_streams,
208 size_t num_temporal_layers, 244 size_t num_temporal_layers,
209 bool nack_enabled) { 245 bool nack_enabled) {
210 video_send_config_.encoder_settings.payload_name = payload_name; 246 video_send_config_.encoder_settings.payload_name = payload_name;
(...skipping 13 matching lines...) Expand all
224 99, 99, kVideoRotation_0); 260 99, 99, kVideoRotation_0);
225 frame.set_ntp_time_ms(ntp_time_ms); 261 frame.set_ntp_time_ms(ntp_time_ms);
226 return frame; 262 return frame;
227 } 263 }
228 264
229 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const { 265 VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const {
230 VideoFrame frame( 266 VideoFrame frame(
231 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height), 99, 99, 267 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height), 99, 99,
232 kVideoRotation_0); 268 kVideoRotation_0);
233 frame.set_ntp_time_ms(ntp_time_ms); 269 frame.set_ntp_time_ms(ntp_time_ms);
270 frame.set_timestamp_us(ntp_time_ms * 1000);
234 return frame; 271 return frame;
235 } 272 }
236 273
237 class TestEncoder : public test::FakeEncoder { 274 class TestEncoder : public test::FakeEncoder {
238 public: 275 public:
239 TestEncoder() 276 TestEncoder()
240 : FakeEncoder(Clock::GetRealTimeClock()), 277 : FakeEncoder(Clock::GetRealTimeClock()),
241 continue_encode_event_(false, false) {} 278 continue_encode_event_(false, false) {}
242 279
243 VideoCodec codec_config() { 280 VideoCodec codec_config() {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); 349 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
313 { 350 {
314 rtc::CritScope lock(&crit_); 351 rtc::CritScope lock(&crit_);
315 timestamp = last_timestamp_; 352 timestamp = last_timestamp_;
316 } 353 }
317 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); 354 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
318 } 355 }
319 356
320 void WaitForEncodedFrame(uint32_t expected_width, 357 void WaitForEncodedFrame(uint32_t expected_width,
321 uint32_t expected_height) { 358 uint32_t expected_height) {
359 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
360 CheckLastFrameSizeMathces(expected_width, expected_height);
361 }
362
363 void CheckLastFrameSizeMathces(uint32_t expected_width,
364 uint32_t expected_height) {
322 uint32_t width = 0; 365 uint32_t width = 0;
323 uint32_t height = 0; 366 uint32_t height = 0;
324 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
325 { 367 {
326 rtc::CritScope lock(&crit_); 368 rtc::CritScope lock(&crit_);
327 width = last_width_; 369 width = last_width_;
328 height = last_height_; 370 height = last_height_;
329 } 371 }
330 EXPECT_EQ(expected_height, height); 372 EXPECT_EQ(expected_height, height);
331 EXPECT_EQ(expected_width, width); 373 EXPECT_EQ(expected_width, width);
332 } 374 }
333 375
334 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(20)); } 376 void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(20)); }
335 377
378 bool WaitForFrame(int64_t timeout_ms) {
379 return encoded_frame_event_.Wait(timeout_ms);
380 }
381
336 void SetExpectNoFrames() { 382 void SetExpectNoFrames() {
337 rtc::CritScope lock(&crit_); 383 rtc::CritScope lock(&crit_);
338 expect_frames_ = false; 384 expect_frames_ = false;
339 } 385 }
340 386
341 int number_of_reconfigurations() { 387 int number_of_reconfigurations() {
342 rtc::CritScope lock(&crit_); 388 rtc::CritScope lock(&crit_);
343 return number_of_reconfigurations_; 389 return number_of_reconfigurations_;
344 } 390 }
345 391
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 bool expect_frames_ = true; 424 bool expect_frames_ = true;
379 int number_of_reconfigurations_ = 0; 425 int number_of_reconfigurations_ = 0;
380 int min_transmit_bitrate_bps_ = 0; 426 int min_transmit_bitrate_bps_ = 0;
381 }; 427 };
382 428
383 VideoSendStream::Config video_send_config_; 429 VideoSendStream::Config video_send_config_;
384 VideoEncoderConfig video_encoder_config_; 430 VideoEncoderConfig video_encoder_config_;
385 int codec_width_; 431 int codec_width_;
386 int codec_height_; 432 int codec_height_;
387 TestEncoder fake_encoder_; 433 TestEncoder fake_encoder_;
388 std::unique_ptr<SendStatisticsProxy> stats_proxy_; 434 std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_;
389 TestSink sink_; 435 TestSink sink_;
390 AdaptingFrameForwarder video_source_; 436 AdaptingFrameForwarder video_source_;
391 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_; 437 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_;
392 }; 438 };
393 439
394 TEST_F(ViEEncoderTest, EncodeOneFrame) { 440 TEST_F(ViEEncoderTest, EncodeOneFrame) {
395 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 441 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
396 rtc::Event frame_destroyed_event(false, false); 442 rtc::Event frame_destroyed_event(false, false);
397 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); 443 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event));
398 sink_.WaitForEncodedFrame(1); 444 sink_.WaitForEncodedFrame(1);
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams); 642 EXPECT_EQ(kNumStreams, fake_encoder_.codec_config().numberOfSimulcastStreams);
597 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers); 643 EXPECT_EQ(kNumTl, fake_encoder_.codec_config().VP8()->numberOfTemporalLayers);
598 // Resilience is on for temporal layers. 644 // Resilience is on for temporal layers.
599 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience); 645 EXPECT_EQ(kResilientStream, fake_encoder_.codec_config().VP8()->resilience);
600 vie_encoder_->Stop(); 646 vie_encoder_->Stop();
601 } 647 }
602 648
603 TEST_F(ViEEncoderTest, SwitchSourceDeregisterEncoderAsSink) { 649 TEST_F(ViEEncoderTest, SwitchSourceDeregisterEncoderAsSink) {
604 EXPECT_TRUE(video_source_.has_sinks()); 650 EXPECT_TRUE(video_source_.has_sinks());
605 test::FrameForwarder new_video_source; 651 test::FrameForwarder new_video_source;
606 vie_encoder_->SetSource(&new_video_source, 652 vie_encoder_->SetSource(
607 VideoSendStream::DegradationPreference::kBalanced); 653 &new_video_source,
654 VideoSendStream::DegradationPreference::kMaintainFramerate);
608 EXPECT_FALSE(video_source_.has_sinks()); 655 EXPECT_FALSE(video_source_.has_sinks());
609 EXPECT_TRUE(new_video_source.has_sinks()); 656 EXPECT_TRUE(new_video_source.has_sinks());
610 657
611 vie_encoder_->Stop(); 658 vie_encoder_->Stop();
612 } 659 }
613 660
614 TEST_F(ViEEncoderTest, SinkWantsRotationApplied) { 661 TEST_F(ViEEncoderTest, SinkWantsRotationApplied) {
615 EXPECT_FALSE(video_source_.sink_wants().rotation_applied); 662 EXPECT_FALSE(video_source_.sink_wants().rotation_applied);
616 vie_encoder_->SetSink(&sink_, true /*rotation_applied*/); 663 vie_encoder_->SetSink(&sink_, true /*rotation_applied*/);
617 EXPECT_TRUE(video_source_.sink_wants().rotation_applied); 664 EXPECT_TRUE(video_source_.sink_wants().rotation_applied);
618 vie_encoder_->Stop(); 665 vie_encoder_->Stop();
619 } 666 }
620 667
621 TEST_F(ViEEncoderTest, SinkWantsFromOveruseDetector) { 668 TEST_F(ViEEncoderTest, SinkWantsFromOveruseDetector) {
622 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 669 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
623 670
624 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 671 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
625 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); 672 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count);
626 673
627 int frame_width = 1280; 674 int frame_width = 1280;
628 int frame_height = 720; 675 int frame_height = 720;
629 676
630 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should 677 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should
631 // request lower resolution. 678 // request lower resolution.
632 for (int i = 1; i <= ViEEncoder::kMaxCpuDowngrades; ++i) { 679 for (int i = 1; i <= ViEEncoder::kMaxCpuResolutionDowngrades; ++i) {
633 video_source_.IncomingCapturedFrame( 680 video_source_.IncomingCapturedFrame(
634 CreateFrame(i, frame_width, frame_height)); 681 CreateFrame(i, frame_width, frame_height));
635 sink_.WaitForEncodedFrame(i); 682 sink_.WaitForEncodedFrame(i);
636 683
637 vie_encoder_->TriggerCpuOveruse(); 684 vie_encoder_->TriggerCpuOveruse();
638 685
639 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 686 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
640 EXPECT_LT(video_source_.sink_wants().max_pixel_count.value_or( 687 EXPECT_LT(video_source_.sink_wants().max_pixel_count.value_or(
641 std::numeric_limits<int>::max()), 688 std::numeric_limits<int>::max()),
642 frame_width * frame_height); 689 frame_width * frame_height);
643 690
644 frame_width /= 2; 691 frame_width /= 2;
645 frame_height /= 2; 692 frame_height /= 2;
646 } 693 }
647 694
648 // Trigger CPU overuse one more time. This should not trigger a request for 695 // Trigger CPU overuse one more time. This should not trigger a request for
649 // lower resolution. 696 // lower resolution.
650 rtc::VideoSinkWants current_wants = video_source_.sink_wants(); 697 rtc::VideoSinkWants current_wants = video_source_.sink_wants();
651 video_source_.IncomingCapturedFrame(CreateFrame( 698 video_source_.IncomingCapturedFrame(CreateFrame(
652 ViEEncoder::kMaxCpuDowngrades + 1, frame_width, frame_height)); 699 ViEEncoder::kMaxCpuResolutionDowngrades + 1, frame_width, frame_height));
653 sink_.WaitForEncodedFrame(ViEEncoder::kMaxCpuDowngrades + 1); 700 sink_.WaitForEncodedFrame(ViEEncoder::kMaxCpuResolutionDowngrades + 1);
654 vie_encoder_->TriggerCpuOveruse(); 701 vie_encoder_->TriggerCpuOveruse();
655 EXPECT_EQ(video_source_.sink_wants().target_pixel_count, 702 EXPECT_EQ(video_source_.sink_wants().target_pixel_count,
656 current_wants.target_pixel_count); 703 current_wants.target_pixel_count);
657 EXPECT_EQ(video_source_.sink_wants().max_pixel_count, 704 EXPECT_EQ(video_source_.sink_wants().max_pixel_count,
658 current_wants.max_pixel_count); 705 current_wants.max_pixel_count);
659 706
660 // Trigger CPU normal use. 707 // Trigger CPU normal use.
661 vie_encoder_->TriggerCpuNormalUsage(); 708 vie_encoder_->TriggerCpuNormalUsage();
662 EXPECT_EQ(frame_width * frame_height * 5 / 3, 709 EXPECT_EQ(frame_width * frame_height * 5 / 3,
663 video_source_.sink_wants().target_pixel_count.value_or(0)); 710 video_source_.sink_wants().target_pixel_count.value_or(0));
664 EXPECT_EQ(frame_width * frame_height * 4, 711 EXPECT_EQ(frame_width * frame_height * 4,
665 video_source_.sink_wants().max_pixel_count.value_or(0)); 712 video_source_.sink_wants().max_pixel_count.value_or(0));
666 713
667 vie_encoder_->Stop(); 714 vie_encoder_->Stop();
668 } 715 }
669 716
670 TEST_F(ViEEncoderTest, 717 TEST_F(ViEEncoderTest, SinkWantsStoredByDegradationPreference) {
671 ResolutionSinkWantsResetOnSetSourceWithDisabledResolutionScaling) {
672 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 718 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
673 719
674 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 720 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
675 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count); 721 EXPECT_FALSE(video_source_.sink_wants().max_pixel_count);
722 EXPECT_FALSE(video_source_.sink_wants().max_framerate_fps);
676 723
677 int frame_width = 1280; 724 const int kFrameWidth = 1280;
678 int frame_height = 720; 725 const int kFrameHeight = 720;
726 const int kFrameIntervalMs = 1000 / 30;
727
728 int frame_timestamp = 1;
679 729
680 video_source_.IncomingCapturedFrame( 730 video_source_.IncomingCapturedFrame(
681 CreateFrame(1, frame_width, frame_height)); 731 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
682 sink_.WaitForEncodedFrame(1); 732 sink_.WaitForEncodedFrame(frame_timestamp);
733 frame_timestamp += kFrameIntervalMs;
734
683 // Trigger CPU overuse. 735 // Trigger CPU overuse.
684 vie_encoder_->TriggerCpuOveruse(); 736 vie_encoder_->TriggerCpuOveruse();
737 video_source_.IncomingCapturedFrame(
738 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
739 sink_.WaitForEncodedFrame(frame_timestamp);
740 frame_timestamp += kFrameIntervalMs;
685 741
686 video_source_.IncomingCapturedFrame( 742 // Default degradation preference in maintain-framerate, so will lower max
687 CreateFrame(2, frame_width, frame_height)); 743 // wanted resolution.
688 sink_.WaitForEncodedFrame(2);
689 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); 744 EXPECT_FALSE(video_source_.sink_wants().target_pixel_count);
690 EXPECT_LT(video_source_.sink_wants().max_pixel_count.value_or( 745 EXPECT_LT(video_source_.sink_wants().max_pixel_count.value_or(
691 std::numeric_limits<int>::max()), 746 std::numeric_limits<int>::max()),
692 frame_width * frame_height); 747 kFrameWidth * kFrameHeight);
748 EXPECT_FALSE(video_source_.sink_wants().max_framerate_fps);
693 749
694 // Set new source. 750 // Set new source, switch to maintain-resolution.
695 test::FrameForwarder new_video_source; 751 test::FrameForwarder new_video_source;
696 vie_encoder_->SetSource( 752 vie_encoder_->SetSource(
697 &new_video_source, 753 &new_video_source,
698 VideoSendStream::DegradationPreference::kMaintainResolution); 754 VideoSendStream::DegradationPreference::kMaintainResolution);
699 755
756 // Initially no degradation registered.
700 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 757 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
701 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 758 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count);
759 EXPECT_FALSE(new_video_source.sink_wants().max_framerate_fps);
702 760
761 // Force an input frame rate to be available, or the adaptation call won't
762 // know what framerate to adapt form.
763 VideoSendStream::Stats stats = stats_proxy_->GetStats();
764 stats.input_frame_rate = 30;
765 stats_proxy_->SetMockStats(stats);
766
767 vie_encoder_->TriggerCpuOveruse();
703 new_video_source.IncomingCapturedFrame( 768 new_video_source.IncomingCapturedFrame(
704 CreateFrame(3, frame_width, frame_height)); 769 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
705 sink_.WaitForEncodedFrame(3); 770 sink_.WaitForEncodedFrame(frame_timestamp);
771 frame_timestamp += kFrameIntervalMs;
772
773 // Some framerate constraint should be set.
706 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 774 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
707 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count); 775 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count);
776 EXPECT_TRUE(new_video_source.sink_wants().max_framerate_fps);
777
778 // Turn of degradation completely.
779 vie_encoder_->SetSource(
780 &new_video_source,
781 VideoSendStream::DegradationPreference::kDegradationDisabled);
782
783 // Initially no degradation registered.
784 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
785 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count);
786 EXPECT_FALSE(new_video_source.sink_wants().max_framerate_fps);
787
788 vie_encoder_->TriggerCpuOveruse();
789 new_video_source.IncomingCapturedFrame(
790 CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight));
791 sink_.WaitForEncodedFrame(frame_timestamp);
792 frame_timestamp += kFrameIntervalMs;
793
794 // Still no degradation.
795 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
796 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count);
797 EXPECT_FALSE(new_video_source.sink_wants().max_framerate_fps);
708 798
709 // Calling SetSource with resolution scaling enabled apply the old SinkWants. 799 // Calling SetSource with resolution scaling enabled apply the old SinkWants.
710 vie_encoder_->SetSource(&new_video_source, 800 vie_encoder_->SetSource(
711 VideoSendStream::DegradationPreference::kBalanced); 801 &new_video_source,
802 VideoSendStream::DegradationPreference::kMaintainFramerate);
712 EXPECT_LT(new_video_source.sink_wants().max_pixel_count.value_or( 803 EXPECT_LT(new_video_source.sink_wants().max_pixel_count.value_or(
713 std::numeric_limits<int>::max()), 804 std::numeric_limits<int>::max()),
714 frame_width * frame_height); 805 kFrameWidth * kFrameHeight);
715 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); 806 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
807 EXPECT_FALSE(new_video_source.sink_wants().max_framerate_fps);
808
809 // Calling SetSource with framerate scaling enabled apply the old SinkWants.
810 vie_encoder_->SetSource(
811 &new_video_source,
812 VideoSendStream::DegradationPreference::kMaintainResolution);
813 EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count);
814 EXPECT_FALSE(new_video_source.sink_wants().max_pixel_count);
815 EXPECT_TRUE(new_video_source.sink_wants().max_framerate_fps);
716 816
717 vie_encoder_->Stop(); 817 vie_encoder_->Stop();
718 } 818 }
719 819
720 TEST_F(ViEEncoderTest, StatsTracksAdaptationStats) { 820 TEST_F(ViEEncoderTest, StatsTracksAdaptationStats) {
721 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 821 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
722 822
723 int frame_width = 1280; 823 int frame_width = 1280;
724 int frame_height = 720; 824 int frame_height = 720;
725 825
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 870
771 video_source_.IncomingCapturedFrame( 871 video_source_.IncomingCapturedFrame(
772 CreateFrame(2, frame_width, frame_height)); 872 CreateFrame(2, frame_width, frame_height));
773 sink_.WaitForEncodedFrame(2); 873 sink_.WaitForEncodedFrame(2);
774 stats = stats_proxy_->GetStats(); 874 stats = stats_proxy_->GetStats();
775 EXPECT_TRUE(stats.cpu_limited_resolution); 875 EXPECT_TRUE(stats.cpu_limited_resolution);
776 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 876 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
777 877
778 // Set new source with adaptation still enabled. 878 // Set new source with adaptation still enabled.
779 test::FrameForwarder new_video_source; 879 test::FrameForwarder new_video_source;
780 vie_encoder_->SetSource(&new_video_source, 880 vie_encoder_->SetSource(
781 VideoSendStream::DegradationPreference::kBalanced); 881 &new_video_source,
882 VideoSendStream::DegradationPreference::kMaintainFramerate);
782 883
783 new_video_source.IncomingCapturedFrame( 884 new_video_source.IncomingCapturedFrame(
784 CreateFrame(3, frame_width, frame_height)); 885 CreateFrame(3, frame_width, frame_height));
785 sink_.WaitForEncodedFrame(3); 886 sink_.WaitForEncodedFrame(3);
786 stats = stats_proxy_->GetStats(); 887 stats = stats_proxy_->GetStats();
787 EXPECT_TRUE(stats.cpu_limited_resolution); 888 EXPECT_TRUE(stats.cpu_limited_resolution);
788 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 889 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
789 890
790 // Set adaptation disabled. 891 // Set adaptation disabled.
791 vie_encoder_->SetSource( 892 vie_encoder_->SetSource(
792 &new_video_source, 893 &new_video_source,
793 VideoSendStream::DegradationPreference::kMaintainResolution); 894 VideoSendStream::DegradationPreference::kDegradationDisabled);
794 895
795 new_video_source.IncomingCapturedFrame( 896 new_video_source.IncomingCapturedFrame(
796 CreateFrame(4, frame_width, frame_height)); 897 CreateFrame(4, frame_width, frame_height));
797 sink_.WaitForEncodedFrame(4); 898 sink_.WaitForEncodedFrame(4);
798 stats = stats_proxy_->GetStats(); 899 stats = stats_proxy_->GetStats();
799 EXPECT_FALSE(stats.cpu_limited_resolution); 900 EXPECT_FALSE(stats.cpu_limited_resolution);
800 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 901 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
801 902
802 // Set adaptation back to enabled. 903 // Set adaptation back to enabled.
803 vie_encoder_->SetSource(&new_video_source, 904 vie_encoder_->SetSource(
804 VideoSendStream::DegradationPreference::kBalanced); 905 &new_video_source,
906 VideoSendStream::DegradationPreference::kMaintainFramerate);
805 907
806 new_video_source.IncomingCapturedFrame( 908 new_video_source.IncomingCapturedFrame(
807 CreateFrame(5, frame_width, frame_height)); 909 CreateFrame(5, frame_width, frame_height));
808 sink_.WaitForEncodedFrame(5); 910 sink_.WaitForEncodedFrame(5);
809 stats = stats_proxy_->GetStats(); 911 stats = stats_proxy_->GetStats();
810 EXPECT_TRUE(stats.cpu_limited_resolution); 912 EXPECT_TRUE(stats.cpu_limited_resolution);
811 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 913 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
812 914
813 vie_encoder_->TriggerCpuNormalUsage(); 915 vie_encoder_->TriggerCpuNormalUsage();
814 916
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
903 video_source_.IncomingCapturedFrame( 1005 video_source_.IncomingCapturedFrame(
904 CreateFrame(sequence, frame_width, frame_height)); 1006 CreateFrame(sequence, frame_width, frame_height));
905 sink_.WaitForEncodedFrame(sequence++); 1007 sink_.WaitForEncodedFrame(sequence++);
906 1008
907 stats = stats_proxy_->GetStats(); 1009 stats = stats_proxy_->GetStats();
908 EXPECT_TRUE(stats.cpu_limited_resolution); 1010 EXPECT_TRUE(stats.cpu_limited_resolution);
909 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1011 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
910 1012
911 // Set new source with adaptation still enabled. 1013 // Set new source with adaptation still enabled.
912 test::FrameForwarder new_video_source; 1014 test::FrameForwarder new_video_source;
913 vie_encoder_->SetSource(&new_video_source, 1015 vie_encoder_->SetSource(
914 VideoSendStream::DegradationPreference::kBalanced); 1016 &new_video_source,
1017 VideoSendStream::DegradationPreference::kMaintainFramerate);
915 1018
916 new_video_source.IncomingCapturedFrame( 1019 new_video_source.IncomingCapturedFrame(
917 CreateFrame(sequence, frame_width, frame_height)); 1020 CreateFrame(sequence, frame_width, frame_height));
918 sink_.WaitForEncodedFrame(sequence++); 1021 sink_.WaitForEncodedFrame(sequence++);
919 stats = stats_proxy_->GetStats(); 1022 stats = stats_proxy_->GetStats();
920 EXPECT_TRUE(stats.cpu_limited_resolution); 1023 EXPECT_TRUE(stats.cpu_limited_resolution);
921 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1024 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
922 1025
923 // Set adaptation disabled. 1026 // Set cpu adaptation by frame dropping.
924 vie_encoder_->SetSource( 1027 vie_encoder_->SetSource(
925 &new_video_source, 1028 &new_video_source,
926 VideoSendStream::DegradationPreference::kMaintainResolution); 1029 VideoSendStream::DegradationPreference::kMaintainResolution);
927 new_video_source.IncomingCapturedFrame( 1030 new_video_source.IncomingCapturedFrame(
928 CreateFrame(sequence, frame_width, frame_height)); 1031 CreateFrame(sequence, frame_width, frame_height));
929 sink_.WaitForEncodedFrame(sequence++); 1032 sink_.WaitForEncodedFrame(sequence++);
930 stats = stats_proxy_->GetStats(); 1033 stats = stats_proxy_->GetStats();
1034 // Not adapted at first.
931 EXPECT_FALSE(stats.cpu_limited_resolution); 1035 EXPECT_FALSE(stats.cpu_limited_resolution);
932 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1036 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes);
933 1037
934 // Switch back the source with adaptation enabled. 1038 // Force an input frame rate to be available, or the adaptation call won't
935 vie_encoder_->SetSource(&video_source_, 1039 // know what framerate to adapt form.
936 VideoSendStream::DegradationPreference::kBalanced); 1040 VideoSendStream::Stats mock_stats = stats_proxy_->GetStats();
1041 mock_stats.input_frame_rate = 30;
1042 stats_proxy_->SetMockStats(mock_stats);
1043 vie_encoder_->TriggerCpuOveruse();
1044 stats_proxy_->ResetMockStats();
1045
1046 new_video_source.IncomingCapturedFrame(
1047 CreateFrame(sequence, frame_width, frame_height));
1048 sink_.WaitForEncodedFrame(sequence++);
1049
1050 // Framerate now adapted.
1051 stats = stats_proxy_->GetStats();
1052 EXPECT_TRUE(stats.cpu_limited_resolution);
1053 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1054
1055 // Disable CPU adaptation.
1056 vie_encoder_->SetSource(
1057 &new_video_source,
1058 VideoSendStream::DegradationPreference::kDegradationDisabled);
1059 new_video_source.IncomingCapturedFrame(
1060 CreateFrame(sequence, frame_width, frame_height));
1061 sink_.WaitForEncodedFrame(sequence++);
1062
1063 stats = stats_proxy_->GetStats();
1064 EXPECT_FALSE(stats.cpu_limited_resolution);
1065 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1066
1067 // Try to trigger overuse. Should not succeed.
1068 stats_proxy_->SetMockStats(mock_stats);
1069 vie_encoder_->TriggerCpuOveruse();
1070 stats_proxy_->ResetMockStats();
1071
1072 stats = stats_proxy_->GetStats();
1073 EXPECT_FALSE(stats.cpu_limited_resolution);
1074 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
1075
1076 // Switch back the source with resolution adaptation enabled.
1077 vie_encoder_->SetSource(
1078 &video_source_,
1079 VideoSendStream::DegradationPreference::kMaintainFramerate);
937 video_source_.IncomingCapturedFrame( 1080 video_source_.IncomingCapturedFrame(
938 CreateFrame(sequence, frame_width, frame_height)); 1081 CreateFrame(sequence, frame_width, frame_height));
939 sink_.WaitForEncodedFrame(sequence++); 1082 sink_.WaitForEncodedFrame(sequence++);
940 stats = stats_proxy_->GetStats(); 1083 stats = stats_proxy_->GetStats();
941 EXPECT_TRUE(stats.cpu_limited_resolution); 1084 EXPECT_TRUE(stats.cpu_limited_resolution);
942 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); 1085 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes);
943 1086
944 // Trigger CPU normal usage. 1087 // Trigger CPU normal usage.
945 vie_encoder_->TriggerCpuNormalUsage(); 1088 vie_encoder_->TriggerCpuNormalUsage();
946 video_source_.IncomingCapturedFrame( 1089 video_source_.IncomingCapturedFrame(
947 CreateFrame(sequence, frame_width, frame_height)); 1090 CreateFrame(sequence, frame_width, frame_height));
948 sink_.WaitForEncodedFrame(sequence++); 1091 sink_.WaitForEncodedFrame(sequence++);
949 stats = stats_proxy_->GetStats(); 1092 stats = stats_proxy_->GetStats();
950 EXPECT_FALSE(stats.cpu_limited_resolution); 1093 EXPECT_FALSE(stats.cpu_limited_resolution);
951 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); 1094 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
1095
1096 // Back to the source with adaptation off, set it back to maintain-resolution.
1097 vie_encoder_->SetSource(
1098 &new_video_source,
1099 VideoSendStream::DegradationPreference::kMaintainResolution);
1100 new_video_source.IncomingCapturedFrame(
1101 CreateFrame(sequence, frame_width, frame_height));
1102 sink_.WaitForEncodedFrame(sequence++);
1103 stats = stats_proxy_->GetStats();
1104 // Disabled, since we previously switched the source too disabled.
1105 EXPECT_FALSE(stats.cpu_limited_resolution);
1106 EXPECT_EQ(3, stats.number_of_cpu_adapt_changes);
1107
1108 // Trigger CPU normal usage.
1109 vie_encoder_->TriggerCpuNormalUsage();
1110 new_video_source.IncomingCapturedFrame(
1111 CreateFrame(sequence, frame_width, frame_height));
1112 sink_.WaitForEncodedFrame(sequence++);
1113 stats = stats_proxy_->GetStats();
1114 EXPECT_FALSE(stats.cpu_limited_resolution);
1115 EXPECT_EQ(4, stats.number_of_cpu_adapt_changes);
952 1116
953 vie_encoder_->Stop(); 1117 vie_encoder_->Stop();
954 } 1118 }
955 1119
956 TEST_F(ViEEncoderTest, StatsTracksPreferredBitrate) { 1120 TEST_F(ViEEncoderTest, StatsTracksPreferredBitrate) {
957 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 1121 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
958 1122
959 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720)); 1123 video_source_.IncomingCapturedFrame(CreateFrame(1, 1280, 720));
960 sink_.WaitForEncodedFrame(1); 1124 sink_.WaitForEncodedFrame(1);
961 1125
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
1227 video_source_.IncomingCapturedFrame( 1391 video_source_.IncomingCapturedFrame(
1228 CreateFrame(1, kFrameWidth, kFrameHeight)); 1392 CreateFrame(1, kFrameWidth, kFrameHeight));
1229 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight); 1393 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight);
1230 1394
1231 // Trigger CPU overuse, downscale by 3/4. 1395 // Trigger CPU overuse, downscale by 3/4.
1232 vie_encoder_->TriggerCpuOveruse(); 1396 vie_encoder_->TriggerCpuOveruse();
1233 video_source_.IncomingCapturedFrame( 1397 video_source_.IncomingCapturedFrame(
1234 CreateFrame(2, kFrameWidth, kFrameHeight)); 1398 CreateFrame(2, kFrameWidth, kFrameHeight));
1235 sink_.WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4); 1399 sink_.WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4);
1236 1400
1237 // Trigger CPU normal use, return to original resoluton; 1401 // Trigger CPU normal use, return to original resolution;
1238 vie_encoder_->TriggerCpuNormalUsage(); 1402 vie_encoder_->TriggerCpuNormalUsage();
1239 video_source_.IncomingCapturedFrame( 1403 video_source_.IncomingCapturedFrame(
1240 CreateFrame(3, kFrameWidth, kFrameHeight)); 1404 CreateFrame(3, kFrameWidth, kFrameHeight));
1241 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight); 1405 sink_.WaitForEncodedFrame(kFrameWidth, kFrameHeight);
1242 1406
1243 vie_encoder_->Stop(); 1407 vie_encoder_->Stop();
1244 } 1408 }
1409
1410 TEST_F(ViEEncoderTest, AdaptsFrameOnOveruseWithMaintainResolution) {
1411 const int kDefaultFramerateFps = 30;
1412 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerateFps;
1413 const int kFrameWidth = 1280;
1414 const int kFrameHeight = 720;
1415 rtc::ScopedFakeClock fake_clock;
1416
1417 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1418 vie_encoder_->SetSource(
1419 &video_source_,
1420 VideoSendStream::DegradationPreference::kMaintainResolution);
1421 video_source_.set_adaptation_enabled(true);
1422
1423 fake_clock.SetTimeMicros(kFrameIntervalMs * 1000);
1424 int64_t timestamp_ms = kFrameIntervalMs;
1425
1426 video_source_.IncomingCapturedFrame(
1427 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1428 sink_.WaitForEncodedFrame(timestamp_ms);
1429
1430 // Try to trigger overuse. No fps estimate available => no effect.
1431 vie_encoder_->TriggerCpuOveruse();
1432
1433 // Insert frames for one second to get a stable estimate.
1434 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1435 timestamp_ms += kFrameIntervalMs;
1436 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1437 video_source_.IncomingCapturedFrame(
1438 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1439 sink_.WaitForEncodedFrame(timestamp_ms);
1440 }
1441
1442 // Trigger CPU overuse, reduce framerate by 2/3.
1443 vie_encoder_->TriggerCpuOveruse();
1444 int num_frames_dropped = 0;
1445 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1446 timestamp_ms += kFrameIntervalMs;
1447 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1448 video_source_.IncomingCapturedFrame(
1449 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1450 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1451 ++num_frames_dropped;
1452 } else {
1453 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1454 }
1455 }
1456
1457 // TODO(sprang): Find where there's rounding errors or stuff causing the
1458 // margin here to be a little larger than we'd like (input fps estimate is
1459 // off) and the frame dropping is a little too aggressive.
1460 const int kErrorMargin = 5;
1461 EXPECT_NEAR(num_frames_dropped,
1462 kDefaultFramerateFps - (kDefaultFramerateFps * 2 / 3),
1463 kErrorMargin);
1464
1465 // Trigger CPU overuse, reduce framerate by 2/3 again.
1466 vie_encoder_->TriggerCpuOveruse();
1467 num_frames_dropped = 0;
1468 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1469 timestamp_ms += kFrameIntervalMs;
1470 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1471 video_source_.IncomingCapturedFrame(
1472 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1473 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1474 ++num_frames_dropped;
1475 } else {
1476 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1477 }
1478 }
1479 EXPECT_NEAR(num_frames_dropped,
1480 kDefaultFramerateFps - (kDefaultFramerateFps * 4 / 9),
1481 kErrorMargin);
1482
1483 // Go back up one step.
1484 vie_encoder_->TriggerCpuNormalUsage();
1485 num_frames_dropped = 0;
1486 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1487 timestamp_ms += kFrameIntervalMs;
1488 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1489 video_source_.IncomingCapturedFrame(
1490 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1491 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1492 ++num_frames_dropped;
1493 } else {
1494 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1495 }
1496 }
1497 EXPECT_NEAR(num_frames_dropped,
1498 kDefaultFramerateFps - (kDefaultFramerateFps * 2 / 3),
1499 kErrorMargin);
1500
1501 // Go back up to original mode.
1502 vie_encoder_->TriggerCpuNormalUsage();
1503 num_frames_dropped = 0;
1504 for (int i = 0; i < kDefaultFramerateFps; ++i) {
1505 timestamp_ms += kFrameIntervalMs;
1506 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1507 video_source_.IncomingCapturedFrame(
1508 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1509 if (!sink_.WaitForFrame(kFrameTimeoutMs)) {
1510 ++num_frames_dropped;
1511 } else {
1512 sink_.CheckLastFrameSizeMathces(kFrameWidth, kFrameHeight);
1513 }
1514 }
1515 EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin);
1516
1517 vie_encoder_->Stop();
1518 }
1519
1520 TEST_F(ViEEncoderTest, DoesntAdaptDownPastMinFramerate) {
1521 const int kFramerateFps = 5;
1522 const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps;
1523 const int kMinFpsFrameInterval = rtc::kNumMillisecsPerSec / kMinFramerateFps;
1524 const int kFrameWidth = 1280;
1525 const int kFrameHeight = 720;
1526
1527 rtc::ScopedFakeClock fake_clock;
1528 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
1529 vie_encoder_->SetSource(
1530 &video_source_,
1531 VideoSendStream::DegradationPreference::kMaintainResolution);
1532 video_source_.set_adaptation_enabled(true);
1533
1534 fake_clock.SetTimeMicros(kFrameIntervalMs * 1000);
1535 int64_t timestamp_ms = kFrameIntervalMs;
1536
1537 // Trigger overuse as much as we can.
1538 for (int i = 0; i < ViEEncoder::kMaxCpuResolutionDowngrades; ++i) {
1539 // Insert frames to get a new fps estimate...
1540 for (int j = 0; j < kFramerateFps; ++j) {
1541 video_source_.IncomingCapturedFrame(
1542 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1543 timestamp_ms += kFrameIntervalMs;
1544 fake_clock.AdvanceTimeMicros(kFrameIntervalMs * 1000);
1545 }
1546 // ...and then try to adapt again.
1547 vie_encoder_->TriggerCpuOveruse();
1548 }
1549
1550 // Drain any frame in the pipeline.
1551 sink_.WaitForFrame(kDefaultTimeoutMs);
1552
1553 // Insert frames at min fps, all should go through.
1554 for (int i = 0; i < 10; ++i) {
1555 timestamp_ms += kMinFpsFrameInterval;
1556 fake_clock.AdvanceTimeMicros(kMinFpsFrameInterval * 1000);
1557 video_source_.IncomingCapturedFrame(
1558 CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight));
1559 sink_.WaitForEncodedFrame(timestamp_ms);
1560 }
1561
1562 vie_encoder_->Stop();
1563 }
1245 } // namespace webrtc 1564 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698