Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2015 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "talk/media/webrtc/fakewebrtccall.h"
     29 
     30 #include <algorithm>
     31 #include <utility>
     32 
     33 #include "talk/media/base/rtputils.h"
     34 #include "webrtc/base/checks.h"
     35 #include "webrtc/base/gunit.h"
     36 #include "webrtc/audio/audio_sink.h"
     37 
     38 namespace cricket {
     39 FakeAudioSendStream::FakeAudioSendStream(
     40     const webrtc::AudioSendStream::Config& config) : config_(config) {
     41   RTC_DCHECK(config.voe_channel_id != -1);
     42 }
     43 
     44 const webrtc::AudioSendStream::Config&
     45     FakeAudioSendStream::GetConfig() const {
     46   return config_;
     47 }
     48 
     49 void FakeAudioSendStream::SetStats(
     50     const webrtc::AudioSendStream::Stats& stats) {
     51   stats_ = stats;
     52 }
     53 
     54 FakeAudioSendStream::TelephoneEvent
     55     FakeAudioSendStream::GetLatestTelephoneEvent() const {
     56   return latest_telephone_event_;
     57 }
     58 
     59 bool FakeAudioSendStream::SendTelephoneEvent(int payload_type, uint8_t event,
     60                                              uint32_t duration_ms) {
     61   latest_telephone_event_.payload_type = payload_type;
     62   latest_telephone_event_.event_code = event;
     63   latest_telephone_event_.duration_ms = duration_ms;
     64   return true;
     65 }
     66 
     67 webrtc::AudioSendStream::Stats FakeAudioSendStream::GetStats() const {
     68   return stats_;
     69 }
     70 
     71 FakeAudioReceiveStream::FakeAudioReceiveStream(
     72     const webrtc::AudioReceiveStream::Config& config)
     73     : config_(config), received_packets_(0) {
     74   RTC_DCHECK(config.voe_channel_id != -1);
     75 }
     76 
     77 const webrtc::AudioReceiveStream::Config&
     78     FakeAudioReceiveStream::GetConfig() const {
     79   return config_;
     80 }
     81 
     82 void FakeAudioReceiveStream::SetStats(
     83     const webrtc::AudioReceiveStream::Stats& stats) {
     84   stats_ = stats;
     85 }
     86 
     87 void FakeAudioReceiveStream::IncrementReceivedPackets() {
     88   received_packets_++;
     89 }
     90 
     91 webrtc::AudioReceiveStream::Stats FakeAudioReceiveStream::GetStats() const {
     92   return stats_;
     93 }
     94 
     95 void FakeAudioReceiveStream::SetSink(
     96     rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
     97   sink_ = std::move(sink);
     98 }
     99 
    100 FakeVideoSendStream::FakeVideoSendStream(
    101     const webrtc::VideoSendStream::Config& config,
    102     const webrtc::VideoEncoderConfig& encoder_config)
    103     : sending_(false),
    104       config_(config),
    105       codec_settings_set_(false),
    106       num_swapped_frames_(0) {
    107   RTC_DCHECK(config.encoder_settings.encoder != NULL);
    108   ReconfigureVideoEncoder(encoder_config);
    109 }
    110 
    111 webrtc::VideoSendStream::Config FakeVideoSendStream::GetConfig() const {
    112   return config_;
    113 }
    114 
    115 webrtc::VideoEncoderConfig FakeVideoSendStream::GetEncoderConfig() const {
    116   return encoder_config_;
    117 }
    118 
    119 std::vector<webrtc::VideoStream> FakeVideoSendStream::GetVideoStreams() {
    120   return encoder_config_.streams;
    121 }
    122 
    123 bool FakeVideoSendStream::IsSending() const {
    124   return sending_;
    125 }
    126 
    127 bool FakeVideoSendStream::GetVp8Settings(
    128     webrtc::VideoCodecVP8* settings) const {
    129   if (!codec_settings_set_) {
    130     return false;
    131   }
    132 
    133   *settings = vpx_settings_.vp8;
    134   return true;
    135 }
    136 
    137 bool FakeVideoSendStream::GetVp9Settings(
    138     webrtc::VideoCodecVP9* settings) const {
    139   if (!codec_settings_set_) {
    140     return false;
    141   }
    142 
    143   *settings = vpx_settings_.vp9;
    144   return true;
    145 }
    146 
    147 int FakeVideoSendStream::GetNumberOfSwappedFrames() const {
    148   return num_swapped_frames_;
    149 }
    150 
    151 int FakeVideoSendStream::GetLastWidth() const {
    152   return last_frame_.width();
    153 }
    154 
    155 int FakeVideoSendStream::GetLastHeight() const {
    156   return last_frame_.height();
    157 }
    158 
    159 int64_t FakeVideoSendStream::GetLastTimestamp() const {
    160   RTC_DCHECK(last_frame_.ntp_time_ms() == 0);
    161   return last_frame_.render_time_ms();
    162 }
    163 
    164 void FakeVideoSendStream::IncomingCapturedFrame(
    165     const webrtc::VideoFrame& frame) {
    166   ++num_swapped_frames_;
    167   last_frame_.ShallowCopy(frame);
    168 }
    169 
    170 void FakeVideoSendStream::SetStats(
    171     const webrtc::VideoSendStream::Stats& stats) {
    172   stats_ = stats;
    173 }
    174 
    175 webrtc::VideoSendStream::Stats FakeVideoSendStream::GetStats() {
    176   return stats_;
    177 }
    178 
    179 bool FakeVideoSendStream::ReconfigureVideoEncoder(
    180     const webrtc::VideoEncoderConfig& config) {
    181   encoder_config_ = config;
    182   if (config.encoder_specific_settings != NULL) {
    183     if (config_.encoder_settings.payload_name == "VP8") {
    184       vpx_settings_.vp8 = *reinterpret_cast<const webrtc::VideoCodecVP8*>(
    185                               config.encoder_specific_settings);
    186     } else if (config_.encoder_settings.payload_name == "VP9") {
    187       vpx_settings_.vp9 = *reinterpret_cast<const webrtc::VideoCodecVP9*>(
    188                               config.encoder_specific_settings);
    189     } else {
    190       ADD_FAILURE() << "Unsupported encoder payload: "
    191                     << config_.encoder_settings.payload_name;
    192     }
    193   }
    194   codec_settings_set_ = config.encoder_specific_settings != NULL;
    195   return true;
    196 }
    197 
    198 webrtc::VideoCaptureInput* FakeVideoSendStream::Input() {
    199   return this;
    200 }
    201 
    202 void FakeVideoSendStream::Start() {
    203   sending_ = true;
    204 }
    205 
    206 void FakeVideoSendStream::Stop() {
    207   sending_ = false;
    208 }
    209 
    210 FakeVideoReceiveStream::FakeVideoReceiveStream(
    211     const webrtc::VideoReceiveStream::Config& config)
    212     : config_(config), receiving_(false) {
    213 }
    214 
    215 webrtc::VideoReceiveStream::Config FakeVideoReceiveStream::GetConfig() {
    216   return config_;
    217 }
    218 
    219 bool FakeVideoReceiveStream::IsReceiving() const {
    220   return receiving_;
    221 }
    222 
    223 void FakeVideoReceiveStream::InjectFrame(const webrtc::VideoFrame& frame,
    224                                          int time_to_render_ms) {
    225   config_.renderer->RenderFrame(frame, time_to_render_ms);
    226 }
    227 
    228 webrtc::VideoReceiveStream::Stats FakeVideoReceiveStream::GetStats() const {
    229   return stats_;
    230 }
    231 
    232 void FakeVideoReceiveStream::Start() {
    233   receiving_ = true;
    234 }
    235 
    236 void FakeVideoReceiveStream::Stop() {
    237   receiving_ = false;
    238 }
    239 
    240 void FakeVideoReceiveStream::SetStats(
    241     const webrtc::VideoReceiveStream::Stats& stats) {
    242   stats_ = stats;
    243 }
    244 
    245 FakeCall::FakeCall(const webrtc::Call::Config& config)
    246     : config_(config),
    247       network_state_(webrtc::kNetworkUp),
    248       num_created_send_streams_(0),
    249       num_created_receive_streams_(0) {}
    250 
    251 FakeCall::~FakeCall() {
    252   EXPECT_EQ(0u, video_send_streams_.size());
    253   EXPECT_EQ(0u, audio_send_streams_.size());
    254   EXPECT_EQ(0u, video_receive_streams_.size());
    255   EXPECT_EQ(0u, audio_receive_streams_.size());
    256 }
    257 
    258 webrtc::Call::Config FakeCall::GetConfig() const {
    259   return config_;
    260 }
    261 
    262 const std::vector<FakeVideoSendStream*>& FakeCall::GetVideoSendStreams() {
    263   return video_send_streams_;
    264 }
    265 
    266 const std::vector<FakeVideoReceiveStream*>& FakeCall::GetVideoReceiveStreams() {
    267   return video_receive_streams_;
    268 }
    269 
    270 const std::vector<FakeAudioSendStream*>& FakeCall::GetAudioSendStreams() {
    271   return audio_send_streams_;
    272 }
    273 
    274 const FakeAudioSendStream* FakeCall::GetAudioSendStream(uint32_t ssrc) {
    275   for (const auto* p : GetAudioSendStreams()) {
    276     if (p->GetConfig().rtp.ssrc == ssrc) {
    277       return p;
    278     }
    279   }
    280   return nullptr;
    281 }
    282 
    283 const std::vector<FakeAudioReceiveStream*>& FakeCall::GetAudioReceiveStreams() {
    284   return audio_receive_streams_;
    285 }
    286 
    287 const FakeAudioReceiveStream* FakeCall::GetAudioReceiveStream(uint32_t ssrc) {
    288   for (const auto* p : GetAudioReceiveStreams()) {
    289     if (p->GetConfig().rtp.remote_ssrc == ssrc) {
    290       return p;
    291     }
    292   }
    293   return nullptr;
    294 }
    295 
    296 webrtc::NetworkState FakeCall::GetNetworkState() const {
    297   return network_state_;
    298 }
    299 
    300 webrtc::AudioSendStream* FakeCall::CreateAudioSendStream(
    301     const webrtc::AudioSendStream::Config& config) {
    302   FakeAudioSendStream* fake_stream = new FakeAudioSendStream(config);
    303   audio_send_streams_.push_back(fake_stream);
    304   ++num_created_send_streams_;
    305   return fake_stream;
    306 }
    307 
    308 void FakeCall::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
    309   auto it = std::find(audio_send_streams_.begin(),
    310                       audio_send_streams_.end(),
    311                       static_cast<FakeAudioSendStream*>(send_stream));
    312   if (it == audio_send_streams_.end()) {
    313     ADD_FAILURE() << "DestroyAudioSendStream called with unknown paramter.";
    314   } else {
    315     delete *it;
    316     audio_send_streams_.erase(it);
    317   }
    318 }
    319 
    320 webrtc::AudioReceiveStream* FakeCall::CreateAudioReceiveStream(
    321     const webrtc::AudioReceiveStream::Config& config) {
    322   audio_receive_streams_.push_back(new FakeAudioReceiveStream(config));
    323   ++num_created_receive_streams_;
    324   return audio_receive_streams_.back();
    325 }
    326 
    327 void FakeCall::DestroyAudioReceiveStream(
    328     webrtc::AudioReceiveStream* receive_stream) {
    329   auto it = std::find(audio_receive_streams_.begin(),
    330                       audio_receive_streams_.end(),
    331                       static_cast<FakeAudioReceiveStream*>(receive_stream));
    332   if (it == audio_receive_streams_.end()) {
    333     ADD_FAILURE() << "DestroyAudioReceiveStream called with unknown paramter.";
    334   } else {
    335     delete *it;
    336     audio_receive_streams_.erase(it);
    337   }
    338 }
    339 
    340 webrtc::VideoSendStream* FakeCall::CreateVideoSendStream(
    341     const webrtc::VideoSendStream::Config& config,
    342     const webrtc::VideoEncoderConfig& encoder_config) {
    343   FakeVideoSendStream* fake_stream =
    344       new FakeVideoSendStream(config, encoder_config);
    345   video_send_streams_.push_back(fake_stream);
    346   ++num_created_send_streams_;
    347   return fake_stream;
    348 }
    349 
    350 void FakeCall::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
    351   auto it = std::find(video_send_streams_.begin(),
    352                       video_send_streams_.end(),
    353                       static_cast<FakeVideoSendStream*>(send_stream));
    354   if (it == video_send_streams_.end()) {
    355     ADD_FAILURE() << "DestroyVideoSendStream called with unknown paramter.";
    356   } else {
    357     delete *it;
    358     video_send_streams_.erase(it);
    359   }
    360 }
    361 
    362 webrtc::VideoReceiveStream* FakeCall::CreateVideoReceiveStream(
    363     const webrtc::VideoReceiveStream::Config& config) {
    364   video_receive_streams_.push_back(new FakeVideoReceiveStream(config));
    365   ++num_created_receive_streams_;
    366   return video_receive_streams_.back();
    367 }
    368 
    369 void FakeCall::DestroyVideoReceiveStream(
    370     webrtc::VideoReceiveStream* receive_stream) {
    371   auto it = std::find(video_receive_streams_.begin(),
    372                       video_receive_streams_.end(),
    373                       static_cast<FakeVideoReceiveStream*>(receive_stream));
    374   if (it == video_receive_streams_.end()) {
    375     ADD_FAILURE() << "DestroyVideoReceiveStream called with unknown paramter.";
    376   } else {
    377     delete *it;
    378     video_receive_streams_.erase(it);
    379   }
    380 }
    381 
    382 webrtc::PacketReceiver* FakeCall::Receiver() {
    383   return this;
    384 }
    385 
    386 FakeCall::DeliveryStatus FakeCall::DeliverPacket(
    387     webrtc::MediaType media_type,
    388     const uint8_t* packet,
    389     size_t length,
    390     const webrtc::PacketTime& packet_time) {
    391   EXPECT_GE(length, 12u);
    392   uint32_t ssrc;
    393   if (!GetRtpSsrc(packet, length, &ssrc))
    394     return DELIVERY_PACKET_ERROR;
    395 
    396   if (media_type == webrtc::MediaType::ANY ||
    397       media_type == webrtc::MediaType::VIDEO) {
    398     for (auto receiver : video_receive_streams_) {
    399       if (receiver->GetConfig().rtp.remote_ssrc == ssrc)
    400         return DELIVERY_OK;
    401     }
    402   }
    403   if (media_type == webrtc::MediaType::ANY ||
    404       media_type == webrtc::MediaType::AUDIO) {
    405     for (auto receiver : audio_receive_streams_) {
    406       if (receiver->GetConfig().rtp.remote_ssrc == ssrc) {
    407         receiver->IncrementReceivedPackets();
    408         return DELIVERY_OK;
    409       }
    410     }
    411   }
    412   return DELIVERY_UNKNOWN_SSRC;
    413 }
    414 
    415 void FakeCall::SetStats(const webrtc::Call::Stats& stats) {
    416   stats_ = stats;
    417 }
    418 
    419 int FakeCall::GetNumCreatedSendStreams() const {
    420   return num_created_send_streams_;
    421 }
    422 
    423 int FakeCall::GetNumCreatedReceiveStreams() const {
    424   return num_created_receive_streams_;
    425 }
    426 
    427 webrtc::Call::Stats FakeCall::GetStats() const {
    428   return stats_;
    429 }
    430 
    431 void FakeCall::SetBitrateConfig(
    432     const webrtc::Call::Config::BitrateConfig& bitrate_config) {
    433   config_.bitrate_config = bitrate_config;
    434 }
    435 
    436 void FakeCall::SignalNetworkState(webrtc::NetworkState state) {
    437   network_state_ = state;
    438 }
    439 
    440 void FakeCall::OnSentPacket(const rtc::SentPacket& sent_packet) {
    441   last_sent_packet_ = sent_packet;
    442 }
    443 }  // namespace cricket
    444