1 /* 2 * libjingle 3 * Copyright 2013, 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 #include "talk/app/webrtc/datachannel.h" 29 30 class FakeDataChannelProvider : public webrtc::DataChannelProviderInterface { 31 public: 32 FakeDataChannelProvider() 33 : send_blocked_(false), 34 transport_available_(false), 35 ready_to_send_(false), 36 transport_error_(false) {} 37 virtual ~FakeDataChannelProvider() {} 38 39 virtual bool SendData(const cricket::SendDataParams& params, 40 const rtc::Buffer& payload, 41 cricket::SendDataResult* result) OVERRIDE { 42 ASSERT(ready_to_send_ && transport_available_); 43 if (send_blocked_) { 44 *result = cricket::SDR_BLOCK; 45 return false; 46 } 47 48 if (transport_error_ || payload.length() == 0) { 49 *result = cricket::SDR_ERROR; 50 return false; 51 } 52 53 last_send_data_params_ = params; 54 return true; 55 } 56 57 virtual bool ConnectDataChannel(webrtc::DataChannel* data_channel) OVERRIDE { 58 ASSERT(connected_channels_.find(data_channel) == connected_channels_.end()); 59 if (!transport_available_) { 60 return false; 61 } 62 LOG(LS_INFO) << "DataChannel connected " << data_channel; 63 connected_channels_.insert(data_channel); 64 return true; 65 } 66 67 virtual void DisconnectDataChannel( 68 webrtc::DataChannel* data_channel) OVERRIDE { 69 ASSERT(connected_channels_.find(data_channel) != connected_channels_.end()); 70 LOG(LS_INFO) << "DataChannel disconnected " << data_channel; 71 connected_channels_.erase(data_channel); 72 } 73 74 virtual void AddSctpDataStream(uint32 sid) OVERRIDE { 75 if (!transport_available_) { 76 return; 77 } 78 send_ssrcs_.insert(sid); 79 recv_ssrcs_.insert(sid); 80 } 81 82 virtual void RemoveSctpDataStream(uint32 sid) OVERRIDE { 83 send_ssrcs_.erase(sid); 84 recv_ssrcs_.erase(sid); 85 } 86 87 virtual bool ReadyToSendData() const OVERRIDE { 88 return ready_to_send_; 89 } 90 91 // Set true to emulate the SCTP stream being blocked by congestion control. 92 void set_send_blocked(bool blocked) { 93 send_blocked_ = blocked; 94 if (!blocked) { 95 std::set<webrtc::DataChannel*>::iterator it; 96 for (it = connected_channels_.begin(); 97 it != connected_channels_.end(); 98 ++it) { 99 (*it)->OnChannelReady(true); 100 } 101 } 102 } 103 104 // Set true to emulate the transport channel creation, e.g. after 105 // setLocalDescription/setRemoteDescription called with data content. 106 void set_transport_available(bool available) { 107 transport_available_ = available; 108 } 109 110 // Set true to emulate the transport ReadyToSendData signal when the transport 111 // becomes writable for the first time. 112 void set_ready_to_send(bool ready) { 113 ASSERT(transport_available_); 114 ready_to_send_ = ready; 115 if (ready) { 116 std::set<webrtc::DataChannel*>::iterator it; 117 for (it = connected_channels_.begin(); 118 it != connected_channels_.end(); 119 ++it) { 120 (*it)->OnChannelReady(true); 121 } 122 } 123 } 124 125 void set_transport_error() { 126 transport_error_ = true; 127 } 128 129 cricket::SendDataParams last_send_data_params() const { 130 return last_send_data_params_; 131 } 132 133 bool IsConnected(webrtc::DataChannel* data_channel) const { 134 return connected_channels_.find(data_channel) != connected_channels_.end(); 135 } 136 137 bool IsSendStreamAdded(uint32 stream) const { 138 return send_ssrcs_.find(stream) != send_ssrcs_.end(); 139 } 140 141 bool IsRecvStreamAdded(uint32 stream) const { 142 return recv_ssrcs_.find(stream) != recv_ssrcs_.end(); 143 } 144 145 private: 146 cricket::SendDataParams last_send_data_params_; 147 bool send_blocked_; 148 bool transport_available_; 149 bool ready_to_send_; 150 bool transport_error_; 151 std::set<webrtc::DataChannel*> connected_channels_; 152 std::set<uint32> send_ssrcs_; 153 std::set<uint32> recv_ssrcs_; 154 }; 155