• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2018 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/base/fake_media_engine.h"
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "absl/algorithm/container.h"
17 #include "absl/strings/match.h"
18 #include "rtc_base/checks.h"
19 
20 namespace cricket {
21 
DtmfInfo(uint32_t ssrc,int event_code,int duration)22 FakeVoiceMediaChannel::DtmfInfo::DtmfInfo(uint32_t ssrc,
23                                           int event_code,
24                                           int duration)
25     : ssrc(ssrc), event_code(event_code), duration(duration) {}
26 
VoiceChannelAudioSink(AudioSource * source)27 FakeVoiceMediaChannel::VoiceChannelAudioSink::VoiceChannelAudioSink(
28     AudioSource* source)
29     : source_(source) {
30   source_->SetSink(this);
31 }
~VoiceChannelAudioSink()32 FakeVoiceMediaChannel::VoiceChannelAudioSink::~VoiceChannelAudioSink() {
33   if (source_) {
34     source_->SetSink(nullptr);
35   }
36 }
OnData(const void * audio_data,int bits_per_sample,int sample_rate,size_t number_of_channels,size_t number_of_frames,absl::optional<int64_t> absolute_capture_timestamp_ms)37 void FakeVoiceMediaChannel::VoiceChannelAudioSink::OnData(
38     const void* audio_data,
39     int bits_per_sample,
40     int sample_rate,
41     size_t number_of_channels,
42     size_t number_of_frames,
43     absl::optional<int64_t> absolute_capture_timestamp_ms) {}
OnClose()44 void FakeVoiceMediaChannel::VoiceChannelAudioSink::OnClose() {
45   source_ = nullptr;
46 }
source() const47 AudioSource* FakeVoiceMediaChannel::VoiceChannelAudioSink::source() const {
48   return source_;
49 }
50 
FakeVoiceMediaChannel(FakeVoiceEngine * engine,const AudioOptions & options)51 FakeVoiceMediaChannel::FakeVoiceMediaChannel(FakeVoiceEngine* engine,
52                                              const AudioOptions& options)
53     : engine_(engine), max_bps_(-1) {
54   output_scalings_[0] = 1.0;  // For default channel.
55   SetOptions(options);
56 }
~FakeVoiceMediaChannel()57 FakeVoiceMediaChannel::~FakeVoiceMediaChannel() {
58   if (engine_) {
59     engine_->UnregisterChannel(this);
60   }
61 }
recv_codecs() const62 const std::vector<AudioCodec>& FakeVoiceMediaChannel::recv_codecs() const {
63   return recv_codecs_;
64 }
send_codecs() const65 const std::vector<AudioCodec>& FakeVoiceMediaChannel::send_codecs() const {
66   return send_codecs_;
67 }
codecs() const68 const std::vector<AudioCodec>& FakeVoiceMediaChannel::codecs() const {
69   return send_codecs();
70 }
71 const std::vector<FakeVoiceMediaChannel::DtmfInfo>&
dtmf_info_queue() const72 FakeVoiceMediaChannel::dtmf_info_queue() const {
73   return dtmf_info_queue_;
74 }
options() const75 const AudioOptions& FakeVoiceMediaChannel::options() const {
76   return options_;
77 }
max_bps() const78 int FakeVoiceMediaChannel::max_bps() const {
79   return max_bps_;
80 }
SetSendParameters(const AudioSendParameters & params)81 bool FakeVoiceMediaChannel::SetSendParameters(
82     const AudioSendParameters& params) {
83   set_send_rtcp_parameters(params.rtcp);
84   return (SetSendCodecs(params.codecs) &&
85           SetSendExtmapAllowMixed(params.extmap_allow_mixed) &&
86           SetSendRtpHeaderExtensions(params.extensions) &&
87           SetMaxSendBandwidth(params.max_bandwidth_bps) &&
88           SetOptions(params.options));
89 }
SetRecvParameters(const AudioRecvParameters & params)90 bool FakeVoiceMediaChannel::SetRecvParameters(
91     const AudioRecvParameters& params) {
92   set_recv_rtcp_parameters(params.rtcp);
93   return (SetRecvCodecs(params.codecs) &&
94           SetRecvRtpHeaderExtensions(params.extensions));
95 }
SetPlayout(bool playout)96 void FakeVoiceMediaChannel::SetPlayout(bool playout) {
97   set_playout(playout);
98 }
SetSend(bool send)99 void FakeVoiceMediaChannel::SetSend(bool send) {
100   set_sending(send);
101 }
SetAudioSend(uint32_t ssrc,bool enable,const AudioOptions * options,AudioSource * source)102 bool FakeVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
103                                          bool enable,
104                                          const AudioOptions* options,
105                                          AudioSource* source) {
106   if (!SetLocalSource(ssrc, source)) {
107     return false;
108   }
109   if (!RtpHelper<VoiceMediaChannel>::MuteStream(ssrc, !enable)) {
110     return false;
111   }
112   if (enable && options) {
113     return SetOptions(*options);
114   }
115   return true;
116 }
HasSource(uint32_t ssrc) const117 bool FakeVoiceMediaChannel::HasSource(uint32_t ssrc) const {
118   return local_sinks_.find(ssrc) != local_sinks_.end();
119 }
AddRecvStream(const StreamParams & sp)120 bool FakeVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
121   if (!RtpHelper<VoiceMediaChannel>::AddRecvStream(sp))
122     return false;
123   output_scalings_[sp.first_ssrc()] = 1.0;
124   output_delays_[sp.first_ssrc()] = 0;
125   return true;
126 }
RemoveRecvStream(uint32_t ssrc)127 bool FakeVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
128   if (!RtpHelper<VoiceMediaChannel>::RemoveRecvStream(ssrc))
129     return false;
130   output_scalings_.erase(ssrc);
131   output_delays_.erase(ssrc);
132   return true;
133 }
CanInsertDtmf()134 bool FakeVoiceMediaChannel::CanInsertDtmf() {
135   for (std::vector<AudioCodec>::const_iterator it = send_codecs_.begin();
136        it != send_codecs_.end(); ++it) {
137     // Find the DTMF telephone event "codec".
138     if (absl::EqualsIgnoreCase(it->name, "telephone-event")) {
139       return true;
140     }
141   }
142   return false;
143 }
InsertDtmf(uint32_t ssrc,int event_code,int duration)144 bool FakeVoiceMediaChannel::InsertDtmf(uint32_t ssrc,
145                                        int event_code,
146                                        int duration) {
147   dtmf_info_queue_.push_back(DtmfInfo(ssrc, event_code, duration));
148   return true;
149 }
SetOutputVolume(uint32_t ssrc,double volume)150 bool FakeVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
151   if (output_scalings_.find(ssrc) != output_scalings_.end()) {
152     output_scalings_[ssrc] = volume;
153     return true;
154   }
155   return false;
156 }
SetDefaultOutputVolume(double volume)157 bool FakeVoiceMediaChannel::SetDefaultOutputVolume(double volume) {
158   for (auto& entry : output_scalings_) {
159     entry.second = volume;
160   }
161   return true;
162 }
GetOutputVolume(uint32_t ssrc,double * volume)163 bool FakeVoiceMediaChannel::GetOutputVolume(uint32_t ssrc, double* volume) {
164   if (output_scalings_.find(ssrc) == output_scalings_.end())
165     return false;
166   *volume = output_scalings_[ssrc];
167   return true;
168 }
SetBaseMinimumPlayoutDelayMs(uint32_t ssrc,int delay_ms)169 bool FakeVoiceMediaChannel::SetBaseMinimumPlayoutDelayMs(uint32_t ssrc,
170                                                          int delay_ms) {
171   if (output_delays_.find(ssrc) == output_delays_.end()) {
172     return false;
173   } else {
174     output_delays_[ssrc] = delay_ms;
175     return true;
176   }
177 }
GetBaseMinimumPlayoutDelayMs(uint32_t ssrc) const178 absl::optional<int> FakeVoiceMediaChannel::GetBaseMinimumPlayoutDelayMs(
179     uint32_t ssrc) const {
180   const auto it = output_delays_.find(ssrc);
181   if (it != output_delays_.end()) {
182     return it->second;
183   }
184   return absl::nullopt;
185 }
GetStats(VoiceMediaInfo * info)186 bool FakeVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
187   return false;
188 }
SetRawAudioSink(uint32_t ssrc,std::unique_ptr<webrtc::AudioSinkInterface> sink)189 void FakeVoiceMediaChannel::SetRawAudioSink(
190     uint32_t ssrc,
191     std::unique_ptr<webrtc::AudioSinkInterface> sink) {
192   sink_ = std::move(sink);
193 }
SetDefaultRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink)194 void FakeVoiceMediaChannel::SetDefaultRawAudioSink(
195     std::unique_ptr<webrtc::AudioSinkInterface> sink) {
196   sink_ = std::move(sink);
197 }
GetSources(uint32_t ssrc) const198 std::vector<webrtc::RtpSource> FakeVoiceMediaChannel::GetSources(
199     uint32_t ssrc) const {
200   return std::vector<webrtc::RtpSource>();
201 }
SetRecvCodecs(const std::vector<AudioCodec> & codecs)202 bool FakeVoiceMediaChannel::SetRecvCodecs(
203     const std::vector<AudioCodec>& codecs) {
204   if (fail_set_recv_codecs()) {
205     // Fake the failure in SetRecvCodecs.
206     return false;
207   }
208   recv_codecs_ = codecs;
209   return true;
210 }
SetSendCodecs(const std::vector<AudioCodec> & codecs)211 bool FakeVoiceMediaChannel::SetSendCodecs(
212     const std::vector<AudioCodec>& codecs) {
213   if (fail_set_send_codecs()) {
214     // Fake the failure in SetSendCodecs.
215     return false;
216   }
217   send_codecs_ = codecs;
218   return true;
219 }
SetMaxSendBandwidth(int bps)220 bool FakeVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
221   max_bps_ = bps;
222   return true;
223 }
SetOptions(const AudioOptions & options)224 bool FakeVoiceMediaChannel::SetOptions(const AudioOptions& options) {
225   // Does a "merge" of current options and set options.
226   options_.SetAll(options);
227   return true;
228 }
SetLocalSource(uint32_t ssrc,AudioSource * source)229 bool FakeVoiceMediaChannel::SetLocalSource(uint32_t ssrc, AudioSource* source) {
230   auto it = local_sinks_.find(ssrc);
231   if (source) {
232     if (it != local_sinks_.end()) {
233       RTC_CHECK(it->second->source() == source);
234     } else {
235       local_sinks_.insert(std::make_pair(
236           ssrc, std::make_unique<VoiceChannelAudioSink>(source)));
237     }
238   } else {
239     if (it != local_sinks_.end()) {
240       local_sinks_.erase(it);
241     }
242   }
243   return true;
244 }
245 
CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo & info,uint32_t ssrc,int event_code,int duration)246 bool CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo& info,
247                      uint32_t ssrc,
248                      int event_code,
249                      int duration) {
250   return (info.duration == duration && info.event_code == event_code &&
251           info.ssrc == ssrc);
252 }
253 
FakeVideoMediaChannel(FakeVideoEngine * engine,const VideoOptions & options)254 FakeVideoMediaChannel::FakeVideoMediaChannel(FakeVideoEngine* engine,
255                                              const VideoOptions& options)
256     : engine_(engine), max_bps_(-1) {
257   SetOptions(options);
258 }
~FakeVideoMediaChannel()259 FakeVideoMediaChannel::~FakeVideoMediaChannel() {
260   if (engine_) {
261     engine_->UnregisterChannel(this);
262   }
263 }
recv_codecs() const264 const std::vector<VideoCodec>& FakeVideoMediaChannel::recv_codecs() const {
265   return recv_codecs_;
266 }
send_codecs() const267 const std::vector<VideoCodec>& FakeVideoMediaChannel::send_codecs() const {
268   return send_codecs_;
269 }
codecs() const270 const std::vector<VideoCodec>& FakeVideoMediaChannel::codecs() const {
271   return send_codecs();
272 }
rendering() const273 bool FakeVideoMediaChannel::rendering() const {
274   return playout();
275 }
options() const276 const VideoOptions& FakeVideoMediaChannel::options() const {
277   return options_;
278 }
279 const std::map<uint32_t, rtc::VideoSinkInterface<webrtc::VideoFrame>*>&
sinks() const280 FakeVideoMediaChannel::sinks() const {
281   return sinks_;
282 }
max_bps() const283 int FakeVideoMediaChannel::max_bps() const {
284   return max_bps_;
285 }
SetSendParameters(const VideoSendParameters & params)286 bool FakeVideoMediaChannel::SetSendParameters(
287     const VideoSendParameters& params) {
288   set_send_rtcp_parameters(params.rtcp);
289   return (SetSendCodecs(params.codecs) &&
290           SetSendExtmapAllowMixed(params.extmap_allow_mixed) &&
291           SetSendRtpHeaderExtensions(params.extensions) &&
292           SetMaxSendBandwidth(params.max_bandwidth_bps));
293 }
SetRecvParameters(const VideoRecvParameters & params)294 bool FakeVideoMediaChannel::SetRecvParameters(
295     const VideoRecvParameters& params) {
296   set_recv_rtcp_parameters(params.rtcp);
297   return (SetRecvCodecs(params.codecs) &&
298           SetRecvRtpHeaderExtensions(params.extensions));
299 }
AddSendStream(const StreamParams & sp)300 bool FakeVideoMediaChannel::AddSendStream(const StreamParams& sp) {
301   return RtpHelper<VideoMediaChannel>::AddSendStream(sp);
302 }
RemoveSendStream(uint32_t ssrc)303 bool FakeVideoMediaChannel::RemoveSendStream(uint32_t ssrc) {
304   return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc);
305 }
GetSendCodec(VideoCodec * send_codec)306 bool FakeVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
307   if (send_codecs_.empty()) {
308     return false;
309   }
310   *send_codec = send_codecs_[0];
311   return true;
312 }
SetSink(uint32_t ssrc,rtc::VideoSinkInterface<webrtc::VideoFrame> * sink)313 bool FakeVideoMediaChannel::SetSink(
314     uint32_t ssrc,
315     rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
316   auto it = sinks_.find(ssrc);
317   if (it == sinks_.end()) {
318     return false;
319   }
320   it->second = sink;
321   return true;
322 }
SetDefaultSink(rtc::VideoSinkInterface<webrtc::VideoFrame> * sink)323 void FakeVideoMediaChannel::SetDefaultSink(
324     rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {}
HasSink(uint32_t ssrc) const325 bool FakeVideoMediaChannel::HasSink(uint32_t ssrc) const {
326   return sinks_.find(ssrc) != sinks_.end() && sinks_.at(ssrc) != nullptr;
327 }
SetSend(bool send)328 bool FakeVideoMediaChannel::SetSend(bool send) {
329   return set_sending(send);
330 }
SetVideoSend(uint32_t ssrc,const VideoOptions * options,rtc::VideoSourceInterface<webrtc::VideoFrame> * source)331 bool FakeVideoMediaChannel::SetVideoSend(
332     uint32_t ssrc,
333     const VideoOptions* options,
334     rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
335   if (options) {
336     if (!SetOptions(*options)) {
337       return false;
338     }
339   }
340   sources_[ssrc] = source;
341   return true;
342 }
HasSource(uint32_t ssrc) const343 bool FakeVideoMediaChannel::HasSource(uint32_t ssrc) const {
344   return sources_.find(ssrc) != sources_.end() && sources_.at(ssrc) != nullptr;
345 }
AddRecvStream(const StreamParams & sp)346 bool FakeVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
347   if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp))
348     return false;
349   sinks_[sp.first_ssrc()] = NULL;
350   output_delays_[sp.first_ssrc()] = 0;
351   return true;
352 }
RemoveRecvStream(uint32_t ssrc)353 bool FakeVideoMediaChannel::RemoveRecvStream(uint32_t ssrc) {
354   if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc))
355     return false;
356   sinks_.erase(ssrc);
357   output_delays_.erase(ssrc);
358   return true;
359 }
FillBitrateInfo(BandwidthEstimationInfo * bwe_info)360 void FakeVideoMediaChannel::FillBitrateInfo(BandwidthEstimationInfo* bwe_info) {
361 }
GetStats(VideoMediaInfo * info)362 bool FakeVideoMediaChannel::GetStats(VideoMediaInfo* info) {
363   return false;
364 }
GetSources(uint32_t ssrc) const365 std::vector<webrtc::RtpSource> FakeVideoMediaChannel::GetSources(
366     uint32_t ssrc) const {
367   return {};
368 }
SetBaseMinimumPlayoutDelayMs(uint32_t ssrc,int delay_ms)369 bool FakeVideoMediaChannel::SetBaseMinimumPlayoutDelayMs(uint32_t ssrc,
370                                                          int delay_ms) {
371   if (output_delays_.find(ssrc) == output_delays_.end()) {
372     return false;
373   } else {
374     output_delays_[ssrc] = delay_ms;
375     return true;
376   }
377 }
GetBaseMinimumPlayoutDelayMs(uint32_t ssrc) const378 absl::optional<int> FakeVideoMediaChannel::GetBaseMinimumPlayoutDelayMs(
379     uint32_t ssrc) const {
380   const auto it = output_delays_.find(ssrc);
381   if (it != output_delays_.end()) {
382     return it->second;
383   }
384   return absl::nullopt;
385 }
SetRecvCodecs(const std::vector<VideoCodec> & codecs)386 bool FakeVideoMediaChannel::SetRecvCodecs(
387     const std::vector<VideoCodec>& codecs) {
388   if (fail_set_recv_codecs()) {
389     // Fake the failure in SetRecvCodecs.
390     return false;
391   }
392   recv_codecs_ = codecs;
393   return true;
394 }
SetSendCodecs(const std::vector<VideoCodec> & codecs)395 bool FakeVideoMediaChannel::SetSendCodecs(
396     const std::vector<VideoCodec>& codecs) {
397   if (fail_set_send_codecs()) {
398     // Fake the failure in SetSendCodecs.
399     return false;
400   }
401   send_codecs_ = codecs;
402 
403   return true;
404 }
SetOptions(const VideoOptions & options)405 bool FakeVideoMediaChannel::SetOptions(const VideoOptions& options) {
406   options_ = options;
407   return true;
408 }
409 
SetMaxSendBandwidth(int bps)410 bool FakeVideoMediaChannel::SetMaxSendBandwidth(int bps) {
411   max_bps_ = bps;
412   return true;
413 }
414 
SetRecordableEncodedFrameCallback(uint32_t ssrc,std::function<void (const webrtc::RecordableEncodedFrame &)> callback)415 void FakeVideoMediaChannel::SetRecordableEncodedFrameCallback(
416     uint32_t ssrc,
417     std::function<void(const webrtc::RecordableEncodedFrame&)> callback) {}
418 
ClearRecordableEncodedFrameCallback(uint32_t ssrc)419 void FakeVideoMediaChannel::ClearRecordableEncodedFrameCallback(uint32_t ssrc) {
420 }
421 
GenerateKeyFrame(uint32_t ssrc)422 void FakeVideoMediaChannel::GenerateKeyFrame(uint32_t ssrc) {}
423 
FakeDataMediaChannel(void * unused,const DataOptions & options)424 FakeDataMediaChannel::FakeDataMediaChannel(void* unused,
425                                            const DataOptions& options)
426     : send_blocked_(false), max_bps_(-1) {}
~FakeDataMediaChannel()427 FakeDataMediaChannel::~FakeDataMediaChannel() {}
recv_codecs() const428 const std::vector<DataCodec>& FakeDataMediaChannel::recv_codecs() const {
429   return recv_codecs_;
430 }
send_codecs() const431 const std::vector<DataCodec>& FakeDataMediaChannel::send_codecs() const {
432   return send_codecs_;
433 }
codecs() const434 const std::vector<DataCodec>& FakeDataMediaChannel::codecs() const {
435   return send_codecs();
436 }
max_bps() const437 int FakeDataMediaChannel::max_bps() const {
438   return max_bps_;
439 }
SetSendParameters(const DataSendParameters & params)440 bool FakeDataMediaChannel::SetSendParameters(const DataSendParameters& params) {
441   set_send_rtcp_parameters(params.rtcp);
442   return (SetSendCodecs(params.codecs) &&
443           SetMaxSendBandwidth(params.max_bandwidth_bps));
444 }
SetRecvParameters(const DataRecvParameters & params)445 bool FakeDataMediaChannel::SetRecvParameters(const DataRecvParameters& params) {
446   set_recv_rtcp_parameters(params.rtcp);
447   return SetRecvCodecs(params.codecs);
448 }
SetSend(bool send)449 bool FakeDataMediaChannel::SetSend(bool send) {
450   return set_sending(send);
451 }
SetReceive(bool receive)452 bool FakeDataMediaChannel::SetReceive(bool receive) {
453   set_playout(receive);
454   return true;
455 }
AddRecvStream(const StreamParams & sp)456 bool FakeDataMediaChannel::AddRecvStream(const StreamParams& sp) {
457   if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp))
458     return false;
459   return true;
460 }
RemoveRecvStream(uint32_t ssrc)461 bool FakeDataMediaChannel::RemoveRecvStream(uint32_t ssrc) {
462   if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc))
463     return false;
464   return true;
465 }
SendData(const SendDataParams & params,const rtc::CopyOnWriteBuffer & payload,SendDataResult * result)466 bool FakeDataMediaChannel::SendData(const SendDataParams& params,
467                                     const rtc::CopyOnWriteBuffer& payload,
468                                     SendDataResult* result) {
469   if (send_blocked_) {
470     *result = SDR_BLOCK;
471     return false;
472   } else {
473     last_sent_data_params_ = params;
474     last_sent_data_ = std::string(payload.data<char>(), payload.size());
475     return true;
476   }
477 }
last_sent_data_params()478 SendDataParams FakeDataMediaChannel::last_sent_data_params() {
479   return last_sent_data_params_;
480 }
last_sent_data()481 std::string FakeDataMediaChannel::last_sent_data() {
482   return last_sent_data_;
483 }
is_send_blocked()484 bool FakeDataMediaChannel::is_send_blocked() {
485   return send_blocked_;
486 }
set_send_blocked(bool blocked)487 void FakeDataMediaChannel::set_send_blocked(bool blocked) {
488   send_blocked_ = blocked;
489 }
SetRecvCodecs(const std::vector<DataCodec> & codecs)490 bool FakeDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) {
491   if (fail_set_recv_codecs()) {
492     // Fake the failure in SetRecvCodecs.
493     return false;
494   }
495   recv_codecs_ = codecs;
496   return true;
497 }
SetSendCodecs(const std::vector<DataCodec> & codecs)498 bool FakeDataMediaChannel::SetSendCodecs(const std::vector<DataCodec>& codecs) {
499   if (fail_set_send_codecs()) {
500     // Fake the failure in SetSendCodecs.
501     return false;
502   }
503   send_codecs_ = codecs;
504   return true;
505 }
SetMaxSendBandwidth(int bps)506 bool FakeDataMediaChannel::SetMaxSendBandwidth(int bps) {
507   max_bps_ = bps;
508   return true;
509 }
510 
FakeVoiceEngine()511 FakeVoiceEngine::FakeVoiceEngine() : fail_create_channel_(false) {
512   // Add a fake audio codec. Note that the name must not be "" as there are
513   // sanity checks against that.
514   SetCodecs({AudioCodec(101, "fake_audio_codec", 0, 0, 1)});
515 }
Init()516 void FakeVoiceEngine::Init() {}
GetAudioState() const517 rtc::scoped_refptr<webrtc::AudioState> FakeVoiceEngine::GetAudioState() const {
518   return rtc::scoped_refptr<webrtc::AudioState>();
519 }
CreateMediaChannel(webrtc::Call * call,const MediaConfig & config,const AudioOptions & options,const webrtc::CryptoOptions & crypto_options)520 VoiceMediaChannel* FakeVoiceEngine::CreateMediaChannel(
521     webrtc::Call* call,
522     const MediaConfig& config,
523     const AudioOptions& options,
524     const webrtc::CryptoOptions& crypto_options) {
525   if (fail_create_channel_) {
526     return nullptr;
527   }
528 
529   FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this, options);
530   channels_.push_back(ch);
531   return ch;
532 }
GetChannel(size_t index)533 FakeVoiceMediaChannel* FakeVoiceEngine::GetChannel(size_t index) {
534   return (channels_.size() > index) ? channels_[index] : NULL;
535 }
UnregisterChannel(VoiceMediaChannel * channel)536 void FakeVoiceEngine::UnregisterChannel(VoiceMediaChannel* channel) {
537   channels_.erase(absl::c_find(channels_, channel));
538 }
send_codecs() const539 const std::vector<AudioCodec>& FakeVoiceEngine::send_codecs() const {
540   return send_codecs_;
541 }
recv_codecs() const542 const std::vector<AudioCodec>& FakeVoiceEngine::recv_codecs() const {
543   return recv_codecs_;
544 }
SetCodecs(const std::vector<AudioCodec> & codecs)545 void FakeVoiceEngine::SetCodecs(const std::vector<AudioCodec>& codecs) {
546   send_codecs_ = codecs;
547   recv_codecs_ = codecs;
548 }
SetRecvCodecs(const std::vector<AudioCodec> & codecs)549 void FakeVoiceEngine::SetRecvCodecs(const std::vector<AudioCodec>& codecs) {
550   recv_codecs_ = codecs;
551 }
SetSendCodecs(const std::vector<AudioCodec> & codecs)552 void FakeVoiceEngine::SetSendCodecs(const std::vector<AudioCodec>& codecs) {
553   send_codecs_ = codecs;
554 }
GetInputLevel()555 int FakeVoiceEngine::GetInputLevel() {
556   return 0;
557 }
StartAecDump(webrtc::FileWrapper file,int64_t max_size_bytes)558 bool FakeVoiceEngine::StartAecDump(webrtc::FileWrapper file,
559                                    int64_t max_size_bytes) {
560   return false;
561 }
StopAecDump()562 void FakeVoiceEngine::StopAecDump() {}
563 
564 std::vector<webrtc::RtpHeaderExtensionCapability>
GetRtpHeaderExtensions() const565 FakeVoiceEngine::GetRtpHeaderExtensions() const {
566   return header_extensions_;
567 }
568 
SetRtpHeaderExtensions(std::vector<webrtc::RtpHeaderExtensionCapability> header_extensions)569 void FakeVoiceEngine::SetRtpHeaderExtensions(
570     std::vector<webrtc::RtpHeaderExtensionCapability> header_extensions) {
571   header_extensions_ = std::move(header_extensions);
572 }
573 
FakeVideoEngine()574 FakeVideoEngine::FakeVideoEngine()
575     : capture_(false), fail_create_channel_(false) {
576   // Add a fake video codec. Note that the name must not be "" as there are
577   // sanity checks against that.
578   send_codecs_.push_back(VideoCodec(0, "fake_video_codec"));
579   recv_codecs_.push_back(VideoCodec(0, "fake_video_codec"));
580 }
SetOptions(const VideoOptions & options)581 bool FakeVideoEngine::SetOptions(const VideoOptions& options) {
582   options_ = options;
583   return true;
584 }
CreateMediaChannel(webrtc::Call * call,const MediaConfig & config,const VideoOptions & options,const webrtc::CryptoOptions & crypto_options,webrtc::VideoBitrateAllocatorFactory * video_bitrate_allocator_factory)585 VideoMediaChannel* FakeVideoEngine::CreateMediaChannel(
586     webrtc::Call* call,
587     const MediaConfig& config,
588     const VideoOptions& options,
589     const webrtc::CryptoOptions& crypto_options,
590     webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) {
591   if (fail_create_channel_) {
592     return nullptr;
593   }
594 
595   FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this, options);
596   channels_.emplace_back(ch);
597   return ch;
598 }
GetChannel(size_t index)599 FakeVideoMediaChannel* FakeVideoEngine::GetChannel(size_t index) {
600   return (channels_.size() > index) ? channels_[index] : nullptr;
601 }
UnregisterChannel(VideoMediaChannel * channel)602 void FakeVideoEngine::UnregisterChannel(VideoMediaChannel* channel) {
603   auto it = absl::c_find(channels_, channel);
604   RTC_DCHECK(it != channels_.end());
605   channels_.erase(it);
606 }
send_codecs() const607 std::vector<VideoCodec> FakeVideoEngine::send_codecs() const {
608   return send_codecs_;
609 }
610 
recv_codecs() const611 std::vector<VideoCodec> FakeVideoEngine::recv_codecs() const {
612   return recv_codecs_;
613 }
614 
SetSendCodecs(const std::vector<VideoCodec> & codecs)615 void FakeVideoEngine::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
616   send_codecs_ = codecs;
617 }
618 
SetRecvCodecs(const std::vector<VideoCodec> & codecs)619 void FakeVideoEngine::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
620   recv_codecs_ = codecs;
621 }
622 
SetCapture(bool capture)623 bool FakeVideoEngine::SetCapture(bool capture) {
624   capture_ = capture;
625   return true;
626 }
627 std::vector<webrtc::RtpHeaderExtensionCapability>
GetRtpHeaderExtensions() const628 FakeVideoEngine::GetRtpHeaderExtensions() const {
629   return header_extensions_;
630 }
SetRtpHeaderExtensions(std::vector<webrtc::RtpHeaderExtensionCapability> header_extensions)631 void FakeVideoEngine::SetRtpHeaderExtensions(
632     std::vector<webrtc::RtpHeaderExtensionCapability> header_extensions) {
633   header_extensions_ = std::move(header_extensions);
634 }
635 
FakeMediaEngine()636 FakeMediaEngine::FakeMediaEngine()
637     : CompositeMediaEngine(std::make_unique<FakeVoiceEngine>(),
638                            std::make_unique<FakeVideoEngine>()),
639       voice_(static_cast<FakeVoiceEngine*>(&voice())),
640       video_(static_cast<FakeVideoEngine*>(&video())) {}
~FakeMediaEngine()641 FakeMediaEngine::~FakeMediaEngine() {}
SetAudioCodecs(const std::vector<AudioCodec> & codecs)642 void FakeMediaEngine::SetAudioCodecs(const std::vector<AudioCodec>& codecs) {
643   voice_->SetCodecs(codecs);
644 }
SetAudioRecvCodecs(const std::vector<AudioCodec> & codecs)645 void FakeMediaEngine::SetAudioRecvCodecs(
646     const std::vector<AudioCodec>& codecs) {
647   voice_->SetRecvCodecs(codecs);
648 }
SetAudioSendCodecs(const std::vector<AudioCodec> & codecs)649 void FakeMediaEngine::SetAudioSendCodecs(
650     const std::vector<AudioCodec>& codecs) {
651   voice_->SetSendCodecs(codecs);
652 }
SetVideoCodecs(const std::vector<VideoCodec> & codecs)653 void FakeMediaEngine::SetVideoCodecs(const std::vector<VideoCodec>& codecs) {
654   video_->SetSendCodecs(codecs);
655   video_->SetRecvCodecs(codecs);
656 }
657 
GetVoiceChannel(size_t index)658 FakeVoiceMediaChannel* FakeMediaEngine::GetVoiceChannel(size_t index) {
659   return voice_->GetChannel(index);
660 }
GetVideoChannel(size_t index)661 FakeVideoMediaChannel* FakeMediaEngine::GetVideoChannel(size_t index) {
662   return video_->GetChannel(index);
663 }
664 
set_fail_create_channel(bool fail)665 void FakeMediaEngine::set_fail_create_channel(bool fail) {
666   voice_->fail_create_channel_ = fail;
667   video_->fail_create_channel_ = fail;
668 }
669 
CreateChannel(const MediaConfig & config)670 DataMediaChannel* FakeDataEngine::CreateChannel(const MediaConfig& config) {
671   FakeDataMediaChannel* ch = new FakeDataMediaChannel(this, DataOptions());
672   channels_.push_back(ch);
673   return ch;
674 }
GetChannel(size_t index)675 FakeDataMediaChannel* FakeDataEngine::GetChannel(size_t index) {
676   return (channels_.size() > index) ? channels_[index] : NULL;
677 }
UnregisterChannel(DataMediaChannel * channel)678 void FakeDataEngine::UnregisterChannel(DataMediaChannel* channel) {
679   channels_.erase(absl::c_find(channels_, channel));
680 }
SetDataCodecs(const std::vector<DataCodec> & data_codecs)681 void FakeDataEngine::SetDataCodecs(const std::vector<DataCodec>& data_codecs) {
682   data_codecs_ = data_codecs;
683 }
data_codecs()684 const std::vector<DataCodec>& FakeDataEngine::data_codecs() {
685   return data_codecs_;
686 }
687 
688 }  // namespace cricket
689