OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_P2P_BASE_SESSION_H_ | 11 #ifndef WEBRTC_P2P_BASE_SESSION_H_ |
12 #define WEBRTC_P2P_BASE_SESSION_H_ | 12 #define WEBRTC_P2P_BASE_SESSION_H_ |
13 | 13 |
14 #include <list> | 14 #include <list> |
15 #include <map> | 15 #include <map> |
16 #include <string> | 16 #include <string> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "webrtc/p2p/base/candidate.h" | |
20 #include "webrtc/p2p/base/port.h" | |
21 #include "webrtc/p2p/base/transport.h" | |
22 #include "webrtc/base/refcount.h" | 19 #include "webrtc/base/refcount.h" |
23 #include "webrtc/base/rtccertificate.h" | 20 #include "webrtc/base/rtccertificate.h" |
24 #include "webrtc/base/scoped_ptr.h" | 21 #include "webrtc/base/scoped_ptr.h" |
25 #include "webrtc/base/scoped_ref_ptr.h" | 22 #include "webrtc/base/scoped_ref_ptr.h" |
26 #include "webrtc/base/socketaddress.h" | 23 #include "webrtc/base/socketaddress.h" |
| 24 #include "webrtc/p2p/base/candidate.h" |
| 25 #include "webrtc/p2p/base/port.h" |
| 26 #include "webrtc/p2p/base/transport.h" |
27 | 27 |
28 namespace cricket { | 28 namespace cricket { |
29 | 29 |
30 class BaseSession; | 30 class BaseSession; |
31 class P2PTransportChannel; | 31 class P2PTransportChannel; |
32 class Transport; | 32 class Transport; |
33 class TransportChannel; | 33 class TransportChannel; |
34 class TransportChannelProxy; | 34 class TransportChannelProxy; |
35 class TransportChannelImpl; | 35 class TransportChannelImpl; |
36 | 36 class TransportController; |
37 typedef rtc::RefCountedObject<rtc::scoped_ptr<Transport> > | |
38 TransportWrapper; | |
39 | |
40 // Bundles a Transport and ChannelMap together. ChannelMap is used to | |
41 // create transport channels before receiving or sending a session | |
42 // initiate, and for speculatively connecting channels. Previously, a | |
43 // session had one ChannelMap and transport. Now, with multiple | |
44 // transports per session, we need multiple ChannelMaps as well. | |
45 | |
46 typedef std::map<int, TransportChannelProxy*> ChannelMap; | |
47 | |
48 class TransportProxy : public sigslot::has_slots<> { | |
49 public: | |
50 TransportProxy( | |
51 rtc::Thread* worker_thread, | |
52 const std::string& sid, | |
53 const std::string& content_name, | |
54 TransportWrapper* transport) | |
55 : worker_thread_(worker_thread), | |
56 sid_(sid), | |
57 content_name_(content_name), | |
58 transport_(transport), | |
59 connecting_(false), | |
60 negotiated_(false), | |
61 sent_candidates_(false), | |
62 candidates_allocated_(false), | |
63 local_description_set_(false), | |
64 remote_description_set_(false) { | |
65 transport_->get()->SignalCandidatesReady.connect( | |
66 this, &TransportProxy::OnTransportCandidatesReady); | |
67 } | |
68 ~TransportProxy(); | |
69 | |
70 const std::string& content_name() const { return content_name_; } | |
71 // TODO(juberti): It's not good form to expose the object you're wrapping, | |
72 // since callers can mutate it. Can we make this return a const Transport*? | |
73 Transport* impl() const { return transport_->get(); } | |
74 | |
75 const std::string& type() const; | |
76 bool negotiated() const { return negotiated_; } | |
77 const Candidates& sent_candidates() const { return sent_candidates_; } | |
78 const Candidates& unsent_candidates() const { return unsent_candidates_; } | |
79 bool candidates_allocated() const { return candidates_allocated_; } | |
80 void set_candidates_allocated(bool allocated) { | |
81 candidates_allocated_ = allocated; | |
82 } | |
83 | |
84 TransportChannel* GetChannel(int component); | |
85 TransportChannel* CreateChannel(int component); | |
86 bool HasChannel(int component); | |
87 void DestroyChannel(int component); | |
88 | |
89 void AddSentCandidates(const Candidates& candidates); | |
90 void AddUnsentCandidates(const Candidates& candidates); | |
91 void ClearSentCandidates() { sent_candidates_.clear(); } | |
92 void ClearUnsentCandidates() { unsent_candidates_.clear(); } | |
93 | |
94 // Start the connection process for any channels, creating impls if needed. | |
95 void ConnectChannels(); | |
96 // Hook up impls to the proxy channels. Doesn't change connect state. | |
97 void CompleteNegotiation(); | |
98 | |
99 // Mux this proxy onto the specified proxy's transport. | |
100 bool SetupMux(TransportProxy* proxy); | |
101 | |
102 // Simple functions that thunk down to the same functions on Transport. | |
103 void SetIceRole(IceRole role); | |
104 void SetCertificate( | |
105 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); | |
106 bool SetLocalTransportDescription(const TransportDescription& description, | |
107 ContentAction action, | |
108 std::string* error_desc); | |
109 bool SetRemoteTransportDescription(const TransportDescription& description, | |
110 ContentAction action, | |
111 std::string* error_desc); | |
112 void OnSignalingReady(); | |
113 bool OnRemoteCandidates(const Candidates& candidates, std::string* error); | |
114 | |
115 // Called when a transport signals that it has new candidates. | |
116 void OnTransportCandidatesReady(cricket::Transport* transport, | |
117 const Candidates& candidates) { | |
118 SignalCandidatesReady(this, candidates); | |
119 } | |
120 | |
121 bool local_description_set() const { | |
122 return local_description_set_; | |
123 } | |
124 bool remote_description_set() const { | |
125 return remote_description_set_; | |
126 } | |
127 | |
128 // Handles sending of ready candidates and receiving of remote candidates. | |
129 sigslot::signal2<TransportProxy*, | |
130 const std::vector<Candidate>&> SignalCandidatesReady; | |
131 | |
132 private: | |
133 TransportChannelProxy* GetChannelProxy(int component) const; | |
134 | |
135 // Creates a new channel on the Transport which causes the reference | |
136 // count to increment. | |
137 void CreateChannelImpl(int component); | |
138 void CreateChannelImpl_w(int component); | |
139 | |
140 // Manipulators of transportchannelimpl in channel proxy. | |
141 void SetChannelImplFromTransport(TransportChannelProxy* proxy, int component); | |
142 void SetChannelImplFromTransport_w(TransportChannelProxy* proxy, | |
143 int component); | |
144 void ReplaceChannelImpl(TransportChannelProxy* proxy, | |
145 TransportChannelImpl* impl); | |
146 void ReplaceChannelImpl_w(TransportChannelProxy* proxy, | |
147 TransportChannelImpl* impl); | |
148 | |
149 rtc::Thread* const worker_thread_; | |
150 const std::string sid_; | |
151 const std::string content_name_; | |
152 rtc::scoped_refptr<TransportWrapper> transport_; | |
153 bool connecting_; | |
154 bool negotiated_; | |
155 ChannelMap channels_; | |
156 Candidates sent_candidates_; | |
157 Candidates unsent_candidates_; | |
158 bool candidates_allocated_; | |
159 bool local_description_set_; | |
160 bool remote_description_set_; | |
161 }; | |
162 | |
163 typedef std::map<std::string, TransportProxy*> TransportMap; | |
164 | 37 |
165 // Statistics for all the transports of this session. | 38 // Statistics for all the transports of this session. |
166 typedef std::map<std::string, TransportStats> TransportStatsMap; | 39 typedef std::map<std::string, TransportStats> TransportStatsMap; |
167 typedef std::map<std::string, std::string> ProxyTransportMap; | 40 typedef std::map<std::string, std::string> ProxyTransportMap; |
168 | 41 |
169 // TODO(pthatcher): Think of a better name for this. We already have | 42 // TODO(pthatcher): Think of a better name for this. We already have |
170 // a TransportStats in transport.h. Perhaps TransportsStats? | 43 // a TransportStats in transport.h. Perhaps TransportsStats? |
171 struct SessionStats { | 44 struct SessionStats { |
172 ProxyTransportMap proxy_to_transport; | 45 ProxyTransportMap proxy_to_transport; |
173 TransportStatsMap transport_stats; | 46 TransportStatsMap transport_stats; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 ERROR_TRANSPORT = 5, // transport error of some kind | 90 ERROR_TRANSPORT = 5, // transport error of some kind |
218 }; | 91 }; |
219 | 92 |
220 // Convert State to a readable string. | 93 // Convert State to a readable string. |
221 static std::string StateToString(State state); | 94 static std::string StateToString(State state); |
222 | 95 |
223 BaseSession(rtc::Thread* signaling_thread, | 96 BaseSession(rtc::Thread* signaling_thread, |
224 rtc::Thread* worker_thread, | 97 rtc::Thread* worker_thread, |
225 PortAllocator* port_allocator, | 98 PortAllocator* port_allocator, |
226 const std::string& sid, | 99 const std::string& sid, |
227 const std::string& content_type, | |
228 bool initiator); | 100 bool initiator); |
229 virtual ~BaseSession(); | 101 virtual ~BaseSession(); |
230 | 102 |
231 // These are const to allow them to be called from const methods. | 103 // These are const to allow them to be called from const methods. |
232 rtc::Thread* signaling_thread() const { return signaling_thread_; } | 104 rtc::Thread* signaling_thread() const { return signaling_thread_; } |
233 rtc::Thread* worker_thread() const { return worker_thread_; } | |
234 PortAllocator* port_allocator() const { return port_allocator_; } | 105 PortAllocator* port_allocator() const { return port_allocator_; } |
235 | 106 |
236 // The ID of this session. | 107 // The ID of this session. |
237 const std::string& id() const { return sid_; } | 108 const std::string& id() const { return sid_; } |
238 | 109 |
239 // TODO(juberti): This data is largely redundant, as it can now be obtained | |
240 // from local/remote_description(). Remove these functions and members. | |
241 // Returns the XML namespace identifying the type of this session. | |
242 const std::string& content_type() const { return content_type_; } | |
243 | |
244 // Indicates whether we initiated this session. | |
245 bool initiator() const { return initiator_; } | |
246 | |
247 // Returns the application-level description given by our client. | 110 // Returns the application-level description given by our client. |
248 // If we are the recipient, this will be NULL until we send an accept. | 111 // If we are the recipient, this will be NULL until we send an accept. |
249 const SessionDescription* local_description() const; | 112 const SessionDescription* local_description() const; |
250 | 113 |
251 // Returns the application-level description given by the other client. | 114 // Returns the application-level description given by the other client. |
252 // If we are the initiator, this will be NULL until we receive an accept. | 115 // If we are the initiator, this will be NULL until we receive an accept. |
253 const SessionDescription* remote_description() const; | 116 const SessionDescription* remote_description() const; |
254 | 117 |
255 SessionDescription* remote_description(); | 118 SessionDescription* remote_description(); |
256 | 119 |
257 // Takes ownership of SessionDescription* | 120 // Takes ownership of SessionDescription* |
258 void set_local_description(const SessionDescription* sdesc); | 121 void set_local_description(const SessionDescription* sdesc); |
259 | 122 |
260 // Takes ownership of SessionDescription* | 123 // Takes ownership of SessionDescription* |
261 void set_remote_description(SessionDescription* sdesc); | 124 void set_remote_description(SessionDescription* sdesc); |
262 | 125 |
| 126 void set_initiator(bool initiator); |
| 127 bool initiator() const { return initiator_; } |
| 128 |
263 const SessionDescription* initiator_description() const; | 129 const SessionDescription* initiator_description() const; |
264 | 130 |
265 // Returns the current state of the session. See the enum above for details. | 131 // Returns the current state of the session. See the enum above for details. |
266 // Each time the state changes, we will fire this signal. | 132 // Each time the state changes, we will fire this signal. |
267 State state() const { return state_; } | 133 State state() const { return state_; } |
268 sigslot::signal2<BaseSession* , State> SignalState; | 134 sigslot::signal2<BaseSession* , State> SignalState; |
269 | 135 |
270 // Returns the last error in the session. See the enum above for details. | 136 // Returns the last error in the session. See the enum above for details. |
271 // Each time the an error occurs, we will fire this signal. | 137 // Each time the an error occurs, we will fire this signal. |
272 Error error() const { return error_; } | 138 Error error() const { return error_; } |
273 const std::string& error_desc() const { return error_desc_; } | 139 const std::string& error_desc() const { return error_desc_; } |
274 sigslot::signal2<BaseSession* , Error> SignalError; | 140 sigslot::signal2<BaseSession* , Error> SignalError; |
275 | 141 |
276 // Updates the state, signaling if necessary. | 142 // Updates the state, signaling if necessary. |
277 virtual void SetState(State state); | 143 virtual void SetState(State state); |
278 | 144 |
279 // Updates the error state, signaling if necessary. | 145 // Updates the error state, signaling if necessary. |
280 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|. | 146 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|. |
281 virtual void SetError(Error error, const std::string& error_desc); | 147 virtual void SetError(Error error, const std::string& error_desc); |
282 | 148 |
283 // Fired when the remote description is updated, with the updated | |
284 // contents. | |
285 sigslot::signal2<BaseSession* , const ContentInfos&> | |
286 SignalRemoteDescriptionUpdate; | |
287 | |
288 // Fired when SetState is called (regardless if there's a state change), which | |
289 // indicates the session description might have be updated. | |
290 sigslot::signal2<BaseSession*, ContentAction> SignalNewLocalDescription; | |
291 | |
292 // Fired when SetState is called (regardless if there's a state change), which | |
293 // indicates the session description might have be updated. | |
294 sigslot::signal2<BaseSession*, ContentAction> SignalNewRemoteDescription; | |
295 | |
296 // Returns the transport that has been negotiated or NULL if | |
297 // negotiation is still in progress. | |
298 virtual Transport* GetTransport(const std::string& content_name); | |
299 | |
300 // Creates a new channel with the given names. This method may be called | |
301 // immediately after creating the session. However, the actual | |
302 // implementation may not be fixed until transport negotiation completes. | |
303 // This will usually be called from the worker thread, but that | |
304 // shouldn't be an issue since the main thread will be blocked in | |
305 // Send when doing so. | |
306 virtual TransportChannel* CreateChannel(const std::string& content_name, | |
307 int component); | |
308 | |
309 // Returns the channel with the given names. | |
310 virtual TransportChannel* GetChannel(const std::string& content_name, | |
311 int component); | |
312 | |
313 // Destroys the channel with the given names. | |
314 // This will usually be called from the worker thread, but that | |
315 // shouldn't be an issue since the main thread will be blocked in | |
316 // Send when doing so. | |
317 virtual void DestroyChannel(const std::string& content_name, | |
318 int component); | |
319 | |
320 // Set the ice connection receiving timeout. | |
321 void SetIceConnectionReceivingTimeout(int timeout_ms); | 149 void SetIceConnectionReceivingTimeout(int timeout_ms); |
322 | 150 |
323 // For testing. | |
324 const rtc::scoped_refptr<rtc::RTCCertificate>& | |
325 certificate_for_testing() const { | |
326 return certificate_; | |
327 } | |
328 | |
329 protected: | 151 protected: |
330 // Specifies the identity to use in this session. | |
331 bool SetCertificate( | |
332 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); | |
333 | |
334 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); | |
335 | |
336 bool PushdownTransportDescription(ContentSource source, | 152 bool PushdownTransportDescription(ContentSource source, |
337 ContentAction action, | 153 ContentAction action, |
338 std::string* error_desc); | 154 std::string* error_desc); |
339 void set_initiator(bool initiator) { initiator_ = initiator; } | |
340 | |
341 const TransportMap& transport_proxies() const { return transports_; } | |
342 // Get a TransportProxy by content_name or transport. NULL if not found. | |
343 TransportProxy* GetTransportProxy(const std::string& content_name); | |
344 void DestroyTransportProxy(const std::string& content_name); | |
345 // TransportProxy is owned by session. Return proxy just for convenience. | |
346 TransportProxy* GetOrCreateTransportProxy(const std::string& content_name); | |
347 // Creates the actual transport object. Overridable for testing. | |
348 virtual Transport* CreateTransport(const std::string& content_name); | |
349 | |
350 void OnSignalingReady(); | |
351 void SpeculativelyConnectAllTransportChannels(); | |
352 // Helper method to provide remote candidates to the transport. | |
353 bool OnRemoteCandidates(const std::string& content_name, | |
354 const Candidates& candidates, | |
355 std::string* error); | |
356 | |
357 // This method will mux transport channels by content_name. | |
358 // First content is used for muxing. | |
359 bool MaybeEnableMuxingSupport(); | |
360 | |
361 // Called when a transport requests signaling. | |
362 virtual void OnTransportRequestSignaling(Transport* transport) { | |
363 } | |
364 | |
365 // Called when the first channel of a transport begins connecting. We use | |
366 // this to start a timer, to make sure that the connection completes in a | |
367 // reasonable amount of time. | |
368 virtual void OnTransportConnecting(Transport* transport) { | |
369 } | |
370 | |
371 // Called when a transport changes its writable state. We track this to make | |
372 // sure that the transport becomes writable within a reasonable amount of | |
373 // time. If this does not occur, we signal an error. | |
374 virtual void OnTransportWritable(Transport* transport) { | |
375 } | |
376 virtual void OnTransportReadable(Transport* transport) { | |
377 } | |
378 | |
379 virtual void OnTransportReceiving(Transport* transport) { | |
380 } | |
381 | |
382 // Called when a transport has found its steady-state connections. | |
383 virtual void OnTransportCompleted(Transport* transport) { | |
384 } | |
385 | |
386 // Called when a transport has failed permanently. | |
387 virtual void OnTransportFailed(Transport* transport) { | |
388 } | |
389 | |
390 // Called when a transport signals that it has new candidates. | |
391 virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy, | |
392 const Candidates& candidates) { | |
393 } | |
394 | |
395 virtual void OnTransportRouteChange( | |
396 Transport* transport, | |
397 int component, | |
398 const cricket::Candidate& remote_candidate) { | |
399 } | |
400 | |
401 virtual void OnTransportCandidatesAllocationDone(Transport* transport); | |
402 | |
403 // Called when all transport channels allocated required candidates. | |
404 // This method should be used as an indication of candidates gathering process | |
405 // is completed and application can now send local candidates list to remote. | |
406 virtual void OnCandidatesAllocationDone() { | |
407 } | |
408 | |
409 // Handles the ice role change callback from Transport. This must be | |
410 // propagated to all the transports. | |
411 virtual void OnRoleConflict(); | |
412 | 155 |
413 // Handles messages posted to us. | 156 // Handles messages posted to us. |
414 virtual void OnMessage(rtc::Message *pmsg); | 157 virtual void OnMessage(rtc::Message *pmsg); |
415 | 158 |
| 159 TransportController* transport_controller() { |
| 160 return transport_controller_.get(); |
| 161 } |
| 162 |
416 protected: | 163 protected: |
417 bool IsCandidateAllocationDone() const; | |
418 | |
419 State state_; | 164 State state_; |
420 Error error_; | 165 Error error_; |
421 std::string error_desc_; | 166 std::string error_desc_; |
422 | 167 |
423 // This method will delete the Transport and TransportChannelImpls | |
424 // and replace those with the Transport object of the first | |
425 // MediaContent in bundle_group. | |
426 bool BundleContentGroup(const ContentGroup* bundle_group); | |
427 | |
428 private: | 168 private: |
429 // Helper methods to push local and remote transport descriptions. | 169 // Helper methods to push local and remote transport descriptions. |
430 bool PushdownLocalTransportDescription( | 170 bool PushdownLocalTransportDescription( |
431 const SessionDescription* sdesc, ContentAction action, | 171 const SessionDescription* sdesc, ContentAction action, |
432 std::string* error_desc); | 172 std::string* error_desc); |
433 bool PushdownRemoteTransportDescription( | 173 bool PushdownRemoteTransportDescription( |
434 const SessionDescription* sdesc, ContentAction action, | 174 const SessionDescription* sdesc, ContentAction action, |
435 std::string* error_desc); | 175 std::string* error_desc); |
436 | 176 |
437 void MaybeCandidateAllocationDone(); | |
438 | |
439 // Log session state. | 177 // Log session state. |
440 void LogState(State old_state, State new_state); | 178 void LogState(State old_state, State new_state); |
441 | 179 |
442 // Returns true and the TransportInfo of the given |content_name| | 180 // Returns true and the TransportInfo of the given |content_name| |
443 // from |description|. Returns false if it's not available. | 181 // from |description|. Returns false if it's not available. |
444 static bool GetTransportDescription(const SessionDescription* description, | 182 static bool GetTransportDescription(const SessionDescription* description, |
445 const std::string& content_name, | 183 const std::string& content_name, |
446 TransportDescription* info); | 184 TransportDescription* info); |
447 | 185 |
448 rtc::Thread* const signaling_thread_; | 186 rtc::Thread* const signaling_thread_; |
449 rtc::Thread* const worker_thread_; | |
450 PortAllocator* const port_allocator_; | 187 PortAllocator* const port_allocator_; |
451 const std::string sid_; | 188 const std::string sid_; |
452 const std::string content_type_; | |
453 bool initiator_; | 189 bool initiator_; |
454 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 190 rtc::scoped_ptr<TransportController> transport_controller_; |
455 rtc::SSLProtocolVersion ssl_max_version_; | |
456 rtc::scoped_ptr<const SessionDescription> local_description_; | 191 rtc::scoped_ptr<const SessionDescription> local_description_; |
457 rtc::scoped_ptr<SessionDescription> remote_description_; | 192 rtc::scoped_ptr<SessionDescription> remote_description_; |
458 uint64 ice_tiebreaker_; | |
459 // This flag will be set to true after the first role switch. This flag | |
460 // will enable us to stop any role switch during the call. | |
461 bool role_switch_; | |
462 TransportMap transports_; | |
463 | |
464 // Timeout value in milliseconds for which no ICE connection receives | |
465 // any packets. | |
466 int ice_receiving_timeout_; | |
467 }; | 193 }; |
468 | 194 |
469 } // namespace cricket | 195 } // namespace cricket |
470 | 196 |
471 #endif // WEBRTC_P2P_BASE_SESSION_H_ | 197 #endif // WEBRTC_P2P_BASE_SESSION_H_ |
OLD | NEW |