blob: 9163c366315ece6fdf0e7d6f7a86086054311b1c [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
14#include <string>
perkjd61bf802016-03-24 03:16:19 -070015#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070016#include <memory>
perkjd61bf802016-03-24 03:16:19 -070017#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/peerconnectioninterface.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020020#include "api/turncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "pc/iceserverparsing.h"
22#include "pc/peerconnectionfactory.h"
23#include "pc/rtcstatscollector.h"
24#include "pc/rtpreceiver.h"
25#include "pc/rtpsender.h"
26#include "pc/statscollector.h"
27#include "pc/streamcollection.h"
28#include "pc/webrtcsession.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
deadbeef70ab1a12015-09-28 16:53:55 -070036// PeerConnection implements the PeerConnectionInterface interface.
deadbeefab9b2d12015-10-14 11:33:11 -070037// It uses WebRtcSession to implement the PeerConnection functionality.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038class PeerConnection : public PeerConnectionInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039 public IceObserver,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000040 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 public sigslot::has_slots<> {
42 public:
zhihuang38ede132017-06-15 12:52:32 -070043 explicit PeerConnection(PeerConnectionFactory* factory,
44 std::unique_ptr<RtcEventLog> event_log,
45 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
deadbeef653b8e02015-11-11 12:55:10 -080047 bool Initialize(
48 const PeerConnectionInterface::RTCConfiguration& configuration,
kwibergd1fe2812016-04-27 06:47:29 -070049 std::unique_ptr<cricket::PortAllocator> allocator,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020050 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
deadbeef653b8e02015-11-11 12:55:10 -080051 PeerConnectionObserver* observer);
52
deadbeefa67696b2015-09-29 11:56:26 -070053 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
54 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
55 bool AddStream(MediaStreamInterface* local_stream) override;
56 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
deadbeefe1f9d832016-01-14 15:35:42 -080058 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
59 MediaStreamTrackInterface* track,
60 std::vector<MediaStreamInterface*> streams) override;
61 bool RemoveTrack(RtpSenderInterface* sender) override;
62
Steve Anton978b8762017-09-29 12:15:02 -070063 // TODO(steveanton): Remove this once all clients have switched to using the
64 // PeerConnection shims for WebRtcSession methods instead of the methods
65 // directly via this getter.
66 virtual WebRtcSession* session() { return session_; }
Alex Loikobf667942017-09-29 10:44:31 +000067
Steve Anton8c0f7a72017-10-03 10:03:10 -070068 // Gets the DTLS SSL certificate associated with the audio transport on the
69 // remote side. This will become populated once the DTLS connection with the
70 // peer has been completed, as indicated by the ICE connection state
71 // transitioning to kIceConnectionCompleted.
72 // Note that this will be removed once we implement RTCDtlsTransport which
73 // has standardized method for getting this information.
74 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
75 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
76
deadbeefa67696b2015-09-29 11:56:26 -070077 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
78 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
deadbeeffac06552015-11-25 11:26:01 -080080 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -080081 const std::string& kind,
82 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -080083
deadbeef70ab1a12015-09-28 16:53:55 -070084 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
85 const override;
86 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
87 const override;
88
deadbeefa67696b2015-09-29 11:56:26 -070089 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -070091 const DataChannelInit* config) override;
92 bool GetStats(StatsObserver* observer,
93 webrtc::MediaStreamTrackInterface* track,
94 StatsOutputLevel level) override;
hbos74e1a4f2016-09-15 23:33:01 -070095 void GetStats(RTCStatsCollectorCallback* callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
deadbeefa67696b2015-09-29 11:56:26 -070097 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
deadbeefa67696b2015-09-29 11:56:26 -070099 IceConnectionState ice_connection_state() override;
100 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
deadbeefa67696b2015-09-29 11:56:26 -0700102 const SessionDescriptionInterface* local_description() const override;
103 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800104 const SessionDescriptionInterface* current_local_description() const override;
105 const SessionDescriptionInterface* current_remote_description()
106 const override;
107 const SessionDescriptionInterface* pending_local_description() const override;
108 const SessionDescriptionInterface* pending_remote_description()
109 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
111 // JSEP01
htaa2a49d92016-03-04 02:51:39 -0800112 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700113 void CreateOffer(CreateSessionDescriptionObserver* observer,
114 const MediaConstraintsInterface* constraints) override;
115 void CreateOffer(CreateSessionDescriptionObserver* observer,
116 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800117 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700118 void CreateAnswer(CreateSessionDescriptionObserver* observer,
119 const MediaConstraintsInterface* constraints) override;
htaa2a49d92016-03-04 02:51:39 -0800120 void CreateAnswer(CreateSessionDescriptionObserver* observer,
121 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700122 void SetLocalDescription(SetSessionDescriptionObserver* observer,
123 SessionDescriptionInterface* desc) override;
124 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
125 SessionDescriptionInterface* desc) override;
deadbeef46c73892016-11-16 19:42:04 -0800126 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 11:56:26 -0700127 bool SetConfiguration(
deadbeef293e9262017-01-11 12:28:30 -0800128 const PeerConnectionInterface::RTCConfiguration& configuration,
129 RTCError* error) override;
130 bool SetConfiguration(
131 const PeerConnectionInterface::RTCConfiguration& configuration) override {
132 return SetConfiguration(configuration, nullptr);
133 }
deadbeefa67696b2015-09-29 11:56:26 -0700134 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700135 bool RemoveIceCandidates(
136 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137
deadbeefa67696b2015-09-29 11:56:26 -0700138 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000139
zstein4b979802017-06-02 14:37:37 -0700140 RTCError SetBitrate(const BitrateParameters& bitrate) override;
141
Alex Narest78609d52017-10-20 10:37:47 +0200142 void SetBitrateAllocationStrategy(
143 std::unique_ptr<rtc::BitrateAllocationStrategy>
144 bitrate_allocation_strategy) override;
145
Elad Alon99c3fe52017-10-13 16:29:40 +0200146 RTC_DEPRECATED bool StartRtcEventLog(rtc::PlatformFile file,
147 int64_t max_size_bytes) override;
148 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700149 void StopRtcEventLog() override;
150
deadbeefa67696b2015-09-29 11:56:26 -0700151 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
hbos82ebe022016-11-14 01:41:09 -0800153 sigslot::signal1<DataChannel*> SignalDataChannelCreated;
154
deadbeefab9b2d12015-10-14 11:33:11 -0700155 // Virtual for unit tests.
156 virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
157 sctp_data_channels() const {
158 return sctp_data_channels_;
perkjd61bf802016-03-24 03:16:19 -0700159 }
deadbeefab9b2d12015-10-14 11:33:11 -0700160
Steve Anton978b8762017-09-29 12:15:02 -0700161 // TODO(steveanton): These methods are temporarily added to facilitate work
162 // towards merging WebRtcSession into PeerConnection. To make this easier, we
163 // want only PeerConnection to interact with WebRtcSession so they can be
164 // merged easily. A few outside classes still access WebRtcSession methods
165 // directly, so these have been added to PeerConnection to remove the
166 // dependency from WebRtcSession.
167
168 rtc::Thread* network_thread() const { return factory_->network_thread(); }
169 rtc::Thread* worker_thread() const { return factory_->worker_thread(); }
170 rtc::Thread* signaling_thread() const { return factory_->signaling_thread(); }
171 virtual const std::string& session_id() const { return session_->id(); }
172 virtual bool session_created() const { return session_ != nullptr; }
173 virtual bool initial_offerer() const { return session_->initial_offerer(); }
174 virtual std::unique_ptr<SessionStats> GetSessionStats_s() {
175 return session_->GetStats_s();
176 }
177 virtual std::unique_ptr<SessionStats> GetSessionStats(
178 const ChannelNamePairs& channel_name_pairs) {
179 return session_->GetStats(channel_name_pairs);
180 }
181 virtual bool GetLocalCertificate(
182 const std::string& transport_name,
183 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
184 return session_->GetLocalCertificate(transport_name, certificate);
185 }
186 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
187 const std::string& transport_name) {
188 return session_->GetRemoteSSLCertificate(transport_name);
189 }
190 virtual Call::Stats GetCallStats() { return session_->GetCallStats(); }
191 virtual cricket::VoiceChannel* voice_channel() {
192 return session_->voice_channel();
193 }
194 virtual cricket::VideoChannel* video_channel() {
195 return session_->video_channel();
196 }
197 virtual cricket::RtpDataChannel* rtp_data_channel() {
198 return session_->rtp_data_channel();
199 }
200 virtual rtc::Optional<std::string> sctp_content_name() const {
201 return session_->sctp_content_name();
202 }
203 virtual rtc::Optional<std::string> sctp_transport_name() const {
204 return session_->sctp_transport_name();
205 }
206 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id) {
207 return session_->GetLocalTrackIdBySsrc(ssrc, track_id);
208 }
209 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id) {
210 return session_->GetRemoteTrackIdBySsrc(ssrc, track_id);
211 }
Steve Antond5585ca2017-10-23 14:49:26 -0700212 bool IceRestartPending(const std::string& content_name) const {
213 return session_->IceRestartPending(content_name);
214 }
215 bool NeedsIceRestart(const std::string& content_name) const {
216 return session_->NeedsIceRestart(content_name);
217 }
218 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) {
219 return session_->GetSslRole(content_name, role);
220 }
Steve Anton978b8762017-09-29 12:15:02 -0700221
222 // This is needed for stats tests to inject a MockWebRtcSession. Once
223 // WebRtcSession has been merged in, this will no longer be needed.
224 void set_session_for_testing(WebRtcSession* session) {
225 session_ = session;
226 }
227
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700229 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230
231 private:
deadbeefab9b2d12015-10-14 11:33:11 -0700232 struct TrackInfo {
233 TrackInfo() : ssrc(0) {}
234 TrackInfo(const std::string& stream_label,
235 const std::string track_id,
236 uint32_t ssrc)
237 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {}
deadbeefbda7e0b2015-12-08 17:13:40 -0800238 bool operator==(const TrackInfo& other) {
239 return this->stream_label == other.stream_label &&
240 this->track_id == other.track_id && this->ssrc == other.ssrc;
241 }
deadbeefab9b2d12015-10-14 11:33:11 -0700242 std::string stream_label;
243 std::string track_id;
244 uint32_t ssrc;
245 };
246 typedef std::vector<TrackInfo> TrackInfos;
247
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700249 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250
deadbeefab9b2d12015-10-14 11:33:11 -0700251 void CreateAudioReceiver(MediaStreamInterface* stream,
perkjd61bf802016-03-24 03:16:19 -0700252 const std::string& track_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700253 uint32_t ssrc);
perkjf0dcfe22016-03-10 18:32:00 +0100254
deadbeefab9b2d12015-10-14 11:33:11 -0700255 void CreateVideoReceiver(MediaStreamInterface* stream,
perkjf0dcfe22016-03-10 18:32:00 +0100256 const std::string& track_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700257 uint32_t ssrc);
Henrik Boström933d8b02017-10-10 10:05:16 -0700258 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
259 const std::string& track_id);
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700260
261 // May be called either by AddStream/RemoveStream, or when a track is
262 // added/removed from a stream previously added via AddStream.
263 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream);
264 void RemoveAudioTrack(AudioTrackInterface* track,
265 MediaStreamInterface* stream);
266 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream);
267 void RemoveVideoTrack(VideoTrackInterface* track,
268 MediaStreamInterface* stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269
270 // Implements IceObserver
zstein6dfd53a2017-03-06 13:49:03 -0800271 void OnIceConnectionStateChange(IceConnectionState new_state) override;
Peter Thatcher54360512015-07-08 11:08:35 -0700272 void OnIceGatheringChange(IceGatheringState new_state) override;
jbauch81bf7b02017-03-25 08:31:12 -0700273 void OnIceCandidate(
274 std::unique_ptr<IceCandidateInterface> candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700275 void OnIceCandidatesRemoved(
276 const std::vector<cricket::Candidate>& candidates) override;
Peter Thatcher54360512015-07-08 11:08:35 -0700277 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278
279 // Signals from WebRtcSession.
deadbeefd59daf82015-10-14 15:02:44 -0700280 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 void ChangeSignalingState(SignalingState signaling_state);
282
deadbeefeb459812015-12-15 19:24:43 -0800283 // Signals from MediaStreamObserver.
284 void OnAudioTrackAdded(AudioTrackInterface* track,
285 MediaStreamInterface* stream);
286 void OnAudioTrackRemoved(AudioTrackInterface* track,
287 MediaStreamInterface* stream);
288 void OnVideoTrackAdded(VideoTrackInterface* track,
289 MediaStreamInterface* stream);
290 void OnVideoTrackRemoved(VideoTrackInterface* track,
291 MediaStreamInterface* stream);
292
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
294 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700295 void PostCreateSessionDescriptionFailure(
296 CreateSessionDescriptionObserver* observer,
297 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298
299 bool IsClosed() const {
300 return signaling_state_ == PeerConnectionInterface::kClosed;
301 }
302
deadbeefab9b2d12015-10-14 11:33:11 -0700303 // Returns a MediaSessionOptions struct with options decided by |options|,
304 // the local MediaStreams and DataChannels.
zhihuang1c378ed2017-08-17 14:10:50 -0700305 void GetOptionsForOffer(
deadbeefab9b2d12015-10-14 11:33:11 -0700306 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
307 cricket::MediaSessionOptions* session_options);
308
309 // Returns a MediaSessionOptions struct with options decided by
310 // |constraints|, the local MediaStreams and DataChannels.
zhihuang1c378ed2017-08-17 14:10:50 -0700311 void GetOptionsForAnswer(const RTCOfferAnswerOptions& options,
312 cricket::MediaSessionOptions* session_options);
htaa2a49d92016-03-04 02:51:39 -0800313
zhihuang1c378ed2017-08-17 14:10:50 -0700314 // Generates MediaDescriptionOptions for the |session_opts| based on existing
315 // local description or remote description.
316 void GenerateMediaDescriptionOptions(
317 const SessionDescriptionInterface* session_desc,
318 cricket::RtpTransceiverDirection audio_direction,
319 cricket::RtpTransceiverDirection video_direction,
320 rtc::Optional<size_t>* audio_index,
321 rtc::Optional<size_t>* video_index,
322 rtc::Optional<size_t>* data_index,
htaa2a49d92016-03-04 02:51:39 -0800323 cricket::MediaSessionOptions* session_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700324
deadbeeffaac4972015-11-12 15:33:07 -0800325 // Remove all local and remote tracks of type |media_type|.
326 // Called when a media type is rejected (m-line set to port 0).
327 void RemoveTracks(cricket::MediaType media_type);
328
deadbeefbda7e0b2015-12-08 17:13:40 -0800329 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
330 // and existing MediaStreamTracks are removed if there is no corresponding
331 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
332 // is created if it doesn't exist; if false, it's removed if it exists.
333 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700334 // If a new MediaStream is created it is added to |new_streams|.
335 void UpdateRemoteStreamsList(
336 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800337 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700338 cricket::MediaType media_type,
339 StreamCollection* new_streams);
340
341 // Triggered when a remote track has been seen for the first time in a remote
342 // session description. It creates a remote MediaStreamTrackInterface
343 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
344 void OnRemoteTrackSeen(const std::string& stream_label,
345 const std::string& track_id,
346 uint32_t ssrc,
347 cricket::MediaType media_type);
348
349 // Triggered when a remote track has been removed from a remote session
350 // description. It removes the remote track with id |track_id| from a remote
351 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
352 void OnRemoteTrackRemoved(const std::string& stream_label,
353 const std::string& track_id,
354 cricket::MediaType media_type);
355
356 // Finds remote MediaStreams without any tracks and removes them from
357 // |remote_streams_| and notifies the observer that the MediaStreams no longer
358 // exist.
359 void UpdateEndedRemoteMediaStreams();
360
deadbeefab9b2d12015-10-14 11:33:11 -0700361 // Loops through the vector of |streams| and finds added and removed
362 // StreamParams since last time this method was called.
363 // For each new or removed StreamParam, OnLocalTrackSeen or
364 // OnLocalTrackRemoved is invoked.
365 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
366 cricket::MediaType media_type);
367
368 // Triggered when a local track has been seen for the first time in a local
369 // session description.
370 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
371 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
372 // in a MediaStream in |local_streams_|
373 void OnLocalTrackSeen(const std::string& stream_label,
374 const std::string& track_id,
375 uint32_t ssrc,
376 cricket::MediaType media_type);
377
378 // Triggered when a local track has been removed from a local session
379 // description.
380 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
381 // has been removed from the local SessionDescription and the stream can be
382 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
383 void OnLocalTrackRemoved(const std::string& stream_label,
384 const std::string& track_id,
385 uint32_t ssrc,
386 cricket::MediaType media_type);
387
388 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
389 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
390 void UpdateClosingRtpDataChannels(
391 const std::vector<std::string>& active_channels,
392 bool is_local_update);
393 void CreateRemoteRtpDataChannel(const std::string& label,
394 uint32_t remote_ssrc);
395
396 // Creates channel and adds it to the collection of DataChannels that will
397 // be offered in a SessionDescription.
398 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
399 const std::string& label,
400 const InternalDataChannelInit* config);
401
402 // Checks if any data channel has been added.
403 bool HasDataChannels() const;
404
405 void AllocateSctpSids(rtc::SSLRole role);
406 void OnSctpDataChannelClosed(DataChannel* channel);
407
408 // Notifications from WebRtcSession relating to BaseChannels.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700409 void OnVoiceChannelCreated();
deadbeefab9b2d12015-10-14 11:33:11 -0700410 void OnVoiceChannelDestroyed();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700411 void OnVideoChannelCreated();
deadbeefab9b2d12015-10-14 11:33:11 -0700412 void OnVideoChannelDestroyed();
413 void OnDataChannelCreated();
414 void OnDataChannelDestroyed();
415 // Called when the cricket::DataChannel receives a message indicating that a
416 // webrtc::DataChannel should be opened.
417 void OnDataChannelOpenMessage(const std::string& label,
418 const InternalDataChannelInit& config);
419
zhihuang1c378ed2017-08-17 14:10:50 -0700420 bool HasRtpSender(cricket::MediaType type) const;
deadbeefa601f5c2016-06-06 14:27:39 -0700421 RtpSenderInternal* FindSenderById(const std::string& id);
deadbeeffac06552015-11-25 11:26:01 -0800422
deadbeefa601f5c2016-06-06 14:27:39 -0700423 std::vector<rtc::scoped_refptr<
424 RtpSenderProxyWithInternal<RtpSenderInternal>>>::iterator
deadbeef70ab1a12015-09-28 16:53:55 -0700425 FindSenderForTrack(MediaStreamTrackInterface* track);
deadbeefa601f5c2016-06-06 14:27:39 -0700426 std::vector<rtc::scoped_refptr<
427 RtpReceiverProxyWithInternal<RtpReceiverInternal>>>::iterator
perkjd61bf802016-03-24 03:16:19 -0700428 FindReceiverForTrack(const std::string& track_id);
deadbeef70ab1a12015-09-28 16:53:55 -0700429
deadbeefab9b2d12015-10-14 11:33:11 -0700430 TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
431 TrackInfos* GetLocalTracks(cricket::MediaType media_type);
432 const TrackInfo* FindTrackInfo(const TrackInfos& infos,
433 const std::string& stream_label,
434 const std::string track_id) const;
435
436 // Returns the specified SCTP DataChannel in sctp_data_channels_,
437 // or nullptr if not found.
438 DataChannel* FindDataChannelBySid(int sid) const;
439
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700440 // Called when first configuring the port allocator.
deadbeef91dd5672016-05-18 16:55:30 -0700441 bool InitializePortAllocator_n(const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800442 // Called when SetConfiguration is called to apply the supported subset
443 // of the configuration on the network thread.
444 bool ReconfigurePortAllocator_n(
445 const cricket::ServerAddresses& stun_servers,
446 const std::vector<cricket::RelayServerConfig>& turn_servers,
447 IceTransportsType type,
448 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200449 bool prune_turn_ports,
450 webrtc::TurnCustomizer* turn_customizer);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700451
Elad Alon99c3fe52017-10-13 16:29:40 +0200452 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700453 // This function should only be called from the worker thread.
Elad Alon99c3fe52017-10-13 16:29:40 +0200454 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output);
455
Elad Alonacb24172017-10-06 14:32:13 +0200456 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700457 // This function should only be called from the worker thread.
458 void StopRtcEventLog_w();
459
Steve Anton038834f2017-07-14 15:59:59 -0700460 // Ensures the configuration doesn't have any parameters with invalid values,
461 // or values that conflict with other parameters.
462 //
463 // Returns RTCError::OK() if there are no issues.
464 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
465
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 // Storing the factory as a scoped reference pointer ensures that the memory
467 // in the PeerConnectionFactoryImpl remains available as long as the
468 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
469 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700470 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000472 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000474 UMAObserver* uma_observer_;
terelius33860252017-05-12 23:37:18 -0700475
476 // The EventLog needs to outlive |call_| (and any other object that uses it).
477 std::unique_ptr<RtcEventLog> event_log_;
478
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 SignalingState signaling_state_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 IceConnectionState ice_connection_state_;
481 IceGatheringState ice_gathering_state_;
deadbeef46c73892016-11-16 19:42:04 -0800482 PeerConnectionInterface::RTCConfiguration configuration_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
kwibergd1fe2812016-04-27 06:47:29 -0700484 std::unique_ptr<cricket::PortAllocator> port_allocator_;
deadbeefab9b2d12015-10-14 11:33:11 -0700485
zhihuang8f65cdf2016-05-06 18:40:30 -0700486 // One PeerConnection has only one RTCP CNAME.
487 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
488 std::string rtcp_cname_;
489
deadbeefab9b2d12015-10-14 11:33:11 -0700490 // Streams added via AddStream.
491 rtc::scoped_refptr<StreamCollection> local_streams_;
492 // Streams created as a result of SetRemoteDescription.
493 rtc::scoped_refptr<StreamCollection> remote_streams_;
494
kwibergd1fe2812016-04-27 06:47:29 -0700495 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_;
deadbeefeb459812015-12-15 19:24:43 -0800496
deadbeefab9b2d12015-10-14 11:33:11 -0700497 // These lists store track info seen in local/remote descriptions.
498 TrackInfos remote_audio_tracks_;
499 TrackInfos remote_video_tracks_;
500 TrackInfos local_audio_tracks_;
501 TrackInfos local_video_tracks_;
502
503 SctpSidAllocator sid_allocator_;
504 // label -> DataChannel
505 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
506 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -0800507 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -0700508
deadbeefbda7e0b2015-12-08 17:13:40 -0800509 bool remote_peer_supports_msid_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700510
terelius33860252017-05-12 23:37:18 -0700511 std::unique_ptr<Call> call_;
Steve Anton978b8762017-09-29 12:15:02 -0700512 WebRtcSession* session_;
513 std::unique_ptr<WebRtcSession> owned_session_;
terelius33860252017-05-12 23:37:18 -0700514 std::unique_ptr<StatsCollector> stats_; // A pointer is passed to senders_
515 rtc::scoped_refptr<RTCStatsCollector> stats_collector_;
516
deadbeefa601f5c2016-06-06 14:27:39 -0700517 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
518 senders_;
519 std::vector<
520 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
521 receivers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522};
523
524} // namespace webrtc
525
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200526#endif // PC_PEERCONNECTION_H_