OLD | NEW |
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 <utility> | 11 #include <utility> |
12 | 12 |
13 #include "webrtc/base/logging.h" | 13 #include "webrtc/base/logging.h" |
| 14 #include "webrtc/system_wrappers/include/metrics_default.h" |
14 #include "webrtc/test/encoder_settings.h" | 15 #include "webrtc/test/encoder_settings.h" |
15 #include "webrtc/test/fake_encoder.h" | 16 #include "webrtc/test/fake_encoder.h" |
16 #include "webrtc/test/frame_generator.h" | 17 #include "webrtc/test/frame_generator.h" |
17 #include "webrtc/test/gtest.h" | 18 #include "webrtc/test/gtest.h" |
18 #include "webrtc/video/send_statistics_proxy.h" | 19 #include "webrtc/video/send_statistics_proxy.h" |
19 #include "webrtc/video/vie_encoder.h" | 20 #include "webrtc/video/vie_encoder.h" |
20 | 21 |
21 namespace webrtc { | 22 namespace webrtc { |
22 | 23 |
| 24 namespace { |
| 25 class TestBuffer : public webrtc::I420Buffer { |
| 26 public: |
| 27 TestBuffer(rtc::Event* event, int width, int height) |
| 28 : I420Buffer(width, height), event_(event) {} |
| 29 |
| 30 private: |
| 31 friend class rtc::RefCountedObject<TestBuffer>; |
| 32 ~TestBuffer() override { |
| 33 if (event_) |
| 34 event_->Set(); |
| 35 } |
| 36 rtc::Event* const event_; |
| 37 }; |
| 38 |
| 39 class ViEEncoderUnderTest : public ViEEncoder { |
| 40 public: |
| 41 ViEEncoderUnderTest( |
| 42 SendStatisticsProxy* stats_proxy, |
| 43 const webrtc::VideoSendStream::Config::EncoderSettings& settings) |
| 44 : ViEEncoder(1 /* number_of_cores */, |
| 45 stats_proxy, |
| 46 settings, |
| 47 nullptr /* pre_encode_callback */, |
| 48 nullptr /* encoder_timing */) {} |
| 49 |
| 50 void TriggerCpuOveruse() { |
| 51 rtc::Event event(false, false); |
| 52 encoder_queue()->PostTask([this, &event] { |
| 53 OveruseDetected(); |
| 54 event.Set(); |
| 55 }); |
| 56 event.Wait(rtc::Event::kForever); |
| 57 } |
| 58 |
| 59 void TriggerCpuNormalUsage() { |
| 60 rtc::Event event(false, false); |
| 61 encoder_queue()->PostTask([this, &event] { |
| 62 NormalUsage(); |
| 63 event.Set(); |
| 64 }); |
| 65 event.Wait(rtc::Event::kForever); |
| 66 } |
| 67 }; |
| 68 |
| 69 } // namespace |
| 70 |
23 class ViEEncoderTest : public ::testing::Test { | 71 class ViEEncoderTest : public ::testing::Test { |
24 public: | 72 public: |
25 static const int kDefaultTimeoutMs = 30 * 1000; | 73 static const int kDefaultTimeoutMs = 30 * 1000; |
26 | 74 |
27 ViEEncoderTest() | 75 ViEEncoderTest() |
28 : video_send_config_(VideoSendStream::Config(nullptr)), | 76 : video_send_config_(VideoSendStream::Config(nullptr)), |
29 codec_width_(320), | 77 codec_width_(320), |
30 codec_height_(240), | 78 codec_height_(240), |
31 fake_encoder_(), | 79 fake_encoder_(), |
32 stats_proxy_(Clock::GetRealTimeClock(), | 80 stats_proxy_(new SendStatisticsProxy( |
33 video_send_config_, | 81 Clock::GetRealTimeClock(), |
34 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo), | 82 video_send_config_, |
| 83 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)), |
35 sink_(&fake_encoder_) {} | 84 sink_(&fake_encoder_) {} |
36 | 85 |
37 void SetUp() override { | 86 void SetUp() override { |
38 video_send_config_ = VideoSendStream::Config(nullptr); | 87 video_send_config_ = VideoSendStream::Config(nullptr); |
39 video_send_config_.encoder_settings.encoder = &fake_encoder_; | 88 video_send_config_.encoder_settings.encoder = &fake_encoder_; |
40 video_send_config_.encoder_settings.payload_name = "FAKE"; | 89 video_send_config_.encoder_settings.payload_name = "FAKE"; |
41 video_send_config_.encoder_settings.payload_type = 125; | 90 video_send_config_.encoder_settings.payload_type = 125; |
42 | 91 |
43 VideoEncoderConfig video_encoder_config; | 92 VideoEncoderConfig video_encoder_config; |
44 test::FillEncoderConfiguration(1, &video_encoder_config); | 93 test::FillEncoderConfiguration(1, &video_encoder_config); |
45 vie_encoder_.reset(new ViEEncoder( | 94 vie_encoder_.reset(new ViEEncoderUnderTest( |
46 1 /* number_of_cores */, &stats_proxy_, | 95 stats_proxy_.get(), video_send_config_.encoder_settings)); |
47 video_send_config_.encoder_settings, nullptr /* pre_encode_callback */, | 96 vie_encoder_->SetSink(&sink_, false /* rotation_applied */); |
48 nullptr /* overuse_callback */, nullptr /* encoder_timing */)); | 97 vie_encoder_->SetSource(&video_source_, |
49 vie_encoder_->SetSink(&sink_); | 98 false /* disable_resolution_scaling */); |
50 vie_encoder_->SetSource(&video_source_); | |
51 vie_encoder_->SetStartBitrate(10000); | 99 vie_encoder_->SetStartBitrate(10000); |
52 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 1440); | 100 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 1440); |
53 } | 101 } |
54 | 102 |
55 VideoFrame CreateFrame(int64_t ntp_ts, rtc::Event* destruction_event) const { | 103 VideoFrame CreateFrame(int64_t ntp_ts, rtc::Event* destruction_event) const { |
56 class TestBuffer : public webrtc::I420Buffer { | 104 class TestBuffer : public webrtc::I420Buffer { |
57 public: | 105 public: |
58 TestBuffer(rtc::Event* event, int width, int height) | 106 TestBuffer(rtc::Event* event, int width, int height) |
59 : I420Buffer(width, height), event_(event) {} | 107 : I420Buffer(width, height), event_(event) {} |
60 | 108 |
61 private: | 109 private: |
62 friend class rtc::RefCountedObject<TestBuffer>; | 110 friend class rtc::RefCountedObject<TestBuffer>; |
63 ~TestBuffer() override { | 111 ~TestBuffer() override { |
64 if (event_) | 112 if (event_) |
65 event_->Set(); | 113 event_->Set(); |
66 } | 114 } |
67 rtc::Event* const event_; | 115 rtc::Event* const event_; |
68 }; | 116 }; |
69 | 117 |
70 VideoFrame frame(new rtc::RefCountedObject<TestBuffer>( | 118 VideoFrame frame(new rtc::RefCountedObject<TestBuffer>( |
71 destruction_event, codec_width_, codec_height_), | 119 destruction_event, codec_width_, codec_height_), |
72 99, 99, kVideoRotation_0); | 120 99, 99, kVideoRotation_0); |
73 frame.set_ntp_time_ms(ntp_ts); | 121 frame.set_ntp_time_ms(ntp_ts); |
74 return frame; | 122 return frame; |
75 } | 123 } |
76 | 124 |
| 125 VideoFrame CreateFrame(int64_t ntp_ts, int width, int height) const { |
| 126 VideoFrame frame( |
| 127 new rtc::RefCountedObject<TestBuffer>(nullptr, width, height), 99, 99, |
| 128 kVideoRotation_0); |
| 129 frame.set_ntp_time_ms(ntp_ts); |
| 130 return frame; |
| 131 } |
| 132 |
77 class TestEncoder : public test::FakeEncoder { | 133 class TestEncoder : public test::FakeEncoder { |
78 public: | 134 public: |
79 TestEncoder() | 135 TestEncoder() |
80 : FakeEncoder(Clock::GetRealTimeClock()), | 136 : FakeEncoder(Clock::GetRealTimeClock()), |
81 continue_encode_event_(false, false) {} | 137 continue_encode_event_(false, false) {} |
82 | 138 |
83 VideoCodec codec_config() { | 139 VideoCodec codec_config() { |
84 rtc::CritScope lock(&crit_); | 140 rtc::CritScope lock(&crit_); |
85 return config_; | 141 return config_; |
86 } | 142 } |
(...skipping 18 matching lines...) Expand all Loading... |
105 const std::vector<FrameType>* frame_types) override { | 161 const std::vector<FrameType>* frame_types) override { |
106 bool block_encode; | 162 bool block_encode; |
107 { | 163 { |
108 rtc::CritScope lock(&crit_); | 164 rtc::CritScope lock(&crit_); |
109 EXPECT_GT(input_image.timestamp(), timestamp_); | 165 EXPECT_GT(input_image.timestamp(), timestamp_); |
110 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_); | 166 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_); |
111 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90); | 167 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90); |
112 | 168 |
113 timestamp_ = input_image.timestamp(); | 169 timestamp_ = input_image.timestamp(); |
114 ntp_time_ms_ = input_image.ntp_time_ms(); | 170 ntp_time_ms_ = input_image.ntp_time_ms(); |
| 171 last_input_width_ = input_image.width(); |
| 172 last_input_height_ = input_image.height(); |
115 block_encode = block_next_encode_; | 173 block_encode = block_next_encode_; |
116 block_next_encode_ = false; | 174 block_next_encode_ = false; |
117 } | 175 } |
118 int32_t result = | 176 int32_t result = |
119 FakeEncoder::Encode(input_image, codec_specific_info, frame_types); | 177 FakeEncoder::Encode(input_image, codec_specific_info, frame_types); |
120 if (block_encode) | 178 if (block_encode) |
121 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs)); | 179 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs)); |
122 return result; | 180 return result; |
123 } | 181 } |
124 | 182 |
125 | |
126 | |
127 rtc::CriticalSection crit_; | 183 rtc::CriticalSection crit_; |
128 bool block_next_encode_ = false; | 184 bool block_next_encode_ = false; |
129 rtc::Event continue_encode_event_; | 185 rtc::Event continue_encode_event_; |
130 uint32_t timestamp_ = 0; | 186 uint32_t timestamp_ = 0; |
131 int64_t ntp_time_ms_ = 0; | 187 int64_t ntp_time_ms_ = 0; |
| 188 int last_input_width_ = 0; |
| 189 int last_input_height_ = 0; |
132 }; | 190 }; |
133 | 191 |
134 class TestSink : public ViEEncoder::EncoderSink { | 192 class TestSink : public ViEEncoder::EncoderSink { |
135 public: | 193 public: |
136 explicit TestSink(TestEncoder* test_encoder) | 194 explicit TestSink(TestEncoder* test_encoder) |
137 : test_encoder_(test_encoder), encoded_frame_event_(false, false) {} | 195 : test_encoder_(test_encoder), encoded_frame_event_(false, false) {} |
138 | 196 |
139 void WaitForEncodedFrame(int64_t expected_ntp_time) { | 197 void WaitForEncodedFrame(int64_t expected_ntp_time) { |
140 uint32_t timestamp = 0; | 198 uint32_t timestamp = 0; |
141 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); | 199 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 uint32_t timestamp_ = 0; | 243 uint32_t timestamp_ = 0; |
186 bool expect_frames_ = true; | 244 bool expect_frames_ = true; |
187 int number_of_reconfigurations_ = 0; | 245 int number_of_reconfigurations_ = 0; |
188 int min_transmit_bitrate_bps_ = 0; | 246 int min_transmit_bitrate_bps_ = 0; |
189 }; | 247 }; |
190 | 248 |
191 VideoSendStream::Config video_send_config_; | 249 VideoSendStream::Config video_send_config_; |
192 int codec_width_; | 250 int codec_width_; |
193 int codec_height_; | 251 int codec_height_; |
194 TestEncoder fake_encoder_; | 252 TestEncoder fake_encoder_; |
195 SendStatisticsProxy stats_proxy_; | 253 std::unique_ptr<SendStatisticsProxy> stats_proxy_; |
196 TestSink sink_; | 254 TestSink sink_; |
197 test::FrameForwarder video_source_; | 255 test::FrameForwarder video_source_; |
198 std::unique_ptr<ViEEncoder> vie_encoder_; | 256 std::unique_ptr<ViEEncoderUnderTest> vie_encoder_; |
199 }; | 257 }; |
200 | 258 |
201 TEST_F(ViEEncoderTest, EncodeOneFrame) { | 259 TEST_F(ViEEncoderTest, EncodeOneFrame) { |
202 const int kTargetBitrateBps = 100000; | 260 const int kTargetBitrateBps = 100000; |
203 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 261 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
204 rtc::Event frame_destroyed_event(false, false); | 262 rtc::Event frame_destroyed_event(false, false); |
205 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); | 263 video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); |
206 sink_.WaitForEncodedFrame(1); | 264 sink_.WaitForEncodedFrame(1); |
207 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); | 265 EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); |
208 vie_encoder_->Stop(); | 266 vie_encoder_->Stop(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 // with the encoder thread. | 385 // with the encoder thread. |
328 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); | 386 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
329 sink_.WaitForEncodedFrame(2); | 387 sink_.WaitForEncodedFrame(2); |
330 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); | 388 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); |
331 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); | 389 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); |
332 EXPECT_EQ(3, sink_.number_of_reconfigurations()); | 390 EXPECT_EQ(3, sink_.number_of_reconfigurations()); |
333 | 391 |
334 vie_encoder_->Stop(); | 392 vie_encoder_->Stop(); |
335 } | 393 } |
336 | 394 |
| 395 TEST_F(ViEEncoderTest, SwitchSourceDeregisterEncoderAsSink) { |
| 396 EXPECT_EQ(1u, video_source_.number_of_sinks()); |
| 397 test::FrameForwarder new_video_source; |
| 398 vie_encoder_->SetSource(&new_video_source, false); |
| 399 EXPECT_EQ(0u, video_source_.number_of_sinks()); |
| 400 EXPECT_EQ(1u, new_video_source.number_of_sinks()); |
| 401 |
| 402 vie_encoder_->Stop(); |
| 403 } |
| 404 |
| 405 TEST_F(ViEEncoderTest, SinkWantsRotationApplied) { |
| 406 EXPECT_FALSE(video_source_.current_sink_wants().rotation_applied); |
| 407 vie_encoder_->SetSink(&sink_, true /*rotation_applied*/); |
| 408 EXPECT_TRUE(video_source_.current_sink_wants().rotation_applied); |
| 409 vie_encoder_->Stop(); |
| 410 } |
| 411 |
| 412 TEST_F(ViEEncoderTest, SinkWantsFromOveruseDetector) { |
| 413 const int kTargetBitrateBps = 100000; |
| 414 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 415 |
| 416 EXPECT_FALSE(video_source_.current_sink_wants().max_pixel_count); |
| 417 EXPECT_FALSE(video_source_.current_sink_wants().max_pixel_count_step_up); |
| 418 |
| 419 int frame_width = 1280; |
| 420 int frame_height = 720; |
| 421 |
| 422 // Trigger CPU overuse kMaxCpuDowngrades times. Every time, ViEEncoder should |
| 423 // request |
| 424 // lower resolution. |
| 425 for (int i = 1; i <= ViEEncoder::kMaxCpuDowngrades; ++i) { |
| 426 video_source_.IncomingCapturedFrame( |
| 427 CreateFrame(i, frame_width, frame_height)); |
| 428 sink_.WaitForEncodedFrame(i); |
| 429 |
| 430 vie_encoder_->TriggerCpuOveruse(); |
| 431 |
| 432 EXPECT_LT(video_source_.current_sink_wants().max_pixel_count.value_or(0), |
| 433 frame_width * frame_height); |
| 434 EXPECT_FALSE(video_source_.current_sink_wants().max_pixel_count_step_up); |
| 435 |
| 436 frame_width /= 2; |
| 437 frame_height /= 2; |
| 438 } |
| 439 |
| 440 // Trigger CPU overuse a one more time. This should not trigger a request for |
| 441 // lower resolution. |
| 442 rtc::VideoSinkWants current_wants = video_source_.current_sink_wants(); |
| 443 video_source_.IncomingCapturedFrame(CreateFrame( |
| 444 ViEEncoder::kMaxCpuDowngrades + 1, frame_width, frame_height)); |
| 445 sink_.WaitForEncodedFrame(ViEEncoder::kMaxCpuDowngrades + 1); |
| 446 vie_encoder_->TriggerCpuOveruse(); |
| 447 EXPECT_EQ(video_source_.current_sink_wants().max_pixel_count, |
| 448 current_wants.max_pixel_count); |
| 449 EXPECT_EQ(video_source_.current_sink_wants().max_pixel_count_step_up, |
| 450 current_wants.max_pixel_count_step_up); |
| 451 |
| 452 // Trigger CPU normal use. |
| 453 vie_encoder_->TriggerCpuNormalUsage(); |
| 454 EXPECT_FALSE(video_source_.current_sink_wants().max_pixel_count); |
| 455 EXPECT_EQ( |
| 456 video_source_.current_sink_wants().max_pixel_count_step_up.value_or(0), |
| 457 frame_width * frame_height); |
| 458 |
| 459 vie_encoder_->Stop(); |
| 460 } |
| 461 |
| 462 TEST_F(ViEEncoderTest, |
| 463 ResolutionSinkWantsResetOnSetSourceWithDisabledResolutionScaling) { |
| 464 const int kTargetBitrateBps = 100000; |
| 465 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 466 |
| 467 EXPECT_FALSE(video_source_.current_sink_wants().max_pixel_count); |
| 468 EXPECT_FALSE(video_source_.current_sink_wants().max_pixel_count_step_up); |
| 469 |
| 470 int frame_width = 1280; |
| 471 int frame_height = 720; |
| 472 |
| 473 // Trigger CPU overuse. |
| 474 vie_encoder_->TriggerCpuOveruse(); |
| 475 |
| 476 video_source_.IncomingCapturedFrame( |
| 477 CreateFrame(1, frame_width, frame_height)); |
| 478 sink_.WaitForEncodedFrame(1); |
| 479 EXPECT_LT(video_source_.current_sink_wants().max_pixel_count.value_or(0), |
| 480 frame_width * frame_height); |
| 481 EXPECT_FALSE(video_source_.current_sink_wants().max_pixel_count_step_up); |
| 482 |
| 483 // Set new source. |
| 484 test::FrameForwarder new_video_source; |
| 485 vie_encoder_->SetSource(&new_video_source, |
| 486 true /* disable_resolution_scaling*/); |
| 487 |
| 488 EXPECT_FALSE(new_video_source.current_sink_wants().max_pixel_count); |
| 489 EXPECT_FALSE(new_video_source.current_sink_wants().max_pixel_count_step_up); |
| 490 |
| 491 new_video_source.IncomingCapturedFrame( |
| 492 CreateFrame(3, frame_width, frame_height)); |
| 493 sink_.WaitForEncodedFrame(3); |
| 494 EXPECT_FALSE(new_video_source.current_sink_wants().max_pixel_count); |
| 495 EXPECT_FALSE(new_video_source.current_sink_wants().max_pixel_count_step_up); |
| 496 |
| 497 // Calling SetSource with resolution scaling enabled apply the old SinkWants. |
| 498 vie_encoder_->SetSource(&new_video_source, |
| 499 false /* disable_resolution_scaling*/); |
| 500 EXPECT_LT(new_video_source.current_sink_wants().max_pixel_count.value_or(0), |
| 501 frame_width * frame_height); |
| 502 EXPECT_FALSE(new_video_source.current_sink_wants().max_pixel_count_step_up); |
| 503 |
| 504 vie_encoder_->Stop(); |
| 505 } |
| 506 |
| 507 TEST_F(ViEEncoderTest, StatsTracksAdaptationStats) { |
| 508 const int kTargetBitrateBps = 100000; |
| 509 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 510 |
| 511 int frame_width = 1280; |
| 512 int frame_height = 720; |
| 513 |
| 514 video_source_.IncomingCapturedFrame( |
| 515 CreateFrame(1, frame_width, frame_height)); |
| 516 sink_.WaitForEncodedFrame(1); |
| 517 VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 518 EXPECT_FALSE(stats.cpu_limited_resolution); |
| 519 EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 520 |
| 521 // Trigger CPU overuse. |
| 522 vie_encoder_->TriggerCpuOveruse(); |
| 523 video_source_.IncomingCapturedFrame( |
| 524 CreateFrame(2, frame_width, frame_height)); |
| 525 sink_.WaitForEncodedFrame(2); |
| 526 |
| 527 stats = stats_proxy_->GetStats(); |
| 528 EXPECT_TRUE(stats.cpu_limited_resolution); |
| 529 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 530 |
| 531 // Trigger CPU normal use. |
| 532 vie_encoder_->TriggerCpuNormalUsage(); |
| 533 video_source_.IncomingCapturedFrame( |
| 534 CreateFrame(3, frame_width, frame_height)); |
| 535 sink_.WaitForEncodedFrame(3); |
| 536 |
| 537 stats = stats_proxy_->GetStats(); |
| 538 EXPECT_FALSE(stats.cpu_limited_resolution); |
| 539 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
| 540 |
| 541 vie_encoder_->Stop(); |
| 542 } |
| 543 |
| 544 TEST_F(ViEEncoderTest, StatsTracksAdaptationStatsWhenSwitchingSource) { |
| 545 const int kTargetBitrateBps = 100000; |
| 546 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 547 |
| 548 // Trigger CPU overuse. |
| 549 vie_encoder_->TriggerCpuOveruse(); |
| 550 int frame_width = 1280; |
| 551 int frame_height = 720; |
| 552 |
| 553 video_source_.IncomingCapturedFrame( |
| 554 CreateFrame(1, frame_width, frame_height)); |
| 555 sink_.WaitForEncodedFrame(1); |
| 556 |
| 557 VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 558 EXPECT_TRUE(stats.cpu_limited_resolution); |
| 559 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 560 |
| 561 // Set new source with adaptation still enabled. |
| 562 test::FrameForwarder new_video_source; |
| 563 vie_encoder_->SetSource(&new_video_source, |
| 564 false /* disable_resolution_scaling*/); |
| 565 |
| 566 new_video_source.IncomingCapturedFrame( |
| 567 CreateFrame(2, frame_width, frame_height)); |
| 568 sink_.WaitForEncodedFrame(2); |
| 569 stats = stats_proxy_->GetStats(); |
| 570 EXPECT_TRUE(stats.cpu_limited_resolution); |
| 571 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 572 |
| 573 // Set adaptation disabled. |
| 574 vie_encoder_->SetSource(&new_video_source, |
| 575 true /* disable_resolution_scaling*/); |
| 576 new_video_source.IncomingCapturedFrame( |
| 577 CreateFrame(3, frame_width, frame_height)); |
| 578 sink_.WaitForEncodedFrame(3); |
| 579 stats = stats_proxy_->GetStats(); |
| 580 EXPECT_FALSE(stats.cpu_limited_resolution); |
| 581 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 582 |
| 583 // Switch back the source with adaptation enabled. |
| 584 vie_encoder_->SetSource(&video_source_, |
| 585 false /* disable_resolution_scaling*/); |
| 586 video_source_.IncomingCapturedFrame( |
| 587 CreateFrame(4, frame_width, frame_height)); |
| 588 sink_.WaitForEncodedFrame(4); |
| 589 stats = stats_proxy_->GetStats(); |
| 590 EXPECT_TRUE(stats.cpu_limited_resolution); |
| 591 EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 592 |
| 593 // Trigger CPU normal usage. |
| 594 vie_encoder_->TriggerCpuNormalUsage(); |
| 595 video_source_.IncomingCapturedFrame( |
| 596 CreateFrame(5, frame_width, frame_height)); |
| 597 sink_.WaitForEncodedFrame(5); |
| 598 stats = stats_proxy_->GetStats(); |
| 599 EXPECT_FALSE(stats.cpu_limited_resolution); |
| 600 EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
| 601 |
| 602 vie_encoder_->Stop(); |
| 603 } |
| 604 |
| 605 TEST_F(ViEEncoderTest, UMACpuLimitedResolutionInPercent) { |
| 606 const int kTargetBitrateBps = 100000; |
| 607 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 608 |
| 609 int frame_width = 640; |
| 610 int frame_height = 360; |
| 611 |
| 612 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredUMASamples; ++i) { |
| 613 video_source_.IncomingCapturedFrame( |
| 614 CreateFrame(i, frame_width, frame_height)); |
| 615 sink_.WaitForEncodedFrame(i); |
| 616 } |
| 617 |
| 618 vie_encoder_->TriggerCpuOveruse(); |
| 619 for (int i = 1; i <= SendStatisticsProxy::kMinRequiredUMASamples; ++i) { |
| 620 video_source_.IncomingCapturedFrame( |
| 621 CreateFrame(SendStatisticsProxy::kMinRequiredUMASamples + i, |
| 622 frame_width, frame_height)); |
| 623 sink_.WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredUMASamples + i); |
| 624 } |
| 625 |
| 626 vie_encoder_->Stop(); |
| 627 |
| 628 stats_proxy_.reset(); |
| 629 EXPECT_EQ(1, |
| 630 metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent")); |
| 631 EXPECT_EQ( |
| 632 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50)); |
| 633 } |
| 634 |
337 } // namespace webrtc | 635 } // namespace webrtc |
OLD | NEW |