henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 37 | #include "talk/media/base/audiorenderer.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | #include "talk/media/base/mediaengine.h" |
| 39 | #include "talk/media/base/rtputils.h" |
| 40 | #include "talk/media/base/streamparams.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 41 | #include "webrtc/p2p/base/sessiondescription.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 42 | #include "webrtc/base/buffer.h" |
| 43 | #include "webrtc/base/stringutils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | |
| 45 | namespace cricket { |
| 46 | |
| 47 | class FakeMediaEngine; |
| 48 | class FakeVideoEngine; |
| 49 | class FakeVoiceEngine; |
| 50 | |
| 51 | // A common helper class that handles sending and receiving RTP/RTCP packets. |
| 52 | template <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.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 73 | if (!sending_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 76 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 77 | return Base::SendPacket(&packet); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | } |
| 79 | bool SendRtcp(const void* data, int len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 80 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 81 | return Base::SendRtcp(&packet); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | } |
| 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.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame^] | 163 | return GetStreamBySsrc(receive_streams_, ssrc) != nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | } |
| 165 | bool HasSendStream(uint32 ssrc) const { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame^] | 166 | return GetStreamBySsrc(send_streams_, ssrc) != nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 167 | } |
| 168 | // TODO(perkj): This is to support legacy unit test that only check one |
| 169 | // sending stream. |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 170 | uint32 send_ssrc() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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; } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 194 | virtual void OnPacketReceived(rtc::Buffer* packet, |
| 195 | const rtc::PacketTime& packet_time) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 196 | rtp_packets_.push_back(std::string(packet->data(), packet->length())); |
| 197 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 198 | virtual void OnRtcpReceived(rtc::Buffer* packet, |
| 199 | const rtc::PacketTime& packet_time) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | 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 | |
| 225 | class 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.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 284 | virtual bool SetMaxSendBandwidth(int bps) { return true; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | 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.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 297 | 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.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 318 | std::map<uint32, VoiceChannelAudioSink*>::iterator it = |
| 319 | local_renderers_.find(ssrc); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 320 | if (renderer) { |
| 321 | if (it != local_renderers_.end()) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 322 | ASSERT(it->second->renderer() == renderer); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 323 | } else { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 324 | local_renderers_.insert(std::make_pair( |
| 325 | ssrc, new VoiceChannelAudioSink(renderer))); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 326 | } |
| 327 | } else { |
| 328 | if (it != local_renderers_.end()) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 329 | delete it->second; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 330 | local_renderers_.erase(it); |
| 331 | } else { |
| 332 | return false; |
| 333 | } |
| 334 | } |
| 335 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 336 | } |
| 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.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 422 | 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 | } |
| 435 | virtual 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 | virtual void OnClose() OVERRIDE { |
| 441 | renderer_ = NULL; |
| 442 | } |
| 443 | AudioRenderer* renderer() const { return renderer_; } |
| 444 | |
| 445 | private: |
| 446 | AudioRenderer* renderer_; |
| 447 | }; |
| 448 | |
| 449 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 450 | FakeVoiceEngine* engine_; |
| 451 | std::vector<AudioCodec> recv_codecs_; |
| 452 | std::vector<AudioCodec> send_codecs_; |
| 453 | std::map<uint32, OutputScaling> output_scalings_; |
| 454 | std::vector<DtmfInfo> dtmf_info_queue_; |
| 455 | bool fail_set_send_; |
| 456 | uint32 ringback_tone_ssrc_; |
| 457 | bool ringback_tone_play_; |
| 458 | bool ringback_tone_loop_; |
| 459 | int time_since_last_typing_; |
| 460 | AudioOptions options_; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 461 | std::map<uint32, VoiceChannelAudioSink*> local_renderers_; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 462 | std::map<uint32, AudioRenderer*> remote_renderers_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | // A helper function to compare the FakeVoiceMediaChannel::DtmfInfo. |
| 466 | inline bool CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo& info, |
| 467 | uint32 ssrc, int event_code, int duration, |
| 468 | int flags) { |
| 469 | return (info.duration == duration && info.event_code == event_code && |
| 470 | info.flags == flags && info.ssrc == ssrc); |
| 471 | } |
| 472 | |
| 473 | class FakeVideoMediaChannel : public RtpHelper<VideoMediaChannel> { |
| 474 | public: |
| 475 | explicit FakeVideoMediaChannel(FakeVideoEngine* engine) |
| 476 | : engine_(engine), |
| 477 | sent_intra_frame_(false), |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 478 | requested_intra_frame_(false), |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 479 | max_bps_(-1) {} |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 480 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 481 | ~FakeVideoMediaChannel(); |
| 482 | |
| 483 | const std::vector<VideoCodec>& recv_codecs() const { return recv_codecs_; } |
| 484 | const std::vector<VideoCodec>& send_codecs() const { return send_codecs_; } |
| 485 | const std::vector<VideoCodec>& codecs() const { return send_codecs(); } |
| 486 | bool rendering() const { return playout(); } |
| 487 | const VideoOptions& options() const { return options_; } |
| 488 | const std::map<uint32, VideoRenderer*>& renderers() const { |
| 489 | return renderers_; |
| 490 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 491 | int max_bps() const { return max_bps_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 492 | bool GetSendStreamFormat(uint32 ssrc, VideoFormat* format) { |
| 493 | if (send_formats_.find(ssrc) == send_formats_.end()) { |
| 494 | return false; |
| 495 | } |
| 496 | *format = send_formats_[ssrc]; |
| 497 | return true; |
| 498 | } |
| 499 | virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) { |
| 500 | if (send_formats_.find(ssrc) == send_formats_.end()) { |
| 501 | return false; |
| 502 | } |
| 503 | send_formats_[ssrc] = format; |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | virtual bool AddSendStream(const StreamParams& sp) { |
| 508 | if (!RtpHelper<VideoMediaChannel>::AddSendStream(sp)) { |
| 509 | return false; |
| 510 | } |
| 511 | SetSendStreamDefaultFormat(sp.first_ssrc()); |
| 512 | return true; |
| 513 | } |
| 514 | virtual bool RemoveSendStream(uint32 ssrc) { |
| 515 | send_formats_.erase(ssrc); |
| 516 | return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc); |
| 517 | } |
| 518 | |
| 519 | virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
| 520 | if (fail_set_recv_codecs()) { |
| 521 | // Fake the failure in SetRecvCodecs. |
| 522 | return false; |
| 523 | } |
| 524 | recv_codecs_ = codecs; |
| 525 | return true; |
| 526 | } |
| 527 | virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 528 | if (fail_set_send_codecs()) { |
| 529 | // Fake the failure in SetSendCodecs. |
| 530 | return false; |
| 531 | } |
| 532 | send_codecs_ = codecs; |
| 533 | |
| 534 | for (std::vector<StreamParams>::const_iterator it = send_streams().begin(); |
| 535 | it != send_streams().end(); ++it) { |
| 536 | SetSendStreamDefaultFormat(it->first_ssrc()); |
| 537 | } |
| 538 | return true; |
| 539 | } |
| 540 | virtual bool GetSendCodec(VideoCodec* send_codec) { |
| 541 | if (send_codecs_.empty()) { |
| 542 | return false; |
| 543 | } |
| 544 | *send_codec = send_codecs_[0]; |
| 545 | return true; |
| 546 | } |
| 547 | virtual bool SetRender(bool render) { |
| 548 | set_playout(render); |
| 549 | return true; |
| 550 | } |
| 551 | virtual bool SetRenderer(uint32 ssrc, VideoRenderer* r) { |
| 552 | if (ssrc != 0 && renderers_.find(ssrc) == renderers_.end()) { |
| 553 | return false; |
| 554 | } |
| 555 | if (ssrc != 0) { |
| 556 | renderers_[ssrc] = r; |
| 557 | } |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | virtual bool SetSend(bool send) { return set_sending(send); } |
| 562 | virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 563 | capturers_[ssrc] = capturer; |
| 564 | return true; |
| 565 | } |
| 566 | bool HasCapturer(uint32 ssrc) const { |
| 567 | return capturers_.find(ssrc) != capturers_.end(); |
| 568 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 569 | virtual bool SetMaxSendBandwidth(int bps) { |
| 570 | max_bps_ = bps; |
| 571 | return true; |
| 572 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 573 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 574 | if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp)) |
| 575 | return false; |
| 576 | renderers_[sp.first_ssrc()] = NULL; |
| 577 | return true; |
| 578 | } |
| 579 | virtual bool RemoveRecvStream(uint32 ssrc) { |
| 580 | if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc)) |
| 581 | return false; |
| 582 | renderers_.erase(ssrc); |
| 583 | return true; |
| 584 | } |
| 585 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 586 | virtual bool GetStats(const StatsOptions& options, |
| 587 | VideoMediaInfo* info) { return false; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 588 | virtual bool SendIntraFrame() { |
| 589 | sent_intra_frame_ = true; |
| 590 | return true; |
| 591 | } |
| 592 | virtual bool RequestIntraFrame() { |
| 593 | requested_intra_frame_ = true; |
| 594 | return true; |
| 595 | } |
| 596 | virtual bool SetOptions(const VideoOptions& options) { |
| 597 | options_ = options; |
| 598 | return true; |
| 599 | } |
| 600 | virtual bool GetOptions(VideoOptions* options) const { |
| 601 | *options = options_; |
| 602 | return true; |
| 603 | } |
| 604 | virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {} |
| 605 | void set_sent_intra_frame(bool v) { sent_intra_frame_ = v; } |
| 606 | bool sent_intra_frame() const { return sent_intra_frame_; } |
| 607 | void set_requested_intra_frame(bool v) { requested_intra_frame_ = v; } |
| 608 | bool requested_intra_frame() const { return requested_intra_frame_; } |
| 609 | |
| 610 | private: |
| 611 | // Be default, each send stream uses the first send codec format. |
| 612 | void SetSendStreamDefaultFormat(uint32 ssrc) { |
| 613 | if (!send_codecs_.empty()) { |
| 614 | send_formats_[ssrc] = VideoFormat( |
| 615 | send_codecs_[0].width, send_codecs_[0].height, |
| 616 | cricket::VideoFormat::FpsToInterval(send_codecs_[0].framerate), |
| 617 | cricket::FOURCC_I420); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | FakeVideoEngine* engine_; |
| 622 | std::vector<VideoCodec> recv_codecs_; |
| 623 | std::vector<VideoCodec> send_codecs_; |
| 624 | std::map<uint32, VideoRenderer*> renderers_; |
| 625 | std::map<uint32, VideoFormat> send_formats_; |
| 626 | std::map<uint32, VideoCapturer*> capturers_; |
| 627 | bool sent_intra_frame_; |
| 628 | bool requested_intra_frame_; |
| 629 | VideoOptions options_; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 630 | int max_bps_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | class FakeSoundclipMedia : public SoundclipMedia { |
| 634 | public: |
| 635 | virtual bool PlaySound(const char* buf, int len, int flags) { return true; } |
| 636 | }; |
| 637 | |
| 638 | class FakeDataMediaChannel : public RtpHelper<DataMediaChannel> { |
| 639 | public: |
| 640 | explicit FakeDataMediaChannel(void* unused) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 641 | : send_blocked_(false), max_bps_(-1) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 642 | ~FakeDataMediaChannel() {} |
| 643 | const std::vector<DataCodec>& recv_codecs() const { return recv_codecs_; } |
| 644 | const std::vector<DataCodec>& send_codecs() const { return send_codecs_; } |
| 645 | const std::vector<DataCodec>& codecs() const { return send_codecs(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 646 | int max_bps() const { return max_bps_; } |
| 647 | |
| 648 | virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) { |
| 649 | if (fail_set_recv_codecs()) { |
| 650 | // Fake the failure in SetRecvCodecs. |
| 651 | return false; |
| 652 | } |
| 653 | recv_codecs_ = codecs; |
| 654 | return true; |
| 655 | } |
| 656 | virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) { |
| 657 | if (fail_set_send_codecs()) { |
| 658 | // Fake the failure in SetSendCodecs. |
| 659 | return false; |
| 660 | } |
| 661 | send_codecs_ = codecs; |
| 662 | return true; |
| 663 | } |
| 664 | virtual bool SetSend(bool send) { return set_sending(send); } |
| 665 | virtual bool SetReceive(bool receive) { |
| 666 | set_playout(receive); |
| 667 | return true; |
| 668 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 669 | virtual bool SetMaxSendBandwidth(int bps) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 670 | max_bps_ = bps; |
| 671 | return true; |
| 672 | } |
| 673 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 674 | if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp)) |
| 675 | return false; |
| 676 | return true; |
| 677 | } |
| 678 | virtual bool RemoveRecvStream(uint32 ssrc) { |
| 679 | if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc)) |
| 680 | return false; |
| 681 | return true; |
| 682 | } |
| 683 | |
| 684 | virtual bool SendData(const SendDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 685 | const rtc::Buffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 686 | SendDataResult* result) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 687 | if (send_blocked_) { |
| 688 | *result = SDR_BLOCK; |
| 689 | return false; |
| 690 | } else { |
| 691 | last_sent_data_params_ = params; |
| 692 | last_sent_data_ = std::string(payload.data(), payload.length()); |
| 693 | return true; |
| 694 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | SendDataParams last_sent_data_params() { return last_sent_data_params_; } |
| 698 | std::string last_sent_data() { return last_sent_data_; } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 699 | bool is_send_blocked() { return send_blocked_; } |
| 700 | void set_send_blocked(bool blocked) { send_blocked_ = blocked; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 701 | |
| 702 | private: |
| 703 | std::vector<DataCodec> recv_codecs_; |
| 704 | std::vector<DataCodec> send_codecs_; |
| 705 | SendDataParams last_sent_data_params_; |
| 706 | std::string last_sent_data_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 707 | bool send_blocked_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 708 | int max_bps_; |
| 709 | }; |
| 710 | |
| 711 | // A base class for all of the shared parts between FakeVoiceEngine |
| 712 | // and FakeVideoEngine. |
| 713 | class FakeBaseEngine { |
| 714 | public: |
| 715 | FakeBaseEngine() |
| 716 | : loglevel_(-1), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 717 | options_changed_(false), |
| 718 | fail_create_channel_(false) {} |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 719 | bool Init(rtc::Thread* worker_thread) { return true; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 720 | void Terminate() {} |
| 721 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 722 | void SetLogging(int level, const char* filter) { |
| 723 | loglevel_ = level; |
| 724 | logfilter_ = filter; |
| 725 | } |
| 726 | |
| 727 | void set_fail_create_channel(bool fail) { fail_create_channel_ = fail; } |
| 728 | |
| 729 | const std::vector<RtpHeaderExtension>& rtp_header_extensions() const { |
| 730 | return rtp_header_extensions_; |
| 731 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 732 | void set_rtp_header_extensions( |
| 733 | const std::vector<RtpHeaderExtension>& extensions) { |
| 734 | rtp_header_extensions_ = extensions; |
| 735 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 736 | |
| 737 | protected: |
| 738 | int loglevel_; |
| 739 | std::string logfilter_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 740 | // Flag used by optionsmessagehandler_unittest for checking whether any |
| 741 | // relevant setting has been updated. |
| 742 | // TODO(thaloun): Replace with explicit checks of before & after values. |
| 743 | bool options_changed_; |
| 744 | bool fail_create_channel_; |
| 745 | std::vector<RtpHeaderExtension> rtp_header_extensions_; |
| 746 | }; |
| 747 | |
| 748 | class FakeVoiceEngine : public FakeBaseEngine { |
| 749 | public: |
| 750 | FakeVoiceEngine() |
| 751 | : output_volume_(-1), |
| 752 | delay_offset_(0), |
| 753 | rx_processor_(NULL), |
| 754 | tx_processor_(NULL) { |
| 755 | // Add a fake audio codec. Note that the name must not be "" as there are |
| 756 | // sanity checks against that. |
| 757 | codecs_.push_back(AudioCodec(101, "fake_audio_codec", 0, 0, 1, 0)); |
| 758 | } |
| 759 | int GetCapabilities() { return AUDIO_SEND | AUDIO_RECV; } |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 760 | AudioOptions GetAudioOptions() const { |
| 761 | return options_; |
| 762 | } |
| 763 | AudioOptions GetOptions() const { |
| 764 | return options_; |
| 765 | } |
| 766 | bool SetOptions(const AudioOptions& options) { |
| 767 | options_ = options; |
| 768 | options_changed_ = true; |
| 769 | return true; |
| 770 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 771 | |
| 772 | VoiceMediaChannel* CreateChannel() { |
| 773 | if (fail_create_channel_) { |
| 774 | return NULL; |
| 775 | } |
| 776 | |
| 777 | FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this); |
| 778 | channels_.push_back(ch); |
| 779 | return ch; |
| 780 | } |
| 781 | FakeVoiceMediaChannel* GetChannel(size_t index) { |
| 782 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 783 | } |
| 784 | void UnregisterChannel(VoiceMediaChannel* channel) { |
| 785 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 786 | } |
| 787 | SoundclipMedia* CreateSoundclip() { return new FakeSoundclipMedia(); } |
| 788 | |
| 789 | const std::vector<AudioCodec>& codecs() { return codecs_; } |
| 790 | void SetCodecs(const std::vector<AudioCodec> codecs) { codecs_ = codecs; } |
| 791 | |
| 792 | bool SetDelayOffset(int offset) { |
| 793 | delay_offset_ = offset; |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | bool SetDevices(const Device* in_device, const Device* out_device) { |
| 798 | in_device_ = (in_device) ? in_device->name : ""; |
| 799 | out_device_ = (out_device) ? out_device->name : ""; |
| 800 | options_changed_ = true; |
| 801 | return true; |
| 802 | } |
| 803 | |
| 804 | bool GetOutputVolume(int* level) { |
| 805 | *level = output_volume_; |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | bool SetOutputVolume(int level) { |
| 810 | output_volume_ = level; |
| 811 | options_changed_ = true; |
| 812 | return true; |
| 813 | } |
| 814 | |
| 815 | int GetInputLevel() { return 0; } |
| 816 | |
| 817 | bool SetLocalMonitor(bool enable) { return true; } |
| 818 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 819 | bool StartAecDump(rtc::PlatformFile file) { return false; } |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 820 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 821 | bool RegisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor, |
| 822 | MediaProcessorDirection direction) { |
| 823 | if (direction == MPD_RX) { |
| 824 | rx_processor_ = voice_processor; |
| 825 | return true; |
| 826 | } else if (direction == MPD_TX) { |
| 827 | tx_processor_ = voice_processor; |
| 828 | return true; |
| 829 | } |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | bool UnregisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor, |
| 834 | MediaProcessorDirection direction) { |
| 835 | bool unregistered = false; |
| 836 | if (direction & MPD_RX) { |
| 837 | rx_processor_ = NULL; |
| 838 | unregistered = true; |
| 839 | } |
| 840 | if (direction & MPD_TX) { |
| 841 | tx_processor_ = NULL; |
| 842 | unregistered = true; |
| 843 | } |
| 844 | return unregistered; |
| 845 | } |
| 846 | |
| 847 | private: |
| 848 | std::vector<FakeVoiceMediaChannel*> channels_; |
| 849 | std::vector<AudioCodec> codecs_; |
| 850 | int output_volume_; |
| 851 | int delay_offset_; |
| 852 | std::string in_device_; |
| 853 | std::string out_device_; |
| 854 | VoiceProcessor* rx_processor_; |
| 855 | VoiceProcessor* tx_processor_; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 856 | AudioOptions options_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 857 | |
| 858 | friend class FakeMediaEngine; |
| 859 | }; |
| 860 | |
| 861 | class FakeVideoEngine : public FakeBaseEngine { |
| 862 | public: |
buildbot@webrtc.org | 3740d74 | 2014-08-22 22:27:04 +0000 | [diff] [blame] | 863 | FakeVideoEngine() : capture_(false), processor_(NULL) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 864 | // Add a fake video codec. Note that the name must not be "" as there are |
| 865 | // sanity checks against that. |
| 866 | codecs_.push_back(VideoCodec(0, "fake_video_codec", 0, 0, 0, 0)); |
| 867 | } |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 868 | bool GetOptions(VideoOptions* options) const { |
| 869 | *options = options_; |
| 870 | return true; |
| 871 | } |
| 872 | bool SetOptions(const VideoOptions& options) { |
| 873 | options_ = options; |
| 874 | options_changed_ = true; |
| 875 | return true; |
| 876 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 877 | int GetCapabilities() { return VIDEO_SEND | VIDEO_RECV; } |
| 878 | bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) { |
| 879 | default_encoder_config_ = config; |
| 880 | return true; |
| 881 | } |
| 882 | const VideoEncoderConfig& default_encoder_config() const { |
| 883 | return default_encoder_config_; |
| 884 | } |
| 885 | |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 886 | VideoMediaChannel* CreateChannel(const VideoOptions& options, |
| 887 | VoiceMediaChannel* channel) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 888 | if (fail_create_channel_) { |
| 889 | return NULL; |
| 890 | } |
| 891 | |
| 892 | FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this); |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 893 | ch->SetOptions(options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 894 | channels_.push_back(ch); |
| 895 | return ch; |
| 896 | } |
| 897 | FakeVideoMediaChannel* GetChannel(size_t index) { |
| 898 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 899 | } |
| 900 | void UnregisterChannel(VideoMediaChannel* channel) { |
| 901 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 902 | } |
| 903 | |
| 904 | const std::vector<VideoCodec>& codecs() const { return codecs_; } |
| 905 | bool FindCodec(const VideoCodec& in) { |
| 906 | for (size_t i = 0; i < codecs_.size(); ++i) { |
| 907 | if (codecs_[i].Matches(in)) { |
| 908 | return true; |
| 909 | } |
| 910 | } |
| 911 | return false; |
| 912 | } |
| 913 | void SetCodecs(const std::vector<VideoCodec> codecs) { codecs_ = codecs; } |
| 914 | |
| 915 | bool SetCaptureDevice(const Device* device) { |
| 916 | in_device_ = (device) ? device->name : ""; |
| 917 | options_changed_ = true; |
| 918 | return true; |
| 919 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 920 | bool SetCapture(bool capture) { |
| 921 | capture_ = capture; |
| 922 | return true; |
| 923 | } |
buildbot@webrtc.org | 992febb | 2014-09-05 16:39:08 +0000 | [diff] [blame] | 924 | VideoFormat GetStartCaptureFormat() const { |
| 925 | return VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30), |
| 926 | FOURCC_I420); |
| 927 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 928 | |
| 929 | sigslot::repeater2<VideoCapturer*, CaptureState> SignalCaptureStateChange; |
| 930 | |
| 931 | private: |
| 932 | std::vector<FakeVideoMediaChannel*> channels_; |
| 933 | std::vector<VideoCodec> codecs_; |
| 934 | VideoEncoderConfig default_encoder_config_; |
| 935 | std::string in_device_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 936 | bool capture_; |
| 937 | VideoProcessor* processor_; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 938 | VideoOptions options_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 939 | |
| 940 | friend class FakeMediaEngine; |
| 941 | }; |
| 942 | |
| 943 | class FakeMediaEngine : |
| 944 | public CompositeMediaEngine<FakeVoiceEngine, FakeVideoEngine> { |
| 945 | public: |
| 946 | FakeMediaEngine() { |
| 947 | voice_ = FakeVoiceEngine(); |
| 948 | video_ = FakeVideoEngine(); |
| 949 | } |
| 950 | virtual ~FakeMediaEngine() {} |
| 951 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 952 | void SetAudioCodecs(const std::vector<AudioCodec>& codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 953 | voice_.SetCodecs(codecs); |
| 954 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 955 | void SetVideoCodecs(const std::vector<VideoCodec>& codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 956 | video_.SetCodecs(codecs); |
| 957 | } |
| 958 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 959 | void SetAudioRtpHeaderExtensions( |
| 960 | const std::vector<RtpHeaderExtension>& extensions) { |
| 961 | voice_.set_rtp_header_extensions(extensions); |
| 962 | } |
| 963 | void SetVideoRtpHeaderExtensions( |
| 964 | const std::vector<RtpHeaderExtension>& extensions) { |
| 965 | video_.set_rtp_header_extensions(extensions); |
| 966 | } |
| 967 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 968 | FakeVoiceMediaChannel* GetVoiceChannel(size_t index) { |
| 969 | return voice_.GetChannel(index); |
| 970 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 971 | FakeVideoMediaChannel* GetVideoChannel(size_t index) { |
| 972 | return video_.GetChannel(index); |
| 973 | } |
| 974 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 975 | AudioOptions audio_options() const { return voice_.options_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 976 | int audio_delay_offset() const { return voice_.delay_offset_; } |
| 977 | int output_volume() const { return voice_.output_volume_; } |
| 978 | const VideoEncoderConfig& default_video_encoder_config() const { |
| 979 | return video_.default_encoder_config_; |
| 980 | } |
| 981 | const std::string& audio_in_device() const { return voice_.in_device_; } |
| 982 | const std::string& audio_out_device() const { return voice_.out_device_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 983 | int voice_loglevel() const { return voice_.loglevel_; } |
| 984 | const std::string& voice_logfilter() const { return voice_.logfilter_; } |
| 985 | int video_loglevel() const { return video_.loglevel_; } |
| 986 | const std::string& video_logfilter() const { return video_.logfilter_; } |
| 987 | bool capture() const { return video_.capture_; } |
| 988 | bool options_changed() const { |
| 989 | return voice_.options_changed_ || video_.options_changed_; |
| 990 | } |
| 991 | void clear_options_changed() { |
| 992 | video_.options_changed_ = false; |
| 993 | voice_.options_changed_ = false; |
| 994 | } |
| 995 | void set_fail_create_channel(bool fail) { |
| 996 | voice_.set_fail_create_channel(fail); |
| 997 | video_.set_fail_create_channel(fail); |
| 998 | } |
| 999 | bool voice_processor_registered(MediaProcessorDirection direction) const { |
| 1000 | if (direction == MPD_RX) { |
| 1001 | return voice_.rx_processor_ != NULL; |
| 1002 | } else if (direction == MPD_TX) { |
| 1003 | return voice_.tx_processor_ != NULL; |
| 1004 | } |
| 1005 | return false; |
| 1006 | } |
| 1007 | }; |
| 1008 | |
| 1009 | // CompositeMediaEngine with FakeVoiceEngine to expose SetAudioCodecs to |
| 1010 | // establish a media connectionwith minimum set of audio codes required |
| 1011 | template <class VIDEO> |
| 1012 | class CompositeMediaEngineWithFakeVoiceEngine : |
| 1013 | public CompositeMediaEngine<FakeVoiceEngine, VIDEO> { |
| 1014 | public: |
| 1015 | CompositeMediaEngineWithFakeVoiceEngine() {} |
| 1016 | virtual ~CompositeMediaEngineWithFakeVoiceEngine() {} |
| 1017 | |
| 1018 | virtual void SetAudioCodecs(const std::vector<AudioCodec>& codecs) { |
| 1019 | CompositeMediaEngine<FakeVoiceEngine, VIDEO>::voice_.SetCodecs(codecs); |
| 1020 | } |
| 1021 | }; |
| 1022 | |
| 1023 | // Have to come afterwards due to declaration order |
| 1024 | inline FakeVoiceMediaChannel::~FakeVoiceMediaChannel() { |
| 1025 | if (engine_) { |
| 1026 | engine_->UnregisterChannel(this); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | inline FakeVideoMediaChannel::~FakeVideoMediaChannel() { |
| 1031 | if (engine_) { |
| 1032 | engine_->UnregisterChannel(this); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | class FakeDataEngine : public DataEngineInterface { |
| 1037 | public: |
| 1038 | FakeDataEngine() : last_channel_type_(DCT_NONE) {} |
| 1039 | |
| 1040 | virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type) { |
| 1041 | last_channel_type_ = data_channel_type; |
| 1042 | FakeDataMediaChannel* ch = new FakeDataMediaChannel(this); |
| 1043 | channels_.push_back(ch); |
| 1044 | return ch; |
| 1045 | } |
| 1046 | |
| 1047 | FakeDataMediaChannel* GetChannel(size_t index) { |
| 1048 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 1049 | } |
| 1050 | |
| 1051 | void UnregisterChannel(DataMediaChannel* channel) { |
| 1052 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 1053 | } |
| 1054 | |
| 1055 | virtual void SetDataCodecs(const std::vector<DataCodec>& data_codecs) { |
| 1056 | data_codecs_ = data_codecs; |
| 1057 | } |
| 1058 | |
| 1059 | virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; } |
| 1060 | |
| 1061 | DataChannelType last_channel_type() const { return last_channel_type_; } |
| 1062 | |
| 1063 | private: |
| 1064 | std::vector<FakeDataMediaChannel*> channels_; |
| 1065 | std::vector<DataCodec> data_codecs_; |
| 1066 | DataChannelType last_channel_type_; |
| 1067 | }; |
| 1068 | |
| 1069 | } // namespace cricket |
| 1070 | |
| 1071 | #endif // TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_ |