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