blob: 6b69bc069b18cacf54fabf5a05629c77c1382d1a [file] [log] [blame]
pbos@webrtc.org5ab75672013-12-16 12:24:44 +00001/*
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>
kwibergb25345e2016-03-12 06:10:44 -080012#include <memory>
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000013#include <string>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "call/call.h"
16#include "rtc_base/checks.h"
17#include "rtc_base/event.h"
18#include "rtc_base/logging.h"
19#include "rtc_base/thread_annotations.h"
20#include "test/call_test.h"
21#include "test/direct_transport.h"
22#include "test/encoder_settings.h"
23#include "test/fake_decoder.h"
24#include "test/fake_encoder.h"
25#include "test/frame_generator_capturer.h"
26#include "test/gtest.h"
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000027
28namespace webrtc {
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000029namespace {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020030// Note: If you consider to re-use this class, think twice and instead consider
Peter Boström7c704b82015-12-04 16:13:05 +010031// writing tests that don't depend on the logging system.
32class LogObserver {
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000033 public:
Peter Boström7c704b82015-12-04 16:13:05 +010034 LogObserver() { rtc::LogMessage::AddLogToStream(&callback_, rtc::LS_INFO); }
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000035
Peter Boström7c704b82015-12-04 16:13:05 +010036 ~LogObserver() { rtc::LogMessage::RemoveLogToStream(&callback_); }
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000037
38 void PushExpectedLogLine(const std::string& expected_log_line) {
39 callback_.PushExpectedLogLine(expected_log_line);
40 }
41
Peter Boström5811a392015-12-10 13:02:50 +010042 bool Wait() { return callback_.Wait(); }
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000043
44 private:
Peter Boström7c704b82015-12-04 16:13:05 +010045 class Callback : public rtc::LogSink {
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000046 public:
Peter Boström5811a392015-12-10 13:02:50 +010047 Callback() : done_(false, false) {}
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000048
Peter Boström7c704b82015-12-04 16:13:05 +010049 void OnLogMessage(const std::string& message) override {
Peter Boströmf2f82832015-05-01 13:00:41 +020050 rtc::CritScope lock(&crit_sect_);
Peter Boström7c704b82015-12-04 16:13:05 +010051 // Ignore log lines that are due to missing AST extensions, these are
52 // logged when we switch back from AST to TOF until the wrapping bitrate
53 // estimator gives up on using AST.
54 if (message.find("BitrateEstimator") != std::string::npos &&
55 message.find("packet is missing") == std::string::npos) {
56 received_log_lines_.push_back(message);
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000057 }
Peter Boström7c704b82015-12-04 16:13:05 +010058
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000059 int num_popped = 0;
60 while (!received_log_lines_.empty() && !expected_log_lines_.empty()) {
61 std::string a = received_log_lines_.front();
62 std::string b = expected_log_lines_.front();
63 received_log_lines_.pop_front();
64 expected_log_lines_.pop_front();
65 num_popped++;
Peter Boström7c704b82015-12-04 16:13:05 +010066 EXPECT_TRUE(a.find(b) != std::string::npos) << a << " != " << b;
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000067 }
68 if (expected_log_lines_.size() <= 0) {
69 if (num_popped > 0) {
Peter Boström5811a392015-12-10 13:02:50 +010070 done_.Set();
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000071 }
72 return;
73 }
74 }
75
Peter Boström5811a392015-12-10 13:02:50 +010076 bool Wait() { return done_.Wait(test::CallTest::kDefaultTimeoutMs); }
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000077
78 void PushExpectedLogLine(const std::string& expected_log_line) {
Peter Boströmf2f82832015-05-01 13:00:41 +020079 rtc::CritScope lock(&crit_sect_);
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000080 expected_log_lines_.push_back(expected_log_line);
81 }
82
83 private:
84 typedef std::list<std::string> Strings;
Peter Boströmf2f82832015-05-01 13:00:41 +020085 rtc::CriticalSection crit_sect_;
danilchapa37de392017-09-09 04:17:22 -070086 Strings received_log_lines_ RTC_GUARDED_BY(crit_sect_);
87 Strings expected_log_lines_ RTC_GUARDED_BY(crit_sect_);
Peter Boström5811a392015-12-10 13:02:50 +010088 rtc::Event done_;
andresp@webrtc.orgb941fe82014-07-07 08:50:48 +000089 };
90
91 Callback callback_;
92};
93} // namespace
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000094
95static const int kTOFExtensionId = 4;
96static const int kASTExtensionId = 5;
97
pbos@webrtc.org994d0b72014-06-27 08:47:52 +000098class BitrateEstimatorTest : public test::CallTest {
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000099 public:
aleloi5d78e8d2016-11-08 03:44:54 -0800100 BitrateEstimatorTest() : receive_config_(nullptr) {}
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000101
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100102 virtual ~BitrateEstimatorTest() { EXPECT_TRUE(streams_.empty()); }
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000103
104 virtual void SetUp() {
eladalon413ee9a2017-08-22 04:02:52 -0700105 task_queue_.SendTask([this]() {
106 Call::Config config(event_log_.get());
107 receiver_call_.reset(Call::Create(config));
108 sender_call_.reset(Call::Create(config));
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000109
eladalon413ee9a2017-08-22 04:02:52 -0700110 send_transport_.reset(new test::DirectTransport(
111 &task_queue_, sender_call_.get(), payload_type_map_));
112 send_transport_->SetReceiver(receiver_call_->Receiver());
113 receive_transport_.reset(new test::DirectTransport(
114 &task_queue_, receiver_call_.get(), payload_type_map_));
115 receive_transport_->SetReceiver(sender_call_->Receiver());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000116
eladalon413ee9a2017-08-22 04:02:52 -0700117 video_send_config_ = VideoSendStream::Config(send_transport_.get());
118 video_send_config_.rtp.ssrcs.push_back(kVideoSendSsrcs[0]);
Niels Möller4db138e2018-04-19 09:04:13 +0200119 video_send_config_.encoder_settings.encoder_factory =
120 &fake_encoder_factory_;
Niels Möller259a4972018-04-05 15:36:51 +0200121 video_send_config_.rtp.payload_name = "FAKE";
122 video_send_config_.rtp.payload_type = kFakeVideoSendPayloadType;
123 test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config_);
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000124
eladalon413ee9a2017-08-22 04:02:52 -0700125 receive_config_ = VideoReceiveStream::Config(receive_transport_.get());
126 // receive_config_.decoders will be set by every stream separately.
127 receive_config_.rtp.remote_ssrc = video_send_config_.rtp.ssrcs[0];
128 receive_config_.rtp.local_ssrc = kReceiverLocalVideoSsrc;
129 receive_config_.rtp.remb = true;
130 receive_config_.rtp.extensions.push_back(
131 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
132 receive_config_.rtp.extensions.push_back(
133 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId));
134 });
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000135 }
136
137 virtual void TearDown() {
eladalon413ee9a2017-08-22 04:02:52 -0700138 task_queue_.SendTask([this]() {
Yusuke Suzukif8d8d6d2018-05-17 22:01:13 +0900139 for (auto* stream : streams_) {
140 stream->StopSending();
141 delete stream;
eladalon413ee9a2017-08-22 04:02:52 -0700142 }
Yusuke Suzukif8d8d6d2018-05-17 22:01:13 +0900143 streams_.clear();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000144
eladalon413ee9a2017-08-22 04:02:52 -0700145 send_transport_.reset();
146 receive_transport_.reset();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000147
eladalon413ee9a2017-08-22 04:02:52 -0700148 receiver_call_.reset();
149 sender_call_.reset();
150 });
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000151 }
152
153 protected:
154 friend class Stream;
155
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000156 class Stream {
157 public:
aleloi5d78e8d2016-11-08 03:44:54 -0800158 explicit Stream(BitrateEstimatorTest* test)
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000159 : test_(test),
160 is_sending_receiving_(false),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000161 send_stream_(nullptr),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000162 frame_generator_capturer_(),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000163 fake_decoder_() {
stefanff483612015-12-21 03:14:00 -0800164 test_->video_send_config_.rtp.ssrcs[0]++;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000165 send_stream_ = test_->sender_call_->CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700166 test_->video_send_config_.Copy(),
167 test_->video_encoder_config_.Copy());
kwibergaf476c72016-11-28 15:21:39 -0800168 RTC_DCHECK_EQ(1, test_->video_encoder_config_.number_of_streams);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000169 frame_generator_capturer_.reset(test::FrameGeneratorCapturer::Create(
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800170 kDefaultWidth, kDefaultHeight, rtc::nullopt, rtc::nullopt,
171 kDefaultFramerate, Clock::GetRealTimeClock()));
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700172 send_stream_->SetSource(frame_generator_capturer_.get(),
173 DegradationPreference::MAINTAIN_FRAMERATE);
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000174 send_stream_->Start();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000175 frame_generator_capturer_->Start();
176
aleloi5d78e8d2016-11-08 03:44:54 -0800177 VideoReceiveStream::Decoder decoder;
178 decoder.decoder = &fake_decoder_;
Niels Möller259a4972018-04-05 15:36:51 +0200179 decoder.payload_type = test_->video_send_config_.rtp.payload_type;
180 decoder.payload_name = test_->video_send_config_.rtp.payload_name;
aleloi5d78e8d2016-11-08 03:44:54 -0800181 test_->receive_config_.decoders.clear();
182 test_->receive_config_.decoders.push_back(decoder);
183 test_->receive_config_.rtp.remote_ssrc =
184 test_->video_send_config_.rtp.ssrcs[0];
185 test_->receive_config_.rtp.local_ssrc++;
186 test_->receive_config_.renderer = &test->fake_renderer_;
187 video_receive_stream_ = test_->receiver_call_->CreateVideoReceiveStream(
188 test_->receive_config_.Copy());
189 video_receive_stream_->Start();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000190 is_sending_receiving_ = true;
191 }
192
193 ~Stream() {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200194 EXPECT_FALSE(is_sending_receiving_);
perkj9fdbda62016-09-15 09:19:20 -0700195 test_->sender_call_->DestroyVideoSendStream(send_stream_);
perkja49cbd32016-09-16 07:53:41 -0700196 frame_generator_capturer_.reset(nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000197 send_stream_ = nullptr;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200198 if (video_receive_stream_) {
199 test_->receiver_call_->DestroyVideoReceiveStream(video_receive_stream_);
200 video_receive_stream_ = nullptr;
201 }
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000202 }
203
204 void StopSending() {
205 if (is_sending_receiving_) {
206 frame_generator_capturer_->Stop();
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000207 send_stream_->Stop();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200208 if (video_receive_stream_) {
209 video_receive_stream_->Stop();
210 }
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000211 is_sending_receiving_ = false;
212 }
213 }
214
215 private:
216 BitrateEstimatorTest* test_;
217 bool is_sending_receiving_;
218 VideoSendStream* send_stream_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200219 VideoReceiveStream* video_receive_stream_;
kwibergb25345e2016-03-12 06:10:44 -0800220 std::unique_ptr<test::FrameGeneratorCapturer> frame_generator_capturer_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000221 test::FakeDecoder fake_decoder_;
222 };
223
Peter Boström7c704b82015-12-04 16:13:05 +0100224 LogObserver receiver_log_;
kwibergb25345e2016-03-12 06:10:44 -0800225 std::unique_ptr<test::DirectTransport> send_transport_;
226 std::unique_ptr<test::DirectTransport> receive_transport_;
227 std::unique_ptr<Call> sender_call_;
228 std::unique_ptr<Call> receiver_call_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000229 VideoReceiveStream::Config receive_config_;
230 std::vector<Stream*> streams_;
231};
232
Erik Språng468e62a2015-07-06 10:50:47 +0200233static const char* kAbsSendTimeLog =
234 "RemoteBitrateEstimatorAbsSendTime: Instantiating.";
235static const char* kSingleStreamLog =
236 "RemoteBitrateEstimatorSingleStream: Instantiating.";
237
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200238TEST_F(BitrateEstimatorTest, InstantiatesTOFPerDefaultForVideo) {
eladalon413ee9a2017-08-22 04:02:52 -0700239 task_queue_.SendTask([this]() {
240 video_send_config_.rtp.extensions.push_back(
241 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
242 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
243 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
244 streams_.push_back(new Stream(this));
245 });
Peter Boström5811a392015-12-10 13:02:50 +0100246 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000247}
248
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200249TEST_F(BitrateEstimatorTest, ImmediatelySwitchToASTForVideo) {
eladalon413ee9a2017-08-22 04:02:52 -0700250 task_queue_.SendTask([this]() {
251 video_send_config_.rtp.extensions.push_back(
252 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId));
253 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
254 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
255 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
256 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
257 streams_.push_back(new Stream(this));
258 });
Peter Boström5811a392015-12-10 13:02:50 +0100259 EXPECT_TRUE(receiver_log_.Wait());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200260}
261
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200262TEST_F(BitrateEstimatorTest, SwitchesToASTForVideo) {
eladalon413ee9a2017-08-22 04:02:52 -0700263 task_queue_.SendTask([this]() {
264 video_send_config_.rtp.extensions.push_back(
265 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
266 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
267 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
268 streams_.push_back(new Stream(this));
269 });
Peter Boström5811a392015-12-10 13:02:50 +0100270 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000271
eladalon413ee9a2017-08-22 04:02:52 -0700272 task_queue_.SendTask([this]() {
273 video_send_config_.rtp.extensions[0] =
274 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId);
275 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
276 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
277 streams_.push_back(new Stream(this));
278 });
Peter Boström5811a392015-12-10 13:02:50 +0100279 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000280}
281
deadbeef1086ed62016-04-22 10:52:40 -0700282// This test is flaky. See webrtc:5790.
283TEST_F(BitrateEstimatorTest, DISABLED_SwitchesToASTThenBackToTOFForVideo) {
eladalon413ee9a2017-08-22 04:02:52 -0700284 task_queue_.SendTask([this]() {
285 video_send_config_.rtp.extensions.push_back(
286 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
287 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
288 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
289 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
290 streams_.push_back(new Stream(this));
291 });
Peter Boström5811a392015-12-10 13:02:50 +0100292 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000293
eladalon413ee9a2017-08-22 04:02:52 -0700294 task_queue_.SendTask([this]() {
295 video_send_config_.rtp.extensions[0] =
296 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId);
297 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
298 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
299 streams_.push_back(new Stream(this));
300 });
Peter Boström5811a392015-12-10 13:02:50 +0100301 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000302
eladalon413ee9a2017-08-22 04:02:52 -0700303 task_queue_.SendTask([this]() {
304 video_send_config_.rtp.extensions[0] =
305 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId);
306 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
307 receiver_log_.PushExpectedLogLine(
308 "WrappingBitrateEstimator: Switching to transmission time offset RBE.");
309 streams_.push_back(new Stream(this));
310 streams_[0]->StopSending();
311 streams_[1]->StopSending();
312 });
Peter Boström5811a392015-12-10 13:02:50 +0100313 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000314}
315} // namespace webrtc