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