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