OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2017 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_P2P_BASE_FAKEICETRANSPORT_H_ | |
12 #define WEBRTC_P2P_BASE_FAKEICETRANSPORT_H_ | |
13 | |
14 #include <string> | |
15 | |
16 #include "webrtc/base/asyncinvoker.h" | |
17 #include "webrtc/base/copyonwritebuffer.h" | |
18 #include "webrtc/p2p/base/icetransportinternal.h" | |
19 | |
20 namespace cricket { | |
21 | |
22 class FakeIceTransport : public IceTransportInternal { | |
23 public: | |
24 explicit FakeIceTransport(const std::string& name, int component) | |
25 : name_(name), component_(component) {} | |
26 ~FakeIceTransport() override { | |
27 if (dest_ && dest_->dest_ == this) { | |
28 dest_->dest_ = nullptr; | |
29 } | |
30 } | |
31 | |
32 // If async, will send packets by "Post"-ing to message queue instead of | |
33 // synchronously "Send"-ing. | |
34 void SetAsync(bool async) { async_ = async; } | |
35 void SetAsyncDelay(int delay_ms) { async_delay_ms_ = delay_ms; } | |
36 | |
37 // SetWritable, SetReceiving and SetDestination are the main methods that can | |
38 // be used for testing, to simulate connectivity or lack thereof. | |
39 void SetWritable(bool writable) { set_writable(writable); } | |
40 void SetReceiving(bool receiving) { set_receiving(receiving); } | |
41 | |
42 // Simulates the two transports connecting to each other. | |
43 // If |asymmetric| is true this method only affects this FakeIceTransport. | |
44 // If false, it affects |dest| as well. | |
45 void SetDestination(FakeIceTransport* dest, bool asymmetric = false) { | |
46 if (dest == dest_) { | |
47 return; | |
48 } | |
49 RTC_DCHECK(!dest || !dest_) | |
50 << "Changing fake destination from one to another is not supported."; | |
51 if (dest) { | |
52 // This simulates the delivery of candidates. | |
53 dest_ = dest; | |
54 set_writable(true); | |
55 if (!asymmetric) { | |
56 dest->SetDestination(this, true); | |
57 } | |
58 } else { | |
59 // Simulates loss of connectivity, by asymmetrically forgetting dest_. | |
60 dest_ = nullptr; | |
61 set_writable(false); | |
62 } | |
63 } | |
64 | |
65 void SetConnectionCount(size_t connection_count) { | |
66 size_t old_connection_count = connection_count_; | |
67 connection_count_ = connection_count; | |
68 if (connection_count) { | |
69 had_connection_ = true; | |
70 } | |
71 // In this fake transport channel, |connection_count_| determines the | |
72 // transport state. | |
73 if (connection_count_ < old_connection_count) { | |
74 SignalStateChanged(this); | |
75 } | |
76 } | |
77 | |
78 void SetCandidatesGatheringComplete() { | |
79 if (gathering_state_ != kIceGatheringComplete) { | |
80 gathering_state_ = kIceGatheringComplete; | |
81 SignalGatheringState(this); | |
82 } | |
83 } | |
84 | |
85 // Convenience functions for accessing ICE config and other things. | |
86 int receiving_timeout() const { return ice_config_.receiving_timeout; } | |
87 bool gather_continually() const { return ice_config_.gather_continually(); } | |
88 const Candidates& remote_candidates() const { return remote_candidates_; } | |
89 | |
90 // Fake IceTransportInternal implementation. | |
91 const std::string& transport_name() const override { return name_; } | |
92 int component() const override { return component_; } | |
93 uint64_t IceTiebreaker() const { return tiebreaker_; } | |
94 IceMode remote_ice_mode() const { return remote_ice_mode_; } | |
95 const std::string& ice_ufrag() const { return ice_ufrag_; } | |
96 const std::string& ice_pwd() const { return ice_pwd_; } | |
97 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } | |
98 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } | |
99 | |
100 IceTransportState GetState() const override { | |
101 if (connection_count_ == 0) { | |
102 return had_connection_ ? IceTransportState::STATE_FAILED | |
103 : IceTransportState::STATE_INIT; | |
104 } | |
105 | |
106 if (connection_count_ == 1) { | |
107 return IceTransportState::STATE_COMPLETED; | |
108 } | |
109 | |
110 return IceTransportState::STATE_CONNECTING; | |
111 } | |
112 | |
113 void SetIceRole(IceRole role) override { role_ = role; } | |
114 IceRole GetIceRole() const override { return role_; } | |
115 void SetIceTiebreaker(uint64_t tiebreaker) override { | |
116 tiebreaker_ = tiebreaker; | |
117 } | |
118 void SetIceParameters(const IceParameters& ice_params) override { | |
119 ice_ufrag_ = ice_params.ufrag; | |
120 ice_pwd_ = ice_params.pwd; | |
121 } | |
122 void SetRemoteIceParameters(const IceParameters& params) override { | |
123 remote_ice_ufrag_ = params.ufrag; | |
124 remote_ice_pwd_ = params.pwd; | |
125 } | |
126 | |
127 void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; } | |
128 | |
129 void MaybeStartGathering() override { | |
130 if (gathering_state_ == kIceGatheringNew) { | |
131 gathering_state_ = kIceGatheringGathering; | |
132 SignalGatheringState(this); | |
133 } | |
134 } | |
135 | |
136 IceGatheringState gathering_state() const override { | |
137 return gathering_state_; | |
138 } | |
139 | |
140 void SetIceConfig(const IceConfig& config) override { ice_config_ = config; } | |
141 | |
142 void AddRemoteCandidate(const Candidate& candidate) override { | |
143 remote_candidates_.push_back(candidate); | |
144 } | |
145 void RemoveRemoteCandidate(const Candidate& candidate) override {} | |
146 | |
147 bool GetStats(ConnectionInfos* infos) override { | |
148 ConnectionInfo info; | |
149 infos->clear(); | |
150 infos->push_back(info); | |
151 return true; | |
152 } | |
153 | |
154 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) override { | |
155 } | |
156 | |
157 // Fake PacketTransportInterface implementation. | |
158 bool writable() const override { return writable_; } | |
159 bool receiving() const override { return receiving_; } | |
160 int SendPacket(const char* data, | |
161 size_t len, | |
162 const rtc::PacketOptions& options, | |
163 int flags) override { | |
164 if (!dest_) { | |
165 return -1; | |
166 } | |
167 rtc::CopyOnWriteBuffer packet(data, len); | |
168 if (async_) { | |
169 invoker_.AsyncInvokeDelayed<void>( | |
170 RTC_FROM_HERE, rtc::Thread::Current(), | |
171 rtc::Bind(&FakeIceTransport::SendPacketInternal, this, packet), | |
172 async_delay_ms_); | |
173 } else { | |
174 SendPacketInternal(packet); | |
175 } | |
176 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis()); | |
177 SignalSentPacket(this, sent_packet); | |
178 return static_cast<int>(len); | |
179 } | |
180 int SetOption(rtc::Socket::Option opt, int value) override { return true; } | |
181 bool GetOption(rtc::Socket::Option opt, int* value) override { return true; } | |
182 int GetError() override { return 0; } | |
183 | |
184 private: | |
185 void set_writable(bool writable) { | |
186 if (writable_ == writable) { | |
187 return; | |
188 } | |
189 LOG(INFO) << "set_writable from:" << writable_ << " to " << writable; | |
190 writable_ = writable; | |
191 if (writable_) { | |
192 SignalReadyToSend(this); | |
193 } | |
194 SignalWritableState(this); | |
195 } | |
196 | |
197 void set_receiving(bool receiving) { | |
198 if (receiving_ == receiving) { | |
199 return; | |
200 } | |
201 receiving_ = receiving; | |
202 SignalReceivingState(this); | |
203 } | |
204 | |
205 void SendPacketInternal(const rtc::CopyOnWriteBuffer& packet) { | |
206 if (dest_) { | |
207 dest_->SignalReadPacket(dest_, packet.data<char>(), packet.size(), | |
208 rtc::CreatePacketTime(0), 0); | |
209 } | |
210 } | |
211 | |
212 rtc::AsyncInvoker invoker_; | |
213 std::string name_; | |
214 int component_; | |
215 FakeIceTransport* dest_ = nullptr; | |
216 bool async_ = false; | |
217 int async_delay_ms_ = 0; | |
218 Candidates remote_candidates_; | |
219 IceConfig ice_config_; | |
220 IceRole role_ = ICEROLE_UNKNOWN; | |
221 uint64_t tiebreaker_ = 0; | |
222 std::string ice_ufrag_; | |
223 std::string ice_pwd_; | |
224 std::string remote_ice_ufrag_; | |
225 std::string remote_ice_pwd_; | |
226 IceMode remote_ice_mode_ = ICEMODE_FULL; | |
227 size_t connection_count_ = 0; | |
228 IceGatheringState gathering_state_ = kIceGatheringNew; | |
229 bool had_connection_ = false; | |
230 bool writable_ = false; | |
231 bool receiving_ = false; | |
232 }; | |
233 | |
234 } // namespace cricket | |
235 | |
236 #endif // WEBRTC_P2P_BASE_FAKEICETRANSPORT_H_ | |
Taylor Brandstetter
2017/01/24 02:00:05
Moved from faketransportcontroller.h, switched to
| |
OLD | NEW |