blob: 41aca9ec21b4f66d3d693d2cd8754d9b11c0bfb6 [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
ossu7bb87ee2017-01-23 04:56:25 -080011#ifndef WEBRTC_PC_WEBRTCSESSION_H_
12#define WEBRTC_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
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/peerconnectioninterface.h"
20#include "webrtc/api/statstypes.h"
kwiberg4485ffb2016-04-26 08:14:39 -070021#include "webrtc/base/constructormagic.h"
deadbeef953c2ce2017-01-09 14:53:41 -080022#include "webrtc/base/optional.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000023#include "webrtc/base/sigslot.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020024#include "webrtc/base/sslidentity.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000025#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080026#include "webrtc/media/base/mediachannel.h"
Honghai Zhang7fb69db2016-03-14 11:59:18 -070027#include "webrtc/p2p/base/candidate.h"
Tommif888bb52015-12-12 01:37:01 +010028#include "webrtc/p2p/base/transportcontroller.h"
ossu7bb87ee2017-01-23 04:56:25 -080029#include "webrtc/pc/datachannel.h"
ossu7bb87ee2017-01-23 04:56:25 -080030#include "webrtc/pc/mediacontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010031#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
zhihuang9763d562016-08-05 11:14:50 -070033#ifdef HAVE_QUIC
ossu7bb87ee2017-01-23 04:56:25 -080034#include "webrtc/pc/quicdatatransport.h"
zhihuang9763d562016-08-05 11:14:50 -070035#endif // HAVE_QUIC
36
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037namespace cricket {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000038
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039class ChannelManager;
deadbeef953c2ce2017-01-09 14:53:41 -080040class RtpDataChannel;
41class SctpTransportInternal;
42class SctpTransportInternalFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043class StatsReport;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044class VideoChannel;
45class VoiceChannel;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000046
zhihuang9763d562016-08-05 11:14:50 -070047#ifdef HAVE_QUIC
48class QuicTransportChannel;
49#endif // HAVE_QUIC
50
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051} // namespace cricket
52
53namespace webrtc {
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000054
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055class IceRestartAnswerLatch;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000056class JsepIceCandidate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057class MediaStreamSignaling;
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[];
64extern const char kMlineMismatch[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065extern const char kPushDownTDFailed[];
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000066extern const char kSdpWithoutDtlsFingerprint[];
67extern const char kSdpWithoutSdesCrypto[];
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000068extern const char kSdpWithoutIceUfragPwd[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000069extern const char kSdpWithoutSdesAndDtlsDisabled[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070extern const char kSessionError[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000071extern const char kSessionErrorDesc[];
deadbeef953c2ce2017-01-09 14:53:41 -080072extern const char kDtlsSrtpSetupFailureRtp[];
73extern const char kDtlsSrtpSetupFailureRtcp[];
deadbeefcbecd352015-09-23 11:50:27 -070074extern const char kEnableBundleFailed[];
75
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000076// Maximum number of received video streams that will be processed by webrtc
77// even if they are not signalled beforehand.
78extern const int kMaxUnsignalledRecvStreams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
80// ICE state callback interface.
81class IceObserver {
82 public:
wu@webrtc.org364f2042013-11-20 21:49:41 +000083 IceObserver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 // Called any time the IceConnectionState changes
zstein6dfd53a2017-03-06 13:49:03 -080085 virtual void OnIceConnectionStateChange(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 PeerConnectionInterface::IceConnectionState new_state) {}
87 // Called any time the IceGatheringState changes
88 virtual void OnIceGatheringChange(
89 PeerConnectionInterface::IceGatheringState new_state) {}
90 // New Ice candidate have been found.
91 virtual void OnIceCandidate(const IceCandidateInterface* candidate) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092
Honghai Zhang7fb69db2016-03-14 11:59:18 -070093 // Some local ICE candidates have been removed.
94 virtual void OnIceCandidatesRemoved(
95 const std::vector<cricket::Candidate>& candidates) = 0;
96
Peter Thatcher54360512015-07-08 11:08:35 -070097 // Called whenever the state changes between receiving and not receiving.
98 virtual void OnIceConnectionReceivingChange(bool receiving) {}
99
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 protected:
101 ~IceObserver() {}
wu@webrtc.org364f2042013-11-20 21:49:41 +0000102
103 private:
henrikg3c089d72015-09-16 05:37:44 -0700104 RTC_DISALLOW_COPY_AND_ASSIGN(IceObserver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105};
106
deadbeefd59daf82015-10-14 15:02:44 -0700107// Statistics for all the transports of the session.
108typedef std::map<std::string, cricket::TransportStats> TransportStatsMap;
109typedef std::map<std::string, std::string> ProxyTransportMap;
110
111// TODO(pthatcher): Think of a better name for this. We already have
112// a TransportStats in transport.h. Perhaps TransportsStats?
113struct SessionStats {
114 ProxyTransportMap proxy_to_transport;
115 TransportStatsMap transport_stats;
116};
117
hbosdf6075a2016-12-19 04:58:02 -0800118struct ChannelNamePair {
119 ChannelNamePair(
120 const std::string& content_name, const std::string& transport_name)
121 : content_name(content_name), transport_name(transport_name) {}
122 std::string content_name;
123 std::string transport_name;
124};
125
126struct ChannelNamePairs {
127 rtc::Optional<ChannelNamePair> voice;
128 rtc::Optional<ChannelNamePair> video;
129 rtc::Optional<ChannelNamePair> data;
130};
131
deadbeefd59daf82015-10-14 15:02:44 -0700132// A WebRtcSession manages general session state. This includes negotiation
133// of both the application-level and network-level protocols: the former
134// defines what will be sent and the latter defines how it will be sent. Each
135// network-level protocol is represented by a Transport object. Each Transport
136// participates in the network-level negotiation. The individual streams of
137// packets are represented by TransportChannels. The application-level protocol
138// is represented by SessionDecription objects.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700139class WebRtcSession :
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700140 public DataChannelProviderInterface,
141 public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 public:
deadbeefd59daf82015-10-14 15:02:44 -0700143 enum State {
144 STATE_INIT = 0,
145 STATE_SENTOFFER, // Sent offer, waiting for answer.
146 STATE_RECEIVEDOFFER, // Received an offer. Need to send answer.
147 STATE_SENTPRANSWER, // Sent provisional answer. Need to send answer.
148 STATE_RECEIVEDPRANSWER, // Received provisional answer, waiting for answer.
149 STATE_INPROGRESS, // Offer/answer exchange completed.
150 STATE_CLOSED, // Close() was called.
151 };
152
153 enum Error {
154 ERROR_NONE = 0, // no error
155 ERROR_CONTENT = 1, // channel errors in SetLocalContent/SetRemoteContent
156 ERROR_TRANSPORT = 2, // transport error of some kind
157 };
158
deadbeef953c2ce2017-01-09 14:53:41 -0800159 // |sctp_factory| may be null, in which case SCTP is treated as unsupported.
zhihuang29ff8442016-07-27 11:07:25 -0700160 WebRtcSession(
161 webrtc::MediaControllerInterface* media_controller,
162 rtc::Thread* network_thread,
163 rtc::Thread* worker_thread,
164 rtc::Thread* signaling_thread,
165 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800166 std::unique_ptr<cricket::TransportController> transport_controller,
167 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 virtual ~WebRtcSession();
169
deadbeefd59daf82015-10-14 15:02:44 -0700170 // These are const to allow them to be called from const methods.
zhihuang9763d562016-08-05 11:14:50 -0700171 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700172 rtc::Thread* worker_thread() const { return worker_thread_; }
danilchape9021a32016-05-17 01:52:02 -0700173 rtc::Thread* signaling_thread() const { return signaling_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700174
175 // The ID of this session.
176 const std::string& id() const { return sid_; }
177
Henrik Lundin64dad832015-05-11 12:44:23 +0200178 bool Initialize(
179 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200180 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200181 const PeerConnectionInterface::RTCConfiguration& rtc_configuration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 // Deletes the voice, video and data channel and changes the session state
deadbeefd59daf82015-10-14 15:02:44 -0700183 // to STATE_CLOSED.
184 void Close();
185
186 // Returns true if we were the initial offerer.
187 bool initial_offerer() const { return initial_offerer_; }
188
189 // Returns the current state of the session. See the enum above for details.
190 // Each time the state changes, we will fire this signal.
191 State state() const { return state_; }
192 sigslot::signal2<WebRtcSession*, State> SignalState;
193
194 // Returns the last error in the session. See the enum above for details.
195 Error error() const { return error_; }
196 const std::string& error_desc() const { return error_desc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197
198 void RegisterIceObserver(IceObserver* observer) {
199 ice_observer_ = observer;
200 }
201
deadbeef953c2ce2017-01-09 14:53:41 -0800202 // Exposed for stats collecting.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 virtual cricket::VoiceChannel* voice_channel() {
204 return voice_channel_.get();
205 }
206 virtual cricket::VideoChannel* video_channel() {
207 return video_channel_.get();
208 }
deadbeef953c2ce2017-01-09 14:53:41 -0800209 // Only valid when using deprecated RTP data channels.
210 virtual cricket::RtpDataChannel* rtp_data_channel() {
211 return rtp_data_channel_.get();
212 }
213 virtual rtc::Optional<std::string> sctp_content_name() const {
214 return sctp_content_name_;
215 }
216 virtual rtc::Optional<std::string> sctp_transport_name() const {
217 return sctp_transport_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 }
219
deadbeef0ed85b22016-02-23 17:24:52 -0800220 cricket::BaseChannel* GetChannel(const std::string& content_name);
221
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000222 cricket::SecurePolicy SdesPolicy() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223
deadbeef953c2ce2017-01-09 14:53:41 -0800224 // Get current SSL role used by SCTP's underlying transport.
225 bool GetSctpSslRole(rtc::SSLRole* role);
226 // Get SSL role for an arbitrary m= section (handles bundling correctly).
227 // TODO(deadbeef): This is only used internally by the session description
228 // factory, it shouldn't really be public).
229 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000230
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000231 void CreateOffer(
232 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700233 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
234 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000235 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700236 const cricket::MediaSessionOptions& session_options);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000237 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 bool SetLocalDescription(SessionDescriptionInterface* desc,
239 std::string* err_desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000240 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 bool SetRemoteDescription(SessionDescriptionInterface* desc,
242 std::string* err_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800243
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 bool ProcessIceMessage(const IceCandidateInterface* ice_candidate);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000245
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700246 bool RemoveRemoteIceCandidates(
247 const std::vector<cricket::Candidate>& candidates);
248
honghaiz1f429e32015-09-28 07:57:34 -0700249 cricket::IceConfig ParseIceConfig(
250 const PeerConnectionInterface::RTCConfiguration& config) const;
251
deadbeefd59daf82015-10-14 15:02:44 -0700252 void SetIceConfig(const cricket::IceConfig& ice_config);
253
254 // Start gathering candidates for any new transports, or transports doing an
255 // ICE restart.
256 void MaybeStartGathering();
257
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 const SessionDescriptionInterface* local_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800259 return pending_local_description_ ? pending_local_description_.get()
260 : current_local_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 }
262 const SessionDescriptionInterface* remote_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800263 return pending_remote_description_ ? pending_remote_description_.get()
264 : current_remote_description_.get();
265 }
266 const SessionDescriptionInterface* current_local_description() const {
267 return current_local_description_.get();
268 }
269 const SessionDescriptionInterface* current_remote_description() const {
270 return current_remote_description_.get();
271 }
272 const SessionDescriptionInterface* pending_local_description() const {
273 return pending_local_description_.get();
274 }
275 const SessionDescriptionInterface* pending_remote_description() const {
276 return pending_remote_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 }
278
279 // Get the id used as a media stream track's "id" field from ssrc.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200280 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
281 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000282
wu@webrtc.org78187522013-10-07 23:32:02 +0000283 // Implements DataChannelProviderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000284 bool SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700285 const rtc::CopyOnWriteBuffer& payload,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000286 cricket::SendDataResult* result) override;
287 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
288 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
289 void AddSctpDataStream(int sid) override;
290 void RemoveSctpDataStream(int sid) override;
291 bool ReadyToSendData() const override;
wu@webrtc.org78187522013-10-07 23:32:02 +0000292
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000293 // Returns stats for all channels of all transports.
294 // This avoids exposing the internal structures used to track them.
hbosdf6075a2016-12-19 04:58:02 -0800295 // The parameterless version creates |ChannelNamePairs| from |voice_channel|,
296 // |video_channel| and |voice_channel| if available - this requires it to be
297 // called on the signaling thread - and invokes the other |GetStats|. The
298 // other |GetStats| can be invoked on any thread; if not invoked on the
299 // network thread a thread hop will happen.
300 std::unique_ptr<SessionStats> GetStats_s();
301 virtual std::unique_ptr<SessionStats> GetStats(
302 const ChannelNamePairs& channel_name_pairs);
deadbeefcbecd352015-09-23 11:50:27 -0700303
304 // virtual so it can be mocked in unit tests
305 virtual bool GetLocalCertificate(
306 const std::string& transport_name,
307 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
308
309 // Caller owns returned certificate
jbauch555604a2016-04-26 03:13:22 -0700310 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -0700311 const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700312
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 cricket::DataChannelType data_channel_type() const;
314
deadbeefd1a38b52016-12-10 13:15:33 -0800315 // Returns true if there was an ICE restart initiated by the remote offer.
deadbeef0ed85b22016-02-23 17:24:52 -0800316 bool IceRestartPending(const std::string& content_name) const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000317
deadbeefd1a38b52016-12-10 13:15:33 -0800318 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
319 // set, offers should generate new ufrags/passwords until an ICE restart
320 // occurs.
321 void SetNeedsIceRestartFlag();
322 // Returns true if the ICE restart flag above was set, and no ICE restart has
323 // occurred yet for this transport (by applying a local description with
324 // changed ufrag/password). If the transport has been deleted as a result of
325 // bundling, returns false.
326 bool NeedsIceRestart(const std::string& content_name) const;
327
Henrik Boströmd8281982015-08-27 10:12:24 +0200328 // Called when an RTCCertificate is generated or retrieved by
wu@webrtc.org91053e72013-08-10 07:18:04 +0000329 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
Henrik Boströmd8281982015-08-27 10:12:24 +0200330 void OnCertificateReady(
331 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
deadbeef953c2ce2017-01-09 14:53:41 -0800332 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000333
334 // For unit test.
Henrik Boströmd8281982015-08-27 10:12:24 +0200335 bool waiting_for_certificate_for_testing() const;
deadbeefcbecd352015-09-23 11:50:27 -0700336 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000337
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000338 void set_metrics_observer(
339 webrtc::MetricsObserverInterface* metrics_observer) {
340 metrics_observer_ = metrics_observer;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700341 transport_controller_->SetMetricsObserver(metrics_observer);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000342 }
343
deadbeef953c2ce2017-01-09 14:53:41 -0800344 // Called when voice_channel_, video_channel_ and
345 // rtp_data_channel_/sctp_transport_ are created and destroyed. As a result
346 // of, for example, setting a new description.
deadbeefab9b2d12015-10-14 11:33:11 -0700347 sigslot::signal0<> SignalVoiceChannelCreated;
348 sigslot::signal0<> SignalVoiceChannelDestroyed;
349 sigslot::signal0<> SignalVideoChannelCreated;
350 sigslot::signal0<> SignalVideoChannelDestroyed;
351 sigslot::signal0<> SignalDataChannelCreated;
352 sigslot::signal0<> SignalDataChannelDestroyed;
353
354 // Called when a valid data channel OPEN message is received.
355 // std::string represents the data channel label.
356 sigslot::signal2<const std::string&, const InternalDataChannelInit&>
357 SignalDataChannelOpenMessage;
zhihuang9763d562016-08-05 11:14:50 -0700358#ifdef HAVE_QUIC
359 QuicDataTransport* quic_data_transport() {
360 return quic_data_transport_.get();
361 }
362#endif // HAVE_QUIC
deadbeefab9b2d12015-10-14 11:33:11 -0700363
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 private:
365 // Indicates the type of SessionDescription in a call to SetLocalDescription
366 // and SetRemoteDescription.
367 enum Action {
368 kOffer,
369 kPrAnswer,
370 kAnswer,
371 };
wu@webrtc.org91053e72013-08-10 07:18:04 +0000372
deadbeeffe4a8a42016-12-20 17:56:17 -0800373 // Non-const versions of local_description()/remote_description(), for use
374 // internally.
375 SessionDescriptionInterface* mutable_local_description() {
376 return pending_local_description_ ? pending_local_description_.get()
377 : current_local_description_.get();
378 }
379 SessionDescriptionInterface* mutable_remote_description() {
380 return pending_remote_description_ ? pending_remote_description_.get()
381 : current_remote_description_.get();
382 }
383
deadbeefd59daf82015-10-14 15:02:44 -0700384 // Log session state.
385 void LogState(State old_state, State new_state);
386
387 // Updates the state, signaling if necessary.
388 virtual void SetState(State state);
389
390 // Updates the error state, signaling if necessary.
391 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|.
392 virtual void SetError(Error error, const std::string& error_desc);
393
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 bool UpdateSessionState(Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 std::string* err_desc);
396 static Action GetAction(const std::string& type);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000397 // Push the media parts of the local or remote session description
398 // down to all of the channels.
399 bool PushdownMediaDescription(cricket::ContentAction action,
400 cricket::ContentSource source,
401 std::string* error_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800402 bool PushdownSctpParameters_n(cricket::ContentSource source);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000403
deadbeefd59daf82015-10-14 15:02:44 -0700404 bool PushdownTransportDescription(cricket::ContentSource source,
405 cricket::ContentAction action,
406 std::string* error_desc);
407
408 // Helper methods to push local and remote transport descriptions.
409 bool PushdownLocalTransportDescription(
410 const cricket::SessionDescription* sdesc,
411 cricket::ContentAction action,
412 std::string* error_desc);
413 bool PushdownRemoteTransportDescription(
414 const cricket::SessionDescription* sdesc,
415 cricket::ContentAction action,
416 std::string* error_desc);
417
418 // Returns true and the TransportInfo of the given |content_name|
419 // from |description|. Returns false if it's not available.
420 static bool GetTransportDescription(
421 const cricket::SessionDescription* description,
422 const std::string& content_name,
423 cricket::TransportDescription* info);
424
skvlad6c87a672016-05-17 17:49:52 -0700425 // Returns the name of the transport channel when BUNDLE is enabled, or
426 // nullptr if the channel is not part of any bundle.
427 const std::string* GetBundleTransportName(
428 const cricket::ContentInfo* content,
429 const cricket::ContentGroup* bundle);
430
deadbeefcbecd352015-09-23 11:50:27 -0700431 // Cause all the BaseChannels in the bundle group to have the same
432 // transport channel.
433 bool EnableBundle(const cricket::ContentGroup& bundle);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 // Enables media channels to allow sending of media.
436 void EnableChannels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 // Returns the media index for a local ice candidate given the content name.
438 // Returns false if the local session description does not have a media
439 // content called |content_name|.
440 bool GetLocalCandidateMediaIndex(const std::string& content_name,
441 int* sdp_mline_index);
442 // Uses all remote candidates in |remote_desc| in this session.
443 bool UseCandidatesInSessionDescription(
444 const SessionDescriptionInterface* remote_desc);
445 // Uses |candidate| in this session.
446 bool UseCandidate(const IceCandidateInterface* candidate);
447 // Deletes the corresponding channel of contents that don't exist in |desc|.
448 // |desc| can be null. This means that all channels are deleted.
deadbeefcbecd352015-09-23 11:50:27 -0700449 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450
451 // Allocates media channels based on the |desc|. If |desc| doesn't have
452 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
453 // This method will also delete any existing media channels before creating.
454 bool CreateChannels(const cricket::SessionDescription* desc);
455
456 // Helper methods to create media channels.
skvlad6c87a672016-05-17 17:49:52 -0700457 bool CreateVoiceChannel(const cricket::ContentInfo* content,
458 const std::string* bundle_transport);
459 bool CreateVideoChannel(const cricket::ContentInfo* content,
460 const std::string* bundle_transport);
461 bool CreateDataChannel(const cricket::ContentInfo* content,
462 const std::string* bundle_transport);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000463
hbosdf6075a2016-12-19 04:58:02 -0800464 std::unique_ptr<SessionStats> GetStats_n(
465 const ChannelNamePairs& channel_name_pairs);
466
deadbeef953c2ce2017-01-09 14:53:41 -0800467 bool CreateSctpTransport_n(const std::string& content_name,
468 const std::string& transport_name);
469 // For bundling.
470 void ChangeSctpTransport_n(const std::string& transport_name);
471 void DestroySctpTransport_n();
472 // SctpTransport signal handlers. Needed to marshal signals from the network
473 // to signaling thread.
474 void OnSctpTransportReadyToSendData_n();
475 // This may be called with "false" if the direction of the m= section causes
476 // us to tear down the SCTP connection.
477 void OnSctpTransportReadyToSendData_s(bool ready);
478 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
479 const rtc::CopyOnWriteBuffer& payload);
480 // Beyond just firing the signal to the signaling thread, listens to SCTP
481 // CONTROL messages on unused SIDs and processes them as OPEN messages.
482 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
483 const rtc::CopyOnWriteBuffer& payload);
484 void OnSctpStreamClosedRemotely_n(int sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000486 std::string BadStateErrMsg(State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
Peter Thatcher54360512015-07-08 11:08:35 -0700488 void SetIceConnectionReceiving(bool receiving);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000490 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000491 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000492 // Below methods are helper methods which verifies SDP.
493 bool ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
494 cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000495 std::string* err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000496
497 // Check if a call to SetLocalDescription is acceptable with |action|.
498 bool ExpectSetLocalDescription(Action action);
499 // Check if a call to SetRemoteDescription is acceptable with |action|.
500 bool ExpectSetRemoteDescription(Action action);
501 // Verifies a=setup attribute as per RFC 5763.
502 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
503 Action action);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000504
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000505 // Returns true if we are ready to push down the remote candidate.
506 // |remote_desc| is the new remote description, or NULL if the current remote
507 // description should be used. Output |valid| is true if the candidate media
508 // index is valid.
509 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
510 const SessionDescriptionInterface* remote_desc,
511 bool* valid);
512
deadbeef7af91dd2016-12-13 11:29:11 -0800513 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
514 // this session.
515 bool SrtpRequired() const;
516
deadbeef953c2ce2017-01-09 14:53:41 -0800517 // TransportController signal handlers.
deadbeefcbecd352015-09-23 11:50:27 -0700518 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
519 void OnTransportControllerReceiving(bool receiving);
520 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
521 void OnTransportControllerCandidatesGathered(
522 const std::string& transport_name,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700523 const std::vector<cricket::Candidate>& candidates);
524 void OnTransportControllerCandidatesRemoved(
525 const std::vector<cricket::Candidate>& candidates);
deadbeef953c2ce2017-01-09 14:53:41 -0800526 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
deadbeefcbecd352015-09-23 11:50:27 -0700527
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000528 std::string GetSessionErrorMsg();
529
deadbeefcbecd352015-09-23 11:50:27 -0700530 // Invoked when TransportController connection completion is signaled.
531 // Reports stats for all transports in use.
532 void ReportTransportStats();
533
534 // Gather the usage of IPv4/IPv6 as best connection.
jbauchac8869e2015-07-03 01:36:14 -0700535 void ReportBestConnectionState(const cricket::TransportStats& stats);
536
537 void ReportNegotiatedCiphers(const cricket::TransportStats& stats);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000538
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200539 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700540
zhihuang9763d562016-08-05 11:14:50 -0700541 const std::string GetTransportName(const std::string& content_name);
542
deadbeefac22f702017-01-12 21:59:29 -0800543 void DestroyRtcpTransport_n(const std::string& transport_name);
zhihuangf5b251b2017-01-12 19:37:48 -0800544 void DestroyVideoChannel();
545 void DestroyVoiceChannel();
546 void DestroyDataChannel();
547
zhihuang9763d562016-08-05 11:14:50 -0700548 rtc::Thread* const network_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700549 rtc::Thread* const worker_thread_;
danilchape9021a32016-05-17 01:52:02 -0700550 rtc::Thread* const signaling_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700551
552 State state_ = STATE_INIT;
553 Error error_ = ERROR_NONE;
554 std::string error_desc_;
555
556 const std::string sid_;
557 bool initial_offerer_ = false;
558
hbosdf6075a2016-12-19 04:58:02 -0800559 const std::unique_ptr<cricket::TransportController> transport_controller_;
deadbeef953c2ce2017-01-09 14:53:41 -0800560 const std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
stefanc1aeaf02015-10-15 07:26:07 -0700561 MediaControllerInterface* media_controller_;
kwibergd1fe2812016-04-27 06:47:29 -0700562 std::unique_ptr<cricket::VoiceChannel> voice_channel_;
563 std::unique_ptr<cricket::VideoChannel> video_channel_;
deadbeef953c2ce2017-01-09 14:53:41 -0800564 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
565 // when using SCTP.
566 std::unique_ptr<cricket::RtpDataChannel> rtp_data_channel_;
567
568 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
569 // |sctp_transport_name_| keeps track of what DTLS transport the SCTP
570 // transport is using (which can change due to bundling).
571 rtc::Optional<std::string> sctp_transport_name_;
572 // |sctp_content_name_| is the content name (MID) in SDP.
573 rtc::Optional<std::string> sctp_content_name_;
574 // Value cached on signaling thread. Only updated when SctpReadyToSendData
575 // fires on the signaling thread.
576 bool sctp_ready_to_send_data_ = false;
577 // Same as signals provided by SctpTransport, but these are guaranteed to
578 // fire on the signaling thread, whereas SctpTransport fires on the networking
579 // thread.
580 // |sctp_invoker_| is used so that any signals queued on the signaling thread
581 // from the network thread are immediately discarded if the SctpTransport is
582 // destroyed (due to m= section being rejected).
583 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
584 // are marshalled to the right thread. Could almost use proxy.h for this,
585 // but it doesn't have a mechanism for marshalling sigslot::signals
586 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
587 sigslot::signal1<bool> SignalSctpReadyToSendData;
588 sigslot::signal2<const cricket::ReceiveDataParams&,
589 const rtc::CopyOnWriteBuffer&>
590 SignalSctpDataReceived;
591 sigslot::signal1<int> SignalSctpStreamClosedRemotely;
592
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 cricket::ChannelManager* channel_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 IceObserver* ice_observer_;
595 PeerConnectionInterface::IceConnectionState ice_connection_state_;
Peter Thatcher54360512015-07-08 11:08:35 -0700596 bool ice_connection_receiving_;
deadbeeffe4a8a42016-12-20 17:56:17 -0800597 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
598 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
599 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
600 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 // If the remote peer is using a older version of implementation.
602 bool older_version_remote_peer_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000603 bool dtls_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 // Specifies which kind of data channel is allowed. This is controlled
605 // by the chrome command-line flag and constraints:
606 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
607 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
608 // not set or false, SCTP is allowed (DCT_SCTP);
609 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
610 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
zhihuang9763d562016-08-05 11:14:50 -0700611 // The data channel type could be DCT_QUIC if the QUIC data channel is
612 // enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 cricket::DataChannelType data_channel_type_;
deadbeef0ed85b22016-02-23 17:24:52 -0800614 // List of content names for which the remote side triggered an ICE restart.
615 std::set<std::string> pending_ice_restarts_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000616
kwibergd1fe2812016-04-27 06:47:29 -0700617 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000618
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000619 // Member variables for caching global options.
620 cricket::AudioOptions audio_options_;
621 cricket::VideoOptions video_options_;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000622 MetricsObserverInterface* metrics_observer_;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000623
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000624 // Declares the bundle policy for the WebRTCSession.
625 PeerConnectionInterface::BundlePolicy bundle_policy_;
626
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700627 // Declares the RTCP mux policy for the WebRTCSession.
628 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy_;
629
zhihuang184a3fd2016-06-14 11:47:14 -0700630 bool received_first_video_packet_ = false;
631 bool received_first_audio_packet_ = false;
632
zhihuang9763d562016-08-05 11:14:50 -0700633#ifdef HAVE_QUIC
634 std::unique_ptr<QuicDataTransport> quic_data_transport_;
635#endif // HAVE_QUIC
636
henrikg3c089d72015-09-16 05:37:44 -0700637 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSession);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000638};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639} // namespace webrtc
640
ossu7bb87ee2017-01-23 04:56:25 -0800641#endif // WEBRTC_PC_WEBRTCSESSION_H_