Home | History | Annotate | Download | only in test
      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 #include "webrtc/base/checks.h"
     11 #include "webrtc/common.h"
     12 #include "webrtc/config.h"
     13 #include "webrtc/test/call_test.h"
     14 #include "webrtc/test/encoder_settings.h"
     15 #include "webrtc/test/testsupport/fileutils.h"
     16 #include "webrtc/voice_engine/include/voe_base.h"
     17 #include "webrtc/voice_engine/include/voe_codec.h"
     18 #include "webrtc/voice_engine/include/voe_network.h"
     19 
     20 namespace webrtc {
     21 namespace test {
     22 
     23 namespace {
     24 const int kVideoRotationRtpExtensionId = 4;
     25 }
     26 
     27 CallTest::CallTest()
     28     : clock_(Clock::GetRealTimeClock()),
     29       video_send_config_(nullptr),
     30       video_send_stream_(nullptr),
     31       audio_send_config_(nullptr),
     32       audio_send_stream_(nullptr),
     33       fake_encoder_(clock_),
     34       num_video_streams_(1),
     35       num_audio_streams_(0),
     36       fake_send_audio_device_(nullptr),
     37       fake_recv_audio_device_(nullptr) {}
     38 
     39 CallTest::~CallTest() {
     40 }
     41 
     42 void CallTest::RunBaseTest(BaseTest* test) {
     43   num_video_streams_ = test->GetNumVideoStreams();
     44   num_audio_streams_ = test->GetNumAudioStreams();
     45   RTC_DCHECK(num_video_streams_ > 0 || num_audio_streams_ > 0);
     46   Call::Config send_config(test->GetSenderCallConfig());
     47   if (num_audio_streams_ > 0) {
     48     CreateVoiceEngines();
     49     AudioState::Config audio_state_config;
     50     audio_state_config.voice_engine = voe_send_.voice_engine;
     51     send_config.audio_state = AudioState::Create(audio_state_config);
     52   }
     53   CreateSenderCall(send_config);
     54   if (test->ShouldCreateReceivers()) {
     55     Call::Config recv_config(test->GetReceiverCallConfig());
     56     if (num_audio_streams_ > 0) {
     57       AudioState::Config audio_state_config;
     58       audio_state_config.voice_engine = voe_recv_.voice_engine;
     59       recv_config.audio_state = AudioState::Create(audio_state_config);
     60     }
     61     CreateReceiverCall(recv_config);
     62   }
     63   test->OnCallsCreated(sender_call_.get(), receiver_call_.get());
     64   send_transport_.reset(test->CreateSendTransport(sender_call_.get()));
     65   receive_transport_.reset(test->CreateReceiveTransport());
     66 
     67   if (test->ShouldCreateReceivers()) {
     68     send_transport_->SetReceiver(receiver_call_->Receiver());
     69     receive_transport_->SetReceiver(sender_call_->Receiver());
     70   } else {
     71     // Sender-only call delivers to itself.
     72     send_transport_->SetReceiver(sender_call_->Receiver());
     73     receive_transport_->SetReceiver(nullptr);
     74   }
     75 
     76   CreateSendConfig(num_video_streams_, num_audio_streams_,
     77                    send_transport_.get());
     78   if (test->ShouldCreateReceivers()) {
     79     CreateMatchingReceiveConfigs(receive_transport_.get());
     80   }
     81   if (num_audio_streams_ > 0)
     82     SetupVoiceEngineTransports(send_transport_.get(), receive_transport_.get());
     83 
     84   if (num_video_streams_ > 0) {
     85     test->ModifyVideoConfigs(&video_send_config_, &video_receive_configs_,
     86                              &video_encoder_config_);
     87   }
     88   if (num_audio_streams_ > 0)
     89     test->ModifyAudioConfigs(&audio_send_config_, &audio_receive_configs_);
     90 
     91   if (num_video_streams_ > 0) {
     92     CreateVideoStreams();
     93     test->OnVideoStreamsCreated(video_send_stream_, video_receive_streams_);
     94   }
     95   if (num_audio_streams_ > 0) {
     96     CreateAudioStreams();
     97     test->OnAudioStreamsCreated(audio_send_stream_, audio_receive_streams_);
     98   }
     99 
    100   if (num_video_streams_ > 0) {
    101     CreateFrameGeneratorCapturer();
    102     test->OnFrameGeneratorCapturerCreated(frame_generator_capturer_.get());
    103   }
    104 
    105   Start();
    106   test->PerformTest();
    107   send_transport_->StopSending();
    108   receive_transport_->StopSending();
    109   Stop();
    110 
    111   DestroyStreams();
    112   DestroyCalls();
    113   if (num_audio_streams_ > 0)
    114     DestroyVoiceEngines();
    115 }
    116 
    117 void CallTest::Start() {
    118   if (video_send_stream_)
    119     video_send_stream_->Start();
    120   for (VideoReceiveStream* video_recv_stream : video_receive_streams_)
    121     video_recv_stream->Start();
    122   if (audio_send_stream_) {
    123     fake_send_audio_device_->Start();
    124     audio_send_stream_->Start();
    125     EXPECT_EQ(0, voe_send_.base->StartSend(voe_send_.channel_id));
    126   }
    127   for (AudioReceiveStream* audio_recv_stream : audio_receive_streams_)
    128     audio_recv_stream->Start();
    129   if (!audio_receive_streams_.empty()) {
    130     fake_recv_audio_device_->Start();
    131     EXPECT_EQ(0, voe_recv_.base->StartPlayout(voe_recv_.channel_id));
    132     EXPECT_EQ(0, voe_recv_.base->StartReceive(voe_recv_.channel_id));
    133   }
    134   if (frame_generator_capturer_.get() != NULL)
    135     frame_generator_capturer_->Start();
    136 }
    137 
    138 void CallTest::Stop() {
    139   if (frame_generator_capturer_.get() != NULL)
    140     frame_generator_capturer_->Stop();
    141   if (!audio_receive_streams_.empty()) {
    142     fake_recv_audio_device_->Stop();
    143     EXPECT_EQ(0, voe_recv_.base->StopReceive(voe_recv_.channel_id));
    144     EXPECT_EQ(0, voe_recv_.base->StopPlayout(voe_recv_.channel_id));
    145   }
    146   for (AudioReceiveStream* audio_recv_stream : audio_receive_streams_)
    147     audio_recv_stream->Stop();
    148   if (audio_send_stream_) {
    149     fake_send_audio_device_->Stop();
    150     EXPECT_EQ(0, voe_send_.base->StopSend(voe_send_.channel_id));
    151     audio_send_stream_->Stop();
    152   }
    153   for (VideoReceiveStream* video_recv_stream : video_receive_streams_)
    154     video_recv_stream->Stop();
    155   if (video_send_stream_)
    156     video_send_stream_->Stop();
    157 }
    158 
    159 void CallTest::CreateCalls(const Call::Config& sender_config,
    160                            const Call::Config& receiver_config) {
    161   CreateSenderCall(sender_config);
    162   CreateReceiverCall(receiver_config);
    163 }
    164 
    165 void CallTest::CreateSenderCall(const Call::Config& config) {
    166   sender_call_.reset(Call::Create(config));
    167 }
    168 
    169 void CallTest::CreateReceiverCall(const Call::Config& config) {
    170   receiver_call_.reset(Call::Create(config));
    171 }
    172 
    173 void CallTest::DestroyCalls() {
    174   sender_call_.reset();
    175   receiver_call_.reset();
    176 }
    177 
    178 void CallTest::CreateSendConfig(size_t num_video_streams,
    179                                 size_t num_audio_streams,
    180                                 Transport* send_transport) {
    181   RTC_DCHECK(num_video_streams <= kNumSsrcs);
    182   RTC_DCHECK_LE(num_audio_streams, 1u);
    183   RTC_DCHECK(num_audio_streams == 0 || voe_send_.channel_id >= 0);
    184   if (num_video_streams > 0) {
    185     video_send_config_ = VideoSendStream::Config(send_transport);
    186     video_send_config_.encoder_settings.encoder = &fake_encoder_;
    187     video_send_config_.encoder_settings.payload_name = "FAKE";
    188     video_send_config_.encoder_settings.payload_type =
    189         kFakeVideoSendPayloadType;
    190     video_send_config_.rtp.extensions.push_back(
    191         RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeExtensionId));
    192     video_encoder_config_.streams = test::CreateVideoStreams(num_video_streams);
    193     for (size_t i = 0; i < num_video_streams; ++i)
    194       video_send_config_.rtp.ssrcs.push_back(kVideoSendSsrcs[i]);
    195     video_send_config_.rtp.extensions.push_back(RtpExtension(
    196         RtpExtension::kVideoRotation, kVideoRotationRtpExtensionId));
    197   }
    198 
    199   if (num_audio_streams > 0) {
    200     audio_send_config_ = AudioSendStream::Config(send_transport);
    201     audio_send_config_.voe_channel_id = voe_send_.channel_id;
    202     audio_send_config_.rtp.ssrc = kAudioSendSsrc;
    203   }
    204 }
    205 
    206 void CallTest::CreateMatchingReceiveConfigs(Transport* rtcp_send_transport) {
    207   RTC_DCHECK(video_receive_configs_.empty());
    208   RTC_DCHECK(allocated_decoders_.empty());
    209   if (num_video_streams_ > 0) {
    210     RTC_DCHECK(!video_send_config_.rtp.ssrcs.empty());
    211     VideoReceiveStream::Config video_config(rtcp_send_transport);
    212     video_config.rtp.remb = true;
    213     video_config.rtp.local_ssrc = kReceiverLocalVideoSsrc;
    214     for (const RtpExtension& extension : video_send_config_.rtp.extensions)
    215       video_config.rtp.extensions.push_back(extension);
    216     for (size_t i = 0; i < video_send_config_.rtp.ssrcs.size(); ++i) {
    217       VideoReceiveStream::Decoder decoder =
    218           test::CreateMatchingDecoder(video_send_config_.encoder_settings);
    219       allocated_decoders_.push_back(decoder.decoder);
    220       video_config.decoders.clear();
    221       video_config.decoders.push_back(decoder);
    222       video_config.rtp.remote_ssrc = video_send_config_.rtp.ssrcs[i];
    223       video_receive_configs_.push_back(video_config);
    224     }
    225   }
    226 
    227   RTC_DCHECK(num_audio_streams_ <= 1);
    228   if (num_audio_streams_ == 1) {
    229     RTC_DCHECK(voe_send_.channel_id >= 0);
    230     AudioReceiveStream::Config audio_config;
    231     audio_config.rtp.local_ssrc = kReceiverLocalAudioSsrc;
    232     audio_config.rtcp_send_transport = rtcp_send_transport;
    233     audio_config.voe_channel_id = voe_recv_.channel_id;
    234     audio_config.rtp.remote_ssrc = audio_send_config_.rtp.ssrc;
    235     audio_receive_configs_.push_back(audio_config);
    236   }
    237 }
    238 
    239 void CallTest::CreateFrameGeneratorCapturer() {
    240   VideoStream stream = video_encoder_config_.streams.back();
    241   frame_generator_capturer_.reset(test::FrameGeneratorCapturer::Create(
    242       video_send_stream_->Input(), stream.width, stream.height,
    243       stream.max_framerate, clock_));
    244 }
    245 
    246 void CallTest::CreateFakeAudioDevices() {
    247   fake_send_audio_device_.reset(new FakeAudioDevice(
    248       clock_, test::ResourcePath("voice_engine/audio_long16", "pcm")));
    249   fake_recv_audio_device_.reset(new FakeAudioDevice(
    250       clock_, test::ResourcePath("voice_engine/audio_long16", "pcm")));
    251 }
    252 
    253 void CallTest::CreateVideoStreams() {
    254   RTC_DCHECK(video_send_stream_ == nullptr);
    255   RTC_DCHECK(video_receive_streams_.empty());
    256   RTC_DCHECK(audio_send_stream_ == nullptr);
    257   RTC_DCHECK(audio_receive_streams_.empty());
    258 
    259   video_send_stream_ = sender_call_->CreateVideoSendStream(
    260       video_send_config_, video_encoder_config_);
    261   for (size_t i = 0; i < video_receive_configs_.size(); ++i) {
    262     video_receive_streams_.push_back(
    263         receiver_call_->CreateVideoReceiveStream(video_receive_configs_[i]));
    264   }
    265 }
    266 
    267 void CallTest::CreateAudioStreams() {
    268   audio_send_stream_ = sender_call_->CreateAudioSendStream(audio_send_config_);
    269   for (size_t i = 0; i < audio_receive_configs_.size(); ++i) {
    270     audio_receive_streams_.push_back(
    271         receiver_call_->CreateAudioReceiveStream(audio_receive_configs_[i]));
    272   }
    273   CodecInst isac = {kAudioSendPayloadType, "ISAC", 16000, 480, 1, 32000};
    274   EXPECT_EQ(0, voe_send_.codec->SetSendCodec(voe_send_.channel_id, isac));
    275 }
    276 
    277 void CallTest::DestroyStreams() {
    278   if (video_send_stream_)
    279     sender_call_->DestroyVideoSendStream(video_send_stream_);
    280   video_send_stream_ = nullptr;
    281   for (VideoReceiveStream* video_recv_stream : video_receive_streams_)
    282     receiver_call_->DestroyVideoReceiveStream(video_recv_stream);
    283 
    284   if (audio_send_stream_)
    285     sender_call_->DestroyAudioSendStream(audio_send_stream_);
    286   audio_send_stream_ = nullptr;
    287   for (AudioReceiveStream* audio_recv_stream : audio_receive_streams_)
    288     receiver_call_->DestroyAudioReceiveStream(audio_recv_stream);
    289   video_receive_streams_.clear();
    290 
    291   allocated_decoders_.clear();
    292 }
    293 
    294 void CallTest::CreateVoiceEngines() {
    295   CreateFakeAudioDevices();
    296   voe_send_.voice_engine = VoiceEngine::Create();
    297   voe_send_.base = VoEBase::GetInterface(voe_send_.voice_engine);
    298   voe_send_.network = VoENetwork::GetInterface(voe_send_.voice_engine);
    299   voe_send_.codec = VoECodec::GetInterface(voe_send_.voice_engine);
    300   EXPECT_EQ(0, voe_send_.base->Init(fake_send_audio_device_.get(), nullptr));
    301   Config voe_config;
    302   voe_config.Set<VoicePacing>(new VoicePacing(true));
    303   voe_send_.channel_id = voe_send_.base->CreateChannel(voe_config);
    304   EXPECT_GE(voe_send_.channel_id, 0);
    305 
    306   voe_recv_.voice_engine = VoiceEngine::Create();
    307   voe_recv_.base = VoEBase::GetInterface(voe_recv_.voice_engine);
    308   voe_recv_.network = VoENetwork::GetInterface(voe_recv_.voice_engine);
    309   voe_recv_.codec = VoECodec::GetInterface(voe_recv_.voice_engine);
    310   EXPECT_EQ(0, voe_recv_.base->Init(fake_recv_audio_device_.get(), nullptr));
    311   voe_recv_.channel_id = voe_recv_.base->CreateChannel();
    312   EXPECT_GE(voe_recv_.channel_id, 0);
    313 }
    314 
    315 void CallTest::SetupVoiceEngineTransports(PacketTransport* send_transport,
    316                                           PacketTransport* recv_transport) {
    317   voe_send_.transport_adapter.reset(
    318       new internal::TransportAdapter(send_transport));
    319   voe_send_.transport_adapter->Enable();
    320   EXPECT_EQ(0, voe_send_.network->RegisterExternalTransport(
    321                    voe_send_.channel_id, *voe_send_.transport_adapter.get()));
    322 
    323   voe_recv_.transport_adapter.reset(
    324       new internal::TransportAdapter(recv_transport));
    325   voe_recv_.transport_adapter->Enable();
    326   EXPECT_EQ(0, voe_recv_.network->RegisterExternalTransport(
    327                    voe_recv_.channel_id, *voe_recv_.transport_adapter.get()));
    328 }
    329 
    330 void CallTest::DestroyVoiceEngines() {
    331   voe_recv_.base->DeleteChannel(voe_recv_.channel_id);
    332   voe_recv_.channel_id = -1;
    333   voe_recv_.base->Release();
    334   voe_recv_.base = nullptr;
    335   voe_recv_.network->Release();
    336   voe_recv_.network = nullptr;
    337   voe_recv_.codec->Release();
    338   voe_recv_.codec = nullptr;
    339 
    340   voe_send_.base->DeleteChannel(voe_send_.channel_id);
    341   voe_send_.channel_id = -1;
    342   voe_send_.base->Release();
    343   voe_send_.base = nullptr;
    344   voe_send_.network->Release();
    345   voe_send_.network = nullptr;
    346   voe_send_.codec->Release();
    347   voe_send_.codec = nullptr;
    348 
    349   VoiceEngine::Delete(voe_send_.voice_engine);
    350   voe_send_.voice_engine = nullptr;
    351   VoiceEngine::Delete(voe_recv_.voice_engine);
    352   voe_recv_.voice_engine = nullptr;
    353 }
    354 
    355 const int CallTest::kDefaultTimeoutMs = 30 * 1000;
    356 const int CallTest::kLongTimeoutMs = 120 * 1000;
    357 const uint8_t CallTest::kVideoSendPayloadType = 100;
    358 const uint8_t CallTest::kFakeVideoSendPayloadType = 125;
    359 const uint8_t CallTest::kSendRtxPayloadType = 98;
    360 const uint8_t CallTest::kRedPayloadType = 118;
    361 const uint8_t CallTest::kRtxRedPayloadType = 99;
    362 const uint8_t CallTest::kUlpfecPayloadType = 119;
    363 const uint8_t CallTest::kAudioSendPayloadType = 103;
    364 const uint32_t CallTest::kSendRtxSsrcs[kNumSsrcs] = {0xBADCAFD, 0xBADCAFE,
    365                                                      0xBADCAFF};
    366 const uint32_t CallTest::kVideoSendSsrcs[kNumSsrcs] = {0xC0FFED, 0xC0FFEE,
    367                                                        0xC0FFEF};
    368 const uint32_t CallTest::kAudioSendSsrc = 0xDEADBEEF;
    369 const uint32_t CallTest::kReceiverLocalVideoSsrc = 0x123456;
    370 const uint32_t CallTest::kReceiverLocalAudioSsrc = 0x1234567;
    371 const int CallTest::kNackRtpHistoryMs = 1000;
    372 
    373 BaseTest::BaseTest(unsigned int timeout_ms) : RtpRtcpObserver(timeout_ms) {
    374 }
    375 
    376 BaseTest::~BaseTest() {
    377 }
    378 
    379 Call::Config BaseTest::GetSenderCallConfig() {
    380   return Call::Config();
    381 }
    382 
    383 Call::Config BaseTest::GetReceiverCallConfig() {
    384   return Call::Config();
    385 }
    386 
    387 void BaseTest::OnCallsCreated(Call* sender_call, Call* receiver_call) {
    388 }
    389 
    390 test::PacketTransport* BaseTest::CreateSendTransport(Call* sender_call) {
    391   return new PacketTransport(sender_call, this, test::PacketTransport::kSender,
    392                              FakeNetworkPipe::Config());
    393 }
    394 
    395 test::PacketTransport* BaseTest::CreateReceiveTransport() {
    396   return new PacketTransport(nullptr, this, test::PacketTransport::kReceiver,
    397                              FakeNetworkPipe::Config());
    398 }
    399 
    400 size_t BaseTest::GetNumVideoStreams() const {
    401   return 1;
    402 }
    403 
    404 size_t BaseTest::GetNumAudioStreams() const {
    405   return 0;
    406 }
    407 
    408 void BaseTest::ModifyVideoConfigs(
    409     VideoSendStream::Config* send_config,
    410     std::vector<VideoReceiveStream::Config>* receive_configs,
    411     VideoEncoderConfig* encoder_config) {}
    412 
    413 void BaseTest::OnVideoStreamsCreated(
    414     VideoSendStream* send_stream,
    415     const std::vector<VideoReceiveStream*>& receive_streams) {}
    416 
    417 void BaseTest::ModifyAudioConfigs(
    418     AudioSendStream::Config* send_config,
    419     std::vector<AudioReceiveStream::Config>* receive_configs) {}
    420 
    421 void BaseTest::OnAudioStreamsCreated(
    422     AudioSendStream* send_stream,
    423     const std::vector<AudioReceiveStream*>& receive_streams) {}
    424 
    425 void BaseTest::OnFrameGeneratorCapturerCreated(
    426     FrameGeneratorCapturer* frame_generator_capturer) {
    427 }
    428 
    429 SendTest::SendTest(unsigned int timeout_ms) : BaseTest(timeout_ms) {
    430 }
    431 
    432 bool SendTest::ShouldCreateReceivers() const {
    433   return false;
    434 }
    435 
    436 EndToEndTest::EndToEndTest(unsigned int timeout_ms) : BaseTest(timeout_ms) {
    437 }
    438 
    439 bool EndToEndTest::ShouldCreateReceivers() const {
    440   return true;
    441 }
    442 
    443 }  // namespace test
    444 }  // namespace webrtc
    445