• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "call/fake_network_pipe.h"
23 #include "call/simulated_network.h"
24 #include "modules/audio_mixer/audio_mixer_impl.h"
25 #include "rtc_base/checks.h"
26 #include "rtc_base/event.h"
27 #include "rtc_base/task_queue_for_test.h"
28 #include "test/fake_encoder.h"
29 #include "test/testsupport/file_utils.h"
30 #include "video/config/video_encoder_config.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       }),
__anon8f278b2a0202() 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(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(&degradation_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(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(VideoReceiveStreamInterface::Config * receive_config)311 void CallTest::SetReceiveUlpFecConfig(
312     VideoReceiveStreamInterface::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<VideoReceiveStreamInterface::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<VideoReceiveStreamInterface::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   VideoReceiveStreamInterface::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     VideoReceiveStreamInterface::Config video_recv_config(
378         default_config.Copy());
379     video_recv_config.decoders.clear();
380     if (!video_send_config.rtp.rtx.ssrcs.empty()) {
381       video_recv_config.rtp.rtx_ssrc = video_send_config.rtp.rtx.ssrcs[i];
382       video_recv_config.rtp.rtx_associated_payload_types[kSendRtxPayloadType] =
383           video_send_config.rtp.payload_type;
384     }
385     video_recv_config.rtp.remote_ssrc = video_send_config.rtp.ssrcs[i];
386     VideoReceiveStreamInterface::Decoder decoder;
387 
388     decoder.payload_type = video_send_config.rtp.payload_type;
389     decoder.video_format = SdpVideoFormat(video_send_config.rtp.payload_name);
390     // Force fake decoders on non-selected simulcast streams.
391     if (!decode_sub_stream || i == *decode_sub_stream) {
392       video_recv_config.decoder_factory = decoder_factory;
393     } else {
394       video_recv_config.decoder_factory = &fake_decoder_factory_;
395     }
396     video_recv_config.decoders.push_back(decoder);
397     receive_configs->emplace_back(std::move(video_recv_config));
398   }
399 }
400 
CreateMatchingAudioAndFecConfigs(Transport * rtcp_send_transport)401 void CallTest::CreateMatchingAudioAndFecConfigs(
402     Transport* rtcp_send_transport) {
403   RTC_DCHECK_GE(1, num_audio_streams_);
404   if (num_audio_streams_ == 1) {
405     CreateMatchingAudioConfigs(rtcp_send_transport, "");
406   }
407 
408   // TODO(brandtr): Update this when we support multistream protection.
409   RTC_DCHECK(num_flexfec_streams_ <= 1);
410   if (num_flexfec_streams_ == 1) {
411     CreateMatchingFecConfig(rtcp_send_transport, *GetVideoSendConfig());
412     for (const RtpExtension& extension : GetVideoSendConfig()->rtp.extensions)
413       GetFlexFecConfig()->rtp.extensions.push_back(extension);
414   }
415 }
416 
CreateMatchingAudioConfigs(Transport * transport,std::string sync_group)417 void CallTest::CreateMatchingAudioConfigs(Transport* transport,
418                                           std::string sync_group) {
419   audio_receive_configs_.push_back(CreateMatchingAudioConfig(
420       audio_send_config_, audio_decoder_factory_, transport, sync_group));
421 }
422 
CreateMatchingAudioConfig(const AudioSendStream::Config & send_config,rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory,Transport * transport,std::string sync_group)423 AudioReceiveStreamInterface::Config CallTest::CreateMatchingAudioConfig(
424     const AudioSendStream::Config& send_config,
425     rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory,
426     Transport* transport,
427     std::string sync_group) {
428   AudioReceiveStreamInterface::Config audio_config;
429   audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
430   audio_config.rtcp_send_transport = transport;
431   audio_config.rtp.remote_ssrc = send_config.rtp.ssrc;
432   audio_config.rtp.transport_cc =
433       send_config.send_codec_spec
434           ? send_config.send_codec_spec->transport_cc_enabled
435           : false;
436   audio_config.rtp.extensions = send_config.rtp.extensions;
437   audio_config.decoder_factory = audio_decoder_factory;
438   audio_config.decoder_map = {{kAudioSendPayloadType, {"opus", 48000, 2}}};
439   audio_config.sync_group = sync_group;
440   return audio_config;
441 }
442 
CreateMatchingFecConfig(Transport * transport,const VideoSendStream::Config & send_config)443 void CallTest::CreateMatchingFecConfig(
444     Transport* transport,
445     const VideoSendStream::Config& send_config) {
446   FlexfecReceiveStream::Config config(transport);
447   config.payload_type = send_config.rtp.flexfec.payload_type;
448   config.rtp.remote_ssrc = send_config.rtp.flexfec.ssrc;
449   config.protected_media_ssrcs = send_config.rtp.flexfec.protected_media_ssrcs;
450   config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
451   if (!video_receive_configs_.empty()) {
452     video_receive_configs_[0].rtp.protected_by_flexfec = true;
453     video_receive_configs_[0].rtp.packet_sink_ = this;
454   }
455   flexfec_receive_configs_.push_back(config);
456 }
457 
CreateMatchingReceiveConfigs(Transport * rtcp_send_transport)458 void CallTest::CreateMatchingReceiveConfigs(Transport* rtcp_send_transport) {
459   video_receive_configs_.clear();
460   for (VideoSendStream::Config& video_send_config : video_send_configs_) {
461     CreateMatchingVideoReceiveConfigs(video_send_config, rtcp_send_transport);
462   }
463   CreateMatchingAudioAndFecConfigs(rtcp_send_transport);
464 }
465 
CreateFrameGeneratorCapturerWithDrift(Clock * clock,float speed,int framerate,int width,int height)466 void CallTest::CreateFrameGeneratorCapturerWithDrift(Clock* clock,
467                                                      float speed,
468                                                      int framerate,
469                                                      int width,
470                                                      int height) {
471   video_sources_.clear();
472   auto frame_generator_capturer =
473       std::make_unique<test::FrameGeneratorCapturer>(
474           clock,
475           test::CreateSquareFrameGenerator(width, height, absl::nullopt,
476                                            absl::nullopt),
477           framerate * speed, *task_queue_factory_);
478   frame_generator_capturer_ = frame_generator_capturer.get();
479   frame_generator_capturer->Init();
480   video_sources_.push_back(std::move(frame_generator_capturer));
481   ConnectVideoSourcesToStreams();
482 }
483 
CreateFrameGeneratorCapturer(int framerate,int width,int height)484 void CallTest::CreateFrameGeneratorCapturer(int framerate,
485                                             int width,
486                                             int height) {
487   video_sources_.clear();
488   auto frame_generator_capturer =
489       std::make_unique<test::FrameGeneratorCapturer>(
490           clock_,
491           test::CreateSquareFrameGenerator(width, height, absl::nullopt,
492                                            absl::nullopt),
493           framerate, *task_queue_factory_);
494   frame_generator_capturer_ = frame_generator_capturer.get();
495   frame_generator_capturer->Init();
496   video_sources_.push_back(std::move(frame_generator_capturer));
497   ConnectVideoSourcesToStreams();
498 }
499 
CreateFakeAudioDevices(std::unique_ptr<TestAudioDeviceModule::Capturer> capturer,std::unique_ptr<TestAudioDeviceModule::Renderer> renderer)500 void CallTest::CreateFakeAudioDevices(
501     std::unique_ptr<TestAudioDeviceModule::Capturer> capturer,
502     std::unique_ptr<TestAudioDeviceModule::Renderer> renderer) {
503   fake_send_audio_device_ = TestAudioDeviceModule::Create(
504       task_queue_factory_.get(), std::move(capturer), nullptr, 1.f);
505   fake_recv_audio_device_ = TestAudioDeviceModule::Create(
506       task_queue_factory_.get(), nullptr, std::move(renderer), 1.f);
507 }
508 
CreateVideoStreams()509 void CallTest::CreateVideoStreams() {
510   RTC_DCHECK(video_receive_streams_.empty());
511   CreateVideoSendStreams();
512   for (size_t i = 0; i < video_receive_configs_.size(); ++i) {
513     video_receive_streams_.push_back(receiver_call_->CreateVideoReceiveStream(
514         video_receive_configs_[i].Copy()));
515   }
516 }
517 
CreateVideoSendStreams()518 void CallTest::CreateVideoSendStreams() {
519   RTC_DCHECK(video_send_streams_.empty());
520 
521   // We currently only support testing external fec controllers with a single
522   // VideoSendStream.
523   if (fec_controller_factory_.get()) {
524     RTC_DCHECK_LE(video_send_configs_.size(), 1);
525   }
526 
527   // TODO(http://crbug/818127):
528   // Remove this workaround when ALR is not screenshare-specific.
529   std::list<size_t> streams_creation_order;
530   for (size_t i = 0; i < video_send_configs_.size(); ++i) {
531     // If dual streams are created, add the screenshare stream last.
532     if (video_encoder_configs_[i].content_type ==
533         VideoEncoderConfig::ContentType::kScreen) {
534       streams_creation_order.push_back(i);
535     } else {
536       streams_creation_order.push_front(i);
537     }
538   }
539 
540   video_send_streams_.resize(video_send_configs_.size(), nullptr);
541 
542   for (size_t i : streams_creation_order) {
543     if (fec_controller_factory_.get()) {
544       video_send_streams_[i] = sender_call_->CreateVideoSendStream(
545           video_send_configs_[i].Copy(), video_encoder_configs_[i].Copy(),
546           fec_controller_factory_->CreateFecController());
547     } else {
548       video_send_streams_[i] = sender_call_->CreateVideoSendStream(
549           video_send_configs_[i].Copy(), video_encoder_configs_[i].Copy());
550     }
551   }
552 }
553 
CreateVideoSendStream(const VideoEncoderConfig & encoder_config)554 void CallTest::CreateVideoSendStream(const VideoEncoderConfig& encoder_config) {
555   RTC_DCHECK(video_send_streams_.empty());
556   video_send_streams_.push_back(sender_call_->CreateVideoSendStream(
557       GetVideoSendConfig()->Copy(), encoder_config.Copy()));
558 }
559 
CreateAudioStreams()560 void CallTest::CreateAudioStreams() {
561   RTC_DCHECK(audio_send_stream_ == nullptr);
562   RTC_DCHECK(audio_receive_streams_.empty());
563   audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
564   for (size_t i = 0; i < audio_receive_configs_.size(); ++i) {
565     audio_receive_streams_.push_back(
566         receiver_call_->CreateAudioReceiveStream(audio_receive_configs_[i]));
567   }
568 }
569 
CreateFlexfecStreams()570 void CallTest::CreateFlexfecStreams() {
571   for (size_t i = 0; i < flexfec_receive_configs_.size(); ++i) {
572     flexfec_receive_streams_.push_back(
573         receiver_call_->CreateFlexfecReceiveStream(
574             flexfec_receive_configs_[i]));
575   }
576 }
577 
ConnectVideoSourcesToStreams()578 void CallTest::ConnectVideoSourcesToStreams() {
579   for (size_t i = 0; i < video_sources_.size(); ++i)
580     video_send_streams_[i]->SetSource(video_sources_[i].get(),
581                                       degradation_preference_);
582 }
583 
Start()584 void CallTest::Start() {
585   StartVideoStreams();
586   if (audio_send_stream_) {
587     audio_send_stream_->Start();
588   }
589   for (AudioReceiveStreamInterface* audio_recv_stream : audio_receive_streams_)
590     audio_recv_stream->Start();
591 }
592 
StartVideoStreams()593 void CallTest::StartVideoStreams() {
594   for (size_t i = 0; i < video_send_streams_.size(); ++i) {
595     std::vector<bool> active_rtp_streams(
596         video_send_configs_[i].rtp.ssrcs.size(), true);
597     video_send_streams_[i]->StartPerRtpStream(active_rtp_streams);
598   }
599   for (VideoReceiveStreamInterface* video_recv_stream : video_receive_streams_)
600     video_recv_stream->Start();
601 }
602 
Stop()603 void CallTest::Stop() {
604   for (AudioReceiveStreamInterface* audio_recv_stream : audio_receive_streams_)
605     audio_recv_stream->Stop();
606   if (audio_send_stream_) {
607     audio_send_stream_->Stop();
608   }
609   StopVideoStreams();
610 }
611 
StopVideoStreams()612 void CallTest::StopVideoStreams() {
613   for (VideoSendStream* video_send_stream : video_send_streams_)
614     video_send_stream->Stop();
615   for (VideoReceiveStreamInterface* video_recv_stream : video_receive_streams_)
616     video_recv_stream->Stop();
617 }
618 
DestroyStreams()619 void CallTest::DestroyStreams() {
620   if (audio_send_stream_)
621     sender_call_->DestroyAudioSendStream(audio_send_stream_);
622   audio_send_stream_ = nullptr;
623   for (AudioReceiveStreamInterface* audio_recv_stream : audio_receive_streams_)
624     receiver_call_->DestroyAudioReceiveStream(audio_recv_stream);
625 
626   DestroyVideoSendStreams();
627 
628   for (VideoReceiveStreamInterface* video_recv_stream : video_receive_streams_)
629     receiver_call_->DestroyVideoReceiveStream(video_recv_stream);
630 
631   for (FlexfecReceiveStream* flexfec_recv_stream : flexfec_receive_streams_)
632     receiver_call_->DestroyFlexfecReceiveStream(flexfec_recv_stream);
633 
634   video_receive_streams_.clear();
635   video_sources_.clear();
636 }
637 
DestroyVideoSendStreams()638 void CallTest::DestroyVideoSendStreams() {
639   for (VideoSendStream* video_send_stream : video_send_streams_)
640     sender_call_->DestroyVideoSendStream(video_send_stream);
641   video_send_streams_.clear();
642 }
643 
SetFakeVideoCaptureRotation(VideoRotation rotation)644 void CallTest::SetFakeVideoCaptureRotation(VideoRotation rotation) {
645   frame_generator_capturer_->SetFakeRotation(rotation);
646 }
647 
SetVideoDegradation(DegradationPreference preference)648 void CallTest::SetVideoDegradation(DegradationPreference preference) {
649   GetVideoSendStream()->SetSource(frame_generator_capturer_, preference);
650 }
651 
GetVideoSendConfig()652 VideoSendStream::Config* CallTest::GetVideoSendConfig() {
653   return &video_send_configs_[0];
654 }
655 
SetVideoSendConfig(const VideoSendStream::Config & config)656 void CallTest::SetVideoSendConfig(const VideoSendStream::Config& config) {
657   video_send_configs_.clear();
658   video_send_configs_.push_back(config.Copy());
659 }
660 
GetVideoEncoderConfig()661 VideoEncoderConfig* CallTest::GetVideoEncoderConfig() {
662   return &video_encoder_configs_[0];
663 }
664 
SetVideoEncoderConfig(const VideoEncoderConfig & config)665 void CallTest::SetVideoEncoderConfig(const VideoEncoderConfig& config) {
666   video_encoder_configs_.clear();
667   video_encoder_configs_.push_back(config.Copy());
668 }
669 
GetVideoSendStream()670 VideoSendStream* CallTest::GetVideoSendStream() {
671   return video_send_streams_[0];
672 }
GetFlexFecConfig()673 FlexfecReceiveStream::Config* CallTest::GetFlexFecConfig() {
674   return &flexfec_receive_configs_[0];
675 }
676 
OnRtpPacket(const RtpPacketReceived & packet)677 void CallTest::OnRtpPacket(const RtpPacketReceived& packet) {
678   // All FlexFEC streams protect all of the video streams.
679   for (FlexfecReceiveStream* flexfec_recv_stream : flexfec_receive_streams_)
680     flexfec_recv_stream->OnRtpPacket(packet);
681 }
682 
GetRtpExtensionByUri(const std::string & uri) const683 absl::optional<RtpExtension> CallTest::GetRtpExtensionByUri(
684     const std::string& uri) const {
685   for (const auto& extension : rtp_extensions_) {
686     if (extension.uri == uri) {
687       return extension;
688     }
689   }
690   return absl::nullopt;
691 }
692 
AddRtpExtensionByUri(const std::string & uri,std::vector<RtpExtension> * extensions) const693 void CallTest::AddRtpExtensionByUri(
694     const std::string& uri,
695     std::vector<RtpExtension>* extensions) const {
696   const absl::optional<RtpExtension> extension = GetRtpExtensionByUri(uri);
697   if (extension) {
698     extensions->push_back(*extension);
699   }
700 }
701 
702 constexpr size_t CallTest::kNumSsrcs;
703 const int CallTest::kDefaultWidth;
704 const int CallTest::kDefaultHeight;
705 const int CallTest::kDefaultFramerate;
706 const uint32_t CallTest::kSendRtxSsrcs[kNumSsrcs] = {
707     0xBADCAFD, 0xBADCAFE, 0xBADCAFF, 0xBADCB00, 0xBADCB01, 0xBADCB02};
708 const uint32_t CallTest::kVideoSendSsrcs[kNumSsrcs] = {
709     0xC0FFED, 0xC0FFEE, 0xC0FFEF, 0xC0FFF0, 0xC0FFF1, 0xC0FFF2};
710 const uint32_t CallTest::kAudioSendSsrc = 0xDEADBEEF;
711 const uint32_t CallTest::kFlexfecSendSsrc = 0xBADBEEF;
712 const uint32_t CallTest::kReceiverLocalVideoSsrc = 0x123456;
713 const uint32_t CallTest::kReceiverLocalAudioSsrc = 0x1234567;
714 const int CallTest::kNackRtpHistoryMs = 1000;
715 
716 const std::map<uint8_t, MediaType> CallTest::payload_type_map_ = {
717     {CallTest::kVideoSendPayloadType, MediaType::VIDEO},
718     {CallTest::kFakeVideoSendPayloadType, MediaType::VIDEO},
719     {CallTest::kSendRtxPayloadType, MediaType::VIDEO},
720     {CallTest::kRedPayloadType, MediaType::VIDEO},
721     {CallTest::kRtxRedPayloadType, MediaType::VIDEO},
722     {CallTest::kUlpfecPayloadType, MediaType::VIDEO},
723     {CallTest::kFlexfecPayloadType, MediaType::VIDEO},
724     {CallTest::kAudioSendPayloadType, MediaType::AUDIO}};
725 
BaseTest()726 BaseTest::BaseTest() {}
727 
BaseTest(TimeDelta timeout)728 BaseTest::BaseTest(TimeDelta timeout) : RtpRtcpObserver(timeout) {}
729 
~BaseTest()730 BaseTest::~BaseTest() {}
731 
CreateCapturer()732 std::unique_ptr<TestAudioDeviceModule::Capturer> BaseTest::CreateCapturer() {
733   return TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000);
734 }
735 
CreateRenderer()736 std::unique_ptr<TestAudioDeviceModule::Renderer> BaseTest::CreateRenderer() {
737   return TestAudioDeviceModule::CreateDiscardRenderer(48000);
738 }
739 
OnFakeAudioDevicesCreated(TestAudioDeviceModule * send_audio_device,TestAudioDeviceModule * recv_audio_device)740 void BaseTest::OnFakeAudioDevicesCreated(
741     TestAudioDeviceModule* send_audio_device,
742     TestAudioDeviceModule* recv_audio_device) {}
743 
ModifySenderBitrateConfig(BitrateConstraints * bitrate_config)744 void BaseTest::ModifySenderBitrateConfig(BitrateConstraints* bitrate_config) {}
745 
ModifyReceiverBitrateConfig(BitrateConstraints * bitrate_config)746 void BaseTest::ModifyReceiverBitrateConfig(BitrateConstraints* bitrate_config) {
747 }
748 
OnCallsCreated(Call * sender_call,Call * receiver_call)749 void BaseTest::OnCallsCreated(Call* sender_call, Call* receiver_call) {}
750 
CreateSendTransport(TaskQueueBase * task_queue,Call * sender_call)751 std::unique_ptr<PacketTransport> BaseTest::CreateSendTransport(
752     TaskQueueBase* task_queue,
753     Call* sender_call) {
754   return std::make_unique<PacketTransport>(
755       task_queue, sender_call, this, test::PacketTransport::kSender,
756       CallTest::payload_type_map_,
757       std::make_unique<FakeNetworkPipe>(
758           Clock::GetRealTimeClock(),
759           std::make_unique<SimulatedNetwork>(BuiltInNetworkBehaviorConfig())));
760 }
761 
CreateReceiveTransport(TaskQueueBase * task_queue)762 std::unique_ptr<PacketTransport> BaseTest::CreateReceiveTransport(
763     TaskQueueBase* task_queue) {
764   return std::make_unique<PacketTransport>(
765       task_queue, nullptr, this, test::PacketTransport::kReceiver,
766       CallTest::payload_type_map_,
767       std::make_unique<FakeNetworkPipe>(
768           Clock::GetRealTimeClock(),
769           std::make_unique<SimulatedNetwork>(BuiltInNetworkBehaviorConfig())));
770 }
771 
GetNumVideoStreams() const772 size_t BaseTest::GetNumVideoStreams() const {
773   return 1;
774 }
775 
GetNumAudioStreams() const776 size_t BaseTest::GetNumAudioStreams() const {
777   return 0;
778 }
779 
GetNumFlexfecStreams() const780 size_t BaseTest::GetNumFlexfecStreams() const {
781   return 0;
782 }
783 
ModifyVideoConfigs(VideoSendStream::Config * send_config,std::vector<VideoReceiveStreamInterface::Config> * receive_configs,VideoEncoderConfig * encoder_config)784 void BaseTest::ModifyVideoConfigs(
785     VideoSendStream::Config* send_config,
786     std::vector<VideoReceiveStreamInterface::Config>* receive_configs,
787     VideoEncoderConfig* encoder_config) {}
788 
ModifyVideoCaptureStartResolution(int * width,int * heigt,int * frame_rate)789 void BaseTest::ModifyVideoCaptureStartResolution(int* width,
790                                                  int* heigt,
791                                                  int* frame_rate) {}
792 
ModifyVideoDegradationPreference(DegradationPreference * degradation_preference)793 void BaseTest::ModifyVideoDegradationPreference(
794     DegradationPreference* degradation_preference) {}
795 
OnVideoStreamsCreated(VideoSendStream * send_stream,const std::vector<VideoReceiveStreamInterface * > & receive_streams)796 void BaseTest::OnVideoStreamsCreated(
797     VideoSendStream* send_stream,
798     const std::vector<VideoReceiveStreamInterface*>& receive_streams) {}
799 
ModifyAudioConfigs(AudioSendStream::Config * send_config,std::vector<AudioReceiveStreamInterface::Config> * receive_configs)800 void BaseTest::ModifyAudioConfigs(
801     AudioSendStream::Config* send_config,
802     std::vector<AudioReceiveStreamInterface::Config>* receive_configs) {}
803 
OnAudioStreamsCreated(AudioSendStream * send_stream,const std::vector<AudioReceiveStreamInterface * > & receive_streams)804 void BaseTest::OnAudioStreamsCreated(
805     AudioSendStream* send_stream,
806     const std::vector<AudioReceiveStreamInterface*>& receive_streams) {}
807 
ModifyFlexfecConfigs(std::vector<FlexfecReceiveStream::Config> * receive_configs)808 void BaseTest::ModifyFlexfecConfigs(
809     std::vector<FlexfecReceiveStream::Config>* receive_configs) {}
810 
OnFlexfecStreamsCreated(const std::vector<FlexfecReceiveStream * > & receive_streams)811 void BaseTest::OnFlexfecStreamsCreated(
812     const std::vector<FlexfecReceiveStream*>& receive_streams) {}
813 
OnFrameGeneratorCapturerCreated(FrameGeneratorCapturer * frame_generator_capturer)814 void BaseTest::OnFrameGeneratorCapturerCreated(
815     FrameGeneratorCapturer* frame_generator_capturer) {}
816 
OnStreamsStopped()817 void BaseTest::OnStreamsStopped() {}
818 
SendTest(TimeDelta timeout)819 SendTest::SendTest(TimeDelta timeout) : BaseTest(timeout) {}
820 
ShouldCreateReceivers() const821 bool SendTest::ShouldCreateReceivers() const {
822   return false;
823 }
824 
EndToEndTest()825 EndToEndTest::EndToEndTest() {}
826 
EndToEndTest(TimeDelta timeout)827 EndToEndTest::EndToEndTest(TimeDelta timeout) : BaseTest(timeout) {}
828 
ShouldCreateReceivers() const829 bool EndToEndTest::ShouldCreateReceivers() const {
830   return true;
831 }
832 
833 }  // namespace test
834 }  // namespace webrtc
835