1 /*
2  *  Copyright (c) 2015 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 "media/engine/fake_webrtc_call.h"
12 
13 #include <utility>
14 
15 #include "absl/algorithm/container.h"
16 #include "api/call/audio_sink.h"
17 #include "media/base/rtp_utils.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/gunit.h"
20 
21 namespace cricket {
FakeAudioSendStream(int id,const webrtc::AudioSendStream::Config & config)22 FakeAudioSendStream::FakeAudioSendStream(
23     int id,
24     const webrtc::AudioSendStream::Config& config)
25     : id_(id), config_(config) {}
26 
Reconfigure(const webrtc::AudioSendStream::Config & config)27 void FakeAudioSendStream::Reconfigure(
28     const webrtc::AudioSendStream::Config& config) {
29   config_ = config;
30 }
31 
GetConfig() const32 const webrtc::AudioSendStream::Config& FakeAudioSendStream::GetConfig() const {
33   return config_;
34 }
35 
SetStats(const webrtc::AudioSendStream::Stats & stats)36 void FakeAudioSendStream::SetStats(
37     const webrtc::AudioSendStream::Stats& stats) {
38   stats_ = stats;
39 }
40 
41 FakeAudioSendStream::TelephoneEvent
GetLatestTelephoneEvent() const42 FakeAudioSendStream::GetLatestTelephoneEvent() const {
43   return latest_telephone_event_;
44 }
45 
SendTelephoneEvent(int payload_type,int payload_frequency,int event,int duration_ms)46 bool FakeAudioSendStream::SendTelephoneEvent(int payload_type,
47                                              int payload_frequency,
48                                              int event,
49                                              int duration_ms) {
50   latest_telephone_event_.payload_type = payload_type;
51   latest_telephone_event_.payload_frequency = payload_frequency;
52   latest_telephone_event_.event_code = event;
53   latest_telephone_event_.duration_ms = duration_ms;
54   return true;
55 }
56 
SetMuted(bool muted)57 void FakeAudioSendStream::SetMuted(bool muted) {
58   muted_ = muted;
59 }
60 
GetStats() const61 webrtc::AudioSendStream::Stats FakeAudioSendStream::GetStats() const {
62   return stats_;
63 }
64 
GetStats(bool) const65 webrtc::AudioSendStream::Stats FakeAudioSendStream::GetStats(
66     bool /*has_remote_tracks*/) const {
67   return stats_;
68 }
69 
FakeAudioReceiveStream(int id,const webrtc::AudioReceiveStream::Config & config)70 FakeAudioReceiveStream::FakeAudioReceiveStream(
71     int id,
72     const webrtc::AudioReceiveStream::Config& config)
73     : id_(id), config_(config) {}
74 
GetConfig() const75 const webrtc::AudioReceiveStream::Config& FakeAudioReceiveStream::GetConfig()
76     const {
77   return config_;
78 }
79 
SetStats(const webrtc::AudioReceiveStream::Stats & stats)80 void FakeAudioReceiveStream::SetStats(
81     const webrtc::AudioReceiveStream::Stats& stats) {
82   stats_ = stats;
83 }
84 
VerifyLastPacket(const uint8_t * data,size_t length) const85 bool FakeAudioReceiveStream::VerifyLastPacket(const uint8_t* data,
86                                               size_t length) const {
87   return last_packet_ == rtc::Buffer(data, length);
88 }
89 
DeliverRtp(const uint8_t * packet,size_t length,int64_t)90 bool FakeAudioReceiveStream::DeliverRtp(const uint8_t* packet,
91                                         size_t length,
92                                         int64_t /* packet_time_us */) {
93   ++received_packets_;
94   last_packet_.SetData(packet, length);
95   return true;
96 }
97 
Reconfigure(const webrtc::AudioReceiveStream::Config & config)98 void FakeAudioReceiveStream::Reconfigure(
99     const webrtc::AudioReceiveStream::Config& config) {
100   config_ = config;
101 }
102 
GetStats() const103 webrtc::AudioReceiveStream::Stats FakeAudioReceiveStream::GetStats() const {
104   return stats_;
105 }
106 
SetSink(webrtc::AudioSinkInterface * sink)107 void FakeAudioReceiveStream::SetSink(webrtc::AudioSinkInterface* sink) {
108   sink_ = sink;
109 }
110 
SetGain(float gain)111 void FakeAudioReceiveStream::SetGain(float gain) {
112   gain_ = gain;
113 }
114 
FakeVideoSendStream(webrtc::VideoSendStream::Config config,webrtc::VideoEncoderConfig encoder_config)115 FakeVideoSendStream::FakeVideoSendStream(
116     webrtc::VideoSendStream::Config config,
117     webrtc::VideoEncoderConfig encoder_config)
118     : sending_(false),
119       config_(std::move(config)),
120       codec_settings_set_(false),
121       resolution_scaling_enabled_(false),
122       framerate_scaling_enabled_(false),
123       source_(nullptr),
124       num_swapped_frames_(0) {
125   RTC_DCHECK(config.encoder_settings.encoder_factory != nullptr);
126   RTC_DCHECK(config.encoder_settings.bitrate_allocator_factory != nullptr);
127   ReconfigureVideoEncoder(std::move(encoder_config));
128 }
129 
~FakeVideoSendStream()130 FakeVideoSendStream::~FakeVideoSendStream() {
131   if (source_)
132     source_->RemoveSink(this);
133 }
134 
GetConfig() const135 const webrtc::VideoSendStream::Config& FakeVideoSendStream::GetConfig() const {
136   return config_;
137 }
138 
GetEncoderConfig() const139 const webrtc::VideoEncoderConfig& FakeVideoSendStream::GetEncoderConfig()
140     const {
141   return encoder_config_;
142 }
143 
GetVideoStreams() const144 const std::vector<webrtc::VideoStream>& FakeVideoSendStream::GetVideoStreams()
145     const {
146   return video_streams_;
147 }
148 
IsSending() const149 bool FakeVideoSendStream::IsSending() const {
150   return sending_;
151 }
152 
GetVp8Settings(webrtc::VideoCodecVP8 * settings) const153 bool FakeVideoSendStream::GetVp8Settings(
154     webrtc::VideoCodecVP8* settings) const {
155   if (!codec_settings_set_) {
156     return false;
157   }
158 
159   *settings = codec_specific_settings_.vp8;
160   return true;
161 }
162 
GetVp9Settings(webrtc::VideoCodecVP9 * settings) const163 bool FakeVideoSendStream::GetVp9Settings(
164     webrtc::VideoCodecVP9* settings) const {
165   if (!codec_settings_set_) {
166     return false;
167   }
168 
169   *settings = codec_specific_settings_.vp9;
170   return true;
171 }
172 
GetH264Settings(webrtc::VideoCodecH264 * settings) const173 bool FakeVideoSendStream::GetH264Settings(
174     webrtc::VideoCodecH264* settings) const {
175   if (!codec_settings_set_) {
176     return false;
177   }
178 
179   *settings = codec_specific_settings_.h264;
180   return true;
181 }
182 
GetNumberOfSwappedFrames() const183 int FakeVideoSendStream::GetNumberOfSwappedFrames() const {
184   return num_swapped_frames_;
185 }
186 
GetLastWidth() const187 int FakeVideoSendStream::GetLastWidth() const {
188   return last_frame_->width();
189 }
190 
GetLastHeight() const191 int FakeVideoSendStream::GetLastHeight() const {
192   return last_frame_->height();
193 }
194 
GetLastTimestamp() const195 int64_t FakeVideoSendStream::GetLastTimestamp() const {
196   RTC_DCHECK(last_frame_->ntp_time_ms() == 0);
197   return last_frame_->render_time_ms();
198 }
199 
OnFrame(const webrtc::VideoFrame & frame)200 void FakeVideoSendStream::OnFrame(const webrtc::VideoFrame& frame) {
201   ++num_swapped_frames_;
202   if (!last_frame_ || frame.width() != last_frame_->width() ||
203       frame.height() != last_frame_->height() ||
204       frame.rotation() != last_frame_->rotation()) {
205     video_streams_ = encoder_config_.video_stream_factory->CreateEncoderStreams(
206         frame.width(), frame.height(), encoder_config_);
207   }
208   last_frame_ = frame;
209 }
210 
SetStats(const webrtc::VideoSendStream::Stats & stats)211 void FakeVideoSendStream::SetStats(
212     const webrtc::VideoSendStream::Stats& stats) {
213   stats_ = stats;
214 }
215 
GetStats()216 webrtc::VideoSendStream::Stats FakeVideoSendStream::GetStats() {
217   return stats_;
218 }
219 
ReconfigureVideoEncoder(webrtc::VideoEncoderConfig config)220 void FakeVideoSendStream::ReconfigureVideoEncoder(
221     webrtc::VideoEncoderConfig config) {
222   int width, height;
223   if (last_frame_) {
224     width = last_frame_->width();
225     height = last_frame_->height();
226   } else {
227     width = height = 0;
228   }
229   video_streams_ =
230       config.video_stream_factory->CreateEncoderStreams(width, height, config);
231   if (config.encoder_specific_settings != NULL) {
232     const unsigned char num_temporal_layers = static_cast<unsigned char>(
233         video_streams_.back().num_temporal_layers.value_or(1));
234     if (config_.rtp.payload_name == "VP8") {
235       config.encoder_specific_settings->FillVideoCodecVp8(
236           &codec_specific_settings_.vp8);
237       if (!video_streams_.empty()) {
238         codec_specific_settings_.vp8.numberOfTemporalLayers =
239             num_temporal_layers;
240       }
241     } else if (config_.rtp.payload_name == "VP9") {
242       config.encoder_specific_settings->FillVideoCodecVp9(
243           &codec_specific_settings_.vp9);
244       if (!video_streams_.empty()) {
245         codec_specific_settings_.vp9.numberOfTemporalLayers =
246             num_temporal_layers;
247       }
248     } else if (config_.rtp.payload_name == "H264") {
249       config.encoder_specific_settings->FillVideoCodecH264(
250           &codec_specific_settings_.h264);
251       codec_specific_settings_.h264.numberOfTemporalLayers =
252           num_temporal_layers;
253     } else {
254       ADD_FAILURE() << "Unsupported encoder payload: "
255                     << config_.rtp.payload_name;
256     }
257   }
258   codec_settings_set_ = config.encoder_specific_settings != NULL;
259   encoder_config_ = std::move(config);
260   ++num_encoder_reconfigurations_;
261 }
262 
UpdateActiveSimulcastLayers(const std::vector<bool> active_layers)263 void FakeVideoSendStream::UpdateActiveSimulcastLayers(
264     const std::vector<bool> active_layers) {
265   sending_ = false;
266   for (const bool active_layer : active_layers) {
267     if (active_layer) {
268       sending_ = true;
269       break;
270     }
271   }
272 }
273 
Start()274 void FakeVideoSendStream::Start() {
275   sending_ = true;
276 }
277 
Stop()278 void FakeVideoSendStream::Stop() {
279   sending_ = false;
280 }
281 
SetSource(rtc::VideoSourceInterface<webrtc::VideoFrame> * source,const webrtc::DegradationPreference & degradation_preference)282 void FakeVideoSendStream::SetSource(
283     rtc::VideoSourceInterface<webrtc::VideoFrame>* source,
284     const webrtc::DegradationPreference& degradation_preference) {
285   if (source_)
286     source_->RemoveSink(this);
287   source_ = source;
288   switch (degradation_preference) {
289     case webrtc::DegradationPreference::MAINTAIN_FRAMERATE:
290       resolution_scaling_enabled_ = true;
291       framerate_scaling_enabled_ = false;
292       break;
293     case webrtc::DegradationPreference::MAINTAIN_RESOLUTION:
294       resolution_scaling_enabled_ = false;
295       framerate_scaling_enabled_ = true;
296       break;
297     case webrtc::DegradationPreference::BALANCED:
298       resolution_scaling_enabled_ = true;
299       framerate_scaling_enabled_ = true;
300       break;
301     case webrtc::DegradationPreference::DISABLED:
302       resolution_scaling_enabled_ = false;
303       framerate_scaling_enabled_ = false;
304       break;
305   }
306   if (source)
307     source->AddOrUpdateSink(this, resolution_scaling_enabled_
308                                       ? sink_wants_
309                                       : rtc::VideoSinkWants());
310 }
311 
InjectVideoSinkWants(const rtc::VideoSinkWants & wants)312 void FakeVideoSendStream::InjectVideoSinkWants(
313     const rtc::VideoSinkWants& wants) {
314   sink_wants_ = wants;
315   source_->AddOrUpdateSink(this, wants);
316 }
317 
FakeVideoReceiveStream(webrtc::VideoReceiveStream::Config config)318 FakeVideoReceiveStream::FakeVideoReceiveStream(
319     webrtc::VideoReceiveStream::Config config)
320     : config_(std::move(config)),
321       receiving_(false),
322       num_added_secondary_sinks_(0),
323       num_removed_secondary_sinks_(0) {}
324 
GetConfig() const325 const webrtc::VideoReceiveStream::Config& FakeVideoReceiveStream::GetConfig()
326     const {
327   return config_;
328 }
329 
IsReceiving() const330 bool FakeVideoReceiveStream::IsReceiving() const {
331   return receiving_;
332 }
333 
InjectFrame(const webrtc::VideoFrame & frame)334 void FakeVideoReceiveStream::InjectFrame(const webrtc::VideoFrame& frame) {
335   config_.renderer->OnFrame(frame);
336 }
337 
GetStats() const338 webrtc::VideoReceiveStream::Stats FakeVideoReceiveStream::GetStats() const {
339   return stats_;
340 }
341 
Start()342 void FakeVideoReceiveStream::Start() {
343   receiving_ = true;
344 }
345 
Stop()346 void FakeVideoReceiveStream::Stop() {
347   receiving_ = false;
348 }
349 
SetStats(const webrtc::VideoReceiveStream::Stats & stats)350 void FakeVideoReceiveStream::SetStats(
351     const webrtc::VideoReceiveStream::Stats& stats) {
352   stats_ = stats;
353 }
354 
AddSecondarySink(webrtc::RtpPacketSinkInterface * sink)355 void FakeVideoReceiveStream::AddSecondarySink(
356     webrtc::RtpPacketSinkInterface* sink) {
357   ++num_added_secondary_sinks_;
358 }
359 
RemoveSecondarySink(const webrtc::RtpPacketSinkInterface * sink)360 void FakeVideoReceiveStream::RemoveSecondarySink(
361     const webrtc::RtpPacketSinkInterface* sink) {
362   ++num_removed_secondary_sinks_;
363 }
364 
GetNumAddedSecondarySinks() const365 int FakeVideoReceiveStream::GetNumAddedSecondarySinks() const {
366   return num_added_secondary_sinks_;
367 }
368 
GetNumRemovedSecondarySinks() const369 int FakeVideoReceiveStream::GetNumRemovedSecondarySinks() const {
370   return num_removed_secondary_sinks_;
371 }
372 
FakeFlexfecReceiveStream(const webrtc::FlexfecReceiveStream::Config & config)373 FakeFlexfecReceiveStream::FakeFlexfecReceiveStream(
374     const webrtc::FlexfecReceiveStream::Config& config)
375     : config_(config) {}
376 
377 const webrtc::FlexfecReceiveStream::Config&
GetConfig() const378 FakeFlexfecReceiveStream::GetConfig() const {
379   return config_;
380 }
381 
382 // TODO(brandtr): Implement when the stats have been designed.
GetStats() const383 webrtc::FlexfecReceiveStream::Stats FakeFlexfecReceiveStream::GetStats() const {
384   return webrtc::FlexfecReceiveStream::Stats();
385 }
386 
OnRtpPacket(const webrtc::RtpPacketReceived &)387 void FakeFlexfecReceiveStream::OnRtpPacket(const webrtc::RtpPacketReceived&) {
388   RTC_NOTREACHED() << "Not implemented.";
389 }
390 
FakeCall()391 FakeCall::FakeCall()
392     : audio_network_state_(webrtc::kNetworkUp),
393       video_network_state_(webrtc::kNetworkUp),
394       num_created_send_streams_(0),
395       num_created_receive_streams_(0) {}
396 
~FakeCall()397 FakeCall::~FakeCall() {
398   EXPECT_EQ(0u, video_send_streams_.size());
399   EXPECT_EQ(0u, audio_send_streams_.size());
400   EXPECT_EQ(0u, video_receive_streams_.size());
401   EXPECT_EQ(0u, audio_receive_streams_.size());
402 }
403 
GetVideoSendStreams()404 const std::vector<FakeVideoSendStream*>& FakeCall::GetVideoSendStreams() {
405   return video_send_streams_;
406 }
407 
GetVideoReceiveStreams()408 const std::vector<FakeVideoReceiveStream*>& FakeCall::GetVideoReceiveStreams() {
409   return video_receive_streams_;
410 }
411 
GetVideoReceiveStream(uint32_t ssrc)412 const FakeVideoReceiveStream* FakeCall::GetVideoReceiveStream(uint32_t ssrc) {
413   for (const auto* p : GetVideoReceiveStreams()) {
414     if (p->GetConfig().rtp.remote_ssrc == ssrc) {
415       return p;
416     }
417   }
418   return nullptr;
419 }
420 
GetAudioSendStreams()421 const std::vector<FakeAudioSendStream*>& FakeCall::GetAudioSendStreams() {
422   return audio_send_streams_;
423 }
424 
GetAudioSendStream(uint32_t ssrc)425 const FakeAudioSendStream* FakeCall::GetAudioSendStream(uint32_t ssrc) {
426   for (const auto* p : GetAudioSendStreams()) {
427     if (p->GetConfig().rtp.ssrc == ssrc) {
428       return p;
429     }
430   }
431   return nullptr;
432 }
433 
GetAudioReceiveStreams()434 const std::vector<FakeAudioReceiveStream*>& FakeCall::GetAudioReceiveStreams() {
435   return audio_receive_streams_;
436 }
437 
GetAudioReceiveStream(uint32_t ssrc)438 const FakeAudioReceiveStream* FakeCall::GetAudioReceiveStream(uint32_t ssrc) {
439   for (const auto* p : GetAudioReceiveStreams()) {
440     if (p->GetConfig().rtp.remote_ssrc == ssrc) {
441       return p;
442     }
443   }
444   return nullptr;
445 }
446 
447 const std::vector<FakeFlexfecReceiveStream*>&
GetFlexfecReceiveStreams()448 FakeCall::GetFlexfecReceiveStreams() {
449   return flexfec_receive_streams_;
450 }
451 
GetNetworkState(webrtc::MediaType media) const452 webrtc::NetworkState FakeCall::GetNetworkState(webrtc::MediaType media) const {
453   switch (media) {
454     case webrtc::MediaType::AUDIO:
455       return audio_network_state_;
456     case webrtc::MediaType::VIDEO:
457       return video_network_state_;
458     case webrtc::MediaType::DATA:
459     case webrtc::MediaType::ANY:
460       ADD_FAILURE() << "GetNetworkState called with unknown parameter.";
461       return webrtc::kNetworkDown;
462   }
463   // Even though all the values for the enum class are listed above,the compiler
464   // will emit a warning as the method may be called with a value outside of the
465   // valid enum range, unless this case is also handled.
466   ADD_FAILURE() << "GetNetworkState called with unknown parameter.";
467   return webrtc::kNetworkDown;
468 }
469 
CreateAudioSendStream(const webrtc::AudioSendStream::Config & config)470 webrtc::AudioSendStream* FakeCall::CreateAudioSendStream(
471     const webrtc::AudioSendStream::Config& config) {
472   FakeAudioSendStream* fake_stream =
473       new FakeAudioSendStream(next_stream_id_++, config);
474   audio_send_streams_.push_back(fake_stream);
475   ++num_created_send_streams_;
476   return fake_stream;
477 }
478 
DestroyAudioSendStream(webrtc::AudioSendStream * send_stream)479 void FakeCall::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) {
480   auto it = absl::c_find(audio_send_streams_,
481                          static_cast<FakeAudioSendStream*>(send_stream));
482   if (it == audio_send_streams_.end()) {
483     ADD_FAILURE() << "DestroyAudioSendStream called with unknown parameter.";
484   } else {
485     delete *it;
486     audio_send_streams_.erase(it);
487   }
488 }
489 
CreateAudioReceiveStream(const webrtc::AudioReceiveStream::Config & config)490 webrtc::AudioReceiveStream* FakeCall::CreateAudioReceiveStream(
491     const webrtc::AudioReceiveStream::Config& config) {
492   audio_receive_streams_.push_back(
493       new FakeAudioReceiveStream(next_stream_id_++, config));
494   ++num_created_receive_streams_;
495   return audio_receive_streams_.back();
496 }
497 
DestroyAudioReceiveStream(webrtc::AudioReceiveStream * receive_stream)498 void FakeCall::DestroyAudioReceiveStream(
499     webrtc::AudioReceiveStream* receive_stream) {
500   auto it = absl::c_find(audio_receive_streams_,
501                          static_cast<FakeAudioReceiveStream*>(receive_stream));
502   if (it == audio_receive_streams_.end()) {
503     ADD_FAILURE() << "DestroyAudioReceiveStream called with unknown parameter.";
504   } else {
505     delete *it;
506     audio_receive_streams_.erase(it);
507   }
508 }
509 
CreateVideoSendStream(webrtc::VideoSendStream::Config config,webrtc::VideoEncoderConfig encoder_config)510 webrtc::VideoSendStream* FakeCall::CreateVideoSendStream(
511     webrtc::VideoSendStream::Config config,
512     webrtc::VideoEncoderConfig encoder_config) {
513   FakeVideoSendStream* fake_stream =
514       new FakeVideoSendStream(std::move(config), std::move(encoder_config));
515   video_send_streams_.push_back(fake_stream);
516   ++num_created_send_streams_;
517   return fake_stream;
518 }
519 
DestroyVideoSendStream(webrtc::VideoSendStream * send_stream)520 void FakeCall::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) {
521   auto it = absl::c_find(video_send_streams_,
522                          static_cast<FakeVideoSendStream*>(send_stream));
523   if (it == video_send_streams_.end()) {
524     ADD_FAILURE() << "DestroyVideoSendStream called with unknown parameter.";
525   } else {
526     delete *it;
527     video_send_streams_.erase(it);
528   }
529 }
530 
CreateVideoReceiveStream(webrtc::VideoReceiveStream::Config config)531 webrtc::VideoReceiveStream* FakeCall::CreateVideoReceiveStream(
532     webrtc::VideoReceiveStream::Config config) {
533   video_receive_streams_.push_back(
534       new FakeVideoReceiveStream(std::move(config)));
535   ++num_created_receive_streams_;
536   return video_receive_streams_.back();
537 }
538 
DestroyVideoReceiveStream(webrtc::VideoReceiveStream * receive_stream)539 void FakeCall::DestroyVideoReceiveStream(
540     webrtc::VideoReceiveStream* receive_stream) {
541   auto it = absl::c_find(video_receive_streams_,
542                          static_cast<FakeVideoReceiveStream*>(receive_stream));
543   if (it == video_receive_streams_.end()) {
544     ADD_FAILURE() << "DestroyVideoReceiveStream called with unknown parameter.";
545   } else {
546     delete *it;
547     video_receive_streams_.erase(it);
548   }
549 }
550 
CreateFlexfecReceiveStream(const webrtc::FlexfecReceiveStream::Config & config)551 webrtc::FlexfecReceiveStream* FakeCall::CreateFlexfecReceiveStream(
552     const webrtc::FlexfecReceiveStream::Config& config) {
553   FakeFlexfecReceiveStream* fake_stream = new FakeFlexfecReceiveStream(config);
554   flexfec_receive_streams_.push_back(fake_stream);
555   ++num_created_receive_streams_;
556   return fake_stream;
557 }
558 
DestroyFlexfecReceiveStream(webrtc::FlexfecReceiveStream * receive_stream)559 void FakeCall::DestroyFlexfecReceiveStream(
560     webrtc::FlexfecReceiveStream* receive_stream) {
561   auto it =
562       absl::c_find(flexfec_receive_streams_,
563                    static_cast<FakeFlexfecReceiveStream*>(receive_stream));
564   if (it == flexfec_receive_streams_.end()) {
565     ADD_FAILURE()
566         << "DestroyFlexfecReceiveStream called with unknown parameter.";
567   } else {
568     delete *it;
569     flexfec_receive_streams_.erase(it);
570   }
571 }
572 
Receiver()573 webrtc::PacketReceiver* FakeCall::Receiver() {
574   return this;
575 }
576 
DeliverPacket(webrtc::MediaType media_type,rtc::CopyOnWriteBuffer packet,int64_t packet_time_us)577 FakeCall::DeliveryStatus FakeCall::DeliverPacket(webrtc::MediaType media_type,
578                                                  rtc::CopyOnWriteBuffer packet,
579                                                  int64_t packet_time_us) {
580   EXPECT_GE(packet.size(), 12u);
581   RTC_DCHECK(media_type == webrtc::MediaType::AUDIO ||
582              media_type == webrtc::MediaType::VIDEO);
583 
584   uint32_t ssrc;
585   if (!GetRtpSsrc(packet.cdata(), packet.size(), &ssrc))
586     return DELIVERY_PACKET_ERROR;
587 
588   if (media_type == webrtc::MediaType::VIDEO) {
589     for (auto receiver : video_receive_streams_) {
590       if (receiver->GetConfig().rtp.remote_ssrc == ssrc)
591         return DELIVERY_OK;
592     }
593   }
594   if (media_type == webrtc::MediaType::AUDIO) {
595     for (auto receiver : audio_receive_streams_) {
596       if (receiver->GetConfig().rtp.remote_ssrc == ssrc) {
597         receiver->DeliverRtp(packet.cdata(), packet.size(), packet_time_us);
598         return DELIVERY_OK;
599       }
600     }
601   }
602   return DELIVERY_UNKNOWN_SSRC;
603 }
604 
SetStats(const webrtc::Call::Stats & stats)605 void FakeCall::SetStats(const webrtc::Call::Stats& stats) {
606   stats_ = stats;
607 }
608 
GetNumCreatedSendStreams() const609 int FakeCall::GetNumCreatedSendStreams() const {
610   return num_created_send_streams_;
611 }
612 
GetNumCreatedReceiveStreams() const613 int FakeCall::GetNumCreatedReceiveStreams() const {
614   return num_created_receive_streams_;
615 }
616 
GetStats() const617 webrtc::Call::Stats FakeCall::GetStats() const {
618   return stats_;
619 }
620 
SignalChannelNetworkState(webrtc::MediaType media,webrtc::NetworkState state)621 void FakeCall::SignalChannelNetworkState(webrtc::MediaType media,
622                                          webrtc::NetworkState state) {
623   switch (media) {
624     case webrtc::MediaType::AUDIO:
625       audio_network_state_ = state;
626       break;
627     case webrtc::MediaType::VIDEO:
628       video_network_state_ = state;
629       break;
630     case webrtc::MediaType::DATA:
631     case webrtc::MediaType::ANY:
632       ADD_FAILURE()
633           << "SignalChannelNetworkState called with unknown parameter.";
634   }
635 }
636 
OnAudioTransportOverheadChanged(int transport_overhead_per_packet)637 void FakeCall::OnAudioTransportOverheadChanged(
638     int transport_overhead_per_packet) {}
639 
OnSentPacket(const rtc::SentPacket & sent_packet)640 void FakeCall::OnSentPacket(const rtc::SentPacket& sent_packet) {
641   last_sent_packet_ = sent_packet;
642   if (sent_packet.packet_id >= 0) {
643     last_sent_nonnegative_packet_id_ = sent_packet.packet_id;
644   }
645 }
646 
647 }  // namespace cricket
648