3 * Copyright 2004 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_
29 #define TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_
37 #include "talk/base/buffer.h"
38 #include "talk/base/stringutils.h"
39 #include "talk/media/base/audiorenderer.h"
40 #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"
47 class FakeMediaEngine;
48 class FakeVideoEngine;
49 class FakeVoiceEngine;
51 // A common helper class that handles sending and receiving RTP/RTCP packets.
52 template <class Base> class RtpHelper : public Base {
57 fail_set_send_codecs_(false),
58 fail_set_recv_codecs_(false),
60 ready_to_send_(false) {}
61 const std::vector<RtpHeaderExtension>& recv_extensions() {
62 return recv_extensions_;
64 const std::vector<RtpHeaderExtension>& send_extensions() {
65 return send_extensions_;
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_; }
72 bool SendRtp(const void* data, int len) {
76 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
77 return Base::SendPacket(&packet);
79 bool SendRtcp(const void* data, int len) {
80 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
81 return Base::SendRtcp(&packet);
84 bool CheckRtp(const void* data, int len) {
85 bool success = !rtp_packets_.empty();
87 std::string packet = rtp_packets_.front();
88 rtp_packets_.pop_front();
89 success = (packet == std::string(static_cast<const char*>(data), len));
93 bool CheckRtcp(const void* data, int len) {
94 bool success = !rtcp_packets_.empty();
96 std::string packet = rtcp_packets_.front();
97 rtcp_packets_.pop_front();
98 success = (packet == std::string(static_cast<const char*>(data), len));
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;
109 virtual bool SetSendRtpHeaderExtensions(
110 const std::vector<RtpHeaderExtension>& extensions) {
111 send_extensions_ = extensions;
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()) {
121 send_streams_.push_back(sp);
124 virtual bool RemoveSendStream(uint32 ssrc) {
125 return RemoveStreamBySsrc(&send_streams_, ssrc);
127 virtual bool AddRecvStream(const StreamParams& sp) {
128 if (std::find(receive_streams_.begin(), receive_streams_.end(), sp) !=
129 receive_streams_.end()) {
132 receive_streams_.push_back(sp);
135 virtual bool RemoveRecvStream(uint32 ssrc) {
136 return RemoveStreamBySsrc(&receive_streams_, ssrc);
138 virtual bool MuteStream(uint32 ssrc, bool on) {
139 if (!HasSendStream(ssrc) && ssrc != 0)
142 muted_streams_.insert(ssrc);
144 muted_streams_.erase(ssrc);
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();
156 const std::vector<StreamParams>& send_streams() const {
157 return send_streams_;
159 const std::vector<StreamParams>& recv_streams() const {
160 return receive_streams_;
162 bool HasRecvStream(uint32 ssrc) const {
163 return GetStreamBySsrc(receive_streams_, ssrc, NULL);
165 bool HasSendStream(uint32 ssrc) const {
166 return GetStreamBySsrc(send_streams_, ssrc, NULL);
168 // TODO(perkj): This is to support legacy unit test that only check one
170 const uint32 send_ssrc() {
171 if (send_streams_.empty())
173 return send_streams_[0].first_ssrc();
176 // TODO(perkj): This is to support legacy unit test that only check one
178 const std::string rtcp_cname() {
179 if (send_streams_.empty())
181 return send_streams_[0].cname;
184 bool ready_to_send() const {
185 return ready_to_send_;
189 bool set_sending(bool send) {
193 void set_playout(bool playout) { playout_ = playout; }
194 virtual void OnPacketReceived(talk_base::Buffer* packet) {
195 rtp_packets_.push_back(std::string(packet->data(), packet->length()));
197 virtual void OnRtcpReceived(talk_base::Buffer* packet) {
198 rtcp_packets_.push_back(std::string(packet->data(), packet->length()));
200 virtual void OnReadyToSend(bool ready) {
201 ready_to_send_ = ready;
203 bool fail_set_send_codecs() const { return fail_set_send_codecs_; }
204 bool fail_set_recv_codecs() const { return fail_set_recv_codecs_; }
209 std::vector<RtpHeaderExtension> recv_extensions_;
210 std::vector<RtpHeaderExtension> send_extensions_;
211 std::list<std::string> rtp_packets_;
212 std::list<std::string> rtcp_packets_;
213 std::vector<StreamParams> send_streams_;
214 std::vector<StreamParams> receive_streams_;
215 std::set<uint32> muted_streams_;
216 bool fail_set_send_codecs_;
217 bool fail_set_recv_codecs_;
219 std::string rtcp_cname_;
223 class FakeVoiceMediaChannel : public RtpHelper<VoiceMediaChannel> {
226 DtmfInfo(uint32 ssrc, int event_code, int duration, int flags)
227 : ssrc(ssrc), event_code(event_code), duration(duration), flags(flags) {
234 explicit FakeVoiceMediaChannel(FakeVoiceEngine* engine)
236 fail_set_send_(false),
237 ringback_tone_ssrc_(0),
238 ringback_tone_play_(false),
239 ringback_tone_loop_(false),
240 time_since_last_typing_(-1) {
241 output_scalings_[0] = OutputScaling(); // For default channel.
243 ~FakeVoiceMediaChannel();
244 const std::vector<AudioCodec>& recv_codecs() const { return recv_codecs_; }
245 const std::vector<AudioCodec>& send_codecs() const { return send_codecs_; }
246 const std::vector<AudioCodec>& codecs() const { return send_codecs(); }
247 const std::vector<DtmfInfo>& dtmf_info_queue() const {
248 return dtmf_info_queue_;
250 const AudioOptions& options() const { return options_; }
252 uint32 ringback_tone_ssrc() const { return ringback_tone_ssrc_; }
253 bool ringback_tone_play() const { return ringback_tone_play_; }
254 bool ringback_tone_loop() const { return ringback_tone_loop_; }
256 virtual bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) {
257 if (fail_set_recv_codecs()) {
258 // Fake the failure in SetRecvCodecs.
261 recv_codecs_ = codecs;
264 virtual bool SetSendCodecs(const std::vector<AudioCodec>& codecs) {
265 if (fail_set_send_codecs()) {
266 // Fake the failure in SetSendCodecs.
269 send_codecs_ = codecs;
272 virtual bool SetPlayout(bool playout) {
273 set_playout(playout);
276 virtual bool SetSend(SendFlags flag) {
277 if (fail_set_send_) {
280 return set_sending(flag != SEND_NOTHING);
282 virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
283 virtual bool AddRecvStream(const StreamParams& sp) {
284 if (!RtpHelper<VoiceMediaChannel>::AddRecvStream(sp))
286 output_scalings_[sp.first_ssrc()] = OutputScaling();
289 virtual bool RemoveRecvStream(uint32 ssrc) {
290 if (!RtpHelper<VoiceMediaChannel>::RemoveRecvStream(ssrc))
292 output_scalings_.erase(ssrc);
295 virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
296 std::map<uint32, AudioRenderer*>::iterator it =
297 remote_renderers_.find(ssrc);
299 if (it != remote_renderers_.end()) {
300 ASSERT(it->second == renderer);
302 remote_renderers_.insert(std::make_pair(ssrc, renderer));
303 renderer->AddChannel(0);
306 if (it != remote_renderers_.end()) {
307 it->second->RemoveChannel(0);
308 remote_renderers_.erase(it);
315 virtual bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
316 std::map<uint32, AudioRenderer*>::iterator it = local_renderers_.find(ssrc);
318 if (it != local_renderers_.end()) {
319 ASSERT(it->second == renderer);
321 local_renderers_.insert(std::make_pair(ssrc, renderer));
322 renderer->AddChannel(0);
325 if (it != local_renderers_.end()) {
326 it->second->RemoveChannel(0);
327 local_renderers_.erase(it);
335 virtual bool GetActiveStreams(AudioInfo::StreamList* streams) { return true; }
336 virtual int GetOutputLevel() { return 0; }
337 void set_time_since_last_typing(int ms) { time_since_last_typing_ = ms; }
338 virtual int GetTimeSinceLastTyping() { return time_since_last_typing_; }
339 virtual void SetTypingDetectionParameters(
340 int time_window, int cost_per_typing, int reporting_threshold,
341 int penalty_decay, int type_event_delay) {}
343 virtual bool SetRingbackTone(const char* buf, int len) { return true; }
344 virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
345 ringback_tone_ssrc_ = ssrc;
346 ringback_tone_play_ = play;
347 ringback_tone_loop_ = loop;
351 virtual bool CanInsertDtmf() {
352 for (std::vector<AudioCodec>::const_iterator it = send_codecs_.begin();
353 it != send_codecs_.end(); ++it) {
354 // Find the DTMF telephone event "codec".
355 if (_stricmp(it->name.c_str(), "telephone-event") == 0) {
361 virtual bool InsertDtmf(uint32 ssrc, int event_code, int duration,
363 dtmf_info_queue_.push_back(DtmfInfo(ssrc, event_code, duration, flags));
367 virtual bool SetOutputScaling(uint32 ssrc, double left, double right) {
369 std::map<uint32, OutputScaling>::iterator it;
370 for (it = output_scalings_.begin(); it != output_scalings_.end(); ++it) {
371 it->second.left = left;
372 it->second.right = right;
375 } else if (output_scalings_.find(ssrc) != output_scalings_.end()) {
376 output_scalings_[ssrc].left = left;
377 output_scalings_[ssrc].right = right;
382 virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) {
383 if (output_scalings_.find(ssrc) == output_scalings_.end())
385 *left = output_scalings_[ssrc].left;
386 *right = output_scalings_[ssrc].right;
390 virtual bool GetStats(VoiceMediaInfo* info) { return false; }
391 virtual void GetLastMediaError(uint32* ssrc,
392 VoiceMediaChannel::Error* error) {
394 *error = fail_set_send_ ? VoiceMediaChannel::ERROR_REC_DEVICE_OPEN_FAILED
395 : VoiceMediaChannel::ERROR_NONE;
398 void set_fail_set_send(bool fail) { fail_set_send_ = fail; }
399 void TriggerError(uint32 ssrc, VoiceMediaChannel::Error error) {
400 VoiceMediaChannel::SignalMediaError(ssrc, error);
403 virtual bool SetOptions(const AudioOptions& options) {
404 // Does a "merge" of current options and set options.
405 options_.SetAll(options);
408 virtual bool GetOptions(AudioOptions* options) const {
414 struct OutputScaling {
415 OutputScaling() : left(1.0), right(1.0) {}
419 FakeVoiceEngine* engine_;
420 std::vector<AudioCodec> recv_codecs_;
421 std::vector<AudioCodec> send_codecs_;
422 std::map<uint32, OutputScaling> output_scalings_;
423 std::vector<DtmfInfo> dtmf_info_queue_;
425 uint32 ringback_tone_ssrc_;
426 bool ringback_tone_play_;
427 bool ringback_tone_loop_;
428 int time_since_last_typing_;
429 AudioOptions options_;
430 std::map<uint32, AudioRenderer*> local_renderers_;
431 std::map<uint32, AudioRenderer*> remote_renderers_;
434 // A helper function to compare the FakeVoiceMediaChannel::DtmfInfo.
435 inline bool CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo& info,
436 uint32 ssrc, int event_code, int duration,
438 return (info.duration == duration && info.event_code == event_code &&
439 info.flags == flags && info.ssrc == ssrc);
442 class FakeVideoMediaChannel : public RtpHelper<VideoMediaChannel> {
444 explicit FakeVideoMediaChannel(FakeVideoEngine* engine)
446 sent_intra_frame_(false),
447 requested_intra_frame_(false) {}
448 ~FakeVideoMediaChannel();
450 const std::vector<VideoCodec>& recv_codecs() const { return recv_codecs_; }
451 const std::vector<VideoCodec>& send_codecs() const { return send_codecs_; }
452 const std::vector<VideoCodec>& codecs() const { return send_codecs(); }
453 bool rendering() const { return playout(); }
454 const VideoOptions& options() const { return options_; }
455 const std::map<uint32, VideoRenderer*>& renderers() const {
458 bool GetSendStreamFormat(uint32 ssrc, VideoFormat* format) {
459 if (send_formats_.find(ssrc) == send_formats_.end()) {
462 *format = send_formats_[ssrc];
465 virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) {
466 if (send_formats_.find(ssrc) == send_formats_.end()) {
469 send_formats_[ssrc] = format;
473 virtual bool AddSendStream(const StreamParams& sp) {
474 if (!RtpHelper<VideoMediaChannel>::AddSendStream(sp)) {
477 SetSendStreamDefaultFormat(sp.first_ssrc());
480 virtual bool RemoveSendStream(uint32 ssrc) {
481 send_formats_.erase(ssrc);
482 return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc);
485 virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
486 if (fail_set_recv_codecs()) {
487 // Fake the failure in SetRecvCodecs.
490 recv_codecs_ = codecs;
493 virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) {
494 if (fail_set_send_codecs()) {
495 // Fake the failure in SetSendCodecs.
498 send_codecs_ = codecs;
500 for (std::vector<StreamParams>::const_iterator it = send_streams().begin();
501 it != send_streams().end(); ++it) {
502 SetSendStreamDefaultFormat(it->first_ssrc());
506 virtual bool GetSendCodec(VideoCodec* send_codec) {
507 if (send_codecs_.empty()) {
510 *send_codec = send_codecs_[0];
513 virtual bool SetRender(bool render) {
517 virtual bool SetRenderer(uint32 ssrc, VideoRenderer* r) {
518 if (ssrc != 0 && renderers_.find(ssrc) == renderers_.end()) {
522 renderers_[ssrc] = r;
527 virtual bool SetSend(bool send) { return set_sending(send); }
528 virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
529 capturers_[ssrc] = capturer;
532 bool HasCapturer(uint32 ssrc) const {
533 return capturers_.find(ssrc) != capturers_.end();
535 virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
536 virtual bool AddRecvStream(const StreamParams& sp) {
537 if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp))
539 renderers_[sp.first_ssrc()] = NULL;
542 virtual bool RemoveRecvStream(uint32 ssrc) {
543 if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc))
545 renderers_.erase(ssrc);
549 virtual bool GetStats(VideoMediaInfo* info) { return false; }
550 virtual bool SendIntraFrame() {
551 sent_intra_frame_ = true;
554 virtual bool RequestIntraFrame() {
555 requested_intra_frame_ = true;
558 virtual bool SetOptions(const VideoOptions& options) {
562 virtual bool GetOptions(VideoOptions* options) const {
566 virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {}
567 void set_sent_intra_frame(bool v) { sent_intra_frame_ = v; }
568 bool sent_intra_frame() const { return sent_intra_frame_; }
569 void set_requested_intra_frame(bool v) { requested_intra_frame_ = v; }
570 bool requested_intra_frame() const { return requested_intra_frame_; }
573 // Be default, each send stream uses the first send codec format.
574 void SetSendStreamDefaultFormat(uint32 ssrc) {
575 if (!send_codecs_.empty()) {
576 send_formats_[ssrc] = VideoFormat(
577 send_codecs_[0].width, send_codecs_[0].height,
578 cricket::VideoFormat::FpsToInterval(send_codecs_[0].framerate),
579 cricket::FOURCC_I420);
583 FakeVideoEngine* engine_;
584 std::vector<VideoCodec> recv_codecs_;
585 std::vector<VideoCodec> send_codecs_;
586 std::map<uint32, VideoRenderer*> renderers_;
587 std::map<uint32, VideoFormat> send_formats_;
588 std::map<uint32, VideoCapturer*> capturers_;
589 bool sent_intra_frame_;
590 bool requested_intra_frame_;
591 VideoOptions options_;
594 class FakeSoundclipMedia : public SoundclipMedia {
596 virtual bool PlaySound(const char* buf, int len, int flags) { return true; }
599 class FakeDataMediaChannel : public RtpHelper<DataMediaChannel> {
601 explicit FakeDataMediaChannel(void* unused)
602 : auto_bandwidth_(false), send_blocked_(false), max_bps_(-1) {}
603 ~FakeDataMediaChannel() {}
604 const std::vector<DataCodec>& recv_codecs() const { return recv_codecs_; }
605 const std::vector<DataCodec>& send_codecs() const { return send_codecs_; }
606 const std::vector<DataCodec>& codecs() const { return send_codecs(); }
607 bool auto_bandwidth() const { return auto_bandwidth_; }
608 int max_bps() const { return max_bps_; }
610 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) {
611 if (fail_set_recv_codecs()) {
612 // Fake the failure in SetRecvCodecs.
615 recv_codecs_ = codecs;
618 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) {
619 if (fail_set_send_codecs()) {
620 // Fake the failure in SetSendCodecs.
623 send_codecs_ = codecs;
626 virtual bool SetSend(bool send) { return set_sending(send); }
627 virtual bool SetReceive(bool receive) {
628 set_playout(receive);
631 virtual bool SetSendBandwidth(bool autobw, int bps) {
632 auto_bandwidth_ = autobw;
636 virtual bool AddRecvStream(const StreamParams& sp) {
637 if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp))
641 virtual bool RemoveRecvStream(uint32 ssrc) {
642 if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc))
647 virtual bool SendData(const SendDataParams& params,
648 const talk_base::Buffer& payload,
649 SendDataResult* result) {
654 last_sent_data_params_ = params;
655 last_sent_data_ = std::string(payload.data(), payload.length());
660 SendDataParams last_sent_data_params() { return last_sent_data_params_; }
661 std::string last_sent_data() { return last_sent_data_; }
662 bool is_send_blocked() { return send_blocked_; }
663 void set_send_blocked(bool blocked) { send_blocked_ = blocked; }
666 std::vector<DataCodec> recv_codecs_;
667 std::vector<DataCodec> send_codecs_;
668 SendDataParams last_sent_data_params_;
669 std::string last_sent_data_;
670 bool auto_bandwidth_;
675 // A base class for all of the shared parts between FakeVoiceEngine
676 // and FakeVideoEngine.
677 class FakeBaseEngine {
681 options_changed_(false),
682 fail_create_channel_(false) {}
683 bool Init(talk_base::Thread* worker_thread) { return true; }
686 void SetLogging(int level, const char* filter) {
691 void set_fail_create_channel(bool fail) { fail_create_channel_ = fail; }
693 const std::vector<RtpHeaderExtension>& rtp_header_extensions() const {
694 return rtp_header_extensions_;
699 std::string logfilter_;
700 // Flag used by optionsmessagehandler_unittest for checking whether any
701 // relevant setting has been updated.
702 // TODO(thaloun): Replace with explicit checks of before & after values.
703 bool options_changed_;
704 bool fail_create_channel_;
705 std::vector<RtpHeaderExtension> rtp_header_extensions_;
708 class FakeVoiceEngine : public FakeBaseEngine {
711 : output_volume_(-1),
714 tx_processor_(NULL) {
715 // Add a fake audio codec. Note that the name must not be "" as there are
716 // sanity checks against that.
717 codecs_.push_back(AudioCodec(101, "fake_audio_codec", 0, 0, 1, 0));
719 int GetCapabilities() { return AUDIO_SEND | AUDIO_RECV; }
720 AudioOptions GetAudioOptions() const {
723 AudioOptions GetOptions() const {
726 bool SetOptions(const AudioOptions& options) {
728 options_changed_ = true;
732 VoiceMediaChannel* CreateChannel() {
733 if (fail_create_channel_) {
737 FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this);
738 channels_.push_back(ch);
741 FakeVoiceMediaChannel* GetChannel(size_t index) {
742 return (channels_.size() > index) ? channels_[index] : NULL;
744 void UnregisterChannel(VoiceMediaChannel* channel) {
745 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
747 SoundclipMedia* CreateSoundclip() { return new FakeSoundclipMedia(); }
749 const std::vector<AudioCodec>& codecs() { return codecs_; }
750 void SetCodecs(const std::vector<AudioCodec> codecs) { codecs_ = codecs; }
752 bool SetDelayOffset(int offset) {
753 delay_offset_ = offset;
757 bool SetDevices(const Device* in_device, const Device* out_device) {
758 in_device_ = (in_device) ? in_device->name : "";
759 out_device_ = (out_device) ? out_device->name : "";
760 options_changed_ = true;
764 bool GetOutputVolume(int* level) {
765 *level = output_volume_;
769 bool SetOutputVolume(int level) {
770 output_volume_ = level;
771 options_changed_ = true;
775 int GetInputLevel() { return 0; }
777 bool SetLocalMonitor(bool enable) { return true; }
779 bool RegisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor,
780 MediaProcessorDirection direction) {
781 if (direction == MPD_RX) {
782 rx_processor_ = voice_processor;
784 } else if (direction == MPD_TX) {
785 tx_processor_ = voice_processor;
791 bool UnregisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor,
792 MediaProcessorDirection direction) {
793 bool unregistered = false;
794 if (direction & MPD_RX) {
795 rx_processor_ = NULL;
798 if (direction & MPD_TX) {
799 tx_processor_ = NULL;
806 std::vector<FakeVoiceMediaChannel*> channels_;
807 std::vector<AudioCodec> codecs_;
810 std::string in_device_;
811 std::string out_device_;
812 VoiceProcessor* rx_processor_;
813 VoiceProcessor* tx_processor_;
814 AudioOptions options_;
816 friend class FakeMediaEngine;
819 class FakeVideoEngine : public FakeBaseEngine {
821 FakeVideoEngine() : renderer_(NULL), capture_(false), processor_(NULL) {
822 // Add a fake video codec. Note that the name must not be "" as there are
823 // sanity checks against that.
824 codecs_.push_back(VideoCodec(0, "fake_video_codec", 0, 0, 0, 0));
826 bool GetOptions(VideoOptions* options) const {
830 bool SetOptions(const VideoOptions& options) {
832 options_changed_ = true;
835 int GetCapabilities() { return VIDEO_SEND | VIDEO_RECV; }
836 bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
837 default_encoder_config_ = config;
840 VideoEncoderConfig GetDefaultEncoderConfig() const {
841 return default_encoder_config_;
843 const VideoEncoderConfig& default_encoder_config() const {
844 return default_encoder_config_;
847 VideoMediaChannel* CreateChannel(VoiceMediaChannel* channel) {
848 if (fail_create_channel_) {
852 FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this);
853 channels_.push_back(ch);
856 FakeVideoMediaChannel* GetChannel(size_t index) {
857 return (channels_.size() > index) ? channels_[index] : NULL;
859 void UnregisterChannel(VideoMediaChannel* channel) {
860 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
863 const std::vector<VideoCodec>& codecs() const { return codecs_; }
864 bool FindCodec(const VideoCodec& in) {
865 for (size_t i = 0; i < codecs_.size(); ++i) {
866 if (codecs_[i].Matches(in)) {
872 void SetCodecs(const std::vector<VideoCodec> codecs) { codecs_ = codecs; }
874 bool SetCaptureDevice(const Device* device) {
875 in_device_ = (device) ? device->name : "";
876 options_changed_ = true;
879 bool SetLocalRenderer(VideoRenderer* r) {
883 bool SetCapture(bool capture) {
887 VideoFormat GetStartCaptureFormat() const {
888 return VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30),
892 sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange;
895 std::vector<FakeVideoMediaChannel*> channels_;
896 std::vector<VideoCodec> codecs_;
897 VideoEncoderConfig default_encoder_config_;
898 std::string in_device_;
899 VideoRenderer* renderer_;
901 VideoProcessor* processor_;
902 VideoOptions options_;
904 friend class FakeMediaEngine;
907 class FakeMediaEngine :
908 public CompositeMediaEngine<FakeVoiceEngine, FakeVideoEngine> {
911 voice_ = FakeVoiceEngine();
912 video_ = FakeVideoEngine();
914 virtual ~FakeMediaEngine() {}
916 virtual void SetAudioCodecs(const std::vector<AudioCodec> codecs) {
917 voice_.SetCodecs(codecs);
920 virtual void SetVideoCodecs(const std::vector<VideoCodec> codecs) {
921 video_.SetCodecs(codecs);
924 FakeVoiceMediaChannel* GetVoiceChannel(size_t index) {
925 return voice_.GetChannel(index);
928 FakeVideoMediaChannel* GetVideoChannel(size_t index) {
929 return video_.GetChannel(index);
932 AudioOptions audio_options() const { return voice_.options_; }
933 int audio_delay_offset() const { return voice_.delay_offset_; }
934 int output_volume() const { return voice_.output_volume_; }
935 const VideoEncoderConfig& default_video_encoder_config() const {
936 return video_.default_encoder_config_;
938 const std::string& audio_in_device() const { return voice_.in_device_; }
939 const std::string& audio_out_device() const { return voice_.out_device_; }
940 VideoRenderer* local_renderer() { return video_.renderer_; }
941 int voice_loglevel() const { return voice_.loglevel_; }
942 const std::string& voice_logfilter() const { return voice_.logfilter_; }
943 int video_loglevel() const { return video_.loglevel_; }
944 const std::string& video_logfilter() const { return video_.logfilter_; }
945 bool capture() const { return video_.capture_; }
946 bool options_changed() const {
947 return voice_.options_changed_ || video_.options_changed_;
949 void clear_options_changed() {
950 video_.options_changed_ = false;
951 voice_.options_changed_ = false;
953 void set_fail_create_channel(bool fail) {
954 voice_.set_fail_create_channel(fail);
955 video_.set_fail_create_channel(fail);
957 bool voice_processor_registered(MediaProcessorDirection direction) const {
958 if (direction == MPD_RX) {
959 return voice_.rx_processor_ != NULL;
960 } else if (direction == MPD_TX) {
961 return voice_.tx_processor_ != NULL;
967 // CompositeMediaEngine with FakeVoiceEngine to expose SetAudioCodecs to
968 // establish a media connectionwith minimum set of audio codes required
969 template <class VIDEO>
970 class CompositeMediaEngineWithFakeVoiceEngine :
971 public CompositeMediaEngine<FakeVoiceEngine, VIDEO> {
973 CompositeMediaEngineWithFakeVoiceEngine() {}
974 virtual ~CompositeMediaEngineWithFakeVoiceEngine() {}
976 virtual void SetAudioCodecs(const std::vector<AudioCodec>& codecs) {
977 CompositeMediaEngine<FakeVoiceEngine, VIDEO>::voice_.SetCodecs(codecs);
981 // Have to come afterwards due to declaration order
982 inline FakeVoiceMediaChannel::~FakeVoiceMediaChannel() {
984 engine_->UnregisterChannel(this);
988 inline FakeVideoMediaChannel::~FakeVideoMediaChannel() {
990 engine_->UnregisterChannel(this);
994 class FakeDataEngine : public DataEngineInterface {
996 FakeDataEngine() : last_channel_type_(DCT_NONE) {}
998 virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type) {
999 last_channel_type_ = data_channel_type;
1000 FakeDataMediaChannel* ch = new FakeDataMediaChannel(this);
1001 channels_.push_back(ch);
1005 FakeDataMediaChannel* GetChannel(size_t index) {
1006 return (channels_.size() > index) ? channels_[index] : NULL;
1009 void UnregisterChannel(DataMediaChannel* channel) {
1010 channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
1013 virtual void SetDataCodecs(const std::vector<DataCodec>& data_codecs) {
1014 data_codecs_ = data_codecs;
1017 virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; }
1019 DataChannelType last_channel_type() const { return last_channel_type_; }
1022 std::vector<FakeDataMediaChannel*> channels_;
1023 std::vector<DataCodec> data_codecs_;
1024 DataChannelType last_channel_type_;
1027 } // namespace cricket
1029 #endif // TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_