blob: 686ac1efe2161ede8dc4db31697906f7b4a2b848 [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
kwiberg84f6a3f2017-09-05 08:43:13 -070019#include "webrtc/api/optional.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/statstypes.h"
stefanf79ade12017-06-02 06:44:03 -070022#include "webrtc/call/call.h"
Honghai Zhang7fb69db2016-03-14 11:59:18 -070023#include "webrtc/p2p/base/candidate.h"
Tommif888bb52015-12-12 01:37:01 +010024#include "webrtc/p2p/base/transportcontroller.h"
ossu7bb87ee2017-01-23 04:56:25 -080025#include "webrtc/pc/datachannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010026#include "webrtc/pc/mediasession.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020027#include "webrtc/rtc_base/constructormagic.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020028#include "webrtc/rtc_base/sigslot.h"
29#include "webrtc/rtc_base/sslidentity.h"
30#include "webrtc/rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
zhihuang9763d562016-08-05 11:14:50 -070032#ifdef HAVE_QUIC
ossu7bb87ee2017-01-23 04:56:25 -080033#include "webrtc/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[];
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.
jbauch81bf7b02017-03-25 08:31:12 -070091 virtual void OnIceCandidate(
92 std::unique_ptr<IceCandidateInterface> candidate) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
Honghai Zhang7fb69db2016-03-14 11:59:18 -070094 // Some local ICE candidates have been removed.
95 virtual void OnIceCandidatesRemoved(
96 const std::vector<cricket::Candidate>& candidates) = 0;
97
Peter Thatcher54360512015-07-08 11:08:35 -070098 // Called whenever the state changes between receiving and not receiving.
99 virtual void OnIceConnectionReceivingChange(bool receiving) {}
100
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 protected:
102 ~IceObserver() {}
wu@webrtc.org364f2042013-11-20 21:49:41 +0000103
104 private:
henrikg3c089d72015-09-16 05:37:44 -0700105 RTC_DISALLOW_COPY_AND_ASSIGN(IceObserver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106};
107
deadbeefd59daf82015-10-14 15:02:44 -0700108// Statistics for all the transports of the session.
109typedef std::map<std::string, cricket::TransportStats> TransportStatsMap;
110typedef std::map<std::string, std::string> ProxyTransportMap;
111
112// TODO(pthatcher): Think of a better name for this. We already have
113// a TransportStats in transport.h. Perhaps TransportsStats?
114struct SessionStats {
115 ProxyTransportMap proxy_to_transport;
116 TransportStatsMap transport_stats;
117};
118
hbosdf6075a2016-12-19 04:58:02 -0800119struct ChannelNamePair {
120 ChannelNamePair(
121 const std::string& content_name, const std::string& transport_name)
122 : content_name(content_name), transport_name(transport_name) {}
123 std::string content_name;
124 std::string transport_name;
125};
126
127struct ChannelNamePairs {
128 rtc::Optional<ChannelNamePair> voice;
129 rtc::Optional<ChannelNamePair> video;
130 rtc::Optional<ChannelNamePair> data;
131};
132
deadbeefd59daf82015-10-14 15:02:44 -0700133// A WebRtcSession manages general session state. This includes negotiation
134// of both the application-level and network-level protocols: the former
135// defines what will be sent and the latter defines how it will be sent. Each
136// network-level protocol is represented by a Transport object. Each Transport
137// participates in the network-level negotiation. The individual streams of
138// packets are represented by TransportChannels. The application-level protocol
139// is represented by SessionDecription objects.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700140class WebRtcSession :
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700141 public DataChannelProviderInterface,
142 public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 public:
deadbeefd59daf82015-10-14 15:02:44 -0700144 enum State {
145 STATE_INIT = 0,
146 STATE_SENTOFFER, // Sent offer, waiting for answer.
147 STATE_RECEIVEDOFFER, // Received an offer. Need to send answer.
148 STATE_SENTPRANSWER, // Sent provisional answer. Need to send answer.
149 STATE_RECEIVEDPRANSWER, // Received provisional answer, waiting for answer.
150 STATE_INPROGRESS, // Offer/answer exchange completed.
151 STATE_CLOSED, // Close() was called.
152 };
153
154 enum Error {
155 ERROR_NONE = 0, // no error
156 ERROR_CONTENT = 1, // channel errors in SetLocalContent/SetRemoteContent
157 ERROR_TRANSPORT = 2, // transport error of some kind
158 };
159
deadbeef953c2ce2017-01-09 14:53:41 -0800160 // |sctp_factory| may be null, in which case SCTP is treated as unsupported.
zhihuang29ff8442016-07-27 11:07:25 -0700161 WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700162 Call* call,
163 cricket::ChannelManager* channel_manager,
164 const cricket::MediaConfig& media_config,
165 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700166 rtc::Thread* network_thread,
167 rtc::Thread* worker_thread,
168 rtc::Thread* signaling_thread,
169 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800170 std::unique_ptr<cricket::TransportController> transport_controller,
171 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 virtual ~WebRtcSession();
173
deadbeefd59daf82015-10-14 15:02:44 -0700174 // These are const to allow them to be called from const methods.
zhihuang9763d562016-08-05 11:14:50 -0700175 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700176 rtc::Thread* worker_thread() const { return worker_thread_; }
danilchape9021a32016-05-17 01:52:02 -0700177 rtc::Thread* signaling_thread() const { return signaling_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700178
179 // The ID of this session.
180 const std::string& id() const { return sid_; }
181
Henrik Lundin64dad832015-05-11 12:44:23 +0200182 bool Initialize(
183 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200184 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Lundin64dad832015-05-11 12:44:23 +0200185 const PeerConnectionInterface::RTCConfiguration& rtc_configuration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 // Deletes the voice, video and data channel and changes the session state
deadbeefd59daf82015-10-14 15:02:44 -0700187 // to STATE_CLOSED.
188 void Close();
189
190 // Returns true if we were the initial offerer.
191 bool initial_offerer() const { return initial_offerer_; }
192
193 // Returns the current state of the session. See the enum above for details.
194 // Each time the state changes, we will fire this signal.
195 State state() const { return state_; }
196 sigslot::signal2<WebRtcSession*, State> SignalState;
197
198 // Returns the last error in the session. See the enum above for details.
199 Error error() const { return error_; }
200 const std::string& error_desc() const { return error_desc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201
202 void RegisterIceObserver(IceObserver* observer) {
203 ice_observer_ = observer;
204 }
205
deadbeef953c2ce2017-01-09 14:53:41 -0800206 // Exposed for stats collecting.
Steve Anton169629a2017-08-30 17:36:36 -0700207 // TODO(steveanton): Switch callers to use the plural form and remove these.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 virtual cricket::VoiceChannel* voice_channel() {
Steve Anton169629a2017-08-30 17:36:36 -0700209 if (voice_channels_.empty()) {
210 return nullptr;
211 } else {
212 return voice_channels_[0];
213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 }
215 virtual cricket::VideoChannel* video_channel() {
Steve Anton169629a2017-08-30 17:36:36 -0700216 if (video_channels_.empty()) {
217 return nullptr;
218 } else {
219 return video_channels_[0];
220 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 }
Steve Anton169629a2017-08-30 17:36:36 -0700222
223 virtual std::vector<cricket::VoiceChannel*> voice_channels() const {
224 return voice_channels_;
225 }
226 virtual std::vector<cricket::VideoChannel*> video_channels() const {
227 return video_channels_;
228 }
229
deadbeef953c2ce2017-01-09 14:53:41 -0800230 // Only valid when using deprecated RTP data channels.
231 virtual cricket::RtpDataChannel* rtp_data_channel() {
232 return rtp_data_channel_.get();
233 }
234 virtual rtc::Optional<std::string> sctp_content_name() const {
235 return sctp_content_name_;
236 }
237 virtual rtc::Optional<std::string> sctp_transport_name() const {
238 return sctp_transport_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 }
240
deadbeef0ed85b22016-02-23 17:24:52 -0800241 cricket::BaseChannel* GetChannel(const std::string& content_name);
242
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000243 cricket::SecurePolicy SdesPolicy() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244
deadbeef953c2ce2017-01-09 14:53:41 -0800245 // Get current SSL role used by SCTP's underlying transport.
246 bool GetSctpSslRole(rtc::SSLRole* role);
247 // Get SSL role for an arbitrary m= section (handles bundling correctly).
248 // TODO(deadbeef): This is only used internally by the session description
249 // factory, it shouldn't really be public).
250 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000251
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000252 void CreateOffer(
253 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700254 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
255 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000256 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700257 const cricket::MediaSessionOptions& session_options);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000258 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 bool SetLocalDescription(SessionDescriptionInterface* desc,
260 std::string* err_desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000261 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 bool SetRemoteDescription(SessionDescriptionInterface* desc,
263 std::string* err_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800264
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 bool ProcessIceMessage(const IceCandidateInterface* ice_candidate);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000266
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700267 bool RemoveRemoteIceCandidates(
268 const std::vector<cricket::Candidate>& candidates);
269
honghaiz1f429e32015-09-28 07:57:34 -0700270 cricket::IceConfig ParseIceConfig(
271 const PeerConnectionInterface::RTCConfiguration& config) const;
272
deadbeefd59daf82015-10-14 15:02:44 -0700273 void SetIceConfig(const cricket::IceConfig& ice_config);
274
275 // Start gathering candidates for any new transports, or transports doing an
276 // ICE restart.
277 void MaybeStartGathering();
278
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 const SessionDescriptionInterface* local_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800280 return pending_local_description_ ? pending_local_description_.get()
281 : current_local_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 }
283 const SessionDescriptionInterface* remote_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800284 return pending_remote_description_ ? pending_remote_description_.get()
285 : current_remote_description_.get();
286 }
287 const SessionDescriptionInterface* current_local_description() const {
288 return current_local_description_.get();
289 }
290 const SessionDescriptionInterface* current_remote_description() const {
291 return current_remote_description_.get();
292 }
293 const SessionDescriptionInterface* pending_local_description() const {
294 return pending_local_description_.get();
295 }
296 const SessionDescriptionInterface* pending_remote_description() const {
297 return pending_remote_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 }
299
300 // Get the id used as a media stream track's "id" field from ssrc.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200301 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
302 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000303
wu@webrtc.org78187522013-10-07 23:32:02 +0000304 // Implements DataChannelProviderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000305 bool SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700306 const rtc::CopyOnWriteBuffer& payload,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000307 cricket::SendDataResult* result) override;
308 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
309 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
310 void AddSctpDataStream(int sid) override;
311 void RemoveSctpDataStream(int sid) override;
312 bool ReadyToSendData() const override;
wu@webrtc.org78187522013-10-07 23:32:02 +0000313
stefanf79ade12017-06-02 06:44:03 -0700314 virtual Call::Stats GetCallStats();
315
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000316 // Returns stats for all channels of all transports.
317 // This avoids exposing the internal structures used to track them.
hbosdf6075a2016-12-19 04:58:02 -0800318 // The parameterless version creates |ChannelNamePairs| from |voice_channel|,
319 // |video_channel| and |voice_channel| if available - this requires it to be
320 // called on the signaling thread - and invokes the other |GetStats|. The
321 // other |GetStats| can be invoked on any thread; if not invoked on the
322 // network thread a thread hop will happen.
323 std::unique_ptr<SessionStats> GetStats_s();
324 virtual std::unique_ptr<SessionStats> GetStats(
325 const ChannelNamePairs& channel_name_pairs);
deadbeefcbecd352015-09-23 11:50:27 -0700326
327 // virtual so it can be mocked in unit tests
328 virtual bool GetLocalCertificate(
329 const std::string& transport_name,
330 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
331
332 // Caller owns returned certificate
jbauch555604a2016-04-26 03:13:22 -0700333 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -0700334 const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700335
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 cricket::DataChannelType data_channel_type() const;
337
deadbeefd1a38b52016-12-10 13:15:33 -0800338 // Returns true if there was an ICE restart initiated by the remote offer.
deadbeef0ed85b22016-02-23 17:24:52 -0800339 bool IceRestartPending(const std::string& content_name) const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000340
deadbeefd1a38b52016-12-10 13:15:33 -0800341 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
342 // set, offers should generate new ufrags/passwords until an ICE restart
343 // occurs.
344 void SetNeedsIceRestartFlag();
345 // Returns true if the ICE restart flag above was set, and no ICE restart has
346 // occurred yet for this transport (by applying a local description with
347 // changed ufrag/password). If the transport has been deleted as a result of
348 // bundling, returns false.
349 bool NeedsIceRestart(const std::string& content_name) const;
350
Henrik Boströmd8281982015-08-27 10:12:24 +0200351 // Called when an RTCCertificate is generated or retrieved by
wu@webrtc.org91053e72013-08-10 07:18:04 +0000352 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
Henrik Boströmd8281982015-08-27 10:12:24 +0200353 void OnCertificateReady(
354 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
deadbeef953c2ce2017-01-09 14:53:41 -0800355 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000356
357 // For unit test.
Henrik Boströmd8281982015-08-27 10:12:24 +0200358 bool waiting_for_certificate_for_testing() const;
deadbeefcbecd352015-09-23 11:50:27 -0700359 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000360
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000361 void set_metrics_observer(
362 webrtc::MetricsObserverInterface* metrics_observer) {
363 metrics_observer_ = metrics_observer;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700364 transport_controller_->SetMetricsObserver(metrics_observer);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000365 }
366
deadbeef953c2ce2017-01-09 14:53:41 -0800367 // Called when voice_channel_, video_channel_ and
368 // rtp_data_channel_/sctp_transport_ are created and destroyed. As a result
369 // of, for example, setting a new description.
deadbeefab9b2d12015-10-14 11:33:11 -0700370 sigslot::signal0<> SignalVoiceChannelCreated;
371 sigslot::signal0<> SignalVoiceChannelDestroyed;
372 sigslot::signal0<> SignalVideoChannelCreated;
373 sigslot::signal0<> SignalVideoChannelDestroyed;
374 sigslot::signal0<> SignalDataChannelCreated;
375 sigslot::signal0<> SignalDataChannelDestroyed;
376
377 // Called when a valid data channel OPEN message is received.
378 // std::string represents the data channel label.
379 sigslot::signal2<const std::string&, const InternalDataChannelInit&>
380 SignalDataChannelOpenMessage;
zhihuang9763d562016-08-05 11:14:50 -0700381#ifdef HAVE_QUIC
382 QuicDataTransport* quic_data_transport() {
383 return quic_data_transport_.get();
384 }
385#endif // HAVE_QUIC
deadbeefab9b2d12015-10-14 11:33:11 -0700386
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 private:
388 // Indicates the type of SessionDescription in a call to SetLocalDescription
389 // and SetRemoteDescription.
390 enum Action {
391 kOffer,
392 kPrAnswer,
393 kAnswer,
394 };
wu@webrtc.org91053e72013-08-10 07:18:04 +0000395
Steve Anton18ee1d52017-09-11 11:32:35 -0700396 // Return all managed, non-null channels.
397 std::vector<cricket::BaseChannel*> Channels() const;
398
deadbeeffe4a8a42016-12-20 17:56:17 -0800399 // Non-const versions of local_description()/remote_description(), for use
400 // internally.
401 SessionDescriptionInterface* mutable_local_description() {
402 return pending_local_description_ ? pending_local_description_.get()
403 : current_local_description_.get();
404 }
405 SessionDescriptionInterface* mutable_remote_description() {
406 return pending_remote_description_ ? pending_remote_description_.get()
407 : current_remote_description_.get();
408 }
409
deadbeefd59daf82015-10-14 15:02:44 -0700410 // Log session state.
411 void LogState(State old_state, State new_state);
412
413 // Updates the state, signaling if necessary.
414 virtual void SetState(State state);
415
416 // Updates the error state, signaling if necessary.
417 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|.
418 virtual void SetError(Error error, const std::string& error_desc);
419
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 bool UpdateSessionState(Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 std::string* err_desc);
422 static Action GetAction(const std::string& type);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000423 // Push the media parts of the local or remote session description
424 // down to all of the channels.
425 bool PushdownMediaDescription(cricket::ContentAction action,
426 cricket::ContentSource source,
427 std::string* error_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800428 bool PushdownSctpParameters_n(cricket::ContentSource source);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000429
deadbeefd59daf82015-10-14 15:02:44 -0700430 bool PushdownTransportDescription(cricket::ContentSource source,
431 cricket::ContentAction action,
432 std::string* error_desc);
433
434 // Helper methods to push local and remote transport descriptions.
435 bool PushdownLocalTransportDescription(
436 const cricket::SessionDescription* sdesc,
437 cricket::ContentAction action,
438 std::string* error_desc);
439 bool PushdownRemoteTransportDescription(
440 const cricket::SessionDescription* sdesc,
441 cricket::ContentAction action,
442 std::string* error_desc);
443
444 // Returns true and the TransportInfo of the given |content_name|
445 // from |description|. Returns false if it's not available.
446 static bool GetTransportDescription(
447 const cricket::SessionDescription* description,
448 const std::string& content_name,
449 cricket::TransportDescription* info);
450
skvlad6c87a672016-05-17 17:49:52 -0700451 // Returns the name of the transport channel when BUNDLE is enabled, or
452 // nullptr if the channel is not part of any bundle.
453 const std::string* GetBundleTransportName(
454 const cricket::ContentInfo* content,
455 const cricket::ContentGroup* bundle);
456
deadbeefcbecd352015-09-23 11:50:27 -0700457 // Cause all the BaseChannels in the bundle group to have the same
458 // transport channel.
459 bool EnableBundle(const cricket::ContentGroup& bundle);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 // Enables media channels to allow sending of media.
462 void EnableChannels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 // Returns the media index for a local ice candidate given the content name.
464 // Returns false if the local session description does not have a media
465 // content called |content_name|.
466 bool GetLocalCandidateMediaIndex(const std::string& content_name,
467 int* sdp_mline_index);
468 // Uses all remote candidates in |remote_desc| in this session.
469 bool UseCandidatesInSessionDescription(
470 const SessionDescriptionInterface* remote_desc);
471 // Uses |candidate| in this session.
472 bool UseCandidate(const IceCandidateInterface* candidate);
473 // Deletes the corresponding channel of contents that don't exist in |desc|.
474 // |desc| can be null. This means that all channels are deleted.
deadbeefcbecd352015-09-23 11:50:27 -0700475 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476
477 // Allocates media channels based on the |desc|. If |desc| doesn't have
478 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
479 // This method will also delete any existing media channels before creating.
480 bool CreateChannels(const cricket::SessionDescription* desc);
481
482 // Helper methods to create media channels.
skvlad6c87a672016-05-17 17:49:52 -0700483 bool CreateVoiceChannel(const cricket::ContentInfo* content,
484 const std::string* bundle_transport);
485 bool CreateVideoChannel(const cricket::ContentInfo* content,
486 const std::string* bundle_transport);
487 bool CreateDataChannel(const cricket::ContentInfo* content,
488 const std::string* bundle_transport);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000489
hbosdf6075a2016-12-19 04:58:02 -0800490 std::unique_ptr<SessionStats> GetStats_n(
491 const ChannelNamePairs& channel_name_pairs);
492
deadbeef953c2ce2017-01-09 14:53:41 -0800493 bool CreateSctpTransport_n(const std::string& content_name,
494 const std::string& transport_name);
495 // For bundling.
496 void ChangeSctpTransport_n(const std::string& transport_name);
497 void DestroySctpTransport_n();
498 // SctpTransport signal handlers. Needed to marshal signals from the network
499 // to signaling thread.
500 void OnSctpTransportReadyToSendData_n();
501 // This may be called with "false" if the direction of the m= section causes
502 // us to tear down the SCTP connection.
503 void OnSctpTransportReadyToSendData_s(bool ready);
504 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
505 const rtc::CopyOnWriteBuffer& payload);
506 // Beyond just firing the signal to the signaling thread, listens to SCTP
507 // CONTROL messages on unused SIDs and processes them as OPEN messages.
508 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
509 const rtc::CopyOnWriteBuffer& payload);
510 void OnSctpStreamClosedRemotely_n(int sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000512 std::string BadStateErrMsg(State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
Peter Thatcher54360512015-07-08 11:08:35 -0700514 void SetIceConnectionReceiving(bool receiving);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000516 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000517 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000518 // Below methods are helper methods which verifies SDP.
519 bool ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
520 cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000521 std::string* err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000522
523 // Check if a call to SetLocalDescription is acceptable with |action|.
524 bool ExpectSetLocalDescription(Action action);
525 // Check if a call to SetRemoteDescription is acceptable with |action|.
526 bool ExpectSetRemoteDescription(Action action);
527 // Verifies a=setup attribute as per RFC 5763.
528 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
529 Action action);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000530
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000531 // Returns true if we are ready to push down the remote candidate.
532 // |remote_desc| is the new remote description, or NULL if the current remote
533 // description should be used. Output |valid| is true if the candidate media
534 // index is valid.
535 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
536 const SessionDescriptionInterface* remote_desc,
537 bool* valid);
538
deadbeef7af91dd2016-12-13 11:29:11 -0800539 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
540 // this session.
541 bool SrtpRequired() const;
542
deadbeef953c2ce2017-01-09 14:53:41 -0800543 // TransportController signal handlers.
deadbeefcbecd352015-09-23 11:50:27 -0700544 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
545 void OnTransportControllerReceiving(bool receiving);
546 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
547 void OnTransportControllerCandidatesGathered(
548 const std::string& transport_name,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700549 const std::vector<cricket::Candidate>& candidates);
550 void OnTransportControllerCandidatesRemoved(
551 const std::vector<cricket::Candidate>& candidates);
deadbeef953c2ce2017-01-09 14:53:41 -0800552 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
deadbeefcbecd352015-09-23 11:50:27 -0700553
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000554 std::string GetSessionErrorMsg();
555
deadbeefcbecd352015-09-23 11:50:27 -0700556 // Invoked when TransportController connection completion is signaled.
557 // Reports stats for all transports in use.
558 void ReportTransportStats();
559
560 // Gather the usage of IPv4/IPv6 as best connection.
jbauchac8869e2015-07-03 01:36:14 -0700561 void ReportBestConnectionState(const cricket::TransportStats& stats);
562
563 void ReportNegotiatedCiphers(const cricket::TransportStats& stats);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000564
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200565 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700566
zhihuang9763d562016-08-05 11:14:50 -0700567 const std::string GetTransportName(const std::string& content_name);
568
deadbeefac22f702017-01-12 21:59:29 -0800569 void DestroyRtcpTransport_n(const std::string& transport_name);
Steve Anton169629a2017-08-30 17:36:36 -0700570 void RemoveAndDestroyVideoChannel(cricket::VideoChannel* video_channel);
571 void DestroyVideoChannel(cricket::VideoChannel* video_channel);
572 void RemoveAndDestroyVoiceChannel(cricket::VoiceChannel* voice_channel);
573 void DestroyVoiceChannel(cricket::VoiceChannel* voice_channel);
zhihuangf5b251b2017-01-12 19:37:48 -0800574 void DestroyDataChannel();
575
zhihuang9763d562016-08-05 11:14:50 -0700576 rtc::Thread* const network_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700577 rtc::Thread* const worker_thread_;
danilchape9021a32016-05-17 01:52:02 -0700578 rtc::Thread* const signaling_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700579
580 State state_ = STATE_INIT;
581 Error error_ = ERROR_NONE;
582 std::string error_desc_;
583
584 const std::string sid_;
585 bool initial_offerer_ = false;
586
hbosdf6075a2016-12-19 04:58:02 -0800587 const std::unique_ptr<cricket::TransportController> transport_controller_;
deadbeef953c2ce2017-01-09 14:53:41 -0800588 const std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
nisseeaabdf62017-05-05 02:23:02 -0700589 const cricket::MediaConfig media_config_;
590 RtcEventLog* event_log_;
591 Call* call_;
Steve Anton169629a2017-08-30 17:36:36 -0700592 // TODO(steveanton): voice_channels_ and video_channels_ used to be a single
593 // VoiceChannel/VideoChannel respectively but are being changed to support
594 // multiple m= lines in unified plan. But until more work is done, these can
595 // only have 0 or 1 channel each.
596 // These channels are owned by ChannelManager.
597 std::vector<cricket::VoiceChannel*> voice_channels_;
598 std::vector<cricket::VideoChannel*> video_channels_;
deadbeef953c2ce2017-01-09 14:53:41 -0800599 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
600 // when using SCTP.
Steve Anton169629a2017-08-30 17:36:36 -0700601 // TODO(steveanton): This should be changed to a bare pointer because
602 // WebRtcSession doesn't actually own the RtpDataChannel
603 // (ChannelManager does).
deadbeef953c2ce2017-01-09 14:53:41 -0800604 std::unique_ptr<cricket::RtpDataChannel> rtp_data_channel_;
605
606 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
607 // |sctp_transport_name_| keeps track of what DTLS transport the SCTP
608 // transport is using (which can change due to bundling).
609 rtc::Optional<std::string> sctp_transport_name_;
610 // |sctp_content_name_| is the content name (MID) in SDP.
611 rtc::Optional<std::string> sctp_content_name_;
612 // Value cached on signaling thread. Only updated when SctpReadyToSendData
613 // fires on the signaling thread.
614 bool sctp_ready_to_send_data_ = false;
615 // Same as signals provided by SctpTransport, but these are guaranteed to
616 // fire on the signaling thread, whereas SctpTransport fires on the networking
617 // thread.
618 // |sctp_invoker_| is used so that any signals queued on the signaling thread
619 // from the network thread are immediately discarded if the SctpTransport is
620 // destroyed (due to m= section being rejected).
621 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
622 // are marshalled to the right thread. Could almost use proxy.h for this,
623 // but it doesn't have a mechanism for marshalling sigslot::signals
624 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
625 sigslot::signal1<bool> SignalSctpReadyToSendData;
626 sigslot::signal2<const cricket::ReceiveDataParams&,
627 const rtc::CopyOnWriteBuffer&>
628 SignalSctpDataReceived;
629 sigslot::signal1<int> SignalSctpStreamClosedRemotely;
630
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 cricket::ChannelManager* channel_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 IceObserver* ice_observer_;
633 PeerConnectionInterface::IceConnectionState ice_connection_state_;
Peter Thatcher54360512015-07-08 11:08:35 -0700634 bool ice_connection_receiving_;
deadbeeffe4a8a42016-12-20 17:56:17 -0800635 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
636 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
637 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
638 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 // If the remote peer is using a older version of implementation.
640 bool older_version_remote_peer_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000641 bool dtls_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 // Specifies which kind of data channel is allowed. This is controlled
643 // by the chrome command-line flag and constraints:
644 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
645 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
646 // not set or false, SCTP is allowed (DCT_SCTP);
647 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
648 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
zhihuang9763d562016-08-05 11:14:50 -0700649 // The data channel type could be DCT_QUIC if the QUIC data channel is
650 // enabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 cricket::DataChannelType data_channel_type_;
deadbeef0ed85b22016-02-23 17:24:52 -0800652 // List of content names for which the remote side triggered an ICE restart.
653 std::set<std::string> pending_ice_restarts_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000654
kwibergd1fe2812016-04-27 06:47:29 -0700655 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000656
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000657 // Member variables for caching global options.
658 cricket::AudioOptions audio_options_;
659 cricket::VideoOptions video_options_;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000660 MetricsObserverInterface* metrics_observer_;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000661
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000662 // Declares the bundle policy for the WebRTCSession.
663 PeerConnectionInterface::BundlePolicy bundle_policy_;
664
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700665 // Declares the RTCP mux policy for the WebRTCSession.
666 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy_;
667
zhihuang184a3fd2016-06-14 11:47:14 -0700668 bool received_first_video_packet_ = false;
669 bool received_first_audio_packet_ = false;
670
zhihuang9763d562016-08-05 11:14:50 -0700671#ifdef HAVE_QUIC
672 std::unique_ptr<QuicDataTransport> quic_data_transport_;
673#endif // HAVE_QUIC
674
henrikg3c089d72015-09-16 05:37:44 -0700675 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSession);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000676};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677} // namespace webrtc
678
ossu7bb87ee2017-01-23 04:56:25 -0800679#endif // WEBRTC_PC_WEBRTCSESSION_H_