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