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