blob: 2afb17c3baf70c7631e8cd9d0abeb17e467710c8 [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
Steve Antonba818672017-11-06 10:21:57 -080036// TODO(steveanton): Remove once WebRtcSession is merged into PeerConnection.
37std::string GetSignalingStateString(
38 PeerConnectionInterface::SignalingState state);
39
deadbeef70ab1a12015-09-28 16:53:55 -070040// PeerConnection implements the PeerConnectionInterface interface.
deadbeefab9b2d12015-10-14 11:33:11 -070041// It uses WebRtcSession to implement the PeerConnection functionality.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042class PeerConnection : public PeerConnectionInterface,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044 public sigslot::has_slots<> {
45 public:
Steve Antonba818672017-11-06 10:21:57 -080046 // TODO(steveanton): Remove once WebRtcSession is merged into PeerConnection.
47 friend class WebRtcSession;
48
zhihuang38ede132017-06-15 12:52:32 -070049 explicit PeerConnection(PeerConnectionFactory* factory,
50 std::unique_ptr<RtcEventLog> event_log,
51 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
deadbeef653b8e02015-11-11 12:55:10 -080053 bool Initialize(
54 const PeerConnectionInterface::RTCConfiguration& configuration,
kwibergd1fe2812016-04-27 06:47:29 -070055 std::unique_ptr<cricket::PortAllocator> allocator,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020056 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
deadbeef653b8e02015-11-11 12:55:10 -080057 PeerConnectionObserver* observer);
58
deadbeefa67696b2015-09-29 11:56:26 -070059 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
60 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
61 bool AddStream(MediaStreamInterface* local_stream) override;
62 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
deadbeefe1f9d832016-01-14 15:35:42 -080064 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
65 MediaStreamTrackInterface* track,
66 std::vector<MediaStreamInterface*> streams) override;
67 bool RemoveTrack(RtpSenderInterface* sender) override;
68
Steve Anton978b8762017-09-29 12:15:02 -070069 // TODO(steveanton): Remove this once all clients have switched to using the
70 // PeerConnection shims for WebRtcSession methods instead of the methods
71 // directly via this getter.
72 virtual WebRtcSession* session() { return session_; }
Alex Loikobf667942017-09-29 10:44:31 +000073
Steve Anton8c0f7a72017-10-03 10:03:10 -070074 // Gets the DTLS SSL certificate associated with the audio transport on the
75 // remote side. This will become populated once the DTLS connection with the
76 // peer has been completed, as indicated by the ICE connection state
77 // transitioning to kIceConnectionCompleted.
78 // Note that this will be removed once we implement RTCDtlsTransport which
79 // has standardized method for getting this information.
80 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
81 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
82
deadbeefa67696b2015-09-29 11:56:26 -070083 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
84 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
deadbeeffac06552015-11-25 11:26:01 -080086 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -080087 const std::string& kind,
88 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -080089
deadbeef70ab1a12015-09-28 16:53:55 -070090 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
91 const override;
92 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
93 const override;
94
deadbeefa67696b2015-09-29 11:56:26 -070095 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -070097 const DataChannelInit* config) override;
98 bool GetStats(StatsObserver* observer,
99 webrtc::MediaStreamTrackInterface* track,
100 StatsOutputLevel level) override;
hbos74e1a4f2016-09-15 23:33:01 -0700101 void GetStats(RTCStatsCollectorCallback* callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102
deadbeefa67696b2015-09-29 11:56:26 -0700103 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
deadbeefa67696b2015-09-29 11:56:26 -0700105 IceConnectionState ice_connection_state() override;
106 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
deadbeefa67696b2015-09-29 11:56:26 -0700108 const SessionDescriptionInterface* local_description() const override;
109 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800110 const SessionDescriptionInterface* current_local_description() const override;
111 const SessionDescriptionInterface* current_remote_description()
112 const override;
113 const SessionDescriptionInterface* pending_local_description() const override;
114 const SessionDescriptionInterface* pending_remote_description()
115 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116
117 // JSEP01
htaa2a49d92016-03-04 02:51:39 -0800118 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700119 void CreateOffer(CreateSessionDescriptionObserver* observer,
120 const MediaConstraintsInterface* constraints) override;
121 void CreateOffer(CreateSessionDescriptionObserver* observer,
122 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800123 // Deprecated, use version without constraints.
deadbeefa67696b2015-09-29 11:56:26 -0700124 void CreateAnswer(CreateSessionDescriptionObserver* observer,
125 const MediaConstraintsInterface* constraints) override;
htaa2a49d92016-03-04 02:51:39 -0800126 void CreateAnswer(CreateSessionDescriptionObserver* observer,
127 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700128 void SetLocalDescription(SetSessionDescriptionObserver* observer,
129 SessionDescriptionInterface* desc) override;
130 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
131 SessionDescriptionInterface* desc) override;
deadbeef46c73892016-11-16 19:42:04 -0800132 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 11:56:26 -0700133 bool SetConfiguration(
deadbeef293e9262017-01-11 12:28:30 -0800134 const PeerConnectionInterface::RTCConfiguration& configuration,
135 RTCError* error) override;
136 bool SetConfiguration(
137 const PeerConnectionInterface::RTCConfiguration& configuration) override {
138 return SetConfiguration(configuration, nullptr);
139 }
deadbeefa67696b2015-09-29 11:56:26 -0700140 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700141 bool RemoveIceCandidates(
142 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143
deadbeefa67696b2015-09-29 11:56:26 -0700144 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000145
zstein4b979802017-06-02 14:37:37 -0700146 RTCError SetBitrate(const BitrateParameters& bitrate) override;
147
Alex Narest78609d52017-10-20 10:37:47 +0200148 void SetBitrateAllocationStrategy(
149 std::unique_ptr<rtc::BitrateAllocationStrategy>
150 bitrate_allocation_strategy) override;
151
henrika5f6bf242017-11-01 11:06:56 +0100152 void SetAudioPlayout(bool playout) override;
153 void SetAudioRecording(bool recording) override;
154
Elad Alon99c3fe52017-10-13 16:29:40 +0200155 RTC_DEPRECATED bool StartRtcEventLog(rtc::PlatformFile file,
156 int64_t max_size_bytes) override;
157 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700158 void StopRtcEventLog() override;
159
deadbeefa67696b2015-09-29 11:56:26 -0700160 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
hbos82ebe022016-11-14 01:41:09 -0800162 sigslot::signal1<DataChannel*> SignalDataChannelCreated;
163
deadbeefab9b2d12015-10-14 11:33:11 -0700164 // Virtual for unit tests.
165 virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
166 sctp_data_channels() const {
167 return sctp_data_channels_;
perkjd61bf802016-03-24 03:16:19 -0700168 }
deadbeefab9b2d12015-10-14 11:33:11 -0700169
Steve Anton978b8762017-09-29 12:15:02 -0700170 // TODO(steveanton): These methods are temporarily added to facilitate work
171 // towards merging WebRtcSession into PeerConnection. To make this easier, we
172 // want only PeerConnection to interact with WebRtcSession so they can be
173 // merged easily. A few outside classes still access WebRtcSession methods
174 // directly, so these have been added to PeerConnection to remove the
175 // dependency from WebRtcSession.
176
177 rtc::Thread* network_thread() const { return factory_->network_thread(); }
178 rtc::Thread* worker_thread() const { return factory_->worker_thread(); }
179 rtc::Thread* signaling_thread() const { return factory_->signaling_thread(); }
Steve Antonba818672017-11-06 10:21:57 -0800180 virtual const std::string& session_id() const {
181 return session_->session_id();
182 }
Steve Anton978b8762017-09-29 12:15:02 -0700183 virtual bool session_created() const { return session_ != nullptr; }
184 virtual bool initial_offerer() const { return session_->initial_offerer(); }
185 virtual std::unique_ptr<SessionStats> GetSessionStats_s() {
Steve Antonba818672017-11-06 10:21:57 -0800186 return session_->GetSessionStats_s();
Steve Anton978b8762017-09-29 12:15:02 -0700187 }
188 virtual std::unique_ptr<SessionStats> GetSessionStats(
189 const ChannelNamePairs& channel_name_pairs) {
Steve Antonba818672017-11-06 10:21:57 -0800190 return session_->GetSessionStats(channel_name_pairs);
Steve Anton978b8762017-09-29 12:15:02 -0700191 }
192 virtual bool GetLocalCertificate(
193 const std::string& transport_name,
194 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
195 return session_->GetLocalCertificate(transport_name, certificate);
196 }
197 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
198 const std::string& transport_name) {
199 return session_->GetRemoteSSLCertificate(transport_name);
200 }
201 virtual Call::Stats GetCallStats() { return session_->GetCallStats(); }
202 virtual cricket::VoiceChannel* voice_channel() {
203 return session_->voice_channel();
204 }
205 virtual cricket::VideoChannel* video_channel() {
206 return session_->video_channel();
207 }
208 virtual cricket::RtpDataChannel* rtp_data_channel() {
209 return session_->rtp_data_channel();
210 }
211 virtual rtc::Optional<std::string> sctp_content_name() const {
212 return session_->sctp_content_name();
213 }
214 virtual rtc::Optional<std::string> sctp_transport_name() const {
215 return session_->sctp_transport_name();
216 }
217 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id) {
218 return session_->GetLocalTrackIdBySsrc(ssrc, track_id);
219 }
220 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id) {
221 return session_->GetRemoteTrackIdBySsrc(ssrc, track_id);
222 }
Steve Antond5585ca2017-10-23 14:49:26 -0700223 bool IceRestartPending(const std::string& content_name) const {
224 return session_->IceRestartPending(content_name);
225 }
226 bool NeedsIceRestart(const std::string& content_name) const {
227 return session_->NeedsIceRestart(content_name);
228 }
229 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) {
230 return session_->GetSslRole(content_name, role);
231 }
Steve Anton978b8762017-09-29 12:15:02 -0700232
233 // This is needed for stats tests to inject a MockWebRtcSession. Once
234 // WebRtcSession has been merged in, this will no longer be needed.
235 void set_session_for_testing(WebRtcSession* session) {
236 session_ = session;
237 }
238
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700240 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241
242 private:
deadbeefab9b2d12015-10-14 11:33:11 -0700243 struct TrackInfo {
244 TrackInfo() : ssrc(0) {}
245 TrackInfo(const std::string& stream_label,
246 const std::string track_id,
247 uint32_t ssrc)
248 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {}
deadbeefbda7e0b2015-12-08 17:13:40 -0800249 bool operator==(const TrackInfo& other) {
250 return this->stream_label == other.stream_label &&
251 this->track_id == other.track_id && this->ssrc == other.ssrc;
252 }
deadbeefab9b2d12015-10-14 11:33:11 -0700253 std::string stream_label;
254 std::string track_id;
255 uint32_t ssrc;
256 };
257 typedef std::vector<TrackInfo> TrackInfos;
258
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700260 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
deadbeefab9b2d12015-10-14 11:33:11 -0700262 void CreateAudioReceiver(MediaStreamInterface* stream,
perkjd61bf802016-03-24 03:16:19 -0700263 const std::string& track_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700264 uint32_t ssrc);
perkjf0dcfe22016-03-10 18:32:00 +0100265
deadbeefab9b2d12015-10-14 11:33:11 -0700266 void CreateVideoReceiver(MediaStreamInterface* stream,
perkjf0dcfe22016-03-10 18:32:00 +0100267 const std::string& track_id,
deadbeefab9b2d12015-10-14 11:33:11 -0700268 uint32_t ssrc);
Henrik Boström933d8b02017-10-10 10:05:16 -0700269 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
270 const std::string& track_id);
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700271
272 // May be called either by AddStream/RemoveStream, or when a track is
273 // added/removed from a stream previously added via AddStream.
274 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream);
275 void RemoveAudioTrack(AudioTrackInterface* track,
276 MediaStreamInterface* stream);
277 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream);
278 void RemoveVideoTrack(VideoTrackInterface* track,
279 MediaStreamInterface* stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280
Steve Antonba818672017-11-06 10:21:57 -0800281 void SetIceConnectionState(IceConnectionState new_state);
282 // Called any time the IceGatheringState changes
283 void OnIceGatheringChange(IceGatheringState new_state);
284 // New ICE candidate has been gathered.
285 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate);
286 // Some local ICE candidates have been removed.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700287 void OnIceCandidatesRemoved(
Steve Antonba818672017-11-06 10:21:57 -0800288 const std::vector<cricket::Candidate>& candidates);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289
Steve Antonba818672017-11-06 10:21:57 -0800290 // Update the state, signaling if necessary.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 void ChangeSignalingState(SignalingState signaling_state);
292
deadbeefeb459812015-12-15 19:24:43 -0800293 // Signals from MediaStreamObserver.
294 void OnAudioTrackAdded(AudioTrackInterface* track,
295 MediaStreamInterface* stream);
296 void OnAudioTrackRemoved(AudioTrackInterface* track,
297 MediaStreamInterface* stream);
298 void OnVideoTrackAdded(VideoTrackInterface* track,
299 MediaStreamInterface* stream);
300 void OnVideoTrackRemoved(VideoTrackInterface* track,
301 MediaStreamInterface* stream);
302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
304 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700305 void PostCreateSessionDescriptionFailure(
306 CreateSessionDescriptionObserver* observer,
307 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308
309 bool IsClosed() const {
310 return signaling_state_ == PeerConnectionInterface::kClosed;
311 }
312
deadbeefab9b2d12015-10-14 11:33:11 -0700313 // Returns a MediaSessionOptions struct with options decided by |options|,
314 // the local MediaStreams and DataChannels.
zhihuang1c378ed2017-08-17 14:10:50 -0700315 void GetOptionsForOffer(
deadbeefab9b2d12015-10-14 11:33:11 -0700316 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
317 cricket::MediaSessionOptions* session_options);
318
319 // Returns a MediaSessionOptions struct with options decided by
320 // |constraints|, the local MediaStreams and DataChannels.
zhihuang1c378ed2017-08-17 14:10:50 -0700321 void GetOptionsForAnswer(const RTCOfferAnswerOptions& options,
322 cricket::MediaSessionOptions* session_options);
htaa2a49d92016-03-04 02:51:39 -0800323
zhihuang1c378ed2017-08-17 14:10:50 -0700324 // Generates MediaDescriptionOptions for the |session_opts| based on existing
325 // local description or remote description.
326 void GenerateMediaDescriptionOptions(
327 const SessionDescriptionInterface* session_desc,
328 cricket::RtpTransceiverDirection audio_direction,
329 cricket::RtpTransceiverDirection video_direction,
330 rtc::Optional<size_t>* audio_index,
331 rtc::Optional<size_t>* video_index,
332 rtc::Optional<size_t>* data_index,
htaa2a49d92016-03-04 02:51:39 -0800333 cricket::MediaSessionOptions* session_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700334
deadbeeffaac4972015-11-12 15:33:07 -0800335 // Remove all local and remote tracks of type |media_type|.
336 // Called when a media type is rejected (m-line set to port 0).
337 void RemoveTracks(cricket::MediaType media_type);
338
deadbeefbda7e0b2015-12-08 17:13:40 -0800339 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
340 // and existing MediaStreamTracks are removed if there is no corresponding
341 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
342 // is created if it doesn't exist; if false, it's removed if it exists.
343 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700344 // If a new MediaStream is created it is added to |new_streams|.
345 void UpdateRemoteStreamsList(
346 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800347 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700348 cricket::MediaType media_type,
349 StreamCollection* new_streams);
350
351 // Triggered when a remote track has been seen for the first time in a remote
352 // session description. It creates a remote MediaStreamTrackInterface
353 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
354 void OnRemoteTrackSeen(const std::string& stream_label,
355 const std::string& track_id,
356 uint32_t ssrc,
357 cricket::MediaType media_type);
358
359 // Triggered when a remote track has been removed from a remote session
360 // description. It removes the remote track with id |track_id| from a remote
361 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
362 void OnRemoteTrackRemoved(const std::string& stream_label,
363 const std::string& track_id,
364 cricket::MediaType media_type);
365
366 // Finds remote MediaStreams without any tracks and removes them from
367 // |remote_streams_| and notifies the observer that the MediaStreams no longer
368 // exist.
369 void UpdateEndedRemoteMediaStreams();
370
deadbeefab9b2d12015-10-14 11:33:11 -0700371 // Loops through the vector of |streams| and finds added and removed
372 // StreamParams since last time this method was called.
373 // For each new or removed StreamParam, OnLocalTrackSeen or
374 // OnLocalTrackRemoved is invoked.
375 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
376 cricket::MediaType media_type);
377
378 // Triggered when a local track has been seen for the first time in a local
379 // session description.
380 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
381 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
382 // in a MediaStream in |local_streams_|
383 void OnLocalTrackSeen(const std::string& stream_label,
384 const std::string& track_id,
385 uint32_t ssrc,
386 cricket::MediaType media_type);
387
388 // Triggered when a local track has been removed from a local session
389 // description.
390 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
391 // has been removed from the local SessionDescription and the stream can be
392 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
393 void OnLocalTrackRemoved(const std::string& stream_label,
394 const std::string& track_id,
395 uint32_t ssrc,
396 cricket::MediaType media_type);
397
398 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
399 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
400 void UpdateClosingRtpDataChannels(
401 const std::vector<std::string>& active_channels,
402 bool is_local_update);
403 void CreateRemoteRtpDataChannel(const std::string& label,
404 uint32_t remote_ssrc);
405
406 // Creates channel and adds it to the collection of DataChannels that will
407 // be offered in a SessionDescription.
408 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
409 const std::string& label,
410 const InternalDataChannelInit* config);
411
412 // Checks if any data channel has been added.
413 bool HasDataChannels() const;
414
415 void AllocateSctpSids(rtc::SSLRole role);
416 void OnSctpDataChannelClosed(DataChannel* channel);
417
Steve Antonba818672017-11-06 10:21:57 -0800418 // Called when voice_channel_, video_channel_ and
419 // rtp_data_channel_/sctp_transport_ are created and destroyed. As a result
420 // of, for example, setting a new description.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700421 void OnVoiceChannelCreated();
deadbeefab9b2d12015-10-14 11:33:11 -0700422 void OnVoiceChannelDestroyed();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700423 void OnVideoChannelCreated();
deadbeefab9b2d12015-10-14 11:33:11 -0700424 void OnVideoChannelDestroyed();
425 void OnDataChannelCreated();
426 void OnDataChannelDestroyed();
Steve Antonba818672017-11-06 10:21:57 -0800427 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700428 void OnDataChannelOpenMessage(const std::string& label,
429 const InternalDataChannelInit& config);
430
zhihuang1c378ed2017-08-17 14:10:50 -0700431 bool HasRtpSender(cricket::MediaType type) const;
deadbeefa601f5c2016-06-06 14:27:39 -0700432 RtpSenderInternal* FindSenderById(const std::string& id);
deadbeeffac06552015-11-25 11:26:01 -0800433
deadbeefa601f5c2016-06-06 14:27:39 -0700434 std::vector<rtc::scoped_refptr<
435 RtpSenderProxyWithInternal<RtpSenderInternal>>>::iterator
deadbeef70ab1a12015-09-28 16:53:55 -0700436 FindSenderForTrack(MediaStreamTrackInterface* track);
deadbeefa601f5c2016-06-06 14:27:39 -0700437 std::vector<rtc::scoped_refptr<
438 RtpReceiverProxyWithInternal<RtpReceiverInternal>>>::iterator
perkjd61bf802016-03-24 03:16:19 -0700439 FindReceiverForTrack(const std::string& track_id);
deadbeef70ab1a12015-09-28 16:53:55 -0700440
deadbeefab9b2d12015-10-14 11:33:11 -0700441 TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
442 TrackInfos* GetLocalTracks(cricket::MediaType media_type);
443 const TrackInfo* FindTrackInfo(const TrackInfos& infos,
444 const std::string& stream_label,
445 const std::string track_id) const;
446
447 // Returns the specified SCTP DataChannel in sctp_data_channels_,
448 // or nullptr if not found.
449 DataChannel* FindDataChannelBySid(int sid) const;
450
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700451 // Called when first configuring the port allocator.
deadbeef91dd5672016-05-18 16:55:30 -0700452 bool InitializePortAllocator_n(const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800453 // Called when SetConfiguration is called to apply the supported subset
454 // of the configuration on the network thread.
455 bool ReconfigurePortAllocator_n(
456 const cricket::ServerAddresses& stun_servers,
457 const std::vector<cricket::RelayServerConfig>& turn_servers,
458 IceTransportsType type,
459 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200460 bool prune_turn_ports,
461 webrtc::TurnCustomizer* turn_customizer);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700462
Elad Alon99c3fe52017-10-13 16:29:40 +0200463 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700464 // This function should only be called from the worker thread.
Elad Alon99c3fe52017-10-13 16:29:40 +0200465 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output);
466
Elad Alonacb24172017-10-06 14:32:13 +0200467 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700468 // This function should only be called from the worker thread.
469 void StopRtcEventLog_w();
470
Steve Anton038834f2017-07-14 15:59:59 -0700471 // Ensures the configuration doesn't have any parameters with invalid values,
472 // or values that conflict with other parameters.
473 //
474 // Returns RTCError::OK() if there are no issues.
475 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
476
Steve Antonba818672017-11-06 10:21:57 -0800477 cricket::ChannelManager* channel_manager() const;
478 MetricsObserverInterface* metrics_observer() const;
479
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 // Storing the factory as a scoped reference pointer ensures that the memory
481 // in the PeerConnectionFactoryImpl remains available as long as the
482 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
483 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700484 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000486 rtc::scoped_refptr<PeerConnectionFactory> factory_;
Steve Antonba818672017-11-06 10:21:57 -0800487 PeerConnectionObserver* observer_ = nullptr;
488 UMAObserver* uma_observer_ = nullptr;
terelius33860252017-05-12 23:37:18 -0700489
490 // The EventLog needs to outlive |call_| (and any other object that uses it).
491 std::unique_ptr<RtcEventLog> event_log_;
492
Steve Antonba818672017-11-06 10:21:57 -0800493 SignalingState signaling_state_ = kStable;
494 IceConnectionState ice_connection_state_ = kIceConnectionNew;
495 IceGatheringState ice_gathering_state_ = kIceGatheringNew;
deadbeef46c73892016-11-16 19:42:04 -0800496 PeerConnectionInterface::RTCConfiguration configuration_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497
kwibergd1fe2812016-04-27 06:47:29 -0700498 std::unique_ptr<cricket::PortAllocator> port_allocator_;
deadbeefab9b2d12015-10-14 11:33:11 -0700499
zhihuang8f65cdf2016-05-06 18:40:30 -0700500 // One PeerConnection has only one RTCP CNAME.
501 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
502 std::string rtcp_cname_;
503
deadbeefab9b2d12015-10-14 11:33:11 -0700504 // Streams added via AddStream.
505 rtc::scoped_refptr<StreamCollection> local_streams_;
506 // Streams created as a result of SetRemoteDescription.
507 rtc::scoped_refptr<StreamCollection> remote_streams_;
508
kwibergd1fe2812016-04-27 06:47:29 -0700509 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_;
deadbeefeb459812015-12-15 19:24:43 -0800510
deadbeefab9b2d12015-10-14 11:33:11 -0700511 // These lists store track info seen in local/remote descriptions.
512 TrackInfos remote_audio_tracks_;
513 TrackInfos remote_video_tracks_;
514 TrackInfos local_audio_tracks_;
515 TrackInfos local_video_tracks_;
516
517 SctpSidAllocator sid_allocator_;
518 // label -> DataChannel
519 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
520 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -0800521 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -0700522
deadbeefbda7e0b2015-12-08 17:13:40 -0800523 bool remote_peer_supports_msid_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700524
terelius33860252017-05-12 23:37:18 -0700525 std::unique_ptr<Call> call_;
Steve Antonba818672017-11-06 10:21:57 -0800526 WebRtcSession* session_ = nullptr;
Steve Anton978b8762017-09-29 12:15:02 -0700527 std::unique_ptr<WebRtcSession> owned_session_;
terelius33860252017-05-12 23:37:18 -0700528 std::unique_ptr<StatsCollector> stats_; // A pointer is passed to senders_
529 rtc::scoped_refptr<RTCStatsCollector> stats_collector_;
530
deadbeefa601f5c2016-06-06 14:27:39 -0700531 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
532 senders_;
533 std::vector<
534 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
535 receivers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536};
537
538} // namespace webrtc
539
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200540#endif // PC_PEERCONNECTION_H_