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" |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 41 | #include "webrtc/audio/audio_sink.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" |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 44 | #include "webrtc/p2p/base/sessiondescription.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | |
| 46 | namespace cricket { |
| 47 | |
| 48 | class FakeMediaEngine; |
| 49 | class FakeVideoEngine; |
| 50 | class FakeVoiceEngine; |
| 51 | |
| 52 | // A common helper class that handles sending and receiving RTP/RTCP packets. |
| 53 | template <class Base> class RtpHelper : public Base { |
| 54 | public: |
| 55 | RtpHelper() |
| 56 | : sending_(false), |
| 57 | playout_(false), |
| 58 | fail_set_send_codecs_(false), |
| 59 | fail_set_recv_codecs_(false), |
| 60 | send_ssrc_(0), |
| 61 | ready_to_send_(false) {} |
| 62 | const std::vector<RtpHeaderExtension>& recv_extensions() { |
| 63 | return recv_extensions_; |
| 64 | } |
| 65 | const std::vector<RtpHeaderExtension>& send_extensions() { |
| 66 | return send_extensions_; |
| 67 | } |
| 68 | bool sending() const { return sending_; } |
| 69 | bool playout() const { return playout_; } |
| 70 | const std::list<std::string>& rtp_packets() const { return rtp_packets_; } |
| 71 | const std::list<std::string>& rtcp_packets() const { return rtcp_packets_; } |
| 72 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 73 | bool SendRtp(const void* data, int len, const rtc::PacketOptions& options) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 74 | if (!sending_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 77 | rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, |
| 78 | kMaxRtpPacketLen); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 79 | return Base::SendPacket(&packet, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | } |
| 81 | bool SendRtcp(const void* data, int len) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 82 | rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, |
| 83 | kMaxRtpPacketLen); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 84 | return Base::SendRtcp(&packet, rtc::PacketOptions()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool CheckRtp(const void* data, int len) { |
| 88 | bool success = !rtp_packets_.empty(); |
| 89 | if (success) { |
| 90 | std::string packet = rtp_packets_.front(); |
| 91 | rtp_packets_.pop_front(); |
| 92 | success = (packet == std::string(static_cast<const char*>(data), len)); |
| 93 | } |
| 94 | return success; |
| 95 | } |
| 96 | bool CheckRtcp(const void* data, int len) { |
| 97 | bool success = !rtcp_packets_.empty(); |
| 98 | if (success) { |
| 99 | std::string packet = rtcp_packets_.front(); |
| 100 | rtcp_packets_.pop_front(); |
| 101 | success = (packet == std::string(static_cast<const char*>(data), len)); |
| 102 | } |
| 103 | return success; |
| 104 | } |
| 105 | bool CheckNoRtp() { return rtp_packets_.empty(); } |
| 106 | bool CheckNoRtcp() { return rtcp_packets_.empty(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | void set_fail_set_send_codecs(bool fail) { fail_set_send_codecs_ = fail; } |
| 108 | void set_fail_set_recv_codecs(bool fail) { fail_set_recv_codecs_ = fail; } |
| 109 | virtual bool AddSendStream(const StreamParams& sp) { |
| 110 | if (std::find(send_streams_.begin(), send_streams_.end(), sp) != |
| 111 | send_streams_.end()) { |
| 112 | return false; |
| 113 | } |
| 114 | send_streams_.push_back(sp); |
| 115 | return true; |
| 116 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 117 | virtual bool RemoveSendStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | return RemoveStreamBySsrc(&send_streams_, ssrc); |
| 119 | } |
| 120 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 121 | if (std::find(receive_streams_.begin(), receive_streams_.end(), sp) != |
| 122 | receive_streams_.end()) { |
| 123 | return false; |
| 124 | } |
| 125 | receive_streams_.push_back(sp); |
| 126 | return true; |
| 127 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 128 | virtual bool RemoveRecvStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | return RemoveStreamBySsrc(&receive_streams_, ssrc); |
| 130 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 131 | bool IsStreamMuted(uint32_t ssrc) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | bool ret = muted_streams_.find(ssrc) != muted_streams_.end(); |
| 133 | // If |ssrc = 0| check if the first send stream is muted. |
| 134 | if (!ret && ssrc == 0 && !send_streams_.empty()) { |
| 135 | return muted_streams_.find(send_streams_[0].first_ssrc()) != |
| 136 | muted_streams_.end(); |
| 137 | } |
| 138 | return ret; |
| 139 | } |
| 140 | const std::vector<StreamParams>& send_streams() const { |
| 141 | return send_streams_; |
| 142 | } |
| 143 | const std::vector<StreamParams>& recv_streams() const { |
| 144 | return receive_streams_; |
| 145 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 146 | bool HasRecvStream(uint32_t ssrc) const { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 147 | return GetStreamBySsrc(receive_streams_, ssrc) != nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 149 | bool HasSendStream(uint32_t ssrc) const { |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 150 | return GetStreamBySsrc(send_streams_, ssrc) != nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | } |
| 152 | // TODO(perkj): This is to support legacy unit test that only check one |
| 153 | // sending stream. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 154 | uint32_t send_ssrc() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | if (send_streams_.empty()) |
| 156 | return 0; |
| 157 | return send_streams_[0].first_ssrc(); |
| 158 | } |
| 159 | |
| 160 | // TODO(perkj): This is to support legacy unit test that only check one |
| 161 | // sending stream. |
| 162 | const std::string rtcp_cname() { |
| 163 | if (send_streams_.empty()) |
| 164 | return ""; |
| 165 | return send_streams_[0].cname; |
| 166 | } |
| 167 | |
| 168 | bool ready_to_send() const { |
| 169 | return ready_to_send_; |
| 170 | } |
| 171 | |
| 172 | protected: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 173 | bool MuteStream(uint32_t ssrc, bool mute) { |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 174 | if (!HasSendStream(ssrc) && ssrc != 0) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 175 | return false; |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 176 | } |
| 177 | if (mute) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 178 | muted_streams_.insert(ssrc); |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 179 | } else { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 180 | muted_streams_.erase(ssrc); |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 181 | } |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 182 | return true; |
| 183 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 184 | bool set_sending(bool send) { |
| 185 | sending_ = send; |
| 186 | return true; |
| 187 | } |
| 188 | void set_playout(bool playout) { playout_ = playout; } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 189 | bool SetRecvRtpHeaderExtensions( |
| 190 | const std::vector<RtpHeaderExtension>& extensions) { |
| 191 | recv_extensions_ = extensions; |
| 192 | return true; |
| 193 | } |
| 194 | bool SetSendRtpHeaderExtensions( |
| 195 | const std::vector<RtpHeaderExtension>& extensions) { |
| 196 | send_extensions_ = extensions; |
| 197 | return true; |
| 198 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 199 | virtual void OnPacketReceived(rtc::Buffer* packet, |
| 200 | const rtc::PacketTime& packet_time) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 201 | rtp_packets_.push_back(std::string(packet->data<char>(), packet->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 202 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 203 | virtual void OnRtcpReceived(rtc::Buffer* packet, |
| 204 | const rtc::PacketTime& packet_time) { |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 205 | rtcp_packets_.push_back(std::string(packet->data<char>(), packet->size())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 206 | } |
| 207 | virtual void OnReadyToSend(bool ready) { |
| 208 | ready_to_send_ = ready; |
| 209 | } |
| 210 | bool fail_set_send_codecs() const { return fail_set_send_codecs_; } |
| 211 | bool fail_set_recv_codecs() const { return fail_set_recv_codecs_; } |
| 212 | |
| 213 | private: |
| 214 | bool sending_; |
| 215 | bool playout_; |
| 216 | std::vector<RtpHeaderExtension> recv_extensions_; |
| 217 | std::vector<RtpHeaderExtension> send_extensions_; |
| 218 | std::list<std::string> rtp_packets_; |
| 219 | std::list<std::string> rtcp_packets_; |
| 220 | std::vector<StreamParams> send_streams_; |
| 221 | std::vector<StreamParams> receive_streams_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 222 | std::set<uint32_t> muted_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 223 | bool fail_set_send_codecs_; |
| 224 | bool fail_set_recv_codecs_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 225 | uint32_t send_ssrc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | std::string rtcp_cname_; |
| 227 | bool ready_to_send_; |
| 228 | }; |
| 229 | |
| 230 | class FakeVoiceMediaChannel : public RtpHelper<VoiceMediaChannel> { |
| 231 | public: |
| 232 | struct DtmfInfo { |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 233 | DtmfInfo(uint32_t ssrc, int event_code, int duration) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 234 | : ssrc(ssrc), |
| 235 | event_code(event_code), |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 236 | duration(duration) {} |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 237 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | int event_code; |
| 239 | int duration; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 240 | }; |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 241 | explicit FakeVoiceMediaChannel(FakeVoiceEngine* engine, |
| 242 | const AudioOptions& options) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 243 | : engine_(engine), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 244 | time_since_last_typing_(-1) { |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 245 | output_scalings_[0] = 1.0; // For default channel. |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 246 | SetOptions(options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | } |
| 248 | ~FakeVoiceMediaChannel(); |
| 249 | const std::vector<AudioCodec>& recv_codecs() const { return recv_codecs_; } |
| 250 | const std::vector<AudioCodec>& send_codecs() const { return send_codecs_; } |
| 251 | const std::vector<AudioCodec>& codecs() const { return send_codecs(); } |
| 252 | const std::vector<DtmfInfo>& dtmf_info_queue() const { |
| 253 | return dtmf_info_queue_; |
| 254 | } |
| 255 | const AudioOptions& options() const { return options_; } |
| 256 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 257 | virtual bool SetSendParameters(const AudioSendParameters& params) { |
| 258 | return (SetSendCodecs(params.codecs) && |
| 259 | SetSendRtpHeaderExtensions(params.extensions) && |
| 260 | SetMaxSendBandwidth(params.max_bandwidth_bps) && |
| 261 | SetOptions(params.options)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 262 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 263 | |
| 264 | virtual bool SetRecvParameters(const AudioRecvParameters& params) { |
| 265 | return (SetRecvCodecs(params.codecs) && |
| 266 | SetRecvRtpHeaderExtensions(params.extensions)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 267 | } |
| 268 | virtual bool SetPlayout(bool playout) { |
| 269 | set_playout(playout); |
| 270 | return true; |
| 271 | } |
| 272 | virtual bool SetSend(SendFlags flag) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 273 | return set_sending(flag != SEND_NOTHING); |
| 274 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 275 | virtual bool SetAudioSend(uint32_t ssrc, |
| 276 | bool enable, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 277 | const AudioOptions* options, |
| 278 | AudioRenderer* renderer) { |
| 279 | if (!SetLocalRenderer(ssrc, renderer)) { |
| 280 | return false; |
| 281 | } |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 282 | if (!RtpHelper<VoiceMediaChannel>::MuteStream(ssrc, !enable)) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 283 | return false; |
| 284 | } |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 285 | if (enable && options) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 286 | return SetOptions(*options); |
| 287 | } |
| 288 | return true; |
| 289 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 290 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 291 | if (!RtpHelper<VoiceMediaChannel>::AddRecvStream(sp)) |
| 292 | return false; |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 293 | output_scalings_[sp.first_ssrc()] = 1.0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 294 | return true; |
| 295 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 296 | virtual bool RemoveRecvStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 297 | if (!RtpHelper<VoiceMediaChannel>::RemoveRecvStream(ssrc)) |
| 298 | return false; |
| 299 | output_scalings_.erase(ssrc); |
| 300 | return true; |
| 301 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 302 | |
| 303 | virtual bool GetActiveStreams(AudioInfo::StreamList* streams) { return true; } |
| 304 | virtual int GetOutputLevel() { return 0; } |
| 305 | void set_time_since_last_typing(int ms) { time_since_last_typing_ = ms; } |
| 306 | virtual int GetTimeSinceLastTyping() { return time_since_last_typing_; } |
| 307 | virtual void SetTypingDetectionParameters( |
| 308 | int time_window, int cost_per_typing, int reporting_threshold, |
| 309 | int penalty_decay, int type_event_delay) {} |
| 310 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 311 | virtual bool CanInsertDtmf() { |
| 312 | for (std::vector<AudioCodec>::const_iterator it = send_codecs_.begin(); |
| 313 | it != send_codecs_.end(); ++it) { |
| 314 | // Find the DTMF telephone event "codec". |
| 315 | if (_stricmp(it->name.c_str(), "telephone-event") == 0) { |
| 316 | return true; |
| 317 | } |
| 318 | } |
| 319 | return false; |
| 320 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 321 | virtual bool InsertDtmf(uint32_t ssrc, |
| 322 | int event_code, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 323 | int duration) { |
| 324 | dtmf_info_queue_.push_back(DtmfInfo(ssrc, event_code, duration)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 325 | return true; |
| 326 | } |
| 327 | |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 328 | virtual bool SetOutputVolume(uint32_t ssrc, double volume) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 329 | if (0 == ssrc) { |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 330 | std::map<uint32_t, double>::iterator it; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 331 | for (it = output_scalings_.begin(); it != output_scalings_.end(); ++it) { |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 332 | it->second = volume; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 333 | } |
| 334 | return true; |
| 335 | } else if (output_scalings_.find(ssrc) != output_scalings_.end()) { |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 336 | output_scalings_[ssrc] = volume; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | return false; |
| 340 | } |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 341 | bool GetOutputVolume(uint32_t ssrc, double* volume) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 342 | if (output_scalings_.find(ssrc) == output_scalings_.end()) |
| 343 | return false; |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 344 | *volume = output_scalings_[ssrc]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
| 348 | virtual bool GetStats(VoiceMediaInfo* info) { return false; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 349 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 350 | virtual void SetRawAudioSink( |
| 351 | uint32_t ssrc, |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 352 | rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) { |
| 353 | sink_ = std::move(sink); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 354 | } |
| 355 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 356 | private: |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 357 | class VoiceChannelAudioSink : public AudioRenderer::Sink { |
| 358 | public: |
| 359 | explicit VoiceChannelAudioSink(AudioRenderer* renderer) |
| 360 | : renderer_(renderer) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 361 | renderer_->SetSink(this); |
| 362 | } |
| 363 | virtual ~VoiceChannelAudioSink() { |
| 364 | if (renderer_) { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 365 | renderer_->SetSink(NULL); |
| 366 | } |
| 367 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 368 | void OnData(const void* audio_data, |
| 369 | int bits_per_sample, |
| 370 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 371 | size_t number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 372 | size_t number_of_frames) override {} |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 373 | void OnClose() override { renderer_ = NULL; } |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 374 | AudioRenderer* renderer() const { return renderer_; } |
| 375 | |
| 376 | private: |
| 377 | AudioRenderer* renderer_; |
| 378 | }; |
| 379 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 380 | bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) { |
| 381 | if (fail_set_recv_codecs()) { |
| 382 | // Fake the failure in SetRecvCodecs. |
| 383 | return false; |
| 384 | } |
| 385 | recv_codecs_ = codecs; |
| 386 | return true; |
| 387 | } |
| 388 | bool SetSendCodecs(const std::vector<AudioCodec>& codecs) { |
| 389 | if (fail_set_send_codecs()) { |
| 390 | // Fake the failure in SetSendCodecs. |
| 391 | return false; |
| 392 | } |
| 393 | send_codecs_ = codecs; |
| 394 | return true; |
| 395 | } |
| 396 | bool SetMaxSendBandwidth(int bps) { return true; } |
| 397 | bool SetOptions(const AudioOptions& options) { |
| 398 | // Does a "merge" of current options and set options. |
| 399 | options_.SetAll(options); |
| 400 | return true; |
| 401 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 402 | bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 403 | auto it = local_renderers_.find(ssrc); |
| 404 | if (renderer) { |
| 405 | if (it != local_renderers_.end()) { |
| 406 | ASSERT(it->second->renderer() == renderer); |
| 407 | } else { |
| 408 | local_renderers_.insert(std::make_pair( |
| 409 | ssrc, new VoiceChannelAudioSink(renderer))); |
| 410 | } |
| 411 | } else { |
| 412 | if (it != local_renderers_.end()) { |
| 413 | delete it->second; |
| 414 | local_renderers_.erase(it); |
| 415 | } |
| 416 | } |
| 417 | return true; |
| 418 | } |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 419 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 420 | FakeVoiceEngine* engine_; |
| 421 | std::vector<AudioCodec> recv_codecs_; |
| 422 | std::vector<AudioCodec> send_codecs_; |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 423 | std::map<uint32_t, double> output_scalings_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 424 | std::vector<DtmfInfo> dtmf_info_queue_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 425 | int time_since_last_typing_; |
| 426 | AudioOptions options_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 427 | std::map<uint32_t, VoiceChannelAudioSink*> local_renderers_; |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 428 | rtc::scoped_ptr<webrtc::AudioSinkInterface> sink_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 429 | }; |
| 430 | |
| 431 | // A helper function to compare the FakeVoiceMediaChannel::DtmfInfo. |
| 432 | inline bool CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo& info, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 433 | uint32_t ssrc, |
| 434 | int event_code, |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 435 | int duration) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | return (info.duration == duration && info.event_code == event_code && |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 437 | info.ssrc == ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | class FakeVideoMediaChannel : public RtpHelper<VideoMediaChannel> { |
| 441 | public: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 442 | explicit FakeVideoMediaChannel(FakeVideoEngine* engine, |
| 443 | const VideoOptions& options) |
Peter Boström | a6c39d9 | 2016-02-01 19:30:33 +0100 | [diff] [blame] | 444 | : engine_(engine), max_bps_(-1) { |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 445 | SetOptions(options); |
| 446 | } |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 447 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 448 | ~FakeVideoMediaChannel(); |
| 449 | |
| 450 | const std::vector<VideoCodec>& recv_codecs() const { return recv_codecs_; } |
| 451 | const std::vector<VideoCodec>& send_codecs() const { return send_codecs_; } |
| 452 | const std::vector<VideoCodec>& codecs() const { return send_codecs(); } |
| 453 | bool rendering() const { return playout(); } |
| 454 | const VideoOptions& options() const { return options_; } |
nisse | 08582ff | 2016-02-04 01:24:52 -0800 | [diff] [blame^] | 455 | const std::map<uint32_t, rtc::VideoSinkInterface<VideoFrame>*>& sinks() |
| 456 | const { |
| 457 | return sinks_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 458 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 459 | int max_bps() const { return max_bps_; } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 460 | virtual bool SetSendParameters(const VideoSendParameters& params) { |
| 461 | return (SetSendCodecs(params.codecs) && |
| 462 | SetSendRtpHeaderExtensions(params.extensions) && |
| 463 | SetMaxSendBandwidth(params.max_bandwidth_bps) && |
| 464 | SetOptions(params.options)); |
| 465 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 466 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 467 | virtual bool SetRecvParameters(const VideoRecvParameters& params) { |
| 468 | return (SetRecvCodecs(params.codecs) && |
| 469 | SetRecvRtpHeaderExtensions(params.extensions)); |
| 470 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 471 | virtual bool AddSendStream(const StreamParams& sp) { |
Peter Boström | ce23bee | 2016-02-02 14:14:30 +0100 | [diff] [blame] | 472 | return RtpHelper<VideoMediaChannel>::AddSendStream(sp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 473 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 474 | virtual bool RemoveSendStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 475 | return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc); |
| 476 | } |
| 477 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 478 | virtual bool GetSendCodec(VideoCodec* send_codec) { |
| 479 | if (send_codecs_.empty()) { |
| 480 | return false; |
| 481 | } |
| 482 | *send_codec = send_codecs_[0]; |
| 483 | return true; |
| 484 | } |
nisse | 08582ff | 2016-02-04 01:24:52 -0800 | [diff] [blame^] | 485 | bool SetSink(uint32_t ssrc, |
| 486 | rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override { |
| 487 | if (ssrc != 0 && sinks_.find(ssrc) == sinks_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | if (ssrc != 0) { |
nisse | 08582ff | 2016-02-04 01:24:52 -0800 | [diff] [blame^] | 491 | sinks_[ssrc] = sink; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | virtual bool SetSend(bool send) { return set_sending(send); } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 497 | virtual bool SetVideoSend(uint32_t ssrc, bool enable, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 498 | const VideoOptions* options) { |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 499 | if (!RtpHelper<VideoMediaChannel>::MuteStream(ssrc, !enable)) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 500 | return false; |
| 501 | } |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 502 | if (enable && options) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 503 | return SetOptions(*options); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 504 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 505 | return true; |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 506 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 507 | virtual bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 508 | capturers_[ssrc] = capturer; |
| 509 | return true; |
| 510 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 511 | bool HasCapturer(uint32_t ssrc) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 512 | return capturers_.find(ssrc) != capturers_.end(); |
| 513 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 514 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 515 | if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp)) |
| 516 | return false; |
nisse | 08582ff | 2016-02-04 01:24:52 -0800 | [diff] [blame^] | 517 | sinks_[sp.first_ssrc()] = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 518 | return true; |
| 519 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 520 | virtual bool RemoveRecvStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 521 | if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc)) |
| 522 | return false; |
nisse | 08582ff | 2016-02-04 01:24:52 -0800 | [diff] [blame^] | 523 | sinks_.erase(ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 524 | return true; |
| 525 | } |
| 526 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 527 | virtual bool GetStats(VideoMediaInfo* info) { return false; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 528 | |
| 529 | private: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 530 | bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
| 531 | if (fail_set_recv_codecs()) { |
| 532 | // Fake the failure in SetRecvCodecs. |
| 533 | return false; |
| 534 | } |
| 535 | recv_codecs_ = codecs; |
| 536 | return true; |
| 537 | } |
| 538 | bool SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 539 | if (fail_set_send_codecs()) { |
| 540 | // Fake the failure in SetSendCodecs. |
| 541 | return false; |
| 542 | } |
| 543 | send_codecs_ = codecs; |
| 544 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 545 | return true; |
| 546 | } |
| 547 | bool SetOptions(const VideoOptions& options) { |
| 548 | options_ = options; |
| 549 | return true; |
| 550 | } |
| 551 | bool SetMaxSendBandwidth(int bps) { |
| 552 | max_bps_ = bps; |
| 553 | return true; |
| 554 | } |
| 555 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 556 | FakeVideoEngine* engine_; |
| 557 | std::vector<VideoCodec> recv_codecs_; |
| 558 | std::vector<VideoCodec> send_codecs_; |
nisse | 08582ff | 2016-02-04 01:24:52 -0800 | [diff] [blame^] | 559 | std::map<uint32_t, rtc::VideoSinkInterface<VideoFrame>*> sinks_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 560 | std::map<uint32_t, VideoCapturer*> capturers_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 561 | VideoOptions options_; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 562 | int max_bps_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 563 | }; |
| 564 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 565 | class FakeDataMediaChannel : public RtpHelper<DataMediaChannel> { |
| 566 | public: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 567 | explicit FakeDataMediaChannel(void* unused, const DataOptions& options) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 568 | : send_blocked_(false), max_bps_(-1) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 569 | ~FakeDataMediaChannel() {} |
| 570 | const std::vector<DataCodec>& recv_codecs() const { return recv_codecs_; } |
| 571 | const std::vector<DataCodec>& send_codecs() const { return send_codecs_; } |
| 572 | const std::vector<DataCodec>& codecs() const { return send_codecs(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 573 | int max_bps() const { return max_bps_; } |
| 574 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 575 | virtual bool SetSendParameters(const DataSendParameters& params) { |
| 576 | return (SetSendCodecs(params.codecs) && |
| 577 | SetMaxSendBandwidth(params.max_bandwidth_bps)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 578 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 579 | virtual bool SetRecvParameters(const DataRecvParameters& params) { |
| 580 | return SetRecvCodecs(params.codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 581 | } |
| 582 | virtual bool SetSend(bool send) { return set_sending(send); } |
| 583 | virtual bool SetReceive(bool receive) { |
| 584 | set_playout(receive); |
| 585 | return true; |
| 586 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 587 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 588 | if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp)) |
| 589 | return false; |
| 590 | return true; |
| 591 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 592 | virtual bool RemoveRecvStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 593 | if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc)) |
| 594 | return false; |
| 595 | return true; |
| 596 | } |
| 597 | |
| 598 | virtual bool SendData(const SendDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 599 | const rtc::Buffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 600 | SendDataResult* result) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 601 | if (send_blocked_) { |
| 602 | *result = SDR_BLOCK; |
| 603 | return false; |
| 604 | } else { |
| 605 | last_sent_data_params_ = params; |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 606 | last_sent_data_ = std::string(payload.data<char>(), payload.size()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 607 | return true; |
| 608 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | SendDataParams last_sent_data_params() { return last_sent_data_params_; } |
| 612 | std::string last_sent_data() { return last_sent_data_; } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 613 | bool is_send_blocked() { return send_blocked_; } |
| 614 | void set_send_blocked(bool blocked) { send_blocked_ = blocked; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 615 | |
| 616 | private: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 617 | bool SetRecvCodecs(const std::vector<DataCodec>& codecs) { |
| 618 | if (fail_set_recv_codecs()) { |
| 619 | // Fake the failure in SetRecvCodecs. |
| 620 | return false; |
| 621 | } |
| 622 | recv_codecs_ = codecs; |
| 623 | return true; |
| 624 | } |
| 625 | bool SetSendCodecs(const std::vector<DataCodec>& codecs) { |
| 626 | if (fail_set_send_codecs()) { |
| 627 | // Fake the failure in SetSendCodecs. |
| 628 | return false; |
| 629 | } |
| 630 | send_codecs_ = codecs; |
| 631 | return true; |
| 632 | } |
| 633 | bool SetMaxSendBandwidth(int bps) { |
| 634 | max_bps_ = bps; |
| 635 | return true; |
| 636 | } |
| 637 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 638 | std::vector<DataCodec> recv_codecs_; |
| 639 | std::vector<DataCodec> send_codecs_; |
| 640 | SendDataParams last_sent_data_params_; |
| 641 | std::string last_sent_data_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 642 | bool send_blocked_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 643 | int max_bps_; |
| 644 | }; |
| 645 | |
| 646 | // A base class for all of the shared parts between FakeVoiceEngine |
| 647 | // and FakeVideoEngine. |
| 648 | class FakeBaseEngine { |
| 649 | public: |
| 650 | FakeBaseEngine() |
solenberg | bd13838 | 2015-11-20 16:08:07 -0800 | [diff] [blame] | 651 | : options_changed_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 652 | fail_create_channel_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 653 | void set_fail_create_channel(bool fail) { fail_create_channel_ = fail; } |
| 654 | |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 655 | RtpCapabilities GetCapabilities() const { return capabilities_; } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 656 | void set_rtp_header_extensions( |
| 657 | const std::vector<RtpHeaderExtension>& extensions) { |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 658 | capabilities_.header_extensions = extensions; |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 659 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 660 | |
| 661 | protected: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 662 | // Flag used by optionsmessagehandler_unittest for checking whether any |
| 663 | // relevant setting has been updated. |
| 664 | // TODO(thaloun): Replace with explicit checks of before & after values. |
| 665 | bool options_changed_; |
| 666 | bool fail_create_channel_; |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 667 | RtpCapabilities capabilities_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 668 | }; |
| 669 | |
| 670 | class FakeVoiceEngine : public FakeBaseEngine { |
| 671 | public: |
| 672 | FakeVoiceEngine() |
solenberg | 4a3ccad | 2015-09-24 03:53:08 -0700 | [diff] [blame] | 673 | : output_volume_(-1) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 674 | // Add a fake audio codec. Note that the name must not be "" as there are |
| 675 | // sanity checks against that. |
| 676 | codecs_.push_back(AudioCodec(101, "fake_audio_codec", 0, 0, 1, 0)); |
| 677 | } |
Fredrik Solenberg | 9a416bd | 2015-05-22 09:04:09 +0200 | [diff] [blame] | 678 | bool Init(rtc::Thread* worker_thread) { return true; } |
| 679 | void Terminate() {} |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 680 | rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const { |
| 681 | return rtc::scoped_refptr<webrtc::AudioState>(); |
| 682 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 683 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 684 | VoiceMediaChannel* CreateChannel(webrtc::Call* call, |
| 685 | const AudioOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 686 | if (fail_create_channel_) { |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 687 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 690 | FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 691 | channels_.push_back(ch); |
| 692 | return ch; |
| 693 | } |
| 694 | FakeVoiceMediaChannel* GetChannel(size_t index) { |
| 695 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 696 | } |
| 697 | void UnregisterChannel(VoiceMediaChannel* channel) { |
| 698 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 699 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 700 | |
| 701 | const std::vector<AudioCodec>& codecs() { return codecs_; } |
| 702 | void SetCodecs(const std::vector<AudioCodec> codecs) { codecs_ = codecs; } |
| 703 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 704 | bool GetOutputVolume(int* level) { |
| 705 | *level = output_volume_; |
| 706 | return true; |
| 707 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 708 | bool SetOutputVolume(int level) { |
| 709 | output_volume_ = level; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 710 | return true; |
| 711 | } |
| 712 | |
| 713 | int GetInputLevel() { return 0; } |
| 714 | |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 715 | bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes) { |
| 716 | return false; |
| 717 | } |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 718 | |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 719 | void StopAecDump() {} |
| 720 | |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 721 | bool StartRtcEventLog(rtc::PlatformFile file) { return false; } |
| 722 | |
| 723 | void StopRtcEventLog() {} |
| 724 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 725 | private: |
| 726 | std::vector<FakeVoiceMediaChannel*> channels_; |
| 727 | std::vector<AudioCodec> codecs_; |
| 728 | int output_volume_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 729 | |
| 730 | friend class FakeMediaEngine; |
| 731 | }; |
| 732 | |
| 733 | class FakeVideoEngine : public FakeBaseEngine { |
| 734 | public: |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 735 | FakeVideoEngine() : capture_(false) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 736 | // Add a fake video codec. Note that the name must not be "" as there are |
| 737 | // sanity checks against that. |
| 738 | codecs_.push_back(VideoCodec(0, "fake_video_codec", 0, 0, 0, 0)); |
| 739 | } |
Fredrik Solenberg | 9a416bd | 2015-05-22 09:04:09 +0200 | [diff] [blame] | 740 | void Init() {} |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 741 | bool SetOptions(const VideoOptions& options) { |
| 742 | options_ = options; |
| 743 | options_changed_ = true; |
| 744 | return true; |
| 745 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 746 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 747 | VideoMediaChannel* CreateChannel(webrtc::Call* call, |
| 748 | const VideoOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 749 | if (fail_create_channel_) { |
| 750 | return NULL; |
| 751 | } |
| 752 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 753 | FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 754 | channels_.push_back(ch); |
| 755 | return ch; |
| 756 | } |
| 757 | FakeVideoMediaChannel* GetChannel(size_t index) { |
| 758 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 759 | } |
| 760 | void UnregisterChannel(VideoMediaChannel* channel) { |
| 761 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 762 | } |
| 763 | |
| 764 | const std::vector<VideoCodec>& codecs() const { return codecs_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 765 | void SetCodecs(const std::vector<VideoCodec> codecs) { codecs_ = codecs; } |
| 766 | |
| 767 | bool SetCaptureDevice(const Device* device) { |
| 768 | in_device_ = (device) ? device->name : ""; |
| 769 | options_changed_ = true; |
| 770 | return true; |
| 771 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 772 | bool SetCapture(bool capture) { |
| 773 | capture_ = capture; |
| 774 | return true; |
| 775 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 776 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 777 | private: |
| 778 | std::vector<FakeVideoMediaChannel*> channels_; |
| 779 | std::vector<VideoCodec> codecs_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 780 | std::string in_device_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 781 | bool capture_; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 782 | VideoOptions options_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 783 | |
| 784 | friend class FakeMediaEngine; |
| 785 | }; |
| 786 | |
| 787 | class FakeMediaEngine : |
| 788 | public CompositeMediaEngine<FakeVoiceEngine, FakeVideoEngine> { |
| 789 | public: |
solenberg | 246b817 | 2015-12-08 09:50:23 -0800 | [diff] [blame] | 790 | FakeMediaEngine() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 791 | virtual ~FakeMediaEngine() {} |
| 792 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 793 | void SetAudioCodecs(const std::vector<AudioCodec>& codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 794 | voice_.SetCodecs(codecs); |
| 795 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 796 | void SetVideoCodecs(const std::vector<VideoCodec>& codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 797 | video_.SetCodecs(codecs); |
| 798 | } |
| 799 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 800 | void SetAudioRtpHeaderExtensions( |
| 801 | const std::vector<RtpHeaderExtension>& extensions) { |
| 802 | voice_.set_rtp_header_extensions(extensions); |
| 803 | } |
| 804 | void SetVideoRtpHeaderExtensions( |
| 805 | const std::vector<RtpHeaderExtension>& extensions) { |
| 806 | video_.set_rtp_header_extensions(extensions); |
| 807 | } |
| 808 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 809 | FakeVoiceMediaChannel* GetVoiceChannel(size_t index) { |
| 810 | return voice_.GetChannel(index); |
| 811 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 812 | FakeVideoMediaChannel* GetVideoChannel(size_t index) { |
| 813 | return video_.GetChannel(index); |
| 814 | } |
| 815 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 816 | int output_volume() const { return voice_.output_volume_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 817 | bool capture() const { return video_.capture_; } |
| 818 | bool options_changed() const { |
solenberg | 246b817 | 2015-12-08 09:50:23 -0800 | [diff] [blame] | 819 | return video_.options_changed_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 820 | } |
| 821 | void clear_options_changed() { |
| 822 | video_.options_changed_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 823 | } |
| 824 | void set_fail_create_channel(bool fail) { |
| 825 | voice_.set_fail_create_channel(fail); |
| 826 | video_.set_fail_create_channel(fail); |
| 827 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 828 | }; |
| 829 | |
| 830 | // CompositeMediaEngine with FakeVoiceEngine to expose SetAudioCodecs to |
| 831 | // establish a media connectionwith minimum set of audio codes required |
| 832 | template <class VIDEO> |
| 833 | class CompositeMediaEngineWithFakeVoiceEngine : |
| 834 | public CompositeMediaEngine<FakeVoiceEngine, VIDEO> { |
| 835 | public: |
| 836 | CompositeMediaEngineWithFakeVoiceEngine() {} |
| 837 | virtual ~CompositeMediaEngineWithFakeVoiceEngine() {} |
| 838 | |
| 839 | virtual void SetAudioCodecs(const std::vector<AudioCodec>& codecs) { |
| 840 | CompositeMediaEngine<FakeVoiceEngine, VIDEO>::voice_.SetCodecs(codecs); |
| 841 | } |
| 842 | }; |
| 843 | |
| 844 | // Have to come afterwards due to declaration order |
| 845 | inline FakeVoiceMediaChannel::~FakeVoiceMediaChannel() { |
| 846 | if (engine_) { |
| 847 | engine_->UnregisterChannel(this); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | inline FakeVideoMediaChannel::~FakeVideoMediaChannel() { |
| 852 | if (engine_) { |
| 853 | engine_->UnregisterChannel(this); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | class FakeDataEngine : public DataEngineInterface { |
| 858 | public: |
| 859 | FakeDataEngine() : last_channel_type_(DCT_NONE) {} |
| 860 | |
| 861 | virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type) { |
| 862 | last_channel_type_ = data_channel_type; |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 863 | FakeDataMediaChannel* ch = new FakeDataMediaChannel(this, DataOptions()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 864 | channels_.push_back(ch); |
| 865 | return ch; |
| 866 | } |
| 867 | |
| 868 | FakeDataMediaChannel* GetChannel(size_t index) { |
| 869 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 870 | } |
| 871 | |
| 872 | void UnregisterChannel(DataMediaChannel* channel) { |
| 873 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 874 | } |
| 875 | |
| 876 | virtual void SetDataCodecs(const std::vector<DataCodec>& data_codecs) { |
| 877 | data_codecs_ = data_codecs; |
| 878 | } |
| 879 | |
| 880 | virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; } |
| 881 | |
| 882 | DataChannelType last_channel_type() const { return last_channel_type_; } |
| 883 | |
| 884 | private: |
| 885 | std::vector<FakeDataMediaChannel*> channels_; |
| 886 | std::vector<DataCodec> data_codecs_; |
| 887 | DataChannelType last_channel_type_; |
| 888 | }; |
| 889 | |
| 890 | } // namespace cricket |
| 891 | |
| 892 | #endif // TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_ |