1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10 #include <functional>
11 #include <list>
12 #include <memory>
13 #include <string>
14
15 #include "absl/strings/string_view.h"
16 #include "api/test/create_frame_generator.h"
17 #include "call/call.h"
18 #include "call/fake_network_pipe.h"
19 #include "call/simulated_network.h"
20 #include "rtc_base/checks.h"
21 #include "rtc_base/event.h"
22 #include "rtc_base/logging.h"
23 #include "rtc_base/synchronization/mutex.h"
24 #include "rtc_base/task_queue_for_test.h"
25 #include "rtc_base/thread_annotations.h"
26 #include "test/call_test.h"
27 #include "test/direct_transport.h"
28 #include "test/encoder_settings.h"
29 #include "test/fake_decoder.h"
30 #include "test/fake_encoder.h"
31 #include "test/frame_generator_capturer.h"
32 #include "test/gtest.h"
33
34 namespace webrtc {
35 namespace {
36 // Note: If you consider to re-use this class, think twice and instead consider
37 // writing tests that don't depend on the logging system.
38 class LogObserver {
39 public:
LogObserver()40 LogObserver() { rtc::LogMessage::AddLogToStream(&callback_, rtc::LS_INFO); }
41
~LogObserver()42 ~LogObserver() { rtc::LogMessage::RemoveLogToStream(&callback_); }
43
PushExpectedLogLine(absl::string_view expected_log_line)44 void PushExpectedLogLine(absl::string_view expected_log_line) {
45 callback_.PushExpectedLogLine(expected_log_line);
46 }
47
Wait()48 bool Wait() { return callback_.Wait(); }
49
50 private:
51 class Callback : public rtc::LogSink {
52 public:
OnLogMessage(const std::string & message)53 void OnLogMessage(const std::string& message) override {
54 OnLogMessage(absl::string_view(message));
55 }
56
OnLogMessage(absl::string_view message)57 void OnLogMessage(absl::string_view message) override {
58 MutexLock lock(&mutex_);
59 // Ignore log lines that are due to missing AST extensions, these are
60 // logged when we switch back from AST to TOF until the wrapping bitrate
61 // estimator gives up on using AST.
62 if (message.find("BitrateEstimator") != absl::string_view::npos &&
63 message.find("packet is missing") == absl::string_view::npos) {
64 received_log_lines_.push_back(std::string(message));
65 }
66
67 int num_popped = 0;
68 while (!received_log_lines_.empty() && !expected_log_lines_.empty()) {
69 std::string a = received_log_lines_.front();
70 std::string b = expected_log_lines_.front();
71 received_log_lines_.pop_front();
72 expected_log_lines_.pop_front();
73 num_popped++;
74 EXPECT_TRUE(a.find(b) != absl::string_view::npos) << a << " != " << b;
75 }
76 if (expected_log_lines_.empty()) {
77 if (num_popped > 0) {
78 done_.Set();
79 }
80 return;
81 }
82 }
83
Wait()84 bool Wait() { return done_.Wait(test::CallTest::kDefaultTimeout); }
85
PushExpectedLogLine(absl::string_view expected_log_line)86 void PushExpectedLogLine(absl::string_view expected_log_line) {
87 MutexLock lock(&mutex_);
88 expected_log_lines_.emplace_back(expected_log_line);
89 }
90
91 private:
92 typedef std::list<std::string> Strings;
93 Mutex mutex_;
94 Strings received_log_lines_ RTC_GUARDED_BY(mutex_);
95 Strings expected_log_lines_ RTC_GUARDED_BY(mutex_);
96 rtc::Event done_;
97 };
98
99 Callback callback_;
100 };
101 } // namespace
102
103 static const int kTOFExtensionId = 4;
104 static const int kASTExtensionId = 5;
105
106 class BitrateEstimatorTest : public test::CallTest {
107 public:
BitrateEstimatorTest()108 BitrateEstimatorTest() : receive_config_(nullptr) {}
109
~BitrateEstimatorTest()110 virtual ~BitrateEstimatorTest() { EXPECT_TRUE(streams_.empty()); }
111
SetUp()112 virtual void SetUp() {
113 SendTask(task_queue(), [this]() {
114 CreateCalls();
115
116 send_transport_.reset(new test::DirectTransport(
117 task_queue(),
118 std::make_unique<FakeNetworkPipe>(
119 Clock::GetRealTimeClock(), std::make_unique<SimulatedNetwork>(
120 BuiltInNetworkBehaviorConfig())),
121 sender_call_.get(), payload_type_map_));
122 send_transport_->SetReceiver(receiver_call_->Receiver());
123 receive_transport_.reset(new test::DirectTransport(
124 task_queue(),
125 std::make_unique<FakeNetworkPipe>(
126 Clock::GetRealTimeClock(), std::make_unique<SimulatedNetwork>(
127 BuiltInNetworkBehaviorConfig())),
128 receiver_call_.get(), payload_type_map_));
129 receive_transport_->SetReceiver(sender_call_->Receiver());
130
131 VideoSendStream::Config video_send_config(send_transport_.get());
132 video_send_config.rtp.ssrcs.push_back(kVideoSendSsrcs[0]);
133 video_send_config.encoder_settings.encoder_factory =
134 &fake_encoder_factory_;
135 video_send_config.encoder_settings.bitrate_allocator_factory =
136 bitrate_allocator_factory_.get();
137 video_send_config.rtp.payload_name = "FAKE";
138 video_send_config.rtp.payload_type = kFakeVideoSendPayloadType;
139 SetVideoSendConfig(video_send_config);
140 VideoEncoderConfig video_encoder_config;
141 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config);
142 SetVideoEncoderConfig(video_encoder_config);
143
144 receive_config_ =
145 VideoReceiveStreamInterface::Config(receive_transport_.get());
146 // receive_config_.decoders will be set by every stream separately.
147 receive_config_.rtp.remote_ssrc = GetVideoSendConfig()->rtp.ssrcs[0];
148 receive_config_.rtp.local_ssrc = kReceiverLocalVideoSsrc;
149 receive_config_.rtp.extensions.push_back(
150 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
151 receive_config_.rtp.extensions.push_back(
152 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId));
153 });
154 }
155
TearDown()156 virtual void TearDown() {
157 SendTask(task_queue(), [this]() {
158 for (auto* stream : streams_) {
159 stream->StopSending();
160 delete stream;
161 }
162 streams_.clear();
163
164 send_transport_.reset();
165 receive_transport_.reset();
166
167 DestroyCalls();
168 });
169 }
170
171 protected:
172 friend class Stream;
173
174 class Stream {
175 public:
Stream(BitrateEstimatorTest * test)176 explicit Stream(BitrateEstimatorTest* test)
177 : test_(test),
178 is_sending_receiving_(false),
179 send_stream_(nullptr),
180 frame_generator_capturer_(),
181 decoder_factory_(
182 []() { return std::make_unique<test::FakeDecoder>(); }) {
183 test_->GetVideoSendConfig()->rtp.ssrcs[0]++;
184 send_stream_ = test_->sender_call_->CreateVideoSendStream(
185 test_->GetVideoSendConfig()->Copy(),
186 test_->GetVideoEncoderConfig()->Copy());
187 RTC_DCHECK_EQ(1, test_->GetVideoEncoderConfig()->number_of_streams);
188 frame_generator_capturer_ =
189 std::make_unique<test::FrameGeneratorCapturer>(
190 test->clock_,
191 test::CreateSquareFrameGenerator(kDefaultWidth, kDefaultHeight,
192 absl::nullopt, absl::nullopt),
193 kDefaultFramerate, *test->task_queue_factory_);
194 frame_generator_capturer_->Init();
195 send_stream_->SetSource(frame_generator_capturer_.get(),
196 DegradationPreference::MAINTAIN_FRAMERATE);
197 send_stream_->Start();
198
199 VideoReceiveStreamInterface::Decoder decoder;
200 test_->receive_config_.decoder_factory = &decoder_factory_;
201 decoder.payload_type = test_->GetVideoSendConfig()->rtp.payload_type;
202 decoder.video_format =
203 SdpVideoFormat(test_->GetVideoSendConfig()->rtp.payload_name);
204 test_->receive_config_.decoders.clear();
205 test_->receive_config_.decoders.push_back(decoder);
206 test_->receive_config_.rtp.remote_ssrc =
207 test_->GetVideoSendConfig()->rtp.ssrcs[0];
208 test_->receive_config_.rtp.local_ssrc++;
209 test_->receive_config_.renderer = &test->fake_renderer_;
210 video_receive_stream_ = test_->receiver_call_->CreateVideoReceiveStream(
211 test_->receive_config_.Copy());
212 video_receive_stream_->Start();
213 is_sending_receiving_ = true;
214 }
215
~Stream()216 ~Stream() {
217 EXPECT_FALSE(is_sending_receiving_);
218 test_->sender_call_->DestroyVideoSendStream(send_stream_);
219 frame_generator_capturer_.reset(nullptr);
220 send_stream_ = nullptr;
221 if (video_receive_stream_) {
222 test_->receiver_call_->DestroyVideoReceiveStream(video_receive_stream_);
223 video_receive_stream_ = nullptr;
224 }
225 }
226
StopSending()227 void StopSending() {
228 if (is_sending_receiving_) {
229 send_stream_->Stop();
230 if (video_receive_stream_) {
231 video_receive_stream_->Stop();
232 }
233 is_sending_receiving_ = false;
234 }
235 }
236
237 private:
238 BitrateEstimatorTest* test_;
239 bool is_sending_receiving_;
240 VideoSendStream* send_stream_;
241 VideoReceiveStreamInterface* video_receive_stream_;
242 std::unique_ptr<test::FrameGeneratorCapturer> frame_generator_capturer_;
243
244 test::FunctionVideoDecoderFactory decoder_factory_;
245 };
246
247 LogObserver receiver_log_;
248 std::unique_ptr<test::DirectTransport> send_transport_;
249 std::unique_ptr<test::DirectTransport> receive_transport_;
250 VideoReceiveStreamInterface::Config receive_config_;
251 std::vector<Stream*> streams_;
252 };
253
254 static const char* kAbsSendTimeLog =
255 "RemoteBitrateEstimatorAbsSendTime: Instantiating.";
256 static const char* kSingleStreamLog =
257 "RemoteBitrateEstimatorSingleStream: Instantiating.";
258
TEST_F(BitrateEstimatorTest,InstantiatesTOFPerDefaultForVideo)259 TEST_F(BitrateEstimatorTest, InstantiatesTOFPerDefaultForVideo) {
260 SendTask(task_queue(), [this]() {
261 GetVideoSendConfig()->rtp.extensions.push_back(
262 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
263 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
264 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
265 streams_.push_back(new Stream(this));
266 });
267 EXPECT_TRUE(receiver_log_.Wait());
268 }
269
TEST_F(BitrateEstimatorTest,ImmediatelySwitchToASTForVideo)270 TEST_F(BitrateEstimatorTest, ImmediatelySwitchToASTForVideo) {
271 SendTask(task_queue(), [this]() {
272 GetVideoSendConfig()->rtp.extensions.push_back(
273 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId));
274 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
275 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
276 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
277 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
278 streams_.push_back(new Stream(this));
279 });
280 EXPECT_TRUE(receiver_log_.Wait());
281 }
282
TEST_F(BitrateEstimatorTest,SwitchesToASTForVideo)283 TEST_F(BitrateEstimatorTest, SwitchesToASTForVideo) {
284 SendTask(task_queue(), [this]() {
285 GetVideoSendConfig()->rtp.extensions.push_back(
286 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
287 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
288 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
289 streams_.push_back(new Stream(this));
290 });
291 EXPECT_TRUE(receiver_log_.Wait());
292
293 SendTask(task_queue(), [this]() {
294 GetVideoSendConfig()->rtp.extensions[0] =
295 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId);
296 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
297 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
298 streams_.push_back(new Stream(this));
299 });
300 EXPECT_TRUE(receiver_log_.Wait());
301 }
302
303 // This test is flaky. See webrtc:5790.
TEST_F(BitrateEstimatorTest,DISABLED_SwitchesToASTThenBackToTOFForVideo)304 TEST_F(BitrateEstimatorTest, DISABLED_SwitchesToASTThenBackToTOFForVideo) {
305 SendTask(task_queue(), [this]() {
306 GetVideoSendConfig()->rtp.extensions.push_back(
307 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
308 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
309 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
310 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
311 streams_.push_back(new Stream(this));
312 });
313 EXPECT_TRUE(receiver_log_.Wait());
314
315 SendTask(task_queue(), [this]() {
316 GetVideoSendConfig()->rtp.extensions[0] =
317 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId);
318 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
319 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
320 streams_.push_back(new Stream(this));
321 });
322 EXPECT_TRUE(receiver_log_.Wait());
323
324 SendTask(task_queue(), [this]() {
325 GetVideoSendConfig()->rtp.extensions[0] =
326 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId);
327 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
328 receiver_log_.PushExpectedLogLine(
329 "WrappingBitrateEstimator: Switching to transmission time offset RBE.");
330 streams_.push_back(new Stream(this));
331 streams_[0]->StopSending();
332 streams_[1]->StopSending();
333 });
334 EXPECT_TRUE(receiver_log_.Wait());
335 }
336 } // namespace webrtc
337