blob: 93c1e3cc5a5d2379f269ff19a55d4e36a48e3292 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 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_SESSION_H_
12#define WEBRTC_P2P_BASE_SESSION_H_
13
14#include <list>
15#include <map>
16#include <string>
17#include <vector>
18
pthatcher@webrtc.orgaacc2342014-12-18 20:31:29 +000019#include "webrtc/p2p/base/candidate.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020#include "webrtc/p2p/base/port.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021#include "webrtc/p2p/base/transport.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022#include "webrtc/base/refcount.h"
23#include "webrtc/base/scoped_ptr.h"
24#include "webrtc/base/scoped_ref_ptr.h"
25#include "webrtc/base/socketaddress.h"
26
27namespace cricket {
28
29class BaseSession;
30class P2PTransportChannel;
31class Transport;
32class TransportChannel;
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +000033class TransportChannelProxy;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000034class TransportChannelImpl;
35
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +000036typedef rtc::RefCountedObject<rtc::scoped_ptr<Transport> >
37TransportWrapper;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039// Bundles a Transport and ChannelMap together. ChannelMap is used to
40// create transport channels before receiving or sending a session
41// initiate, and for speculatively connecting channels. Previously, a
42// session had one ChannelMap and transport. Now, with multiple
43// transports per session, we need multiple ChannelMaps as well.
44
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +000045typedef std::map<int, TransportChannelProxy*> ChannelMap;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046
pthatcher@webrtc.org990a00c2015-03-13 18:20:33 +000047class TransportProxy : public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048 public:
49 TransportProxy(
50 rtc::Thread* worker_thread,
51 const std::string& sid,
52 const std::string& content_name,
53 TransportWrapper* transport)
54 : worker_thread_(worker_thread),
55 sid_(sid),
56 content_name_(content_name),
57 transport_(transport),
58 connecting_(false),
59 negotiated_(false),
60 sent_candidates_(false),
61 candidates_allocated_(false),
62 local_description_set_(false),
63 remote_description_set_(false) {
64 transport_->get()->SignalCandidatesReady.connect(
65 this, &TransportProxy::OnTransportCandidatesReady);
66 }
67 ~TransportProxy();
68
69 const std::string& content_name() const { return content_name_; }
70 // TODO(juberti): It's not good form to expose the object you're wrapping,
71 // since callers can mutate it. Can we make this return a const Transport*?
72 Transport* impl() const { return transport_->get(); }
73
74 const std::string& type() const;
75 bool negotiated() const { return negotiated_; }
76 const Candidates& sent_candidates() const { return sent_candidates_; }
77 const Candidates& unsent_candidates() const { return unsent_candidates_; }
78 bool candidates_allocated() const { return candidates_allocated_; }
79 void set_candidates_allocated(bool allocated) {
80 candidates_allocated_ = allocated;
81 }
82
83 TransportChannel* GetChannel(int component);
84 TransportChannel* CreateChannel(const std::string& channel_name,
85 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 SetIdentity(rtc::SSLIdentity* identity);
105 bool SetLocalTransportDescription(const TransportDescription& description,
106 ContentAction action,
107 std::string* error_desc);
108 bool SetRemoteTransportDescription(const TransportDescription& description,
109 ContentAction action,
110 std::string* error_desc);
111 void OnSignalingReady();
112 bool OnRemoteCandidates(const Candidates& candidates, std::string* error);
113
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 // Called when a transport signals that it has new candidates.
115 void OnTransportCandidatesReady(cricket::Transport* transport,
116 const Candidates& candidates) {
117 SignalCandidatesReady(this, candidates);
118 }
119
120 bool local_description_set() const {
121 return local_description_set_;
122 }
123 bool remote_description_set() const {
124 return remote_description_set_;
125 }
126
127 // Handles sending of ready candidates and receiving of remote candidates.
128 sigslot::signal2<TransportProxy*,
129 const std::vector<Candidate>&> SignalCandidatesReady;
130
131 private:
132 TransportChannelProxy* GetChannelProxy(int component) const;
133 TransportChannelProxy* GetChannelProxyByName(const std::string& name) const;
134
decurtis@webrtc.org357469d2015-01-15 22:53:49 +0000135 // 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);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139
140 // Manipulators of transportchannelimpl in channel proxy.
decurtis@webrtc.org357469d2015-01-15 22:53:49 +0000141 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);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148
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
163typedef std::map<std::string, TransportProxy*> TransportMap;
164
165// Statistics for all the transports of this session.
166typedef std::map<std::string, TransportStats> TransportStatsMap;
167typedef std::map<std::string, std::string> ProxyTransportMap;
168
169struct SessionStats {
170 ProxyTransportMap proxy_to_transport;
171 TransportStatsMap transport_stats;
172};
173
174// A BaseSession manages general session state. This includes negotiation
175// of both the application-level and network-level protocols: the former
176// defines what will be sent and the latter defines how it will be sent. Each
177// network-level protocol is represented by a Transport object. Each Transport
178// participates in the network-level negotiation. The individual streams of
179// packets are represented by TransportChannels. The application-level protocol
180// is represented by SessionDecription objects.
181class BaseSession : public sigslot::has_slots<>,
182 public rtc::MessageHandler {
183 public:
184 enum {
185 MSG_TIMEOUT = 0,
186 MSG_ERROR,
187 MSG_STATE,
188 };
189
190 enum State {
191 STATE_INIT = 0,
192 STATE_SENTINITIATE, // sent initiate, waiting for Accept or Reject
193 STATE_RECEIVEDINITIATE, // received an initiate. Call Accept or Reject
194 STATE_SENTPRACCEPT, // sent provisional Accept
195 STATE_SENTACCEPT, // sent accept. begin connecting transport
196 STATE_RECEIVEDPRACCEPT, // received provisional Accept, waiting for Accept
197 STATE_RECEIVEDACCEPT, // received accept. begin connecting transport
198 STATE_SENTMODIFY, // sent modify, waiting for Accept or Reject
199 STATE_RECEIVEDMODIFY, // received modify, call Accept or Reject
200 STATE_SENTREJECT, // sent reject after receiving initiate
201 STATE_RECEIVEDREJECT, // received reject after sending initiate
202 STATE_SENTREDIRECT, // sent direct after receiving initiate
203 STATE_SENTTERMINATE, // sent terminate (any time / either side)
204 STATE_RECEIVEDTERMINATE, // received terminate (any time / either side)
205 STATE_INPROGRESS, // session accepted and in progress
206 STATE_DEINIT, // session is being destroyed
207 };
208
209 enum Error {
210 ERROR_NONE = 0, // no error
211 ERROR_TIME = 1, // no response to signaling
212 ERROR_RESPONSE = 2, // error during signaling
213 ERROR_NETWORK = 3, // network error, could not allocate network resources
214 ERROR_CONTENT = 4, // channel errors in SetLocalContent/SetRemoteContent
215 ERROR_TRANSPORT = 5, // transport error of some kind
216 };
217
218 // Convert State to a readable string.
219 static std::string StateToString(State state);
220
221 BaseSession(rtc::Thread* signaling_thread,
222 rtc::Thread* worker_thread,
223 PortAllocator* port_allocator,
224 const std::string& sid,
225 const std::string& content_type,
226 bool initiator);
227 virtual ~BaseSession();
228
229 // These are const to allow them to be called from const methods.
230 rtc::Thread* signaling_thread() const { return signaling_thread_; }
231 rtc::Thread* worker_thread() const { return worker_thread_; }
232 PortAllocator* port_allocator() const { return port_allocator_; }
233
234 // The ID of this session.
235 const std::string& id() const { return sid_; }
236
237 // TODO(juberti): This data is largely redundant, as it can now be obtained
238 // from local/remote_description(). Remove these functions and members.
239 // Returns the XML namespace identifying the type of this session.
240 const std::string& content_type() const { return content_type_; }
241 // Returns the XML namespace identifying the transport used for this session.
242 const std::string& transport_type() const { return transport_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.
248 // If we are the recipient, this will be NULL until we send an accept.
249 const SessionDescription* local_description() const;
250
251 // 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.
253 const SessionDescription* remote_description() const;
254
255 SessionDescription* remote_description();
256
257 // Takes ownership of SessionDescription*
258 void set_local_description(const SessionDescription* sdesc);
259
260 // Takes ownership of SessionDescription*
261 void set_remote_description(SessionDescription* sdesc);
262
263 const SessionDescription* initiator_description() const;
264
265 // Returns the current state of the session. See the enum above for details.
266 // Each time the state changes, we will fire this signal.
267 State state() const { return state_; }
268 sigslot::signal2<BaseSession* , State> SignalState;
269
270 // 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.
272 Error error() const { return error_; }
273 const std::string& error_desc() const { return error_desc_; }
274 sigslot::signal2<BaseSession* , Error> SignalError;
275
276 // Updates the state, signaling if necessary.
277 virtual void SetState(State state);
278
279 // Updates the error state, signaling if necessary.
280 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|.
281 virtual void SetError(Error error, const std::string& error_desc);
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 const std::string& channel_name,
308 int component);
309
310 // Returns the channel with the given names.
311 virtual TransportChannel* GetChannel(const std::string& content_name,
312 int component);
313
314 // Destroys the channel with the given names.
315 // This will usually be called from the worker thread, but that
316 // shouldn't be an issue since the main thread will be blocked in
317 // Send when doing so.
318 virtual void DestroyChannel(const std::string& content_name,
319 int component);
320
321 // Returns stats for all channels of all transports.
322 // This avoids exposing the internal structures used to track them.
323 virtual bool GetStats(SessionStats* stats);
324
325 rtc::SSLIdentity* identity() { return identity_; }
326
327 protected:
328 // Specifies the identity to use in this session.
329 bool SetIdentity(rtc::SSLIdentity* identity);
330
331 bool PushdownTransportDescription(ContentSource source,
332 ContentAction action,
333 std::string* error_desc);
334 void set_initiator(bool initiator) { initiator_ = initiator; }
335
336 const TransportMap& transport_proxies() const { return transports_; }
337 // Get a TransportProxy by content_name or transport. NULL if not found.
338 TransportProxy* GetTransportProxy(const std::string& content_name);
339 TransportProxy* GetTransportProxy(const Transport* transport);
340 TransportProxy* GetFirstTransportProxy();
341 void DestroyTransportProxy(const std::string& content_name);
342 // TransportProxy is owned by session. Return proxy just for convenience.
343 TransportProxy* GetOrCreateTransportProxy(const std::string& content_name);
344 // Creates the actual transport object. Overridable for testing.
345 virtual Transport* CreateTransport(const std::string& content_name);
346
347 void OnSignalingReady();
348 void SpeculativelyConnectAllTransportChannels();
349 // Helper method to provide remote candidates to the transport.
350 bool OnRemoteCandidates(const std::string& content_name,
351 const Candidates& candidates,
352 std::string* error);
353
354 // This method will mux transport channels by content_name.
355 // First content is used for muxing.
356 bool MaybeEnableMuxingSupport();
357
358 // Called when a transport requests signaling.
359 virtual void OnTransportRequestSignaling(Transport* transport) {
360 }
361
362 // Called when the first channel of a transport begins connecting. We use
363 // this to start a timer, to make sure that the connection completes in a
364 // reasonable amount of time.
365 virtual void OnTransportConnecting(Transport* transport) {
366 }
367
368 // Called when a transport changes its writable state. We track this to make
369 // sure that the transport becomes writable within a reasonable amount of
370 // time. If this does not occur, we signal an error.
371 virtual void OnTransportWritable(Transport* transport) {
372 }
373 virtual void OnTransportReadable(Transport* transport) {
374 }
375
376 // Called when a transport has found its steady-state connections.
377 virtual void OnTransportCompleted(Transport* transport) {
378 }
379
380 // Called when a transport has failed permanently.
381 virtual void OnTransportFailed(Transport* transport) {
382 }
383
384 // Called when a transport signals that it has new candidates.
385 virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy,
386 const Candidates& candidates) {
387 }
388
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000389 virtual void OnTransportRouteChange(
390 Transport* transport,
391 int component,
392 const cricket::Candidate& remote_candidate) {
393 }
394
395 virtual void OnTransportCandidatesAllocationDone(Transport* transport);
396
397 // Called when all transport channels allocated required candidates.
398 // This method should be used as an indication of candidates gathering process
399 // is completed and application can now send local candidates list to remote.
400 virtual void OnCandidatesAllocationDone() {
401 }
402
403 // Handles the ice role change callback from Transport. This must be
404 // propagated to all the transports.
405 virtual void OnRoleConflict();
406
407 // Handles messages posted to us.
408 virtual void OnMessage(rtc::Message *pmsg);
409
410 protected:
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000411 bool IsCandidateAllocationDone() const;
412
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000413 State state_;
414 Error error_;
415 std::string error_desc_;
416
417 private:
418 // Helper methods to push local and remote transport descriptions.
419 bool PushdownLocalTransportDescription(
420 const SessionDescription* sdesc, ContentAction action,
421 std::string* error_desc);
422 bool PushdownRemoteTransportDescription(
423 const SessionDescription* sdesc, ContentAction action,
424 std::string* error_desc);
425
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000426 void MaybeCandidateAllocationDone();
427
428 // This method will delete the Transport and TransportChannelImpls and
429 // replace those with the selected Transport objects. Selection is done
430 // based on the content_name and in this case first MediaContent information
431 // is used for mux.
432 bool SetSelectedProxy(const std::string& content_name,
433 const ContentGroup* muxed_group);
434 // Log session state.
435 void LogState(State old_state, State new_state);
436
437 // Returns true and the TransportInfo of the given |content_name|
438 // from |description|. Returns false if it's not available.
439 static bool GetTransportDescription(const SessionDescription* description,
440 const std::string& content_name,
441 TransportDescription* info);
442
443 // Fires the new description signal according to the current state.
444 void SignalNewDescription();
445
446 // Gets the ContentAction and ContentSource according to the session state.
447 bool GetContentAction(ContentAction* action, ContentSource* source);
448
449 rtc::Thread* const signaling_thread_;
450 rtc::Thread* const worker_thread_;
451 PortAllocator* const port_allocator_;
452 const std::string sid_;
453 const std::string content_type_;
454 const std::string transport_type_;
455 bool initiator_;
456 rtc::SSLIdentity* identity_;
457 rtc::scoped_ptr<const SessionDescription> local_description_;
458 rtc::scoped_ptr<SessionDescription> remote_description_;
459 uint64 ice_tiebreaker_;
460 // This flag will be set to true after the first role switch. This flag
461 // will enable us to stop any role switch during the call.
462 bool role_switch_;
463 TransportMap transports_;
464};
465
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000466} // namespace cricket
467
468#endif // WEBRTC_P2P_BASE_SESSION_H_