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