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

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

Issue 2338133003: Let ViEEncoder tell VideoSendStream about reconfigurations. (Closed)
Patch Set: Rebased Created 4 years, 3 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
« no previous file with comments | « webrtc/video/vie_encoder.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <utility>
12
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webrtc/base/logging.h" 14 #include "webrtc/base/logging.h"
13 #include "webrtc/test/encoder_settings.h" 15 #include "webrtc/test/encoder_settings.h"
14 #include "webrtc/test/fake_encoder.h" 16 #include "webrtc/test/fake_encoder.h"
15 #include "webrtc/test/frame_generator.h" 17 #include "webrtc/test/frame_generator.h"
16 #include "webrtc/video/send_statistics_proxy.h" 18 #include "webrtc/video/send_statistics_proxy.h"
17 #include "webrtc/video/vie_encoder.h" 19 #include "webrtc/video/vie_encoder.h"
18 20
19 namespace webrtc { 21 namespace webrtc {
20 22
21 class ViEEncoderTest : public ::testing::Test { 23 class ViEEncoderTest : public ::testing::Test {
22 public: 24 public:
23 static const int kDefaultTimeoutMs = 30 * 1000; 25 static const int kDefaultTimeoutMs = 30 * 1000;
24 26
25 ViEEncoderTest() 27 ViEEncoderTest()
26 : video_send_config_(VideoSendStream::Config(nullptr)), 28 : video_send_config_(VideoSendStream::Config(nullptr)),
27 fake_encoder_(), 29 fake_encoder_(),
28 stats_proxy_(Clock::GetRealTimeClock(), 30 stats_proxy_(Clock::GetRealTimeClock(),
29 video_send_config_, 31 video_send_config_,
30 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo), 32 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo),
31 sink_(&fake_encoder_) {} 33 sink_(&fake_encoder_) {}
32 34
33 void SetUp() override { 35 void SetUp() override {
34 video_send_config_ = VideoSendStream::Config(nullptr); 36 video_send_config_ = VideoSendStream::Config(nullptr);
35 video_send_config_.encoder_settings.encoder = &fake_encoder_; 37 video_send_config_.encoder_settings.encoder = &fake_encoder_;
36 video_send_config_.encoder_settings.payload_name = "FAKE"; 38 video_send_config_.encoder_settings.payload_name = "FAKE";
37 video_send_config_.encoder_settings.payload_type = 125; 39 video_send_config_.encoder_settings.payload_type = 125;
38 40
39 video_encoder_config_.streams = test::CreateVideoStreams(1); 41 VideoEncoderConfig 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);
40 45
41 vie_encoder_.reset(new ViEEncoder( 46 vie_encoder_.reset(new ViEEncoder(
42 1 /* number_of_cores */, &stats_proxy_, 47 1 /* number_of_cores */, &stats_proxy_,
43 video_send_config_.encoder_settings, nullptr /* pre_encode_callback */, 48 video_send_config_.encoder_settings, nullptr /* pre_encode_callback */,
44 nullptr /* overuse_callback */, nullptr /* encoder_timing */)); 49 nullptr /* overuse_callback */, nullptr /* encoder_timing */));
45 vie_encoder_->SetSink(&sink_); 50 vie_encoder_->SetSink(&sink_);
46 vie_encoder_->SetSource(&video_source_); 51 vie_encoder_->SetSource(&video_source_);
47 vie_encoder_->SetStartBitrate(10000); 52 vie_encoder_->SetStartBitrate(10000);
48 vie_encoder_->ConfigureEncoder(video_encoder_config_, 1440); 53 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 1440);
49 } 54 }
50 55
51 VideoFrame CreateFrame(int64_t ntp_ts, rtc::Event* destruction_event) const { 56 VideoFrame CreateFrame(int64_t ntp_ts, rtc::Event* destruction_event) const {
52 class TestBuffer : public webrtc::I420Buffer { 57 class TestBuffer : public webrtc::I420Buffer {
53 public: 58 public:
54 TestBuffer(rtc::Event* event, int width, int height) 59 TestBuffer(rtc::Event* event, int width, int height)
55 : I420Buffer(width, height), event_(event) {} 60 : I420Buffer(width, height), event_(event) {}
56 61
57 private: 62 private:
58 friend class rtc::RefCountedObject<TestBuffer>; 63 friend class rtc::RefCountedObject<TestBuffer>;
59 ~TestBuffer() override { 64 ~TestBuffer() override {
60 if (event_) 65 if (event_)
61 event_->Set(); 66 event_->Set();
62 } 67 }
63 rtc::Event* const event_; 68 rtc::Event* const event_;
64 }; 69 };
65 70
66 VideoFrame frame( 71 VideoFrame frame(new rtc::RefCountedObject<TestBuffer>(
67 new rtc::RefCountedObject<TestBuffer>( 72 destruction_event, codec_width_, codec_height_),
68 destruction_event, 73 99, 99, kVideoRotation_0);
69 static_cast<int>(video_encoder_config_.streams[0].width),
70 static_cast<int>(video_encoder_config_.streams[0].height)),
71 99, 99, kVideoRotation_0);
72 frame.set_ntp_time_ms(ntp_ts); 74 frame.set_ntp_time_ms(ntp_ts);
73 return frame; 75 return frame;
74 } 76 }
75 77
76 class TestEncoder : public test::FakeEncoder { 78 class TestEncoder : public test::FakeEncoder {
77 public: 79 public:
78 TestEncoder() 80 TestEncoder()
79 : FakeEncoder(Clock::GetRealTimeClock()), 81 : FakeEncoder(Clock::GetRealTimeClock()),
80 continue_encode_event_(false, false) {} 82 continue_encode_event_(false, false) {}
81 83
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 } 118 }
117 119
118 private: 120 private:
119 rtc::CriticalSection crit_; 121 rtc::CriticalSection crit_;
120 bool block_next_encode_ = false; 122 bool block_next_encode_ = false;
121 rtc::Event continue_encode_event_; 123 rtc::Event continue_encode_event_;
122 uint32_t timestamp_ = 0; 124 uint32_t timestamp_ = 0;
123 int64_t ntp_time_ms_ = 0; 125 int64_t ntp_time_ms_ = 0;
124 }; 126 };
125 127
126 class TestSink : public EncodedImageCallback { 128 class TestSink : public ViEEncoder::EncoderSink {
127 public: 129 public:
128 explicit TestSink(TestEncoder* test_encoder) 130 explicit TestSink(TestEncoder* test_encoder)
129 : test_encoder_(test_encoder), encoded_frame_event_(false, false) {} 131 : test_encoder_(test_encoder), encoded_frame_event_(false, false) {}
130 132
131 int32_t Encoded(const EncodedImage& encoded_image,
132 const CodecSpecificInfo* codec_specific_info,
133 const RTPFragmentationHeader* fragmentation) override {
134 rtc::CritScope lock(&crit_);
135 EXPECT_TRUE(expect_frames_);
136 timestamp_ = encoded_image._timeStamp;
137 encoded_frame_event_.Set();
138 return 0;
139 }
140
141 void WaitForEncodedFrame(int64_t expected_ntp_time) { 133 void WaitForEncodedFrame(int64_t expected_ntp_time) {
142 uint32_t timestamp = 0; 134 uint32_t timestamp = 0;
143 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); 135 EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs));
144 { 136 {
145 rtc::CritScope lock(&crit_); 137 rtc::CritScope lock(&crit_);
146 timestamp = timestamp_; 138 timestamp = timestamp_;
147 } 139 }
148 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); 140 test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp);
149 } 141 }
150 142
151 void SetExpectNoFrames() { 143 void SetExpectNoFrames() {
152 rtc::CritScope lock(&crit_); 144 rtc::CritScope lock(&crit_);
153 expect_frames_ = false; 145 expect_frames_ = false;
154 } 146 }
155 147
148 int number_of_reconfigurations() {
149 rtc::CritScope lock(&crit_);
150 return number_of_reconfigurations_;
151 }
152
153 int last_min_transmit_bitrate() {
154 rtc::CritScope lock(&crit_);
155 return min_transmit_bitrate_bps_;
156 }
157
156 private: 158 private:
159 int32_t Encoded(const EncodedImage& encoded_image,
160 const CodecSpecificInfo* codec_specific_info,
161 const RTPFragmentationHeader* fragmentation) override {
162 rtc::CritScope lock(&crit_);
163 EXPECT_TRUE(expect_frames_);
164 timestamp_ = encoded_image._timeStamp;
165 encoded_frame_event_.Set();
166 return 0;
167 }
168
169 void OnEncoderConfigurationChanged(std::vector<VideoStream> streams,
170 int min_transmit_bitrate_bps) override {
171 rtc::CriticalSection crit_;
172 ++number_of_reconfigurations_;
173 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
174 }
175
157 rtc::CriticalSection crit_; 176 rtc::CriticalSection crit_;
158 TestEncoder* test_encoder_; 177 TestEncoder* test_encoder_;
159 rtc::Event encoded_frame_event_; 178 rtc::Event encoded_frame_event_;
160 uint32_t timestamp_ = 0; 179 uint32_t timestamp_ = 0;
161 bool expect_frames_ = true; 180 bool expect_frames_ = true;
181 int number_of_reconfigurations_ = 0;
182 int min_transmit_bitrate_bps_ = 0;
162 }; 183 };
163 184
164 VideoSendStream::Config video_send_config_; 185 VideoSendStream::Config video_send_config_;
165 VideoEncoderConfig video_encoder_config_; 186 int codec_width_;
187 int codec_height_;
166 TestEncoder fake_encoder_; 188 TestEncoder fake_encoder_;
167 SendStatisticsProxy stats_proxy_; 189 SendStatisticsProxy stats_proxy_;
168 TestSink sink_; 190 TestSink sink_;
169 test::FrameForwarder video_source_; 191 test::FrameForwarder video_source_;
170 std::unique_ptr<ViEEncoder> vie_encoder_; 192 std::unique_ptr<ViEEncoder> vie_encoder_;
171 }; 193 };
172 194
173 TEST_F(ViEEncoderTest, EncodeOneFrame) { 195 TEST_F(ViEEncoderTest, EncodeOneFrame) {
174 const int kTargetBitrateBps = 100000; 196 const int kTargetBitrateBps = 100000;
175 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0); 197 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 // Here, the encoder thread will be blocked in the TestEncoder waiting for a 270 // Here, the encoder thread will be blocked in the TestEncoder waiting for a
249 // call to ContinueEncode. 271 // call to ContinueEncode.
250 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); 272 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
251 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); 273 video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr));
252 fake_encoder_.ContinueEncode(); 274 fake_encoder_.ContinueEncode();
253 sink_.WaitForEncodedFrame(3); 275 sink_.WaitForEncodedFrame(3);
254 276
255 vie_encoder_->Stop(); 277 vie_encoder_->Stop();
256 } 278 }
257 279
280 TEST_F(ViEEncoderTest, ConfigureEncoderTriggersOnEncoderConfigurationChanged) {
281 const int kTargetBitrateBps = 100000;
282 vie_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
283
284 // Capture a frame and wait for it to synchronize with the encoder thread.
285 video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr));
286 sink_.WaitForEncodedFrame(1);
287 EXPECT_EQ(1, sink_.number_of_reconfigurations());
288
289 VideoEncoderConfig video_encoder_config;
290 video_encoder_config.streams = test::CreateVideoStreams(1);
291 video_encoder_config.min_transmit_bitrate_bps = 9999;
292 vie_encoder_->ConfigureEncoder(std::move(video_encoder_config), 1440);
293
294 // Capture a frame and wait for it to synchronize with the encoder thread.
295 video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr));
296 sink_.WaitForEncodedFrame(2);
297 EXPECT_EQ(2, sink_.number_of_reconfigurations());
298 EXPECT_EQ(9999, sink_.last_min_transmit_bitrate());
299
300 vie_encoder_->Stop();
301 }
302
258 } // namespace webrtc 303 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/video/vie_encoder.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698