blob: 284c601b41997de313f69dd571ad42292a2fd488 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_WEBRTCSESSION_H_
12#define PC_WEBRTCSESSION_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
deadbeef0ed85b22016-02-23 17:24:52 -080015#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016#include <string>
deadbeefcbecd352015-09-23 11:50:27 -070017#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/optional.h"
21#include "api/peerconnectioninterface.h"
22#include "api/statstypes.h"
23#include "call/call.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "pc/datachannel.h"
25#include "pc/mediasession.h"
Zhi Huangb5261582017-09-29 10:51:43 -070026#include "pc/transportcontroller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/constructormagic.h"
28#include "rtc_base/sigslot.h"
29#include "rtc_base/sslidentity.h"
30#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
zhihuang9763d562016-08-05 11:14:50 -070032#ifdef HAVE_QUIC
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "pc/quicdatatransport.h"
zhihuang9763d562016-08-05 11:14:50 -070034#endif // HAVE_QUIC
35
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036namespace cricket {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000037
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038class ChannelManager;
deadbeef953c2ce2017-01-09 14:53:41 -080039class RtpDataChannel;
40class SctpTransportInternal;
41class SctpTransportInternalFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042class StatsReport;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043class VideoChannel;
44class VoiceChannel;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000045
zhihuang9763d562016-08-05 11:14:50 -070046#ifdef HAVE_QUIC
47class QuicTransportChannel;
48#endif // HAVE_QUIC
49
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050} // namespace cricket
51
52namespace webrtc {
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000053
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054class IceRestartAnswerLatch;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000055class JsepIceCandidate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056class MediaStreamSignaling;
nisseeaabdf62017-05-05 02:23:02 -070057class RtcEventLog;
wu@webrtc.org91053e72013-08-10 07:18:04 +000058class WebRtcSessionDescriptionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000060extern const char kBundleWithoutRtcpMux[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000061extern const char kCreateChannelFailed[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062extern const char kInvalidCandidates[];
63extern const char kInvalidSdp[];
Zhi Huang2a5e4262017-09-14 01:15:03 -070064extern const char kMlineMismatchInAnswer[];
65extern const char kMlineMismatchInSubsequentOffer[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000066extern const char kPushDownTDFailed[];
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000067extern const char kSdpWithoutDtlsFingerprint[];
68extern const char kSdpWithoutSdesCrypto[];
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000069extern const char kSdpWithoutIceUfragPwd[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000070extern const char kSdpWithoutSdesAndDtlsDisabled[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071extern const char kSessionError[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000072extern const char kSessionErrorDesc[];
deadbeef953c2ce2017-01-09 14:53:41 -080073extern const char kDtlsSrtpSetupFailureRtp[];
74extern const char kDtlsSrtpSetupFailureRtcp[];
deadbeefcbecd352015-09-23 11:50:27 -070075extern const char kEnableBundleFailed[];
76
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000077// Maximum number of received video streams that will be processed by webrtc
78// even if they are not signalled beforehand.
79extern const int kMaxUnsignalledRecvStreams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
81// ICE state callback interface.
82class IceObserver {
83 public:
wu@webrtc.org364f2042013-11-20 21:49:41 +000084 IceObserver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 // Called any time the IceConnectionState changes
zstein6dfd53a2017-03-06 13:49:03 -080086 virtual void OnIceConnectionStateChange(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 PeerConnectionInterface::IceConnectionState new_state) {}
88 // Called any time the IceGatheringState changes
89 virtual void OnIceGatheringChange(
90 PeerConnectionInterface::IceGatheringState new_state) {}
91 // New Ice candidate have been found.
jbauch81bf7b02017-03-25 08:31:12 -070092 virtual void OnIceCandidate(
93 std::unique_ptr<IceCandidateInterface> candidate) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
Honghai Zhang7fb69db2016-03-14 11:59:18 -070095 // Some local ICE candidates have been removed.
96 virtual void OnIceCandidatesRemoved(
97 const std::vector<cricket::Candidate>& candidates) = 0;
98
Peter Thatcher54360512015-07-08 11:08:35 -070099 // Called whenever the state changes between receiving and not receiving.
100 virtual void OnIceConnectionReceivingChange(bool receiving) {}
101
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 protected:
103 ~IceObserver() {}
wu@webrtc.org364f2042013-11-20 21:49:41 +0000104
105 private:
henrikg3c089d72015-09-16 05:37:44 -0700106 RTC_DISALLOW_COPY_AND_ASSIGN(IceObserver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107};
108
deadbeefd59daf82015-10-14 15:02:44 -0700109// Statistics for all the transports of the session.
110typedef std::map<std::string, cricket::TransportStats> TransportStatsMap;
111typedef std::map<std::string, std::string> ProxyTransportMap;
112
113// TODO(pthatcher): Think of a better name for this. We already have
114// a TransportStats in transport.h. Perhaps TransportsStats?
115struct SessionStats {
116 ProxyTransportMap proxy_to_transport;
117 TransportStatsMap transport_stats;
118};
119
hbosdf6075a2016-12-19 04:58:02 -0800120struct ChannelNamePair {
121 ChannelNamePair(
122 const std::string& content_name, const std::string& transport_name)
123 : content_name(content_name), transport_name(transport_name) {}
124 std::string content_name;
125 std::string transport_name;
126};
127
128struct ChannelNamePairs {
129 rtc::Optional<ChannelNamePair> voice;
130 rtc::Optional<ChannelNamePair> video;
131 rtc::Optional<ChannelNamePair> data;
132};
133
deadbeefd59daf82015-10-14 15:02:44 -0700134// A WebRtcSession manages general session state. This includes negotiation
135// of both the application-level and network-level protocols: the former
136// defines what will be sent and the latter defines how it will be sent. Each
137// network-level protocol is represented by a Transport object. Each Transport
138// participates in the network-level negotiation. The individual streams of
139// packets are represented by TransportChannels. The application-level protocol
140// is represented by SessionDecription objects.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700141class WebRtcSession :
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700142 public DataChannelProviderInterface,
143 public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 public:
deadbeefd59daf82015-10-14 15:02:44 -0700145 enum State {
146 STATE_INIT = 0,
147 STATE_SENTOFFER, // Sent offer, waiting for answer.
148 STATE_RECEIVEDOFFER, // Received an offer. Need to send answer.
149 STATE_SENTPRANSWER, // Sent provisional answer. Need to send answer.
150 STATE_RECEIVEDPRANSWER, // Received provisional answer, waiting for answer.
151 STATE_INPROGRESS, // Offer/answer exchange completed.
152 STATE_CLOSED, // Close() was called.
153 };
154
155 enum Error {
156 ERROR_NONE = 0, // no error
157 ERROR_CONTENT = 1, // channel errors in SetLocalContent/SetRemoteContent
158 ERROR_TRANSPORT = 2, // transport error of some kind
159 };
160
deadbeef953c2ce2017-01-09 14:53:41 -0800161 // |sctp_factory| may be null, in which case SCTP is treated as unsupported.
zhihuang29ff8442016-07-27 11:07:25 -0700162 WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700163 Call* call,
164 cricket::ChannelManager* channel_manager,
165 const cricket::MediaConfig& media_config,
166 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700167 rtc::Thread* network_thread,
168 rtc::Thread* worker_thread,
169 rtc::Thread* signaling_thread,
170 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800171 std::unique_ptr<cricket::TransportController> transport_controller,
172 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 virtual ~WebRtcSession();
174
deadbeefd59daf82015-10-14 15:02:44 -0700175 // These are const to allow them to be called from const methods.
zhihuang9763d562016-08-05 11:14:50 -0700176 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700177 rtc::Thread* worker_thread() const { return worker_thread_; }
danilchape9021a32016-05-17 01:52:02 -0700178 rtc::Thread* signaling_thread() const { return signaling_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700179
180 // The ID of this session.
181 const std::string& id() const { return sid_; }
182
Henrik Lundin64dad832015-05-11 12:44:23 +0200183 bool Initialize(
184 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200185 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200186 const PeerConnectionInterface::RTCConfiguration& rtc_configuration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 // Deletes the voice, video and data channel and changes the session state
deadbeefd59daf82015-10-14 15:02:44 -0700188 // to STATE_CLOSED.
189 void Close();
190
191 // Returns true if we were the initial offerer.
192 bool initial_offerer() const { return initial_offerer_; }
193
194 // Returns the current state of the session. See the enum above for details.
195 // Each time the state changes, we will fire this signal.
196 State state() const { return state_; }
197 sigslot::signal2<WebRtcSession*, State> SignalState;
198
199 // Returns the last error in the session. See the enum above for details.
200 Error error() const { return error_; }
201 const std::string& error_desc() const { return error_desc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202
203 void RegisterIceObserver(IceObserver* observer) {
204 ice_observer_ = observer;
205 }
206
deadbeef953c2ce2017-01-09 14:53:41 -0800207 // Exposed for stats collecting.
Steve Anton169629a2017-08-30 17:36:36 -0700208 // TODO(steveanton): Switch callers to use the plural form and remove these.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 virtual cricket::VoiceChannel* voice_channel() {
Steve Anton169629a2017-08-30 17:36:36 -0700210 if (voice_channels_.empty()) {
211 return nullptr;
212 } else {
213 return voice_channels_[0];
214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 }
216 virtual cricket::VideoChannel* video_channel() {
Steve Anton169629a2017-08-30 17:36:36 -0700217 if (video_channels_.empty()) {
218 return nullptr;
219 } else {
220 return video_channels_[0];
221 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 }
Steve Anton169629a2017-08-30 17:36:36 -0700223
224 virtual std::vector<cricket::VoiceChannel*> voice_channels() const {
225 return voice_channels_;
226 }
227 virtual std::vector<cricket::VideoChannel*> video_channels() const {
228 return video_channels_;
229 }
230
deadbeef953c2ce2017-01-09 14:53:41 -0800231 // Only valid when using deprecated RTP data channels.
232 virtual cricket::RtpDataChannel* rtp_data_channel() {
233 return rtp_data_channel_.get();
234 }
235 virtual rtc::Optional<std::string> sctp_content_name() const {
236 return sctp_content_name_;
237 }
238 virtual rtc::Optional<std::string> sctp_transport_name() const {
239 return sctp_transport_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 }
241
deadbeef0ed85b22016-02-23 17:24:52 -0800242 cricket::BaseChannel* GetChannel(const std::string& content_name);
243
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000244 cricket::SecurePolicy SdesPolicy() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245
deadbeef953c2ce2017-01-09 14:53:41 -0800246 // Get current SSL role used by SCTP's underlying transport.
247 bool GetSctpSslRole(rtc::SSLRole* role);
248 // Get SSL role for an arbitrary m= section (handles bundling correctly).
249 // TODO(deadbeef): This is only used internally by the session description
250 // factory, it shouldn't really be public).
251 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000252
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000253 void CreateOffer(
254 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700255 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
256 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000257 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700258 const cricket::MediaSessionOptions& session_options);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000259 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 bool SetLocalDescription(SessionDescriptionInterface* desc,
261 std::string* err_desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000262 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 bool SetRemoteDescription(SessionDescriptionInterface* desc,
264 std::string* err_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 bool ProcessIceMessage(const IceCandidateInterface* ice_candidate);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000267
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700268 bool RemoveRemoteIceCandidates(
269 const std::vector<cricket::Candidate>& candidates);
270
honghaiz1f429e32015-09-28 07:57:34 -0700271 cricket::IceConfig ParseIceConfig(
272 const PeerConnectionInterface::RTCConfiguration& config) const;
273
deadbeefd59daf82015-10-14 15:02:44 -0700274 void SetIceConfig(const cricket::IceConfig& ice_config);
275
276 // Start gathering candidates for any new transports, or transports doing an
277 // ICE restart.
278 void MaybeStartGathering();
279
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 const SessionDescriptionInterface* local_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800281 return pending_local_description_ ? pending_local_description_.get()
282 : current_local_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 }
284 const SessionDescriptionInterface* remote_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800285 return pending_remote_description_ ? pending_remote_description_.get()
286 : current_remote_description_.get();
287 }
288 const SessionDescriptionInterface* current_local_description() const {
289 return current_local_description_.get();
290 }
291 const SessionDescriptionInterface* current_remote_description() const {
292 return current_remote_description_.get();
293 }
294 const SessionDescriptionInterface* pending_local_description() const {
295 return pending_local_description_.get();
296 }
297 const SessionDescriptionInterface* pending_remote_description() const {
298 return pending_remote_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 }
300
301 // Get the id used as a media stream track's "id" field from ssrc.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200302 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
303 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000304
wu@webrtc.org78187522013-10-07 23:32:02 +0000305 // Implements DataChannelProviderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000306 bool SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700307 const rtc::CopyOnWriteBuffer& payload,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000308 cricket::SendDataResult* result) override;
309 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
310 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
311 void AddSctpDataStream(int sid) override;
312 void RemoveSctpDataStream(int sid) override;
313 bool ReadyToSendData() const override;
wu@webrtc.org78187522013-10-07 23:32:02 +0000314
stefanf79ade12017-06-02 06:44:03 -0700315 virtual Call::Stats GetCallStats();
316
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000317 // Returns stats for all channels of all transports.
318 // This avoids exposing the internal structures used to track them.
hbosdf6075a2016-12-19 04:58:02 -0800319 // The parameterless version creates |ChannelNamePairs| from |voice_channel|,
320 // |video_channel| and |voice_channel| if available - this requires it to be
321 // called on the signaling thread - and invokes the other |GetStats|. The
322 // other |GetStats| can be invoked on any thread; if not invoked on the
323 // network thread a thread hop will happen.
324 std::unique_ptr<SessionStats> GetStats_s();
325 virtual std::unique_ptr<SessionStats> GetStats(
326 const ChannelNamePairs& channel_name_pairs);
deadbeefcbecd352015-09-23 11:50:27 -0700327
328 // virtual so it can be mocked in unit tests
329 virtual bool GetLocalCertificate(
330 const std::string& transport_name,
331 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
332
333 // Caller owns returned certificate
jbauch555604a2016-04-26 03:13:22 -0700334 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -0700335 const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700336
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 cricket::DataChannelType data_channel_type() const;
338
deadbeefd1a38b52016-12-10 13:15:33 -0800339 // Returns true if there was an ICE restart initiated by the remote offer.
deadbeef0ed85b22016-02-23 17:24:52 -0800340 bool IceRestartPending(const std::string& content_name) const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000341
deadbeefd1a38b52016-12-10 13:15:33 -0800342 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
343 // set, offers should generate new ufrags/passwords until an ICE restart
344 // occurs.
345 void SetNeedsIceRestartFlag();
346 // Returns true if the ICE restart flag above was set, and no ICE restart has
347 // occurred yet for this transport (by applying a local description with
348 // changed ufrag/password). If the transport has been deleted as a result of
349 // bundling, returns false.
350 bool NeedsIceRestart(const std::string& content_name) const;
351
Henrik Boströmd8281982015-08-27 10:12:24 +0200352 // Called when an RTCCertificate is generated or retrieved by
wu@webrtc.org91053e72013-08-10 07:18:04 +0000353 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
Henrik Boströmd8281982015-08-27 10:12:24 +0200354 void OnCertificateReady(
355 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
deadbeef953c2ce2017-01-09 14:53:41 -0800356 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000357
358 // For unit test.
Henrik Boströmd8281982015-08-27 10:12:24 +0200359 bool waiting_for_certificate_for_testing() const;
deadbeefcbecd352015-09-23 11:50:27 -0700360 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000361
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000362 void set_metrics_observer(
363 webrtc::MetricsObserverInterface* metrics_observer) {
364 metrics_observer_ = metrics_observer;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700365 transport_controller_->SetMetricsObserver(metrics_observer);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000366 }
367
deadbeef953c2ce2017-01-09 14:53:41 -0800368 // Called when voice_channel_, video_channel_ and
369 // rtp_data_channel_/sctp_transport_ are created and destroyed. As a result
370 // of, for example, setting a new description.
deadbeefab9b2d12015-10-14 11:33:11 -0700371 sigslot::signal0<> SignalVoiceChannelCreated;
372 sigslot::signal0<> SignalVoiceChannelDestroyed;
373 sigslot::signal0<> SignalVideoChannelCreated;
374 sigslot::signal0<> SignalVideoChannelDestroyed;
375 sigslot::signal0<> SignalDataChannelCreated;
376 sigslot::signal0<> SignalDataChannelDestroyed;
377
378 // Called when a valid data channel OPEN message is received.
379 // std::string represents the data channel label.
380 sigslot::signal2<const std::string&, const InternalDataChannelInit&>
381 SignalDataChannelOpenMessage;
zhihuang9763d562016-08-05 11:14:50 -0700382#ifdef HAVE_QUIC
383 QuicDataTransport* quic_data_transport() {
384 return quic_data_transport_.get();
385 }
386#endif // HAVE_QUIC
deadbeefab9b2d12015-10-14 11:33:11 -0700387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 private:
389 // Indicates the type of SessionDescription in a call to SetLocalDescription
390 // and SetRemoteDescription.
391 enum Action {
392 kOffer,
393 kPrAnswer,
394 kAnswer,
395 };
wu@webrtc.org91053e72013-08-10 07:18:04 +0000396
Steve Anton18ee1d52017-09-11 11:32:35 -0700397 // Return all managed, non-null channels.
398 std::vector<cricket::BaseChannel*> Channels() const;
399
deadbeeffe4a8a42016-12-20 17:56:17 -0800400 // Non-const versions of local_description()/remote_description(), for use
401 // internally.
402 SessionDescriptionInterface* mutable_local_description() {
403 return pending_local_description_ ? pending_local_description_.get()
404 : current_local_description_.get();
405 }
406 SessionDescriptionInterface* mutable_remote_description() {
407 return pending_remote_description_ ? pending_remote_description_.get()
408 : current_remote_description_.get();
409 }
410
deadbeefd59daf82015-10-14 15:02:44 -0700411 // Log session state.
412 void LogState(State old_state, State new_state);
413
414 // Updates the state, signaling if necessary.
415 virtual void SetState(State state);
416
417 // Updates the error state, signaling if necessary.
418 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|.
419 virtual void SetError(Error error, const std::string& error_desc);
420
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 bool UpdateSessionState(Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 std::string* err_desc);
423 static Action GetAction(const std::string& type);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000424 // Push the media parts of the local or remote session description
425 // down to all of the channels.
426 bool PushdownMediaDescription(cricket::ContentAction action,
427 cricket::ContentSource source,
428 std::string* error_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800429 bool PushdownSctpParameters_n(cricket::ContentSource source);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000430
deadbeefd59daf82015-10-14 15:02:44 -0700431 bool PushdownTransportDescription(cricket::ContentSource source,
432 cricket::ContentAction action,
433 std::string* error_desc);
434
435 // Helper methods to push local and remote transport descriptions.
436 bool PushdownLocalTransportDescription(
437 const cricket::SessionDescription* sdesc,
438 cricket::ContentAction action,
439 std::string* error_desc);
440 bool PushdownRemoteTransportDescription(
441 const cricket::SessionDescription* sdesc,
442 cricket::ContentAction action,
443 std::string* error_desc);
444
445 // Returns true and the TransportInfo of the given |content_name|
446 // from |description|. Returns false if it's not available.
447 static bool GetTransportDescription(
448 const cricket::SessionDescription* description,
449 const std::string& content_name,
450 cricket::TransportDescription* info);
451
skvlad6c87a672016-05-17 17:49:52 -0700452 // Returns the name of the transport channel when BUNDLE is enabled, or
453 // nullptr if the channel is not part of any bundle.
454 const std::string* GetBundleTransportName(
455 const cricket::ContentInfo* content,
456 const cricket::ContentGroup* bundle);
457
deadbeefcbecd352015-09-23 11:50:27 -0700458 // Cause all the BaseChannels in the bundle group to have the same
459 // transport channel.
460 bool EnableBundle(const cricket::ContentGroup& bundle);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 // Enables media channels to allow sending of media.
463 void EnableChannels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 // Returns the media index for a local ice candidate given the content name.
465 // Returns false if the local session description does not have a media
466 // content called |content_name|.
467 bool GetLocalCandidateMediaIndex(const std::string& content_name,
468 int* sdp_mline_index);
469 // Uses all remote candidates in |remote_desc| in this session.
470 bool UseCandidatesInSessionDescription(
471 const SessionDescriptionInterface* remote_desc);
472 // Uses |candidate| in this session.
473 bool UseCandidate(const IceCandidateInterface* candidate);
474 // Deletes the corresponding channel of contents that don't exist in |desc|.
475 // |desc| can be null. This means that all channels are deleted.
deadbeefcbecd352015-09-23 11:50:27 -0700476 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477
478 // Allocates media channels based on the |desc|. If |desc| doesn't have
479 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
480 // This method will also delete any existing media channels before creating.
481 bool CreateChannels(const cricket::SessionDescription* desc);
482
483 // Helper methods to create media channels.
skvlad6c87a672016-05-17 17:49:52 -0700484 bool CreateVoiceChannel(const cricket::ContentInfo* content,
485 const std::string* bundle_transport);
486 bool CreateVideoChannel(const cricket::ContentInfo* content,
487 const std::string* bundle_transport);
488 bool CreateDataChannel(const cricket::ContentInfo* content,
489 const std::string* bundle_transport);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000490
hbosdf6075a2016-12-19 04:58:02 -0800491 std::unique_ptr<SessionStats> GetStats_n(
492 const ChannelNamePairs& channel_name_pairs);
493
deadbeef953c2ce2017-01-09 14:53:41 -0800494 bool CreateSctpTransport_n(const std::string& content_name,
495 const std::string& transport_name);
496 // For bundling.
497 void ChangeSctpTransport_n(const std::string& transport_name);
498 void DestroySctpTransport_n();
499 // SctpTransport signal handlers. Needed to marshal signals from the network
500 // to signaling thread.
501 void OnSctpTransportReadyToSendData_n();
502 // This may be called with "false" if the direction of the m= section causes
503 // us to tear down the SCTP connection.
504 void OnSctpTransportReadyToSendData_s(bool ready);
505 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
506 const rtc::CopyOnWriteBuffer& payload);
507 // Beyond just firing the signal to the signaling thread, listens to SCTP
508 // CONTROL messages on unused SIDs and processes them as OPEN messages.
509 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
510 const rtc::CopyOnWriteBuffer& payload);
511 void OnSctpStreamClosedRemotely_n(int sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000513 std::string BadStateErrMsg(State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
Peter Thatcher54360512015-07-08 11:08:35 -0700515 void SetIceConnectionReceiving(bool receiving);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000517 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000518 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000519 // Below methods are helper methods which verifies SDP.
520 bool ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
521 cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000522 std::string* err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000523
524 // Check if a call to SetLocalDescription is acceptable with |action|.
525 bool ExpectSetLocalDescription(Action action);
526 // Check if a call to SetRemoteDescription is acceptable with |action|.
527 bool ExpectSetRemoteDescription(Action action);
528 // Verifies a=setup attribute as per RFC 5763.
529 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
530 Action action);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000531
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000532 // Returns true if we are ready to push down the remote candidate.
533 // |remote_desc| is the new remote description, or NULL if the current remote
534 // description should be used. Output |valid| is true if the candidate media
535 // index is valid.
536 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
537 const SessionDescriptionInterface* remote_desc,
538 bool* valid);
539
deadbeef7af91dd2016-12-13 11:29:11 -0800540 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
541 // this session.
542 bool SrtpRequired() const;
543
deadbeef953c2ce2017-01-09 14:53:41 -0800544 // TransportController signal handlers.
deadbeefcbecd352015-09-23 11:50:27 -0700545 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
546 void OnTransportControllerReceiving(bool receiving);
547 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
548 void OnTransportControllerCandidatesGathered(
549 const std::string& transport_name,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700550 const std::vector<cricket::Candidate>& candidates);
551 void OnTransportControllerCandidatesRemoved(
552 const std::vector<cricket::Candidate>& candidates);
deadbeef953c2ce2017-01-09 14:53:41 -0800553 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
deadbeefcbecd352015-09-23 11:50:27 -0700554
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000555 std::string GetSessionErrorMsg();
556
deadbeefcbecd352015-09-23 11:50:27 -0700557 // Invoked when TransportController connection completion is signaled.
558 // Reports stats for all transports in use.
559 void ReportTransportStats();
560
561 // Gather the usage of IPv4/IPv6 as best connection.
jbauchac8869e2015-07-03 01:36:14 -0700562 void ReportBestConnectionState(const cricket::TransportStats& stats);
563
564 void ReportNegotiatedCiphers(const cricket::TransportStats& stats);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000565
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200566 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700567
zhihuang9763d562016-08-05 11:14:50 -0700568 const std::string GetTransportName(const std::string& content_name);
569
deadbeefac22f702017-01-12 21:59:29 -0800570 void DestroyRtcpTransport_n(const std::string& transport_name);
Steve Anton169629a2017-08-30 17:36:36 -0700571 void RemoveAndDestroyVideoChannel(cricket::VideoChannel* video_channel);
572 void DestroyVideoChannel(cricket::VideoChannel* video_channel);
573 void RemoveAndDestroyVoiceChannel(cricket::VoiceChannel* voice_channel);
574 void DestroyVoiceChannel(cricket::VoiceChannel* voice_channel);
zhihuangf5b251b2017-01-12 19:37:48 -0800575 void DestroyDataChannel();
576
zhihuang9763d562016-08-05 11:14:50 -0700577 rtc::Thread* const network_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700578 rtc::Thread* const worker_thread_;
danilchape9021a32016-05-17 01:52:02 -0700579 rtc::Thread* const signaling_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700580
581 State state_ = STATE_INIT;
582 Error error_ = ERROR_NONE;
583 std::string error_desc_;
584
585 const std::string sid_;
586 bool initial_offerer_ = false;
587
hbosdf6075a2016-12-19 04:58:02 -0800588 const std::unique_ptr<cricket::TransportController> transport_controller_;
deadbeef953c2ce2017-01-09 14:53:41 -0800589 const std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
nisseeaabdf62017-05-05 02:23:02 -0700590 const cricket::MediaConfig media_config_;
591 RtcEventLog* event_log_;
592 Call* call_;
Steve Anton169629a2017-08-30 17:36:36 -0700593 // TODO(steveanton): voice_channels_ and video_channels_ used to be a single
594 // VoiceChannel/VideoChannel respectively but are being changed to support
595 // multiple m= lines in unified plan. But until more work is done, these can
596 // only have 0 or 1 channel each.
597 // These channels are owned by ChannelManager.
598 std::vector<cricket::VoiceChannel*> voice_channels_;
599 std::vector<cricket::VideoChannel*> video_channels_;
deadbeef953c2ce2017-01-09 14:53:41 -0800600 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
601 // when using SCTP.
Steve Anton169629a2017-08-30 17:36:36 -0700602 // TODO(steveanton): This should be changed to a bare pointer because
603 // WebRtcSession doesn't actually own the RtpDataChannel
604 // (ChannelManager does).
deadbeef953c2ce2017-01-09 14:53:41 -0800605 std::unique_ptr<cricket::RtpDataChannel> rtp_data_channel_;
606
607 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
608 // |sctp_transport_name_| keeps track of what DTLS transport the SCTP
609 // transport is using (which can change due to bundling).
610 rtc::Optional<std::string> sctp_transport_name_;
611 // |sctp_content_name_| is the content name (MID) in SDP.
612 rtc::Optional<std::string> sctp_content_name_;
613 // Value cached on signaling thread. Only updated when SctpReadyToSendData
614 // fires on the signaling thread.
615 bool sctp_ready_to_send_data_ = false;
616 // Same as signals provided by SctpTransport, but these are guaranteed to
617 // fire on the signaling thread, whereas SctpTransport fires on the networking
618 // thread.
619 // |sctp_invoker_| is used so that any signals queued on the signaling thread
620 // from the network thread are immediately discarded if the SctpTransport is
621 // destroyed (due to m= section being rejected).
622 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
623 // are marshalled to the right thread. Could almost use proxy.h for this,
624 // but it doesn't have a mechanism for marshalling sigslot::signals
625 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
626 sigslot::signal1<bool> SignalSctpReadyToSendData;
627 sigslot::signal2<const cricket::ReceiveDataParams&,
628 const rtc::CopyOnWriteBuffer&>
629 SignalSctpDataReceived;
630 sigslot::signal1<int> SignalSctpStreamClosedRemotely;
631
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 cricket::ChannelManager* channel_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 IceObserver* ice_observer_;
634 PeerConnectionInterface::IceConnectionState ice_connection_state_;
Peter Thatcher54360512015-07-08 11:08:35 -0700635 bool ice_connection_receiving_;
deadbeeffe4a8a42016-12-20 17:56:17 -0800636 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
637 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
638 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
639 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 // If the remote peer is using a older version of implementation.
641 bool older_version_remote_peer_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000642 bool dtls_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 // Specifies which kind of data channel is allowed. This is controlled
644 // by the chrome command-line flag and constraints:
645 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
646 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
647 // not set or false, SCTP is allowed (DCT_SCTP);
648 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
649 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
zhihuang9763d562016-08-05 11:14:50 -0700650 // The data channel type could be DCT_QUIC if the QUIC data channel is
651 // enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 cricket::DataChannelType data_channel_type_;
deadbeef0ed85b22016-02-23 17:24:52 -0800653 // List of content names for which the remote side triggered an ICE restart.
654 std::set<std::string> pending_ice_restarts_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000655
kwibergd1fe2812016-04-27 06:47:29 -0700656 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000657
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000658 // Member variables for caching global options.
659 cricket::AudioOptions audio_options_;
660 cricket::VideoOptions video_options_;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000661 MetricsObserverInterface* metrics_observer_;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000662
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000663 // Declares the bundle policy for the WebRTCSession.
664 PeerConnectionInterface::BundlePolicy bundle_policy_;
665
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700666 // Declares the RTCP mux policy for the WebRTCSession.
667 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy_;
668
zhihuang184a3fd2016-06-14 11:47:14 -0700669 bool received_first_video_packet_ = false;
670 bool received_first_audio_packet_ = false;
671
zhihuang9763d562016-08-05 11:14:50 -0700672#ifdef HAVE_QUIC
673 std::unique_ptr<QuicDataTransport> quic_data_transport_;
674#endif // HAVE_QUIC
675
henrikg3c089d72015-09-16 05:37:44 -0700676 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSession);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000677};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678} // namespace webrtc
679
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200680#endif // PC_WEBRTCSESSION_H_