blob: 1b00ef5103620662d87c4137e64f61b0d10c5c0e [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_PEERCONNECTION_H_
12#define PC_PEERCONNECTION_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
perkjd61bf802016-03-24 03:16:19 -070014#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070015#include <memory>
Steve Anton75737c02017-11-06 10:37:17 -080016#include <set>
17#include <string>
perkjd61bf802016-03-24 03:16:19 -070018#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/peerconnectioninterface.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020021#include "api/turncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "pc/iceserverparsing.h"
Zhi Huange830e682018-03-30 10:48:35 -070023#include "pc/jseptransportcontroller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "pc/peerconnectionfactory.h"
Steve Anton2d8609c2018-01-23 16:38:46 -080025#include "pc/peerconnectioninternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "pc/rtcstatscollector.h"
Steve Anton4171afb2017-11-20 10:20:22 -080027#include "pc/rtptransceiver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "pc/statscollector.h"
29#include "pc/streamcollection.h"
Steve Anton75737c02017-11-06 10:37:17 -080030#include "pc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
32namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
deadbeefeb459812015-12-15 19:24:43 -080034class MediaStreamObserver;
perkjf0dcfe22016-03-10 18:32:00 +010035class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 11:53:05 -070036class RtcEventLog;
deadbeefab9b2d12015-10-14 11:33:11 -070037
Steve Anton75737c02017-11-06 10:37:17 -080038// PeerConnection is the implementation of the PeerConnection object as defined
39// by the PeerConnectionInterface API surface.
40// The class currently is solely responsible for the following:
41// - Managing the session state machine (signaling state).
42// - Creating and initializing lower-level objects, like PortAllocator and
43// BaseChannels.
44// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
45// objects.
46// - Tracking the current and pending local/remote session descriptions.
47// The class currently is jointly responsible for the following:
48// - Parsing and interpreting SDP.
49// - Generating offers and answers based on the current state.
50// - The ICE state machine.
51// - Generating stats.
Steve Anton2d8609c2018-01-23 16:38:46 -080052class PeerConnection : public PeerConnectionInternal,
Steve Anton75737c02017-11-06 10:37:17 -080053 public DataChannelProviderInterface,
Zhi Huang365381f2018-04-13 16:44:34 -070054 public JsepTransportController::Observer,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 public sigslot::has_slots<> {
57 public:
zhihuang38ede132017-06-15 12:52:32 -070058 explicit PeerConnection(PeerConnectionFactory* factory,
59 std::unique_ptr<RtcEventLog> event_log,
60 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
deadbeef653b8e02015-11-11 12:55:10 -080062 bool Initialize(
63 const PeerConnectionInterface::RTCConfiguration& configuration,
kwibergd1fe2812016-04-27 06:47:29 -070064 std::unique_ptr<cricket::PortAllocator> allocator,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020065 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
deadbeef653b8e02015-11-11 12:55:10 -080066 PeerConnectionObserver* observer);
67
deadbeefa67696b2015-09-29 11:56:26 -070068 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
69 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
70 bool AddStream(MediaStreamInterface* local_stream) override;
71 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
Steve Anton2d6c76a2018-01-05 17:10:52 -080073 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -080074 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -080075 const std::vector<std::string>& stream_ids) override;
deadbeefe1f9d832016-01-14 15:35:42 -080076 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
77 MediaStreamTrackInterface* track,
78 std::vector<MediaStreamInterface*> streams) override;
79 bool RemoveTrack(RtpSenderInterface* sender) override;
80
Steve Anton9158ef62017-11-27 13:01:52 -080081 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
82 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
83 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
84 rtc::scoped_refptr<MediaStreamTrackInterface> track,
85 const RtpTransceiverInit& init) override;
86 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
87 cricket::MediaType media_type) override;
88 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
89 cricket::MediaType media_type,
90 const RtpTransceiverInit& init) override;
91
Steve Anton8c0f7a72017-10-03 10:03:10 -070092 // Gets the DTLS SSL certificate associated with the audio transport on the
93 // remote side. This will become populated once the DTLS connection with the
94 // peer has been completed, as indicated by the ICE connection state
95 // transitioning to kIceConnectionCompleted.
96 // Note that this will be removed once we implement RTCDtlsTransport which
97 // has standardized method for getting this information.
98 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
99 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
100
Zhi Huang70b820f2018-01-27 14:16:15 -0800101 // Version of the above method that returns the full certificate chain.
102 std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
103
deadbeefa67696b2015-09-29 11:56:26 -0700104 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
105 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
deadbeeffac06552015-11-25 11:26:01 -0800107 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800108 const std::string& kind,
109 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800110
deadbeef70ab1a12015-09-28 16:53:55 -0700111 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
112 const override;
113 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
114 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800115 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
116 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700117
deadbeefa67696b2015-09-29 11:56:26 -0700118 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700120 const DataChannelInit* config) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100121 // WARNING: LEGACY. See peerconnectioninterface.h
deadbeefa67696b2015-09-29 11:56:26 -0700122 bool GetStats(StatsObserver* observer,
123 webrtc::MediaStreamTrackInterface* track,
124 StatsOutputLevel level) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100125 // Spec-complaint GetStats(). See peerconnectioninterface.h
hbos74e1a4f2016-09-15 23:33:01 -0700126 void GetStats(RTCStatsCollectorCallback* callback) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100127 void GetStats(
128 rtc::scoped_refptr<RtpSenderInterface> selector,
129 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
130 void GetStats(
131 rtc::scoped_refptr<RtpReceiverInterface> selector,
132 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100133 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134
deadbeefa67696b2015-09-29 11:56:26 -0700135 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
deadbeefa67696b2015-09-29 11:56:26 -0700137 IceConnectionState ice_connection_state() override;
138 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
deadbeefa67696b2015-09-29 11:56:26 -0700140 const SessionDescriptionInterface* local_description() const override;
141 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800142 const SessionDescriptionInterface* current_local_description() const override;
143 const SessionDescriptionInterface* current_remote_description()
144 const override;
145 const SessionDescriptionInterface* pending_local_description() const override;
146 const SessionDescriptionInterface* pending_remote_description()
147 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
149 // JSEP01
htaa2a49d92016-03-04 02:51:39 -0800150 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700151 void CreateOffer(CreateSessionDescriptionObserver* observer,
152 const MediaConstraintsInterface* constraints) override;
153 void CreateOffer(CreateSessionDescriptionObserver* observer,
154 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800155 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700156 void CreateAnswer(CreateSessionDescriptionObserver* observer,
157 const MediaConstraintsInterface* constraints) override;
htaa2a49d92016-03-04 02:51:39 -0800158 void CreateAnswer(CreateSessionDescriptionObserver* observer,
159 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700160 void SetLocalDescription(SetSessionDescriptionObserver* observer,
161 SessionDescriptionInterface* desc) override;
Henrik Boströma4ecf552017-11-23 14:17:07 +0000162 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
163 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 17:48:32 +0100164 void SetRemoteDescription(
165 std::unique_ptr<SessionDescriptionInterface> desc,
166 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
167 override;
deadbeef46c73892016-11-16 19:42:04 -0800168 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 11:56:26 -0700169 bool SetConfiguration(
deadbeef293e9262017-01-11 12:28:30 -0800170 const PeerConnectionInterface::RTCConfiguration& configuration,
171 RTCError* error) override;
172 bool SetConfiguration(
173 const PeerConnectionInterface::RTCConfiguration& configuration) override {
174 return SetConfiguration(configuration, nullptr);
175 }
deadbeefa67696b2015-09-29 11:56:26 -0700176 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700177 bool RemoveIceCandidates(
178 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179
deadbeefa67696b2015-09-29 11:56:26 -0700180 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000181
zstein4b979802017-06-02 14:37:37 -0700182 RTCError SetBitrate(const BitrateParameters& bitrate) override;
183
Alex Narest78609d52017-10-20 10:37:47 +0200184 void SetBitrateAllocationStrategy(
185 std::unique_ptr<rtc::BitrateAllocationStrategy>
186 bitrate_allocation_strategy) override;
187
henrika5f6bf242017-11-01 11:06:56 +0100188 void SetAudioPlayout(bool playout) override;
189 void SetAudioRecording(bool recording) override;
190
Elad Alon99c3fe52017-10-13 16:29:40 +0200191 RTC_DEPRECATED bool StartRtcEventLog(rtc::PlatformFile file,
192 int64_t max_size_bytes) override;
Bjorn Tereliusde939432017-11-20 17:38:14 +0100193 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
194 int64_t output_period_ms) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700195 void StopRtcEventLog() override;
196
deadbeefa67696b2015-09-29 11:56:26 -0700197 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198
Steve Anton2d8609c2018-01-23 16:38:46 -0800199 // PeerConnectionInternal implementation.
200 rtc::Thread* network_thread() const override {
201 return factory_->network_thread();
202 }
203 rtc::Thread* worker_thread() const override {
204 return factory_->worker_thread();
205 }
206 rtc::Thread* signaling_thread() const override {
207 return factory_->signaling_thread();
perkjd61bf802016-03-24 03:16:19 -0700208 }
deadbeefab9b2d12015-10-14 11:33:11 -0700209
Steve Antonbe5e2082018-01-24 15:29:17 -0800210 std::string session_id() const override { return session_id_; }
Steve Anton75737c02017-11-06 10:37:17 -0800211
Steve Anton2d8609c2018-01-23 16:38:46 -0800212 bool initial_offerer() const override {
Zhi Huange830e682018-03-30 10:48:35 -0700213 return transport_controller_ && transport_controller_->initial_offerer();
Steve Anton2d8609c2018-01-23 16:38:46 -0800214 }
Steve Anton75737c02017-11-06 10:37:17 -0800215
Steve Anton2d8609c2018-01-23 16:38:46 -0800216 std::vector<
217 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 10:07:54 -0800218 GetTransceiversInternal() const override {
Steve Anton2d8609c2018-01-23 16:38:46 -0800219 return transceivers_;
220 }
221
222 bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override;
223 bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override;
224
225 sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
226 return SignalDataChannelCreated_;
227 }
228
229 cricket::RtpDataChannel* rtp_data_channel() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800230 return rtp_data_channel_;
Steve Anton978b8762017-09-29 12:15:02 -0700231 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800232
Steve Antonbe5e2082018-01-24 15:29:17 -0800233 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels()
Steve Anton2d8609c2018-01-23 16:38:46 -0800234 const override {
235 return sctp_data_channels_;
236 }
237
238 rtc::Optional<std::string> sctp_content_name() const override {
Zhi Huange830e682018-03-30 10:48:35 -0700239 return sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -0800240 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800241
Zhi Huange830e682018-03-30 10:48:35 -0700242 rtc::Optional<std::string> sctp_transport_name() const override;
Steve Anton75737c02017-11-06 10:37:17 -0800243
Qingsi Wang72a43a12018-02-20 16:03:18 -0800244 cricket::CandidateStatsList GetPooledCandidateStats() const override;
Steve Anton5dfde182018-02-06 10:34:40 -0800245 std::map<std::string, std::string> GetTransportNamesByMid() const override;
246 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
247 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800248 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 10:37:17 -0800249
Steve Anton2d8609c2018-01-23 16:38:46 -0800250 bool GetLocalCertificate(
251 const std::string& transport_name,
252 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800253 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Steve Anton2d8609c2018-01-23 16:38:46 -0800254 const std::string& transport_name) override;
255 bool IceRestartPending(const std::string& content_name) const override;
256 bool NeedsIceRestart(const std::string& content_name) const override;
257 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Steve Anton7464fca2018-01-19 11:10:37 -0800258
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700260 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
262 private:
Henrik Boström31638672017-11-23 17:48:32 +0100263 class SetRemoteDescriptionObserverAdapter;
264 friend class SetRemoteDescriptionObserverAdapter;
265
Steve Anton4171afb2017-11-20 10:20:22 -0800266 struct RtpSenderInfo {
267 RtpSenderInfo() : first_ssrc(0) {}
Seth Hampson845e8782018-03-02 11:34:10 -0800268 RtpSenderInfo(const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800269 const std::string sender_id,
270 uint32_t ssrc)
Seth Hampson845e8782018-03-02 11:34:10 -0800271 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
Steve Anton4171afb2017-11-20 10:20:22 -0800272 bool operator==(const RtpSenderInfo& other) {
Seth Hampson845e8782018-03-02 11:34:10 -0800273 return this->stream_id == other.stream_id &&
Steve Anton4171afb2017-11-20 10:20:22 -0800274 this->sender_id == other.sender_id &&
275 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-08 17:13:40 -0800276 }
Seth Hampson845e8782018-03-02 11:34:10 -0800277 std::string stream_id;
Steve Anton4171afb2017-11-20 10:20:22 -0800278 std::string sender_id;
279 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
280 // for communicating with the lower layers.
281 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 11:33:11 -0700282 };
deadbeefab9b2d12015-10-14 11:33:11 -0700283
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700285 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286
Steve Antonafb0bb72018-02-20 11:35:37 -0800287 // Plan B helpers for getting the voice/video media channels for the single
288 // audio/video transceiver, if it exists.
289 cricket::VoiceMediaChannel* voice_media_channel() const;
290 cricket::VideoMediaChannel* video_media_channel() const;
Steve Anton60776752018-01-10 11:51:34 -0800291
Steve Anton4171afb2017-11-20 10:20:22 -0800292 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
293 GetSendersInternal() const;
294 std::vector<
295 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
296 GetReceiversInternal() const;
297
298 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
299 GetAudioTransceiver() const;
300 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
301 GetVideoTransceiver() const;
302
Steve Antonafb0bb72018-02-20 11:35:37 -0800303 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
304 GetFirstAudioTransceiver() const;
305
deadbeefab9b2d12015-10-14 11:33:11 -0700306 void CreateAudioReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 10:20:22 -0800307 const RtpSenderInfo& remote_sender_info);
perkjf0dcfe22016-03-10 18:32:00 +0100308
deadbeefab9b2d12015-10-14 11:33:11 -0700309 void CreateVideoReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 10:20:22 -0800310 const RtpSenderInfo& remote_sender_info);
Henrik Boström933d8b02017-10-10 10:05:16 -0700311 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Steve Anton4171afb2017-11-20 10:20:22 -0800312 const RtpSenderInfo& remote_sender_info);
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700313
314 // May be called either by AddStream/RemoveStream, or when a track is
315 // added/removed from a stream previously added via AddStream.
316 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream);
317 void RemoveAudioTrack(AudioTrackInterface* track,
318 MediaStreamInterface* stream);
319 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream);
320 void RemoveVideoTrack(VideoTrackInterface* track,
321 MediaStreamInterface* stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322
Steve Antonf9381f02017-12-14 10:23:57 -0800323 // AddTrack implementation when Unified Plan is specified.
324 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
325 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800326 const std::vector<std::string>& stream_ids);
Steve Antonf9381f02017-12-14 10:23:57 -0800327 // AddTrack implementation when Plan B is specified.
328 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
329 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800330 const std::vector<std::string>& stream_ids);
Steve Antonf9381f02017-12-14 10:23:57 -0800331
332 // Returns the first RtpTransceiver suitable for a newly added track, if such
333 // transceiver is available.
334 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
335 FindFirstTransceiverForAddedTrack(
336 rtc::scoped_refptr<MediaStreamTrackInterface> track);
337
338 // RemoveTrack that returns an RTCError.
339 RTCError RemoveTrackInternal(rtc::scoped_refptr<RtpSenderInterface> sender);
340
341 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
342 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender);
343
Steve Anton22da89f2018-01-25 13:58:07 -0800344 // Internal implementation for AddTransceiver family of methods. If
345 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
Steve Anton9158ef62017-11-27 13:01:52 -0800346 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
347 cricket::MediaType media_type,
348 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 13:58:07 -0800349 const RtpTransceiverInit& init,
350 bool fire_callback = true);
Steve Anton9158ef62017-11-27 13:01:52 -0800351
Steve Anton02ee47c2018-01-10 16:26:06 -0800352 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
353 CreateSender(cricket::MediaType media_type,
354 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800355 const std::vector<std::string>& stream_ids);
Steve Anton02ee47c2018-01-10 16:26:06 -0800356
357 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
358 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
359
Steve Antonf9381f02017-12-14 10:23:57 -0800360 // Create a new RtpTransceiver of the given type and add it to the list of
361 // transceivers.
362 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Steve Anton02ee47c2018-01-10 16:26:06 -0800363 CreateAndAddTransceiver(
364 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
365 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
366 receiver);
Steve Antonf9381f02017-12-14 10:23:57 -0800367
Steve Antonba818672017-11-06 10:21:57 -0800368 void SetIceConnectionState(IceConnectionState new_state);
369 // Called any time the IceGatheringState changes
370 void OnIceGatheringChange(IceGatheringState new_state);
371 // New ICE candidate has been gathered.
372 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate);
373 // Some local ICE candidates have been removed.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700374 void OnIceCandidatesRemoved(
Steve Antonba818672017-11-06 10:21:57 -0800375 const std::vector<cricket::Candidate>& candidates);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376
Steve Antonba818672017-11-06 10:21:57 -0800377 // Update the state, signaling if necessary.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 void ChangeSignalingState(SignalingState signaling_state);
379
deadbeefeb459812015-12-15 19:24:43 -0800380 // Signals from MediaStreamObserver.
381 void OnAudioTrackAdded(AudioTrackInterface* track,
382 MediaStreamInterface* stream);
383 void OnAudioTrackRemoved(AudioTrackInterface* track,
384 MediaStreamInterface* stream);
385 void OnVideoTrackAdded(VideoTrackInterface* track,
386 MediaStreamInterface* stream);
387 void OnVideoTrackRemoved(VideoTrackInterface* track,
388 MediaStreamInterface* stream);
389
Henrik Boström31638672017-11-23 17:48:32 +0100390 void PostSetSessionDescriptionSuccess(
391 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100393 RTCError&& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700394 void PostCreateSessionDescriptionFailure(
395 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100396 RTCError error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397
Steve Anton8a006912017-12-04 15:25:56 -0800398 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
399 // that return an RTCError instead of invoking a callback.
400 RTCError ApplyLocalDescription(
401 std::unique_ptr<SessionDescriptionInterface> desc);
402 RTCError ApplyRemoteDescription(
403 std::unique_ptr<SessionDescriptionInterface> desc);
404
Steve Antondcc3c022017-12-22 16:02:54 -0800405 // Updates the local RtpTransceivers according to the JSEP rules. Called as
406 // part of setting the local/remote description.
407 RTCError UpdateTransceiversAndDataChannels(
408 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800409 const SessionDescriptionInterface& new_session,
410 const SessionDescriptionInterface* old_local_description,
411 const SessionDescriptionInterface* old_remote_description);
Steve Antondcc3c022017-12-22 16:02:54 -0800412
413 // Either creates or destroys the transceiver's BaseChannel according to the
414 // given media section.
415 RTCError UpdateTransceiverChannel(
416 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
417 transceiver,
418 const cricket::ContentInfo& content,
419 const cricket::ContentGroup* bundle_group);
420
Steve Antonfa2260d2017-12-28 16:38:23 -0800421 // Either creates or destroys the local data channel according to the given
422 // media section.
423 RTCError UpdateDataChannel(cricket::ContentSource source,
424 const cricket::ContentInfo& content,
425 const cricket::ContentGroup* bundle_group);
426
Steve Antondcc3c022017-12-22 16:02:54 -0800427 // Associate the given transceiver according to the JSEP rules.
428 RTCErrorOr<
429 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
430 AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800431 SdpType type,
Steve Antondcc3c022017-12-22 16:02:54 -0800432 size_t mline_index,
433 const cricket::ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800434 const cricket::ContentInfo* old_local_content,
435 const cricket::ContentInfo* old_remote_content);
Steve Antondcc3c022017-12-22 16:02:54 -0800436
437 // Returns the RtpTransceiver, if found, that is associated to the given MID.
438 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
439 GetAssociatedTransceiver(const std::string& mid) const;
440
441 // Returns the RtpTransceiver, if found, that was assigned to the given mline
442 // index in CreateOffer.
443 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
444 GetTransceiverByMLineIndex(size_t mline_index) const;
445
446 // Returns an RtpTransciever, if available, that can be used to receive the
447 // given media type according to JSEP rules.
448 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
449 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
450
Steve Antoned10bd92017-12-05 10:52:59 -0800451 // Returns the media section in the given session description that is
452 // associated with the RtpTransceiver. Returns null if none found or this
453 // RtpTransceiver is not associated. Logic varies depending on the
454 // SdpSemantics specified in the configuration.
455 const cricket::ContentInfo* FindMediaSectionForTransceiver(
456 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
457 transceiver,
458 const SessionDescriptionInterface* sdesc) const;
459
Steve Anton52d86772018-02-20 15:48:12 -0800460 void OnNegotiationNeeded();
461
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 bool IsClosed() const {
463 return signaling_state_ == PeerConnectionInterface::kClosed;
464 }
465
deadbeefab9b2d12015-10-14 11:33:11 -0700466 // Returns a MediaSessionOptions struct with options decided by |options|,
467 // the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800468 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
469 offer_answer_options,
470 cricket::MediaSessionOptions* session_options);
471 void GetOptionsForPlanBOffer(
472 const PeerConnectionInterface::RTCOfferAnswerOptions&
473 offer_answer_options,
474 cricket::MediaSessionOptions* session_options);
475 void GetOptionsForUnifiedPlanOffer(
476 const PeerConnectionInterface::RTCOfferAnswerOptions&
477 offer_answer_options,
deadbeefab9b2d12015-10-14 11:33:11 -0700478 cricket::MediaSessionOptions* session_options);
479
Steve Anton22da89f2018-01-25 13:58:07 -0800480 RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options);
481 void RemoveRecvDirectionFromReceivingTransceiversOfType(
482 cricket::MediaType media_type);
483 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
484 std::vector<
485 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
486 GetReceivingTransceiversOfType(cricket::MediaType media_type);
487
deadbeefab9b2d12015-10-14 11:33:11 -0700488 // Returns a MediaSessionOptions struct with options decided by
489 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800490 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
zhihuang1c378ed2017-08-17 14:10:50 -0700491 cricket::MediaSessionOptions* session_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800492 void GetOptionsForPlanBAnswer(
493 const PeerConnectionInterface::RTCOfferAnswerOptions&
494 offer_answer_options,
495 cricket::MediaSessionOptions* session_options);
496 void GetOptionsForUnifiedPlanAnswer(
497 const PeerConnectionInterface::RTCOfferAnswerOptions&
498 offer_answer_options,
499 cricket::MediaSessionOptions* session_options);
htaa2a49d92016-03-04 02:51:39 -0800500
zhihuang1c378ed2017-08-17 14:10:50 -0700501 // Generates MediaDescriptionOptions for the |session_opts| based on existing
502 // local description or remote description.
503 void GenerateMediaDescriptionOptions(
504 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -0800505 RtpTransceiverDirection audio_direction,
506 RtpTransceiverDirection video_direction,
zhihuang1c378ed2017-08-17 14:10:50 -0700507 rtc::Optional<size_t>* audio_index,
508 rtc::Optional<size_t>* video_index,
509 rtc::Optional<size_t>* data_index,
htaa2a49d92016-03-04 02:51:39 -0800510 cricket::MediaSessionOptions* session_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700511
Steve Antonfa2260d2017-12-28 16:38:23 -0800512 // Generates the active MediaDescriptionOptions for the local data channel
513 // given the specified MID.
514 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
515 const std::string& mid) const;
516
517 // Generates the rejected MediaDescriptionOptions for the local data channel
518 // given the specified MID.
519 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
520 const std::string& mid) const;
521
522 // Returns the MID for the data section associated with either the
523 // RtpDataChannel or SCTP data channel, if it has been set. If no data
524 // channels are configured this will return nullopt.
525 rtc::Optional<std::string> GetDataMid() const;
526
Steve Anton4171afb2017-11-20 10:20:22 -0800527 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 15:33:07 -0800528 // Called when a media type is rejected (m-line set to port 0).
Steve Anton4171afb2017-11-20 10:20:22 -0800529 void RemoveSenders(cricket::MediaType media_type);
deadbeeffaac4972015-11-12 15:33:07 -0800530
deadbeefbda7e0b2015-12-08 17:13:40 -0800531 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
532 // and existing MediaStreamTracks are removed if there is no corresponding
533 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
534 // is created if it doesn't exist; if false, it's removed if it exists.
535 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700536 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 10:20:22 -0800537 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -0700538 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800539 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700540 cricket::MediaType media_type,
541 StreamCollection* new_streams);
542
Steve Anton4171afb2017-11-20 10:20:22 -0800543 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700544 // session description. It creates a remote MediaStreamTrackInterface
545 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800546 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
547 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700548
Steve Anton4171afb2017-11-20 10:20:22 -0800549 // Triggered when a remote sender has been removed from a remote session
550 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700551 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800552 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
553 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700554
555 // Finds remote MediaStreams without any tracks and removes them from
556 // |remote_streams_| and notifies the observer that the MediaStreams no longer
557 // exist.
558 void UpdateEndedRemoteMediaStreams();
559
deadbeefab9b2d12015-10-14 11:33:11 -0700560 // Loops through the vector of |streams| and finds added and removed
561 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 10:20:22 -0800562 // For each new or removed StreamParam, OnLocalSenderSeen or
563 // OnLocalSenderRemoved is invoked.
564 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
565 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700566
Steve Anton4171afb2017-11-20 10:20:22 -0800567 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 11:33:11 -0700568 // session description.
569 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
570 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
571 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 10:20:22 -0800572 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
573 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700574
Steve Anton4171afb2017-11-20 10:20:22 -0800575 // Triggered when a local sender has been removed from a local session
deadbeefab9b2d12015-10-14 11:33:11 -0700576 // description.
577 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
578 // has been removed from the local SessionDescription and the stream can be
579 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 10:20:22 -0800580 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
581 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700582
583 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
584 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
585 void UpdateClosingRtpDataChannels(
586 const std::vector<std::string>& active_channels,
587 bool is_local_update);
588 void CreateRemoteRtpDataChannel(const std::string& label,
589 uint32_t remote_ssrc);
590
591 // Creates channel and adds it to the collection of DataChannels that will
592 // be offered in a SessionDescription.
593 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
594 const std::string& label,
595 const InternalDataChannelInit* config);
596
597 // Checks if any data channel has been added.
598 bool HasDataChannels() const;
599
600 void AllocateSctpSids(rtc::SSLRole role);
601 void OnSctpDataChannelClosed(DataChannel* channel);
602
deadbeefab9b2d12015-10-14 11:33:11 -0700603 void OnDataChannelDestroyed();
Steve Antonba818672017-11-06 10:21:57 -0800604 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700605 void OnDataChannelOpenMessage(const std::string& label,
606 const InternalDataChannelInit& config);
607
Steve Anton4171afb2017-11-20 10:20:22 -0800608 // Returns true if the PeerConnection is configured to use Unified Plan
609 // semantics for creating offers/answers and setting local/remote
610 // descriptions. If this is true the RtpTransceiver API will also be available
611 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 10:25:56 -0800612 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
613 // sufficient time has passed.
614 bool IsUnifiedPlan() const {
615 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
616 }
Steve Anton4171afb2017-11-20 10:20:22 -0800617
618 // Is there an RtpSender of the given type?
zhihuang1c378ed2017-08-17 14:10:50 -0700619 bool HasRtpSender(cricket::MediaType type) const;
deadbeeffac06552015-11-25 11:26:01 -0800620
Steve Anton4171afb2017-11-20 10:20:22 -0800621 // Return the RtpSender with the given track attached.
622 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
623 FindSenderForTrack(MediaStreamTrackInterface* track) const;
deadbeef70ab1a12015-09-28 16:53:55 -0700624
Steve Anton4171afb2017-11-20 10:20:22 -0800625 // Return the RtpSender with the given id, or null if none exists.
626 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
627 FindSenderById(const std::string& sender_id) const;
628
629 // Return the RtpReceiver with the given id, or null if none exists.
630 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
631 FindReceiverById(const std::string& receiver_id) const;
632
633 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
634 cricket::MediaType media_type);
635 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
636 cricket::MediaType media_type);
637 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
Emircan Uysalerbc609ea2018-03-27 21:57:18 +0000638 const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800639 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 11:33:11 -0700640
641 // Returns the specified SCTP DataChannel in sctp_data_channels_,
642 // or nullptr if not found.
643 DataChannel* FindDataChannelBySid(int sid) const;
644
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700645 // Called when first configuring the port allocator.
deadbeef91dd5672016-05-18 16:55:30 -0700646 bool InitializePortAllocator_n(const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800647 // Called when SetConfiguration is called to apply the supported subset
648 // of the configuration on the network thread.
649 bool ReconfigurePortAllocator_n(
650 const cricket::ServerAddresses& stun_servers,
651 const std::vector<cricket::RelayServerConfig>& turn_servers,
652 IceTransportsType type,
653 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200654 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800655 webrtc::TurnCustomizer* turn_customizer,
656 rtc::Optional<int> stun_candidate_keepalive_interval);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700657
Qingsi Wanga2d60672018-04-11 16:57:45 -0700658 void SetMetricObserver_n(UMAObserver* observer);
659
Elad Alon99c3fe52017-10-13 16:29:40 +0200660 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700661 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100662 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
663 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200664
Elad Alonacb24172017-10-06 14:32:13 +0200665 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700666 // This function should only be called from the worker thread.
667 void StopRtcEventLog_w();
668
Steve Anton038834f2017-07-14 15:59:59 -0700669 // Ensures the configuration doesn't have any parameters with invalid values,
670 // or values that conflict with other parameters.
671 //
672 // Returns RTCError::OK() if there are no issues.
673 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
674
Steve Antonba818672017-11-06 10:21:57 -0800675 cricket::ChannelManager* channel_manager() const;
676 MetricsObserverInterface* metrics_observer() const;
677
Steve Antonf8470812017-12-04 10:46:21 -0800678 enum class SessionError {
679 kNone, // No error.
680 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
681 kTransport, // Error from the underlying transport.
682 };
683
Steve Anton75737c02017-11-06 10:37:17 -0800684 // Returns the last error in the session. See the enum above for details.
Steve Antonf8470812017-12-04 10:46:21 -0800685 SessionError session_error() const { return session_error_; }
686 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 10:37:17 -0800687
Steve Anton75737c02017-11-06 10:37:17 -0800688 cricket::BaseChannel* GetChannel(const std::string& content_name);
689
690 // Get current SSL role used by SCTP's underlying transport.
691 bool GetSctpSslRole(rtc::SSLRole* role);
692
Steve Anton75737c02017-11-06 10:37:17 -0800693 cricket::IceConfig ParseIceConfig(
694 const PeerConnectionInterface::RTCConfiguration& config) const;
695
Steve Anton75737c02017-11-06 10:37:17 -0800696 // Implements DataChannelProviderInterface.
697 bool SendData(const cricket::SendDataParams& params,
698 const rtc::CopyOnWriteBuffer& payload,
699 cricket::SendDataResult* result) override;
700 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
701 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
702 void AddSctpDataStream(int sid) override;
703 void RemoveSctpDataStream(int sid) override;
704 bool ReadyToSendData() const override;
705
706 cricket::DataChannelType data_channel_type() const;
707
Steve Anton75737c02017-11-06 10:37:17 -0800708 // Called when an RTCCertificate is generated or retrieved by
709 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
710 void OnCertificateReady(
711 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
712 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
713
Zhi Huange830e682018-03-30 10:48:35 -0700714 JsepTransportController* transport_controller() const {
Steve Anton75737c02017-11-06 10:37:17 -0800715 return transport_controller_.get();
716 }
717
Steve Anton75737c02017-11-06 10:37:17 -0800718 // Non-const versions of local_description()/remote_description(), for use
719 // internally.
720 SessionDescriptionInterface* mutable_local_description() {
721 return pending_local_description_ ? pending_local_description_.get()
722 : current_local_description_.get();
723 }
724 SessionDescriptionInterface* mutable_remote_description() {
725 return pending_remote_description_ ? pending_remote_description_.get()
726 : current_remote_description_.get();
727 }
728
729 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 10:46:21 -0800730 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 10:37:17 -0800731
Zhi Huange830e682018-03-30 10:48:35 -0700732 RTCError UpdateSessionState(SdpType type,
733 cricket::ContentSource source,
734 const cricket::SessionDescription* description);
Steve Anton75737c02017-11-06 10:37:17 -0800735 // Push the media parts of the local or remote session description
736 // down to all of the channels.
Steve Anton3828c062017-12-06 10:34:51 -0800737 RTCError PushdownMediaDescription(SdpType type,
Steve Anton8a006912017-12-04 15:25:56 -0800738 cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800739 bool PushdownSctpParameters_n(cricket::ContentSource source);
740
Steve Anton8a006912017-12-04 15:25:56 -0800741 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -0800742 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800743
744 // Returns true and the TransportInfo of the given |content_name|
745 // from |description|. Returns false if it's not available.
746 static bool GetTransportDescription(
747 const cricket::SessionDescription* description,
748 const std::string& content_name,
749 cricket::TransportDescription* info);
750
Steve Anton75737c02017-11-06 10:37:17 -0800751 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 10:52:59 -0800752 // This enables media to flow on all configured audio/video channels and the
753 // RtpDataChannel.
754 void EnableSending();
Steve Anton3fe1b152017-12-12 10:20:08 -0800755
Steve Anton8af21862017-12-15 11:20:13 -0800756 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
757 void DestroyAllChannels();
Steve Anton3fe1b152017-12-12 10:20:08 -0800758
Steve Anton75737c02017-11-06 10:37:17 -0800759 // Returns the media index for a local ice candidate given the content name.
760 // Returns false if the local session description does not have a media
761 // content called |content_name|.
762 bool GetLocalCandidateMediaIndex(const std::string& content_name,
763 int* sdp_mline_index);
764 // Uses all remote candidates in |remote_desc| in this session.
765 bool UseCandidatesInSessionDescription(
766 const SessionDescriptionInterface* remote_desc);
767 // Uses |candidate| in this session.
768 bool UseCandidate(const IceCandidateInterface* candidate);
769 // Deletes the corresponding channel of contents that don't exist in |desc|.
770 // |desc| can be null. This means that all channels are deleted.
771 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
772
773 // Allocates media channels based on the |desc|. If |desc| doesn't have
774 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
775 // This method will also delete any existing media channels before creating.
Steve Antondcc3c022017-12-22 16:02:54 -0800776 RTCError CreateChannels(const cricket::SessionDescription& desc);
777
778 // If the BUNDLE policy is max-bundle, then we know for sure that all
779 // transports will be bundled from the start. This method returns the BUNDLE
780 // group if that's the case, or null if BUNDLE will be negotiated later. An
781 // error is returned if max-bundle is specified but the session description
782 // does not have a BUNDLE group.
783 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
784 const cricket::SessionDescription& desc) const;
Steve Anton75737c02017-11-06 10:37:17 -0800785
786 // Helper methods to create media channels.
Zhi Huange830e682018-03-30 10:48:35 -0700787 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
788 cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
789 bool CreateDataChannel(const std::string& mid);
Steve Anton75737c02017-11-06 10:37:17 -0800790
Zhi Huange830e682018-03-30 10:48:35 -0700791 bool CreateSctpTransport_n(const std::string& mid);
Steve Anton75737c02017-11-06 10:37:17 -0800792 // For bundling.
Steve Anton75737c02017-11-06 10:37:17 -0800793 void DestroySctpTransport_n();
794 // SctpTransport signal handlers. Needed to marshal signals from the network
795 // to signaling thread.
796 void OnSctpTransportReadyToSendData_n();
797 // This may be called with "false" if the direction of the m= section causes
798 // us to tear down the SCTP connection.
799 void OnSctpTransportReadyToSendData_s(bool ready);
800 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
801 const rtc::CopyOnWriteBuffer& payload);
802 // Beyond just firing the signal to the signaling thread, listens to SCTP
803 // CONTROL messages on unused SIDs and processes them as OPEN messages.
804 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
805 const rtc::CopyOnWriteBuffer& payload);
806 void OnSctpStreamClosedRemotely_n(int sid);
807
808 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
809 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
810 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 15:25:56 -0800811 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
812 cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800813
Steve Anton3828c062017-12-06 10:34:51 -0800814 // Check if a call to SetLocalDescription is acceptable with a session
815 // description of the given type.
816 bool ExpectSetLocalDescription(SdpType type);
817 // Check if a call to SetRemoteDescription is acceptable with a session
818 // description of the given type.
819 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800820 // Verifies a=setup attribute as per RFC 5763.
821 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 10:34:51 -0800822 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800823
824 // Returns true if we are ready to push down the remote candidate.
825 // |remote_desc| is the new remote description, or NULL if the current remote
826 // description should be used. Output |valid| is true if the candidate media
827 // index is valid.
828 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
829 const SessionDescriptionInterface* remote_desc,
830 bool* valid);
831
832 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
833 // this session.
834 bool SrtpRequired() const;
835
Zhi Huange830e682018-03-30 10:48:35 -0700836 // JsepTransportController signal handlers.
Steve Anton75737c02017-11-06 10:37:17 -0800837 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
838 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
839 void OnTransportControllerCandidatesGathered(
840 const std::string& transport_name,
841 const std::vector<cricket::Candidate>& candidates);
842 void OnTransportControllerCandidatesRemoved(
843 const std::vector<cricket::Candidate>& candidates);
844 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
845
Steve Antonf8470812017-12-04 10:46:21 -0800846 const char* SessionErrorToString(SessionError error) const;
Steve Anton75737c02017-11-06 10:37:17 -0800847 std::string GetSessionErrorMsg();
848
Steve Anton8e20f172018-03-06 10:55:04 -0800849 // Report the UMA metric SdpFormatReceived for the given remote offer.
850 void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
851
Steve Anton0ffaaa22018-02-23 10:31:30 -0800852 // Report inferred negotiated SDP semantics from a local/remote answer to the
853 // UMA observer.
854 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
855
Steve Anton75737c02017-11-06 10:37:17 -0800856 // Invoked when TransportController connection completion is signaled.
857 // Reports stats for all transports in use.
858 void ReportTransportStats();
859
860 // Gather the usage of IPv4/IPv6 as best connection.
861 void ReportBestConnectionState(const cricket::TransportStats& stats);
862
Steve Antonc7b964c2018-02-01 14:39:45 -0800863 void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
864 const std::set<cricket::MediaType>& media_types);
Steve Anton75737c02017-11-06 10:37:17 -0800865
866 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
867
868 const std::string GetTransportName(const std::string& content_name);
869
Steve Anton6fec8802017-12-04 10:37:29 -0800870 // Destroys and clears the BaseChannel associated with the given transceiver,
871 // if such channel is set.
872 void DestroyTransceiverChannel(
873 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
874 transceiver);
875
876 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Steve Anton75737c02017-11-06 10:37:17 -0800877 void DestroyDataChannel();
878
Steve Anton6fec8802017-12-04 10:37:29 -0800879 // Destroys the given BaseChannel. The channel cannot be accessed after this
880 // method is called.
881 void DestroyBaseChannel(cricket::BaseChannel* channel);
882
Zhi Huang365381f2018-04-13 16:44:34 -0700883 // JsepTransportController::Observer override.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700884 //
885 // Called by |transport_controller_| when processing transport information
886 // from a session description, and the mapping from m= sections to transports
887 // changed (as a result of BUNDLE negotiation, or m= sections being
888 // rejected).
889 bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -0700890 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700891 RtpTransportInternal* rtp_transport,
Zhi Huang365381f2018-04-13 16:44:34 -0700892 cricket::DtlsTransportInternal* dtls_transport) override;
Zhi Huange830e682018-03-30 10:48:35 -0700893
Steve Anton2d8609c2018-01-23 16:38:46 -0800894 sigslot::signal1<DataChannel*> SignalDataChannelCreated_;
895
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 // Storing the factory as a scoped reference pointer ensures that the memory
897 // in the PeerConnectionFactoryImpl remains available as long as the
898 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
899 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700900 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000902 rtc::scoped_refptr<PeerConnectionFactory> factory_;
Steve Antonba818672017-11-06 10:21:57 -0800903 PeerConnectionObserver* observer_ = nullptr;
Taylor Brandstetter215fda72018-01-03 17:14:20 -0800904 rtc::scoped_refptr<UMAObserver> uma_observer_ = nullptr;
terelius33860252017-05-12 23:37:18 -0700905
906 // The EventLog needs to outlive |call_| (and any other object that uses it).
907 std::unique_ptr<RtcEventLog> event_log_;
908
Steve Antonba818672017-11-06 10:21:57 -0800909 SignalingState signaling_state_ = kStable;
910 IceConnectionState ice_connection_state_ = kIceConnectionNew;
911 IceGatheringState ice_gathering_state_ = kIceGatheringNew;
deadbeef46c73892016-11-16 19:42:04 -0800912 PeerConnectionInterface::RTCConfiguration configuration_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913
kwibergd1fe2812016-04-27 06:47:29 -0700914 std::unique_ptr<cricket::PortAllocator> port_allocator_;
Qingsi Wanga2d60672018-04-11 16:57:45 -0700915 int port_allocator_flags_ = 0;
deadbeefab9b2d12015-10-14 11:33:11 -0700916
zhihuang8f65cdf2016-05-06 18:40:30 -0700917 // One PeerConnection has only one RTCP CNAME.
918 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
919 std::string rtcp_cname_;
920
deadbeefab9b2d12015-10-14 11:33:11 -0700921 // Streams added via AddStream.
922 rtc::scoped_refptr<StreamCollection> local_streams_;
923 // Streams created as a result of SetRemoteDescription.
924 rtc::scoped_refptr<StreamCollection> remote_streams_;
925
kwibergd1fe2812016-04-27 06:47:29 -0700926 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_;
deadbeefeb459812015-12-15 19:24:43 -0800927
Steve Anton4171afb2017-11-20 10:20:22 -0800928 // These lists store sender info seen in local/remote descriptions.
929 std::vector<RtpSenderInfo> remote_audio_sender_infos_;
930 std::vector<RtpSenderInfo> remote_video_sender_infos_;
931 std::vector<RtpSenderInfo> local_audio_sender_infos_;
932 std::vector<RtpSenderInfo> local_video_sender_infos_;
deadbeefab9b2d12015-10-14 11:33:11 -0700933
934 SctpSidAllocator sid_allocator_;
935 // label -> DataChannel
936 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
937 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -0800938 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -0700939
deadbeefbda7e0b2015-12-08 17:13:40 -0800940 bool remote_peer_supports_msid_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700941
terelius33860252017-05-12 23:37:18 -0700942 std::unique_ptr<Call> call_;
terelius33860252017-05-12 23:37:18 -0700943 std::unique_ptr<StatsCollector> stats_; // A pointer is passed to senders_
944 rtc::scoped_refptr<RTCStatsCollector> stats_collector_;
945
deadbeefa601f5c2016-06-06 14:27:39 -0700946 std::vector<
Steve Anton4171afb2017-11-20 10:20:22 -0800947 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
948 transceivers_;
Steve Antondcc3c022017-12-22 16:02:54 -0800949 // MIDs that have been seen either by SetLocalDescription or
950 // SetRemoteDescription over the life of the PeerConnection.
951 std::set<std::string> seen_mids_;
Steve Anton75737c02017-11-06 10:37:17 -0800952
Steve Antonf8470812017-12-04 10:46:21 -0800953 SessionError session_error_ = SessionError::kNone;
954 std::string session_error_desc_;
Steve Anton75737c02017-11-06 10:37:17 -0800955
956 std::string session_id_;
Steve Anton75737c02017-11-06 10:37:17 -0800957
Zhi Huange830e682018-03-30 10:48:35 -0700958 std::unique_ptr<JsepTransportController> transport_controller_;
Steve Anton75737c02017-11-06 10:37:17 -0800959 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
Steve Anton75737c02017-11-06 10:37:17 -0800960 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
961 // when using SCTP.
962 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
963
964 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
Zhi Huange830e682018-03-30 10:48:35 -0700965 // |sctp_mid_| is the content name (MID) in SDP.
966 rtc::Optional<std::string> sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -0800967 // Value cached on signaling thread. Only updated when SctpReadyToSendData
968 // fires on the signaling thread.
969 bool sctp_ready_to_send_data_ = false;
970 // Same as signals provided by SctpTransport, but these are guaranteed to
971 // fire on the signaling thread, whereas SctpTransport fires on the networking
972 // thread.
973 // |sctp_invoker_| is used so that any signals queued on the signaling thread
974 // from the network thread are immediately discarded if the SctpTransport is
975 // destroyed (due to m= section being rejected).
976 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
977 // are marshalled to the right thread. Could almost use proxy.h for this,
978 // but it doesn't have a mechanism for marshalling sigslot::signals
979 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
980 sigslot::signal1<bool> SignalSctpReadyToSendData;
981 sigslot::signal2<const cricket::ReceiveDataParams&,
982 const rtc::CopyOnWriteBuffer&>
983 SignalSctpDataReceived;
984 sigslot::signal1<int> SignalSctpStreamClosedRemotely;
985
986 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
987 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
988 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
989 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
990 bool dtls_enabled_ = false;
991 // Specifies which kind of data channel is allowed. This is controlled
992 // by the chrome command-line flag and constraints:
993 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
994 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
995 // not set or false, SCTP is allowed (DCT_SCTP);
996 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
997 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
998 cricket::DataChannelType data_channel_type_ = cricket::DCT_NONE;
999 // List of content names for which the remote side triggered an ICE restart.
1000 std::set<std::string> pending_ice_restarts_;
1001
1002 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
1003
1004 // Member variables for caching global options.
1005 cricket::AudioOptions audio_options_;
1006 cricket::VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007};
1008
1009} // namespace webrtc
1010
Mirko Bonadei92ea95e2017-09-15 06:47:31 +02001011#endif // PC_PEERCONNECTION_H_