blob: 6214dad0b1239491ad307d554fb90462570fc4e6 [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);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +000084 TransportChannel* CreateChannel(int component);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000085 bool HasChannel(int component);
86 void DestroyChannel(int component);
87
88 void AddSentCandidates(const Candidates& candidates);
89 void AddUnsentCandidates(const Candidates& candidates);
90 void ClearSentCandidates() { sent_candidates_.clear(); }
91 void ClearUnsentCandidates() { unsent_candidates_.clear(); }
92
93 // Start the connection process for any channels, creating impls if needed.
94 void ConnectChannels();
95 // Hook up impls to the proxy channels. Doesn't change connect state.
96 void CompleteNegotiation();
97
98 // Mux this proxy onto the specified proxy's transport.
99 bool SetupMux(TransportProxy* proxy);
100
101 // Simple functions that thunk down to the same functions on Transport.
102 void SetIceRole(IceRole role);
103 void SetIdentity(rtc::SSLIdentity* identity);
104 bool SetLocalTransportDescription(const TransportDescription& description,
105 ContentAction action,
106 std::string* error_desc);
107 bool SetRemoteTransportDescription(const TransportDescription& description,
108 ContentAction action,
109 std::string* error_desc);
110 void OnSignalingReady();
111 bool OnRemoteCandidates(const Candidates& candidates, std::string* error);
112
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 // Called when a transport signals that it has new candidates.
114 void OnTransportCandidatesReady(cricket::Transport* transport,
115 const Candidates& candidates) {
116 SignalCandidatesReady(this, candidates);
117 }
118
119 bool local_description_set() const {
120 return local_description_set_;
121 }
122 bool remote_description_set() const {
123 return remote_description_set_;
124 }
125
126 // Handles sending of ready candidates and receiving of remote candidates.
127 sigslot::signal2<TransportProxy*,
128 const std::vector<Candidate>&> SignalCandidatesReady;
129
130 private:
131 TransportChannelProxy* GetChannelProxy(int component) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132
decurtis@webrtc.org357469d2015-01-15 22:53:49 +0000133 // Creates a new channel on the Transport which causes the reference
134 // count to increment.
135 void CreateChannelImpl(int component);
136 void CreateChannelImpl_w(int component);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137
138 // Manipulators of transportchannelimpl in channel proxy.
decurtis@webrtc.org357469d2015-01-15 22:53:49 +0000139 void SetChannelImplFromTransport(TransportChannelProxy* proxy, int component);
140 void SetChannelImplFromTransport_w(TransportChannelProxy* proxy,
141 int component);
142 void ReplaceChannelImpl(TransportChannelProxy* proxy,
143 TransportChannelImpl* impl);
144 void ReplaceChannelImpl_w(TransportChannelProxy* proxy,
145 TransportChannelImpl* impl);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000146
147 rtc::Thread* const worker_thread_;
148 const std::string sid_;
149 const std::string content_name_;
150 rtc::scoped_refptr<TransportWrapper> transport_;
151 bool connecting_;
152 bool negotiated_;
153 ChannelMap channels_;
154 Candidates sent_candidates_;
155 Candidates unsent_candidates_;
156 bool candidates_allocated_;
157 bool local_description_set_;
158 bool remote_description_set_;
159};
160
161typedef std::map<std::string, TransportProxy*> TransportMap;
162
163// Statistics for all the transports of this session.
164typedef std::map<std::string, TransportStats> TransportStatsMap;
165typedef std::map<std::string, std::string> ProxyTransportMap;
166
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000167// TODO(pthatcher): Think of a better name for this. We already have
168// a TransportStats in transport.h. Perhaps TransportsStats?
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000169struct 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,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000307 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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000320 rtc::SSLIdentity* identity() { return identity_; }
321
322 protected:
323 // Specifies the identity to use in this session.
324 bool SetIdentity(rtc::SSLIdentity* identity);
325
Joachim Bauch04e5b492015-05-29 09:40:39 +0200326 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
327
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000328 bool PushdownTransportDescription(ContentSource source,
329 ContentAction action,
330 std::string* error_desc);
331 void set_initiator(bool initiator) { initiator_ = initiator; }
332
333 const TransportMap& transport_proxies() const { return transports_; }
334 // Get a TransportProxy by content_name or transport. NULL if not found.
335 TransportProxy* GetTransportProxy(const std::string& content_name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000336 void DestroyTransportProxy(const std::string& content_name);
337 // TransportProxy is owned by session. Return proxy just for convenience.
338 TransportProxy* GetOrCreateTransportProxy(const std::string& content_name);
339 // Creates the actual transport object. Overridable for testing.
340 virtual Transport* CreateTransport(const std::string& content_name);
341
342 void OnSignalingReady();
343 void SpeculativelyConnectAllTransportChannels();
344 // Helper method to provide remote candidates to the transport.
345 bool OnRemoteCandidates(const std::string& content_name,
346 const Candidates& candidates,
347 std::string* error);
348
349 // This method will mux transport channels by content_name.
350 // First content is used for muxing.
351 bool MaybeEnableMuxingSupport();
352
353 // Called when a transport requests signaling.
354 virtual void OnTransportRequestSignaling(Transport* transport) {
355 }
356
357 // Called when the first channel of a transport begins connecting. We use
358 // this to start a timer, to make sure that the connection completes in a
359 // reasonable amount of time.
360 virtual void OnTransportConnecting(Transport* transport) {
361 }
362
363 // Called when a transport changes its writable state. We track this to make
364 // sure that the transport becomes writable within a reasonable amount of
365 // time. If this does not occur, we signal an error.
366 virtual void OnTransportWritable(Transport* transport) {
367 }
368 virtual void OnTransportReadable(Transport* transport) {
369 }
370
Peter Thatcher54360512015-07-08 11:08:35 -0700371 virtual void OnTransportReceiving(Transport* transport) {
372 }
373
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000374 // Called when a transport has found its steady-state connections.
375 virtual void OnTransportCompleted(Transport* transport) {
376 }
377
378 // Called when a transport has failed permanently.
379 virtual void OnTransportFailed(Transport* transport) {
380 }
381
382 // Called when a transport signals that it has new candidates.
383 virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy,
384 const Candidates& candidates) {
385 }
386
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000387 virtual void OnTransportRouteChange(
388 Transport* transport,
389 int component,
390 const cricket::Candidate& remote_candidate) {
391 }
392
393 virtual void OnTransportCandidatesAllocationDone(Transport* transport);
394
395 // Called when all transport channels allocated required candidates.
396 // This method should be used as an indication of candidates gathering process
397 // is completed and application can now send local candidates list to remote.
398 virtual void OnCandidatesAllocationDone() {
399 }
400
401 // Handles the ice role change callback from Transport. This must be
402 // propagated to all the transports.
403 virtual void OnRoleConflict();
404
405 // Handles messages posted to us.
406 virtual void OnMessage(rtc::Message *pmsg);
407
408 protected:
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000409 bool IsCandidateAllocationDone() const;
410
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 State state_;
412 Error error_;
413 std::string error_desc_;
414
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000415 // Fires the new description signal according to the current state.
416 virtual void SignalNewDescription();
Donald Curtis0e209b02015-03-24 09:29:54 -0700417 // This method will delete the Transport and TransportChannelImpls
418 // and replace those with the Transport object of the first
419 // MediaContent in bundle_group.
420 bool BundleContentGroup(const ContentGroup* bundle_group);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000421
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000422 private:
423 // Helper methods to push local and remote transport descriptions.
424 bool PushdownLocalTransportDescription(
425 const SessionDescription* sdesc, ContentAction action,
426 std::string* error_desc);
427 bool PushdownRemoteTransportDescription(
428 const SessionDescription* sdesc, ContentAction action,
429 std::string* error_desc);
430
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000431 void MaybeCandidateAllocationDone();
432
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000433 // Log session state.
434 void LogState(State old_state, State new_state);
435
436 // Returns true and the TransportInfo of the given |content_name|
437 // from |description|. Returns false if it's not available.
438 static bool GetTransportDescription(const SessionDescription* description,
439 const std::string& content_name,
440 TransportDescription* info);
441
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442 // Gets the ContentAction and ContentSource according to the session state.
443 bool GetContentAction(ContentAction* action, ContentSource* source);
444
445 rtc::Thread* const signaling_thread_;
446 rtc::Thread* const worker_thread_;
447 PortAllocator* const port_allocator_;
448 const std::string sid_;
449 const std::string content_type_;
450 const std::string transport_type_;
451 bool initiator_;
452 rtc::SSLIdentity* identity_;
Joachim Bauch04e5b492015-05-29 09:40:39 +0200453 rtc::SSLProtocolVersion ssl_max_version_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000454 rtc::scoped_ptr<const SessionDescription> local_description_;
455 rtc::scoped_ptr<SessionDescription> remote_description_;
456 uint64 ice_tiebreaker_;
457 // This flag will be set to true after the first role switch. This flag
458 // will enable us to stop any role switch during the call.
459 bool role_switch_;
460 TransportMap transports_;
461};
462
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000463} // namespace cricket
464
465#endif // WEBRTC_P2P_BASE_SESSION_H_