blob: 09e5a3e4c4a449431e551da919c4ff73a9bb3823 [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
Steve Anton36b29d12017-10-30 09:57:42 -070014#include <map>
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
deadbeef0ed85b22016-02-23 17:24:52 -080016#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <string>
deadbeefcbecd352015-09-23 11:50:27 -070018#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
Patrik Höglunde2d6a062017-10-05 14:53:33 +020020#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/optional.h"
22#include "api/peerconnectioninterface.h"
23#include "api/statstypes.h"
24#include "call/call.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "pc/datachannel.h"
26#include "pc/mediasession.h"
Zhi Huangb5261582017-09-29 10:51:43 -070027#include "pc/transportcontroller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/constructormagic.h"
29#include "rtc_base/sigslot.h"
30#include "rtc_base/sslidentity.h"
31#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
33namespace cricket {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000034
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035class ChannelManager;
deadbeef953c2ce2017-01-09 14:53:41 -080036class RtpDataChannel;
37class SctpTransportInternal;
38class SctpTransportInternalFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039class StatsReport;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040class VideoChannel;
41class VoiceChannel;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000042
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043} // namespace cricket
44
45namespace webrtc {
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000046
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047class IceRestartAnswerLatch;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000048class JsepIceCandidate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049class MediaStreamSignaling;
Steve Antond5585ca2017-10-23 14:49:26 -070050class PeerConnection;
nisseeaabdf62017-05-05 02:23:02 -070051class RtcEventLog;
wu@webrtc.org91053e72013-08-10 07:18:04 +000052class WebRtcSessionDescriptionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000054extern const char kBundleWithoutRtcpMux[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000055extern const char kCreateChannelFailed[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056extern const char kInvalidCandidates[];
57extern const char kInvalidSdp[];
Zhi Huang2a5e4262017-09-14 01:15:03 -070058extern const char kMlineMismatchInAnswer[];
59extern const char kMlineMismatchInSubsequentOffer[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000060extern const char kPushDownTDFailed[];
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000061extern const char kSdpWithoutDtlsFingerprint[];
62extern const char kSdpWithoutSdesCrypto[];
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000063extern const char kSdpWithoutIceUfragPwd[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000064extern const char kSdpWithoutSdesAndDtlsDisabled[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065extern const char kSessionError[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000066extern const char kSessionErrorDesc[];
deadbeef953c2ce2017-01-09 14:53:41 -080067extern const char kDtlsSrtpSetupFailureRtp[];
68extern const char kDtlsSrtpSetupFailureRtcp[];
deadbeefcbecd352015-09-23 11:50:27 -070069extern const char kEnableBundleFailed[];
70
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000071// Maximum number of received video streams that will be processed by webrtc
72// even if they are not signalled beforehand.
73extern const int kMaxUnsignalledRecvStreams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
75// ICE state callback interface.
76class IceObserver {
77 public:
wu@webrtc.org364f2042013-11-20 21:49:41 +000078 IceObserver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 // Called any time the IceConnectionState changes
zstein6dfd53a2017-03-06 13:49:03 -080080 virtual void OnIceConnectionStateChange(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 PeerConnectionInterface::IceConnectionState new_state) {}
82 // Called any time the IceGatheringState changes
83 virtual void OnIceGatheringChange(
84 PeerConnectionInterface::IceGatheringState new_state) {}
85 // New Ice candidate have been found.
jbauch81bf7b02017-03-25 08:31:12 -070086 virtual void OnIceCandidate(
87 std::unique_ptr<IceCandidateInterface> candidate) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
Honghai Zhang7fb69db2016-03-14 11:59:18 -070089 // Some local ICE candidates have been removed.
90 virtual void OnIceCandidatesRemoved(
91 const std::vector<cricket::Candidate>& candidates) = 0;
92
Peter Thatcher54360512015-07-08 11:08:35 -070093 // Called whenever the state changes between receiving and not receiving.
94 virtual void OnIceConnectionReceivingChange(bool receiving) {}
95
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 protected:
97 ~IceObserver() {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000098
99 private:
henrikg3c089d72015-09-16 05:37:44 -0700100 RTC_DISALLOW_COPY_AND_ASSIGN(IceObserver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101};
102
deadbeefd59daf82015-10-14 15:02:44 -0700103// Statistics for all the transports of the session.
deadbeefd59daf82015-10-14 15:02:44 -0700104// TODO(pthatcher): Think of a better name for this. We already have
105// a TransportStats in transport.h. Perhaps TransportsStats?
106struct SessionStats {
Steve Antond5585ca2017-10-23 14:49:26 -0700107 std::map<std::string, std::string> proxy_to_transport;
108 std::map<std::string, cricket::TransportStats> transport_stats;
deadbeefd59daf82015-10-14 15:02:44 -0700109};
110
hbosdf6075a2016-12-19 04:58:02 -0800111struct ChannelNamePair {
112 ChannelNamePair(
113 const std::string& content_name, const std::string& transport_name)
114 : content_name(content_name), transport_name(transport_name) {}
115 std::string content_name;
116 std::string transport_name;
117};
118
119struct ChannelNamePairs {
120 rtc::Optional<ChannelNamePair> voice;
121 rtc::Optional<ChannelNamePair> video;
122 rtc::Optional<ChannelNamePair> data;
123};
124
deadbeefd59daf82015-10-14 15:02:44 -0700125// A WebRtcSession manages general session state. This includes negotiation
126// of both the application-level and network-level protocols: the former
127// defines what will be sent and the latter defines how it will be sent. Each
128// network-level protocol is represented by a Transport object. Each Transport
129// participates in the network-level negotiation. The individual streams of
130// packets are represented by TransportChannels. The application-level protocol
131// is represented by SessionDecription objects.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700132class WebRtcSession :
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700133 public DataChannelProviderInterface,
134 public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 public:
deadbeefd59daf82015-10-14 15:02:44 -0700136 enum State {
137 STATE_INIT = 0,
138 STATE_SENTOFFER, // Sent offer, waiting for answer.
139 STATE_RECEIVEDOFFER, // Received an offer. Need to send answer.
140 STATE_SENTPRANSWER, // Sent provisional answer. Need to send answer.
141 STATE_RECEIVEDPRANSWER, // Received provisional answer, waiting for answer.
142 STATE_INPROGRESS, // Offer/answer exchange completed.
143 STATE_CLOSED, // Close() was called.
144 };
145
146 enum Error {
147 ERROR_NONE = 0, // no error
148 ERROR_CONTENT = 1, // channel errors in SetLocalContent/SetRemoteContent
149 ERROR_TRANSPORT = 2, // transport error of some kind
150 };
151
deadbeef953c2ce2017-01-09 14:53:41 -0800152 // |sctp_factory| may be null, in which case SCTP is treated as unsupported.
zhihuang29ff8442016-07-27 11:07:25 -0700153 WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700154 Call* call,
155 cricket::ChannelManager* channel_manager,
156 const cricket::MediaConfig& media_config,
157 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700158 rtc::Thread* network_thread,
159 rtc::Thread* worker_thread,
160 rtc::Thread* signaling_thread,
161 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800162 std::unique_ptr<cricket::TransportController> transport_controller,
163 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 virtual ~WebRtcSession();
165
deadbeefd59daf82015-10-14 15:02:44 -0700166 // These are const to allow them to be called from const methods.
zhihuang9763d562016-08-05 11:14:50 -0700167 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700168 rtc::Thread* worker_thread() const { return worker_thread_; }
danilchape9021a32016-05-17 01:52:02 -0700169 rtc::Thread* signaling_thread() const { return signaling_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700170
171 // The ID of this session.
172 const std::string& id() const { return sid_; }
173
Steve Antond5585ca2017-10-23 14:49:26 -0700174 void Initialize(
Henrik Lundin64dad832015-05-11 12:44:23 +0200175 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200176 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Steve Antond5585ca2017-10-23 14:49:26 -0700177 const PeerConnectionInterface::RTCConfiguration& rtc_configuration,
178 PeerConnection* pc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 // Deletes the voice, video and data channel and changes the session state
deadbeefd59daf82015-10-14 15:02:44 -0700180 // to STATE_CLOSED.
181 void Close();
182
183 // Returns true if we were the initial offerer.
184 bool initial_offerer() const { return initial_offerer_; }
185
186 // Returns the current state of the session. See the enum above for details.
187 // Each time the state changes, we will fire this signal.
188 State state() const { return state_; }
189 sigslot::signal2<WebRtcSession*, State> SignalState;
190
191 // Returns the last error in the session. See the enum above for details.
192 Error error() const { return error_; }
193 const std::string& error_desc() const { return error_desc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194
195 void RegisterIceObserver(IceObserver* observer) {
196 ice_observer_ = observer;
197 }
198
deadbeef953c2ce2017-01-09 14:53:41 -0800199 // Exposed for stats collecting.
Steve Anton169629a2017-08-30 17:36:36 -0700200 // TODO(steveanton): Switch callers to use the plural form and remove these.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 virtual cricket::VoiceChannel* voice_channel() {
Steve Anton169629a2017-08-30 17:36:36 -0700202 if (voice_channels_.empty()) {
203 return nullptr;
204 } else {
205 return voice_channels_[0];
206 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 }
208 virtual cricket::VideoChannel* video_channel() {
Steve Anton169629a2017-08-30 17:36:36 -0700209 if (video_channels_.empty()) {
210 return nullptr;
211 } else {
212 return video_channels_[0];
213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 }
Steve Anton169629a2017-08-30 17:36:36 -0700215
216 virtual std::vector<cricket::VoiceChannel*> voice_channels() const {
217 return voice_channels_;
218 }
219 virtual std::vector<cricket::VideoChannel*> video_channels() const {
220 return video_channels_;
221 }
222
deadbeef953c2ce2017-01-09 14:53:41 -0800223 // Only valid when using deprecated RTP data channels.
224 virtual cricket::RtpDataChannel* rtp_data_channel() {
225 return rtp_data_channel_.get();
226 }
227 virtual rtc::Optional<std::string> sctp_content_name() const {
228 return sctp_content_name_;
229 }
230 virtual rtc::Optional<std::string> sctp_transport_name() const {
231 return sctp_transport_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 }
233
deadbeef0ed85b22016-02-23 17:24:52 -0800234 cricket::BaseChannel* GetChannel(const std::string& content_name);
235
deadbeef953c2ce2017-01-09 14:53:41 -0800236 // Get current SSL role used by SCTP's underlying transport.
237 bool GetSctpSslRole(rtc::SSLRole* role);
238 // Get SSL role for an arbitrary m= section (handles bundling correctly).
239 // TODO(deadbeef): This is only used internally by the session description
240 // factory, it shouldn't really be public).
241 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000242
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000243 void CreateOffer(
244 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700245 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
246 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000247 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700248 const cricket::MediaSessionOptions& session_options);
Steve Anton8d3444d2017-10-20 15:30:51 -0700249 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 std::string* err_desc);
Steve Anton8d3444d2017-10-20 15:30:51 -0700251 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 std::string* err_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800253
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 bool ProcessIceMessage(const IceCandidateInterface* ice_candidate);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000255
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700256 bool RemoveRemoteIceCandidates(
257 const std::vector<cricket::Candidate>& candidates);
258
honghaiz1f429e32015-09-28 07:57:34 -0700259 cricket::IceConfig ParseIceConfig(
260 const PeerConnectionInterface::RTCConfiguration& config) const;
261
deadbeefd59daf82015-10-14 15:02:44 -0700262 void SetIceConfig(const cricket::IceConfig& ice_config);
263
264 // Start gathering candidates for any new transports, or transports doing an
265 // ICE restart.
266 void MaybeStartGathering();
267
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 const SessionDescriptionInterface* local_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800269 return pending_local_description_ ? pending_local_description_.get()
270 : current_local_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 }
272 const SessionDescriptionInterface* remote_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800273 return pending_remote_description_ ? pending_remote_description_.get()
274 : current_remote_description_.get();
275 }
276 const SessionDescriptionInterface* current_local_description() const {
277 return current_local_description_.get();
278 }
279 const SessionDescriptionInterface* current_remote_description() const {
280 return current_remote_description_.get();
281 }
282 const SessionDescriptionInterface* pending_local_description() const {
283 return pending_local_description_.get();
284 }
285 const SessionDescriptionInterface* pending_remote_description() const {
286 return pending_remote_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 }
288
289 // Get the id used as a media stream track's "id" field from ssrc.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200290 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
291 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000292
wu@webrtc.org78187522013-10-07 23:32:02 +0000293 // Implements DataChannelProviderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000294 bool SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700295 const rtc::CopyOnWriteBuffer& payload,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000296 cricket::SendDataResult* result) override;
297 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
298 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
299 void AddSctpDataStream(int sid) override;
300 void RemoveSctpDataStream(int sid) override;
301 bool ReadyToSendData() const override;
wu@webrtc.org78187522013-10-07 23:32:02 +0000302
stefanf79ade12017-06-02 06:44:03 -0700303 virtual Call::Stats GetCallStats();
304
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000305 // Returns stats for all channels of all transports.
306 // This avoids exposing the internal structures used to track them.
hbosdf6075a2016-12-19 04:58:02 -0800307 // The parameterless version creates |ChannelNamePairs| from |voice_channel|,
308 // |video_channel| and |voice_channel| if available - this requires it to be
309 // called on the signaling thread - and invokes the other |GetStats|. The
310 // other |GetStats| can be invoked on any thread; if not invoked on the
311 // network thread a thread hop will happen.
312 std::unique_ptr<SessionStats> GetStats_s();
313 virtual std::unique_ptr<SessionStats> GetStats(
314 const ChannelNamePairs& channel_name_pairs);
deadbeefcbecd352015-09-23 11:50:27 -0700315
316 // virtual so it can be mocked in unit tests
317 virtual bool GetLocalCertificate(
318 const std::string& transport_name,
319 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
320
321 // Caller owns returned certificate
jbauch555604a2016-04-26 03:13:22 -0700322 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -0700323 const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 cricket::DataChannelType data_channel_type() const;
326
deadbeefd1a38b52016-12-10 13:15:33 -0800327 // Returns true if there was an ICE restart initiated by the remote offer.
deadbeef0ed85b22016-02-23 17:24:52 -0800328 bool IceRestartPending(const std::string& content_name) const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000329
deadbeefd1a38b52016-12-10 13:15:33 -0800330 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
331 // set, offers should generate new ufrags/passwords until an ICE restart
332 // occurs.
333 void SetNeedsIceRestartFlag();
334 // Returns true if the ICE restart flag above was set, and no ICE restart has
335 // occurred yet for this transport (by applying a local description with
336 // changed ufrag/password). If the transport has been deleted as a result of
337 // bundling, returns false.
338 bool NeedsIceRestart(const std::string& content_name) const;
339
Henrik Boströmd8281982015-08-27 10:12:24 +0200340 // Called when an RTCCertificate is generated or retrieved by
wu@webrtc.org91053e72013-08-10 07:18:04 +0000341 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
Henrik Boströmd8281982015-08-27 10:12:24 +0200342 void OnCertificateReady(
343 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
deadbeef953c2ce2017-01-09 14:53:41 -0800344 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000345
346 // For unit test.
Henrik Boströmd8281982015-08-27 10:12:24 +0200347 bool waiting_for_certificate_for_testing() const;
deadbeefcbecd352015-09-23 11:50:27 -0700348 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000349
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000350 void set_metrics_observer(
351 webrtc::MetricsObserverInterface* metrics_observer) {
352 metrics_observer_ = metrics_observer;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700353 transport_controller_->SetMetricsObserver(metrics_observer);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000354 }
355
deadbeef953c2ce2017-01-09 14:53:41 -0800356 // Called when voice_channel_, video_channel_ and
357 // rtp_data_channel_/sctp_transport_ are created and destroyed. As a result
358 // of, for example, setting a new description.
deadbeefab9b2d12015-10-14 11:33:11 -0700359 sigslot::signal0<> SignalVoiceChannelCreated;
360 sigslot::signal0<> SignalVoiceChannelDestroyed;
361 sigslot::signal0<> SignalVideoChannelCreated;
362 sigslot::signal0<> SignalVideoChannelDestroyed;
363 sigslot::signal0<> SignalDataChannelCreated;
364 sigslot::signal0<> SignalDataChannelDestroyed;
365
366 // Called when a valid data channel OPEN message is received.
367 // std::string represents the data channel label.
368 sigslot::signal2<const std::string&, const InternalDataChannelInit&>
369 SignalDataChannelOpenMessage;
370
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 private:
372 // Indicates the type of SessionDescription in a call to SetLocalDescription
373 // and SetRemoteDescription.
374 enum Action {
375 kOffer,
376 kPrAnswer,
377 kAnswer,
378 };
wu@webrtc.org91053e72013-08-10 07:18:04 +0000379
Steve Anton18ee1d52017-09-11 11:32:35 -0700380 // Return all managed, non-null channels.
381 std::vector<cricket::BaseChannel*> Channels() const;
382
deadbeeffe4a8a42016-12-20 17:56:17 -0800383 // Non-const versions of local_description()/remote_description(), for use
384 // internally.
385 SessionDescriptionInterface* mutable_local_description() {
386 return pending_local_description_ ? pending_local_description_.get()
387 : current_local_description_.get();
388 }
389 SessionDescriptionInterface* mutable_remote_description() {
390 return pending_remote_description_ ? pending_remote_description_.get()
391 : current_remote_description_.get();
392 }
393
deadbeefd59daf82015-10-14 15:02:44 -0700394 // Log session state.
395 void LogState(State old_state, State new_state);
396
397 // Updates the state, signaling if necessary.
398 virtual void SetState(State state);
399
400 // Updates the error state, signaling if necessary.
401 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|.
402 virtual void SetError(Error error, const std::string& error_desc);
403
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 bool UpdateSessionState(Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 std::string* err_desc);
406 static Action GetAction(const std::string& type);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000407 // Push the media parts of the local or remote session description
408 // down to all of the channels.
409 bool PushdownMediaDescription(cricket::ContentAction action,
410 cricket::ContentSource source,
411 std::string* error_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800412 bool PushdownSctpParameters_n(cricket::ContentSource source);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000413
deadbeefd59daf82015-10-14 15:02:44 -0700414 bool PushdownTransportDescription(cricket::ContentSource source,
415 cricket::ContentAction action,
416 std::string* error_desc);
417
418 // Helper methods to push local and remote transport descriptions.
419 bool PushdownLocalTransportDescription(
420 const cricket::SessionDescription* sdesc,
421 cricket::ContentAction action,
422 std::string* error_desc);
423 bool PushdownRemoteTransportDescription(
424 const cricket::SessionDescription* sdesc,
425 cricket::ContentAction action,
426 std::string* error_desc);
427
428 // Returns true and the TransportInfo of the given |content_name|
429 // from |description|. Returns false if it's not available.
430 static bool GetTransportDescription(
431 const cricket::SessionDescription* description,
432 const std::string& content_name,
433 cricket::TransportDescription* info);
434
skvlad6c87a672016-05-17 17:49:52 -0700435 // Returns the name of the transport channel when BUNDLE is enabled, or
436 // nullptr if the channel is not part of any bundle.
437 const std::string* GetBundleTransportName(
438 const cricket::ContentInfo* content,
439 const cricket::ContentGroup* bundle);
440
deadbeefcbecd352015-09-23 11:50:27 -0700441 // Cause all the BaseChannels in the bundle group to have the same
442 // transport channel.
443 bool EnableBundle(const cricket::ContentGroup& bundle);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 // Enables media channels to allow sending of media.
446 void EnableChannels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 // Returns the media index for a local ice candidate given the content name.
448 // Returns false if the local session description does not have a media
449 // content called |content_name|.
450 bool GetLocalCandidateMediaIndex(const std::string& content_name,
451 int* sdp_mline_index);
452 // Uses all remote candidates in |remote_desc| in this session.
453 bool UseCandidatesInSessionDescription(
454 const SessionDescriptionInterface* remote_desc);
455 // Uses |candidate| in this session.
456 bool UseCandidate(const IceCandidateInterface* candidate);
457 // Deletes the corresponding channel of contents that don't exist in |desc|.
458 // |desc| can be null. This means that all channels are deleted.
deadbeefcbecd352015-09-23 11:50:27 -0700459 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460
461 // Allocates media channels based on the |desc|. If |desc| doesn't have
462 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
463 // This method will also delete any existing media channels before creating.
464 bool CreateChannels(const cricket::SessionDescription* desc);
465
466 // Helper methods to create media channels.
skvlad6c87a672016-05-17 17:49:52 -0700467 bool CreateVoiceChannel(const cricket::ContentInfo* content,
468 const std::string* bundle_transport);
469 bool CreateVideoChannel(const cricket::ContentInfo* content,
470 const std::string* bundle_transport);
471 bool CreateDataChannel(const cricket::ContentInfo* content,
472 const std::string* bundle_transport);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000473
hbosdf6075a2016-12-19 04:58:02 -0800474 std::unique_ptr<SessionStats> GetStats_n(
475 const ChannelNamePairs& channel_name_pairs);
476
deadbeef953c2ce2017-01-09 14:53:41 -0800477 bool CreateSctpTransport_n(const std::string& content_name,
478 const std::string& transport_name);
479 // For bundling.
480 void ChangeSctpTransport_n(const std::string& transport_name);
481 void DestroySctpTransport_n();
482 // SctpTransport signal handlers. Needed to marshal signals from the network
483 // to signaling thread.
484 void OnSctpTransportReadyToSendData_n();
485 // This may be called with "false" if the direction of the m= section causes
486 // us to tear down the SCTP connection.
487 void OnSctpTransportReadyToSendData_s(bool ready);
488 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
489 const rtc::CopyOnWriteBuffer& payload);
490 // Beyond just firing the signal to the signaling thread, listens to SCTP
491 // CONTROL messages on unused SIDs and processes them as OPEN messages.
492 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
493 const rtc::CopyOnWriteBuffer& payload);
494 void OnSctpStreamClosedRemotely_n(int sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000496 std::string BadStateErrMsg(State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
Peter Thatcher54360512015-07-08 11:08:35 -0700498 void SetIceConnectionReceiving(bool receiving);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000500 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000501 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000502 // Below methods are helper methods which verifies SDP.
503 bool ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
504 cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000505 std::string* err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000506
507 // Check if a call to SetLocalDescription is acceptable with |action|.
508 bool ExpectSetLocalDescription(Action action);
509 // Check if a call to SetRemoteDescription is acceptable with |action|.
510 bool ExpectSetRemoteDescription(Action action);
511 // Verifies a=setup attribute as per RFC 5763.
512 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
513 Action action);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000514
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000515 // Returns true if we are ready to push down the remote candidate.
516 // |remote_desc| is the new remote description, or NULL if the current remote
517 // description should be used. Output |valid| is true if the candidate media
518 // index is valid.
519 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
520 const SessionDescriptionInterface* remote_desc,
521 bool* valid);
522
deadbeef7af91dd2016-12-13 11:29:11 -0800523 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
524 // this session.
525 bool SrtpRequired() const;
526
deadbeef953c2ce2017-01-09 14:53:41 -0800527 // TransportController signal handlers.
deadbeefcbecd352015-09-23 11:50:27 -0700528 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
529 void OnTransportControllerReceiving(bool receiving);
530 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
531 void OnTransportControllerCandidatesGathered(
532 const std::string& transport_name,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700533 const std::vector<cricket::Candidate>& candidates);
534 void OnTransportControllerCandidatesRemoved(
535 const std::vector<cricket::Candidate>& candidates);
deadbeef953c2ce2017-01-09 14:53:41 -0800536 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
deadbeefcbecd352015-09-23 11:50:27 -0700537
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000538 std::string GetSessionErrorMsg();
539
deadbeefcbecd352015-09-23 11:50:27 -0700540 // Invoked when TransportController connection completion is signaled.
541 // Reports stats for all transports in use.
542 void ReportTransportStats();
543
544 // Gather the usage of IPv4/IPv6 as best connection.
jbauchac8869e2015-07-03 01:36:14 -0700545 void ReportBestConnectionState(const cricket::TransportStats& stats);
546
547 void ReportNegotiatedCiphers(const cricket::TransportStats& stats);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000548
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200549 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700550
zhihuang9763d562016-08-05 11:14:50 -0700551 const std::string GetTransportName(const std::string& content_name);
552
deadbeefac22f702017-01-12 21:59:29 -0800553 void DestroyRtcpTransport_n(const std::string& transport_name);
Steve Anton169629a2017-08-30 17:36:36 -0700554 void RemoveAndDestroyVideoChannel(cricket::VideoChannel* video_channel);
555 void DestroyVideoChannel(cricket::VideoChannel* video_channel);
556 void RemoveAndDestroyVoiceChannel(cricket::VoiceChannel* voice_channel);
557 void DestroyVoiceChannel(cricket::VoiceChannel* voice_channel);
zhihuangf5b251b2017-01-12 19:37:48 -0800558 void DestroyDataChannel();
559
zhihuang9763d562016-08-05 11:14:50 -0700560 rtc::Thread* const network_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700561 rtc::Thread* const worker_thread_;
danilchape9021a32016-05-17 01:52:02 -0700562 rtc::Thread* const signaling_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700563
564 State state_ = STATE_INIT;
565 Error error_ = ERROR_NONE;
566 std::string error_desc_;
567
568 const std::string sid_;
569 bool initial_offerer_ = false;
570
hbosdf6075a2016-12-19 04:58:02 -0800571 const std::unique_ptr<cricket::TransportController> transport_controller_;
deadbeef953c2ce2017-01-09 14:53:41 -0800572 const std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
nisseeaabdf62017-05-05 02:23:02 -0700573 const cricket::MediaConfig media_config_;
574 RtcEventLog* event_log_;
575 Call* call_;
Steve Anton169629a2017-08-30 17:36:36 -0700576 // TODO(steveanton): voice_channels_ and video_channels_ used to be a single
577 // VoiceChannel/VideoChannel respectively but are being changed to support
578 // multiple m= lines in unified plan. But until more work is done, these can
579 // only have 0 or 1 channel each.
580 // These channels are owned by ChannelManager.
581 std::vector<cricket::VoiceChannel*> voice_channels_;
582 std::vector<cricket::VideoChannel*> video_channels_;
deadbeef953c2ce2017-01-09 14:53:41 -0800583 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
584 // when using SCTP.
Steve Anton169629a2017-08-30 17:36:36 -0700585 // TODO(steveanton): This should be changed to a bare pointer because
586 // WebRtcSession doesn't actually own the RtpDataChannel
587 // (ChannelManager does).
deadbeef953c2ce2017-01-09 14:53:41 -0800588 std::unique_ptr<cricket::RtpDataChannel> rtp_data_channel_;
589
590 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
591 // |sctp_transport_name_| keeps track of what DTLS transport the SCTP
592 // transport is using (which can change due to bundling).
593 rtc::Optional<std::string> sctp_transport_name_;
594 // |sctp_content_name_| is the content name (MID) in SDP.
595 rtc::Optional<std::string> sctp_content_name_;
596 // Value cached on signaling thread. Only updated when SctpReadyToSendData
597 // fires on the signaling thread.
598 bool sctp_ready_to_send_data_ = false;
599 // Same as signals provided by SctpTransport, but these are guaranteed to
600 // fire on the signaling thread, whereas SctpTransport fires on the networking
601 // thread.
602 // |sctp_invoker_| is used so that any signals queued on the signaling thread
603 // from the network thread are immediately discarded if the SctpTransport is
604 // destroyed (due to m= section being rejected).
605 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
606 // are marshalled to the right thread. Could almost use proxy.h for this,
607 // but it doesn't have a mechanism for marshalling sigslot::signals
608 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
609 sigslot::signal1<bool> SignalSctpReadyToSendData;
610 sigslot::signal2<const cricket::ReceiveDataParams&,
611 const rtc::CopyOnWriteBuffer&>
612 SignalSctpDataReceived;
613 sigslot::signal1<int> SignalSctpStreamClosedRemotely;
614
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 cricket::ChannelManager* channel_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 IceObserver* ice_observer_;
617 PeerConnectionInterface::IceConnectionState ice_connection_state_;
Peter Thatcher54360512015-07-08 11:08:35 -0700618 bool ice_connection_receiving_;
deadbeeffe4a8a42016-12-20 17:56:17 -0800619 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
620 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
621 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
622 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 // If the remote peer is using a older version of implementation.
624 bool older_version_remote_peer_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000625 bool dtls_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 // Specifies which kind of data channel is allowed. This is controlled
627 // by the chrome command-line flag and constraints:
628 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
629 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
630 // not set or false, SCTP is allowed (DCT_SCTP);
631 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
632 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
633 cricket::DataChannelType data_channel_type_;
deadbeef0ed85b22016-02-23 17:24:52 -0800634 // List of content names for which the remote side triggered an ICE restart.
635 std::set<std::string> pending_ice_restarts_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000636
kwibergd1fe2812016-04-27 06:47:29 -0700637 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000638
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000639 // Member variables for caching global options.
640 cricket::AudioOptions audio_options_;
641 cricket::VideoOptions video_options_;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000642 MetricsObserverInterface* metrics_observer_;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000643
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000644 // Declares the bundle policy for the WebRTCSession.
645 PeerConnectionInterface::BundlePolicy bundle_policy_;
646
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700647 // Declares the RTCP mux policy for the WebRTCSession.
648 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy_;
649
zhihuang184a3fd2016-06-14 11:47:14 -0700650 bool received_first_video_packet_ = false;
651 bool received_first_audio_packet_ = false;
652
henrikg3c089d72015-09-16 05:37:44 -0700653 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSession);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000654};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655} // namespace webrtc
656
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200657#endif // PC_WEBRTCSESSION_H_