blob: c7b52c44a32997339bdaec46926f6074674a35a5 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 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#ifndef TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_
29#define TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_
30
31#include <list>
32#include <map>
33#include <set>
34#include <string>
35#include <vector>
36
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000037#include "talk/media/base/audiorenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/media/base/mediaengine.h"
39#include "talk/media/base/rtputils.h"
40#include "talk/media/base/streamparams.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000041#include "webrtc/p2p/base/sessiondescription.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000042#include "webrtc/base/buffer.h"
43#include "webrtc/base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45namespace cricket {
46
47class FakeMediaEngine;
48class FakeVideoEngine;
49class FakeVoiceEngine;
50
51// A common helper class that handles sending and receiving RTP/RTCP packets.
52template <class Base> class RtpHelper : public Base {
53 public:
54 RtpHelper()
55 : sending_(false),
56 playout_(false),
57 fail_set_send_codecs_(false),
58 fail_set_recv_codecs_(false),
59 send_ssrc_(0),
60 ready_to_send_(false) {}
61 const std::vector<RtpHeaderExtension>& recv_extensions() {
62 return recv_extensions_;
63 }
64 const std::vector<RtpHeaderExtension>& send_extensions() {
65 return send_extensions_;
66 }
67 bool sending() const { return sending_; }
68 bool playout() const { return playout_; }
69 const std::list<std::string>& rtp_packets() const { return rtp_packets_; }
70 const std::list<std::string>& rtcp_packets() const { return rtcp_packets_; }
71
72 bool SendRtp(const void* data, int len) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000073 if (!sending_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 return false;
75 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000077 return Base::SendPacket(&packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 }
79 bool SendRtcp(const void* data, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000081 return Base::SendRtcp(&packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 }
83
84 bool CheckRtp(const void* data, int len) {
85 bool success = !rtp_packets_.empty();
86 if (success) {
87 std::string packet = rtp_packets_.front();
88 rtp_packets_.pop_front();
89 success = (packet == std::string(static_cast<const char*>(data), len));
90 }
91 return success;
92 }
93 bool CheckRtcp(const void* data, int len) {
94 bool success = !rtcp_packets_.empty();
95 if (success) {
96 std::string packet = rtcp_packets_.front();
97 rtcp_packets_.pop_front();
98 success = (packet == std::string(static_cast<const char*>(data), len));
99 }
100 return success;
101 }
102 bool CheckNoRtp() { return rtp_packets_.empty(); }
103 bool CheckNoRtcp() { return rtcp_packets_.empty(); }
104 virtual bool SetRecvRtpHeaderExtensions(
105 const std::vector<RtpHeaderExtension>& extensions) {
106 recv_extensions_ = extensions;
107 return true;
108 }
109 virtual bool SetSendRtpHeaderExtensions(
110 const std::vector<RtpHeaderExtension>& extensions) {
111 send_extensions_ = extensions;
112 return true;
113 }
114 void set_fail_set_send_codecs(bool fail) { fail_set_send_codecs_ = fail; }
115 void set_fail_set_recv_codecs(bool fail) { fail_set_recv_codecs_ = fail; }
116 virtual bool AddSendStream(const StreamParams& sp) {
117 if (std::find(send_streams_.begin(), send_streams_.end(), sp) !=
118 send_streams_.end()) {
119 return false;
120 }
121 send_streams_.push_back(sp);
122 return true;
123 }
124 virtual bool RemoveSendStream(uint32 ssrc) {
125 return RemoveStreamBySsrc(&send_streams_, ssrc);
126 }
127 virtual bool AddRecvStream(const StreamParams& sp) {
128 if (std::find(receive_streams_.begin(), receive_streams_.end(), sp) !=
129 receive_streams_.end()) {
130 return false;
131 }
132 receive_streams_.push_back(sp);
133 return true;
134 }
135 virtual bool RemoveRecvStream(uint32 ssrc) {
136 return RemoveStreamBySsrc(&receive_streams_, ssrc);
137 }
138 virtual bool MuteStream(uint32 ssrc, bool on) {
139 if (!HasSendStream(ssrc) && ssrc != 0)
140 return false;
141 if (on)
142 muted_streams_.insert(ssrc);
143 else
144 muted_streams_.erase(ssrc);
145 return true;
146 }
147 bool IsStreamMuted(uint32 ssrc) const {
148 bool ret = muted_streams_.find(ssrc) != muted_streams_.end();
149 // If |ssrc = 0| check if the first send stream is muted.
150 if (!ret && ssrc == 0 && !send_streams_.empty()) {
151 return muted_streams_.find(send_streams_[0].first_ssrc()) !=
152 muted_streams_.end();
153 }
154 return ret;
155 }
156 const std::vector<StreamParams>& send_streams() const {
157 return send_streams_;
158 }
159 const std::vector<StreamParams>& recv_streams() const {
160 return receive_streams_;
161 }
162 bool HasRecvStream(uint32 ssrc) const {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000163 return GetStreamBySsrc(receive_streams_, ssrc) != nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 }
165 bool HasSendStream(uint32 ssrc) const {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000166 return GetStreamBySsrc(send_streams_, ssrc) != nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 }
168 // TODO(perkj): This is to support legacy unit test that only check one
169 // sending stream.
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000170 uint32 send_ssrc() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 if (send_streams_.empty())
172 return 0;
173 return send_streams_[0].first_ssrc();
174 }
175
176 // TODO(perkj): This is to support legacy unit test that only check one
177 // sending stream.
178 const std::string rtcp_cname() {
179 if (send_streams_.empty())
180 return "";
181 return send_streams_[0].cname;
182 }
183
184 bool ready_to_send() const {
185 return ready_to_send_;
186 }
187
188 protected:
189 bool set_sending(bool send) {
190 sending_ = send;
191 return true;
192 }
193 void set_playout(bool playout) { playout_ = playout; }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000194 virtual void OnPacketReceived(rtc::Buffer* packet,
195 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 rtp_packets_.push_back(std::string(packet->data(), packet->length()));
197 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 virtual void OnRtcpReceived(rtc::Buffer* packet,
199 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 rtcp_packets_.push_back(std::string(packet->data(), packet->length()));
201 }
202 virtual void OnReadyToSend(bool ready) {
203 ready_to_send_ = ready;
204 }
205 bool fail_set_send_codecs() const { return fail_set_send_codecs_; }
206 bool fail_set_recv_codecs() const { return fail_set_recv_codecs_; }
207
208 private:
209 bool sending_;
210 bool playout_;
211 std::vector<RtpHeaderExtension> recv_extensions_;
212 std::vector<RtpHeaderExtension> send_extensions_;
213 std::list<std::string> rtp_packets_;
214 std::list<std::string> rtcp_packets_;
215 std::vector<StreamParams> send_streams_;
216 std::vector<StreamParams> receive_streams_;
217 std::set<uint32> muted_streams_;
218 bool fail_set_send_codecs_;
219 bool fail_set_recv_codecs_;
220 uint32 send_ssrc_;
221 std::string rtcp_cname_;
222 bool ready_to_send_;
223};
224
225class FakeVoiceMediaChannel : public RtpHelper<VoiceMediaChannel> {
226 public:
227 struct DtmfInfo {
228 DtmfInfo(uint32 ssrc, int event_code, int duration, int flags)
229 : ssrc(ssrc), event_code(event_code), duration(duration), flags(flags) {
230 }
231 uint32 ssrc;
232 int event_code;
233 int duration;
234 int flags;
235 };
236 explicit FakeVoiceMediaChannel(FakeVoiceEngine* engine)
237 : engine_(engine),
238 fail_set_send_(false),
239 ringback_tone_ssrc_(0),
240 ringback_tone_play_(false),
241 ringback_tone_loop_(false),
242 time_since_last_typing_(-1) {
243 output_scalings_[0] = OutputScaling(); // For default channel.
244 }
245 ~FakeVoiceMediaChannel();
246 const std::vector<AudioCodec>& recv_codecs() const { return recv_codecs_; }
247 const std::vector<AudioCodec>& send_codecs() const { return send_codecs_; }
248 const std::vector<AudioCodec>& codecs() const { return send_codecs(); }
249 const std::vector<DtmfInfo>& dtmf_info_queue() const {
250 return dtmf_info_queue_;
251 }
252 const AudioOptions& options() const { return options_; }
253
254 uint32 ringback_tone_ssrc() const { return ringback_tone_ssrc_; }
255 bool ringback_tone_play() const { return ringback_tone_play_; }
256 bool ringback_tone_loop() const { return ringback_tone_loop_; }
257
258 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) {
259 if (fail_set_recv_codecs()) {
260 // Fake the failure in SetRecvCodecs.
261 return false;
262 }
263 recv_codecs_ = codecs;
264 return true;
265 }
266 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs) {
267 if (fail_set_send_codecs()) {
268 // Fake the failure in SetSendCodecs.
269 return false;
270 }
271 send_codecs_ = codecs;
272 return true;
273 }
274 virtual bool SetPlayout(bool playout) {
275 set_playout(playout);
276 return true;
277 }
278 virtual bool SetSend(SendFlags flag) {
279 if (fail_set_send_) {
280 return false;
281 }
282 return set_sending(flag != SEND_NOTHING);
283 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000284 virtual bool SetMaxSendBandwidth(int bps) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 virtual bool AddRecvStream(const StreamParams& sp) {
286 if (!RtpHelper<VoiceMediaChannel>::AddRecvStream(sp))
287 return false;
288 output_scalings_[sp.first_ssrc()] = OutputScaling();
289 return true;
290 }
291 virtual bool RemoveRecvStream(uint32 ssrc) {
292 if (!RtpHelper<VoiceMediaChannel>::RemoveRecvStream(ssrc))
293 return false;
294 output_scalings_.erase(ssrc);
295 return true;
296 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000297 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
298 std::map<uint32, AudioRenderer*>::iterator it =
299 remote_renderers_.find(ssrc);
300 if (renderer) {
301 if (it != remote_renderers_.end()) {
302 ASSERT(it->second == renderer);
303 } else {
304 remote_renderers_.insert(std::make_pair(ssrc, renderer));
305 renderer->AddChannel(0);
306 }
307 } else {
308 if (it != remote_renderers_.end()) {
309 it->second->RemoveChannel(0);
310 remote_renderers_.erase(it);
311 } else {
312 return false;
313 }
314 }
315 return true;
316 }
317 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000318 std::map<uint32, VoiceChannelAudioSink*>::iterator it =
319 local_renderers_.find(ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000320 if (renderer) {
321 if (it != local_renderers_.end()) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000322 ASSERT(it->second->renderer() == renderer);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000323 } else {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000324 local_renderers_.insert(std::make_pair(
325 ssrc, new VoiceChannelAudioSink(renderer)));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000326 }
327 } else {
328 if (it != local_renderers_.end()) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000329 delete it->second;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000330 local_renderers_.erase(it);
331 } else {
332 return false;
333 }
334 }
335 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 }
337
338 virtual bool GetActiveStreams(AudioInfo::StreamList* streams) { return true; }
339 virtual int GetOutputLevel() { return 0; }
340 void set_time_since_last_typing(int ms) { time_since_last_typing_ = ms; }
341 virtual int GetTimeSinceLastTyping() { return time_since_last_typing_; }
342 virtual void SetTypingDetectionParameters(
343 int time_window, int cost_per_typing, int reporting_threshold,
344 int penalty_decay, int type_event_delay) {}
345
346 virtual bool SetRingbackTone(const char* buf, int len) { return true; }
347 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
348 ringback_tone_ssrc_ = ssrc;
349 ringback_tone_play_ = play;
350 ringback_tone_loop_ = loop;
351 return true;
352 }
353
354 virtual bool CanInsertDtmf() {
355 for (std::vector<AudioCodec>::const_iterator it = send_codecs_.begin();
356 it != send_codecs_.end(); ++it) {
357 // Find the DTMF telephone event "codec".
358 if (_stricmp(it->name.c_str(), "telephone-event") == 0) {
359 return true;
360 }
361 }
362 return false;
363 }
364 virtual bool InsertDtmf(uint32 ssrc, int event_code, int duration,
365 int flags) {
366 dtmf_info_queue_.push_back(DtmfInfo(ssrc, event_code, duration, flags));
367 return true;
368 }
369
370 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) {
371 if (0 == ssrc) {
372 std::map<uint32, OutputScaling>::iterator it;
373 for (it = output_scalings_.begin(); it != output_scalings_.end(); ++it) {
374 it->second.left = left;
375 it->second.right = right;
376 }
377 return true;
378 } else if (output_scalings_.find(ssrc) != output_scalings_.end()) {
379 output_scalings_[ssrc].left = left;
380 output_scalings_[ssrc].right = right;
381 return true;
382 }
383 return false;
384 }
385 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) {
386 if (output_scalings_.find(ssrc) == output_scalings_.end())
387 return false;
388 *left = output_scalings_[ssrc].left;
389 *right = output_scalings_[ssrc].right;
390 return true;
391 }
392
393 virtual bool GetStats(VoiceMediaInfo* info) { return false; }
394 virtual void GetLastMediaError(uint32* ssrc,
395 VoiceMediaChannel::Error* error) {
396 *ssrc = 0;
397 *error = fail_set_send_ ? VoiceMediaChannel::ERROR_REC_DEVICE_OPEN_FAILED
398 : VoiceMediaChannel::ERROR_NONE;
399 }
400
401 void set_fail_set_send(bool fail) { fail_set_send_ = fail; }
402 void TriggerError(uint32 ssrc, VoiceMediaChannel::Error error) {
403 VoiceMediaChannel::SignalMediaError(ssrc, error);
404 }
405
406 virtual bool SetOptions(const AudioOptions& options) {
407 // Does a "merge" of current options and set options.
408 options_.SetAll(options);
409 return true;
410 }
411 virtual bool GetOptions(AudioOptions* options) const {
412 *options = options_;
413 return true;
414 }
415
416 private:
417 struct OutputScaling {
418 OutputScaling() : left(1.0), right(1.0) {}
419 double left, right;
420 };
421
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000422 class VoiceChannelAudioSink : public AudioRenderer::Sink {
423 public:
424 explicit VoiceChannelAudioSink(AudioRenderer* renderer)
425 : renderer_(renderer) {
426 renderer_->AddChannel(0);
427 renderer_->SetSink(this);
428 }
429 virtual ~VoiceChannelAudioSink() {
430 if (renderer_) {
431 renderer_->RemoveChannel(0);
432 renderer_->SetSink(NULL);
433 }
434 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000435 void OnData(const void* audio_data,
436 int bits_per_sample,
437 int sample_rate,
438 int number_of_channels,
439 int number_of_frames) override {}
440 void OnClose() override { renderer_ = NULL; }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000441 AudioRenderer* renderer() const { return renderer_; }
442
443 private:
444 AudioRenderer* renderer_;
445 };
446
447
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 FakeVoiceEngine* engine_;
449 std::vector<AudioCodec> recv_codecs_;
450 std::vector<AudioCodec> send_codecs_;
451 std::map<uint32, OutputScaling> output_scalings_;
452 std::vector<DtmfInfo> dtmf_info_queue_;
453 bool fail_set_send_;
454 uint32 ringback_tone_ssrc_;
455 bool ringback_tone_play_;
456 bool ringback_tone_loop_;
457 int time_since_last_typing_;
458 AudioOptions options_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000459 std::map<uint32, VoiceChannelAudioSink*> local_renderers_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000460 std::map<uint32, AudioRenderer*> remote_renderers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461};
462
463// A helper function to compare the FakeVoiceMediaChannel::DtmfInfo.
464inline bool CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo& info,
465 uint32 ssrc, int event_code, int duration,
466 int flags) {
467 return (info.duration == duration && info.event_code == event_code &&
468 info.flags == flags && info.ssrc == ssrc);
469}
470
471class FakeVideoMediaChannel : public RtpHelper<VideoMediaChannel> {
472 public:
473 explicit FakeVideoMediaChannel(FakeVideoEngine* engine)
474 : engine_(engine),
475 sent_intra_frame_(false),
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000476 requested_intra_frame_(false),
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000477 max_bps_(-1) {}
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000478
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 ~FakeVideoMediaChannel();
480
481 const std::vector<VideoCodec>& recv_codecs() const { return recv_codecs_; }
482 const std::vector<VideoCodec>& send_codecs() const { return send_codecs_; }
483 const std::vector<VideoCodec>& codecs() const { return send_codecs(); }
484 bool rendering() const { return playout(); }
485 const VideoOptions& options() const { return options_; }
486 const std::map<uint32, VideoRenderer*>& renderers() const {
487 return renderers_;
488 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000489 int max_bps() const { return max_bps_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 bool GetSendStreamFormat(uint32 ssrc, VideoFormat* format) {
491 if (send_formats_.find(ssrc) == send_formats_.end()) {
492 return false;
493 }
494 *format = send_formats_[ssrc];
495 return true;
496 }
497 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) {
498 if (send_formats_.find(ssrc) == send_formats_.end()) {
499 return false;
500 }
501 send_formats_[ssrc] = format;
502 return true;
503 }
504
505 virtual bool AddSendStream(const StreamParams& sp) {
506 if (!RtpHelper<VideoMediaChannel>::AddSendStream(sp)) {
507 return false;
508 }
509 SetSendStreamDefaultFormat(sp.first_ssrc());
510 return true;
511 }
512 virtual bool RemoveSendStream(uint32 ssrc) {
513 send_formats_.erase(ssrc);
514 return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc);
515 }
516
517 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
518 if (fail_set_recv_codecs()) {
519 // Fake the failure in SetRecvCodecs.
520 return false;
521 }
522 recv_codecs_ = codecs;
523 return true;
524 }
525 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) {
526 if (fail_set_send_codecs()) {
527 // Fake the failure in SetSendCodecs.
528 return false;
529 }
530 send_codecs_ = codecs;
531
532 for (std::vector<StreamParams>::const_iterator it = send_streams().begin();
533 it != send_streams().end(); ++it) {
534 SetSendStreamDefaultFormat(it->first_ssrc());
535 }
536 return true;
537 }
538 virtual bool GetSendCodec(VideoCodec* send_codec) {
539 if (send_codecs_.empty()) {
540 return false;
541 }
542 *send_codec = send_codecs_[0];
543 return true;
544 }
545 virtual bool SetRender(bool render) {
546 set_playout(render);
547 return true;
548 }
549 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* r) {
550 if (ssrc != 0 && renderers_.find(ssrc) == renderers_.end()) {
551 return false;
552 }
553 if (ssrc != 0) {
554 renderers_[ssrc] = r;
555 }
556 return true;
557 }
558
559 virtual bool SetSend(bool send) { return set_sending(send); }
560 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
561 capturers_[ssrc] = capturer;
562 return true;
563 }
564 bool HasCapturer(uint32 ssrc) const {
565 return capturers_.find(ssrc) != capturers_.end();
566 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000567 virtual bool SetMaxSendBandwidth(int bps) {
568 max_bps_ = bps;
569 return true;
570 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 virtual bool AddRecvStream(const StreamParams& sp) {
572 if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp))
573 return false;
574 renderers_[sp.first_ssrc()] = NULL;
575 return true;
576 }
577 virtual bool RemoveRecvStream(uint32 ssrc) {
578 if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc))
579 return false;
580 renderers_.erase(ssrc);
581 return true;
582 }
583
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000584 virtual bool GetStats(VideoMediaInfo* info) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 virtual bool SendIntraFrame() {
586 sent_intra_frame_ = true;
587 return true;
588 }
589 virtual bool RequestIntraFrame() {
590 requested_intra_frame_ = true;
591 return true;
592 }
593 virtual bool SetOptions(const VideoOptions& options) {
594 options_ = options;
595 return true;
596 }
597 virtual bool GetOptions(VideoOptions* options) const {
598 *options = options_;
599 return true;
600 }
601 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {}
602 void set_sent_intra_frame(bool v) { sent_intra_frame_ = v; }
603 bool sent_intra_frame() const { return sent_intra_frame_; }
604 void set_requested_intra_frame(bool v) { requested_intra_frame_ = v; }
605 bool requested_intra_frame() const { return requested_intra_frame_; }
606
607 private:
608 // Be default, each send stream uses the first send codec format.
609 void SetSendStreamDefaultFormat(uint32 ssrc) {
610 if (!send_codecs_.empty()) {
611 send_formats_[ssrc] = VideoFormat(
612 send_codecs_[0].width, send_codecs_[0].height,
613 cricket::VideoFormat::FpsToInterval(send_codecs_[0].framerate),
614 cricket::FOURCC_I420);
615 }
616 }
617
618 FakeVideoEngine* engine_;
619 std::vector<VideoCodec> recv_codecs_;
620 std::vector<VideoCodec> send_codecs_;
621 std::map<uint32, VideoRenderer*> renderers_;
622 std::map<uint32, VideoFormat> send_formats_;
623 std::map<uint32, VideoCapturer*> capturers_;
624 bool sent_intra_frame_;
625 bool requested_intra_frame_;
626 VideoOptions options_;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000627 int max_bps_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628};
629
630class FakeSoundclipMedia : public SoundclipMedia {
631 public:
632 virtual bool PlaySound(const char* buf, int len, int flags) { return true; }
633};
634
635class FakeDataMediaChannel : public RtpHelper<DataMediaChannel> {
636 public:
637 explicit FakeDataMediaChannel(void* unused)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000638 : send_blocked_(false), max_bps_(-1) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 ~FakeDataMediaChannel() {}
640 const std::vector<DataCodec>& recv_codecs() const { return recv_codecs_; }
641 const std::vector<DataCodec>& send_codecs() const { return send_codecs_; }
642 const std::vector<DataCodec>& codecs() const { return send_codecs(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 int max_bps() const { return max_bps_; }
644
645 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) {
646 if (fail_set_recv_codecs()) {
647 // Fake the failure in SetRecvCodecs.
648 return false;
649 }
650 recv_codecs_ = codecs;
651 return true;
652 }
653 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) {
654 if (fail_set_send_codecs()) {
655 // Fake the failure in SetSendCodecs.
656 return false;
657 }
658 send_codecs_ = codecs;
659 return true;
660 }
661 virtual bool SetSend(bool send) { return set_sending(send); }
662 virtual bool SetReceive(bool receive) {
663 set_playout(receive);
664 return true;
665 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000666 virtual bool SetMaxSendBandwidth(int bps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 max_bps_ = bps;
668 return true;
669 }
670 virtual bool AddRecvStream(const StreamParams& sp) {
671 if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp))
672 return false;
673 return true;
674 }
675 virtual bool RemoveRecvStream(uint32 ssrc) {
676 if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc))
677 return false;
678 return true;
679 }
680
681 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000682 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 SendDataResult* result) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000684 if (send_blocked_) {
685 *result = SDR_BLOCK;
686 return false;
687 } else {
688 last_sent_data_params_ = params;
689 last_sent_data_ = std::string(payload.data(), payload.length());
690 return true;
691 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 }
693
694 SendDataParams last_sent_data_params() { return last_sent_data_params_; }
695 std::string last_sent_data() { return last_sent_data_; }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000696 bool is_send_blocked() { return send_blocked_; }
697 void set_send_blocked(bool blocked) { send_blocked_ = blocked; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698
699 private:
700 std::vector<DataCodec> recv_codecs_;
701 std::vector<DataCodec> send_codecs_;
702 SendDataParams last_sent_data_params_;
703 std::string last_sent_data_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000704 bool send_blocked_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 int max_bps_;
706};
707
708// A base class for all of the shared parts between FakeVoiceEngine
709// and FakeVideoEngine.
710class FakeBaseEngine {
711 public:
712 FakeBaseEngine()
713 : loglevel_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 options_changed_(false),
715 fail_create_channel_(false) {}
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000716 bool Init(rtc::Thread* worker_thread) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 void Terminate() {}
718
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 void SetLogging(int level, const char* filter) {
720 loglevel_ = level;
721 logfilter_ = filter;
722 }
723
724 void set_fail_create_channel(bool fail) { fail_create_channel_ = fail; }
725
726 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const {
727 return rtp_header_extensions_;
728 }
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000729 void set_rtp_header_extensions(
730 const std::vector<RtpHeaderExtension>& extensions) {
731 rtp_header_extensions_ = extensions;
732 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733
734 protected:
735 int loglevel_;
736 std::string logfilter_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 // Flag used by optionsmessagehandler_unittest for checking whether any
738 // relevant setting has been updated.
739 // TODO(thaloun): Replace with explicit checks of before & after values.
740 bool options_changed_;
741 bool fail_create_channel_;
742 std::vector<RtpHeaderExtension> rtp_header_extensions_;
743};
744
745class FakeVoiceEngine : public FakeBaseEngine {
746 public:
747 FakeVoiceEngine()
748 : output_volume_(-1),
749 delay_offset_(0),
750 rx_processor_(NULL),
751 tx_processor_(NULL) {
752 // Add a fake audio codec. Note that the name must not be "" as there are
753 // sanity checks against that.
754 codecs_.push_back(AudioCodec(101, "fake_audio_codec", 0, 0, 1, 0));
755 }
756 int GetCapabilities() { return AUDIO_SEND | AUDIO_RECV; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000757 AudioOptions GetAudioOptions() const {
758 return options_;
759 }
760 AudioOptions GetOptions() const {
761 return options_;
762 }
763 bool SetOptions(const AudioOptions& options) {
764 options_ = options;
765 options_changed_ = true;
766 return true;
767 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
769 VoiceMediaChannel* CreateChannel() {
770 if (fail_create_channel_) {
771 return NULL;
772 }
773
774 FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this);
775 channels_.push_back(ch);
776 return ch;
777 }
778 FakeVoiceMediaChannel* GetChannel(size_t index) {
779 return (channels_.size() > index) ? channels_[index] : NULL;
780 }
781 void UnregisterChannel(VoiceMediaChannel* channel) {
782 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
783 }
784 SoundclipMedia* CreateSoundclip() { return new FakeSoundclipMedia(); }
785
786 const std::vector<AudioCodec>& codecs() { return codecs_; }
787 void SetCodecs(const std::vector<AudioCodec> codecs) { codecs_ = codecs; }
788
789 bool SetDelayOffset(int offset) {
790 delay_offset_ = offset;
791 return true;
792 }
793
794 bool SetDevices(const Device* in_device, const Device* out_device) {
795 in_device_ = (in_device) ? in_device->name : "";
796 out_device_ = (out_device) ? out_device->name : "";
797 options_changed_ = true;
798 return true;
799 }
800
801 bool GetOutputVolume(int* level) {
802 *level = output_volume_;
803 return true;
804 }
805
806 bool SetOutputVolume(int level) {
807 output_volume_ = level;
808 options_changed_ = true;
809 return true;
810 }
811
812 int GetInputLevel() { return 0; }
813
814 bool SetLocalMonitor(bool enable) { return true; }
815
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000816 bool StartAecDump(rtc::PlatformFile file) { return false; }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000817
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 bool RegisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor,
819 MediaProcessorDirection direction) {
820 if (direction == MPD_RX) {
821 rx_processor_ = voice_processor;
822 return true;
823 } else if (direction == MPD_TX) {
824 tx_processor_ = voice_processor;
825 return true;
826 }
827 return false;
828 }
829
830 bool UnregisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor,
831 MediaProcessorDirection direction) {
832 bool unregistered = false;
833 if (direction & MPD_RX) {
834 rx_processor_ = NULL;
835 unregistered = true;
836 }
837 if (direction & MPD_TX) {
838 tx_processor_ = NULL;
839 unregistered = true;
840 }
841 return unregistered;
842 }
843
844 private:
845 std::vector<FakeVoiceMediaChannel*> channels_;
846 std::vector<AudioCodec> codecs_;
847 int output_volume_;
848 int delay_offset_;
849 std::string in_device_;
850 std::string out_device_;
851 VoiceProcessor* rx_processor_;
852 VoiceProcessor* tx_processor_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000853 AudioOptions options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854
855 friend class FakeMediaEngine;
856};
857
858class FakeVideoEngine : public FakeBaseEngine {
859 public:
pbos@webrtc.orgf1f0d9a2015-03-02 13:30:15 +0000860 FakeVideoEngine() : FakeVideoEngine(nullptr) {}
861 explicit FakeVideoEngine(FakeVoiceEngine* voice)
862 : capture_(false), processor_(NULL) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 // Add a fake video codec. Note that the name must not be "" as there are
864 // sanity checks against that.
865 codecs_.push_back(VideoCodec(0, "fake_video_codec", 0, 0, 0, 0));
866 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000867 bool GetOptions(VideoOptions* options) const {
868 *options = options_;
869 return true;
870 }
871 bool SetOptions(const VideoOptions& options) {
872 options_ = options;
873 options_changed_ = true;
874 return true;
875 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 int GetCapabilities() { return VIDEO_SEND | VIDEO_RECV; }
877 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
878 default_encoder_config_ = config;
879 return true;
880 }
881 const VideoEncoderConfig& default_encoder_config() const {
882 return default_encoder_config_;
883 }
884
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000885 VideoMediaChannel* CreateChannel(const VideoOptions& options,
886 VoiceMediaChannel* channel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 if (fail_create_channel_) {
888 return NULL;
889 }
890
891 FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000892 ch->SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 channels_.push_back(ch);
894 return ch;
895 }
896 FakeVideoMediaChannel* GetChannel(size_t index) {
897 return (channels_.size() > index) ? channels_[index] : NULL;
898 }
899 void UnregisterChannel(VideoMediaChannel* channel) {
900 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
901 }
902
903 const std::vector<VideoCodec>& codecs() const { return codecs_; }
904 bool FindCodec(const VideoCodec& in) {
905 for (size_t i = 0; i < codecs_.size(); ++i) {
906 if (codecs_[i].Matches(in)) {
907 return true;
908 }
909 }
910 return false;
911 }
912 void SetCodecs(const std::vector<VideoCodec> codecs) { codecs_ = codecs; }
913
914 bool SetCaptureDevice(const Device* device) {
915 in_device_ = (device) ? device->name : "";
916 options_changed_ = true;
917 return true;
918 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 bool SetCapture(bool capture) {
920 capture_ = capture;
921 return true;
922 }
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000923 VideoFormat GetStartCaptureFormat() const {
924 return VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30),
925 FOURCC_I420);
926 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927
928 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
929
930 private:
931 std::vector<FakeVideoMediaChannel*> channels_;
932 std::vector<VideoCodec> codecs_;
933 VideoEncoderConfig default_encoder_config_;
934 std::string in_device_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 bool capture_;
936 VideoProcessor* processor_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000937 VideoOptions options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938
939 friend class FakeMediaEngine;
940};
941
942class FakeMediaEngine :
943 public CompositeMediaEngine<FakeVoiceEngine, FakeVideoEngine> {
944 public:
945 FakeMediaEngine() {
946 voice_ = FakeVoiceEngine();
947 video_ = FakeVideoEngine();
948 }
949 virtual ~FakeMediaEngine() {}
950
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000951 void SetAudioCodecs(const std::vector<AudioCodec>& codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 voice_.SetCodecs(codecs);
953 }
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000954 void SetVideoCodecs(const std::vector<VideoCodec>& codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 video_.SetCodecs(codecs);
956 }
957
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000958 void SetAudioRtpHeaderExtensions(
959 const std::vector<RtpHeaderExtension>& extensions) {
960 voice_.set_rtp_header_extensions(extensions);
961 }
962 void SetVideoRtpHeaderExtensions(
963 const std::vector<RtpHeaderExtension>& extensions) {
964 video_.set_rtp_header_extensions(extensions);
965 }
966
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 FakeVoiceMediaChannel* GetVoiceChannel(size_t index) {
968 return voice_.GetChannel(index);
969 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 FakeVideoMediaChannel* GetVideoChannel(size_t index) {
971 return video_.GetChannel(index);
972 }
973
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000974 AudioOptions audio_options() const { return voice_.options_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 int audio_delay_offset() const { return voice_.delay_offset_; }
976 int output_volume() const { return voice_.output_volume_; }
977 const VideoEncoderConfig& default_video_encoder_config() const {
978 return video_.default_encoder_config_;
979 }
980 const std::string& audio_in_device() const { return voice_.in_device_; }
981 const std::string& audio_out_device() const { return voice_.out_device_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 int voice_loglevel() const { return voice_.loglevel_; }
983 const std::string& voice_logfilter() const { return voice_.logfilter_; }
984 int video_loglevel() const { return video_.loglevel_; }
985 const std::string& video_logfilter() const { return video_.logfilter_; }
986 bool capture() const { return video_.capture_; }
987 bool options_changed() const {
988 return voice_.options_changed_ || video_.options_changed_;
989 }
990 void clear_options_changed() {
991 video_.options_changed_ = false;
992 voice_.options_changed_ = false;
993 }
994 void set_fail_create_channel(bool fail) {
995 voice_.set_fail_create_channel(fail);
996 video_.set_fail_create_channel(fail);
997 }
998 bool voice_processor_registered(MediaProcessorDirection direction) const {
999 if (direction == MPD_RX) {
1000 return voice_.rx_processor_ != NULL;
1001 } else if (direction == MPD_TX) {
1002 return voice_.tx_processor_ != NULL;
1003 }
1004 return false;
1005 }
1006};
1007
1008// CompositeMediaEngine with FakeVoiceEngine to expose SetAudioCodecs to
1009// establish a media connectionwith minimum set of audio codes required
1010template <class VIDEO>
1011class CompositeMediaEngineWithFakeVoiceEngine :
1012 public CompositeMediaEngine<FakeVoiceEngine, VIDEO> {
1013 public:
1014 CompositeMediaEngineWithFakeVoiceEngine() {}
1015 virtual ~CompositeMediaEngineWithFakeVoiceEngine() {}
1016
1017 virtual void SetAudioCodecs(const std::vector<AudioCodec>& codecs) {
1018 CompositeMediaEngine<FakeVoiceEngine, VIDEO>::voice_.SetCodecs(codecs);
1019 }
1020};
1021
1022// Have to come afterwards due to declaration order
1023inline FakeVoiceMediaChannel::~FakeVoiceMediaChannel() {
1024 if (engine_) {
1025 engine_->UnregisterChannel(this);
1026 }
1027}
1028
1029inline FakeVideoMediaChannel::~FakeVideoMediaChannel() {
1030 if (engine_) {
1031 engine_->UnregisterChannel(this);
1032 }
1033}
1034
1035class FakeDataEngine : public DataEngineInterface {
1036 public:
1037 FakeDataEngine() : last_channel_type_(DCT_NONE) {}
1038
1039 virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type) {
1040 last_channel_type_ = data_channel_type;
1041 FakeDataMediaChannel* ch = new FakeDataMediaChannel(this);
1042 channels_.push_back(ch);
1043 return ch;
1044 }
1045
1046 FakeDataMediaChannel* GetChannel(size_t index) {
1047 return (channels_.size() > index) ? channels_[index] : NULL;
1048 }
1049
1050 void UnregisterChannel(DataMediaChannel* channel) {
1051 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
1052 }
1053
1054 virtual void SetDataCodecs(const std::vector<DataCodec>& data_codecs) {
1055 data_codecs_ = data_codecs;
1056 }
1057
1058 virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; }
1059
1060 DataChannelType last_channel_type() const { return last_channel_type_; }
1061
1062 private:
1063 std::vector<FakeDataMediaChannel*> channels_;
1064 std::vector<DataCodec> data_codecs_;
1065 DataChannelType last_channel_type_;
1066};
1067
1068} // namespace cricket
1069
1070#endif // TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_