| 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 "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/test/encoder_settings.h" | 15 #include "webrtc/test/encoder_settings.h" |
| 16 #include "webrtc/test/fake_encoder.h" | 16 #include "webrtc/test/fake_encoder.h" |
| 17 #include "webrtc/test/frame_generator.h" | 17 #include "webrtc/test/frame_generator.h" |
| 18 #include "webrtc/video/send_statistics_proxy.h" | 18 #include "webrtc/video/send_statistics_proxy.h" |
| 19 #include "webrtc/video/vie_encoder.h" | 19 #include "webrtc/video/vie_encoder.h" |
| 20 | 20 |
| 21 namespace webrtc { | 21 namespace webrtc { |
| 22 | 22 |
| 23 class ViEEncoderTest : public ::testing::Test { | 23 class ViEEncoderTest : public ::testing::Test { |
| 24 public: | 24 public: |
| 25 static const int kDefaultTimeoutMs = 30 * 1000; | 25 static const int kDefaultTimeoutMs = 30 * 1000; |
| 26 | 26 |
| 27 ViEEncoderTest() | 27 ViEEncoderTest() |
| 28 : video_send_config_(VideoSendStream::Config(nullptr)), | 28 : video_send_config_(VideoSendStream::Config(nullptr)), |
| 29 codec_width_(320), |
| 30 codec_height_(240), |
| 29 fake_encoder_(), | 31 fake_encoder_(), |
| 30 stats_proxy_(Clock::GetRealTimeClock(), | 32 stats_proxy_(Clock::GetRealTimeClock(), |
| 31 video_send_config_, | 33 video_send_config_, |
| 32 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo), | 34 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo), |
| 33 sink_(&fake_encoder_) {} | 35 sink_(&fake_encoder_) {} |
| 34 | 36 |
| 35 void SetUp() override { | 37 void SetUp() override { |
| 36 video_send_config_ = VideoSendStream::Config(nullptr); | 38 video_send_config_ = VideoSendStream::Config(nullptr); |
| 37 video_send_config_.encoder_settings.encoder = &fake_encoder_; | 39 video_send_config_.encoder_settings.encoder = &fake_encoder_; |
| 38 video_send_config_.encoder_settings.payload_name = "FAKE"; | 40 video_send_config_.encoder_settings.payload_name = "FAKE"; |
| 39 video_send_config_.encoder_settings.payload_type = 125; | 41 video_send_config_.encoder_settings.payload_type = 125; |
| 40 | 42 |
| 41 VideoEncoderConfig video_encoder_config; | 43 VideoEncoderConfig video_encoder_config; |
| 42 video_encoder_config.streams = test::CreateVideoStreams(1); | 44 test::FillEncoderConfiguration(1, &video_encoder_config); |
| 43 codec_width_ = static_cast<int>(video_encoder_config.streams[0].width); | |
| 44 codec_height_ = static_cast<int>(video_encoder_config.streams[0].height); | |
| 45 | |
| 46 vie_encoder_.reset(new ViEEncoder( | 45 vie_encoder_.reset(new ViEEncoder( |
| 47 1 /* number_of_cores */, &stats_proxy_, | 46 1 /* number_of_cores */, &stats_proxy_, |
| 48 video_send_config_.encoder_settings, nullptr /* pre_encode_callback */, | 47 video_send_config_.encoder_settings, nullptr /* pre_encode_callback */, |
| 49 nullptr /* overuse_callback */, nullptr /* encoder_timing */)); | 48 nullptr /* overuse_callback */, nullptr /* encoder_timing */)); |
| 50 vie_encoder_->SetSink(&sink_); | 49 vie_encoder_->SetSink(&sink_); |
| 51 vie_encoder_->SetSource(&video_source_); | 50 vie_encoder_->SetSource(&video_source_); |
| 52 vie_encoder_->SetStartBitrate(10000); | 51 vie_encoder_->SetStartBitrate(10000); |
| 53 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 1440); | 52 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 1440); |
| 54 } | 53 } |
| 55 | 54 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 74 frame.set_ntp_time_ms(ntp_ts); | 73 frame.set_ntp_time_ms(ntp_ts); |
| 75 return frame; | 74 return frame; |
| 76 } | 75 } |
| 77 | 76 |
| 78 class TestEncoder : public test::FakeEncoder { | 77 class TestEncoder : public test::FakeEncoder { |
| 79 public: | 78 public: |
| 80 TestEncoder() | 79 TestEncoder() |
| 81 : FakeEncoder(Clock::GetRealTimeClock()), | 80 : FakeEncoder(Clock::GetRealTimeClock()), |
| 82 continue_encode_event_(false, false) {} | 81 continue_encode_event_(false, false) {} |
| 83 | 82 |
| 83 VideoCodec codec_config() { |
| 84 rtc::CritScope lock(&crit_); |
| 85 return config_; |
| 86 } |
| 87 |
| 88 void BlockNextEncode() { |
| 89 rtc::CritScope lock(&crit_); |
| 90 block_next_encode_ = true; |
| 91 } |
| 92 |
| 93 void ContinueEncode() { continue_encode_event_.Set(); } |
| 94 |
| 95 void CheckLastTimeStampsMatch(int64_t ntp_time_ms, |
| 96 uint32_t timestamp) const { |
| 97 rtc::CritScope lock(&crit_); |
| 98 EXPECT_EQ(timestamp_, timestamp); |
| 99 EXPECT_EQ(ntp_time_ms_, ntp_time_ms); |
| 100 } |
| 101 |
| 102 private: |
| 84 int32_t Encode(const VideoFrame& input_image, | 103 int32_t Encode(const VideoFrame& input_image, |
| 85 const CodecSpecificInfo* codec_specific_info, | 104 const CodecSpecificInfo* codec_specific_info, |
| 86 const std::vector<FrameType>* frame_types) override { | 105 const std::vector<FrameType>* frame_types) override { |
| 87 bool block_encode; | 106 bool block_encode; |
| 88 { | 107 { |
| 89 rtc::CritScope lock(&crit_); | 108 rtc::CritScope lock(&crit_); |
| 90 EXPECT_GT(input_image.timestamp(), timestamp_); | 109 EXPECT_GT(input_image.timestamp(), timestamp_); |
| 91 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_); | 110 EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_); |
| 92 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90); | 111 EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90); |
| 93 | 112 |
| 94 timestamp_ = input_image.timestamp(); | 113 timestamp_ = input_image.timestamp(); |
| 95 ntp_time_ms_ = input_image.ntp_time_ms(); | 114 ntp_time_ms_ = input_image.ntp_time_ms(); |
| 96 block_encode = block_next_encode_; | 115 block_encode = block_next_encode_; |
| 97 block_next_encode_ = false; | 116 block_next_encode_ = false; |
| 98 } | 117 } |
| 99 int32_t result = | 118 int32_t result = |
| 100 FakeEncoder::Encode(input_image, codec_specific_info, frame_types); | 119 FakeEncoder::Encode(input_image, codec_specific_info, frame_types); |
| 101 if (block_encode) | 120 if (block_encode) |
| 102 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs)); | 121 EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs)); |
| 103 return result; | 122 return result; |
| 104 } | 123 } |
| 105 | 124 |
| 106 void BlockNextEncode() { | |
| 107 rtc::CritScope lock(&crit_); | |
| 108 block_next_encode_ = true; | |
| 109 } | |
| 110 | 125 |
| 111 void ContinueEncode() { continue_encode_event_.Set(); } | |
| 112 | 126 |
| 113 void CheckLastTimeStampsMatch(int64_t ntp_time_ms, | |
| 114 uint32_t timestamp) const { | |
| 115 rtc::CritScope lock(&crit_); | |
| 116 EXPECT_EQ(timestamp_, timestamp); | |
| 117 EXPECT_EQ(ntp_time_ms_, ntp_time_ms); | |
| 118 } | |
| 119 | |
| 120 private: | |
| 121 rtc::CriticalSection crit_; | 127 rtc::CriticalSection crit_; |
| 122 bool block_next_encode_ = false; | 128 bool block_next_encode_ = false; |
| 123 rtc::Event continue_encode_event_; | 129 rtc::Event continue_encode_event_; |
| 124 uint32_t timestamp_ = 0; | 130 uint32_t timestamp_ = 0; |
| 125 int64_t ntp_time_ms_ = 0; | 131 int64_t ntp_time_ms_ = 0; |
| 126 }; | 132 }; |
| 127 | 133 |
| 128 class TestSink : public ViEEncoder::EncoderSink { | 134 class TestSink : public ViEEncoder::EncoderSink { |
| 129 public: | 135 public: |
| 130 explicit TestSink(TestEncoder* test_encoder) | 136 explicit TestSink(TestEncoder* test_encoder) |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 vie_encoder_->Stop(); | 283 vie_encoder_->Stop(); |
| 278 } | 284 } |
| 279 | 285 |
| 280 TEST_F(ViEEncoderTest, ConfigureEncoderTriggersOnEncoderConfigurationChanged) { | 286 TEST_F(ViEEncoderTest, ConfigureEncoderTriggersOnEncoderConfigurationChanged) { |
| 281 const int kTargetBitrateBps = 100000; | 287 const int kTargetBitrateBps = 100000; |
| 282 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); | 288 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 283 | 289 |
| 284 // Capture a frame and wait for it to synchronize with the encoder thread. | 290 // Capture a frame and wait for it to synchronize with the encoder thread. |
| 285 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); | 291 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
| 286 sink_.WaitForEncodedFrame(1); | 292 sink_.WaitForEncodedFrame(1); |
| 287 EXPECT_EQ(1, sink_.number_of_reconfigurations()); | 293 // The encoder will have been configured twice. First time before the first |
| 294 // frame has been received. Then a second time when the resolution is known. |
| 295 EXPECT_EQ(2, sink_.number_of_reconfigurations()); |
| 288 | 296 |
| 289 VideoEncoderConfig video_encoder_config; | 297 VideoEncoderConfig video_encoder_config; |
| 290 video_encoder_config.streams = test::CreateVideoStreams(1); | 298 test::FillEncoderConfiguration(1, &video_encoder_config); |
| 291 video_encoder_config.min_transmit_bitrate_bps = 9999; | 299 video_encoder_config.min_transmit_bitrate_bps = 9999; |
| 292 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 1440); | 300 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 1440); |
| 293 | 301 |
| 294 // Capture a frame and wait for it to synchronize with the encoder thread. | 302 // Capture a frame and wait for it to synchronize with the encoder thread. |
| 295 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); | 303 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
| 296 sink_.WaitForEncodedFrame(2); | 304 sink_.WaitForEncodedFrame(2); |
| 297 EXPECT_EQ(2, sink_.number_of_reconfigurations()); | 305 EXPECT_EQ(3, sink_.number_of_reconfigurations()); |
| 298 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate()); | 306 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate()); |
| 299 | 307 |
| 300 vie_encoder_->Stop(); | 308 vie_encoder_->Stop(); |
| 301 } | 309 } |
| 302 | 310 |
| 311 TEST_F(ViEEncoderTest, FrameResolutionChangeReconfigureEncoder) { |
| 312 const int kTargetBitrateBps = 100000; |
| 313 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); |
| 314 |
| 315 // Capture a frame and wait for it to synchronize with the encoder thread. |
| 316 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
| 317 sink_.WaitForEncodedFrame(1); |
| 318 // The encoder will have been configured twice. First time before the first |
| 319 // frame has been received. Then a second time when the resolution is known. |
| 320 EXPECT_EQ(2, sink_.number_of_reconfigurations()); |
| 321 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); |
| 322 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); |
| 323 |
| 324 codec_width_ *= 2; |
| 325 codec_height_ *= 2; |
| 326 // Capture a frame with a higher resolution and wait for it to synchronize |
| 327 // with the encoder thread. |
| 328 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
| 329 sink_.WaitForEncodedFrame(2); |
| 330 EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); |
| 331 EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); |
| 332 EXPECT_EQ(3, sink_.number_of_reconfigurations()); |
| 333 |
| 334 vie_encoder_->Stop(); |
| 335 } |
| 336 |
| 303 } // namespace webrtc | 337 } // namespace webrtc |
| OLD | NEW |