1 /*
2 * Copyright (c) 2014 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
11 #include "test/call_test.h"
12
13 #include <algorithm>
14 #include <memory>
15
16 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
17 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
18 #include "api/task_queue/default_task_queue_factory.h"
19 #include "api/task_queue/task_queue_base.h"
20 #include "api/test/create_frame_generator.h"
21 #include "api/video/builtin_video_bitrate_allocator_factory.h"
22 #include "api/video_codecs/video_encoder_config.h"
23 #include "call/fake_network_pipe.h"
24 #include "call/simulated_network.h"
25 #include "modules/audio_mixer/audio_mixer_impl.h"
26 #include "rtc_base/checks.h"
27 #include "rtc_base/event.h"
28 #include "rtc_base/task_queue_for_test.h"
29 #include "test/fake_encoder.h"
30 #include "test/testsupport/file_utils.h"
31
32 namespace webrtc {
33 namespace test {
34
CallTest()35 CallTest::CallTest()
36 : clock_(Clock::GetRealTimeClock()),
37 task_queue_factory_(CreateDefaultTaskQueueFactory()),
38 send_event_log_(std::make_unique<RtcEventLogNull>()),
39 recv_event_log_(std::make_unique<RtcEventLogNull>()),
40 audio_send_config_(/*send_transport=*/nullptr),
41 audio_send_stream_(nullptr),
42 frame_generator_capturer_(nullptr),
43 fake_encoder_factory_([this]() {
44 std::unique_ptr<FakeEncoder> fake_encoder;
45 if (video_encoder_configs_[0].codec_type == kVideoCodecVP8) {
46 fake_encoder = std::make_unique<FakeVp8Encoder>(clock_);
47 } else {
48 fake_encoder = std::make_unique<FakeEncoder>(clock_);
49 }
50 fake_encoder->SetMaxBitrate(fake_encoder_max_bitrate_);
51 return fake_encoder;
52 }),
__anoncaf0c1cc0202() 53 fake_decoder_factory_([]() { return std::make_unique<FakeDecoder>(); }),
54 bitrate_allocator_factory_(CreateBuiltinVideoBitrateAllocatorFactory()),
55 num_video_streams_(1),
56 num_audio_streams_(0),
57 num_flexfec_streams_(0),
58 audio_decoder_factory_(CreateBuiltinAudioDecoderFactory()),
59 audio_encoder_factory_(CreateBuiltinAudioEncoderFactory()),
60 task_queue_(task_queue_factory_->CreateTaskQueue(
61 "CallTestTaskQueue",
62 TaskQueueFactory::Priority::NORMAL)) {}
63
64 CallTest::~CallTest() = default;
65
RegisterRtpExtension(const RtpExtension & extension)66 void CallTest::RegisterRtpExtension(const RtpExtension& extension) {
67 for (const RtpExtension& registered_extension : rtp_extensions_) {
68 if (registered_extension.id == extension.id) {
69 ASSERT_EQ(registered_extension.uri, extension.uri)
70 << "Different URIs associated with ID " << extension.id << ".";
71 ASSERT_EQ(registered_extension.encrypt, extension.encrypt)
72 << "Encryption mismatch associated with ID " << extension.id << ".";
73 return;
74 } else { // Different IDs.
75 // Different IDs referring to the same extension probably indicate
76 // a mistake in the test.
77 ASSERT_FALSE(registered_extension.uri == extension.uri &&
78 registered_extension.encrypt == extension.encrypt)
79 << "URI " << extension.uri
80 << (extension.encrypt ? " with " : " without ")
81 << "encryption already registered with a different "
82 "ID ("
83 << extension.id << " vs. " << registered_extension.id << ").";
84 }
85 }
86 rtp_extensions_.push_back(extension);
87 }
88
RunBaseTest(BaseTest * test)89 void CallTest::RunBaseTest(BaseTest* test) {
90 SendTask(RTC_FROM_HERE, task_queue(), [this, test]() {
91 num_video_streams_ = test->GetNumVideoStreams();
92 num_audio_streams_ = test->GetNumAudioStreams();
93 num_flexfec_streams_ = test->GetNumFlexfecStreams();
94 RTC_DCHECK(num_video_streams_ > 0 || num_audio_streams_ > 0);
95 Call::Config send_config(send_event_log_.get());
96 test->ModifySenderBitrateConfig(&send_config.bitrate_config);
97 if (num_audio_streams_ > 0) {
98 CreateFakeAudioDevices(test->CreateCapturer(), test->CreateRenderer());
99 test->OnFakeAudioDevicesCreated(fake_send_audio_device_.get(),
100 fake_recv_audio_device_.get());
101 apm_send_ = AudioProcessingBuilder().Create();
102 apm_recv_ = AudioProcessingBuilder().Create();
103 EXPECT_EQ(0, fake_send_audio_device_->Init());
104 EXPECT_EQ(0, fake_recv_audio_device_->Init());
105 AudioState::Config audio_state_config;
106 audio_state_config.audio_mixer = AudioMixerImpl::Create();
107 audio_state_config.audio_processing = apm_send_;
108 audio_state_config.audio_device_module = fake_send_audio_device_;
109 send_config.audio_state = AudioState::Create(audio_state_config);
110 fake_send_audio_device_->RegisterAudioCallback(
111 send_config.audio_state->audio_transport());
112 }
113 CreateSenderCall(send_config);
114 if (test->ShouldCreateReceivers()) {
115 Call::Config recv_config(recv_event_log_.get());
116 test->ModifyReceiverBitrateConfig(&recv_config.bitrate_config);
117 if (num_audio_streams_ > 0) {
118 AudioState::Config audio_state_config;
119 audio_state_config.audio_mixer = AudioMixerImpl::Create();
120 audio_state_config.audio_processing = apm_recv_;
121 audio_state_config.audio_device_module = fake_recv_audio_device_;
122 recv_config.audio_state = AudioState::Create(audio_state_config);
123 fake_recv_audio_device_->RegisterAudioCallback(
124 recv_config.audio_state->audio_transport());
125 }
126 CreateReceiverCall(recv_config);
127 }
128 test->OnCallsCreated(sender_call_.get(), receiver_call_.get());
129 receive_transport_ = test->CreateReceiveTransport(task_queue());
130 send_transport_ =
131 test->CreateSendTransport(task_queue(), sender_call_.get());
132
133 if (test->ShouldCreateReceivers()) {
134 send_transport_->SetReceiver(receiver_call_->Receiver());
135 receive_transport_->SetReceiver(sender_call_->Receiver());
136 if (num_video_streams_ > 0)
137 receiver_call_->SignalChannelNetworkState(MediaType::VIDEO, kNetworkUp);
138 if (num_audio_streams_ > 0)
139 receiver_call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
140 } else {
141 // Sender-only call delivers to itself.
142 send_transport_->SetReceiver(sender_call_->Receiver());
143 receive_transport_->SetReceiver(nullptr);
144 }
145
146 CreateSendConfig(num_video_streams_, num_audio_streams_,
147 num_flexfec_streams_, send_transport_.get());
148 if (test->ShouldCreateReceivers()) {
149 CreateMatchingReceiveConfigs(receive_transport_.get());
150 }
151 if (num_video_streams_ > 0) {
152 test->ModifyVideoConfigs(GetVideoSendConfig(), &video_receive_configs_,
153 GetVideoEncoderConfig());
154 }
155 if (num_audio_streams_ > 0) {
156 test->ModifyAudioConfigs(&audio_send_config_, &audio_receive_configs_);
157 }
158 if (num_flexfec_streams_ > 0) {
159 test->ModifyFlexfecConfigs(&flexfec_receive_configs_);
160 }
161
162 if (num_flexfec_streams_ > 0) {
163 CreateFlexfecStreams();
164 test->OnFlexfecStreamsCreated(flexfec_receive_streams_);
165 }
166 if (num_video_streams_ > 0) {
167 CreateVideoStreams();
168 test->OnVideoStreamsCreated(GetVideoSendStream(), video_receive_streams_);
169 }
170 if (num_audio_streams_ > 0) {
171 CreateAudioStreams();
172 test->OnAudioStreamsCreated(audio_send_stream_, audio_receive_streams_);
173 }
174
175 if (num_video_streams_ > 0) {
176 int width = kDefaultWidth;
177 int height = kDefaultHeight;
178 int frame_rate = kDefaultFramerate;
179 test->ModifyVideoCaptureStartResolution(&width, &height, &frame_rate);
180 test->ModifyVideoDegradationPreference(°radation_preference_);
181 CreateFrameGeneratorCapturer(frame_rate, width, height);
182 test->OnFrameGeneratorCapturerCreated(frame_generator_capturer_);
183 }
184
185 Start();
186 });
187
188 test->PerformTest();
189
190 SendTask(RTC_FROM_HERE, task_queue(), [this, test]() {
191 Stop();
192 test->OnStreamsStopped();
193 DestroyStreams();
194 send_transport_.reset();
195 receive_transport_.reset();
196
197 frame_generator_capturer_ = nullptr;
198 DestroyCalls();
199
200 fake_send_audio_device_ = nullptr;
201 fake_recv_audio_device_ = nullptr;
202 });
203 }
204
CreateCalls()205 void CallTest::CreateCalls() {
206 CreateCalls(Call::Config(send_event_log_.get()),
207 Call::Config(recv_event_log_.get()));
208 }
209
CreateCalls(const Call::Config & sender_config,const Call::Config & receiver_config)210 void CallTest::CreateCalls(const Call::Config& sender_config,
211 const Call::Config& receiver_config) {
212 CreateSenderCall(sender_config);
213 CreateReceiverCall(receiver_config);
214 }
215
CreateSenderCall()216 void CallTest::CreateSenderCall() {
217 CreateSenderCall(Call::Config(send_event_log_.get()));
218 }
219
CreateSenderCall(const Call::Config & config)220 void CallTest::CreateSenderCall(const Call::Config& config) {
221 auto sender_config = config;
222 sender_config.task_queue_factory = task_queue_factory_.get();
223 sender_config.network_state_predictor_factory =
224 network_state_predictor_factory_.get();
225 sender_config.network_controller_factory = network_controller_factory_.get();
226 sender_config.trials = &field_trials_;
227 sender_call_.reset(Call::Create(sender_config));
228 }
229
CreateReceiverCall(const Call::Config & config)230 void CallTest::CreateReceiverCall(const Call::Config& config) {
231 auto receiver_config = config;
232 receiver_config.task_queue_factory = task_queue_factory_.get();
233 receiver_config.trials = &field_trials_;
234 receiver_call_.reset(Call::Create(receiver_config));
235 }
236
DestroyCalls()237 void CallTest::DestroyCalls() {
238 sender_call_.reset();
239 receiver_call_.reset();
240 }
241
CreateVideoSendConfig(VideoSendStream::Config * video_config,size_t num_video_streams,size_t num_used_ssrcs,Transport * send_transport)242 void CallTest::CreateVideoSendConfig(VideoSendStream::Config* video_config,
243 size_t num_video_streams,
244 size_t num_used_ssrcs,
245 Transport* send_transport) {
246 RTC_DCHECK_LE(num_video_streams + num_used_ssrcs, kNumSsrcs);
247 *video_config = VideoSendStream::Config(send_transport);
248 video_config->encoder_settings.encoder_factory = &fake_encoder_factory_;
249 video_config->encoder_settings.bitrate_allocator_factory =
250 bitrate_allocator_factory_.get();
251 video_config->rtp.payload_name = "FAKE";
252 video_config->rtp.payload_type = kFakeVideoSendPayloadType;
253 video_config->rtp.extmap_allow_mixed = true;
254 AddRtpExtensionByUri(RtpExtension::kTransportSequenceNumberUri,
255 &video_config->rtp.extensions);
256 AddRtpExtensionByUri(RtpExtension::kVideoContentTypeUri,
257 &video_config->rtp.extensions);
258 AddRtpExtensionByUri(RtpExtension::kGenericFrameDescriptorUri00,
259 &video_config->rtp.extensions);
260 AddRtpExtensionByUri(RtpExtension::kDependencyDescriptorUri,
261 &video_config->rtp.extensions);
262 if (video_encoder_configs_.empty()) {
263 video_encoder_configs_.emplace_back();
264 FillEncoderConfiguration(kVideoCodecGeneric, num_video_streams,
265 &video_encoder_configs_.back());
266 }
267 for (size_t i = 0; i < num_video_streams; ++i)
268 video_config->rtp.ssrcs.push_back(kVideoSendSsrcs[num_used_ssrcs + i]);
269 AddRtpExtensionByUri(RtpExtension::kVideoRotationUri,
270 &video_config->rtp.extensions);
271 AddRtpExtensionByUri(RtpExtension::kColorSpaceUri,
272 &video_config->rtp.extensions);
273 }
274
CreateAudioAndFecSendConfigs(size_t num_audio_streams,size_t num_flexfec_streams,Transport * send_transport)275 void CallTest::CreateAudioAndFecSendConfigs(size_t num_audio_streams,
276 size_t num_flexfec_streams,
277 Transport* send_transport) {
278 RTC_DCHECK_LE(num_audio_streams, 1);
279 RTC_DCHECK_LE(num_flexfec_streams, 1);
280 if (num_audio_streams > 0) {
281 AudioSendStream::Config audio_send_config(send_transport);
282 audio_send_config.rtp.ssrc = kAudioSendSsrc;
283 audio_send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
284 kAudioSendPayloadType, {"opus", 48000, 2, {{"stereo", "1"}}});
285 audio_send_config.encoder_factory = audio_encoder_factory_;
286 SetAudioConfig(audio_send_config);
287 }
288
289 // TODO(brandtr): Update this when we support multistream protection.
290 if (num_flexfec_streams > 0) {
291 SetSendFecConfig({kVideoSendSsrcs[0]});
292 }
293 }
294
SetAudioConfig(const AudioSendStream::Config & config)295 void CallTest::SetAudioConfig(const AudioSendStream::Config& config) {
296 audio_send_config_ = config;
297 }
298
SetSendFecConfig(std::vector<uint32_t> video_send_ssrcs)299 void CallTest::SetSendFecConfig(std::vector<uint32_t> video_send_ssrcs) {
300 GetVideoSendConfig()->rtp.flexfec.payload_type = kFlexfecPayloadType;
301 GetVideoSendConfig()->rtp.flexfec.ssrc = kFlexfecSendSsrc;
302 GetVideoSendConfig()->rtp.flexfec.protected_media_ssrcs = video_send_ssrcs;
303 }
304
SetSendUlpFecConfig(VideoSendStream::Config * send_config)305 void CallTest::SetSendUlpFecConfig(VideoSendStream::Config* send_config) {
306 send_config->rtp.ulpfec.red_payload_type = kRedPayloadType;
307 send_config->rtp.ulpfec.ulpfec_payload_type = kUlpfecPayloadType;
308 send_config->rtp.ulpfec.red_rtx_payload_type = kRtxRedPayloadType;
309 }
310
SetReceiveUlpFecConfig(VideoReceiveStream::Config * receive_config)311 void CallTest::SetReceiveUlpFecConfig(
312 VideoReceiveStream::Config* receive_config) {
313 receive_config->rtp.red_payload_type = kRedPayloadType;
314 receive_config->rtp.ulpfec_payload_type = kUlpfecPayloadType;
315 receive_config->rtp.rtx_associated_payload_types[kRtxRedPayloadType] =
316 kRedPayloadType;
317 }
318
CreateSendConfig(size_t num_video_streams,size_t num_audio_streams,size_t num_flexfec_streams,Transport * send_transport)319 void CallTest::CreateSendConfig(size_t num_video_streams,
320 size_t num_audio_streams,
321 size_t num_flexfec_streams,
322 Transport* send_transport) {
323 if (num_video_streams > 0) {
324 video_send_configs_.clear();
325 video_send_configs_.emplace_back(nullptr);
326 CreateVideoSendConfig(&video_send_configs_.back(), num_video_streams, 0,
327 send_transport);
328 }
329 CreateAudioAndFecSendConfigs(num_audio_streams, num_flexfec_streams,
330 send_transport);
331 }
332
CreateMatchingVideoReceiveConfigs(const VideoSendStream::Config & video_send_config,Transport * rtcp_send_transport)333 void CallTest::CreateMatchingVideoReceiveConfigs(
334 const VideoSendStream::Config& video_send_config,
335 Transport* rtcp_send_transport) {
336 CreateMatchingVideoReceiveConfigs(video_send_config, rtcp_send_transport,
337 true, &fake_decoder_factory_, absl::nullopt,
338 false, 0);
339 }
340
CreateMatchingVideoReceiveConfigs(const VideoSendStream::Config & video_send_config,Transport * rtcp_send_transport,bool send_side_bwe,VideoDecoderFactory * decoder_factory,absl::optional<size_t> decode_sub_stream,bool receiver_reference_time_report,int rtp_history_ms)341 void CallTest::CreateMatchingVideoReceiveConfigs(
342 const VideoSendStream::Config& video_send_config,
343 Transport* rtcp_send_transport,
344 bool send_side_bwe,
345 VideoDecoderFactory* decoder_factory,
346 absl::optional<size_t> decode_sub_stream,
347 bool receiver_reference_time_report,
348 int rtp_history_ms) {
349 AddMatchingVideoReceiveConfigs(
350 &video_receive_configs_, video_send_config, rtcp_send_transport,
351 send_side_bwe, decoder_factory, decode_sub_stream,
352 receiver_reference_time_report, rtp_history_ms);
353 }
354
AddMatchingVideoReceiveConfigs(std::vector<VideoReceiveStream::Config> * receive_configs,const VideoSendStream::Config & video_send_config,Transport * rtcp_send_transport,bool send_side_bwe,VideoDecoderFactory * decoder_factory,absl::optional<size_t> decode_sub_stream,bool receiver_reference_time_report,int rtp_history_ms)355 void CallTest::AddMatchingVideoReceiveConfigs(
356 std::vector<VideoReceiveStream::Config>* receive_configs,
357 const VideoSendStream::Config& video_send_config,
358 Transport* rtcp_send_transport,
359 bool send_side_bwe,
360 VideoDecoderFactory* decoder_factory,
361 absl::optional<size_t> decode_sub_stream,
362 bool receiver_reference_time_report,
363 int rtp_history_ms) {
364 RTC_DCHECK(!video_send_config.rtp.ssrcs.empty());
365 VideoReceiveStream::Config default_config(rtcp_send_transport);
366 default_config.rtp.transport_cc = send_side_bwe;
367 default_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
368 for (const RtpExtension& extension : video_send_config.rtp.extensions)
369 default_config.rtp.extensions.push_back(extension);
370 default_config.rtp.nack.rtp_history_ms = rtp_history_ms;
371 // Enable RTT calculation so NTP time estimator will work.
372 default_config.rtp.rtcp_xr.receiver_reference_time_report =
373 receiver_reference_time_report;
374 default_config.renderer = &fake_renderer_;
375
376 for (size_t i = 0; i < video_send_config.rtp.ssrcs.size(); ++i) {
377 VideoReceiveStream::Config video_recv_config(default_config.Copy());
378 video_recv_config.decoders.clear();
379 if (!video_send_config.rtp.rtx.ssrcs.empty()) {
380 video_recv_config.rtp.rtx_ssrc = video_send_config.rtp.rtx.ssrcs[i];
381 video_recv_config.rtp.rtx_associated_payload_types[kSendRtxPayloadType] =
382 video_send_config.rtp.payload_type;
383 }
384 video_recv_config.rtp.remote_ssrc = video_send_config.rtp.ssrcs[i];
385 VideoReceiveStream::Decoder decoder;
386
387 decoder.payload_type = video_send_config.rtp.payload_type;
388 decoder.video_format = SdpVideoFormat(video_send_config.rtp.payload_name);
389 // Force fake decoders on non-selected simulcast streams.
390 if (!decode_sub_stream || i == *decode_sub_stream) {
391 decoder.decoder_factory = decoder_factory;
392 } else {
393 decoder.decoder_factory = &fake_decoder_factory_;
394 }
395 video_recv_config.decoders.push_back(decoder);
396 receive_configs->emplace_back(std::move(video_recv_config));
397 }
398 }
399
CreateMatchingAudioAndFecConfigs(Transport * rtcp_send_transport)400 void CallTest::CreateMatchingAudioAndFecConfigs(
401 Transport* rtcp_send_transport) {
402 RTC_DCHECK_GE(1, num_audio_streams_);
403 if (num_audio_streams_ == 1) {
404 CreateMatchingAudioConfigs(rtcp_send_transport, "");
405 }
406
407 // TODO(brandtr): Update this when we support multistream protection.
408 RTC_DCHECK(num_flexfec_streams_ <= 1);
409 if (num_flexfec_streams_ == 1) {
410 CreateMatchingFecConfig(rtcp_send_transport, *GetVideoSendConfig());
411 for (const RtpExtension& extension : GetVideoSendConfig()->rtp.extensions)
412 GetFlexFecConfig()->rtp_header_extensions.push_back(extension);
413 }
414 }
415
CreateMatchingAudioConfigs(Transport * transport,std::string sync_group)416 void CallTest::CreateMatchingAudioConfigs(Transport* transport,
417 std::string sync_group) {
418 audio_receive_configs_.push_back(CreateMatchingAudioConfig(
419 audio_send_config_, audio_decoder_factory_, transport, sync_group));
420 }
421
CreateMatchingAudioConfig(const AudioSendStream::Config & send_config,rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory,Transport * transport,std::string sync_group)422 AudioReceiveStream::Config CallTest::CreateMatchingAudioConfig(
423 const AudioSendStream::Config& send_config,
424 rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory,
425 Transport* transport,
426 std::string sync_group) {
427 AudioReceiveStream::Config audio_config;
428 audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
429 audio_config.rtcp_send_transport = transport;
430 audio_config.rtp.remote_ssrc = send_config.rtp.ssrc;
431 audio_config.rtp.transport_cc =
432 send_config.send_codec_spec
433 ? send_config.send_codec_spec->transport_cc_enabled
434 : false;
435 audio_config.rtp.extensions = send_config.rtp.extensions;
436 audio_config.decoder_factory = audio_decoder_factory;
437 audio_config.decoder_map = {{kAudioSendPayloadType, {"opus", 48000, 2}}};
438 audio_config.sync_group = sync_group;
439 return audio_config;
440 }
441
CreateMatchingFecConfig(Transport * transport,const VideoSendStream::Config & send_config)442 void CallTest::CreateMatchingFecConfig(
443 Transport* transport,
444 const VideoSendStream::Config& send_config) {
445 FlexfecReceiveStream::Config config(transport);
446 config.payload_type = send_config.rtp.flexfec.payload_type;
447 config.remote_ssrc = send_config.rtp.flexfec.ssrc;
448 config.protected_media_ssrcs = send_config.rtp.flexfec.protected_media_ssrcs;
449 config.local_ssrc = kReceiverLocalVideoSsrc;
450 if (!video_receive_configs_.empty())
451 video_receive_configs_[0].rtp.protected_by_flexfec = true;
452 flexfec_receive_configs_.push_back(config);
453 }
454
CreateMatchingReceiveConfigs(Transport * rtcp_send_transport)455 void CallTest::CreateMatchingReceiveConfigs(Transport* rtcp_send_transport) {
456 video_receive_configs_.clear();
457 for (VideoSendStream::Config& video_send_config : video_send_configs_) {
458 CreateMatchingVideoReceiveConfigs(video_send_config, rtcp_send_transport);
459 }
460 CreateMatchingAudioAndFecConfigs(rtcp_send_transport);
461 }
462
CreateFrameGeneratorCapturerWithDrift(Clock * clock,float speed,int framerate,int width,int height)463 void CallTest::CreateFrameGeneratorCapturerWithDrift(Clock* clock,
464 float speed,
465 int framerate,
466 int width,
467 int height) {
468 video_sources_.clear();
469 auto frame_generator_capturer =
470 std::make_unique<test::FrameGeneratorCapturer>(
471 clock,
472 test::CreateSquareFrameGenerator(width, height, absl::nullopt,
473 absl::nullopt),
474 framerate * speed, *task_queue_factory_);
475 frame_generator_capturer_ = frame_generator_capturer.get();
476 frame_generator_capturer->Init();
477 video_sources_.push_back(std::move(frame_generator_capturer));
478 ConnectVideoSourcesToStreams();
479 }
480
CreateFrameGeneratorCapturer(int framerate,int width,int height)481 void CallTest::CreateFrameGeneratorCapturer(int framerate,
482 int width,
483 int height) {
484 video_sources_.clear();
485 auto frame_generator_capturer =
486 std::make_unique<test::FrameGeneratorCapturer>(
487 clock_,
488 test::CreateSquareFrameGenerator(width, height, absl::nullopt,
489 absl::nullopt),
490 framerate, *task_queue_factory_);
491 frame_generator_capturer_ = frame_generator_capturer.get();
492 frame_generator_capturer->Init();
493 video_sources_.push_back(std::move(frame_generator_capturer));
494 ConnectVideoSourcesToStreams();
495 }
496
CreateFakeAudioDevices(std::unique_ptr<TestAudioDeviceModule::Capturer> capturer,std::unique_ptr<TestAudioDeviceModule::Renderer> renderer)497 void CallTest::CreateFakeAudioDevices(
498 std::unique_ptr<TestAudioDeviceModule::Capturer> capturer,
499 std::unique_ptr<TestAudioDeviceModule::Renderer> renderer) {
500 fake_send_audio_device_ = TestAudioDeviceModule::Create(
501 task_queue_factory_.get(), std::move(capturer), nullptr, 1.f);
502 fake_recv_audio_device_ = TestAudioDeviceModule::Create(
503 task_queue_factory_.get(), nullptr, std::move(renderer), 1.f);
504 }
505
CreateVideoStreams()506 void CallTest::CreateVideoStreams() {
507 RTC_DCHECK(video_receive_streams_.empty());
508 CreateVideoSendStreams();
509 for (size_t i = 0; i < video_receive_configs_.size(); ++i) {
510 video_receive_streams_.push_back(receiver_call_->CreateVideoReceiveStream(
511 video_receive_configs_[i].Copy()));
512 }
513
514 AssociateFlexfecStreamsWithVideoStreams();
515 }
516
CreateVideoSendStreams()517 void CallTest::CreateVideoSendStreams() {
518 RTC_DCHECK(video_send_streams_.empty());
519
520 // We currently only support testing external fec controllers with a single
521 // VideoSendStream.
522 if (fec_controller_factory_.get()) {
523 RTC_DCHECK_LE(video_send_configs_.size(), 1);
524 }
525
526 // TODO(http://crbug/818127):
527 // Remove this workaround when ALR is not screenshare-specific.
528 std::list<size_t> streams_creation_order;
529 for (size_t i = 0; i < video_send_configs_.size(); ++i) {
530 // If dual streams are created, add the screenshare stream last.
531 if (video_encoder_configs_[i].content_type ==
532 VideoEncoderConfig::ContentType::kScreen) {
533 streams_creation_order.push_back(i);
534 } else {
535 streams_creation_order.push_front(i);
536 }
537 }
538
539 video_send_streams_.resize(video_send_configs_.size(), nullptr);
540
541 for (size_t i : streams_creation_order) {
542 if (fec_controller_factory_.get()) {
543 video_send_streams_[i] = sender_call_->CreateVideoSendStream(
544 video_send_configs_[i].Copy(), video_encoder_configs_[i].Copy(),
545 fec_controller_factory_->CreateFecController());
546 } else {
547 video_send_streams_[i] = sender_call_->CreateVideoSendStream(
548 video_send_configs_[i].Copy(), video_encoder_configs_[i].Copy());
549 }
550 }
551 }
552
CreateVideoSendStream(const VideoEncoderConfig & encoder_config)553 void CallTest::CreateVideoSendStream(const VideoEncoderConfig& encoder_config) {
554 RTC_DCHECK(video_send_streams_.empty());
555 video_send_streams_.push_back(sender_call_->CreateVideoSendStream(
556 GetVideoSendConfig()->Copy(), encoder_config.Copy()));
557 }
558
CreateAudioStreams()559 void CallTest::CreateAudioStreams() {
560 RTC_DCHECK(audio_send_stream_ == nullptr);
561 RTC_DCHECK(audio_receive_streams_.empty());
562 audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
563 for (size_t i = 0; i < audio_receive_configs_.size(); ++i) {
564 audio_receive_streams_.push_back(
565 receiver_call_->CreateAudioReceiveStream(audio_receive_configs_[i]));
566 }
567 }
568
CreateFlexfecStreams()569 void CallTest::CreateFlexfecStreams() {
570 for (size_t i = 0; i < flexfec_receive_configs_.size(); ++i) {
571 flexfec_receive_streams_.push_back(
572 receiver_call_->CreateFlexfecReceiveStream(
573 flexfec_receive_configs_[i]));
574 }
575
576 AssociateFlexfecStreamsWithVideoStreams();
577 }
578
ConnectVideoSourcesToStreams()579 void CallTest::ConnectVideoSourcesToStreams() {
580 for (size_t i = 0; i < video_sources_.size(); ++i)
581 video_send_streams_[i]->SetSource(video_sources_[i].get(),
582 degradation_preference_);
583 }
584
AssociateFlexfecStreamsWithVideoStreams()585 void CallTest::AssociateFlexfecStreamsWithVideoStreams() {
586 // All FlexFEC streams protect all of the video streams.
587 for (FlexfecReceiveStream* flexfec_recv_stream : flexfec_receive_streams_) {
588 for (VideoReceiveStream* video_recv_stream : video_receive_streams_) {
589 video_recv_stream->AddSecondarySink(flexfec_recv_stream);
590 }
591 }
592 }
593
DissociateFlexfecStreamsFromVideoStreams()594 void CallTest::DissociateFlexfecStreamsFromVideoStreams() {
595 for (FlexfecReceiveStream* flexfec_recv_stream : flexfec_receive_streams_) {
596 for (VideoReceiveStream* video_recv_stream : video_receive_streams_) {
597 video_recv_stream->RemoveSecondarySink(flexfec_recv_stream);
598 }
599 }
600 }
601
Start()602 void CallTest::Start() {
603 StartVideoStreams();
604 if (audio_send_stream_) {
605 audio_send_stream_->Start();
606 }
607 for (AudioReceiveStream* audio_recv_stream : audio_receive_streams_)
608 audio_recv_stream->Start();
609 }
610
StartVideoStreams()611 void CallTest::StartVideoStreams() {
612 for (VideoSendStream* video_send_stream : video_send_streams_)
613 video_send_stream->Start();
614 for (VideoReceiveStream* video_recv_stream : video_receive_streams_)
615 video_recv_stream->Start();
616 }
617
Stop()618 void CallTest::Stop() {
619 for (AudioReceiveStream* audio_recv_stream : audio_receive_streams_)
620 audio_recv_stream->Stop();
621 if (audio_send_stream_) {
622 audio_send_stream_->Stop();
623 }
624 StopVideoStreams();
625 }
626
StopVideoStreams()627 void CallTest::StopVideoStreams() {
628 for (VideoSendStream* video_send_stream : video_send_streams_)
629 video_send_stream->Stop();
630 for (VideoReceiveStream* video_recv_stream : video_receive_streams_)
631 video_recv_stream->Stop();
632 }
633
DestroyStreams()634 void CallTest::DestroyStreams() {
635 DissociateFlexfecStreamsFromVideoStreams();
636
637 if (audio_send_stream_)
638 sender_call_->DestroyAudioSendStream(audio_send_stream_);
639 audio_send_stream_ = nullptr;
640 for (AudioReceiveStream* audio_recv_stream : audio_receive_streams_)
641 receiver_call_->DestroyAudioReceiveStream(audio_recv_stream);
642
643 DestroyVideoSendStreams();
644
645 for (VideoReceiveStream* video_recv_stream : video_receive_streams_)
646 receiver_call_->DestroyVideoReceiveStream(video_recv_stream);
647
648 for (FlexfecReceiveStream* flexfec_recv_stream : flexfec_receive_streams_)
649 receiver_call_->DestroyFlexfecReceiveStream(flexfec_recv_stream);
650
651 video_receive_streams_.clear();
652 video_sources_.clear();
653 }
654
DestroyVideoSendStreams()655 void CallTest::DestroyVideoSendStreams() {
656 for (VideoSendStream* video_send_stream : video_send_streams_)
657 sender_call_->DestroyVideoSendStream(video_send_stream);
658 video_send_streams_.clear();
659 }
660
SetFakeVideoCaptureRotation(VideoRotation rotation)661 void CallTest::SetFakeVideoCaptureRotation(VideoRotation rotation) {
662 frame_generator_capturer_->SetFakeRotation(rotation);
663 }
664
SetVideoDegradation(DegradationPreference preference)665 void CallTest::SetVideoDegradation(DegradationPreference preference) {
666 GetVideoSendStream()->SetSource(frame_generator_capturer_, preference);
667 }
668
GetVideoSendConfig()669 VideoSendStream::Config* CallTest::GetVideoSendConfig() {
670 return &video_send_configs_[0];
671 }
672
SetVideoSendConfig(const VideoSendStream::Config & config)673 void CallTest::SetVideoSendConfig(const VideoSendStream::Config& config) {
674 video_send_configs_.clear();
675 video_send_configs_.push_back(config.Copy());
676 }
677
GetVideoEncoderConfig()678 VideoEncoderConfig* CallTest::GetVideoEncoderConfig() {
679 return &video_encoder_configs_[0];
680 }
681
SetVideoEncoderConfig(const VideoEncoderConfig & config)682 void CallTest::SetVideoEncoderConfig(const VideoEncoderConfig& config) {
683 video_encoder_configs_.clear();
684 video_encoder_configs_.push_back(config.Copy());
685 }
686
GetVideoSendStream()687 VideoSendStream* CallTest::GetVideoSendStream() {
688 return video_send_streams_[0];
689 }
GetFlexFecConfig()690 FlexfecReceiveStream::Config* CallTest::GetFlexFecConfig() {
691 return &flexfec_receive_configs_[0];
692 }
693
GetRtpExtensionByUri(const std::string & uri) const694 absl::optional<RtpExtension> CallTest::GetRtpExtensionByUri(
695 const std::string& uri) const {
696 for (const auto& extension : rtp_extensions_) {
697 if (extension.uri == uri) {
698 return extension;
699 }
700 }
701 return absl::nullopt;
702 }
703
AddRtpExtensionByUri(const std::string & uri,std::vector<RtpExtension> * extensions) const704 void CallTest::AddRtpExtensionByUri(
705 const std::string& uri,
706 std::vector<RtpExtension>* extensions) const {
707 const absl::optional<RtpExtension> extension = GetRtpExtensionByUri(uri);
708 if (extension) {
709 extensions->push_back(*extension);
710 }
711 }
712
713 constexpr size_t CallTest::kNumSsrcs;
714 const int CallTest::kDefaultWidth;
715 const int CallTest::kDefaultHeight;
716 const int CallTest::kDefaultFramerate;
717 const int CallTest::kDefaultTimeoutMs = 30 * 1000;
718 const int CallTest::kLongTimeoutMs = 120 * 1000;
719 const uint32_t CallTest::kSendRtxSsrcs[kNumSsrcs] = {
720 0xBADCAFD, 0xBADCAFE, 0xBADCAFF, 0xBADCB00, 0xBADCB01, 0xBADCB02};
721 const uint32_t CallTest::kVideoSendSsrcs[kNumSsrcs] = {
722 0xC0FFED, 0xC0FFEE, 0xC0FFEF, 0xC0FFF0, 0xC0FFF1, 0xC0FFF2};
723 const uint32_t CallTest::kAudioSendSsrc = 0xDEADBEEF;
724 const uint32_t CallTest::kFlexfecSendSsrc = 0xBADBEEF;
725 const uint32_t CallTest::kReceiverLocalVideoSsrc = 0x123456;
726 const uint32_t CallTest::kReceiverLocalAudioSsrc = 0x1234567;
727 const int CallTest::kNackRtpHistoryMs = 1000;
728
729 const std::map<uint8_t, MediaType> CallTest::payload_type_map_ = {
730 {CallTest::kVideoSendPayloadType, MediaType::VIDEO},
731 {CallTest::kFakeVideoSendPayloadType, MediaType::VIDEO},
732 {CallTest::kSendRtxPayloadType, MediaType::VIDEO},
733 {CallTest::kRedPayloadType, MediaType::VIDEO},
734 {CallTest::kRtxRedPayloadType, MediaType::VIDEO},
735 {CallTest::kUlpfecPayloadType, MediaType::VIDEO},
736 {CallTest::kFlexfecPayloadType, MediaType::VIDEO},
737 {CallTest::kAudioSendPayloadType, MediaType::AUDIO}};
738
BaseTest()739 BaseTest::BaseTest() {}
740
BaseTest(int timeout_ms)741 BaseTest::BaseTest(int timeout_ms) : RtpRtcpObserver(timeout_ms) {}
742
~BaseTest()743 BaseTest::~BaseTest() {}
744
CreateCapturer()745 std::unique_ptr<TestAudioDeviceModule::Capturer> BaseTest::CreateCapturer() {
746 return TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000);
747 }
748
CreateRenderer()749 std::unique_ptr<TestAudioDeviceModule::Renderer> BaseTest::CreateRenderer() {
750 return TestAudioDeviceModule::CreateDiscardRenderer(48000);
751 }
752
OnFakeAudioDevicesCreated(TestAudioDeviceModule * send_audio_device,TestAudioDeviceModule * recv_audio_device)753 void BaseTest::OnFakeAudioDevicesCreated(
754 TestAudioDeviceModule* send_audio_device,
755 TestAudioDeviceModule* recv_audio_device) {}
756
ModifySenderBitrateConfig(BitrateConstraints * bitrate_config)757 void BaseTest::ModifySenderBitrateConfig(BitrateConstraints* bitrate_config) {}
758
ModifyReceiverBitrateConfig(BitrateConstraints * bitrate_config)759 void BaseTest::ModifyReceiverBitrateConfig(BitrateConstraints* bitrate_config) {
760 }
761
OnCallsCreated(Call * sender_call,Call * receiver_call)762 void BaseTest::OnCallsCreated(Call* sender_call, Call* receiver_call) {}
763
CreateSendTransport(TaskQueueBase * task_queue,Call * sender_call)764 std::unique_ptr<PacketTransport> BaseTest::CreateSendTransport(
765 TaskQueueBase* task_queue,
766 Call* sender_call) {
767 return std::make_unique<PacketTransport>(
768 task_queue, sender_call, this, test::PacketTransport::kSender,
769 CallTest::payload_type_map_,
770 std::make_unique<FakeNetworkPipe>(
771 Clock::GetRealTimeClock(),
772 std::make_unique<SimulatedNetwork>(BuiltInNetworkBehaviorConfig())));
773 }
774
CreateReceiveTransport(TaskQueueBase * task_queue)775 std::unique_ptr<PacketTransport> BaseTest::CreateReceiveTransport(
776 TaskQueueBase* task_queue) {
777 return std::make_unique<PacketTransport>(
778 task_queue, nullptr, this, test::PacketTransport::kReceiver,
779 CallTest::payload_type_map_,
780 std::make_unique<FakeNetworkPipe>(
781 Clock::GetRealTimeClock(),
782 std::make_unique<SimulatedNetwork>(BuiltInNetworkBehaviorConfig())));
783 }
784
GetNumVideoStreams() const785 size_t BaseTest::GetNumVideoStreams() const {
786 return 1;
787 }
788
GetNumAudioStreams() const789 size_t BaseTest::GetNumAudioStreams() const {
790 return 0;
791 }
792
GetNumFlexfecStreams() const793 size_t BaseTest::GetNumFlexfecStreams() const {
794 return 0;
795 }
796
ModifyVideoConfigs(VideoSendStream::Config * send_config,std::vector<VideoReceiveStream::Config> * receive_configs,VideoEncoderConfig * encoder_config)797 void BaseTest::ModifyVideoConfigs(
798 VideoSendStream::Config* send_config,
799 std::vector<VideoReceiveStream::Config>* receive_configs,
800 VideoEncoderConfig* encoder_config) {}
801
ModifyVideoCaptureStartResolution(int * width,int * heigt,int * frame_rate)802 void BaseTest::ModifyVideoCaptureStartResolution(int* width,
803 int* heigt,
804 int* frame_rate) {}
805
ModifyVideoDegradationPreference(DegradationPreference * degradation_preference)806 void BaseTest::ModifyVideoDegradationPreference(
807 DegradationPreference* degradation_preference) {}
808
OnVideoStreamsCreated(VideoSendStream * send_stream,const std::vector<VideoReceiveStream * > & receive_streams)809 void BaseTest::OnVideoStreamsCreated(
810 VideoSendStream* send_stream,
811 const std::vector<VideoReceiveStream*>& receive_streams) {}
812
ModifyAudioConfigs(AudioSendStream::Config * send_config,std::vector<AudioReceiveStream::Config> * receive_configs)813 void BaseTest::ModifyAudioConfigs(
814 AudioSendStream::Config* send_config,
815 std::vector<AudioReceiveStream::Config>* receive_configs) {}
816
OnAudioStreamsCreated(AudioSendStream * send_stream,const std::vector<AudioReceiveStream * > & receive_streams)817 void BaseTest::OnAudioStreamsCreated(
818 AudioSendStream* send_stream,
819 const std::vector<AudioReceiveStream*>& receive_streams) {}
820
ModifyFlexfecConfigs(std::vector<FlexfecReceiveStream::Config> * receive_configs)821 void BaseTest::ModifyFlexfecConfigs(
822 std::vector<FlexfecReceiveStream::Config>* receive_configs) {}
823
OnFlexfecStreamsCreated(const std::vector<FlexfecReceiveStream * > & receive_streams)824 void BaseTest::OnFlexfecStreamsCreated(
825 const std::vector<FlexfecReceiveStream*>& receive_streams) {}
826
OnFrameGeneratorCapturerCreated(FrameGeneratorCapturer * frame_generator_capturer)827 void BaseTest::OnFrameGeneratorCapturerCreated(
828 FrameGeneratorCapturer* frame_generator_capturer) {}
829
OnStreamsStopped()830 void BaseTest::OnStreamsStopped() {}
831
SendTest(int timeout_ms)832 SendTest::SendTest(int timeout_ms) : BaseTest(timeout_ms) {}
833
ShouldCreateReceivers() const834 bool SendTest::ShouldCreateReceivers() const {
835 return false;
836 }
837
EndToEndTest()838 EndToEndTest::EndToEndTest() {}
839
EndToEndTest(int timeout_ms)840 EndToEndTest::EndToEndTest(int timeout_ms) : BaseTest(timeout_ms) {}
841
ShouldCreateReceivers() const842 bool EndToEndTest::ShouldCreateReceivers() const {
843 return true;
844 }
845
846 } // namespace test
847 } // namespace webrtc
848