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 | } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 76 | rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, |
| 77 | kMaxRtpPacketLen); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 78 | return Base::SendPacket(&packet); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | } |
| 80 | bool SendRtcp(const void* data, int len) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 81 | rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, |
| 82 | kMaxRtpPacketLen); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 83 | return Base::SendRtcp(&packet); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool CheckRtp(const void* data, int len) { |
| 87 | bool success = !rtp_packets_.empty(); |
| 88 | if (success) { |
| 89 | std::string packet = rtp_packets_.front(); |
| 90 | rtp_packets_.pop_front(); |
| 91 | success = (packet == std::string(static_cast<const char*>(data), len)); |
| 92 | } |
| 93 | return success; |
| 94 | } |
| 95 | bool CheckRtcp(const void* data, int len) { |
| 96 | bool success = !rtcp_packets_.empty(); |
| 97 | if (success) { |
| 98 | std::string packet = rtcp_packets_.front(); |
| 99 | rtcp_packets_.pop_front(); |
| 100 | success = (packet == std::string(static_cast<const char*>(data), len)); |
| 101 | } |
| 102 | return success; |
| 103 | } |
| 104 | bool CheckNoRtp() { return rtp_packets_.empty(); } |
| 105 | bool CheckNoRtcp() { return rtcp_packets_.empty(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | void set_fail_set_send_codecs(bool fail) { fail_set_send_codecs_ = fail; } |
| 107 | void set_fail_set_recv_codecs(bool fail) { fail_set_recv_codecs_ = fail; } |
| 108 | virtual bool AddSendStream(const StreamParams& sp) { |
| 109 | if (std::find(send_streams_.begin(), send_streams_.end(), sp) != |
| 110 | send_streams_.end()) { |
| 111 | return false; |
| 112 | } |
| 113 | send_streams_.push_back(sp); |
| 114 | return true; |
| 115 | } |
| 116 | virtual bool RemoveSendStream(uint32 ssrc) { |
| 117 | return RemoveStreamBySsrc(&send_streams_, ssrc); |
| 118 | } |
| 119 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 120 | if (std::find(receive_streams_.begin(), receive_streams_.end(), sp) != |
| 121 | receive_streams_.end()) { |
| 122 | return false; |
| 123 | } |
| 124 | receive_streams_.push_back(sp); |
| 125 | return true; |
| 126 | } |
| 127 | virtual bool RemoveRecvStream(uint32 ssrc) { |
| 128 | return RemoveStreamBySsrc(&receive_streams_, ssrc); |
| 129 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 130 | bool IsStreamMuted(uint32 ssrc) const { |
| 131 | bool ret = muted_streams_.find(ssrc) != muted_streams_.end(); |
| 132 | // If |ssrc = 0| check if the first send stream is muted. |
| 133 | if (!ret && ssrc == 0 && !send_streams_.empty()) { |
| 134 | return muted_streams_.find(send_streams_[0].first_ssrc()) != |
| 135 | muted_streams_.end(); |
| 136 | } |
| 137 | return ret; |
| 138 | } |
| 139 | const std::vector<StreamParams>& send_streams() const { |
| 140 | return send_streams_; |
| 141 | } |
| 142 | const std::vector<StreamParams>& recv_streams() const { |
| 143 | return receive_streams_; |
| 144 | } |
| 145 | bool HasRecvStream(uint32 ssrc) const { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 146 | return GetStreamBySsrc(receive_streams_, ssrc) != nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | } |
| 148 | bool HasSendStream(uint32 ssrc) const { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 149 | return GetStreamBySsrc(send_streams_, ssrc) != nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | } |
| 151 | // TODO(perkj): This is to support legacy unit test that only check one |
| 152 | // sending stream. |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 153 | uint32 send_ssrc() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | if (send_streams_.empty()) |
| 155 | return 0; |
| 156 | return send_streams_[0].first_ssrc(); |
| 157 | } |
| 158 | |
| 159 | // TODO(perkj): This is to support legacy unit test that only check one |
| 160 | // sending stream. |
| 161 | const std::string rtcp_cname() { |
| 162 | if (send_streams_.empty()) |
| 163 | return ""; |
| 164 | return send_streams_[0].cname; |
| 165 | } |
| 166 | |
| 167 | bool ready_to_send() const { |
| 168 | return ready_to_send_; |
| 169 | } |
| 170 | |
| 171 | protected: |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 172 | bool MuteStream(uint32 ssrc, bool mute) { |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 173 | if (!HasSendStream(ssrc) && ssrc != 0) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 174 | return false; |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 175 | } |
| 176 | if (mute) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 177 | muted_streams_.insert(ssrc); |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 178 | } else { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 179 | muted_streams_.erase(ssrc); |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 180 | } |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 181 | return true; |
| 182 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | bool set_sending(bool send) { |
| 184 | sending_ = send; |
| 185 | return true; |
| 186 | } |
| 187 | void set_playout(bool playout) { playout_ = playout; } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 188 | bool SetRecvRtpHeaderExtensions( |
| 189 | const std::vector<RtpHeaderExtension>& extensions) { |
| 190 | recv_extensions_ = extensions; |
| 191 | return true; |
| 192 | } |
| 193 | bool SetSendRtpHeaderExtensions( |
| 194 | const std::vector<RtpHeaderExtension>& extensions) { |
| 195 | send_extensions_ = extensions; |
| 196 | return true; |
| 197 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 198 | virtual void OnPacketReceived(rtc::Buffer* packet, |
| 199 | const rtc::PacketTime& packet_time) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 200 | rtp_packets_.push_back(std::string(packet->data<char>(), packet->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 201 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 202 | virtual void OnRtcpReceived(rtc::Buffer* packet, |
| 203 | const rtc::PacketTime& packet_time) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 204 | rtcp_packets_.push_back(std::string(packet->data<char>(), packet->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | } |
| 206 | virtual void OnReadyToSend(bool ready) { |
| 207 | ready_to_send_ = ready; |
| 208 | } |
| 209 | bool fail_set_send_codecs() const { return fail_set_send_codecs_; } |
| 210 | bool fail_set_recv_codecs() const { return fail_set_recv_codecs_; } |
| 211 | |
| 212 | private: |
| 213 | bool sending_; |
| 214 | bool playout_; |
| 215 | std::vector<RtpHeaderExtension> recv_extensions_; |
| 216 | std::vector<RtpHeaderExtension> send_extensions_; |
| 217 | std::list<std::string> rtp_packets_; |
| 218 | std::list<std::string> rtcp_packets_; |
| 219 | std::vector<StreamParams> send_streams_; |
| 220 | std::vector<StreamParams> receive_streams_; |
| 221 | std::set<uint32> muted_streams_; |
| 222 | bool fail_set_send_codecs_; |
| 223 | bool fail_set_recv_codecs_; |
| 224 | uint32 send_ssrc_; |
| 225 | std::string rtcp_cname_; |
| 226 | bool ready_to_send_; |
| 227 | }; |
| 228 | |
| 229 | class FakeVoiceMediaChannel : public RtpHelper<VoiceMediaChannel> { |
| 230 | public: |
| 231 | struct DtmfInfo { |
| 232 | DtmfInfo(uint32 ssrc, int event_code, int duration, int flags) |
| 233 | : ssrc(ssrc), event_code(event_code), duration(duration), flags(flags) { |
| 234 | } |
| 235 | uint32 ssrc; |
| 236 | int event_code; |
| 237 | int duration; |
| 238 | int flags; |
| 239 | }; |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 240 | explicit FakeVoiceMediaChannel(FakeVoiceEngine* engine, |
| 241 | const AudioOptions& options) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 242 | : engine_(engine), |
| 243 | fail_set_send_(false), |
| 244 | ringback_tone_ssrc_(0), |
| 245 | ringback_tone_play_(false), |
| 246 | ringback_tone_loop_(false), |
| 247 | time_since_last_typing_(-1) { |
| 248 | output_scalings_[0] = OutputScaling(); // For default channel. |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 249 | SetOptions(options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 250 | } |
| 251 | ~FakeVoiceMediaChannel(); |
| 252 | const std::vector<AudioCodec>& recv_codecs() const { return recv_codecs_; } |
| 253 | const std::vector<AudioCodec>& send_codecs() const { return send_codecs_; } |
| 254 | const std::vector<AudioCodec>& codecs() const { return send_codecs(); } |
| 255 | const std::vector<DtmfInfo>& dtmf_info_queue() const { |
| 256 | return dtmf_info_queue_; |
| 257 | } |
| 258 | const AudioOptions& options() const { return options_; } |
| 259 | |
| 260 | uint32 ringback_tone_ssrc() const { return ringback_tone_ssrc_; } |
| 261 | bool ringback_tone_play() const { return ringback_tone_play_; } |
| 262 | bool ringback_tone_loop() const { return ringback_tone_loop_; } |
| 263 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 264 | virtual bool SetSendParameters(const AudioSendParameters& params) { |
| 265 | return (SetSendCodecs(params.codecs) && |
| 266 | SetSendRtpHeaderExtensions(params.extensions) && |
| 267 | SetMaxSendBandwidth(params.max_bandwidth_bps) && |
| 268 | SetOptions(params.options)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 270 | |
| 271 | virtual bool SetRecvParameters(const AudioRecvParameters& params) { |
| 272 | return (SetRecvCodecs(params.codecs) && |
| 273 | SetRecvRtpHeaderExtensions(params.extensions)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 274 | } |
| 275 | virtual bool SetPlayout(bool playout) { |
| 276 | set_playout(playout); |
| 277 | return true; |
| 278 | } |
| 279 | virtual bool SetSend(SendFlags flag) { |
| 280 | if (fail_set_send_) { |
| 281 | return false; |
| 282 | } |
| 283 | return set_sending(flag != SEND_NOTHING); |
| 284 | } |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 285 | virtual bool SetAudioSend(uint32 ssrc, bool mute, |
| 286 | const AudioOptions* options, |
| 287 | AudioRenderer* renderer) { |
| 288 | if (!SetLocalRenderer(ssrc, renderer)) { |
| 289 | return false; |
| 290 | } |
| 291 | if (!RtpHelper<VoiceMediaChannel>::MuteStream(ssrc, mute)) { |
| 292 | return false; |
| 293 | } |
| 294 | if (!mute && options) { |
| 295 | return SetOptions(*options); |
| 296 | } |
| 297 | return true; |
| 298 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 299 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 300 | if (!RtpHelper<VoiceMediaChannel>::AddRecvStream(sp)) |
| 301 | return false; |
| 302 | output_scalings_[sp.first_ssrc()] = OutputScaling(); |
| 303 | return true; |
| 304 | } |
| 305 | virtual bool RemoveRecvStream(uint32 ssrc) { |
| 306 | if (!RtpHelper<VoiceMediaChannel>::RemoveRecvStream(ssrc)) |
| 307 | return false; |
| 308 | output_scalings_.erase(ssrc); |
| 309 | return true; |
| 310 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 311 | virtual bool SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) { |
| 312 | std::map<uint32, AudioRenderer*>::iterator it = |
| 313 | remote_renderers_.find(ssrc); |
| 314 | if (renderer) { |
| 315 | if (it != remote_renderers_.end()) { |
| 316 | ASSERT(it->second == renderer); |
| 317 | } else { |
| 318 | remote_renderers_.insert(std::make_pair(ssrc, renderer)); |
| 319 | renderer->AddChannel(0); |
| 320 | } |
| 321 | } else { |
| 322 | if (it != remote_renderers_.end()) { |
| 323 | it->second->RemoveChannel(0); |
| 324 | remote_renderers_.erase(it); |
| 325 | } else { |
| 326 | return false; |
| 327 | } |
| 328 | } |
| 329 | return true; |
| 330 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 331 | |
| 332 | virtual bool GetActiveStreams(AudioInfo::StreamList* streams) { return true; } |
| 333 | virtual int GetOutputLevel() { return 0; } |
| 334 | void set_time_since_last_typing(int ms) { time_since_last_typing_ = ms; } |
| 335 | virtual int GetTimeSinceLastTyping() { return time_since_last_typing_; } |
| 336 | virtual void SetTypingDetectionParameters( |
| 337 | int time_window, int cost_per_typing, int reporting_threshold, |
| 338 | int penalty_decay, int type_event_delay) {} |
| 339 | |
| 340 | virtual bool SetRingbackTone(const char* buf, int len) { return true; } |
| 341 | virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) { |
| 342 | ringback_tone_ssrc_ = ssrc; |
| 343 | ringback_tone_play_ = play; |
| 344 | ringback_tone_loop_ = loop; |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | virtual bool CanInsertDtmf() { |
| 349 | for (std::vector<AudioCodec>::const_iterator it = send_codecs_.begin(); |
| 350 | it != send_codecs_.end(); ++it) { |
| 351 | // Find the DTMF telephone event "codec". |
| 352 | if (_stricmp(it->name.c_str(), "telephone-event") == 0) { |
| 353 | return true; |
| 354 | } |
| 355 | } |
| 356 | return false; |
| 357 | } |
| 358 | virtual bool InsertDtmf(uint32 ssrc, int event_code, int duration, |
| 359 | int flags) { |
| 360 | dtmf_info_queue_.push_back(DtmfInfo(ssrc, event_code, duration, flags)); |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | virtual bool SetOutputScaling(uint32 ssrc, double left, double right) { |
| 365 | if (0 == ssrc) { |
| 366 | std::map<uint32, OutputScaling>::iterator it; |
| 367 | for (it = output_scalings_.begin(); it != output_scalings_.end(); ++it) { |
| 368 | it->second.left = left; |
| 369 | it->second.right = right; |
| 370 | } |
| 371 | return true; |
| 372 | } else if (output_scalings_.find(ssrc) != output_scalings_.end()) { |
| 373 | output_scalings_[ssrc].left = left; |
| 374 | output_scalings_[ssrc].right = right; |
| 375 | return true; |
| 376 | } |
| 377 | return false; |
| 378 | } |
solenberg | bb741b3 | 2015-09-07 03:56:38 -0700 | [diff] [blame] | 379 | bool GetOutputScaling(uint32 ssrc, double* left, double* right) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 380 | if (output_scalings_.find(ssrc) == output_scalings_.end()) |
| 381 | return false; |
| 382 | *left = output_scalings_[ssrc].left; |
| 383 | *right = output_scalings_[ssrc].right; |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | virtual bool GetStats(VoiceMediaInfo* info) { return false; } |
| 388 | virtual void GetLastMediaError(uint32* ssrc, |
| 389 | VoiceMediaChannel::Error* error) { |
| 390 | *ssrc = 0; |
| 391 | *error = fail_set_send_ ? VoiceMediaChannel::ERROR_REC_DEVICE_OPEN_FAILED |
| 392 | : VoiceMediaChannel::ERROR_NONE; |
| 393 | } |
| 394 | |
| 395 | void set_fail_set_send(bool fail) { fail_set_send_ = fail; } |
| 396 | void TriggerError(uint32 ssrc, VoiceMediaChannel::Error error) { |
| 397 | VoiceMediaChannel::SignalMediaError(ssrc, error); |
| 398 | } |
| 399 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 400 | private: |
| 401 | struct OutputScaling { |
| 402 | OutputScaling() : left(1.0), right(1.0) {} |
| 403 | double left, right; |
| 404 | }; |
| 405 | |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 406 | class VoiceChannelAudioSink : public AudioRenderer::Sink { |
| 407 | public: |
| 408 | explicit VoiceChannelAudioSink(AudioRenderer* renderer) |
| 409 | : renderer_(renderer) { |
| 410 | renderer_->AddChannel(0); |
| 411 | renderer_->SetSink(this); |
| 412 | } |
| 413 | virtual ~VoiceChannelAudioSink() { |
| 414 | if (renderer_) { |
| 415 | renderer_->RemoveChannel(0); |
| 416 | renderer_->SetSink(NULL); |
| 417 | } |
| 418 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 419 | void OnData(const void* audio_data, |
| 420 | int bits_per_sample, |
| 421 | int sample_rate, |
| 422 | int number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 423 | size_t number_of_frames) override {} |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 424 | void OnClose() override { renderer_ = NULL; } |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 425 | AudioRenderer* renderer() const { return renderer_; } |
| 426 | |
| 427 | private: |
| 428 | AudioRenderer* renderer_; |
| 429 | }; |
| 430 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 431 | bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) { |
| 432 | if (fail_set_recv_codecs()) { |
| 433 | // Fake the failure in SetRecvCodecs. |
| 434 | return false; |
| 435 | } |
| 436 | recv_codecs_ = codecs; |
| 437 | return true; |
| 438 | } |
| 439 | bool SetSendCodecs(const std::vector<AudioCodec>& codecs) { |
| 440 | if (fail_set_send_codecs()) { |
| 441 | // Fake the failure in SetSendCodecs. |
| 442 | return false; |
| 443 | } |
| 444 | send_codecs_ = codecs; |
| 445 | return true; |
| 446 | } |
| 447 | bool SetMaxSendBandwidth(int bps) { return true; } |
| 448 | bool SetOptions(const AudioOptions& options) { |
| 449 | // Does a "merge" of current options and set options. |
| 450 | options_.SetAll(options); |
| 451 | return true; |
| 452 | } |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 453 | bool SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) { |
| 454 | auto it = local_renderers_.find(ssrc); |
| 455 | if (renderer) { |
| 456 | if (it != local_renderers_.end()) { |
| 457 | ASSERT(it->second->renderer() == renderer); |
| 458 | } else { |
| 459 | local_renderers_.insert(std::make_pair( |
| 460 | ssrc, new VoiceChannelAudioSink(renderer))); |
| 461 | } |
| 462 | } else { |
| 463 | if (it != local_renderers_.end()) { |
| 464 | delete it->second; |
| 465 | local_renderers_.erase(it); |
| 466 | } |
| 467 | } |
| 468 | return true; |
| 469 | } |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 470 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 471 | FakeVoiceEngine* engine_; |
| 472 | std::vector<AudioCodec> recv_codecs_; |
| 473 | std::vector<AudioCodec> send_codecs_; |
| 474 | std::map<uint32, OutputScaling> output_scalings_; |
| 475 | std::vector<DtmfInfo> dtmf_info_queue_; |
| 476 | bool fail_set_send_; |
| 477 | uint32 ringback_tone_ssrc_; |
| 478 | bool ringback_tone_play_; |
| 479 | bool ringback_tone_loop_; |
| 480 | int time_since_last_typing_; |
| 481 | AudioOptions options_; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 482 | std::map<uint32, VoiceChannelAudioSink*> local_renderers_; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 483 | std::map<uint32, AudioRenderer*> remote_renderers_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | // A helper function to compare the FakeVoiceMediaChannel::DtmfInfo. |
| 487 | inline bool CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo& info, |
| 488 | uint32 ssrc, int event_code, int duration, |
| 489 | int flags) { |
| 490 | return (info.duration == duration && info.event_code == event_code && |
| 491 | info.flags == flags && info.ssrc == ssrc); |
| 492 | } |
| 493 | |
| 494 | class FakeVideoMediaChannel : public RtpHelper<VideoMediaChannel> { |
| 495 | public: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 496 | explicit FakeVideoMediaChannel(FakeVideoEngine* engine, |
| 497 | const VideoOptions& options) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 498 | : engine_(engine), |
| 499 | sent_intra_frame_(false), |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 500 | requested_intra_frame_(false), |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 501 | max_bps_(-1) { |
| 502 | SetOptions(options); |
| 503 | } |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 504 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 505 | ~FakeVideoMediaChannel(); |
| 506 | |
| 507 | const std::vector<VideoCodec>& recv_codecs() const { return recv_codecs_; } |
| 508 | const std::vector<VideoCodec>& send_codecs() const { return send_codecs_; } |
| 509 | const std::vector<VideoCodec>& codecs() const { return send_codecs(); } |
| 510 | bool rendering() const { return playout(); } |
| 511 | const VideoOptions& options() const { return options_; } |
| 512 | const std::map<uint32, VideoRenderer*>& renderers() const { |
| 513 | return renderers_; |
| 514 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 515 | int max_bps() const { return max_bps_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 516 | bool GetSendStreamFormat(uint32 ssrc, VideoFormat* format) { |
| 517 | if (send_formats_.find(ssrc) == send_formats_.end()) { |
| 518 | return false; |
| 519 | } |
| 520 | *format = send_formats_[ssrc]; |
| 521 | return true; |
| 522 | } |
| 523 | virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) { |
| 524 | if (send_formats_.find(ssrc) == send_formats_.end()) { |
| 525 | return false; |
| 526 | } |
| 527 | send_formats_[ssrc] = format; |
| 528 | return true; |
| 529 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 530 | virtual bool SetSendParameters(const VideoSendParameters& params) { |
| 531 | return (SetSendCodecs(params.codecs) && |
| 532 | SetSendRtpHeaderExtensions(params.extensions) && |
| 533 | SetMaxSendBandwidth(params.max_bandwidth_bps) && |
| 534 | SetOptions(params.options)); |
| 535 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 536 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 537 | virtual bool SetRecvParameters(const VideoRecvParameters& params) { |
| 538 | return (SetRecvCodecs(params.codecs) && |
| 539 | SetRecvRtpHeaderExtensions(params.extensions)); |
| 540 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 541 | virtual bool AddSendStream(const StreamParams& sp) { |
| 542 | if (!RtpHelper<VideoMediaChannel>::AddSendStream(sp)) { |
| 543 | return false; |
| 544 | } |
| 545 | SetSendStreamDefaultFormat(sp.first_ssrc()); |
| 546 | return true; |
| 547 | } |
| 548 | virtual bool RemoveSendStream(uint32 ssrc) { |
| 549 | send_formats_.erase(ssrc); |
| 550 | return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc); |
| 551 | } |
| 552 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 553 | virtual bool GetSendCodec(VideoCodec* send_codec) { |
| 554 | if (send_codecs_.empty()) { |
| 555 | return false; |
| 556 | } |
| 557 | *send_codec = send_codecs_[0]; |
| 558 | return true; |
| 559 | } |
| 560 | virtual bool SetRender(bool render) { |
| 561 | set_playout(render); |
| 562 | return true; |
| 563 | } |
| 564 | virtual bool SetRenderer(uint32 ssrc, VideoRenderer* r) { |
| 565 | if (ssrc != 0 && renderers_.find(ssrc) == renderers_.end()) { |
| 566 | return false; |
| 567 | } |
| 568 | if (ssrc != 0) { |
| 569 | renderers_[ssrc] = r; |
| 570 | } |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | virtual bool SetSend(bool send) { return set_sending(send); } |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 575 | virtual bool SetVideoSend(uint32 ssrc, bool mute, |
| 576 | const VideoOptions* options) { |
| 577 | if (!RtpHelper<VideoMediaChannel>::MuteStream(ssrc, mute)) { |
| 578 | return false; |
| 579 | } |
| 580 | if (!mute && options) { |
| 581 | return SetOptions(*options); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 582 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 583 | return true; |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 584 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 585 | virtual bool SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 586 | capturers_[ssrc] = capturer; |
| 587 | return true; |
| 588 | } |
| 589 | bool HasCapturer(uint32 ssrc) const { |
| 590 | return capturers_.find(ssrc) != capturers_.end(); |
| 591 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 592 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 593 | if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp)) |
| 594 | return false; |
| 595 | renderers_[sp.first_ssrc()] = NULL; |
| 596 | return true; |
| 597 | } |
| 598 | virtual bool RemoveRecvStream(uint32 ssrc) { |
| 599 | if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc)) |
| 600 | return false; |
| 601 | renderers_.erase(ssrc); |
| 602 | return true; |
| 603 | } |
| 604 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 605 | virtual bool GetStats(VideoMediaInfo* info) { return false; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 606 | virtual bool SendIntraFrame() { |
| 607 | sent_intra_frame_ = true; |
| 608 | return true; |
| 609 | } |
| 610 | virtual bool RequestIntraFrame() { |
| 611 | requested_intra_frame_ = true; |
| 612 | return true; |
| 613 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 614 | virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {} |
| 615 | void set_sent_intra_frame(bool v) { sent_intra_frame_ = v; } |
| 616 | bool sent_intra_frame() const { return sent_intra_frame_; } |
| 617 | void set_requested_intra_frame(bool v) { requested_intra_frame_ = v; } |
| 618 | bool requested_intra_frame() const { return requested_intra_frame_; } |
| 619 | |
| 620 | private: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 621 | bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
| 622 | if (fail_set_recv_codecs()) { |
| 623 | // Fake the failure in SetRecvCodecs. |
| 624 | return false; |
| 625 | } |
| 626 | recv_codecs_ = codecs; |
| 627 | return true; |
| 628 | } |
| 629 | bool SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 630 | if (fail_set_send_codecs()) { |
| 631 | // Fake the failure in SetSendCodecs. |
| 632 | return false; |
| 633 | } |
| 634 | send_codecs_ = codecs; |
| 635 | |
| 636 | for (std::vector<StreamParams>::const_iterator it = send_streams().begin(); |
| 637 | it != send_streams().end(); ++it) { |
| 638 | SetSendStreamDefaultFormat(it->first_ssrc()); |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | bool SetOptions(const VideoOptions& options) { |
| 643 | options_ = options; |
| 644 | return true; |
| 645 | } |
| 646 | bool SetMaxSendBandwidth(int bps) { |
| 647 | max_bps_ = bps; |
| 648 | return true; |
| 649 | } |
| 650 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 651 | // Be default, each send stream uses the first send codec format. |
| 652 | void SetSendStreamDefaultFormat(uint32 ssrc) { |
| 653 | if (!send_codecs_.empty()) { |
| 654 | send_formats_[ssrc] = VideoFormat( |
| 655 | send_codecs_[0].width, send_codecs_[0].height, |
| 656 | cricket::VideoFormat::FpsToInterval(send_codecs_[0].framerate), |
| 657 | cricket::FOURCC_I420); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | FakeVideoEngine* engine_; |
| 662 | std::vector<VideoCodec> recv_codecs_; |
| 663 | std::vector<VideoCodec> send_codecs_; |
| 664 | std::map<uint32, VideoRenderer*> renderers_; |
| 665 | std::map<uint32, VideoFormat> send_formats_; |
| 666 | std::map<uint32, VideoCapturer*> capturers_; |
| 667 | bool sent_intra_frame_; |
| 668 | bool requested_intra_frame_; |
| 669 | VideoOptions options_; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 670 | int max_bps_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 671 | }; |
| 672 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 673 | class FakeDataMediaChannel : public RtpHelper<DataMediaChannel> { |
| 674 | public: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 675 | explicit FakeDataMediaChannel(void* unused, const DataOptions& options) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 676 | : send_blocked_(false), max_bps_(-1) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 677 | ~FakeDataMediaChannel() {} |
| 678 | const std::vector<DataCodec>& recv_codecs() const { return recv_codecs_; } |
| 679 | const std::vector<DataCodec>& send_codecs() const { return send_codecs_; } |
| 680 | const std::vector<DataCodec>& codecs() const { return send_codecs(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 681 | int max_bps() const { return max_bps_; } |
| 682 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 683 | virtual bool SetSendParameters(const DataSendParameters& params) { |
| 684 | return (SetSendCodecs(params.codecs) && |
| 685 | SetMaxSendBandwidth(params.max_bandwidth_bps)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 686 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 687 | virtual bool SetRecvParameters(const DataRecvParameters& params) { |
| 688 | return SetRecvCodecs(params.codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 689 | } |
| 690 | virtual bool SetSend(bool send) { return set_sending(send); } |
| 691 | virtual bool SetReceive(bool receive) { |
| 692 | set_playout(receive); |
| 693 | return true; |
| 694 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 695 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 696 | if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp)) |
| 697 | return false; |
| 698 | return true; |
| 699 | } |
| 700 | virtual bool RemoveRecvStream(uint32 ssrc) { |
| 701 | if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc)) |
| 702 | return false; |
| 703 | return true; |
| 704 | } |
| 705 | |
| 706 | virtual bool SendData(const SendDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 707 | const rtc::Buffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 708 | SendDataResult* result) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 709 | if (send_blocked_) { |
| 710 | *result = SDR_BLOCK; |
| 711 | return false; |
| 712 | } else { |
| 713 | last_sent_data_params_ = params; |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 714 | last_sent_data_ = std::string(payload.data<char>(), payload.size()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 715 | return true; |
| 716 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | SendDataParams last_sent_data_params() { return last_sent_data_params_; } |
| 720 | std::string last_sent_data() { return last_sent_data_; } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 721 | bool is_send_blocked() { return send_blocked_; } |
| 722 | void set_send_blocked(bool blocked) { send_blocked_ = blocked; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 723 | |
| 724 | private: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 725 | bool SetRecvCodecs(const std::vector<DataCodec>& codecs) { |
| 726 | if (fail_set_recv_codecs()) { |
| 727 | // Fake the failure in SetRecvCodecs. |
| 728 | return false; |
| 729 | } |
| 730 | recv_codecs_ = codecs; |
| 731 | return true; |
| 732 | } |
| 733 | bool SetSendCodecs(const std::vector<DataCodec>& codecs) { |
| 734 | if (fail_set_send_codecs()) { |
| 735 | // Fake the failure in SetSendCodecs. |
| 736 | return false; |
| 737 | } |
| 738 | send_codecs_ = codecs; |
| 739 | return true; |
| 740 | } |
| 741 | bool SetMaxSendBandwidth(int bps) { |
| 742 | max_bps_ = bps; |
| 743 | return true; |
| 744 | } |
| 745 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 746 | std::vector<DataCodec> recv_codecs_; |
| 747 | std::vector<DataCodec> send_codecs_; |
| 748 | SendDataParams last_sent_data_params_; |
| 749 | std::string last_sent_data_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 750 | bool send_blocked_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 751 | int max_bps_; |
| 752 | }; |
| 753 | |
| 754 | // A base class for all of the shared parts between FakeVoiceEngine |
| 755 | // and FakeVideoEngine. |
| 756 | class FakeBaseEngine { |
| 757 | public: |
| 758 | FakeBaseEngine() |
| 759 | : loglevel_(-1), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 760 | options_changed_(false), |
| 761 | fail_create_channel_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 762 | void SetLogging(int level, const char* filter) { |
| 763 | loglevel_ = level; |
| 764 | logfilter_ = filter; |
| 765 | } |
| 766 | |
| 767 | void set_fail_create_channel(bool fail) { fail_create_channel_ = fail; } |
| 768 | |
| 769 | const std::vector<RtpHeaderExtension>& rtp_header_extensions() const { |
| 770 | return rtp_header_extensions_; |
| 771 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 772 | void set_rtp_header_extensions( |
| 773 | const std::vector<RtpHeaderExtension>& extensions) { |
| 774 | rtp_header_extensions_ = extensions; |
| 775 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 776 | |
| 777 | protected: |
| 778 | int loglevel_; |
| 779 | std::string logfilter_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 780 | // Flag used by optionsmessagehandler_unittest for checking whether any |
| 781 | // relevant setting has been updated. |
| 782 | // TODO(thaloun): Replace with explicit checks of before & after values. |
| 783 | bool options_changed_; |
| 784 | bool fail_create_channel_; |
| 785 | std::vector<RtpHeaderExtension> rtp_header_extensions_; |
| 786 | }; |
| 787 | |
| 788 | class FakeVoiceEngine : public FakeBaseEngine { |
| 789 | public: |
| 790 | FakeVoiceEngine() |
| 791 | : output_volume_(-1), |
| 792 | delay_offset_(0), |
| 793 | rx_processor_(NULL), |
| 794 | tx_processor_(NULL) { |
| 795 | // Add a fake audio codec. Note that the name must not be "" as there are |
| 796 | // sanity checks against that. |
| 797 | codecs_.push_back(AudioCodec(101, "fake_audio_codec", 0, 0, 1, 0)); |
| 798 | } |
Fredrik Solenberg | 9a416bd | 2015-05-22 09:04:09 +0200 | [diff] [blame] | 799 | bool Init(rtc::Thread* worker_thread) { return true; } |
| 800 | void Terminate() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 801 | int GetCapabilities() { return AUDIO_SEND | AUDIO_RECV; } |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 802 | webrtc::VoiceEngine* GetVoE() { return nullptr; } |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 803 | AudioOptions GetOptions() const { |
| 804 | return options_; |
| 805 | } |
| 806 | bool SetOptions(const AudioOptions& options) { |
| 807 | options_ = options; |
| 808 | options_changed_ = true; |
| 809 | return true; |
| 810 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 811 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 812 | VoiceMediaChannel* CreateChannel(webrtc::Call* call, |
| 813 | const AudioOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 814 | if (fail_create_channel_) { |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 815 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 818 | FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 819 | channels_.push_back(ch); |
| 820 | return ch; |
| 821 | } |
| 822 | FakeVoiceMediaChannel* GetChannel(size_t index) { |
| 823 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 824 | } |
| 825 | void UnregisterChannel(VoiceMediaChannel* channel) { |
| 826 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 827 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 828 | |
| 829 | const std::vector<AudioCodec>& codecs() { return codecs_; } |
| 830 | void SetCodecs(const std::vector<AudioCodec> codecs) { codecs_ = codecs; } |
| 831 | |
| 832 | bool SetDelayOffset(int offset) { |
| 833 | delay_offset_ = offset; |
| 834 | return true; |
| 835 | } |
| 836 | |
| 837 | bool SetDevices(const Device* in_device, const Device* out_device) { |
| 838 | in_device_ = (in_device) ? in_device->name : ""; |
| 839 | out_device_ = (out_device) ? out_device->name : ""; |
| 840 | options_changed_ = true; |
| 841 | return true; |
| 842 | } |
| 843 | |
| 844 | bool GetOutputVolume(int* level) { |
| 845 | *level = output_volume_; |
| 846 | return true; |
| 847 | } |
| 848 | |
| 849 | bool SetOutputVolume(int level) { |
| 850 | output_volume_ = level; |
| 851 | options_changed_ = true; |
| 852 | return true; |
| 853 | } |
| 854 | |
| 855 | int GetInputLevel() { return 0; } |
| 856 | |
| 857 | bool SetLocalMonitor(bool enable) { return true; } |
| 858 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 859 | bool StartAecDump(rtc::PlatformFile file) { return false; } |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 860 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 861 | bool RegisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor, |
| 862 | MediaProcessorDirection direction) { |
| 863 | if (direction == MPD_RX) { |
| 864 | rx_processor_ = voice_processor; |
| 865 | return true; |
| 866 | } else if (direction == MPD_TX) { |
| 867 | tx_processor_ = voice_processor; |
| 868 | return true; |
| 869 | } |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | bool UnregisterProcessor(uint32 ssrc, VoiceProcessor* voice_processor, |
| 874 | MediaProcessorDirection direction) { |
| 875 | bool unregistered = false; |
| 876 | if (direction & MPD_RX) { |
| 877 | rx_processor_ = NULL; |
| 878 | unregistered = true; |
| 879 | } |
| 880 | if (direction & MPD_TX) { |
| 881 | tx_processor_ = NULL; |
| 882 | unregistered = true; |
| 883 | } |
| 884 | return unregistered; |
| 885 | } |
| 886 | |
| 887 | private: |
| 888 | std::vector<FakeVoiceMediaChannel*> channels_; |
| 889 | std::vector<AudioCodec> codecs_; |
| 890 | int output_volume_; |
| 891 | int delay_offset_; |
| 892 | std::string in_device_; |
| 893 | std::string out_device_; |
| 894 | VoiceProcessor* rx_processor_; |
| 895 | VoiceProcessor* tx_processor_; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 896 | AudioOptions options_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 897 | |
| 898 | friend class FakeMediaEngine; |
| 899 | }; |
| 900 | |
| 901 | class FakeVideoEngine : public FakeBaseEngine { |
| 902 | public: |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 903 | FakeVideoEngine() : capture_(false) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 904 | // Add a fake video codec. Note that the name must not be "" as there are |
| 905 | // sanity checks against that. |
| 906 | codecs_.push_back(VideoCodec(0, "fake_video_codec", 0, 0, 0, 0)); |
| 907 | } |
Fredrik Solenberg | 9a416bd | 2015-05-22 09:04:09 +0200 | [diff] [blame] | 908 | void Init() {} |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 909 | bool SetOptions(const VideoOptions& options) { |
| 910 | options_ = options; |
| 911 | options_changed_ = true; |
| 912 | return true; |
| 913 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 914 | int GetCapabilities() { return VIDEO_SEND | VIDEO_RECV; } |
| 915 | bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) { |
| 916 | default_encoder_config_ = config; |
| 917 | return true; |
| 918 | } |
| 919 | const VideoEncoderConfig& default_encoder_config() const { |
| 920 | return default_encoder_config_; |
| 921 | } |
| 922 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 923 | VideoMediaChannel* CreateChannel(webrtc::Call* call, |
| 924 | const VideoOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 925 | if (fail_create_channel_) { |
| 926 | return NULL; |
| 927 | } |
| 928 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 929 | FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 930 | channels_.push_back(ch); |
| 931 | return ch; |
| 932 | } |
| 933 | FakeVideoMediaChannel* GetChannel(size_t index) { |
| 934 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 935 | } |
| 936 | void UnregisterChannel(VideoMediaChannel* channel) { |
| 937 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 938 | } |
| 939 | |
| 940 | const std::vector<VideoCodec>& codecs() const { return codecs_; } |
| 941 | bool FindCodec(const VideoCodec& in) { |
| 942 | for (size_t i = 0; i < codecs_.size(); ++i) { |
| 943 | if (codecs_[i].Matches(in)) { |
| 944 | return true; |
| 945 | } |
| 946 | } |
| 947 | return false; |
| 948 | } |
| 949 | void SetCodecs(const std::vector<VideoCodec> codecs) { codecs_ = codecs; } |
| 950 | |
| 951 | bool SetCaptureDevice(const Device* device) { |
| 952 | in_device_ = (device) ? device->name : ""; |
| 953 | options_changed_ = true; |
| 954 | return true; |
| 955 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 956 | bool SetCapture(bool capture) { |
| 957 | capture_ = capture; |
| 958 | return true; |
| 959 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 960 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 961 | private: |
| 962 | std::vector<FakeVideoMediaChannel*> channels_; |
| 963 | std::vector<VideoCodec> codecs_; |
| 964 | VideoEncoderConfig default_encoder_config_; |
| 965 | std::string in_device_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 966 | bool capture_; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 967 | VideoOptions options_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 968 | |
| 969 | friend class FakeMediaEngine; |
| 970 | }; |
| 971 | |
| 972 | class FakeMediaEngine : |
| 973 | public CompositeMediaEngine<FakeVoiceEngine, FakeVideoEngine> { |
| 974 | public: |
| 975 | FakeMediaEngine() { |
| 976 | voice_ = FakeVoiceEngine(); |
| 977 | video_ = FakeVideoEngine(); |
| 978 | } |
| 979 | virtual ~FakeMediaEngine() {} |
| 980 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 981 | void SetAudioCodecs(const std::vector<AudioCodec>& codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 982 | voice_.SetCodecs(codecs); |
| 983 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 984 | void SetVideoCodecs(const std::vector<VideoCodec>& codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 985 | video_.SetCodecs(codecs); |
| 986 | } |
| 987 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 988 | void SetAudioRtpHeaderExtensions( |
| 989 | const std::vector<RtpHeaderExtension>& extensions) { |
| 990 | voice_.set_rtp_header_extensions(extensions); |
| 991 | } |
| 992 | void SetVideoRtpHeaderExtensions( |
| 993 | const std::vector<RtpHeaderExtension>& extensions) { |
| 994 | video_.set_rtp_header_extensions(extensions); |
| 995 | } |
| 996 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 997 | FakeVoiceMediaChannel* GetVoiceChannel(size_t index) { |
| 998 | return voice_.GetChannel(index); |
| 999 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1000 | FakeVideoMediaChannel* GetVideoChannel(size_t index) { |
| 1001 | return video_.GetChannel(index); |
| 1002 | } |
| 1003 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 1004 | AudioOptions audio_options() const { return voice_.options_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1005 | int audio_delay_offset() const { return voice_.delay_offset_; } |
| 1006 | int output_volume() const { return voice_.output_volume_; } |
| 1007 | const VideoEncoderConfig& default_video_encoder_config() const { |
| 1008 | return video_.default_encoder_config_; |
| 1009 | } |
| 1010 | const std::string& audio_in_device() const { return voice_.in_device_; } |
| 1011 | const std::string& audio_out_device() const { return voice_.out_device_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1012 | int voice_loglevel() const { return voice_.loglevel_; } |
| 1013 | const std::string& voice_logfilter() const { return voice_.logfilter_; } |
| 1014 | int video_loglevel() const { return video_.loglevel_; } |
| 1015 | const std::string& video_logfilter() const { return video_.logfilter_; } |
| 1016 | bool capture() const { return video_.capture_; } |
| 1017 | bool options_changed() const { |
| 1018 | return voice_.options_changed_ || video_.options_changed_; |
| 1019 | } |
| 1020 | void clear_options_changed() { |
| 1021 | video_.options_changed_ = false; |
| 1022 | voice_.options_changed_ = false; |
| 1023 | } |
| 1024 | void set_fail_create_channel(bool fail) { |
| 1025 | voice_.set_fail_create_channel(fail); |
| 1026 | video_.set_fail_create_channel(fail); |
| 1027 | } |
| 1028 | bool voice_processor_registered(MediaProcessorDirection direction) const { |
| 1029 | if (direction == MPD_RX) { |
| 1030 | return voice_.rx_processor_ != NULL; |
| 1031 | } else if (direction == MPD_TX) { |
| 1032 | return voice_.tx_processor_ != NULL; |
| 1033 | } |
| 1034 | return false; |
| 1035 | } |
| 1036 | }; |
| 1037 | |
| 1038 | // CompositeMediaEngine with FakeVoiceEngine to expose SetAudioCodecs to |
| 1039 | // establish a media connectionwith minimum set of audio codes required |
| 1040 | template <class VIDEO> |
| 1041 | class CompositeMediaEngineWithFakeVoiceEngine : |
| 1042 | public CompositeMediaEngine<FakeVoiceEngine, VIDEO> { |
| 1043 | public: |
| 1044 | CompositeMediaEngineWithFakeVoiceEngine() {} |
| 1045 | virtual ~CompositeMediaEngineWithFakeVoiceEngine() {} |
| 1046 | |
| 1047 | virtual void SetAudioCodecs(const std::vector<AudioCodec>& codecs) { |
| 1048 | CompositeMediaEngine<FakeVoiceEngine, VIDEO>::voice_.SetCodecs(codecs); |
| 1049 | } |
| 1050 | }; |
| 1051 | |
| 1052 | // Have to come afterwards due to declaration order |
| 1053 | inline FakeVoiceMediaChannel::~FakeVoiceMediaChannel() { |
| 1054 | if (engine_) { |
| 1055 | engine_->UnregisterChannel(this); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | inline FakeVideoMediaChannel::~FakeVideoMediaChannel() { |
| 1060 | if (engine_) { |
| 1061 | engine_->UnregisterChannel(this); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | class FakeDataEngine : public DataEngineInterface { |
| 1066 | public: |
| 1067 | FakeDataEngine() : last_channel_type_(DCT_NONE) {} |
| 1068 | |
| 1069 | virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type) { |
| 1070 | last_channel_type_ = data_channel_type; |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame^] | 1071 | FakeDataMediaChannel* ch = new FakeDataMediaChannel(this, DataOptions()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1072 | channels_.push_back(ch); |
| 1073 | return ch; |
| 1074 | } |
| 1075 | |
| 1076 | FakeDataMediaChannel* GetChannel(size_t index) { |
| 1077 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 1078 | } |
| 1079 | |
| 1080 | void UnregisterChannel(DataMediaChannel* channel) { |
| 1081 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 1082 | } |
| 1083 | |
| 1084 | virtual void SetDataCodecs(const std::vector<DataCodec>& data_codecs) { |
| 1085 | data_codecs_ = data_codecs; |
| 1086 | } |
| 1087 | |
| 1088 | virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; } |
| 1089 | |
| 1090 | DataChannelType last_channel_type() const { return last_channel_type_; } |
| 1091 | |
| 1092 | private: |
| 1093 | std::vector<FakeDataMediaChannel*> channels_; |
| 1094 | std::vector<DataCodec> data_codecs_; |
| 1095 | DataChannelType last_channel_type_; |
| 1096 | }; |
| 1097 | |
| 1098 | } // namespace cricket |
| 1099 | |
| 1100 | #endif // TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_ |