blob: 07202eec80339c54e3fe9d620dfc10fabceeda5c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_WEBRTCSESSION_H_
12#define PC_WEBRTCSESSION_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
deadbeef0ed85b22016-02-23 17:24:52 -080015#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016#include <string>
deadbeefcbecd352015-09-23 11:50:27 -070017#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/optional.h"
21#include "api/peerconnectioninterface.h"
22#include "api/statstypes.h"
23#include "call/call.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "pc/datachannel.h"
25#include "pc/mediasession.h"
Zhi Huangb5261582017-09-29 10:51:43 -070026#include "pc/transportcontroller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/constructormagic.h"
28#include "rtc_base/sigslot.h"
29#include "rtc_base/sslidentity.h"
30#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
32namespace cricket {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000033
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034class ChannelManager;
deadbeef953c2ce2017-01-09 14:53:41 -080035class RtpDataChannel;
36class SctpTransportInternal;
37class SctpTransportInternalFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038class StatsReport;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039class VideoChannel;
40class VoiceChannel;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000041
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042} // namespace cricket
43
44namespace webrtc {
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000045
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046class IceRestartAnswerLatch;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000047class JsepIceCandidate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048class MediaStreamSignaling;
Steve Antond5585ca2017-10-23 14:49:26 -070049class PeerConnection;
nisseeaabdf62017-05-05 02:23:02 -070050class RtcEventLog;
wu@webrtc.org91053e72013-08-10 07:18:04 +000051class WebRtcSessionDescriptionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000053extern const char kBundleWithoutRtcpMux[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000054extern const char kCreateChannelFailed[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055extern const char kInvalidCandidates[];
56extern const char kInvalidSdp[];
Zhi Huang2a5e4262017-09-14 01:15:03 -070057extern const char kMlineMismatchInAnswer[];
58extern const char kMlineMismatchInSubsequentOffer[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000059extern const char kPushDownTDFailed[];
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000060extern const char kSdpWithoutDtlsFingerprint[];
61extern const char kSdpWithoutSdesCrypto[];
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000062extern const char kSdpWithoutIceUfragPwd[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000063extern const char kSdpWithoutSdesAndDtlsDisabled[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064extern const char kSessionError[];
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065extern const char kSessionErrorDesc[];
deadbeef953c2ce2017-01-09 14:53:41 -080066extern const char kDtlsSrtpSetupFailureRtp[];
67extern const char kDtlsSrtpSetupFailureRtcp[];
deadbeefcbecd352015-09-23 11:50:27 -070068extern const char kEnableBundleFailed[];
69
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000070// Maximum number of received video streams that will be processed by webrtc
71// even if they are not signalled beforehand.
72extern const int kMaxUnsignalledRecvStreams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
74// ICE state callback interface.
75class IceObserver {
76 public:
wu@webrtc.org364f2042013-11-20 21:49:41 +000077 IceObserver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 // Called any time the IceConnectionState changes
zstein6dfd53a2017-03-06 13:49:03 -080079 virtual void OnIceConnectionStateChange(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 PeerConnectionInterface::IceConnectionState new_state) {}
81 // Called any time the IceGatheringState changes
82 virtual void OnIceGatheringChange(
83 PeerConnectionInterface::IceGatheringState new_state) {}
84 // New Ice candidate have been found.
jbauch81bf7b02017-03-25 08:31:12 -070085 virtual void OnIceCandidate(
86 std::unique_ptr<IceCandidateInterface> candidate) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
Honghai Zhang7fb69db2016-03-14 11:59:18 -070088 // Some local ICE candidates have been removed.
89 virtual void OnIceCandidatesRemoved(
90 const std::vector<cricket::Candidate>& candidates) = 0;
91
Peter Thatcher54360512015-07-08 11:08:35 -070092 // Called whenever the state changes between receiving and not receiving.
93 virtual void OnIceConnectionReceivingChange(bool receiving) {}
94
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 protected:
96 ~IceObserver() {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000097
98 private:
henrikg3c089d72015-09-16 05:37:44 -070099 RTC_DISALLOW_COPY_AND_ASSIGN(IceObserver);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100};
101
deadbeefd59daf82015-10-14 15:02:44 -0700102// Statistics for all the transports of the session.
deadbeefd59daf82015-10-14 15:02:44 -0700103// TODO(pthatcher): Think of a better name for this. We already have
104// a TransportStats in transport.h. Perhaps TransportsStats?
105struct SessionStats {
Steve Antond5585ca2017-10-23 14:49:26 -0700106 std::map<std::string, std::string> proxy_to_transport;
107 std::map<std::string, cricket::TransportStats> transport_stats;
deadbeefd59daf82015-10-14 15:02:44 -0700108};
109
hbosdf6075a2016-12-19 04:58:02 -0800110struct ChannelNamePair {
111 ChannelNamePair(
112 const std::string& content_name, const std::string& transport_name)
113 : content_name(content_name), transport_name(transport_name) {}
114 std::string content_name;
115 std::string transport_name;
116};
117
118struct ChannelNamePairs {
119 rtc::Optional<ChannelNamePair> voice;
120 rtc::Optional<ChannelNamePair> video;
121 rtc::Optional<ChannelNamePair> data;
122};
123
deadbeefd59daf82015-10-14 15:02:44 -0700124// A WebRtcSession manages general session state. This includes negotiation
125// of both the application-level and network-level protocols: the former
126// defines what will be sent and the latter defines how it will be sent. Each
127// network-level protocol is represented by a Transport object. Each Transport
128// participates in the network-level negotiation. The individual streams of
129// packets are represented by TransportChannels. The application-level protocol
130// is represented by SessionDecription objects.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700131class WebRtcSession :
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700132 public DataChannelProviderInterface,
133 public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 public:
deadbeefd59daf82015-10-14 15:02:44 -0700135 enum State {
136 STATE_INIT = 0,
137 STATE_SENTOFFER, // Sent offer, waiting for answer.
138 STATE_RECEIVEDOFFER, // Received an offer. Need to send answer.
139 STATE_SENTPRANSWER, // Sent provisional answer. Need to send answer.
140 STATE_RECEIVEDPRANSWER, // Received provisional answer, waiting for answer.
141 STATE_INPROGRESS, // Offer/answer exchange completed.
142 STATE_CLOSED, // Close() was called.
143 };
144
145 enum Error {
146 ERROR_NONE = 0, // no error
147 ERROR_CONTENT = 1, // channel errors in SetLocalContent/SetRemoteContent
148 ERROR_TRANSPORT = 2, // transport error of some kind
149 };
150
deadbeef953c2ce2017-01-09 14:53:41 -0800151 // |sctp_factory| may be null, in which case SCTP is treated as unsupported.
zhihuang29ff8442016-07-27 11:07:25 -0700152 WebRtcSession(
nisseeaabdf62017-05-05 02:23:02 -0700153 Call* call,
154 cricket::ChannelManager* channel_manager,
155 const cricket::MediaConfig& media_config,
156 RtcEventLog* event_log,
zhihuang29ff8442016-07-27 11:07:25 -0700157 rtc::Thread* network_thread,
158 rtc::Thread* worker_thread,
159 rtc::Thread* signaling_thread,
160 cricket::PortAllocator* port_allocator,
deadbeef953c2ce2017-01-09 14:53:41 -0800161 std::unique_ptr<cricket::TransportController> transport_controller,
162 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 virtual ~WebRtcSession();
164
deadbeefd59daf82015-10-14 15:02:44 -0700165 // These are const to allow them to be called from const methods.
zhihuang9763d562016-08-05 11:14:50 -0700166 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700167 rtc::Thread* worker_thread() const { return worker_thread_; }
danilchape9021a32016-05-17 01:52:02 -0700168 rtc::Thread* signaling_thread() const { return signaling_thread_; }
deadbeefd59daf82015-10-14 15:02:44 -0700169
170 // The ID of this session.
171 const std::string& id() const { return sid_; }
172
Steve Antond5585ca2017-10-23 14:49:26 -0700173 void Initialize(
Henrik Lundin64dad832015-05-11 12:44:23 +0200174 const PeerConnectionFactoryInterface::Options& options,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200175 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Steve Antond5585ca2017-10-23 14:49:26 -0700176 const PeerConnectionInterface::RTCConfiguration& rtc_configuration,
177 PeerConnection* pc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 // Deletes the voice, video and data channel and changes the session state
deadbeefd59daf82015-10-14 15:02:44 -0700179 // to STATE_CLOSED.
180 void Close();
181
182 // Returns true if we were the initial offerer.
183 bool initial_offerer() const { return initial_offerer_; }
184
185 // Returns the current state of the session. See the enum above for details.
186 // Each time the state changes, we will fire this signal.
187 State state() const { return state_; }
188 sigslot::signal2<WebRtcSession*, State> SignalState;
189
190 // Returns the last error in the session. See the enum above for details.
191 Error error() const { return error_; }
192 const std::string& error_desc() const { return error_desc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193
194 void RegisterIceObserver(IceObserver* observer) {
195 ice_observer_ = observer;
196 }
197
deadbeef953c2ce2017-01-09 14:53:41 -0800198 // Exposed for stats collecting.
Steve Anton169629a2017-08-30 17:36:36 -0700199 // TODO(steveanton): Switch callers to use the plural form and remove these.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 virtual cricket::VoiceChannel* voice_channel() {
Steve Anton169629a2017-08-30 17:36:36 -0700201 if (voice_channels_.empty()) {
202 return nullptr;
203 } else {
204 return voice_channels_[0];
205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 }
207 virtual cricket::VideoChannel* video_channel() {
Steve Anton169629a2017-08-30 17:36:36 -0700208 if (video_channels_.empty()) {
209 return nullptr;
210 } else {
211 return video_channels_[0];
212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 }
Steve Anton169629a2017-08-30 17:36:36 -0700214
215 virtual std::vector<cricket::VoiceChannel*> voice_channels() const {
216 return voice_channels_;
217 }
218 virtual std::vector<cricket::VideoChannel*> video_channels() const {
219 return video_channels_;
220 }
221
deadbeef953c2ce2017-01-09 14:53:41 -0800222 // Only valid when using deprecated RTP data channels.
223 virtual cricket::RtpDataChannel* rtp_data_channel() {
224 return rtp_data_channel_.get();
225 }
226 virtual rtc::Optional<std::string> sctp_content_name() const {
227 return sctp_content_name_;
228 }
229 virtual rtc::Optional<std::string> sctp_transport_name() const {
230 return sctp_transport_name_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 }
232
deadbeef0ed85b22016-02-23 17:24:52 -0800233 cricket::BaseChannel* GetChannel(const std::string& content_name);
234
deadbeef953c2ce2017-01-09 14:53:41 -0800235 // Get current SSL role used by SCTP's underlying transport.
236 bool GetSctpSslRole(rtc::SSLRole* role);
237 // Get SSL role for an arbitrary m= section (handles bundling correctly).
238 // TODO(deadbeef): This is only used internally by the session description
239 // factory, it shouldn't really be public).
240 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000241
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000242 void CreateOffer(
243 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700244 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
245 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000246 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700247 const cricket::MediaSessionOptions& session_options);
Steve Anton8d3444d2017-10-20 15:30:51 -0700248 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 std::string* err_desc);
Steve Anton8d3444d2017-10-20 15:30:51 -0700250 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 std::string* err_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800252
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 bool ProcessIceMessage(const IceCandidateInterface* ice_candidate);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000254
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700255 bool RemoveRemoteIceCandidates(
256 const std::vector<cricket::Candidate>& candidates);
257
honghaiz1f429e32015-09-28 07:57:34 -0700258 cricket::IceConfig ParseIceConfig(
259 const PeerConnectionInterface::RTCConfiguration& config) const;
260
deadbeefd59daf82015-10-14 15:02:44 -0700261 void SetIceConfig(const cricket::IceConfig& ice_config);
262
263 // Start gathering candidates for any new transports, or transports doing an
264 // ICE restart.
265 void MaybeStartGathering();
266
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 const SessionDescriptionInterface* local_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800268 return pending_local_description_ ? pending_local_description_.get()
269 : current_local_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 }
271 const SessionDescriptionInterface* remote_description() const {
deadbeeffe4a8a42016-12-20 17:56:17 -0800272 return pending_remote_description_ ? pending_remote_description_.get()
273 : current_remote_description_.get();
274 }
275 const SessionDescriptionInterface* current_local_description() const {
276 return current_local_description_.get();
277 }
278 const SessionDescriptionInterface* current_remote_description() const {
279 return current_remote_description_.get();
280 }
281 const SessionDescriptionInterface* pending_local_description() const {
282 return pending_local_description_.get();
283 }
284 const SessionDescriptionInterface* pending_remote_description() const {
285 return pending_remote_description_.get();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 }
287
288 // Get the id used as a media stream track's "id" field from ssrc.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200289 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
290 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000291
wu@webrtc.org78187522013-10-07 23:32:02 +0000292 // Implements DataChannelProviderInterface.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000293 bool SendData(const cricket::SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -0700294 const rtc::CopyOnWriteBuffer& payload,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000295 cricket::SendDataResult* result) override;
296 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
297 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
298 void AddSctpDataStream(int sid) override;
299 void RemoveSctpDataStream(int sid) override;
300 bool ReadyToSendData() const override;
wu@webrtc.org78187522013-10-07 23:32:02 +0000301
stefanf79ade12017-06-02 06:44:03 -0700302 virtual Call::Stats GetCallStats();
303
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000304 // Returns stats for all channels of all transports.
305 // This avoids exposing the internal structures used to track them.
hbosdf6075a2016-12-19 04:58:02 -0800306 // The parameterless version creates |ChannelNamePairs| from |voice_channel|,
307 // |video_channel| and |voice_channel| if available - this requires it to be
308 // called on the signaling thread - and invokes the other |GetStats|. The
309 // other |GetStats| can be invoked on any thread; if not invoked on the
310 // network thread a thread hop will happen.
311 std::unique_ptr<SessionStats> GetStats_s();
312 virtual std::unique_ptr<SessionStats> GetStats(
313 const ChannelNamePairs& channel_name_pairs);
deadbeefcbecd352015-09-23 11:50:27 -0700314
315 // virtual so it can be mocked in unit tests
316 virtual bool GetLocalCertificate(
317 const std::string& transport_name,
318 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
319
320 // Caller owns returned certificate
jbauch555604a2016-04-26 03:13:22 -0700321 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
kwibergb4d01c42016-04-06 05:15:06 -0700322 const std::string& transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700323
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 cricket::DataChannelType data_channel_type() const;
325
deadbeefd1a38b52016-12-10 13:15:33 -0800326 // Returns true if there was an ICE restart initiated by the remote offer.
deadbeef0ed85b22016-02-23 17:24:52 -0800327 bool IceRestartPending(const std::string& content_name) const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000328
deadbeefd1a38b52016-12-10 13:15:33 -0800329 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
330 // set, offers should generate new ufrags/passwords until an ICE restart
331 // occurs.
332 void SetNeedsIceRestartFlag();
333 // Returns true if the ICE restart flag above was set, and no ICE restart has
334 // occurred yet for this transport (by applying a local description with
335 // changed ufrag/password). If the transport has been deleted as a result of
336 // bundling, returns false.
337 bool NeedsIceRestart(const std::string& content_name) const;
338
Henrik Boströmd8281982015-08-27 10:12:24 +0200339 // Called when an RTCCertificate is generated or retrieved by
wu@webrtc.org91053e72013-08-10 07:18:04 +0000340 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
Henrik Boströmd8281982015-08-27 10:12:24 +0200341 void OnCertificateReady(
342 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
deadbeef953c2ce2017-01-09 14:53:41 -0800343 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000344
345 // For unit test.
Henrik Boströmd8281982015-08-27 10:12:24 +0200346 bool waiting_for_certificate_for_testing() const;
deadbeefcbecd352015-09-23 11:50:27 -0700347 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000348
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000349 void set_metrics_observer(
350 webrtc::MetricsObserverInterface* metrics_observer) {
351 metrics_observer_ = metrics_observer;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700352 transport_controller_->SetMetricsObserver(metrics_observer);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000353 }
354
deadbeef953c2ce2017-01-09 14:53:41 -0800355 // Called when voice_channel_, video_channel_ and
356 // rtp_data_channel_/sctp_transport_ are created and destroyed. As a result
357 // of, for example, setting a new description.
deadbeefab9b2d12015-10-14 11:33:11 -0700358 sigslot::signal0<> SignalVoiceChannelCreated;
359 sigslot::signal0<> SignalVoiceChannelDestroyed;
360 sigslot::signal0<> SignalVideoChannelCreated;
361 sigslot::signal0<> SignalVideoChannelDestroyed;
362 sigslot::signal0<> SignalDataChannelCreated;
363 sigslot::signal0<> SignalDataChannelDestroyed;
364
365 // Called when a valid data channel OPEN message is received.
366 // std::string represents the data channel label.
367 sigslot::signal2<const std::string&, const InternalDataChannelInit&>
368 SignalDataChannelOpenMessage;
369
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 private:
371 // Indicates the type of SessionDescription in a call to SetLocalDescription
372 // and SetRemoteDescription.
373 enum Action {
374 kOffer,
375 kPrAnswer,
376 kAnswer,
377 };
wu@webrtc.org91053e72013-08-10 07:18:04 +0000378
Steve Anton18ee1d52017-09-11 11:32:35 -0700379 // Return all managed, non-null channels.
380 std::vector<cricket::BaseChannel*> Channels() const;
381
deadbeeffe4a8a42016-12-20 17:56:17 -0800382 // Non-const versions of local_description()/remote_description(), for use
383 // internally.
384 SessionDescriptionInterface* mutable_local_description() {
385 return pending_local_description_ ? pending_local_description_.get()
386 : current_local_description_.get();
387 }
388 SessionDescriptionInterface* mutable_remote_description() {
389 return pending_remote_description_ ? pending_remote_description_.get()
390 : current_remote_description_.get();
391 }
392
deadbeefd59daf82015-10-14 15:02:44 -0700393 // Log session state.
394 void LogState(State old_state, State new_state);
395
396 // Updates the state, signaling if necessary.
397 virtual void SetState(State state);
398
399 // Updates the error state, signaling if necessary.
400 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|.
401 virtual void SetError(Error error, const std::string& error_desc);
402
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 bool UpdateSessionState(Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 std::string* err_desc);
405 static Action GetAction(const std::string& type);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000406 // Push the media parts of the local or remote session description
407 // down to all of the channels.
408 bool PushdownMediaDescription(cricket::ContentAction action,
409 cricket::ContentSource source,
410 std::string* error_desc);
deadbeef953c2ce2017-01-09 14:53:41 -0800411 bool PushdownSctpParameters_n(cricket::ContentSource source);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000412
deadbeefd59daf82015-10-14 15:02:44 -0700413 bool PushdownTransportDescription(cricket::ContentSource source,
414 cricket::ContentAction action,
415 std::string* error_desc);
416
417 // Helper methods to push local and remote transport descriptions.
418 bool PushdownLocalTransportDescription(
419 const cricket::SessionDescription* sdesc,
420 cricket::ContentAction action,
421 std::string* error_desc);
422 bool PushdownRemoteTransportDescription(
423 const cricket::SessionDescription* sdesc,
424 cricket::ContentAction action,
425 std::string* error_desc);
426
427 // Returns true and the TransportInfo of the given |content_name|
428 // from |description|. Returns false if it's not available.
429 static bool GetTransportDescription(
430 const cricket::SessionDescription* description,
431 const std::string& content_name,
432 cricket::TransportDescription* info);
433
skvlad6c87a672016-05-17 17:49:52 -0700434 // Returns the name of the transport channel when BUNDLE is enabled, or
435 // nullptr if the channel is not part of any bundle.
436 const std::string* GetBundleTransportName(
437 const cricket::ContentInfo* content,
438 const cricket::ContentGroup* bundle);
439
deadbeefcbecd352015-09-23 11:50:27 -0700440 // Cause all the BaseChannels in the bundle group to have the same
441 // transport channel.
442 bool EnableBundle(const cricket::ContentGroup& bundle);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 // Enables media channels to allow sending of media.
445 void EnableChannels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 // Returns the media index for a local ice candidate given the content name.
447 // Returns false if the local session description does not have a media
448 // content called |content_name|.
449 bool GetLocalCandidateMediaIndex(const std::string& content_name,
450 int* sdp_mline_index);
451 // Uses all remote candidates in |remote_desc| in this session.
452 bool UseCandidatesInSessionDescription(
453 const SessionDescriptionInterface* remote_desc);
454 // Uses |candidate| in this session.
455 bool UseCandidate(const IceCandidateInterface* candidate);
456 // Deletes the corresponding channel of contents that don't exist in |desc|.
457 // |desc| can be null. This means that all channels are deleted.
deadbeefcbecd352015-09-23 11:50:27 -0700458 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459
460 // Allocates media channels based on the |desc|. If |desc| doesn't have
461 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
462 // This method will also delete any existing media channels before creating.
463 bool CreateChannels(const cricket::SessionDescription* desc);
464
465 // Helper methods to create media channels.
skvlad6c87a672016-05-17 17:49:52 -0700466 bool CreateVoiceChannel(const cricket::ContentInfo* content,
467 const std::string* bundle_transport);
468 bool CreateVideoChannel(const cricket::ContentInfo* content,
469 const std::string* bundle_transport);
470 bool CreateDataChannel(const cricket::ContentInfo* content,
471 const std::string* bundle_transport);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000472
hbosdf6075a2016-12-19 04:58:02 -0800473 std::unique_ptr<SessionStats> GetStats_n(
474 const ChannelNamePairs& channel_name_pairs);
475
deadbeef953c2ce2017-01-09 14:53:41 -0800476 bool CreateSctpTransport_n(const std::string& content_name,
477 const std::string& transport_name);
478 // For bundling.
479 void ChangeSctpTransport_n(const std::string& transport_name);
480 void DestroySctpTransport_n();
481 // SctpTransport signal handlers. Needed to marshal signals from the network
482 // to signaling thread.
483 void OnSctpTransportReadyToSendData_n();
484 // This may be called with "false" if the direction of the m= section causes
485 // us to tear down the SCTP connection.
486 void OnSctpTransportReadyToSendData_s(bool ready);
487 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
488 const rtc::CopyOnWriteBuffer& payload);
489 // Beyond just firing the signal to the signaling thread, listens to SCTP
490 // CONTROL messages on unused SIDs and processes them as OPEN messages.
491 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
492 const rtc::CopyOnWriteBuffer& payload);
493 void OnSctpStreamClosedRemotely_n(int sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000495 std::string BadStateErrMsg(State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
Peter Thatcher54360512015-07-08 11:08:35 -0700497 void SetIceConnectionReceiving(bool receiving);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000499 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000500 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000501 // Below methods are helper methods which verifies SDP.
502 bool ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
503 cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000504 std::string* err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000505
506 // Check if a call to SetLocalDescription is acceptable with |action|.
507 bool ExpectSetLocalDescription(Action action);
508 // Check if a call to SetRemoteDescription is acceptable with |action|.
509 bool ExpectSetRemoteDescription(Action action);
510 // Verifies a=setup attribute as per RFC 5763.
511 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
512 Action action);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000513
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +0000514 // Returns true if we are ready to push down the remote candidate.
515 // |remote_desc| is the new remote description, or NULL if the current remote
516 // description should be used. Output |valid| is true if the candidate media
517 // index is valid.
518 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
519 const SessionDescriptionInterface* remote_desc,
520 bool* valid);
521
deadbeef7af91dd2016-12-13 11:29:11 -0800522 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
523 // this session.
524 bool SrtpRequired() const;
525
deadbeef953c2ce2017-01-09 14:53:41 -0800526 // TransportController signal handlers.
deadbeefcbecd352015-09-23 11:50:27 -0700527 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
528 void OnTransportControllerReceiving(bool receiving);
529 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
530 void OnTransportControllerCandidatesGathered(
531 const std::string& transport_name,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700532 const std::vector<cricket::Candidate>& candidates);
533 void OnTransportControllerCandidatesRemoved(
534 const std::vector<cricket::Candidate>& candidates);
deadbeef953c2ce2017-01-09 14:53:41 -0800535 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
deadbeefcbecd352015-09-23 11:50:27 -0700536
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000537 std::string GetSessionErrorMsg();
538
deadbeefcbecd352015-09-23 11:50:27 -0700539 // Invoked when TransportController connection completion is signaled.
540 // Reports stats for all transports in use.
541 void ReportTransportStats();
542
543 // Gather the usage of IPv4/IPv6 as best connection.
jbauchac8869e2015-07-03 01:36:14 -0700544 void ReportBestConnectionState(const cricket::TransportStats& stats);
545
546 void ReportNegotiatedCiphers(const cricket::TransportStats& stats);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000547
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200548 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700549
zhihuang9763d562016-08-05 11:14:50 -0700550 const std::string GetTransportName(const std::string& content_name);
551
deadbeefac22f702017-01-12 21:59:29 -0800552 void DestroyRtcpTransport_n(const std::string& transport_name);
Steve Anton169629a2017-08-30 17:36:36 -0700553 void RemoveAndDestroyVideoChannel(cricket::VideoChannel* video_channel);
554 void DestroyVideoChannel(cricket::VideoChannel* video_channel);
555 void RemoveAndDestroyVoiceChannel(cricket::VoiceChannel* voice_channel);
556 void DestroyVoiceChannel(cricket::VoiceChannel* voice_channel);
zhihuangf5b251b2017-01-12 19:37:48 -0800557 void DestroyDataChannel();
558
zhihuang9763d562016-08-05 11:14:50 -0700559 rtc::Thread* const network_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700560 rtc::Thread* const worker_thread_;
danilchape9021a32016-05-17 01:52:02 -0700561 rtc::Thread* const signaling_thread_;
deadbeefd59daf82015-10-14 15:02:44 -0700562
563 State state_ = STATE_INIT;
564 Error error_ = ERROR_NONE;
565 std::string error_desc_;
566
567 const std::string sid_;
568 bool initial_offerer_ = false;
569
hbosdf6075a2016-12-19 04:58:02 -0800570 const std::unique_ptr<cricket::TransportController> transport_controller_;
deadbeef953c2ce2017-01-09 14:53:41 -0800571 const std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
nisseeaabdf62017-05-05 02:23:02 -0700572 const cricket::MediaConfig media_config_;
573 RtcEventLog* event_log_;
574 Call* call_;
Steve Anton169629a2017-08-30 17:36:36 -0700575 // TODO(steveanton): voice_channels_ and video_channels_ used to be a single
576 // VoiceChannel/VideoChannel respectively but are being changed to support
577 // multiple m= lines in unified plan. But until more work is done, these can
578 // only have 0 or 1 channel each.
579 // These channels are owned by ChannelManager.
580 std::vector<cricket::VoiceChannel*> voice_channels_;
581 std::vector<cricket::VideoChannel*> video_channels_;
deadbeef953c2ce2017-01-09 14:53:41 -0800582 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
583 // when using SCTP.
Steve Anton169629a2017-08-30 17:36:36 -0700584 // TODO(steveanton): This should be changed to a bare pointer because
585 // WebRtcSession doesn't actually own the RtpDataChannel
586 // (ChannelManager does).
deadbeef953c2ce2017-01-09 14:53:41 -0800587 std::unique_ptr<cricket::RtpDataChannel> rtp_data_channel_;
588
589 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
590 // |sctp_transport_name_| keeps track of what DTLS transport the SCTP
591 // transport is using (which can change due to bundling).
592 rtc::Optional<std::string> sctp_transport_name_;
593 // |sctp_content_name_| is the content name (MID) in SDP.
594 rtc::Optional<std::string> sctp_content_name_;
595 // Value cached on signaling thread. Only updated when SctpReadyToSendData
596 // fires on the signaling thread.
597 bool sctp_ready_to_send_data_ = false;
598 // Same as signals provided by SctpTransport, but these are guaranteed to
599 // fire on the signaling thread, whereas SctpTransport fires on the networking
600 // thread.
601 // |sctp_invoker_| is used so that any signals queued on the signaling thread
602 // from the network thread are immediately discarded if the SctpTransport is
603 // destroyed (due to m= section being rejected).
604 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
605 // are marshalled to the right thread. Could almost use proxy.h for this,
606 // but it doesn't have a mechanism for marshalling sigslot::signals
607 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
608 sigslot::signal1<bool> SignalSctpReadyToSendData;
609 sigslot::signal2<const cricket::ReceiveDataParams&,
610 const rtc::CopyOnWriteBuffer&>
611 SignalSctpDataReceived;
612 sigslot::signal1<int> SignalSctpStreamClosedRemotely;
613
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 cricket::ChannelManager* channel_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 IceObserver* ice_observer_;
616 PeerConnectionInterface::IceConnectionState ice_connection_state_;
Peter Thatcher54360512015-07-08 11:08:35 -0700617 bool ice_connection_receiving_;
deadbeeffe4a8a42016-12-20 17:56:17 -0800618 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
619 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
620 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
621 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 // If the remote peer is using a older version of implementation.
623 bool older_version_remote_peer_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000624 bool dtls_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 // Specifies which kind of data channel is allowed. This is controlled
626 // by the chrome command-line flag and constraints:
627 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
628 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
629 // not set or false, SCTP is allowed (DCT_SCTP);
630 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
631 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
632 cricket::DataChannelType data_channel_type_;
deadbeef0ed85b22016-02-23 17:24:52 -0800633 // List of content names for which the remote side triggered an ICE restart.
634 std::set<std::string> pending_ice_restarts_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000635
kwibergd1fe2812016-04-27 06:47:29 -0700636 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000637
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000638 // Member variables for caching global options.
639 cricket::AudioOptions audio_options_;
640 cricket::VideoOptions video_options_;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000641 MetricsObserverInterface* metrics_observer_;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000642
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000643 // Declares the bundle policy for the WebRTCSession.
644 PeerConnectionInterface::BundlePolicy bundle_policy_;
645
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700646 // Declares the RTCP mux policy for the WebRTCSession.
647 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy_;
648
zhihuang184a3fd2016-06-14 11:47:14 -0700649 bool received_first_video_packet_ = false;
650 bool received_first_audio_packet_ = false;
651
henrikg3c089d72015-09-16 05:37:44 -0700652 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSession);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000653};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654} // namespace webrtc
655
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200656#endif // PC_WEBRTCSESSION_H_