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 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 116 | virtual bool RemoveSendStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 127 | virtual bool RemoveRecvStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | return RemoveStreamBySsrc(&receive_streams_, ssrc); |
| 129 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 130 | bool IsStreamMuted(uint32_t ssrc) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 145 | bool HasRecvStream(uint32_t 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 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 148 | bool HasSendStream(uint32_t 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. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 153 | uint32_t 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: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 172 | bool MuteStream(uint32_t 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_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 221 | std::set<uint32_t> muted_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | bool fail_set_send_codecs_; |
| 223 | bool fail_set_recv_codecs_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 224 | uint32_t send_ssrc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 225 | std::string rtcp_cname_; |
| 226 | bool ready_to_send_; |
| 227 | }; |
| 228 | |
| 229 | class FakeVoiceMediaChannel : public RtpHelper<VoiceMediaChannel> { |
| 230 | public: |
| 231 | struct DtmfInfo { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 232 | DtmfInfo(uint32_t ssrc, int event_code, int duration, int flags) |
| 233 | : ssrc(ssrc), |
| 234 | event_code(event_code), |
| 235 | duration(duration), |
| 236 | flags(flags) {} |
| 237 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | int event_code; |
| 239 | int duration; |
| 240 | int flags; |
| 241 | }; |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 242 | explicit FakeVoiceMediaChannel(FakeVoiceEngine* engine, |
| 243 | const AudioOptions& options) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 244 | : engine_(engine), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 245 | time_since_last_typing_(-1) { |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 246 | output_scalings_[0] = 1.0; // For default channel. |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 247 | SetOptions(options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 248 | } |
| 249 | ~FakeVoiceMediaChannel(); |
| 250 | const std::vector<AudioCodec>& recv_codecs() const { return recv_codecs_; } |
| 251 | const std::vector<AudioCodec>& send_codecs() const { return send_codecs_; } |
| 252 | const std::vector<AudioCodec>& codecs() const { return send_codecs(); } |
| 253 | const std::vector<DtmfInfo>& dtmf_info_queue() const { |
| 254 | return dtmf_info_queue_; |
| 255 | } |
| 256 | const AudioOptions& options() const { return options_; } |
| 257 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 258 | virtual bool SetSendParameters(const AudioSendParameters& params) { |
| 259 | return (SetSendCodecs(params.codecs) && |
| 260 | SetSendRtpHeaderExtensions(params.extensions) && |
| 261 | SetMaxSendBandwidth(params.max_bandwidth_bps) && |
| 262 | SetOptions(params.options)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 263 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 264 | |
| 265 | virtual bool SetRecvParameters(const AudioRecvParameters& params) { |
| 266 | return (SetRecvCodecs(params.codecs) && |
| 267 | SetRecvRtpHeaderExtensions(params.extensions)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | } |
| 269 | virtual bool SetPlayout(bool playout) { |
| 270 | set_playout(playout); |
| 271 | return true; |
| 272 | } |
| 273 | virtual bool SetSend(SendFlags flag) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 274 | return set_sending(flag != SEND_NOTHING); |
| 275 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 276 | virtual bool SetAudioSend(uint32_t ssrc, |
| 277 | bool enable, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 278 | const AudioOptions* options, |
| 279 | AudioRenderer* renderer) { |
| 280 | if (!SetLocalRenderer(ssrc, renderer)) { |
| 281 | return false; |
| 282 | } |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 283 | if (!RtpHelper<VoiceMediaChannel>::MuteStream(ssrc, !enable)) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 286 | if (enable && options) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 287 | return SetOptions(*options); |
| 288 | } |
| 289 | return true; |
| 290 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 291 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 292 | if (!RtpHelper<VoiceMediaChannel>::AddRecvStream(sp)) |
| 293 | return false; |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 294 | output_scalings_[sp.first_ssrc()] = 1.0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 295 | return true; |
| 296 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 297 | virtual bool RemoveRecvStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 298 | if (!RtpHelper<VoiceMediaChannel>::RemoveRecvStream(ssrc)) |
| 299 | return false; |
| 300 | output_scalings_.erase(ssrc); |
| 301 | return true; |
| 302 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 303 | virtual bool SetRemoteRenderer(uint32_t ssrc, AudioRenderer* renderer) { |
| 304 | std::map<uint32_t, AudioRenderer*>::iterator it = |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 305 | remote_renderers_.find(ssrc); |
| 306 | if (renderer) { |
| 307 | if (it != remote_renderers_.end()) { |
| 308 | ASSERT(it->second == renderer); |
| 309 | } else { |
| 310 | remote_renderers_.insert(std::make_pair(ssrc, renderer)); |
torbjorng | eefbc3b | 2015-10-08 13:10:36 -0700 | [diff] [blame] | 311 | renderer->AddChannel(0); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 312 | } |
| 313 | } else { |
| 314 | if (it != remote_renderers_.end()) { |
torbjorng | eefbc3b | 2015-10-08 13:10:36 -0700 | [diff] [blame] | 315 | it->second->RemoveChannel(0); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 316 | remote_renderers_.erase(it); |
| 317 | } else { |
| 318 | return false; |
| 319 | } |
| 320 | } |
| 321 | return true; |
| 322 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 323 | |
| 324 | virtual bool GetActiveStreams(AudioInfo::StreamList* streams) { return true; } |
| 325 | virtual int GetOutputLevel() { return 0; } |
| 326 | void set_time_since_last_typing(int ms) { time_since_last_typing_ = ms; } |
| 327 | virtual int GetTimeSinceLastTyping() { return time_since_last_typing_; } |
| 328 | virtual void SetTypingDetectionParameters( |
| 329 | int time_window, int cost_per_typing, int reporting_threshold, |
| 330 | int penalty_decay, int type_event_delay) {} |
| 331 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 332 | virtual bool CanInsertDtmf() { |
| 333 | for (std::vector<AudioCodec>::const_iterator it = send_codecs_.begin(); |
| 334 | it != send_codecs_.end(); ++it) { |
| 335 | // Find the DTMF telephone event "codec". |
| 336 | if (_stricmp(it->name.c_str(), "telephone-event") == 0) { |
| 337 | return true; |
| 338 | } |
| 339 | } |
| 340 | return false; |
| 341 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 342 | virtual bool InsertDtmf(uint32_t ssrc, |
| 343 | int event_code, |
| 344 | int duration, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 345 | int flags) { |
| 346 | dtmf_info_queue_.push_back(DtmfInfo(ssrc, event_code, duration, flags)); |
| 347 | return true; |
| 348 | } |
| 349 | |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 350 | virtual bool SetOutputVolume(uint32_t ssrc, double volume) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 351 | if (0 == ssrc) { |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 352 | std::map<uint32_t, double>::iterator it; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 353 | for (it = output_scalings_.begin(); it != output_scalings_.end(); ++it) { |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 354 | it->second = volume; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 355 | } |
| 356 | return true; |
| 357 | } else if (output_scalings_.find(ssrc) != output_scalings_.end()) { |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 358 | output_scalings_[ssrc] = volume; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | return false; |
| 362 | } |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 363 | bool GetOutputVolume(uint32_t ssrc, double* volume) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 364 | if (output_scalings_.find(ssrc) == output_scalings_.end()) |
| 365 | return false; |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 366 | *volume = output_scalings_[ssrc]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
| 370 | virtual bool GetStats(VoiceMediaInfo* info) { return false; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 371 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 372 | private: |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 373 | class VoiceChannelAudioSink : public AudioRenderer::Sink { |
| 374 | public: |
| 375 | explicit VoiceChannelAudioSink(AudioRenderer* renderer) |
| 376 | : renderer_(renderer) { |
torbjorng | eefbc3b | 2015-10-08 13:10:36 -0700 | [diff] [blame] | 377 | renderer_->AddChannel(0); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 378 | renderer_->SetSink(this); |
| 379 | } |
| 380 | virtual ~VoiceChannelAudioSink() { |
| 381 | if (renderer_) { |
torbjorng | eefbc3b | 2015-10-08 13:10:36 -0700 | [diff] [blame] | 382 | renderer_->RemoveChannel(0); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 383 | renderer_->SetSink(NULL); |
| 384 | } |
| 385 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 386 | void OnData(const void* audio_data, |
| 387 | int bits_per_sample, |
| 388 | int sample_rate, |
| 389 | int number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 390 | size_t number_of_frames) override {} |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 391 | void OnClose() override { renderer_ = NULL; } |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 392 | AudioRenderer* renderer() const { return renderer_; } |
| 393 | |
| 394 | private: |
| 395 | AudioRenderer* renderer_; |
| 396 | }; |
| 397 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 398 | bool SetRecvCodecs(const std::vector<AudioCodec>& codecs) { |
| 399 | if (fail_set_recv_codecs()) { |
| 400 | // Fake the failure in SetRecvCodecs. |
| 401 | return false; |
| 402 | } |
| 403 | recv_codecs_ = codecs; |
| 404 | return true; |
| 405 | } |
| 406 | bool SetSendCodecs(const std::vector<AudioCodec>& codecs) { |
| 407 | if (fail_set_send_codecs()) { |
| 408 | // Fake the failure in SetSendCodecs. |
| 409 | return false; |
| 410 | } |
| 411 | send_codecs_ = codecs; |
| 412 | return true; |
| 413 | } |
| 414 | bool SetMaxSendBandwidth(int bps) { return true; } |
| 415 | bool SetOptions(const AudioOptions& options) { |
| 416 | // Does a "merge" of current options and set options. |
| 417 | options_.SetAll(options); |
| 418 | return true; |
| 419 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 420 | bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 421 | auto it = local_renderers_.find(ssrc); |
| 422 | if (renderer) { |
| 423 | if (it != local_renderers_.end()) { |
| 424 | ASSERT(it->second->renderer() == renderer); |
| 425 | } else { |
| 426 | local_renderers_.insert(std::make_pair( |
| 427 | ssrc, new VoiceChannelAudioSink(renderer))); |
| 428 | } |
| 429 | } else { |
| 430 | if (it != local_renderers_.end()) { |
| 431 | delete it->second; |
| 432 | local_renderers_.erase(it); |
| 433 | } |
| 434 | } |
| 435 | return true; |
| 436 | } |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 437 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 438 | FakeVoiceEngine* engine_; |
| 439 | std::vector<AudioCodec> recv_codecs_; |
| 440 | std::vector<AudioCodec> send_codecs_; |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame^] | 441 | std::map<uint32_t, double> output_scalings_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 442 | std::vector<DtmfInfo> dtmf_info_queue_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 443 | int time_since_last_typing_; |
| 444 | AudioOptions options_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 445 | std::map<uint32_t, VoiceChannelAudioSink*> local_renderers_; |
| 446 | std::map<uint32_t, AudioRenderer*> remote_renderers_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | // A helper function to compare the FakeVoiceMediaChannel::DtmfInfo. |
| 450 | inline bool CompareDtmfInfo(const FakeVoiceMediaChannel::DtmfInfo& info, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 451 | uint32_t ssrc, |
| 452 | int event_code, |
| 453 | int duration, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 454 | int flags) { |
| 455 | return (info.duration == duration && info.event_code == event_code && |
| 456 | info.flags == flags && info.ssrc == ssrc); |
| 457 | } |
| 458 | |
| 459 | class FakeVideoMediaChannel : public RtpHelper<VideoMediaChannel> { |
| 460 | public: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 461 | explicit FakeVideoMediaChannel(FakeVideoEngine* engine, |
| 462 | const VideoOptions& options) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 463 | : engine_(engine), |
| 464 | sent_intra_frame_(false), |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 465 | requested_intra_frame_(false), |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 466 | max_bps_(-1) { |
| 467 | SetOptions(options); |
| 468 | } |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 469 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 470 | ~FakeVideoMediaChannel(); |
| 471 | |
| 472 | const std::vector<VideoCodec>& recv_codecs() const { return recv_codecs_; } |
| 473 | const std::vector<VideoCodec>& send_codecs() const { return send_codecs_; } |
| 474 | const std::vector<VideoCodec>& codecs() const { return send_codecs(); } |
| 475 | bool rendering() const { return playout(); } |
| 476 | const VideoOptions& options() const { return options_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 477 | const std::map<uint32_t, VideoRenderer*>& renderers() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 478 | return renderers_; |
| 479 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 480 | int max_bps() const { return max_bps_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 481 | bool GetSendStreamFormat(uint32_t ssrc, VideoFormat* format) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 482 | if (send_formats_.find(ssrc) == send_formats_.end()) { |
| 483 | return false; |
| 484 | } |
| 485 | *format = send_formats_[ssrc]; |
| 486 | return true; |
| 487 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 488 | virtual bool SetSendStreamFormat(uint32_t ssrc, const VideoFormat& format) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 489 | if (send_formats_.find(ssrc) == send_formats_.end()) { |
| 490 | return false; |
| 491 | } |
| 492 | send_formats_[ssrc] = format; |
| 493 | return true; |
| 494 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 495 | virtual bool SetSendParameters(const VideoSendParameters& params) { |
| 496 | return (SetSendCodecs(params.codecs) && |
| 497 | SetSendRtpHeaderExtensions(params.extensions) && |
| 498 | SetMaxSendBandwidth(params.max_bandwidth_bps) && |
| 499 | SetOptions(params.options)); |
| 500 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 501 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 502 | virtual bool SetRecvParameters(const VideoRecvParameters& params) { |
| 503 | return (SetRecvCodecs(params.codecs) && |
| 504 | SetRecvRtpHeaderExtensions(params.extensions)); |
| 505 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 506 | virtual bool AddSendStream(const StreamParams& sp) { |
| 507 | if (!RtpHelper<VideoMediaChannel>::AddSendStream(sp)) { |
| 508 | return false; |
| 509 | } |
| 510 | SetSendStreamDefaultFormat(sp.first_ssrc()); |
| 511 | return true; |
| 512 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 513 | virtual bool RemoveSendStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 514 | send_formats_.erase(ssrc); |
| 515 | return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc); |
| 516 | } |
| 517 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 518 | virtual bool GetSendCodec(VideoCodec* send_codec) { |
| 519 | if (send_codecs_.empty()) { |
| 520 | return false; |
| 521 | } |
| 522 | *send_codec = send_codecs_[0]; |
| 523 | return true; |
| 524 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 525 | virtual bool SetRenderer(uint32_t ssrc, VideoRenderer* r) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 526 | if (ssrc != 0 && renderers_.find(ssrc) == renderers_.end()) { |
| 527 | return false; |
| 528 | } |
| 529 | if (ssrc != 0) { |
| 530 | renderers_[ssrc] = r; |
| 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | virtual bool SetSend(bool send) { return set_sending(send); } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 536 | virtual bool SetVideoSend(uint32_t ssrc, bool enable, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 537 | const VideoOptions* options) { |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 538 | if (!RtpHelper<VideoMediaChannel>::MuteStream(ssrc, !enable)) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 539 | return false; |
| 540 | } |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 541 | if (enable && options) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 542 | return SetOptions(*options); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 543 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 544 | return true; |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 545 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 546 | virtual bool SetCapturer(uint32_t ssrc, VideoCapturer* capturer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 547 | capturers_[ssrc] = capturer; |
| 548 | return true; |
| 549 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 550 | bool HasCapturer(uint32_t ssrc) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 551 | return capturers_.find(ssrc) != capturers_.end(); |
| 552 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 553 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 554 | if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp)) |
| 555 | return false; |
| 556 | renderers_[sp.first_ssrc()] = NULL; |
| 557 | return true; |
| 558 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 559 | virtual bool RemoveRecvStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 560 | if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc)) |
| 561 | return false; |
| 562 | renderers_.erase(ssrc); |
| 563 | return true; |
| 564 | } |
| 565 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 566 | virtual bool GetStats(VideoMediaInfo* info) { return false; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 567 | virtual bool SendIntraFrame() { |
| 568 | sent_intra_frame_ = true; |
| 569 | return true; |
| 570 | } |
| 571 | virtual bool RequestIntraFrame() { |
| 572 | requested_intra_frame_ = true; |
| 573 | return true; |
| 574 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 575 | virtual void UpdateAspectRatio(int ratio_w, int ratio_h) {} |
| 576 | void set_sent_intra_frame(bool v) { sent_intra_frame_ = v; } |
| 577 | bool sent_intra_frame() const { return sent_intra_frame_; } |
| 578 | void set_requested_intra_frame(bool v) { requested_intra_frame_ = v; } |
| 579 | bool requested_intra_frame() const { return requested_intra_frame_; } |
| 580 | |
| 581 | private: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 582 | bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
| 583 | if (fail_set_recv_codecs()) { |
| 584 | // Fake the failure in SetRecvCodecs. |
| 585 | return false; |
| 586 | } |
| 587 | recv_codecs_ = codecs; |
| 588 | return true; |
| 589 | } |
| 590 | bool SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 591 | if (fail_set_send_codecs()) { |
| 592 | // Fake the failure in SetSendCodecs. |
| 593 | return false; |
| 594 | } |
| 595 | send_codecs_ = codecs; |
| 596 | |
| 597 | for (std::vector<StreamParams>::const_iterator it = send_streams().begin(); |
| 598 | it != send_streams().end(); ++it) { |
| 599 | SetSendStreamDefaultFormat(it->first_ssrc()); |
| 600 | } |
| 601 | return true; |
| 602 | } |
| 603 | bool SetOptions(const VideoOptions& options) { |
| 604 | options_ = options; |
| 605 | return true; |
| 606 | } |
| 607 | bool SetMaxSendBandwidth(int bps) { |
| 608 | max_bps_ = bps; |
| 609 | return true; |
| 610 | } |
| 611 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 612 | // Be default, each send stream uses the first send codec format. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 613 | void SetSendStreamDefaultFormat(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 614 | if (!send_codecs_.empty()) { |
| 615 | send_formats_[ssrc] = VideoFormat( |
| 616 | send_codecs_[0].width, send_codecs_[0].height, |
| 617 | cricket::VideoFormat::FpsToInterval(send_codecs_[0].framerate), |
| 618 | cricket::FOURCC_I420); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | FakeVideoEngine* engine_; |
| 623 | std::vector<VideoCodec> recv_codecs_; |
| 624 | std::vector<VideoCodec> send_codecs_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 625 | std::map<uint32_t, VideoRenderer*> renderers_; |
| 626 | std::map<uint32_t, VideoFormat> send_formats_; |
| 627 | std::map<uint32_t, VideoCapturer*> capturers_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 628 | bool sent_intra_frame_; |
| 629 | bool requested_intra_frame_; |
| 630 | VideoOptions options_; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 631 | int max_bps_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 632 | }; |
| 633 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 634 | class FakeDataMediaChannel : public RtpHelper<DataMediaChannel> { |
| 635 | public: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 636 | explicit FakeDataMediaChannel(void* unused, const DataOptions& options) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 637 | : send_blocked_(false), max_bps_(-1) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 638 | ~FakeDataMediaChannel() {} |
| 639 | const std::vector<DataCodec>& recv_codecs() const { return recv_codecs_; } |
| 640 | const std::vector<DataCodec>& send_codecs() const { return send_codecs_; } |
| 641 | const std::vector<DataCodec>& codecs() const { return send_codecs(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 642 | int max_bps() const { return max_bps_; } |
| 643 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 644 | virtual bool SetSendParameters(const DataSendParameters& params) { |
| 645 | return (SetSendCodecs(params.codecs) && |
| 646 | SetMaxSendBandwidth(params.max_bandwidth_bps)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 647 | } |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 648 | virtual bool SetRecvParameters(const DataRecvParameters& params) { |
| 649 | return SetRecvCodecs(params.codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 650 | } |
| 651 | virtual bool SetSend(bool send) { return set_sending(send); } |
| 652 | virtual bool SetReceive(bool receive) { |
| 653 | set_playout(receive); |
| 654 | return true; |
| 655 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 656 | virtual bool AddRecvStream(const StreamParams& sp) { |
| 657 | if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp)) |
| 658 | return false; |
| 659 | return true; |
| 660 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 661 | virtual bool RemoveRecvStream(uint32_t ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 662 | if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc)) |
| 663 | return false; |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | virtual bool SendData(const SendDataParams& params, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 668 | const rtc::Buffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 669 | SendDataResult* result) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 670 | if (send_blocked_) { |
| 671 | *result = SDR_BLOCK; |
| 672 | return false; |
| 673 | } else { |
| 674 | last_sent_data_params_ = params; |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 675 | last_sent_data_ = std::string(payload.data<char>(), payload.size()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 676 | return true; |
| 677 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | SendDataParams last_sent_data_params() { return last_sent_data_params_; } |
| 681 | std::string last_sent_data() { return last_sent_data_; } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 682 | bool is_send_blocked() { return send_blocked_; } |
| 683 | void set_send_blocked(bool blocked) { send_blocked_ = blocked; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 684 | |
| 685 | private: |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 686 | bool SetRecvCodecs(const std::vector<DataCodec>& codecs) { |
| 687 | if (fail_set_recv_codecs()) { |
| 688 | // Fake the failure in SetRecvCodecs. |
| 689 | return false; |
| 690 | } |
| 691 | recv_codecs_ = codecs; |
| 692 | return true; |
| 693 | } |
| 694 | bool SetSendCodecs(const std::vector<DataCodec>& codecs) { |
| 695 | if (fail_set_send_codecs()) { |
| 696 | // Fake the failure in SetSendCodecs. |
| 697 | return false; |
| 698 | } |
| 699 | send_codecs_ = codecs; |
| 700 | return true; |
| 701 | } |
| 702 | bool SetMaxSendBandwidth(int bps) { |
| 703 | max_bps_ = bps; |
| 704 | return true; |
| 705 | } |
| 706 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 707 | std::vector<DataCodec> recv_codecs_; |
| 708 | std::vector<DataCodec> send_codecs_; |
| 709 | SendDataParams last_sent_data_params_; |
| 710 | std::string last_sent_data_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 711 | bool send_blocked_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 712 | int max_bps_; |
| 713 | }; |
| 714 | |
| 715 | // A base class for all of the shared parts between FakeVoiceEngine |
| 716 | // and FakeVideoEngine. |
| 717 | class FakeBaseEngine { |
| 718 | public: |
| 719 | FakeBaseEngine() |
| 720 | : loglevel_(-1), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 721 | options_changed_(false), |
| 722 | fail_create_channel_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 723 | void SetLogging(int level, const char* filter) { |
| 724 | loglevel_ = level; |
| 725 | logfilter_ = filter; |
| 726 | } |
| 727 | |
| 728 | void set_fail_create_channel(bool fail) { fail_create_channel_ = fail; } |
| 729 | |
| 730 | const std::vector<RtpHeaderExtension>& rtp_header_extensions() const { |
| 731 | return rtp_header_extensions_; |
| 732 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 733 | void set_rtp_header_extensions( |
| 734 | const std::vector<RtpHeaderExtension>& extensions) { |
| 735 | rtp_header_extensions_ = extensions; |
| 736 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 737 | |
| 738 | protected: |
| 739 | int loglevel_; |
| 740 | std::string logfilter_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 741 | // Flag used by optionsmessagehandler_unittest for checking whether any |
| 742 | // relevant setting has been updated. |
| 743 | // TODO(thaloun): Replace with explicit checks of before & after values. |
| 744 | bool options_changed_; |
| 745 | bool fail_create_channel_; |
| 746 | std::vector<RtpHeaderExtension> rtp_header_extensions_; |
| 747 | }; |
| 748 | |
| 749 | class FakeVoiceEngine : public FakeBaseEngine { |
| 750 | public: |
| 751 | FakeVoiceEngine() |
solenberg | 4a3ccad | 2015-09-24 03:53:08 -0700 | [diff] [blame] | 752 | : output_volume_(-1) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 753 | // Add a fake audio codec. Note that the name must not be "" as there are |
| 754 | // sanity checks against that. |
| 755 | codecs_.push_back(AudioCodec(101, "fake_audio_codec", 0, 0, 1, 0)); |
| 756 | } |
Fredrik Solenberg | 9a416bd | 2015-05-22 09:04:09 +0200 | [diff] [blame] | 757 | bool Init(rtc::Thread* worker_thread) { return true; } |
| 758 | void Terminate() {} |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 759 | webrtc::VoiceEngine* GetVoE() { return nullptr; } |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 760 | AudioOptions GetOptions() const { |
| 761 | return options_; |
| 762 | } |
| 763 | bool SetOptions(const AudioOptions& options) { |
| 764 | options_ = options; |
| 765 | options_changed_ = true; |
| 766 | return true; |
| 767 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 768 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 769 | VoiceMediaChannel* CreateChannel(webrtc::Call* call, |
| 770 | const AudioOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 771 | if (fail_create_channel_) { |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 772 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 775 | FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 776 | channels_.push_back(ch); |
| 777 | return ch; |
| 778 | } |
| 779 | FakeVoiceMediaChannel* GetChannel(size_t index) { |
| 780 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 781 | } |
| 782 | void UnregisterChannel(VoiceMediaChannel* channel) { |
| 783 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 784 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 785 | |
| 786 | const std::vector<AudioCodec>& codecs() { return codecs_; } |
| 787 | void SetCodecs(const std::vector<AudioCodec> codecs) { codecs_ = codecs; } |
| 788 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 789 | bool SetDevices(const Device* in_device, const Device* out_device) { |
| 790 | in_device_ = (in_device) ? in_device->name : ""; |
| 791 | out_device_ = (out_device) ? out_device->name : ""; |
| 792 | options_changed_ = true; |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | bool GetOutputVolume(int* level) { |
| 797 | *level = output_volume_; |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | bool SetOutputVolume(int level) { |
| 802 | output_volume_ = level; |
| 803 | options_changed_ = true; |
| 804 | return true; |
| 805 | } |
| 806 | |
| 807 | int GetInputLevel() { return 0; } |
| 808 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 809 | bool StartAecDump(rtc::PlatformFile file) { return false; } |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 810 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 811 | private: |
| 812 | std::vector<FakeVoiceMediaChannel*> channels_; |
| 813 | std::vector<AudioCodec> codecs_; |
| 814 | int output_volume_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 815 | std::string in_device_; |
| 816 | std::string out_device_; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 817 | AudioOptions options_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 818 | |
| 819 | friend class FakeMediaEngine; |
| 820 | }; |
| 821 | |
| 822 | class FakeVideoEngine : public FakeBaseEngine { |
| 823 | public: |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 824 | FakeVideoEngine() : capture_(false) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 825 | // Add a fake video codec. Note that the name must not be "" as there are |
| 826 | // sanity checks against that. |
| 827 | codecs_.push_back(VideoCodec(0, "fake_video_codec", 0, 0, 0, 0)); |
| 828 | } |
Fredrik Solenberg | 9a416bd | 2015-05-22 09:04:09 +0200 | [diff] [blame] | 829 | void Init() {} |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 830 | bool SetOptions(const VideoOptions& options) { |
| 831 | options_ = options; |
| 832 | options_changed_ = true; |
| 833 | return true; |
| 834 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 835 | bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) { |
| 836 | default_encoder_config_ = config; |
| 837 | return true; |
| 838 | } |
| 839 | const VideoEncoderConfig& default_encoder_config() const { |
| 840 | return default_encoder_config_; |
| 841 | } |
| 842 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 843 | VideoMediaChannel* CreateChannel(webrtc::Call* call, |
| 844 | const VideoOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 845 | if (fail_create_channel_) { |
| 846 | return NULL; |
| 847 | } |
| 848 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 849 | FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this, options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 850 | channels_.push_back(ch); |
| 851 | return ch; |
| 852 | } |
| 853 | FakeVideoMediaChannel* GetChannel(size_t index) { |
| 854 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 855 | } |
| 856 | void UnregisterChannel(VideoMediaChannel* channel) { |
| 857 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 858 | } |
| 859 | |
| 860 | const std::vector<VideoCodec>& codecs() const { return codecs_; } |
| 861 | bool FindCodec(const VideoCodec& in) { |
| 862 | for (size_t i = 0; i < codecs_.size(); ++i) { |
| 863 | if (codecs_[i].Matches(in)) { |
| 864 | return true; |
| 865 | } |
| 866 | } |
| 867 | return false; |
| 868 | } |
| 869 | void SetCodecs(const std::vector<VideoCodec> codecs) { codecs_ = codecs; } |
| 870 | |
| 871 | bool SetCaptureDevice(const Device* device) { |
| 872 | in_device_ = (device) ? device->name : ""; |
| 873 | options_changed_ = true; |
| 874 | return true; |
| 875 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 876 | bool SetCapture(bool capture) { |
| 877 | capture_ = capture; |
| 878 | return true; |
| 879 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 880 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 881 | private: |
| 882 | std::vector<FakeVideoMediaChannel*> channels_; |
| 883 | std::vector<VideoCodec> codecs_; |
| 884 | VideoEncoderConfig default_encoder_config_; |
| 885 | std::string in_device_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 886 | bool capture_; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 887 | VideoOptions options_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 888 | |
| 889 | friend class FakeMediaEngine; |
| 890 | }; |
| 891 | |
| 892 | class FakeMediaEngine : |
| 893 | public CompositeMediaEngine<FakeVoiceEngine, FakeVideoEngine> { |
| 894 | public: |
| 895 | FakeMediaEngine() { |
| 896 | voice_ = FakeVoiceEngine(); |
| 897 | video_ = FakeVideoEngine(); |
| 898 | } |
| 899 | virtual ~FakeMediaEngine() {} |
| 900 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 901 | void SetAudioCodecs(const std::vector<AudioCodec>& codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 902 | voice_.SetCodecs(codecs); |
| 903 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 904 | void SetVideoCodecs(const std::vector<VideoCodec>& codecs) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 905 | video_.SetCodecs(codecs); |
| 906 | } |
| 907 | |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 908 | void SetAudioRtpHeaderExtensions( |
| 909 | const std::vector<RtpHeaderExtension>& extensions) { |
| 910 | voice_.set_rtp_header_extensions(extensions); |
| 911 | } |
| 912 | void SetVideoRtpHeaderExtensions( |
| 913 | const std::vector<RtpHeaderExtension>& extensions) { |
| 914 | video_.set_rtp_header_extensions(extensions); |
| 915 | } |
| 916 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 917 | FakeVoiceMediaChannel* GetVoiceChannel(size_t index) { |
| 918 | return voice_.GetChannel(index); |
| 919 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 920 | FakeVideoMediaChannel* GetVideoChannel(size_t index) { |
| 921 | return video_.GetChannel(index); |
| 922 | } |
| 923 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 924 | AudioOptions audio_options() const { return voice_.options_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 925 | int output_volume() const { return voice_.output_volume_; } |
| 926 | const VideoEncoderConfig& default_video_encoder_config() const { |
| 927 | return video_.default_encoder_config_; |
| 928 | } |
| 929 | const std::string& audio_in_device() const { return voice_.in_device_; } |
| 930 | const std::string& audio_out_device() const { return voice_.out_device_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 931 | int voice_loglevel() const { return voice_.loglevel_; } |
| 932 | const std::string& voice_logfilter() const { return voice_.logfilter_; } |
| 933 | int video_loglevel() const { return video_.loglevel_; } |
| 934 | const std::string& video_logfilter() const { return video_.logfilter_; } |
| 935 | bool capture() const { return video_.capture_; } |
| 936 | bool options_changed() const { |
| 937 | return voice_.options_changed_ || video_.options_changed_; |
| 938 | } |
| 939 | void clear_options_changed() { |
| 940 | video_.options_changed_ = false; |
| 941 | voice_.options_changed_ = false; |
| 942 | } |
| 943 | void set_fail_create_channel(bool fail) { |
| 944 | voice_.set_fail_create_channel(fail); |
| 945 | video_.set_fail_create_channel(fail); |
| 946 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 947 | }; |
| 948 | |
| 949 | // CompositeMediaEngine with FakeVoiceEngine to expose SetAudioCodecs to |
| 950 | // establish a media connectionwith minimum set of audio codes required |
| 951 | template <class VIDEO> |
| 952 | class CompositeMediaEngineWithFakeVoiceEngine : |
| 953 | public CompositeMediaEngine<FakeVoiceEngine, VIDEO> { |
| 954 | public: |
| 955 | CompositeMediaEngineWithFakeVoiceEngine() {} |
| 956 | virtual ~CompositeMediaEngineWithFakeVoiceEngine() {} |
| 957 | |
| 958 | virtual void SetAudioCodecs(const std::vector<AudioCodec>& codecs) { |
| 959 | CompositeMediaEngine<FakeVoiceEngine, VIDEO>::voice_.SetCodecs(codecs); |
| 960 | } |
| 961 | }; |
| 962 | |
| 963 | // Have to come afterwards due to declaration order |
| 964 | inline FakeVoiceMediaChannel::~FakeVoiceMediaChannel() { |
| 965 | if (engine_) { |
| 966 | engine_->UnregisterChannel(this); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | inline FakeVideoMediaChannel::~FakeVideoMediaChannel() { |
| 971 | if (engine_) { |
| 972 | engine_->UnregisterChannel(this); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | class FakeDataEngine : public DataEngineInterface { |
| 977 | public: |
| 978 | FakeDataEngine() : last_channel_type_(DCT_NONE) {} |
| 979 | |
| 980 | virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type) { |
| 981 | last_channel_type_ = data_channel_type; |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 982 | FakeDataMediaChannel* ch = new FakeDataMediaChannel(this, DataOptions()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 983 | channels_.push_back(ch); |
| 984 | return ch; |
| 985 | } |
| 986 | |
| 987 | FakeDataMediaChannel* GetChannel(size_t index) { |
| 988 | return (channels_.size() > index) ? channels_[index] : NULL; |
| 989 | } |
| 990 | |
| 991 | void UnregisterChannel(DataMediaChannel* channel) { |
| 992 | channels_.erase(std::find(channels_.begin(), channels_.end(), channel)); |
| 993 | } |
| 994 | |
| 995 | virtual void SetDataCodecs(const std::vector<DataCodec>& data_codecs) { |
| 996 | data_codecs_ = data_codecs; |
| 997 | } |
| 998 | |
| 999 | virtual const std::vector<DataCodec>& data_codecs() { return data_codecs_; } |
| 1000 | |
| 1001 | DataChannelType last_channel_type() const { return last_channel_type_; } |
| 1002 | |
| 1003 | private: |
| 1004 | std::vector<FakeDataMediaChannel*> channels_; |
| 1005 | std::vector<DataCodec> data_codecs_; |
| 1006 | DataChannelType last_channel_type_; |
| 1007 | }; |
| 1008 | |
| 1009 | } // namespace cricket |
| 1010 | |
| 1011 | #endif // TALK_MEDIA_BASE_FAKEMEDIAENGINE_H_ |