Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 #include "webrtc/test/direct_transport.h"
     11 
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 #include "webrtc/call.h"
     15 #include "webrtc/system_wrappers/include/clock.h"
     16 
     17 namespace webrtc {
     18 namespace test {
     19 
     20 DirectTransport::DirectTransport(Call* send_call)
     21     : DirectTransport(FakeNetworkPipe::Config(), send_call) {}
     22 
     23 DirectTransport::DirectTransport(const FakeNetworkPipe::Config& config,
     24                                  Call* send_call)
     25     : send_call_(send_call),
     26       packet_event_(false, false),
     27       thread_(NetworkProcess, this, "NetworkProcess"),
     28       clock_(Clock::GetRealTimeClock()),
     29       shutting_down_(false),
     30       fake_network_(clock_, config) {
     31   thread_.Start();
     32 }
     33 
     34 DirectTransport::~DirectTransport() { StopSending(); }
     35 
     36 void DirectTransport::SetConfig(const FakeNetworkPipe::Config& config) {
     37   fake_network_.SetConfig(config);
     38 }
     39 
     40 void DirectTransport::StopSending() {
     41   {
     42     rtc::CritScope crit(&lock_);
     43     shutting_down_ = true;
     44   }
     45 
     46   packet_event_.Set();
     47   thread_.Stop();
     48 }
     49 
     50 void DirectTransport::SetReceiver(PacketReceiver* receiver) {
     51   fake_network_.SetReceiver(receiver);
     52 }
     53 
     54 bool DirectTransport::SendRtp(const uint8_t* data,
     55                               size_t length,
     56                               const PacketOptions& options) {
     57   if (send_call_) {
     58     rtc::SentPacket sent_packet(options.packet_id,
     59                                 clock_->TimeInMilliseconds());
     60     send_call_->OnSentPacket(sent_packet);
     61   }
     62   fake_network_.SendPacket(data, length);
     63   packet_event_.Set();
     64   return true;
     65 }
     66 
     67 bool DirectTransport::SendRtcp(const uint8_t* data, size_t length) {
     68   fake_network_.SendPacket(data, length);
     69   packet_event_.Set();
     70   return true;
     71 }
     72 
     73 int DirectTransport::GetAverageDelayMs() {
     74   return fake_network_.AverageDelay();
     75 }
     76 
     77 bool DirectTransport::NetworkProcess(void* transport) {
     78   return static_cast<DirectTransport*>(transport)->SendPackets();
     79 }
     80 
     81 bool DirectTransport::SendPackets() {
     82   fake_network_.Process();
     83   int64_t wait_time_ms = fake_network_.TimeUntilNextProcess();
     84   if (wait_time_ms > 0) {
     85     packet_event_.Wait(static_cast<int>(wait_time_ms));
     86   }
     87   rtc::CritScope crit(&lock_);
     88   return shutting_down_ ? false : true;
     89 }
     90 }  // namespace test
     91 }  // namespace webrtc
     92