blob: e6d7b7ee9d9d68970a99b9f1ddf063aefd69478d [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"
23#include "pc/peerconnectionfactory.h"
Steve Anton2d8609c2018-01-23 16:38:46 -080024#include "pc/peerconnectioninternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "pc/rtcstatscollector.h"
Steve Anton4171afb2017-11-20 10:20:22 -080026#include "pc/rtptransceiver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "pc/statscollector.h"
28#include "pc/streamcollection.h"
Steve Anton75737c02017-11-06 10:37:17 -080029#include "pc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
deadbeefeb459812015-12-15 19:24:43 -080033class MediaStreamObserver;
perkjf0dcfe22016-03-10 18:32:00 +010034class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 11:53:05 -070035class RtcEventLog;
deadbeefab9b2d12015-10-14 11:33:11 -070036
Steve Anton75737c02017-11-06 10:37:17 -080037// PeerConnection is the implementation of the PeerConnection object as defined
38// by the PeerConnectionInterface API surface.
39// The class currently is solely responsible for the following:
40// - Managing the session state machine (signaling state).
41// - Creating and initializing lower-level objects, like PortAllocator and
42// BaseChannels.
43// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
44// objects.
45// - Tracking the current and pending local/remote session descriptions.
46// The class currently is jointly responsible for the following:
47// - Parsing and interpreting SDP.
48// - Generating offers and answers based on the current state.
49// - The ICE state machine.
50// - Generating stats.
Steve Anton2d8609c2018-01-23 16:38:46 -080051class PeerConnection : public PeerConnectionInternal,
Steve Anton75737c02017-11-06 10:37:17 -080052 public DataChannelProviderInterface,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000053 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 public sigslot::has_slots<> {
55 public:
zhihuang38ede132017-06-15 12:52:32 -070056 explicit PeerConnection(PeerConnectionFactory* factory,
57 std::unique_ptr<RtcEventLog> event_log,
58 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
deadbeef653b8e02015-11-11 12:55:10 -080060 bool Initialize(
61 const PeerConnectionInterface::RTCConfiguration& configuration,
kwibergd1fe2812016-04-27 06:47:29 -070062 std::unique_ptr<cricket::PortAllocator> allocator,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020063 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
deadbeef653b8e02015-11-11 12:55:10 -080064 PeerConnectionObserver* observer);
65
deadbeefa67696b2015-09-29 11:56:26 -070066 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
67 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
68 bool AddStream(MediaStreamInterface* local_stream) override;
69 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
Steve Anton2d6c76a2018-01-05 17:10:52 -080071 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -080072 rtc::scoped_refptr<MediaStreamTrackInterface> track,
73 const std::vector<std::string>& stream_labels) override;
deadbeefe1f9d832016-01-14 15:35:42 -080074 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
75 MediaStreamTrackInterface* track,
76 std::vector<MediaStreamInterface*> streams) override;
77 bool RemoveTrack(RtpSenderInterface* sender) override;
78
Steve Anton9158ef62017-11-27 13:01:52 -080079 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
80 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
81 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
82 rtc::scoped_refptr<MediaStreamTrackInterface> track,
83 const RtpTransceiverInit& init) override;
84 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
85 cricket::MediaType media_type) override;
86 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
87 cricket::MediaType media_type,
88 const RtpTransceiverInit& init) override;
89
Steve Anton8c0f7a72017-10-03 10:03:10 -070090 // Gets the DTLS SSL certificate associated with the audio transport on the
91 // remote side. This will become populated once the DTLS connection with the
92 // peer has been completed, as indicated by the ICE connection state
93 // transitioning to kIceConnectionCompleted.
94 // Note that this will be removed once we implement RTCDtlsTransport which
95 // has standardized method for getting this information.
96 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
97 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
98
Zhi Huang70b820f2018-01-27 14:16:15 -080099 // Version of the above method that returns the full certificate chain.
100 std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
101
deadbeefa67696b2015-09-29 11:56:26 -0700102 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
103 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
deadbeeffac06552015-11-25 11:26:01 -0800105 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800106 const std::string& kind,
107 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800108
deadbeef70ab1a12015-09-28 16:53:55 -0700109 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
110 const override;
111 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
112 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800113 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
114 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700115
deadbeefa67696b2015-09-29 11:56:26 -0700116 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700118 const DataChannelInit* config) override;
119 bool GetStats(StatsObserver* observer,
120 webrtc::MediaStreamTrackInterface* track,
121 StatsOutputLevel level) override;
hbos74e1a4f2016-09-15 23:33:01 -0700122 void GetStats(RTCStatsCollectorCallback* callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100123 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
deadbeefa67696b2015-09-29 11:56:26 -0700125 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
deadbeefa67696b2015-09-29 11:56:26 -0700127 IceConnectionState ice_connection_state() override;
128 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
deadbeefa67696b2015-09-29 11:56:26 -0700130 const SessionDescriptionInterface* local_description() const override;
131 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800132 const SessionDescriptionInterface* current_local_description() const override;
133 const SessionDescriptionInterface* current_remote_description()
134 const override;
135 const SessionDescriptionInterface* pending_local_description() const override;
136 const SessionDescriptionInterface* pending_remote_description()
137 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138
139 // JSEP01
htaa2a49d92016-03-04 02:51:39 -0800140 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700141 void CreateOffer(CreateSessionDescriptionObserver* observer,
142 const MediaConstraintsInterface* constraints) override;
143 void CreateOffer(CreateSessionDescriptionObserver* observer,
144 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800145 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700146 void CreateAnswer(CreateSessionDescriptionObserver* observer,
147 const MediaConstraintsInterface* constraints) override;
htaa2a49d92016-03-04 02:51:39 -0800148 void CreateAnswer(CreateSessionDescriptionObserver* observer,
149 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700150 void SetLocalDescription(SetSessionDescriptionObserver* observer,
151 SessionDescriptionInterface* desc) override;
Henrik Boströma4ecf552017-11-23 14:17:07 +0000152 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
153 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 17:48:32 +0100154 void SetRemoteDescription(
155 std::unique_ptr<SessionDescriptionInterface> desc,
156 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
157 override;
deadbeef46c73892016-11-16 19:42:04 -0800158 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 11:56:26 -0700159 bool SetConfiguration(
deadbeef293e9262017-01-11 12:28:30 -0800160 const PeerConnectionInterface::RTCConfiguration& configuration,
161 RTCError* error) override;
162 bool SetConfiguration(
163 const PeerConnectionInterface::RTCConfiguration& configuration) override {
164 return SetConfiguration(configuration, nullptr);
165 }
deadbeefa67696b2015-09-29 11:56:26 -0700166 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700167 bool RemoveIceCandidates(
168 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
deadbeefa67696b2015-09-29 11:56:26 -0700170 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000171
zstein4b979802017-06-02 14:37:37 -0700172 RTCError SetBitrate(const BitrateParameters& bitrate) override;
173
Alex Narest78609d52017-10-20 10:37:47 +0200174 void SetBitrateAllocationStrategy(
175 std::unique_ptr<rtc::BitrateAllocationStrategy>
176 bitrate_allocation_strategy) override;
177
henrika5f6bf242017-11-01 11:06:56 +0100178 void SetAudioPlayout(bool playout) override;
179 void SetAudioRecording(bool recording) override;
180
Elad Alon99c3fe52017-10-13 16:29:40 +0200181 RTC_DEPRECATED bool StartRtcEventLog(rtc::PlatformFile file,
182 int64_t max_size_bytes) override;
Bjorn Tereliusde939432017-11-20 17:38:14 +0100183 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
184 int64_t output_period_ms) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700185 void StopRtcEventLog() override;
186
deadbeefa67696b2015-09-29 11:56:26 -0700187 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188
Steve Anton2d8609c2018-01-23 16:38:46 -0800189 // PeerConnectionInternal implementation.
190 rtc::Thread* network_thread() const override {
191 return factory_->network_thread();
192 }
193 rtc::Thread* worker_thread() const override {
194 return factory_->worker_thread();
195 }
196 rtc::Thread* signaling_thread() const override {
197 return factory_->signaling_thread();
perkjd61bf802016-03-24 03:16:19 -0700198 }
deadbeefab9b2d12015-10-14 11:33:11 -0700199
Steve Antonbe5e2082018-01-24 15:29:17 -0800200 std::string session_id() const override { return session_id_; }
Steve Anton75737c02017-11-06 10:37:17 -0800201
Steve Anton2d8609c2018-01-23 16:38:46 -0800202 bool initial_offerer() const override {
203 return initial_offerer_ && *initial_offerer_;
204 }
Steve Anton75737c02017-11-06 10:37:17 -0800205
Steve Anton2d8609c2018-01-23 16:38:46 -0800206 cricket::VoiceChannel* voice_channel() const override {
Steve Anton3fe1b152017-12-12 10:20:08 -0800207 if (IsUnifiedPlan()) {
Steve Anton2d8609c2018-01-23 16:38:46 -0800208 // TODO(bugs.webrtc.org/8764): Change stats collection to work with
209 // transceivers.
Steve Anton3fe1b152017-12-12 10:20:08 -0800210 return nullptr;
211 }
Steve Anton4171afb2017-11-20 10:20:22 -0800212 return static_cast<cricket::VoiceChannel*>(
213 GetAudioTransceiver()->internal()->channel());
Steve Anton978b8762017-09-29 12:15:02 -0700214 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800215
216 cricket::VideoChannel* video_channel() const override {
Steve Anton3fe1b152017-12-12 10:20:08 -0800217 if (IsUnifiedPlan()) {
Steve Anton2d8609c2018-01-23 16:38:46 -0800218 // TODO(bugs.webrtc.org/8764): Change stats collection to work with
219 // transceivers.
Steve Anton3fe1b152017-12-12 10:20:08 -0800220 return nullptr;
221 }
Steve Anton4171afb2017-11-20 10:20:22 -0800222 return static_cast<cricket::VideoChannel*>(
223 GetVideoTransceiver()->internal()->channel());
Steve Antond5585ca2017-10-23 14:49:26 -0700224 }
Steve Anton978b8762017-09-29 12:15:02 -0700225
Steve Anton2d8609c2018-01-23 16:38:46 -0800226 std::vector<
227 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 10:07:54 -0800228 GetTransceiversInternal() const override {
Steve Anton2d8609c2018-01-23 16:38:46 -0800229 return transceivers_;
230 }
231
232 bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override;
233 bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override;
234
235 sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
236 return SignalDataChannelCreated_;
237 }
238
239 cricket::RtpDataChannel* rtp_data_channel() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800240 return rtp_data_channel_;
Steve Anton978b8762017-09-29 12:15:02 -0700241 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800242
Steve Antonbe5e2082018-01-24 15:29:17 -0800243 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels()
Steve Anton2d8609c2018-01-23 16:38:46 -0800244 const override {
245 return sctp_data_channels_;
246 }
247
248 rtc::Optional<std::string> sctp_content_name() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800249 return sctp_content_name_;
250 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800251
252 rtc::Optional<std::string> sctp_transport_name() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800253 return sctp_transport_name_;
254 }
255
Steve Anton5dfde182018-02-06 10:34:40 -0800256 std::map<std::string, std::string> GetTransportNamesByMid() const override;
257 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
258 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800259 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 10:37:17 -0800260
Steve Anton2d8609c2018-01-23 16:38:46 -0800261 bool GetLocalCertificate(
262 const std::string& transport_name,
263 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
264 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
265 const std::string& transport_name) override;
266 bool IceRestartPending(const std::string& content_name) const override;
267 bool NeedsIceRestart(const std::string& content_name) const override;
268 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Steve Anton7464fca2018-01-19 11:10:37 -0800269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700271 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272
273 private:
Henrik Boström31638672017-11-23 17:48:32 +0100274 class SetRemoteDescriptionObserverAdapter;
275 friend class SetRemoteDescriptionObserverAdapter;
276
Steve Anton4171afb2017-11-20 10:20:22 -0800277 struct RtpSenderInfo {
278 RtpSenderInfo() : first_ssrc(0) {}
279 RtpSenderInfo(const std::string& stream_label,
280 const std::string sender_id,
281 uint32_t ssrc)
282 : stream_label(stream_label), sender_id(sender_id), first_ssrc(ssrc) {}
283 bool operator==(const RtpSenderInfo& other) {
deadbeefbda7e0b2015-12-08 17:13:40 -0800284 return this->stream_label == other.stream_label &&
Steve Anton4171afb2017-11-20 10:20:22 -0800285 this->sender_id == other.sender_id &&
286 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-08 17:13:40 -0800287 }
deadbeefab9b2d12015-10-14 11:33:11 -0700288 std::string stream_label;
Steve Anton4171afb2017-11-20 10:20:22 -0800289 std::string sender_id;
290 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
291 // for communicating with the lower layers.
292 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 11:33:11 -0700293 };
deadbeefab9b2d12015-10-14 11:33:11 -0700294
Steve Antonef65ef12018-01-10 17:15:20 -0800295 struct TrackEvent {
296 rtc::scoped_refptr<RtpReceiverInterface> receiver;
297 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
298 };
299
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700301 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
Steve Anton60776752018-01-10 11:51:34 -0800303 cricket::VoiceMediaChannel* voice_media_channel() const {
304 return voice_channel() ? voice_channel()->media_channel() : nullptr;
305 }
306
307 cricket::VideoMediaChannel* video_media_channel() const {
308 return video_channel() ? video_channel()->media_channel() : nullptr;
309 }
310
Steve Anton4171afb2017-11-20 10:20:22 -0800311 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
312 GetSendersInternal() const;
313 std::vector<
314 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
315 GetReceiversInternal() const;
316
317 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
318 GetAudioTransceiver() const;
319 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
320 GetVideoTransceiver() const;
321
deadbeefab9b2d12015-10-14 11:33:11 -0700322 void CreateAudioReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 10:20:22 -0800323 const RtpSenderInfo& remote_sender_info);
perkjf0dcfe22016-03-10 18:32:00 +0100324
deadbeefab9b2d12015-10-14 11:33:11 -0700325 void CreateVideoReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 10:20:22 -0800326 const RtpSenderInfo& remote_sender_info);
Henrik Boström933d8b02017-10-10 10:05:16 -0700327 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Steve Anton4171afb2017-11-20 10:20:22 -0800328 const RtpSenderInfo& remote_sender_info);
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700329
330 // May be called either by AddStream/RemoveStream, or when a track is
331 // added/removed from a stream previously added via AddStream.
332 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream);
333 void RemoveAudioTrack(AudioTrackInterface* track,
334 MediaStreamInterface* stream);
335 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream);
336 void RemoveVideoTrack(VideoTrackInterface* track,
337 MediaStreamInterface* stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
Steve Antonf9381f02017-12-14 10:23:57 -0800339 // AddTrack implementation when Unified Plan is specified.
340 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
341 rtc::scoped_refptr<MediaStreamTrackInterface> track,
342 const std::vector<std::string>& stream_labels);
343 // AddTrack implementation when Plan B is specified.
344 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
345 rtc::scoped_refptr<MediaStreamTrackInterface> track,
346 const std::vector<std::string>& stream_labels);
347
348 // Returns the first RtpTransceiver suitable for a newly added track, if such
349 // transceiver is available.
350 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
351 FindFirstTransceiverForAddedTrack(
352 rtc::scoped_refptr<MediaStreamTrackInterface> track);
353
354 // RemoveTrack that returns an RTCError.
355 RTCError RemoveTrackInternal(rtc::scoped_refptr<RtpSenderInterface> sender);
356
357 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
358 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender);
359
Steve Anton22da89f2018-01-25 13:58:07 -0800360 // Internal implementation for AddTransceiver family of methods. If
361 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
Steve Anton9158ef62017-11-27 13:01:52 -0800362 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
363 cricket::MediaType media_type,
364 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 13:58:07 -0800365 const RtpTransceiverInit& init,
366 bool fire_callback = true);
Steve Anton9158ef62017-11-27 13:01:52 -0800367
Steve Anton02ee47c2018-01-10 16:26:06 -0800368 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
369 CreateSender(cricket::MediaType media_type,
370 rtc::scoped_refptr<MediaStreamTrackInterface> track,
371 const std::vector<std::string>& stream_labels);
372
373 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
374 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
375
Steve Antonf9381f02017-12-14 10:23:57 -0800376 // Create a new RtpTransceiver of the given type and add it to the list of
377 // transceivers.
378 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Steve Anton02ee47c2018-01-10 16:26:06 -0800379 CreateAndAddTransceiver(
380 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
381 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
382 receiver);
Steve Antonf9381f02017-12-14 10:23:57 -0800383
Steve Antonba818672017-11-06 10:21:57 -0800384 void SetIceConnectionState(IceConnectionState new_state);
385 // Called any time the IceGatheringState changes
386 void OnIceGatheringChange(IceGatheringState new_state);
387 // New ICE candidate has been gathered.
388 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate);
389 // Some local ICE candidates have been removed.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700390 void OnIceCandidatesRemoved(
Steve Antonba818672017-11-06 10:21:57 -0800391 const std::vector<cricket::Candidate>& candidates);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392
Steve Antonba818672017-11-06 10:21:57 -0800393 // Update the state, signaling if necessary.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 void ChangeSignalingState(SignalingState signaling_state);
395
deadbeefeb459812015-12-15 19:24:43 -0800396 // Signals from MediaStreamObserver.
397 void OnAudioTrackAdded(AudioTrackInterface* track,
398 MediaStreamInterface* stream);
399 void OnAudioTrackRemoved(AudioTrackInterface* track,
400 MediaStreamInterface* stream);
401 void OnVideoTrackAdded(VideoTrackInterface* track,
402 MediaStreamInterface* stream);
403 void OnVideoTrackRemoved(VideoTrackInterface* track,
404 MediaStreamInterface* stream);
405
Henrik Boström31638672017-11-23 17:48:32 +0100406 void PostSetSessionDescriptionSuccess(
407 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
409 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700410 void PostCreateSessionDescriptionFailure(
411 CreateSessionDescriptionObserver* observer,
412 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413
Steve Anton8a006912017-12-04 15:25:56 -0800414 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
415 // that return an RTCError instead of invoking a callback.
416 RTCError ApplyLocalDescription(
417 std::unique_ptr<SessionDescriptionInterface> desc);
418 RTCError ApplyRemoteDescription(
419 std::unique_ptr<SessionDescriptionInterface> desc);
420
Steve Antondcc3c022017-12-22 16:02:54 -0800421 // Updates the local RtpTransceivers according to the JSEP rules. Called as
422 // part of setting the local/remote description.
423 RTCError UpdateTransceiversAndDataChannels(
424 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800425 const SessionDescriptionInterface& new_session,
426 const SessionDescriptionInterface* old_local_description,
427 const SessionDescriptionInterface* old_remote_description);
Steve Antondcc3c022017-12-22 16:02:54 -0800428
429 // Either creates or destroys the transceiver's BaseChannel according to the
430 // given media section.
431 RTCError UpdateTransceiverChannel(
432 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
433 transceiver,
434 const cricket::ContentInfo& content,
435 const cricket::ContentGroup* bundle_group);
436
Steve Antonfa2260d2017-12-28 16:38:23 -0800437 // Either creates or destroys the local data channel according to the given
438 // media section.
439 RTCError UpdateDataChannel(cricket::ContentSource source,
440 const cricket::ContentInfo& content,
441 const cricket::ContentGroup* bundle_group);
442
Steve Antondcc3c022017-12-22 16:02:54 -0800443 // Associate the given transceiver according to the JSEP rules.
444 RTCErrorOr<
445 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
446 AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800447 SdpType type,
Steve Antondcc3c022017-12-22 16:02:54 -0800448 size_t mline_index,
449 const cricket::ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800450 const cricket::ContentInfo* old_local_content,
451 const cricket::ContentInfo* old_remote_content);
Steve Antondcc3c022017-12-22 16:02:54 -0800452
453 // Returns the RtpTransceiver, if found, that is associated to the given MID.
454 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
455 GetAssociatedTransceiver(const std::string& mid) const;
456
457 // Returns the RtpTransceiver, if found, that was assigned to the given mline
458 // index in CreateOffer.
459 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
460 GetTransceiverByMLineIndex(size_t mline_index) const;
461
462 // Returns an RtpTransciever, if available, that can be used to receive the
463 // given media type according to JSEP rules.
464 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
465 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
466
Steve Antoned10bd92017-12-05 10:52:59 -0800467 // Returns the media section in the given session description that is
468 // associated with the RtpTransceiver. Returns null if none found or this
469 // RtpTransceiver is not associated. Logic varies depending on the
470 // SdpSemantics specified in the configuration.
471 const cricket::ContentInfo* FindMediaSectionForTransceiver(
472 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
473 transceiver,
474 const SessionDescriptionInterface* sdesc) const;
475
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 bool IsClosed() const {
477 return signaling_state_ == PeerConnectionInterface::kClosed;
478 }
479
deadbeefab9b2d12015-10-14 11:33:11 -0700480 // Returns a MediaSessionOptions struct with options decided by |options|,
481 // the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800482 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
483 offer_answer_options,
484 cricket::MediaSessionOptions* session_options);
485 void GetOptionsForPlanBOffer(
486 const PeerConnectionInterface::RTCOfferAnswerOptions&
487 offer_answer_options,
488 cricket::MediaSessionOptions* session_options);
489 void GetOptionsForUnifiedPlanOffer(
490 const PeerConnectionInterface::RTCOfferAnswerOptions&
491 offer_answer_options,
deadbeefab9b2d12015-10-14 11:33:11 -0700492 cricket::MediaSessionOptions* session_options);
493
Steve Anton22da89f2018-01-25 13:58:07 -0800494 RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options);
495 void RemoveRecvDirectionFromReceivingTransceiversOfType(
496 cricket::MediaType media_type);
497 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
498 std::vector<
499 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
500 GetReceivingTransceiversOfType(cricket::MediaType media_type);
501
deadbeefab9b2d12015-10-14 11:33:11 -0700502 // Returns a MediaSessionOptions struct with options decided by
503 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800504 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
zhihuang1c378ed2017-08-17 14:10:50 -0700505 cricket::MediaSessionOptions* session_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800506 void GetOptionsForPlanBAnswer(
507 const PeerConnectionInterface::RTCOfferAnswerOptions&
508 offer_answer_options,
509 cricket::MediaSessionOptions* session_options);
510 void GetOptionsForUnifiedPlanAnswer(
511 const PeerConnectionInterface::RTCOfferAnswerOptions&
512 offer_answer_options,
513 cricket::MediaSessionOptions* session_options);
htaa2a49d92016-03-04 02:51:39 -0800514
zhihuang1c378ed2017-08-17 14:10:50 -0700515 // Generates MediaDescriptionOptions for the |session_opts| based on existing
516 // local description or remote description.
517 void GenerateMediaDescriptionOptions(
518 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -0800519 RtpTransceiverDirection audio_direction,
520 RtpTransceiverDirection video_direction,
zhihuang1c378ed2017-08-17 14:10:50 -0700521 rtc::Optional<size_t>* audio_index,
522 rtc::Optional<size_t>* video_index,
523 rtc::Optional<size_t>* data_index,
htaa2a49d92016-03-04 02:51:39 -0800524 cricket::MediaSessionOptions* session_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700525
Steve Antonfa2260d2017-12-28 16:38:23 -0800526 // Generates the active MediaDescriptionOptions for the local data channel
527 // given the specified MID.
528 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
529 const std::string& mid) const;
530
531 // Generates the rejected MediaDescriptionOptions for the local data channel
532 // given the specified MID.
533 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
534 const std::string& mid) const;
535
536 // Returns the MID for the data section associated with either the
537 // RtpDataChannel or SCTP data channel, if it has been set. If no data
538 // channels are configured this will return nullopt.
539 rtc::Optional<std::string> GetDataMid() const;
540
Steve Anton4171afb2017-11-20 10:20:22 -0800541 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 15:33:07 -0800542 // Called when a media type is rejected (m-line set to port 0).
Steve Anton4171afb2017-11-20 10:20:22 -0800543 void RemoveSenders(cricket::MediaType media_type);
deadbeeffaac4972015-11-12 15:33:07 -0800544
deadbeefbda7e0b2015-12-08 17:13:40 -0800545 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
546 // and existing MediaStreamTracks are removed if there is no corresponding
547 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
548 // is created if it doesn't exist; if false, it's removed if it exists.
549 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700550 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 10:20:22 -0800551 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -0700552 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800553 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700554 cricket::MediaType media_type,
555 StreamCollection* new_streams);
556
Steve Anton4171afb2017-11-20 10:20:22 -0800557 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700558 // session description. It creates a remote MediaStreamTrackInterface
559 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800560 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
561 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700562
Steve Anton4171afb2017-11-20 10:20:22 -0800563 // Triggered when a remote sender has been removed from a remote session
564 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700565 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800566 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
567 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700568
569 // Finds remote MediaStreams without any tracks and removes them from
570 // |remote_streams_| and notifies the observer that the MediaStreams no longer
571 // exist.
572 void UpdateEndedRemoteMediaStreams();
573
deadbeefab9b2d12015-10-14 11:33:11 -0700574 // Loops through the vector of |streams| and finds added and removed
575 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 10:20:22 -0800576 // For each new or removed StreamParam, OnLocalSenderSeen or
577 // OnLocalSenderRemoved is invoked.
578 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
579 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700580
Steve Anton4171afb2017-11-20 10:20:22 -0800581 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 11:33:11 -0700582 // session description.
583 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
584 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
585 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 10:20:22 -0800586 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
587 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700588
Steve Anton4171afb2017-11-20 10:20:22 -0800589 // Triggered when a local sender has been removed from a local session
deadbeefab9b2d12015-10-14 11:33:11 -0700590 // description.
591 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
592 // has been removed from the local SessionDescription and the stream can be
593 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 10:20:22 -0800594 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
595 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700596
597 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
598 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
599 void UpdateClosingRtpDataChannels(
600 const std::vector<std::string>& active_channels,
601 bool is_local_update);
602 void CreateRemoteRtpDataChannel(const std::string& label,
603 uint32_t remote_ssrc);
604
605 // Creates channel and adds it to the collection of DataChannels that will
606 // be offered in a SessionDescription.
607 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
608 const std::string& label,
609 const InternalDataChannelInit* config);
610
611 // Checks if any data channel has been added.
612 bool HasDataChannels() const;
613
614 void AllocateSctpSids(rtc::SSLRole role);
615 void OnSctpDataChannelClosed(DataChannel* channel);
616
deadbeefab9b2d12015-10-14 11:33:11 -0700617 void OnDataChannelDestroyed();
Steve Antonba818672017-11-06 10:21:57 -0800618 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700619 void OnDataChannelOpenMessage(const std::string& label,
620 const InternalDataChannelInit& config);
621
Steve Anton4171afb2017-11-20 10:20:22 -0800622 // Returns true if the PeerConnection is configured to use Unified Plan
623 // semantics for creating offers/answers and setting local/remote
624 // descriptions. If this is true the RtpTransceiver API will also be available
625 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 10:25:56 -0800626 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
627 // sufficient time has passed.
628 bool IsUnifiedPlan() const {
629 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
630 }
Steve Anton4171afb2017-11-20 10:20:22 -0800631
632 // Is there an RtpSender of the given type?
zhihuang1c378ed2017-08-17 14:10:50 -0700633 bool HasRtpSender(cricket::MediaType type) const;
deadbeeffac06552015-11-25 11:26:01 -0800634
Steve Anton4171afb2017-11-20 10:20:22 -0800635 // Return the RtpSender with the given track attached.
636 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
637 FindSenderForTrack(MediaStreamTrackInterface* track) const;
deadbeef70ab1a12015-09-28 16:53:55 -0700638
Steve Anton4171afb2017-11-20 10:20:22 -0800639 // Return the RtpSender with the given id, or null if none exists.
640 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
641 FindSenderById(const std::string& sender_id) const;
642
643 // Return the RtpReceiver with the given id, or null if none exists.
644 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
645 FindReceiverById(const std::string& receiver_id) const;
646
647 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
648 cricket::MediaType media_type);
649 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
650 cricket::MediaType media_type);
651 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
652 const std::string& stream_label,
653 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 11:33:11 -0700654
655 // Returns the specified SCTP DataChannel in sctp_data_channels_,
656 // or nullptr if not found.
657 DataChannel* FindDataChannelBySid(int sid) const;
658
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700659 // Called when first configuring the port allocator.
deadbeef91dd5672016-05-18 16:55:30 -0700660 bool InitializePortAllocator_n(const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800661 // Called when SetConfiguration is called to apply the supported subset
662 // of the configuration on the network thread.
663 bool ReconfigurePortAllocator_n(
664 const cricket::ServerAddresses& stun_servers,
665 const std::vector<cricket::RelayServerConfig>& turn_servers,
666 IceTransportsType type,
667 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200668 bool prune_turn_ports,
669 webrtc::TurnCustomizer* turn_customizer);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700670
Elad Alon99c3fe52017-10-13 16:29:40 +0200671 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700672 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100673 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
674 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200675
Elad Alonacb24172017-10-06 14:32:13 +0200676 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700677 // This function should only be called from the worker thread.
678 void StopRtcEventLog_w();
679
Steve Anton038834f2017-07-14 15:59:59 -0700680 // Ensures the configuration doesn't have any parameters with invalid values,
681 // or values that conflict with other parameters.
682 //
683 // Returns RTCError::OK() if there are no issues.
684 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
685
Steve Antonba818672017-11-06 10:21:57 -0800686 cricket::ChannelManager* channel_manager() const;
687 MetricsObserverInterface* metrics_observer() const;
688
Steve Antonf8470812017-12-04 10:46:21 -0800689 enum class SessionError {
690 kNone, // No error.
691 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
692 kTransport, // Error from the underlying transport.
693 };
694
Steve Anton75737c02017-11-06 10:37:17 -0800695 // Returns the last error in the session. See the enum above for details.
Steve Antonf8470812017-12-04 10:46:21 -0800696 SessionError session_error() const { return session_error_; }
697 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 10:37:17 -0800698
Steve Anton75737c02017-11-06 10:37:17 -0800699 cricket::BaseChannel* GetChannel(const std::string& content_name);
700
701 // Get current SSL role used by SCTP's underlying transport.
702 bool GetSctpSslRole(rtc::SSLRole* role);
703
Steve Anton75737c02017-11-06 10:37:17 -0800704 cricket::IceConfig ParseIceConfig(
705 const PeerConnectionInterface::RTCConfiguration& config) const;
706
Steve Anton75737c02017-11-06 10:37:17 -0800707 // Implements DataChannelProviderInterface.
708 bool SendData(const cricket::SendDataParams& params,
709 const rtc::CopyOnWriteBuffer& payload,
710 cricket::SendDataResult* result) override;
711 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
712 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
713 void AddSctpDataStream(int sid) override;
714 void RemoveSctpDataStream(int sid) override;
715 bool ReadyToSendData() const override;
716
717 cricket::DataChannelType data_channel_type() const;
718
Steve Anton75737c02017-11-06 10:37:17 -0800719 // Called when an RTCCertificate is generated or retrieved by
720 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
721 void OnCertificateReady(
722 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
723 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
724
725 cricket::TransportController* transport_controller() const {
726 return transport_controller_.get();
727 }
728
Steve Anton75737c02017-11-06 10:37:17 -0800729 // Non-const versions of local_description()/remote_description(), for use
730 // internally.
731 SessionDescriptionInterface* mutable_local_description() {
732 return pending_local_description_ ? pending_local_description_.get()
733 : current_local_description_.get();
734 }
735 SessionDescriptionInterface* mutable_remote_description() {
736 return pending_remote_description_ ? pending_remote_description_.get()
737 : current_remote_description_.get();
738 }
739
740 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 10:46:21 -0800741 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 10:37:17 -0800742
Steve Anton3828c062017-12-06 10:34:51 -0800743 RTCError UpdateSessionState(SdpType type, cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800744 // Push the media parts of the local or remote session description
745 // down to all of the channels.
Steve Anton3828c062017-12-06 10:34:51 -0800746 RTCError PushdownMediaDescription(SdpType type,
Steve Anton8a006912017-12-04 15:25:56 -0800747 cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800748 bool PushdownSctpParameters_n(cricket::ContentSource source);
749
Steve Anton8a006912017-12-04 15:25:56 -0800750 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -0800751 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800752
753 // Returns true and the TransportInfo of the given |content_name|
754 // from |description|. Returns false if it's not available.
755 static bool GetTransportDescription(
756 const cricket::SessionDescription* description,
757 const std::string& content_name,
758 cricket::TransportDescription* info);
759
Steve Antoneda6ccd2017-12-04 10:21:55 -0800760 // Returns the transport name for the given media section identified by |mid|.
761 // If BUNDLE is enabled and the media section is part of the bundle group,
762 // the transport name will be the first mid in the bundle group. Otherwise,
763 // the transport name will be the mid of the media section.
764 std::string GetTransportNameForMediaSection(
765 const std::string& mid,
766 const cricket::ContentGroup* bundle_group) const;
Steve Anton75737c02017-11-06 10:37:17 -0800767
768 // Cause all the BaseChannels in the bundle group to have the same
769 // transport channel.
770 bool EnableBundle(const cricket::ContentGroup& bundle);
771
772 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 10:52:59 -0800773 // This enables media to flow on all configured audio/video channels and the
774 // RtpDataChannel.
775 void EnableSending();
Steve Anton3fe1b152017-12-12 10:20:08 -0800776
Steve Anton8af21862017-12-15 11:20:13 -0800777 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
778 void DestroyAllChannels();
Steve Anton3fe1b152017-12-12 10:20:08 -0800779
Steve Anton75737c02017-11-06 10:37:17 -0800780 // Returns the media index for a local ice candidate given the content name.
781 // Returns false if the local session description does not have a media
782 // content called |content_name|.
783 bool GetLocalCandidateMediaIndex(const std::string& content_name,
784 int* sdp_mline_index);
785 // Uses all remote candidates in |remote_desc| in this session.
786 bool UseCandidatesInSessionDescription(
787 const SessionDescriptionInterface* remote_desc);
788 // Uses |candidate| in this session.
789 bool UseCandidate(const IceCandidateInterface* candidate);
790 // Deletes the corresponding channel of contents that don't exist in |desc|.
791 // |desc| can be null. This means that all channels are deleted.
792 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
793
794 // Allocates media channels based on the |desc|. If |desc| doesn't have
795 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
796 // This method will also delete any existing media channels before creating.
Steve Antondcc3c022017-12-22 16:02:54 -0800797 RTCError CreateChannels(const cricket::SessionDescription& desc);
798
799 // If the BUNDLE policy is max-bundle, then we know for sure that all
800 // transports will be bundled from the start. This method returns the BUNDLE
801 // group if that's the case, or null if BUNDLE will be negotiated later. An
802 // error is returned if max-bundle is specified but the session description
803 // does not have a BUNDLE group.
804 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
805 const cricket::SessionDescription& desc) const;
Steve Anton75737c02017-11-06 10:37:17 -0800806
807 // Helper methods to create media channels.
Steve Antoneda6ccd2017-12-04 10:21:55 -0800808 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid,
809 const std::string& transport_name);
810 cricket::VideoChannel* CreateVideoChannel(const std::string& mid,
811 const std::string& transport_name);
812 bool CreateDataChannel(const std::string& mid,
813 const std::string& transport_name);
Steve Anton75737c02017-11-06 10:37:17 -0800814
Steve Anton75737c02017-11-06 10:37:17 -0800815 bool CreateSctpTransport_n(const std::string& content_name,
816 const std::string& transport_name);
817 // For bundling.
818 void ChangeSctpTransport_n(const std::string& transport_name);
819 void DestroySctpTransport_n();
820 // SctpTransport signal handlers. Needed to marshal signals from the network
821 // to signaling thread.
822 void OnSctpTransportReadyToSendData_n();
823 // This may be called with "false" if the direction of the m= section causes
824 // us to tear down the SCTP connection.
825 void OnSctpTransportReadyToSendData_s(bool ready);
826 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
827 const rtc::CopyOnWriteBuffer& payload);
828 // Beyond just firing the signal to the signaling thread, listens to SCTP
829 // CONTROL messages on unused SIDs and processes them as OPEN messages.
830 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
831 const rtc::CopyOnWriteBuffer& payload);
832 void OnSctpStreamClosedRemotely_n(int sid);
833
834 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
835 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
836 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 15:25:56 -0800837 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
838 cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800839
Steve Anton3828c062017-12-06 10:34:51 -0800840 // Check if a call to SetLocalDescription is acceptable with a session
841 // description of the given type.
842 bool ExpectSetLocalDescription(SdpType type);
843 // Check if a call to SetRemoteDescription is acceptable with a session
844 // description of the given type.
845 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800846 // Verifies a=setup attribute as per RFC 5763.
847 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 10:34:51 -0800848 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800849
850 // Returns true if we are ready to push down the remote candidate.
851 // |remote_desc| is the new remote description, or NULL if the current remote
852 // description should be used. Output |valid| is true if the candidate media
853 // index is valid.
854 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
855 const SessionDescriptionInterface* remote_desc,
856 bool* valid);
857
858 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
859 // this session.
860 bool SrtpRequired() const;
861
862 // TransportController signal handlers.
863 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
864 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
865 void OnTransportControllerCandidatesGathered(
866 const std::string& transport_name,
867 const std::vector<cricket::Candidate>& candidates);
868 void OnTransportControllerCandidatesRemoved(
869 const std::vector<cricket::Candidate>& candidates);
870 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
871
Steve Antonf8470812017-12-04 10:46:21 -0800872 const char* SessionErrorToString(SessionError error) const;
Steve Anton75737c02017-11-06 10:37:17 -0800873 std::string GetSessionErrorMsg();
874
875 // Invoked when TransportController connection completion is signaled.
876 // Reports stats for all transports in use.
877 void ReportTransportStats();
878
879 // Gather the usage of IPv4/IPv6 as best connection.
880 void ReportBestConnectionState(const cricket::TransportStats& stats);
881
Steve Antonc7b964c2018-02-01 14:39:45 -0800882 void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
883 const std::set<cricket::MediaType>& media_types);
Steve Anton75737c02017-11-06 10:37:17 -0800884
885 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
886
887 const std::string GetTransportName(const std::string& content_name);
888
889 void DestroyRtcpTransport_n(const std::string& transport_name);
Steve Anton6fec8802017-12-04 10:37:29 -0800890
891 // Destroys and clears the BaseChannel associated with the given transceiver,
892 // if such channel is set.
893 void DestroyTransceiverChannel(
894 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
895 transceiver);
896
897 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Steve Anton75737c02017-11-06 10:37:17 -0800898 void DestroyDataChannel();
899
Steve Anton6fec8802017-12-04 10:37:29 -0800900 // Destroys the given BaseChannel. The channel cannot be accessed after this
901 // method is called.
902 void DestroyBaseChannel(cricket::BaseChannel* channel);
903
Steve Anton2d8609c2018-01-23 16:38:46 -0800904 sigslot::signal1<DataChannel*> SignalDataChannelCreated_;
905
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 // Storing the factory as a scoped reference pointer ensures that the memory
907 // in the PeerConnectionFactoryImpl remains available as long as the
908 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
909 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700910 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000912 rtc::scoped_refptr<PeerConnectionFactory> factory_;
Steve Antonba818672017-11-06 10:21:57 -0800913 PeerConnectionObserver* observer_ = nullptr;
Taylor Brandstetter215fda72018-01-03 17:14:20 -0800914 rtc::scoped_refptr<UMAObserver> uma_observer_ = nullptr;
terelius33860252017-05-12 23:37:18 -0700915
916 // The EventLog needs to outlive |call_| (and any other object that uses it).
917 std::unique_ptr<RtcEventLog> event_log_;
918
Steve Antonba818672017-11-06 10:21:57 -0800919 SignalingState signaling_state_ = kStable;
920 IceConnectionState ice_connection_state_ = kIceConnectionNew;
921 IceGatheringState ice_gathering_state_ = kIceGatheringNew;
deadbeef46c73892016-11-16 19:42:04 -0800922 PeerConnectionInterface::RTCConfiguration configuration_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923
kwibergd1fe2812016-04-27 06:47:29 -0700924 std::unique_ptr<cricket::PortAllocator> port_allocator_;
deadbeefab9b2d12015-10-14 11:33:11 -0700925
zhihuang8f65cdf2016-05-06 18:40:30 -0700926 // One PeerConnection has only one RTCP CNAME.
927 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
928 std::string rtcp_cname_;
929
deadbeefab9b2d12015-10-14 11:33:11 -0700930 // Streams added via AddStream.
931 rtc::scoped_refptr<StreamCollection> local_streams_;
932 // Streams created as a result of SetRemoteDescription.
933 rtc::scoped_refptr<StreamCollection> remote_streams_;
934
kwibergd1fe2812016-04-27 06:47:29 -0700935 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_;
deadbeefeb459812015-12-15 19:24:43 -0800936
Steve Anton4171afb2017-11-20 10:20:22 -0800937 // These lists store sender info seen in local/remote descriptions.
938 std::vector<RtpSenderInfo> remote_audio_sender_infos_;
939 std::vector<RtpSenderInfo> remote_video_sender_infos_;
940 std::vector<RtpSenderInfo> local_audio_sender_infos_;
941 std::vector<RtpSenderInfo> local_video_sender_infos_;
deadbeefab9b2d12015-10-14 11:33:11 -0700942
943 SctpSidAllocator sid_allocator_;
944 // label -> DataChannel
945 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
946 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -0800947 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -0700948
deadbeefbda7e0b2015-12-08 17:13:40 -0800949 bool remote_peer_supports_msid_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700950
terelius33860252017-05-12 23:37:18 -0700951 std::unique_ptr<Call> call_;
terelius33860252017-05-12 23:37:18 -0700952 std::unique_ptr<StatsCollector> stats_; // A pointer is passed to senders_
953 rtc::scoped_refptr<RTCStatsCollector> stats_collector_;
954
deadbeefa601f5c2016-06-06 14:27:39 -0700955 std::vector<
Steve Anton4171afb2017-11-20 10:20:22 -0800956 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
957 transceivers_;
Steve Antondcc3c022017-12-22 16:02:54 -0800958 // MIDs that have been seen either by SetLocalDescription or
959 // SetRemoteDescription over the life of the PeerConnection.
960 std::set<std::string> seen_mids_;
Steve Anton75737c02017-11-06 10:37:17 -0800961
Steve Antonf8470812017-12-04 10:46:21 -0800962 SessionError session_error_ = SessionError::kNone;
963 std::string session_error_desc_;
Steve Anton75737c02017-11-06 10:37:17 -0800964
965 std::string session_id_;
966 rtc::Optional<bool> initial_offerer_;
967
968 std::unique_ptr<cricket::TransportController> transport_controller_;
969 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
Steve Anton75737c02017-11-06 10:37:17 -0800970 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
971 // when using SCTP.
972 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
973
974 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
975 // |sctp_transport_name_| keeps track of what DTLS transport the SCTP
976 // transport is using (which can change due to bundling).
977 rtc::Optional<std::string> sctp_transport_name_;
978 // |sctp_content_name_| is the content name (MID) in SDP.
979 rtc::Optional<std::string> sctp_content_name_;
980 // Value cached on signaling thread. Only updated when SctpReadyToSendData
981 // fires on the signaling thread.
982 bool sctp_ready_to_send_data_ = false;
983 // Same as signals provided by SctpTransport, but these are guaranteed to
984 // fire on the signaling thread, whereas SctpTransport fires on the networking
985 // thread.
986 // |sctp_invoker_| is used so that any signals queued on the signaling thread
987 // from the network thread are immediately discarded if the SctpTransport is
988 // destroyed (due to m= section being rejected).
989 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
990 // are marshalled to the right thread. Could almost use proxy.h for this,
991 // but it doesn't have a mechanism for marshalling sigslot::signals
992 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
993 sigslot::signal1<bool> SignalSctpReadyToSendData;
994 sigslot::signal2<const cricket::ReceiveDataParams&,
995 const rtc::CopyOnWriteBuffer&>
996 SignalSctpDataReceived;
997 sigslot::signal1<int> SignalSctpStreamClosedRemotely;
998
999 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
1000 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
1001 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
1002 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
1003 bool dtls_enabled_ = false;
1004 // Specifies which kind of data channel is allowed. This is controlled
1005 // by the chrome command-line flag and constraints:
1006 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
1007 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
1008 // not set or false, SCTP is allowed (DCT_SCTP);
1009 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
1010 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
1011 cricket::DataChannelType data_channel_type_ = cricket::DCT_NONE;
1012 // List of content names for which the remote side triggered an ICE restart.
1013 std::set<std::string> pending_ice_restarts_;
1014
1015 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
1016
1017 // Member variables for caching global options.
1018 cricket::AudioOptions audio_options_;
1019 cricket::VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020};
1021
1022} // namespace webrtc
1023
Mirko Bonadei92ea95e2017-09-15 06:47:31 +02001024#endif // PC_PEERCONNECTION_H_