blob: 9ffe07453b9a5f6abf421afdf9dc03e46f2bbcd5 [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
37#include "talk/base/buffer.h"
38#include "talk/base/stringutils.h"
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000039#include "talk/media/base/audiorenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/media/base/mediaengine.h"
41#include "talk/media/base/rtputils.h"
42#include "talk/media/base/streamparams.h"
43#include "talk/p2p/base/sessiondescription.h"
44
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 }
76 talk_base::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) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 talk_base::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 {
163 return GetStreamBySsrc(receive_streams_, ssrc, NULL);
164 }
165 bool HasSendStream(uint32 ssrc) const {
166 return GetStreamBySsrc(send_streams_, ssrc, NULL);
167 }
168 // TODO(perkj): This is to support legacy unit test that only check one
169 // sending stream.
170 const uint32 send_ssrc() {
171 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; }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000194 virtual void OnPacketReceived(talk_base::Buffer* packet,
195 const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 rtp_packets_.push_back(std::string(packet->data(), packet->length()));
197 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000198 virtual void OnRtcpReceived(talk_base::Buffer* packet,
199 const talk_base::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 SetStartSendBandwidth(int bps) { return true; }
285 virtual bool SetMaxSendBandwidth(int bps) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 virtual bool AddRecvStream(const StreamParams& sp) {
287 if (!RtpHelper<VoiceMediaChannel>::AddRecvStream(sp))
288 return false;
289 output_scalings_[sp.first_ssrc()] = OutputScaling();
290 return true;
291 }
292 virtual bool RemoveRecvStream(uint32 ssrc) {
293 if (!RtpHelper<VoiceMediaChannel>::RemoveRecvStream(ssrc))
294 return false;
295 output_scalings_.erase(ssrc);
296 return true;
297 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000298 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
299 std::map<uint32, AudioRenderer*>::iterator it =
300 remote_renderers_.find(ssrc);
301 if (renderer) {
302 if (it != remote_renderers_.end()) {
303 ASSERT(it->second == renderer);
304 } else {
305 remote_renderers_.insert(std::make_pair(ssrc, renderer));
306 renderer->AddChannel(0);
307 }
308 } else {
309 if (it != remote_renderers_.end()) {
310 it->second->RemoveChannel(0);
311 remote_renderers_.erase(it);
312 } else {
313 return false;
314 }
315 }
316 return true;
317 }
318 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
319 std::map<uint32, AudioRenderer*>::iterator it = local_renderers_.find(ssrc);
320 if (renderer) {
321 if (it != local_renderers_.end()) {
322 ASSERT(it->second == renderer);
323 } else {
324 local_renderers_.insert(std::make_pair(ssrc, renderer));
325 renderer->AddChannel(0);
326 }
327 } else {
328 if (it != local_renderers_.end()) {
329 it->second->RemoveChannel(0);
330 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
422 FakeVoiceEngine* engine_;
423 std::vector<AudioCodec> recv_codecs_;
424 std::vector<AudioCodec> send_codecs_;
425 std::map<uint32, OutputScaling> output_scalings_;
426 std::vector<DtmfInfo> dtmf_info_queue_;
427 bool fail_set_send_;
428 uint32 ringback_tone_ssrc_;
429 bool ringback_tone_play_;
430 bool ringback_tone_loop_;
431 int time_since_last_typing_;
432 AudioOptions options_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000433 std::map<uint32, AudioRenderer*> local_renderers_;
434 std::map<uint32, AudioRenderer*> remote_renderers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435};
436
437// A helper function to compare the FakeVoiceMediaChannel::DtmfInfo.
438inline bool CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo& info,
439 uint32 ssrc, int event_code, int duration,
440 int flags) {
441 return (info.duration == duration && info.event_code == event_code &&
442 info.flags == flags && info.ssrc == ssrc);
443}
444
445class FakeVideoMediaChannel : public RtpHelper<VideoMediaChannel> {
446 public:
447 explicit FakeVideoMediaChannel(FakeVideoEngine* engine)
448 : engine_(engine),
449 sent_intra_frame_(false),
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000450 requested_intra_frame_(false),
451 start_bps_(-1),
452 max_bps_(-1) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 ~FakeVideoMediaChannel();
454
455 const std::vector<VideoCodec>& recv_codecs() const { return recv_codecs_; }
456 const std::vector<VideoCodec>& send_codecs() const { return send_codecs_; }
457 const std::vector<VideoCodec>& codecs() const { return send_codecs(); }
458 bool rendering() const { return playout(); }
459 const VideoOptions& options() const { return options_; }
460 const std::map<uint32, VideoRenderer*>& renderers() const {
461 return renderers_;
462 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000463 int start_bps() const { return start_bps_; }
464 int max_bps() const { return max_bps_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 bool GetSendStreamFormat(uint32 ssrc, VideoFormat* format) {
466 if (send_formats_.find(ssrc) == send_formats_.end()) {
467 return false;
468 }
469 *format = send_formats_[ssrc];
470 return true;
471 }
472 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) {
473 if (send_formats_.find(ssrc) == send_formats_.end()) {
474 return false;
475 }
476 send_formats_[ssrc] = format;
477 return true;
478 }
479
480 virtual bool AddSendStream(const StreamParams& sp) {
481 if (!RtpHelper<VideoMediaChannel>::AddSendStream(sp)) {
482 return false;
483 }
484 SetSendStreamDefaultFormat(sp.first_ssrc());
485 return true;
486 }
487 virtual bool RemoveSendStream(uint32 ssrc) {
488 send_formats_.erase(ssrc);
489 return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc);
490 }
491
492 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
493 if (fail_set_recv_codecs()) {
494 // Fake the failure in SetRecvCodecs.
495 return false;
496 }
497 recv_codecs_ = codecs;
498 return true;
499 }
500 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) {
501 if (fail_set_send_codecs()) {
502 // Fake the failure in SetSendCodecs.
503 return false;
504 }
505 send_codecs_ = codecs;
506
507 for (std::vector<StreamParams>::const_iterator it = send_streams().begin();
508 it != send_streams().end(); ++it) {
509 SetSendStreamDefaultFormat(it->first_ssrc());
510 }
511 return true;
512 }
513 virtual bool GetSendCodec(VideoCodec* send_codec) {
514 if (send_codecs_.empty()) {
515 return false;
516 }
517 *send_codec = send_codecs_[0];
518 return true;
519 }
520 virtual bool SetRender(bool render) {
521 set_playout(render);
522 return true;
523 }
524 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* r) {
525 if (ssrc != 0 && renderers_.find(ssrc) == renderers_.end()) {
526 return false;
527 }
528 if (ssrc != 0) {
529 renderers_[ssrc] = r;
530 }
531 return true;
532 }
533
534 virtual bool SetSend(bool send) { return set_sending(send); }
535 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
536 capturers_[ssrc] = capturer;
537 return true;
538 }
539 bool HasCapturer(uint32 ssrc) const {
540 return capturers_.find(ssrc) != capturers_.end();
541 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000542 virtual bool SetStartSendBandwidth(int bps) {
543 start_bps_ = bps;
544 return true;
545 }
546 virtual bool SetMaxSendBandwidth(int bps) {
547 max_bps_ = bps;
548 return true;
549 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 virtual bool AddRecvStream(const StreamParams& sp) {
551 if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp))
552 return false;
553 renderers_[sp.first_ssrc()] = NULL;
554 return true;
555 }
556 virtual bool RemoveRecvStream(uint32 ssrc) {
557 if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc))
558 return false;
559 renderers_.erase(ssrc);
560 return true;
561 }
562
563 virtual bool GetStats(VideoMediaInfo* info) { return false; }
564 virtual bool SendIntraFrame() {
565 sent_intra_frame_ = true;
566 return true;
567 }
568 virtual bool RequestIntraFrame() {
569 requested_intra_frame_ = true;
570 return true;
571 }
572 virtual bool SetOptions(const VideoOptions& options) {
573 options_ = options;
574 return true;
575 }
576 virtual bool GetOptions(VideoOptions* options) const {
577 *options = options_;
578 return true;
579 }
580 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {}
581 void set_sent_intra_frame(bool v) { sent_intra_frame_ = v; }
582 bool sent_intra_frame() const { return sent_intra_frame_; }
583 void set_requested_intra_frame(bool v) { requested_intra_frame_ = v; }
584 bool requested_intra_frame() const { return requested_intra_frame_; }
585
586 private:
587 // Be default, each send stream uses the first send codec format.
588 void SetSendStreamDefaultFormat(uint32 ssrc) {
589 if (!send_codecs_.empty()) {
590 send_formats_[ssrc] = VideoFormat(
591 send_codecs_[0].width, send_codecs_[0].height,
592 cricket::VideoFormat::FpsToInterval(send_codecs_[0].framerate),
593 cricket::FOURCC_I420);
594 }
595 }
596
597 FakeVideoEngine* engine_;
598 std::vector<VideoCodec> recv_codecs_;
599 std::vector<VideoCodec> send_codecs_;
600 std::map<uint32, VideoRenderer*> renderers_;
601 std::map<uint32, VideoFormat> send_formats_;
602 std::map<uint32, VideoCapturer*> capturers_;
603 bool sent_intra_frame_;
604 bool requested_intra_frame_;
605 VideoOptions options_;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000606 int start_bps_;
607 int max_bps_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608};
609
610class FakeSoundclipMedia : public SoundclipMedia {
611 public:
612 virtual bool PlaySound(const char* buf, int len, int flags) { return true; }
613};
614
615class FakeDataMediaChannel : public RtpHelper<DataMediaChannel> {
616 public:
617 explicit FakeDataMediaChannel(void* unused)
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000618 : send_blocked_(false), max_bps_(-1) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 ~FakeDataMediaChannel() {}
620 const std::vector<DataCodec>& recv_codecs() const { return recv_codecs_; }
621 const std::vector<DataCodec>& send_codecs() const { return send_codecs_; }
622 const std::vector<DataCodec>& codecs() const { return send_codecs(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 int max_bps() const { return max_bps_; }
624
625 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) {
626 if (fail_set_recv_codecs()) {
627 // Fake the failure in SetRecvCodecs.
628 return false;
629 }
630 recv_codecs_ = codecs;
631 return true;
632 }
633 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) {
634 if (fail_set_send_codecs()) {
635 // Fake the failure in SetSendCodecs.
636 return false;
637 }
638 send_codecs_ = codecs;
639 return true;
640 }
641 virtual bool SetSend(bool send) { return set_sending(send); }
642 virtual bool SetReceive(bool receive) {
643 set_playout(receive);
644 return true;
645 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000646 virtual bool SetStartSendBandwidth(int bps) { return true; }
647 virtual bool SetMaxSendBandwidth(int bps) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 max_bps_ = bps;
649 return true;
650 }
651 virtual bool AddRecvStream(const StreamParams& sp) {
652 if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp))
653 return false;
654 return true;
655 }
656 virtual bool RemoveRecvStream(uint32 ssrc) {
657 if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc))
658 return false;
659 return true;
660 }
661
662 virtual bool SendData(const SendDataParams& params,
663 const talk_base::Buffer& payload,
664 SendDataResult* result) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000665 if (send_blocked_) {
666 *result = SDR_BLOCK;
667 return false;
668 } else {
669 last_sent_data_params_ = params;
670 last_sent_data_ = std::string(payload.data(), payload.length());
671 return true;
672 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 }
674
675 SendDataParams last_sent_data_params() { return last_sent_data_params_; }
676 std::string last_sent_data() { return last_sent_data_; }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000677 bool is_send_blocked() { return send_blocked_; }
678 void set_send_blocked(bool blocked) { send_blocked_ = blocked; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679
680 private:
681 std::vector<DataCodec> recv_codecs_;
682 std::vector<DataCodec> send_codecs_;
683 SendDataParams last_sent_data_params_;
684 std::string last_sent_data_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000685 bool send_blocked_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 int max_bps_;
687};
688
689// A base class for all of the shared parts between FakeVoiceEngine
690// and FakeVideoEngine.
691class FakeBaseEngine {
692 public:
693 FakeBaseEngine()
694 : loglevel_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 options_changed_(false),
696 fail_create_channel_(false) {}
697 bool Init(talk_base::Thread* worker_thread) { return true; }
698 void Terminate() {}
699
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 void SetLogging(int level, const char* filter) {
701 loglevel_ = level;
702 logfilter_ = filter;
703 }
704
705 void set_fail_create_channel(bool fail) { fail_create_channel_ = fail; }
706
707 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const {
708 return rtp_header_extensions_;
709 }
710
711 protected:
712 int loglevel_;
713 std::string logfilter_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 // Flag used by optionsmessagehandler_unittest for checking whether any
715 // relevant setting has been updated.
716 // TODO(thaloun): Replace with explicit checks of before & after values.
717 bool options_changed_;
718 bool fail_create_channel_;
719 std::vector<RtpHeaderExtension> rtp_header_extensions_;
720};
721
722class FakeVoiceEngine : public FakeBaseEngine {
723 public:
724 FakeVoiceEngine()
725 : output_volume_(-1),
726 delay_offset_(0),
727 rx_processor_(NULL),
728 tx_processor_(NULL) {
729 // Add a fake audio codec. Note that the name must not be "" as there are
730 // sanity checks against that.
731 codecs_.push_back(AudioCodec(101, "fake_audio_codec", 0, 0, 1, 0));
732 }
733 int GetCapabilities() { return AUDIO_SEND | AUDIO_RECV; }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000734 AudioOptions GetAudioOptions() const {
735 return options_;
736 }
737 AudioOptions GetOptions() const {
738 return options_;
739 }
740 bool SetOptions(const AudioOptions& options) {
741 options_ = options;
742 options_changed_ = true;
743 return true;
744 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745
746 VoiceMediaChannel* CreateChannel() {
747 if (fail_create_channel_) {
748 return NULL;
749 }
750
751 FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this);
752 channels_.push_back(ch);
753 return ch;
754 }
755 FakeVoiceMediaChannel* GetChannel(size_t index) {
756 return (channels_.size() > index) ? channels_[index] : NULL;
757 }
758 void UnregisterChannel(VoiceMediaChannel* channel) {
759 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
760 }
761 SoundclipMedia* CreateSoundclip() { return new FakeSoundclipMedia(); }
762
763 const std::vector<AudioCodec>& codecs() { return codecs_; }
764 void SetCodecs(const std::vector<AudioCodec> codecs) { codecs_ = codecs; }
765
766 bool SetDelayOffset(int offset) {
767 delay_offset_ = offset;
768 return true;
769 }
770
771 bool SetDevices(const Device* in_device, const Device* out_device) {
772 in_device_ = (in_device) ? in_device->name : "";
773 out_device_ = (out_device) ? out_device->name : "";
774 options_changed_ = true;
775 return true;
776 }
777
778 bool GetOutputVolume(int* level) {
779 *level = output_volume_;
780 return true;
781 }
782
783 bool SetOutputVolume(int level) {
784 output_volume_ = level;
785 options_changed_ = true;
786 return true;
787 }
788
789 int GetInputLevel() { return 0; }
790
791 bool SetLocalMonitor(bool enable) { return true; }
792
wu@webrtc.orga9890802013-12-13 00:21:03 +0000793 bool StartAecDump(FILE* file) { return false; }
794
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 bool RegisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor,
796 MediaProcessorDirection direction) {
797 if (direction == MPD_RX) {
798 rx_processor_ = voice_processor;
799 return true;
800 } else if (direction == MPD_TX) {
801 tx_processor_ = voice_processor;
802 return true;
803 }
804 return false;
805 }
806
807 bool UnregisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor,
808 MediaProcessorDirection direction) {
809 bool unregistered = false;
810 if (direction & MPD_RX) {
811 rx_processor_ = NULL;
812 unregistered = true;
813 }
814 if (direction & MPD_TX) {
815 tx_processor_ = NULL;
816 unregistered = true;
817 }
818 return unregistered;
819 }
820
821 private:
822 std::vector<FakeVoiceMediaChannel*> channels_;
823 std::vector<AudioCodec> codecs_;
824 int output_volume_;
825 int delay_offset_;
826 std::string in_device_;
827 std::string out_device_;
828 VoiceProcessor* rx_processor_;
829 VoiceProcessor* tx_processor_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000830 AudioOptions options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831
832 friend class FakeMediaEngine;
833};
834
835class FakeVideoEngine : public FakeBaseEngine {
836 public:
837 FakeVideoEngine() : renderer_(NULL), capture_(false), processor_(NULL) {
838 // Add a fake video codec. Note that the name must not be "" as there are
839 // sanity checks against that.
840 codecs_.push_back(VideoCodec(0, "fake_video_codec", 0, 0, 0, 0));
841 }
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000842 bool GetOptions(VideoOptions* options) const {
843 *options = options_;
844 return true;
845 }
846 bool SetOptions(const VideoOptions& options) {
847 options_ = options;
848 options_changed_ = true;
849 return true;
850 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 int GetCapabilities() { return VIDEO_SEND | VIDEO_RECV; }
852 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
853 default_encoder_config_ = config;
854 return true;
855 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000856 VideoEncoderConfig GetDefaultEncoderConfig() const {
857 return default_encoder_config_;
858 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 const VideoEncoderConfig& default_encoder_config() const {
860 return default_encoder_config_;
861 }
862
863 VideoMediaChannel* CreateChannel(VoiceMediaChannel* channel) {
864 if (fail_create_channel_) {
865 return NULL;
866 }
867
868 FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this);
869 channels_.push_back(ch);
870 return ch;
871 }
872 FakeVideoMediaChannel* GetChannel(size_t index) {
873 return (channels_.size() > index) ? channels_[index] : NULL;
874 }
875 void UnregisterChannel(VideoMediaChannel* channel) {
876 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
877 }
878
879 const std::vector<VideoCodec>& codecs() const { return codecs_; }
880 bool FindCodec(const VideoCodec& in) {
881 for (size_t i = 0; i < codecs_.size(); ++i) {
882 if (codecs_[i].Matches(in)) {
883 return true;
884 }
885 }
886 return false;
887 }
888 void SetCodecs(const std::vector<VideoCodec> codecs) { codecs_ = codecs; }
889
890 bool SetCaptureDevice(const Device* device) {
891 in_device_ = (device) ? device->name : "";
892 options_changed_ = true;
893 return true;
894 }
895 bool SetLocalRenderer(VideoRenderer* r) {
896 renderer_ = r;
897 return true;
898 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 bool SetCapture(bool capture) {
900 capture_ = capture;
901 return true;
902 }
903 VideoFormat GetStartCaptureFormat() const {
904 return VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30),
905 FOURCC_I420);
906 }
907
908 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
909
910 private:
911 std::vector<FakeVideoMediaChannel*> channels_;
912 std::vector<VideoCodec> codecs_;
913 VideoEncoderConfig default_encoder_config_;
914 std::string in_device_;
915 VideoRenderer* renderer_;
916 bool capture_;
917 VideoProcessor* processor_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000918 VideoOptions options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919
920 friend class FakeMediaEngine;
921};
922
923class FakeMediaEngine :
924 public CompositeMediaEngine<FakeVoiceEngine, FakeVideoEngine> {
925 public:
926 FakeMediaEngine() {
927 voice_ = FakeVoiceEngine();
928 video_ = FakeVideoEngine();
929 }
930 virtual ~FakeMediaEngine() {}
931
932 virtual void SetAudioCodecs(const std::vector<AudioCodec> codecs) {
933 voice_.SetCodecs(codecs);
934 }
935
936 virtual void SetVideoCodecs(const std::vector<VideoCodec> codecs) {
937 video_.SetCodecs(codecs);
938 }
939
940 FakeVoiceMediaChannel* GetVoiceChannel(size_t index) {
941 return voice_.GetChannel(index);
942 }
943
944 FakeVideoMediaChannel* GetVideoChannel(size_t index) {
945 return video_.GetChannel(index);
946 }
947
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000948 AudioOptions audio_options() const { return voice_.options_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 int audio_delay_offset() const { return voice_.delay_offset_; }
950 int output_volume() const { return voice_.output_volume_; }
951 const VideoEncoderConfig& default_video_encoder_config() const {
952 return video_.default_encoder_config_;
953 }
954 const std::string& audio_in_device() const { return voice_.in_device_; }
955 const std::string& audio_out_device() const { return voice_.out_device_; }
956 VideoRenderer* local_renderer() { return video_.renderer_; }
957 int voice_loglevel() const { return voice_.loglevel_; }
958 const std::string& voice_logfilter() const { return voice_.logfilter_; }
959 int video_loglevel() const { return video_.loglevel_; }
960 const std::string& video_logfilter() const { return video_.logfilter_; }
961 bool capture() const { return video_.capture_; }
962 bool options_changed() const {
963 return voice_.options_changed_ || video_.options_changed_;
964 }
965 void clear_options_changed() {
966 video_.options_changed_ = false;
967 voice_.options_changed_ = false;
968 }
969 void set_fail_create_channel(bool fail) {
970 voice_.set_fail_create_channel(fail);
971 video_.set_fail_create_channel(fail);
972 }
973 bool voice_processor_registered(MediaProcessorDirection direction) const {
974 if (direction == MPD_RX) {
975 return voice_.rx_processor_ != NULL;
976 } else if (direction == MPD_TX) {
977 return voice_.tx_processor_ != NULL;
978 }
979 return false;
980 }
981};
982
983// CompositeMediaEngine with FakeVoiceEngine to expose SetAudioCodecs to
984// establish a media connectionwith minimum set of audio codes required
985template <class VIDEO>
986class CompositeMediaEngineWithFakeVoiceEngine :
987 public CompositeMediaEngine<FakeVoiceEngine, VIDEO> {
988 public:
989 CompositeMediaEngineWithFakeVoiceEngine() {}
990 virtual ~CompositeMediaEngineWithFakeVoiceEngine() {}
991
992 virtual void SetAudioCodecs(const std::vector<AudioCodec>& codecs) {
993 CompositeMediaEngine<FakeVoiceEngine, VIDEO>::voice_.SetCodecs(codecs);
994 }
995};
996
997// Have to come afterwards due to declaration order
998inline FakeVoiceMediaChannel::~FakeVoiceMediaChannel() {
999 if (engine_) {
1000 engine_->UnregisterChannel(this);
1001 }
1002}
1003
1004inline FakeVideoMediaChannel::~FakeVideoMediaChannel() {
1005 if (engine_) {
1006 engine_->UnregisterChannel(this);
1007 }
1008}
1009
1010class FakeDataEngine : public DataEngineInterface {
1011 public:
1012 FakeDataEngine() : last_channel_type_(DCT_NONE) {}
1013
1014 virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type) {
1015 last_channel_type_ = data_channel_type;
1016 FakeDataMediaChannel* ch = new FakeDataMediaChannel(this);
1017 channels_.push_back(ch);
1018 return ch;
1019 }
1020
1021 FakeDataMediaChannel* GetChannel(size_t index) {
1022 return (channels_.size() > index) ? channels_[index] : NULL;
1023 }
1024
1025 void UnregisterChannel(DataMediaChannel* channel) {
1026 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
1027 }
1028
1029 virtual void SetDataCodecs(const std::vector<DataCodec>& data_codecs) {
1030 data_codecs_ = data_codecs;
1031 }
1032
1033 virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; }
1034
1035 DataChannelType last_channel_type() const { return last_channel_type_; }
1036
1037 private:
1038 std::vector<FakeDataMediaChannel*> channels_;
1039 std::vector<DataCodec> data_codecs_;
1040 DataChannelType last_channel_type_;
1041};
1042
1043} // namespace cricket
1044
1045#endif // TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_