blob: 4c7973bd1f89f0bfdac17071edd5bb258e7b33f0 [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"
24#include "pc/rtcstatscollector.h"
Steve Anton4171afb2017-11-20 10:20:22 -080025#include "pc/rtptransceiver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "pc/statscollector.h"
27#include "pc/streamcollection.h"
Steve Anton75737c02017-11-06 10:37:17 -080028#include "pc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
deadbeefeb459812015-12-15 19:24:43 -080032class MediaStreamObserver;
perkjf0dcfe22016-03-10 18:32:00 +010033class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 11:53:05 -070034class RtcEventLog;
deadbeefab9b2d12015-10-14 11:33:11 -070035
Steve Anton75737c02017-11-06 10:37:17 -080036// Statistics for all the transports of the session.
37// TODO(pthatcher): Think of a better name for this. We already have
38// a TransportStats in transport.h. Perhaps TransportsStats?
39struct SessionStats {
Steve Anton75737c02017-11-06 10:37:17 -080040 std::map<std::string, cricket::TransportStats> transport_stats;
41};
Steve Antonba818672017-11-06 10:21:57 -080042
Steve Anton75737c02017-11-06 10:37:17 -080043struct ChannelNamePair {
44 ChannelNamePair(const std::string& content_name,
45 const std::string& transport_name)
46 : content_name(content_name), transport_name(transport_name) {}
47 std::string content_name;
48 std::string transport_name;
49};
50
51struct ChannelNamePairs {
52 rtc::Optional<ChannelNamePair> voice;
53 rtc::Optional<ChannelNamePair> video;
54 rtc::Optional<ChannelNamePair> data;
55};
56
57// PeerConnection is the implementation of the PeerConnection object as defined
58// by the PeerConnectionInterface API surface.
59// The class currently is solely responsible for the following:
60// - Managing the session state machine (signaling state).
61// - Creating and initializing lower-level objects, like PortAllocator and
62// BaseChannels.
63// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
64// objects.
65// - Tracking the current and pending local/remote session descriptions.
66// The class currently is jointly responsible for the following:
67// - Parsing and interpreting SDP.
68// - Generating offers and answers based on the current state.
69// - The ICE state machine.
70// - Generating stats.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071class PeerConnection : public PeerConnectionInterface,
Steve Anton75737c02017-11-06 10:37:17 -080072 public DataChannelProviderInterface,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 public sigslot::has_slots<> {
75 public:
zhihuang38ede132017-06-15 12:52:32 -070076 explicit PeerConnection(PeerConnectionFactory* factory,
77 std::unique_ptr<RtcEventLog> event_log,
78 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
deadbeef653b8e02015-11-11 12:55:10 -080080 bool Initialize(
81 const PeerConnectionInterface::RTCConfiguration& configuration,
kwibergd1fe2812016-04-27 06:47:29 -070082 std::unique_ptr<cricket::PortAllocator> allocator,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020083 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
deadbeef653b8e02015-11-11 12:55:10 -080084 PeerConnectionObserver* observer);
85
deadbeefa67696b2015-09-29 11:56:26 -070086 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
87 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
88 bool AddStream(MediaStreamInterface* local_stream) override;
89 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
Steve Anton2d6c76a2018-01-05 17:10:52 -080091 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -080092 rtc::scoped_refptr<MediaStreamTrackInterface> track,
93 const std::vector<std::string>& stream_labels) override;
deadbeefe1f9d832016-01-14 15:35:42 -080094 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
95 MediaStreamTrackInterface* track,
96 std::vector<MediaStreamInterface*> streams) override;
97 bool RemoveTrack(RtpSenderInterface* sender) override;
98
Steve Anton9158ef62017-11-27 13:01:52 -080099 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
100 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
101 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
102 rtc::scoped_refptr<MediaStreamTrackInterface> track,
103 const RtpTransceiverInit& init) override;
104 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
105 cricket::MediaType media_type) override;
106 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
107 cricket::MediaType media_type,
108 const RtpTransceiverInit& init) override;
109
Steve Anton8c0f7a72017-10-03 10:03:10 -0700110 // Gets the DTLS SSL certificate associated with the audio transport on the
111 // remote side. This will become populated once the DTLS connection with the
112 // peer has been completed, as indicated by the ICE connection state
113 // transitioning to kIceConnectionCompleted.
114 // Note that this will be removed once we implement RTCDtlsTransport which
115 // has standardized method for getting this information.
116 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
117 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
118
deadbeefa67696b2015-09-29 11:56:26 -0700119 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
120 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
deadbeeffac06552015-11-25 11:26:01 -0800122 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800123 const std::string& kind,
124 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800125
deadbeef70ab1a12015-09-28 16:53:55 -0700126 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
127 const override;
128 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
129 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800130 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
131 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700132
deadbeefa67696b2015-09-29 11:56:26 -0700133 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700135 const DataChannelInit* config) override;
136 bool GetStats(StatsObserver* observer,
137 webrtc::MediaStreamTrackInterface* track,
138 StatsOutputLevel level) override;
hbos74e1a4f2016-09-15 23:33:01 -0700139 void GetStats(RTCStatsCollectorCallback* callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100140 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141
deadbeefa67696b2015-09-29 11:56:26 -0700142 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143
deadbeefa67696b2015-09-29 11:56:26 -0700144 IceConnectionState ice_connection_state() override;
145 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
deadbeefa67696b2015-09-29 11:56:26 -0700147 const SessionDescriptionInterface* local_description() const override;
148 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800149 const SessionDescriptionInterface* current_local_description() const override;
150 const SessionDescriptionInterface* current_remote_description()
151 const override;
152 const SessionDescriptionInterface* pending_local_description() const override;
153 const SessionDescriptionInterface* pending_remote_description()
154 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
156 // JSEP01
htaa2a49d92016-03-04 02:51:39 -0800157 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700158 void CreateOffer(CreateSessionDescriptionObserver* observer,
159 const MediaConstraintsInterface* constraints) override;
160 void CreateOffer(CreateSessionDescriptionObserver* observer,
161 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800162 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700163 void CreateAnswer(CreateSessionDescriptionObserver* observer,
164 const MediaConstraintsInterface* constraints) override;
htaa2a49d92016-03-04 02:51:39 -0800165 void CreateAnswer(CreateSessionDescriptionObserver* observer,
166 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700167 void SetLocalDescription(SetSessionDescriptionObserver* observer,
168 SessionDescriptionInterface* desc) override;
Henrik Boströma4ecf552017-11-23 14:17:07 +0000169 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
170 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 17:48:32 +0100171 void SetRemoteDescription(
172 std::unique_ptr<SessionDescriptionInterface> desc,
173 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
174 override;
deadbeef46c73892016-11-16 19:42:04 -0800175 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 11:56:26 -0700176 bool SetConfiguration(
deadbeef293e9262017-01-11 12:28:30 -0800177 const PeerConnectionInterface::RTCConfiguration& configuration,
178 RTCError* error) override;
179 bool SetConfiguration(
180 const PeerConnectionInterface::RTCConfiguration& configuration) override {
181 return SetConfiguration(configuration, nullptr);
182 }
deadbeefa67696b2015-09-29 11:56:26 -0700183 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700184 bool RemoveIceCandidates(
185 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
deadbeefa67696b2015-09-29 11:56:26 -0700187 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000188
zstein4b979802017-06-02 14:37:37 -0700189 RTCError SetBitrate(const BitrateParameters& bitrate) override;
190
Alex Narest78609d52017-10-20 10:37:47 +0200191 void SetBitrateAllocationStrategy(
192 std::unique_ptr<rtc::BitrateAllocationStrategy>
193 bitrate_allocation_strategy) override;
194
henrika5f6bf242017-11-01 11:06:56 +0100195 void SetAudioPlayout(bool playout) override;
196 void SetAudioRecording(bool recording) override;
197
Elad Alon99c3fe52017-10-13 16:29:40 +0200198 RTC_DEPRECATED bool StartRtcEventLog(rtc::PlatformFile file,
199 int64_t max_size_bytes) override;
Bjorn Tereliusde939432017-11-20 17:38:14 +0100200 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
201 int64_t output_period_ms) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700202 void StopRtcEventLog() override;
203
deadbeefa67696b2015-09-29 11:56:26 -0700204 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205
hbos82ebe022016-11-14 01:41:09 -0800206 sigslot::signal1<DataChannel*> SignalDataChannelCreated;
207
deadbeefab9b2d12015-10-14 11:33:11 -0700208 // Virtual for unit tests.
209 virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
210 sctp_data_channels() const {
211 return sctp_data_channels_;
perkjd61bf802016-03-24 03:16:19 -0700212 }
deadbeefab9b2d12015-10-14 11:33:11 -0700213
Steve Anton978b8762017-09-29 12:15:02 -0700214 rtc::Thread* network_thread() const { return factory_->network_thread(); }
215 rtc::Thread* worker_thread() const { return factory_->worker_thread(); }
216 rtc::Thread* signaling_thread() const { return factory_->signaling_thread(); }
Steve Anton75737c02017-11-06 10:37:17 -0800217
218 // The SDP session ID as defined by RFC 3264.
219 virtual const std::string& session_id() const { return session_id_; }
220
221 // Returns true if we were the initial offerer.
222 bool initial_offerer() const { return initial_offerer_ && *initial_offerer_; }
223
224 // Returns stats for all channels of all transports.
225 // This avoids exposing the internal structures used to track them.
226 // The parameterless version creates |ChannelNamePairs| from |voice_channel|,
227 // |video_channel| and |voice_channel| if available - this requires it to be
228 // called on the signaling thread - and invokes the other |GetStats|. The
229 // other |GetStats| can be invoked on any thread; if not invoked on the
230 // network thread a thread hop will happen.
231 std::unique_ptr<SessionStats> GetSessionStats_s();
Steve Anton978b8762017-09-29 12:15:02 -0700232 virtual std::unique_ptr<SessionStats> GetSessionStats(
Steve Anton75737c02017-11-06 10:37:17 -0800233 const ChannelNamePairs& channel_name_pairs);
234
235 // virtual so it can be mocked in unit tests
Steve Anton978b8762017-09-29 12:15:02 -0700236 virtual bool GetLocalCertificate(
237 const std::string& transport_name,
Steve Anton75737c02017-11-06 10:37:17 -0800238 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
Steve Anton978b8762017-09-29 12:15:02 -0700239 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
Steve Anton75737c02017-11-06 10:37:17 -0800240 const std::string& transport_name);
241
242 virtual Call::Stats GetCallStats();
243
244 // Exposed for stats collecting.
245 // TODO(steveanton): Switch callers to use the plural form and remove these.
Steve Anton4171afb2017-11-20 10:20:22 -0800246 virtual cricket::VoiceChannel* voice_channel() const {
Steve Anton3fe1b152017-12-12 10:20:08 -0800247 if (IsUnifiedPlan()) {
248 // TODO(steveanton): Change stats collection to work with transceivers.
249 return nullptr;
250 }
Steve Anton4171afb2017-11-20 10:20:22 -0800251 return static_cast<cricket::VoiceChannel*>(
252 GetAudioTransceiver()->internal()->channel());
Steve Anton978b8762017-09-29 12:15:02 -0700253 }
Steve Anton4171afb2017-11-20 10:20:22 -0800254 virtual cricket::VideoChannel* video_channel() const {
Steve Anton3fe1b152017-12-12 10:20:08 -0800255 if (IsUnifiedPlan()) {
256 // TODO(steveanton): Change stats collection to work with transceivers.
257 return nullptr;
258 }
Steve Anton4171afb2017-11-20 10:20:22 -0800259 return static_cast<cricket::VideoChannel*>(
260 GetVideoTransceiver()->internal()->channel());
Steve Antond5585ca2017-10-23 14:49:26 -0700261 }
Steve Anton978b8762017-09-29 12:15:02 -0700262
Steve Anton75737c02017-11-06 10:37:17 -0800263 // Only valid when using deprecated RTP data channels.
264 virtual cricket::RtpDataChannel* rtp_data_channel() {
265 return rtp_data_channel_;
Steve Anton978b8762017-09-29 12:15:02 -0700266 }
Steve Anton75737c02017-11-06 10:37:17 -0800267 virtual rtc::Optional<std::string> sctp_content_name() const {
268 return sctp_content_name_;
269 }
270 virtual rtc::Optional<std::string> sctp_transport_name() const {
271 return sctp_transport_name_;
272 }
273
274 // Get the id used as a media stream track's "id" field from ssrc.
275 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
276 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
277
278 // Returns true if there was an ICE restart initiated by the remote offer.
279 bool IceRestartPending(const std::string& content_name) const;
280
281 // Returns true if the ICE restart flag above was set, and no ICE restart has
282 // occurred yet for this transport (by applying a local description with
283 // changed ufrag/password). If the transport has been deleted as a result of
284 // bundling, returns false.
285 bool NeedsIceRestart(const std::string& content_name) const;
286
287 // Get SSL role for an arbitrary m= section (handles bundling correctly).
288 // TODO(deadbeef): This is only used internally by the session description
289 // factory, it shouldn't really be public).
290 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role);
291
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700293 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294
295 private:
Henrik Boström31638672017-11-23 17:48:32 +0100296 class SetRemoteDescriptionObserverAdapter;
297 friend class SetRemoteDescriptionObserverAdapter;
298
Steve Anton4171afb2017-11-20 10:20:22 -0800299 struct RtpSenderInfo {
300 RtpSenderInfo() : first_ssrc(0) {}
301 RtpSenderInfo(const std::string& stream_label,
302 const std::string sender_id,
303 uint32_t ssrc)
304 : stream_label(stream_label), sender_id(sender_id), first_ssrc(ssrc) {}
305 bool operator==(const RtpSenderInfo& other) {
deadbeefbda7e0b2015-12-08 17:13:40 -0800306 return this->stream_label == other.stream_label &&
Steve Anton4171afb2017-11-20 10:20:22 -0800307 this->sender_id == other.sender_id &&
308 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-08 17:13:40 -0800309 }
deadbeefab9b2d12015-10-14 11:33:11 -0700310 std::string stream_label;
Steve Anton4171afb2017-11-20 10:20:22 -0800311 std::string sender_id;
312 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
313 // for communicating with the lower layers.
314 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 11:33:11 -0700315 };
deadbeefab9b2d12015-10-14 11:33:11 -0700316
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700318 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319
Steve Anton60776752018-01-10 11:51:34 -0800320 cricket::VoiceMediaChannel* voice_media_channel() const {
321 return voice_channel() ? voice_channel()->media_channel() : nullptr;
322 }
323
324 cricket::VideoMediaChannel* video_media_channel() const {
325 return video_channel() ? video_channel()->media_channel() : nullptr;
326 }
327
Steve Anton4171afb2017-11-20 10:20:22 -0800328 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
329 GetSendersInternal() const;
330 std::vector<
331 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
332 GetReceiversInternal() const;
333
334 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
335 GetAudioTransceiver() const;
336 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
337 GetVideoTransceiver() const;
338
deadbeefab9b2d12015-10-14 11:33:11 -0700339 void CreateAudioReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 10:20:22 -0800340 const RtpSenderInfo& remote_sender_info);
perkjf0dcfe22016-03-10 18:32:00 +0100341
deadbeefab9b2d12015-10-14 11:33:11 -0700342 void CreateVideoReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 10:20:22 -0800343 const RtpSenderInfo& remote_sender_info);
Henrik Boström933d8b02017-10-10 10:05:16 -0700344 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Steve Anton4171afb2017-11-20 10:20:22 -0800345 const RtpSenderInfo& remote_sender_info);
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700346
347 // May be called either by AddStream/RemoveStream, or when a track is
348 // added/removed from a stream previously added via AddStream.
349 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream);
350 void RemoveAudioTrack(AudioTrackInterface* track,
351 MediaStreamInterface* stream);
352 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream);
353 void RemoveVideoTrack(VideoTrackInterface* track,
354 MediaStreamInterface* stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355
Steve Antonf9381f02017-12-14 10:23:57 -0800356 // AddTrack implementation when Unified Plan is specified.
357 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
358 rtc::scoped_refptr<MediaStreamTrackInterface> track,
359 const std::vector<std::string>& stream_labels);
360 // AddTrack implementation when Plan B is specified.
361 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
362 rtc::scoped_refptr<MediaStreamTrackInterface> track,
363 const std::vector<std::string>& stream_labels);
364
365 // Returns the first RtpTransceiver suitable for a newly added track, if such
366 // transceiver is available.
367 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
368 FindFirstTransceiverForAddedTrack(
369 rtc::scoped_refptr<MediaStreamTrackInterface> track);
370
371 // RemoveTrack that returns an RTCError.
372 RTCError RemoveTrackInternal(rtc::scoped_refptr<RtpSenderInterface> sender);
373
374 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
375 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender);
376
Steve Anton9158ef62017-11-27 13:01:52 -0800377 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
378 cricket::MediaType media_type,
379 rtc::scoped_refptr<MediaStreamTrackInterface> track,
380 const RtpTransceiverInit& init);
381
Steve Antonf9381f02017-12-14 10:23:57 -0800382 // Create a new RtpTransceiver of the given type and add it to the list of
383 // transceivers.
384 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
385 CreateTransceiver(cricket::MediaType media_type);
386
Steve Antonba818672017-11-06 10:21:57 -0800387 void SetIceConnectionState(IceConnectionState new_state);
388 // Called any time the IceGatheringState changes
389 void OnIceGatheringChange(IceGatheringState new_state);
390 // New ICE candidate has been gathered.
391 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate);
392 // Some local ICE candidates have been removed.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700393 void OnIceCandidatesRemoved(
Steve Antonba818672017-11-06 10:21:57 -0800394 const std::vector<cricket::Candidate>& candidates);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395
Steve Antonba818672017-11-06 10:21:57 -0800396 // Update the state, signaling if necessary.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 void ChangeSignalingState(SignalingState signaling_state);
398
deadbeefeb459812015-12-15 19:24:43 -0800399 // Signals from MediaStreamObserver.
400 void OnAudioTrackAdded(AudioTrackInterface* track,
401 MediaStreamInterface* stream);
402 void OnAudioTrackRemoved(AudioTrackInterface* track,
403 MediaStreamInterface* stream);
404 void OnVideoTrackAdded(VideoTrackInterface* track,
405 MediaStreamInterface* stream);
406 void OnVideoTrackRemoved(VideoTrackInterface* track,
407 MediaStreamInterface* stream);
408
Henrik Boström31638672017-11-23 17:48:32 +0100409 void PostSetSessionDescriptionSuccess(
410 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
412 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700413 void PostCreateSessionDescriptionFailure(
414 CreateSessionDescriptionObserver* observer,
415 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416
Steve Anton8a006912017-12-04 15:25:56 -0800417 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
418 // that return an RTCError instead of invoking a callback.
419 RTCError ApplyLocalDescription(
420 std::unique_ptr<SessionDescriptionInterface> desc);
421 RTCError ApplyRemoteDescription(
422 std::unique_ptr<SessionDescriptionInterface> desc);
423
Steve Antondcc3c022017-12-22 16:02:54 -0800424 // Updates the local RtpTransceivers according to the JSEP rules. Called as
425 // part of setting the local/remote description.
426 RTCError UpdateTransceiversAndDataChannels(
427 cricket::ContentSource source,
428 const SessionDescriptionInterface* old_session,
429 const SessionDescriptionInterface& new_session);
430
431 // Either creates or destroys the transceiver's BaseChannel according to the
432 // given media section.
433 RTCError UpdateTransceiverChannel(
434 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
435 transceiver,
436 const cricket::ContentInfo& content,
437 const cricket::ContentGroup* bundle_group);
438
Steve Antonfa2260d2017-12-28 16:38:23 -0800439 // Either creates or destroys the local data channel according to the given
440 // media section.
441 RTCError UpdateDataChannel(cricket::ContentSource source,
442 const cricket::ContentInfo& content,
443 const cricket::ContentGroup* bundle_group);
444
Steve Antondcc3c022017-12-22 16:02:54 -0800445 // Associate the given transceiver according to the JSEP rules.
446 RTCErrorOr<
447 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
448 AssociateTransceiver(cricket::ContentSource source,
449 size_t mline_index,
450 const cricket::ContentInfo& content,
451 const cricket::ContentInfo* old_content);
452
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
494 // Returns a MediaSessionOptions struct with options decided by
495 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800496 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
zhihuang1c378ed2017-08-17 14:10:50 -0700497 cricket::MediaSessionOptions* session_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800498 void GetOptionsForPlanBAnswer(
499 const PeerConnectionInterface::RTCOfferAnswerOptions&
500 offer_answer_options,
501 cricket::MediaSessionOptions* session_options);
502 void GetOptionsForUnifiedPlanAnswer(
503 const PeerConnectionInterface::RTCOfferAnswerOptions&
504 offer_answer_options,
505 cricket::MediaSessionOptions* session_options);
htaa2a49d92016-03-04 02:51:39 -0800506
zhihuang1c378ed2017-08-17 14:10:50 -0700507 // Generates MediaDescriptionOptions for the |session_opts| based on existing
508 // local description or remote description.
509 void GenerateMediaDescriptionOptions(
510 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -0800511 RtpTransceiverDirection audio_direction,
512 RtpTransceiverDirection video_direction,
zhihuang1c378ed2017-08-17 14:10:50 -0700513 rtc::Optional<size_t>* audio_index,
514 rtc::Optional<size_t>* video_index,
515 rtc::Optional<size_t>* data_index,
htaa2a49d92016-03-04 02:51:39 -0800516 cricket::MediaSessionOptions* session_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700517
Steve Antonfa2260d2017-12-28 16:38:23 -0800518 // Generates the active MediaDescriptionOptions for the local data channel
519 // given the specified MID.
520 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
521 const std::string& mid) const;
522
523 // Generates the rejected MediaDescriptionOptions for the local data channel
524 // given the specified MID.
525 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
526 const std::string& mid) const;
527
528 // Returns the MID for the data section associated with either the
529 // RtpDataChannel or SCTP data channel, if it has been set. If no data
530 // channels are configured this will return nullopt.
531 rtc::Optional<std::string> GetDataMid() const;
532
Steve Anton4171afb2017-11-20 10:20:22 -0800533 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 15:33:07 -0800534 // Called when a media type is rejected (m-line set to port 0).
Steve Anton4171afb2017-11-20 10:20:22 -0800535 void RemoveSenders(cricket::MediaType media_type);
deadbeeffaac4972015-11-12 15:33:07 -0800536
deadbeefbda7e0b2015-12-08 17:13:40 -0800537 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
538 // and existing MediaStreamTracks are removed if there is no corresponding
539 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
540 // is created if it doesn't exist; if false, it's removed if it exists.
541 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700542 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 10:20:22 -0800543 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -0700544 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800545 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700546 cricket::MediaType media_type,
547 StreamCollection* new_streams);
548
Steve Anton4171afb2017-11-20 10:20:22 -0800549 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700550 // session description. It creates a remote MediaStreamTrackInterface
551 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800552 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
553 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700554
Steve Anton4171afb2017-11-20 10:20:22 -0800555 // Triggered when a remote sender has been removed from a remote session
556 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700557 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800558 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
559 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700560
561 // Finds remote MediaStreams without any tracks and removes them from
562 // |remote_streams_| and notifies the observer that the MediaStreams no longer
563 // exist.
564 void UpdateEndedRemoteMediaStreams();
565
deadbeefab9b2d12015-10-14 11:33:11 -0700566 // Loops through the vector of |streams| and finds added and removed
567 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 10:20:22 -0800568 // For each new or removed StreamParam, OnLocalSenderSeen or
569 // OnLocalSenderRemoved is invoked.
570 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
571 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700572
Steve Anton4171afb2017-11-20 10:20:22 -0800573 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 11:33:11 -0700574 // session description.
575 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
576 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
577 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 10:20:22 -0800578 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
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 removed from a local session
deadbeefab9b2d12015-10-14 11:33:11 -0700582 // description.
583 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
584 // has been removed from the local SessionDescription and the stream can be
585 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 10:20:22 -0800586 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
587 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700588
589 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
590 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
591 void UpdateClosingRtpDataChannels(
592 const std::vector<std::string>& active_channels,
593 bool is_local_update);
594 void CreateRemoteRtpDataChannel(const std::string& label,
595 uint32_t remote_ssrc);
596
597 // Creates channel and adds it to the collection of DataChannels that will
598 // be offered in a SessionDescription.
599 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
600 const std::string& label,
601 const InternalDataChannelInit* config);
602
603 // Checks if any data channel has been added.
604 bool HasDataChannels() const;
605
606 void AllocateSctpSids(rtc::SSLRole role);
607 void OnSctpDataChannelClosed(DataChannel* channel);
608
deadbeefab9b2d12015-10-14 11:33:11 -0700609 void OnDataChannelDestroyed();
Steve Antonba818672017-11-06 10:21:57 -0800610 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700611 void OnDataChannelOpenMessage(const std::string& label,
612 const InternalDataChannelInit& config);
613
Steve Anton4171afb2017-11-20 10:20:22 -0800614 // Returns true if the PeerConnection is configured to use Unified Plan
615 // semantics for creating offers/answers and setting local/remote
616 // descriptions. If this is true the RtpTransceiver API will also be available
617 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 10:25:56 -0800618 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
619 // sufficient time has passed.
620 bool IsUnifiedPlan() const {
621 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
622 }
Steve Anton4171afb2017-11-20 10:20:22 -0800623
624 // Is there an RtpSender of the given type?
zhihuang1c378ed2017-08-17 14:10:50 -0700625 bool HasRtpSender(cricket::MediaType type) const;
deadbeeffac06552015-11-25 11:26:01 -0800626
Steve Anton4171afb2017-11-20 10:20:22 -0800627 // Return the RtpSender with the given track attached.
628 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
629 FindSenderForTrack(MediaStreamTrackInterface* track) const;
deadbeef70ab1a12015-09-28 16:53:55 -0700630
Steve Anton4171afb2017-11-20 10:20:22 -0800631 // Return the RtpSender with the given id, or null if none exists.
632 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
633 FindSenderById(const std::string& sender_id) const;
634
635 // Return the RtpReceiver with the given id, or null if none exists.
636 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
637 FindReceiverById(const std::string& receiver_id) const;
638
639 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
640 cricket::MediaType media_type);
641 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
642 cricket::MediaType media_type);
643 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
644 const std::string& stream_label,
645 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 11:33:11 -0700646
647 // Returns the specified SCTP DataChannel in sctp_data_channels_,
648 // or nullptr if not found.
649 DataChannel* FindDataChannelBySid(int sid) const;
650
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700651 // Called when first configuring the port allocator.
deadbeef91dd5672016-05-18 16:55:30 -0700652 bool InitializePortAllocator_n(const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800653 // Called when SetConfiguration is called to apply the supported subset
654 // of the configuration on the network thread.
655 bool ReconfigurePortAllocator_n(
656 const cricket::ServerAddresses& stun_servers,
657 const std::vector<cricket::RelayServerConfig>& turn_servers,
658 IceTransportsType type,
659 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200660 bool prune_turn_ports,
661 webrtc::TurnCustomizer* turn_customizer);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700662
Elad Alon99c3fe52017-10-13 16:29:40 +0200663 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700664 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100665 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
666 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200667
Elad Alonacb24172017-10-06 14:32:13 +0200668 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700669 // This function should only be called from the worker thread.
670 void StopRtcEventLog_w();
671
Steve Anton038834f2017-07-14 15:59:59 -0700672 // Ensures the configuration doesn't have any parameters with invalid values,
673 // or values that conflict with other parameters.
674 //
675 // Returns RTCError::OK() if there are no issues.
676 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
677
Steve Antonba818672017-11-06 10:21:57 -0800678 cricket::ChannelManager* channel_manager() const;
679 MetricsObserverInterface* metrics_observer() const;
680
Steve Antonf8470812017-12-04 10:46:21 -0800681 enum class SessionError {
682 kNone, // No error.
683 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
684 kTransport, // Error from the underlying transport.
685 };
686
Steve Anton75737c02017-11-06 10:37:17 -0800687 // Returns the last error in the session. See the enum above for details.
Steve Antonf8470812017-12-04 10:46:21 -0800688 SessionError session_error() const { return session_error_; }
689 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 10:37:17 -0800690
Steve Anton75737c02017-11-06 10:37:17 -0800691 cricket::BaseChannel* GetChannel(const std::string& content_name);
692
693 // Get current SSL role used by SCTP's underlying transport.
694 bool GetSctpSslRole(rtc::SSLRole* role);
695
Steve Anton75737c02017-11-06 10:37:17 -0800696 cricket::IceConfig ParseIceConfig(
697 const PeerConnectionInterface::RTCConfiguration& config) const;
698
Steve Anton75737c02017-11-06 10:37:17 -0800699 // Implements DataChannelProviderInterface.
700 bool SendData(const cricket::SendDataParams& params,
701 const rtc::CopyOnWriteBuffer& payload,
702 cricket::SendDataResult* result) override;
703 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
704 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
705 void AddSctpDataStream(int sid) override;
706 void RemoveSctpDataStream(int sid) override;
707 bool ReadyToSendData() const override;
708
709 cricket::DataChannelType data_channel_type() const;
710
Steve Anton75737c02017-11-06 10:37:17 -0800711 // Called when an RTCCertificate is generated or retrieved by
712 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
713 void OnCertificateReady(
714 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
715 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
716
717 cricket::TransportController* transport_controller() const {
718 return transport_controller_.get();
719 }
720
721 // Return all managed, non-null channels.
722 std::vector<cricket::BaseChannel*> Channels() const;
723
724 // Non-const versions of local_description()/remote_description(), for use
725 // internally.
726 SessionDescriptionInterface* mutable_local_description() {
727 return pending_local_description_ ? pending_local_description_.get()
728 : current_local_description_.get();
729 }
730 SessionDescriptionInterface* mutable_remote_description() {
731 return pending_remote_description_ ? pending_remote_description_.get()
732 : current_remote_description_.get();
733 }
734
735 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 10:46:21 -0800736 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 10:37:17 -0800737
Steve Anton3828c062017-12-06 10:34:51 -0800738 RTCError UpdateSessionState(SdpType type, cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800739 // Push the media parts of the local or remote session description
740 // down to all of the channels.
Steve Anton3828c062017-12-06 10:34:51 -0800741 RTCError PushdownMediaDescription(SdpType type,
Steve Anton8a006912017-12-04 15:25:56 -0800742 cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800743 bool PushdownSctpParameters_n(cricket::ContentSource source);
744
Steve Anton8a006912017-12-04 15:25:56 -0800745 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -0800746 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800747
748 // Returns true and the TransportInfo of the given |content_name|
749 // from |description|. Returns false if it's not available.
750 static bool GetTransportDescription(
751 const cricket::SessionDescription* description,
752 const std::string& content_name,
753 cricket::TransportDescription* info);
754
Steve Antoneda6ccd2017-12-04 10:21:55 -0800755 // Returns the transport name for the given media section identified by |mid|.
756 // If BUNDLE is enabled and the media section is part of the bundle group,
757 // the transport name will be the first mid in the bundle group. Otherwise,
758 // the transport name will be the mid of the media section.
759 std::string GetTransportNameForMediaSection(
760 const std::string& mid,
761 const cricket::ContentGroup* bundle_group) const;
Steve Anton75737c02017-11-06 10:37:17 -0800762
763 // Cause all the BaseChannels in the bundle group to have the same
764 // transport channel.
765 bool EnableBundle(const cricket::ContentGroup& bundle);
766
767 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 10:52:59 -0800768 // This enables media to flow on all configured audio/video channels and the
769 // RtpDataChannel.
770 void EnableSending();
Steve Anton3fe1b152017-12-12 10:20:08 -0800771
Steve Anton8af21862017-12-15 11:20:13 -0800772 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
773 void DestroyAllChannels();
Steve Anton3fe1b152017-12-12 10:20:08 -0800774
Steve Anton75737c02017-11-06 10:37:17 -0800775 // Returns the media index for a local ice candidate given the content name.
776 // Returns false if the local session description does not have a media
777 // content called |content_name|.
778 bool GetLocalCandidateMediaIndex(const std::string& content_name,
779 int* sdp_mline_index);
780 // Uses all remote candidates in |remote_desc| in this session.
781 bool UseCandidatesInSessionDescription(
782 const SessionDescriptionInterface* remote_desc);
783 // Uses |candidate| in this session.
784 bool UseCandidate(const IceCandidateInterface* candidate);
785 // Deletes the corresponding channel of contents that don't exist in |desc|.
786 // |desc| can be null. This means that all channels are deleted.
787 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
788
789 // Allocates media channels based on the |desc|. If |desc| doesn't have
790 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
791 // This method will also delete any existing media channels before creating.
Steve Antondcc3c022017-12-22 16:02:54 -0800792 RTCError CreateChannels(const cricket::SessionDescription& desc);
793
794 // If the BUNDLE policy is max-bundle, then we know for sure that all
795 // transports will be bundled from the start. This method returns the BUNDLE
796 // group if that's the case, or null if BUNDLE will be negotiated later. An
797 // error is returned if max-bundle is specified but the session description
798 // does not have a BUNDLE group.
799 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
800 const cricket::SessionDescription& desc) const;
Steve Anton75737c02017-11-06 10:37:17 -0800801
802 // Helper methods to create media channels.
Steve Antoneda6ccd2017-12-04 10:21:55 -0800803 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid,
804 const std::string& transport_name);
805 cricket::VideoChannel* CreateVideoChannel(const std::string& mid,
806 const std::string& transport_name);
807 bool CreateDataChannel(const std::string& mid,
808 const std::string& transport_name);
Steve Anton75737c02017-11-06 10:37:17 -0800809
810 std::unique_ptr<SessionStats> GetSessionStats_n(
811 const ChannelNamePairs& channel_name_pairs);
812
813 bool CreateSctpTransport_n(const std::string& content_name,
814 const std::string& transport_name);
815 // For bundling.
816 void ChangeSctpTransport_n(const std::string& transport_name);
817 void DestroySctpTransport_n();
818 // SctpTransport signal handlers. Needed to marshal signals from the network
819 // to signaling thread.
820 void OnSctpTransportReadyToSendData_n();
821 // This may be called with "false" if the direction of the m= section causes
822 // us to tear down the SCTP connection.
823 void OnSctpTransportReadyToSendData_s(bool ready);
824 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
825 const rtc::CopyOnWriteBuffer& payload);
826 // Beyond just firing the signal to the signaling thread, listens to SCTP
827 // CONTROL messages on unused SIDs and processes them as OPEN messages.
828 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
829 const rtc::CopyOnWriteBuffer& payload);
830 void OnSctpStreamClosedRemotely_n(int sid);
831
832 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
833 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
834 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 15:25:56 -0800835 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
836 cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800837
Steve Anton3828c062017-12-06 10:34:51 -0800838 // Check if a call to SetLocalDescription is acceptable with a session
839 // description of the given type.
840 bool ExpectSetLocalDescription(SdpType type);
841 // Check if a call to SetRemoteDescription is acceptable with a session
842 // description of the given type.
843 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800844 // Verifies a=setup attribute as per RFC 5763.
845 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 10:34:51 -0800846 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800847
848 // Returns true if we are ready to push down the remote candidate.
849 // |remote_desc| is the new remote description, or NULL if the current remote
850 // description should be used. Output |valid| is true if the candidate media
851 // index is valid.
852 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
853 const SessionDescriptionInterface* remote_desc,
854 bool* valid);
855
856 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
857 // this session.
858 bool SrtpRequired() const;
859
860 // TransportController signal handlers.
861 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
862 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
863 void OnTransportControllerCandidatesGathered(
864 const std::string& transport_name,
865 const std::vector<cricket::Candidate>& candidates);
866 void OnTransportControllerCandidatesRemoved(
867 const std::vector<cricket::Candidate>& candidates);
868 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
869
Steve Antonf8470812017-12-04 10:46:21 -0800870 const char* SessionErrorToString(SessionError error) const;
Steve Anton75737c02017-11-06 10:37:17 -0800871 std::string GetSessionErrorMsg();
872
873 // Invoked when TransportController connection completion is signaled.
874 // Reports stats for all transports in use.
875 void ReportTransportStats();
876
877 // Gather the usage of IPv4/IPv6 as best connection.
878 void ReportBestConnectionState(const cricket::TransportStats& stats);
879
880 void ReportNegotiatedCiphers(const cricket::TransportStats& stats);
881
882 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
883
884 const std::string GetTransportName(const std::string& content_name);
885
886 void DestroyRtcpTransport_n(const std::string& transport_name);
Steve Anton6fec8802017-12-04 10:37:29 -0800887
888 // Destroys and clears the BaseChannel associated with the given transceiver,
889 // if such channel is set.
890 void DestroyTransceiverChannel(
891 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
892 transceiver);
893
894 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Steve Anton75737c02017-11-06 10:37:17 -0800895 void DestroyDataChannel();
896
Steve Anton6fec8802017-12-04 10:37:29 -0800897 // Destroys the given BaseChannel. The channel cannot be accessed after this
898 // method is called.
899 void DestroyBaseChannel(cricket::BaseChannel* channel);
900
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 // Storing the factory as a scoped reference pointer ensures that the memory
902 // in the PeerConnectionFactoryImpl remains available as long as the
903 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
904 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700905 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000907 rtc::scoped_refptr<PeerConnectionFactory> factory_;
Steve Antonba818672017-11-06 10:21:57 -0800908 PeerConnectionObserver* observer_ = nullptr;
909 UMAObserver* uma_observer_ = nullptr;
terelius33860252017-05-12 23:37:18 -0700910
911 // The EventLog needs to outlive |call_| (and any other object that uses it).
912 std::unique_ptr<RtcEventLog> event_log_;
913
Steve Antonba818672017-11-06 10:21:57 -0800914 SignalingState signaling_state_ = kStable;
915 IceConnectionState ice_connection_state_ = kIceConnectionNew;
916 IceGatheringState ice_gathering_state_ = kIceGatheringNew;
deadbeef46c73892016-11-16 19:42:04 -0800917 PeerConnectionInterface::RTCConfiguration configuration_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918
kwibergd1fe2812016-04-27 06:47:29 -0700919 std::unique_ptr<cricket::PortAllocator> port_allocator_;
deadbeefab9b2d12015-10-14 11:33:11 -0700920
zhihuang8f65cdf2016-05-06 18:40:30 -0700921 // One PeerConnection has only one RTCP CNAME.
922 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
923 std::string rtcp_cname_;
924
deadbeefab9b2d12015-10-14 11:33:11 -0700925 // Streams added via AddStream.
926 rtc::scoped_refptr<StreamCollection> local_streams_;
927 // Streams created as a result of SetRemoteDescription.
928 rtc::scoped_refptr<StreamCollection> remote_streams_;
929
kwibergd1fe2812016-04-27 06:47:29 -0700930 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_;
deadbeefeb459812015-12-15 19:24:43 -0800931
Steve Anton4171afb2017-11-20 10:20:22 -0800932 // These lists store sender info seen in local/remote descriptions.
933 std::vector<RtpSenderInfo> remote_audio_sender_infos_;
934 std::vector<RtpSenderInfo> remote_video_sender_infos_;
935 std::vector<RtpSenderInfo> local_audio_sender_infos_;
936 std::vector<RtpSenderInfo> local_video_sender_infos_;
deadbeefab9b2d12015-10-14 11:33:11 -0700937
938 SctpSidAllocator sid_allocator_;
939 // label -> DataChannel
940 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
941 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -0800942 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -0700943
deadbeefbda7e0b2015-12-08 17:13:40 -0800944 bool remote_peer_supports_msid_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700945
terelius33860252017-05-12 23:37:18 -0700946 std::unique_ptr<Call> call_;
terelius33860252017-05-12 23:37:18 -0700947 std::unique_ptr<StatsCollector> stats_; // A pointer is passed to senders_
948 rtc::scoped_refptr<RTCStatsCollector> stats_collector_;
949
deadbeefa601f5c2016-06-06 14:27:39 -0700950 std::vector<
Steve Anton4171afb2017-11-20 10:20:22 -0800951 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
952 transceivers_;
Steve Antondcc3c022017-12-22 16:02:54 -0800953 // MIDs that have been seen either by SetLocalDescription or
954 // SetRemoteDescription over the life of the PeerConnection.
955 std::set<std::string> seen_mids_;
Steve Anton75737c02017-11-06 10:37:17 -0800956
Steve Antonf8470812017-12-04 10:46:21 -0800957 SessionError session_error_ = SessionError::kNone;
958 std::string session_error_desc_;
Steve Anton75737c02017-11-06 10:37:17 -0800959
960 std::string session_id_;
961 rtc::Optional<bool> initial_offerer_;
962
963 std::unique_ptr<cricket::TransportController> transport_controller_;
964 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
Steve Anton75737c02017-11-06 10:37:17 -0800965 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
966 // when using SCTP.
967 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
968
969 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
970 // |sctp_transport_name_| keeps track of what DTLS transport the SCTP
971 // transport is using (which can change due to bundling).
972 rtc::Optional<std::string> sctp_transport_name_;
973 // |sctp_content_name_| is the content name (MID) in SDP.
974 rtc::Optional<std::string> sctp_content_name_;
975 // Value cached on signaling thread. Only updated when SctpReadyToSendData
976 // fires on the signaling thread.
977 bool sctp_ready_to_send_data_ = false;
978 // Same as signals provided by SctpTransport, but these are guaranteed to
979 // fire on the signaling thread, whereas SctpTransport fires on the networking
980 // thread.
981 // |sctp_invoker_| is used so that any signals queued on the signaling thread
982 // from the network thread are immediately discarded if the SctpTransport is
983 // destroyed (due to m= section being rejected).
984 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
985 // are marshalled to the right thread. Could almost use proxy.h for this,
986 // but it doesn't have a mechanism for marshalling sigslot::signals
987 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
988 sigslot::signal1<bool> SignalSctpReadyToSendData;
989 sigslot::signal2<const cricket::ReceiveDataParams&,
990 const rtc::CopyOnWriteBuffer&>
991 SignalSctpDataReceived;
992 sigslot::signal1<int> SignalSctpStreamClosedRemotely;
993
994 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
995 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
996 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
997 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
998 bool dtls_enabled_ = false;
999 // Specifies which kind of data channel is allowed. This is controlled
1000 // by the chrome command-line flag and constraints:
1001 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
1002 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
1003 // not set or false, SCTP is allowed (DCT_SCTP);
1004 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
1005 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
1006 cricket::DataChannelType data_channel_type_ = cricket::DCT_NONE;
1007 // List of content names for which the remote side triggered an ICE restart.
1008 std::set<std::string> pending_ice_restarts_;
1009
1010 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
1011
1012 // Member variables for caching global options.
1013 cricket::AudioOptions audio_options_;
1014 cricket::VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015};
1016
1017} // namespace webrtc
1018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +02001019#endif // PC_PEERCONNECTION_H_