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" |
19 #include "webrtc/base/refcount.h" | 22 #include "webrtc/base/refcount.h" |
20 #include "webrtc/base/rtccertificate.h" | 23 #include "webrtc/base/rtccertificate.h" |
21 #include "webrtc/base/scoped_ptr.h" | 24 #include "webrtc/base/scoped_ptr.h" |
22 #include "webrtc/base/scoped_ref_ptr.h" | 25 #include "webrtc/base/scoped_ref_ptr.h" |
23 #include "webrtc/base/socketaddress.h" | 26 #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 TransportChannelImpl; | 35 class TransportChannelImpl; |
35 class TransportController; | 36 |
| 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; |
36 | 164 |
37 // Statistics for all the transports of this session. | 165 // Statistics for all the transports of this session. |
38 typedef std::map<std::string, TransportStats> TransportStatsMap; | 166 typedef std::map<std::string, TransportStats> TransportStatsMap; |
39 typedef std::map<std::string, std::string> ProxyTransportMap; | 167 typedef std::map<std::string, std::string> ProxyTransportMap; |
40 | 168 |
41 // TODO(pthatcher): Think of a better name for this. We already have | 169 // TODO(pthatcher): Think of a better name for this. We already have |
42 // a TransportStats in transport.h. Perhaps TransportsStats? | 170 // a TransportStats in transport.h. Perhaps TransportsStats? |
43 struct SessionStats { | 171 struct SessionStats { |
44 ProxyTransportMap proxy_to_transport; | 172 ProxyTransportMap proxy_to_transport; |
45 TransportStatsMap transport_stats; | 173 TransportStatsMap transport_stats; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 ERROR_TRANSPORT = 5, // transport error of some kind | 217 ERROR_TRANSPORT = 5, // transport error of some kind |
90 }; | 218 }; |
91 | 219 |
92 // Convert State to a readable string. | 220 // Convert State to a readable string. |
93 static std::string StateToString(State state); | 221 static std::string StateToString(State state); |
94 | 222 |
95 BaseSession(rtc::Thread* signaling_thread, | 223 BaseSession(rtc::Thread* signaling_thread, |
96 rtc::Thread* worker_thread, | 224 rtc::Thread* worker_thread, |
97 PortAllocator* port_allocator, | 225 PortAllocator* port_allocator, |
98 const std::string& sid, | 226 const std::string& sid, |
| 227 const std::string& content_type, |
99 bool initiator); | 228 bool initiator); |
100 virtual ~BaseSession(); | 229 virtual ~BaseSession(); |
101 | 230 |
102 // These are const to allow them to be called from const methods. | 231 // These are const to allow them to be called from const methods. |
103 rtc::Thread* signaling_thread() const { return signaling_thread_; } | 232 rtc::Thread* signaling_thread() const { return signaling_thread_; } |
104 rtc::Thread* worker_thread() const { return worker_thread_; } | 233 rtc::Thread* worker_thread() const { return worker_thread_; } |
105 PortAllocator* port_allocator() const { return port_allocator_; } | 234 PortAllocator* port_allocator() const { return port_allocator_; } |
106 | 235 |
107 // The ID of this session. | 236 // The ID of this session. |
108 const std::string& id() const { return sid_; } | 237 const std::string& id() const { return sid_; } |
109 | 238 |
| 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 |
110 // Returns the application-level description given by our client. | 247 // Returns the application-level description given by our client. |
111 // If we are the recipient, this will be NULL until we send an accept. | 248 // If we are the recipient, this will be NULL until we send an accept. |
112 const SessionDescription* local_description() const; | 249 const SessionDescription* local_description() const; |
113 | 250 |
114 // Returns the application-level description given by the other client. | 251 // Returns the application-level description given by the other client. |
115 // If we are the initiator, this will be NULL until we receive an accept. | 252 // If we are the initiator, this will be NULL until we receive an accept. |
116 const SessionDescription* remote_description() const; | 253 const SessionDescription* remote_description() const; |
117 | 254 |
118 SessionDescription* remote_description(); | 255 SessionDescription* remote_description(); |
119 | 256 |
120 // Takes ownership of SessionDescription* | 257 // Takes ownership of SessionDescription* |
121 void set_local_description(const SessionDescription* sdesc); | 258 void set_local_description(const SessionDescription* sdesc); |
122 | 259 |
123 // Takes ownership of SessionDescription* | 260 // Takes ownership of SessionDescription* |
124 void set_remote_description(SessionDescription* sdesc); | 261 void set_remote_description(SessionDescription* sdesc); |
125 | 262 |
126 void set_initiator(bool initiator); | |
127 bool initiator() const { return initiator_; } | |
128 | |
129 const SessionDescription* initiator_description() const; | 263 const SessionDescription* initiator_description() const; |
130 | 264 |
131 // Returns the current state of the session. See the enum above for details. | 265 // Returns the current state of the session. See the enum above for details. |
132 // Each time the state changes, we will fire this signal. | 266 // Each time the state changes, we will fire this signal. |
133 State state() const { return state_; } | 267 State state() const { return state_; } |
134 sigslot::signal2<BaseSession* , State> SignalState; | 268 sigslot::signal2<BaseSession* , State> SignalState; |
135 | 269 |
136 // Returns the last error in the session. See the enum above for details. | 270 // Returns the last error in the session. See the enum above for details. |
137 // Each time the an error occurs, we will fire this signal. | 271 // Each time the an error occurs, we will fire this signal. |
138 Error error() const { return error_; } | 272 Error error() const { return error_; } |
139 const std::string& error_desc() const { return error_desc_; } | 273 const std::string& error_desc() const { return error_desc_; } |
140 sigslot::signal2<BaseSession* , Error> SignalError; | 274 sigslot::signal2<BaseSession* , Error> SignalError; |
141 | 275 |
142 // Updates the state, signaling if necessary. | 276 // Updates the state, signaling if necessary. |
143 virtual void SetState(State state); | 277 virtual void SetState(State state); |
144 | 278 |
145 // Updates the error state, signaling if necessary. | 279 // Updates the error state, signaling if necessary. |
146 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|. | 280 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|. |
147 virtual void SetError(Error error, const std::string& error_desc); | 281 virtual void SetError(Error error, const std::string& error_desc); |
148 | 282 |
| 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. |
149 void SetIceConnectionReceivingTimeout(int timeout_ms); | 321 void SetIceConnectionReceivingTimeout(int timeout_ms); |
150 | 322 |
151 // Start gathering candidates for any new transports, or transports doing an | 323 // For testing. |
152 // ICE restart. | 324 const rtc::scoped_refptr<rtc::RTCCertificate>& |
153 void MaybeStartGathering(); | 325 certificate_for_testing() const { |
| 326 return certificate_; |
| 327 } |
154 | 328 |
155 protected: | 329 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 |
156 bool PushdownTransportDescription(ContentSource source, | 336 bool PushdownTransportDescription(ContentSource source, |
157 ContentAction action, | 337 ContentAction action, |
158 std::string* error_desc); | 338 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(); |
159 | 412 |
160 // Handles messages posted to us. | 413 // Handles messages posted to us. |
161 virtual void OnMessage(rtc::Message *pmsg); | 414 virtual void OnMessage(rtc::Message *pmsg); |
162 | 415 |
163 TransportController* transport_controller() { | 416 protected: |
164 return transport_controller_.get(); | 417 bool IsCandidateAllocationDone() const; |
165 } | |
166 | 418 |
167 protected: | |
168 State state_; | 419 State state_; |
169 Error error_; | 420 Error error_; |
170 std::string error_desc_; | 421 std::string error_desc_; |
171 | 422 |
| 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 |
172 private: | 428 private: |
173 // Helper methods to push local and remote transport descriptions. | 429 // Helper methods to push local and remote transport descriptions. |
174 bool PushdownLocalTransportDescription( | 430 bool PushdownLocalTransportDescription( |
175 const SessionDescription* sdesc, ContentAction action, | 431 const SessionDescription* sdesc, ContentAction action, |
176 std::string* error_desc); | 432 std::string* error_desc); |
177 bool PushdownRemoteTransportDescription( | 433 bool PushdownRemoteTransportDescription( |
178 const SessionDescription* sdesc, ContentAction action, | 434 const SessionDescription* sdesc, ContentAction action, |
179 std::string* error_desc); | 435 std::string* error_desc); |
180 | 436 |
| 437 void MaybeCandidateAllocationDone(); |
| 438 |
181 // Log session state. | 439 // Log session state. |
182 void LogState(State old_state, State new_state); | 440 void LogState(State old_state, State new_state); |
183 | 441 |
184 // Returns true and the TransportInfo of the given |content_name| | 442 // Returns true and the TransportInfo of the given |content_name| |
185 // from |description|. Returns false if it's not available. | 443 // from |description|. Returns false if it's not available. |
186 static bool GetTransportDescription(const SessionDescription* description, | 444 static bool GetTransportDescription(const SessionDescription* description, |
187 const std::string& content_name, | 445 const std::string& content_name, |
188 TransportDescription* info); | 446 TransportDescription* info); |
189 | 447 |
190 rtc::Thread* const signaling_thread_; | 448 rtc::Thread* const signaling_thread_; |
191 rtc::Thread* const worker_thread_; | 449 rtc::Thread* const worker_thread_; |
192 PortAllocator* const port_allocator_; | 450 PortAllocator* const port_allocator_; |
193 const std::string sid_; | 451 const std::string sid_; |
| 452 const std::string content_type_; |
194 bool initiator_; | 453 bool initiator_; |
195 rtc::scoped_ptr<TransportController> transport_controller_; | 454 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
| 455 rtc::SSLProtocolVersion ssl_max_version_; |
196 rtc::scoped_ptr<const SessionDescription> local_description_; | 456 rtc::scoped_ptr<const SessionDescription> local_description_; |
197 rtc::scoped_ptr<SessionDescription> remote_description_; | 457 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_; |
198 }; | 467 }; |
199 | 468 |
200 } // namespace cricket | 469 } // namespace cricket |
201 | 470 |
202 #endif // WEBRTC_P2P_BASE_SESSION_H_ | 471 #endif // WEBRTC_P2P_BASE_SESSION_H_ |
OLD | NEW |