blob: 8d7aa21c222b3c19d42ac943d3666d3a05f72cb2 [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
Guo-wei Shieh89024332015-09-18 13:50:22 -070019#include "webrtc/p2p/base/candidate.h"
20#include "webrtc/p2p/base/port.h"
21#include "webrtc/p2p/base/transport.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022#include "webrtc/base/refcount.h"
Henrik Boströmd8281982015-08-27 10:12:24 +020023#include "webrtc/base/rtccertificate.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024#include "webrtc/base/scoped_ptr.h"
25#include "webrtc/base/scoped_ref_ptr.h"
26#include "webrtc/base/socketaddress.h"
27
28namespace cricket {
29
30class BaseSession;
31class P2PTransportChannel;
32class Transport;
33class TransportChannel;
Guo-wei Shieh89024332015-09-18 13:50:22 -070034class TransportChannelProxy;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035class TransportChannelImpl;
Guo-wei Shieh89024332015-09-18 13:50:22 -070036
37typedef rtc::RefCountedObject<rtc::scoped_ptr<Transport> >
38TransportWrapper;
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
46typedef std::map<int, TransportChannelProxy*> ChannelMap;
47
48class 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
163typedef std::map<std::string, TransportProxy*> TransportMap;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000164
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
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000169// TODO(pthatcher): Think of a better name for this. We already have
170// a TransportStats in transport.h. Perhaps TransportsStats?
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171struct SessionStats {
172 ProxyTransportMap proxy_to_transport;
173 TransportStatsMap transport_stats;
174};
175
176// A BaseSession manages general session state. This includes negotiation
177// of both the application-level and network-level protocols: the former
178// defines what will be sent and the latter defines how it will be sent. Each
179// network-level protocol is represented by a Transport object. Each Transport
180// participates in the network-level negotiation. The individual streams of
181// packets are represented by TransportChannels. The application-level protocol
182// is represented by SessionDecription objects.
183class BaseSession : public sigslot::has_slots<>,
184 public rtc::MessageHandler {
185 public:
186 enum {
187 MSG_TIMEOUT = 0,
188 MSG_ERROR,
189 MSG_STATE,
190 };
191
192 enum State {
193 STATE_INIT = 0,
194 STATE_SENTINITIATE, // sent initiate, waiting for Accept or Reject
195 STATE_RECEIVEDINITIATE, // received an initiate. Call Accept or Reject
196 STATE_SENTPRACCEPT, // sent provisional Accept
197 STATE_SENTACCEPT, // sent accept. begin connecting transport
198 STATE_RECEIVEDPRACCEPT, // received provisional Accept, waiting for Accept
199 STATE_RECEIVEDACCEPT, // received accept. begin connecting transport
200 STATE_SENTMODIFY, // sent modify, waiting for Accept or Reject
201 STATE_RECEIVEDMODIFY, // received modify, call Accept or Reject
202 STATE_SENTREJECT, // sent reject after receiving initiate
203 STATE_RECEIVEDREJECT, // received reject after sending initiate
204 STATE_SENTREDIRECT, // sent direct after receiving initiate
205 STATE_SENTTERMINATE, // sent terminate (any time / either side)
206 STATE_RECEIVEDTERMINATE, // received terminate (any time / either side)
207 STATE_INPROGRESS, // session accepted and in progress
208 STATE_DEINIT, // session is being destroyed
209 };
210
211 enum Error {
212 ERROR_NONE = 0, // no error
213 ERROR_TIME = 1, // no response to signaling
214 ERROR_RESPONSE = 2, // error during signaling
215 ERROR_NETWORK = 3, // network error, could not allocate network resources
216 ERROR_CONTENT = 4, // channel errors in SetLocalContent/SetRemoteContent
217 ERROR_TRANSPORT = 5, // transport error of some kind
218 };
219
220 // Convert State to a readable string.
221 static std::string StateToString(State state);
222
223 BaseSession(rtc::Thread* signaling_thread,
224 rtc::Thread* worker_thread,
225 PortAllocator* port_allocator,
226 const std::string& sid,
Guo-wei Shieh89024332015-09-18 13:50:22 -0700227 const std::string& content_type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228 bool initiator);
229 virtual ~BaseSession();
230
231 // These are const to allow them to be called from const methods.
232 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_; }
235
236 // The ID of this session.
237 const std::string& id() const { return sid_; }
238
Guo-wei Shieh89024332015-09-18 13:50:22 -0700239 // 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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247 // 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
Guo-wei Shieh89024332015-09-18 13:50:22 -0700283 // 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.
honghaiz90099622015-07-13 12:19:33 -0700321 void SetIceConnectionReceivingTimeout(int timeout_ms);
322
Guo-wei Shieh89024332015-09-18 13:50:22 -0700323 // For testing.
324 const rtc::scoped_refptr<rtc::RTCCertificate>&
325 certificate_for_testing() const {
326 return certificate_;
327 }
328
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000329 protected:
Guo-wei Shieh89024332015-09-18 13:50:22 -0700330 // 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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000336 bool PushdownTransportDescription(ContentSource source,
337 ContentAction action,
338 std::string* error_desc);
Guo-wei Shieh89024332015-09-18 13:50:22 -0700339 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();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000412
413 // Handles messages posted to us.
414 virtual void OnMessage(rtc::Message *pmsg);
415
deadbeef9af63f42015-09-18 12:55:56 -0700416 protected:
Guo-wei Shieh89024332015-09-18 13:50:22 -0700417 bool IsCandidateAllocationDone() const;
418
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000419 State state_;
420 Error error_;
421 std::string error_desc_;
422
Guo-wei Shieh89024332015-09-18 13:50:22 -0700423 // 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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000428 private:
429 // Helper methods to push local and remote transport descriptions.
430 bool PushdownLocalTransportDescription(
431 const SessionDescription* sdesc, ContentAction action,
432 std::string* error_desc);
433 bool PushdownRemoteTransportDescription(
434 const SessionDescription* sdesc, ContentAction action,
435 std::string* error_desc);
436
Guo-wei Shieh89024332015-09-18 13:50:22 -0700437 void MaybeCandidateAllocationDone();
438
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000439 // Log session state.
440 void LogState(State old_state, State new_state);
441
442 // Returns true and the TransportInfo of the given |content_name|
443 // from |description|. Returns false if it's not available.
444 static bool GetTransportDescription(const SessionDescription* description,
445 const std::string& content_name,
446 TransportDescription* info);
447
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000448 rtc::Thread* const signaling_thread_;
449 rtc::Thread* const worker_thread_;
450 PortAllocator* const port_allocator_;
451 const std::string sid_;
Guo-wei Shieh89024332015-09-18 13:50:22 -0700452 const std::string content_type_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000453 bool initiator_;
Guo-wei Shieh89024332015-09-18 13:50:22 -0700454 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
455 rtc::SSLProtocolVersion ssl_max_version_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000456 rtc::scoped_ptr<const SessionDescription> local_description_;
457 rtc::scoped_ptr<SessionDescription> remote_description_;
Guo-wei Shieh89024332015-09-18 13:50:22 -0700458 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_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000467};
468
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000469} // namespace cricket
470
471#endif // WEBRTC_P2P_BASE_SESSION_H_