blob: ea48a973ea90a3f5d077921ac2cc904960a3b037 [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]() {
139 std::for_each(streams_.begin(), streams_.end(),
140 std::mem_fun(&Stream::StopSending));
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000141
eladalon413ee9a2017-08-22 04:02:52 -0700142 while (!streams_.empty()) {
143 delete streams_.back();
144 streams_.pop_back();
145 }
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000146
eladalon413ee9a2017-08-22 04:02:52 -0700147 send_transport_.reset();
148 receive_transport_.reset();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000149
eladalon413ee9a2017-08-22 04:02:52 -0700150 receiver_call_.reset();
151 sender_call_.reset();
152 });
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000153 }
154
155 protected:
156 friend class Stream;
157
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000158 class Stream {
159 public:
aleloi5d78e8d2016-11-08 03:44:54 -0800160 explicit Stream(BitrateEstimatorTest* test)
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000161 : test_(test),
162 is_sending_receiving_(false),
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000163 send_stream_(nullptr),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000164 frame_generator_capturer_(),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000165 fake_decoder_() {
stefanff483612015-12-21 03:14:00 -0800166 test_->video_send_config_.rtp.ssrcs[0]++;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000167 send_stream_ = test_->sender_call_->CreateVideoSendStream(
perkj26091b12016-09-01 01:17:40 -0700168 test_->video_send_config_.Copy(),
169 test_->video_encoder_config_.Copy());
kwibergaf476c72016-11-28 15:21:39 -0800170 RTC_DCHECK_EQ(1, test_->video_encoder_config_.number_of_streams);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +0000171 frame_generator_capturer_.reset(test::FrameGeneratorCapturer::Create(
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800172 kDefaultWidth, kDefaultHeight, rtc::nullopt, rtc::nullopt,
173 kDefaultFramerate, Clock::GetRealTimeClock()));
perkj803d97f2016-11-01 11:45:46 -0700174 send_stream_->SetSource(
175 frame_generator_capturer_.get(),
sprangc5d62e22017-04-02 23:53:04 -0700176 VideoSendStream::DegradationPreference::kMaintainFramerate);
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000177 send_stream_->Start();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000178 frame_generator_capturer_->Start();
179
aleloi5d78e8d2016-11-08 03:44:54 -0800180 VideoReceiveStream::Decoder decoder;
181 decoder.decoder = &fake_decoder_;
Niels Möller259a4972018-04-05 15:36:51 +0200182 decoder.payload_type = test_->video_send_config_.rtp.payload_type;
183 decoder.payload_name = test_->video_send_config_.rtp.payload_name;
aleloi5d78e8d2016-11-08 03:44:54 -0800184 test_->receive_config_.decoders.clear();
185 test_->receive_config_.decoders.push_back(decoder);
186 test_->receive_config_.rtp.remote_ssrc =
187 test_->video_send_config_.rtp.ssrcs[0];
188 test_->receive_config_.rtp.local_ssrc++;
189 test_->receive_config_.renderer = &test->fake_renderer_;
190 video_receive_stream_ = test_->receiver_call_->CreateVideoReceiveStream(
191 test_->receive_config_.Copy());
192 video_receive_stream_->Start();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000193 is_sending_receiving_ = true;
194 }
195
196 ~Stream() {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200197 EXPECT_FALSE(is_sending_receiving_);
perkj9fdbda62016-09-15 09:19:20 -0700198 test_->sender_call_->DestroyVideoSendStream(send_stream_);
perkja49cbd32016-09-16 07:53:41 -0700199 frame_generator_capturer_.reset(nullptr);
pbos@webrtc.org2b4ce3a2015-03-23 13:12:24 +0000200 send_stream_ = nullptr;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200201 if (video_receive_stream_) {
202 test_->receiver_call_->DestroyVideoReceiveStream(video_receive_stream_);
203 video_receive_stream_ = nullptr;
204 }
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000205 }
206
207 void StopSending() {
208 if (is_sending_receiving_) {
209 frame_generator_capturer_->Stop();
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000210 send_stream_->Stop();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200211 if (video_receive_stream_) {
212 video_receive_stream_->Stop();
213 }
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000214 is_sending_receiving_ = false;
215 }
216 }
217
218 private:
219 BitrateEstimatorTest* test_;
220 bool is_sending_receiving_;
221 VideoSendStream* send_stream_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200222 VideoReceiveStream* video_receive_stream_;
kwibergb25345e2016-03-12 06:10:44 -0800223 std::unique_ptr<test::FrameGeneratorCapturer> frame_generator_capturer_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000224 test::FakeDecoder fake_decoder_;
225 };
226
Peter Boström7c704b82015-12-04 16:13:05 +0100227 LogObserver receiver_log_;
kwibergb25345e2016-03-12 06:10:44 -0800228 std::unique_ptr<test::DirectTransport> send_transport_;
229 std::unique_ptr<test::DirectTransport> receive_transport_;
230 std::unique_ptr<Call> sender_call_;
231 std::unique_ptr<Call> receiver_call_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000232 VideoReceiveStream::Config receive_config_;
233 std::vector<Stream*> streams_;
234};
235
Erik Språng468e62a2015-07-06 10:50:47 +0200236static const char* kAbsSendTimeLog =
237 "RemoteBitrateEstimatorAbsSendTime: Instantiating.";
238static const char* kSingleStreamLog =
239 "RemoteBitrateEstimatorSingleStream: Instantiating.";
240
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200241TEST_F(BitrateEstimatorTest, InstantiatesTOFPerDefaultForVideo) {
eladalon413ee9a2017-08-22 04:02:52 -0700242 task_queue_.SendTask([this]() {
243 video_send_config_.rtp.extensions.push_back(
244 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
245 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
246 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
247 streams_.push_back(new Stream(this));
248 });
Peter Boström5811a392015-12-10 13:02:50 +0100249 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000250}
251
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200252TEST_F(BitrateEstimatorTest, ImmediatelySwitchToASTForVideo) {
eladalon413ee9a2017-08-22 04:02:52 -0700253 task_queue_.SendTask([this]() {
254 video_send_config_.rtp.extensions.push_back(
255 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId));
256 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
257 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
258 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
259 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
260 streams_.push_back(new Stream(this));
261 });
Peter Boström5811a392015-12-10 13:02:50 +0100262 EXPECT_TRUE(receiver_log_.Wait());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200263}
264
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200265TEST_F(BitrateEstimatorTest, SwitchesToASTForVideo) {
eladalon413ee9a2017-08-22 04:02:52 -0700266 task_queue_.SendTask([this]() {
267 video_send_config_.rtp.extensions.push_back(
268 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
269 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
270 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
271 streams_.push_back(new Stream(this));
272 });
Peter Boström5811a392015-12-10 13:02:50 +0100273 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000274
eladalon413ee9a2017-08-22 04:02:52 -0700275 task_queue_.SendTask([this]() {
276 video_send_config_.rtp.extensions[0] =
277 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId);
278 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
279 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
280 streams_.push_back(new Stream(this));
281 });
Peter Boström5811a392015-12-10 13:02:50 +0100282 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000283}
284
deadbeef1086ed62016-04-22 10:52:40 -0700285// This test is flaky. See webrtc:5790.
286TEST_F(BitrateEstimatorTest, DISABLED_SwitchesToASTThenBackToTOFForVideo) {
eladalon413ee9a2017-08-22 04:02:52 -0700287 task_queue_.SendTask([this]() {
288 video_send_config_.rtp.extensions.push_back(
289 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId));
290 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
291 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
292 receiver_log_.PushExpectedLogLine(kSingleStreamLog);
293 streams_.push_back(new Stream(this));
294 });
Peter Boström5811a392015-12-10 13:02:50 +0100295 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000296
eladalon413ee9a2017-08-22 04:02:52 -0700297 task_queue_.SendTask([this]() {
298 video_send_config_.rtp.extensions[0] =
299 RtpExtension(RtpExtension::kAbsSendTimeUri, kASTExtensionId);
300 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
301 receiver_log_.PushExpectedLogLine("Switching to absolute send time RBE.");
302 streams_.push_back(new Stream(this));
303 });
Peter Boström5811a392015-12-10 13:02:50 +0100304 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000305
eladalon413ee9a2017-08-22 04:02:52 -0700306 task_queue_.SendTask([this]() {
307 video_send_config_.rtp.extensions[0] =
308 RtpExtension(RtpExtension::kTimestampOffsetUri, kTOFExtensionId);
309 receiver_log_.PushExpectedLogLine(kAbsSendTimeLog);
310 receiver_log_.PushExpectedLogLine(
311 "WrappingBitrateEstimator: Switching to transmission time offset RBE.");
312 streams_.push_back(new Stream(this));
313 streams_[0]->StopSending();
314 streams_[1]->StopSending();
315 });
Peter Boström5811a392015-12-10 13:02:50 +0100316 EXPECT_TRUE(receiver_log_.Wait());
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000317}
318} // namespace webrtc