blob: c1d3d40c2aec01ba131cebaa7fccea1317f9d946 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef PC_PEERCONNECTION_H_
12#define PC_PEERCONNECTION_H_
henrike@webrtc.org28e20752013-07-10 00:45:3613
perkjd61bf802016-03-24 10:16:1914#include <map>
kwibergd1fe2812016-04-27 13:47:2915#include <memory>
Steve Anton75737c02017-11-06 18:37:1716#include <set>
17#include <string>
perkjd61bf802016-03-24 10:16:1918#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:3619
Piotr (Peter) Slatalae0c2e972018-10-08 16:43:2120#include "api/media_transport_interface.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "api/peerconnectioninterface.h"
Jonas Orelandbdcee282017-10-10 12:01:4022#include "api/turncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "pc/iceserverparsing.h"
Zhi Huange830e682018-03-30 17:48:3524#include "pc/jseptransportcontroller.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "pc/peerconnectionfactory.h"
Steve Anton2d8609c2018-01-24 00:38:4626#include "pc/peerconnectioninternal.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "pc/rtcstatscollector.h"
Steve Anton4171afb2017-11-20 18:20:2228#include "pc/rtptransceiver.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3129#include "pc/statscollector.h"
30#include "pc/streamcollection.h"
Steve Anton75737c02017-11-06 18:37:1731#include "pc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:3632
33namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:3634
deadbeefeb459812015-12-16 03:24:4335class MediaStreamObserver;
perkjf0dcfe22016-03-10 17:32:0036class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 18:53:0537class RtcEventLog;
deadbeefab9b2d12015-10-14 18:33:1138
Steve Anton75737c02017-11-06 18:37:1739// PeerConnection is the implementation of the PeerConnection object as defined
40// by the PeerConnectionInterface API surface.
41// The class currently is solely responsible for the following:
42// - Managing the session state machine (signaling state).
43// - Creating and initializing lower-level objects, like PortAllocator and
44// BaseChannels.
45// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
46// objects.
47// - Tracking the current and pending local/remote session descriptions.
48// The class currently is jointly responsible for the following:
49// - Parsing and interpreting SDP.
50// - Generating offers and answers based on the current state.
51// - The ICE state machine.
52// - Generating stats.
Steve Anton2d8609c2018-01-24 00:38:4653class PeerConnection : public PeerConnectionInternal,
Steve Anton75737c02017-11-06 18:37:1754 public DataChannelProviderInterface,
Zhi Huang365381f2018-04-13 23:44:3455 public JsepTransportController::Observer,
Steve Antonad182762018-09-05 20:22:4056 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:3657 public sigslot::has_slots<> {
58 public:
Harald Alvestrand8ebba742018-05-31 12:00:3459 enum class UsageEvent : int {
60 TURN_SERVER_ADDED = 0x01,
61 STUN_SERVER_ADDED = 0x02,
62 DATA_ADDED = 0x04,
63 AUDIO_ADDED = 0x08,
64 VIDEO_ADDED = 0x10,
65 SET_LOCAL_DESCRIPTION_CALLED = 0x20,
66 SET_REMOTE_DESCRIPTION_CALLED = 0x40,
67 CANDIDATE_COLLECTED = 0x80,
68 REMOTE_CANDIDATE_ADDED = 0x100,
69 ICE_STATE_CONNECTED = 0x200,
Qingsi Wang7fc821d2018-07-12 19:54:5370 CLOSE_CALLED = 0x400,
Harald Alvestrand056d8112018-07-16 17:18:5871 PRIVATE_CANDIDATE_COLLECTED = 0x800,
72 MAX_VALUE = 0x1000,
Harald Alvestrand8ebba742018-05-31 12:00:3473 };
74
zhihuang38ede132017-06-15 19:52:3275 explicit PeerConnection(PeerConnectionFactory* factory,
76 std::unique_ptr<RtcEventLog> event_log,
77 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:3678
deadbeef653b8e02015-11-11 20:55:1079 bool Initialize(
80 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab588882018-05-02 22:12:4781 PeerConnectionDependencies dependencies);
deadbeef653b8e02015-11-11 20:55:1082
deadbeefa67696b2015-09-29 18:56:2683 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
84 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
85 bool AddStream(MediaStreamInterface* local_stream) override;
86 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:3687
Steve Anton2d6c76a2018-01-06 01:10:5288 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 18:23:5789 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:1090 const std::vector<std::string>& stream_ids) override;
deadbeefe1f9d832016-01-14 23:35:4291 bool RemoveTrack(RtpSenderInterface* sender) override;
Steve Anton24db5732018-07-23 17:27:3392 RTCError RemoveTrackNew(
93 rtc::scoped_refptr<RtpSenderInterface> sender) override;
deadbeefe1f9d832016-01-14 23:35:4294
Steve Anton9158ef62017-11-27 21:01:5295 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
96 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
97 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
98 rtc::scoped_refptr<MediaStreamTrackInterface> track,
99 const RtpTransceiverInit& init) override;
100 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
101 cricket::MediaType media_type) override;
102 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
103 cricket::MediaType media_type,
104 const RtpTransceiverInit& init) override;
105
Steve Anton8c0f7a72017-10-03 17:03:10106 // Gets the DTLS SSL certificate associated with the audio transport on the
107 // remote side. This will become populated once the DTLS connection with the
108 // peer has been completed, as indicated by the ICE connection state
109 // transitioning to kIceConnectionCompleted.
110 // Note that this will be removed once we implement RTCDtlsTransport which
111 // has standardized method for getting this information.
112 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
113 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
114
Zhi Huang70b820f2018-01-27 22:16:15115 // Version of the above method that returns the full certificate chain.
116 std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
117
deadbeeffac06552015-11-25 19:26:01118 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-19 00:58:44119 const std::string& kind,
120 const std::string& stream_id) override;
deadbeeffac06552015-11-25 19:26:01121
deadbeef70ab1a12015-09-28 23:53:55122 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
123 const override;
124 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
125 const override;
Steve Anton9158ef62017-11-27 21:01:52126 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
127 const override;
deadbeef70ab1a12015-09-28 23:53:55128
deadbeefa67696b2015-09-29 18:56:26129 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36130 const std::string& label,
deadbeefa67696b2015-09-29 18:56:26131 const DataChannelInit* config) override;
Henrik Boström1df1bf82018-03-20 12:24:20132 // WARNING: LEGACY. See peerconnectioninterface.h
deadbeefa67696b2015-09-29 18:56:26133 bool GetStats(StatsObserver* observer,
134 webrtc::MediaStreamTrackInterface* track,
135 StatsOutputLevel level) override;
Henrik Boström1df1bf82018-03-20 12:24:20136 // Spec-complaint GetStats(). See peerconnectioninterface.h
hbos74e1a4f2016-09-16 06:33:01137 void GetStats(RTCStatsCollectorCallback* callback) override;
Henrik Boström1df1bf82018-03-20 12:24:20138 void GetStats(
139 rtc::scoped_refptr<RtpSenderInterface> selector,
140 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
141 void GetStats(
142 rtc::scoped_refptr<RtpReceiverInterface> selector,
143 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
Harald Alvestrand89061872018-01-02 13:08:34144 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36145
deadbeefa67696b2015-09-29 18:56:26146 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36147
deadbeefa67696b2015-09-29 18:56:26148 IceConnectionState ice_connection_state() override;
149 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36150
deadbeefa67696b2015-09-29 18:56:26151 const SessionDescriptionInterface* local_description() const override;
152 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-21 01:56:17153 const SessionDescriptionInterface* current_local_description() const override;
154 const SessionDescriptionInterface* current_remote_description()
155 const override;
156 const SessionDescriptionInterface* pending_local_description() const override;
157 const SessionDescriptionInterface* pending_remote_description()
158 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36159
160 // JSEP01
deadbeefa67696b2015-09-29 18:56:26161 void CreateOffer(CreateSessionDescriptionObserver* observer,
162 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 10:51:39163 void CreateAnswer(CreateSessionDescriptionObserver* observer,
164 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 18:56:26165 void SetLocalDescription(SetSessionDescriptionObserver* observer,
166 SessionDescriptionInterface* desc) override;
Henrik Boströma4ecf552017-11-23 14:17:07167 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
168 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 16:48:32169 void SetRemoteDescription(
170 std::unique_ptr<SessionDescriptionInterface> desc,
171 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
172 override;
deadbeef46c73892016-11-17 03:42:04173 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 18:56:26174 bool SetConfiguration(
deadbeef293e9262017-01-11 20:28:30175 const PeerConnectionInterface::RTCConfiguration& configuration,
176 RTCError* error) override;
177 bool SetConfiguration(
178 const PeerConnectionInterface::RTCConfiguration& configuration) override {
179 return SetConfiguration(configuration, nullptr);
180 }
deadbeefa67696b2015-09-29 18:56:26181 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 18:59:18182 bool RemoveIceCandidates(
183 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36184
Niels Möller0c4f7be2018-05-07 12:01:37185 RTCError SetBitrate(const BitrateSettings& bitrate) override;
zstein4b979802017-06-02 21:37:37186
Alex Narest78609d52017-10-20 08:37:47187 void SetBitrateAllocationStrategy(
188 std::unique_ptr<rtc::BitrateAllocationStrategy>
189 bitrate_allocation_strategy) override;
190
henrika5f6bf242017-11-01 10:06:56191 void SetAudioPlayout(bool playout) override;
192 void SetAudioRecording(bool recording) override;
193
Elad Alon99c3fe52017-10-13 14:29:40194 RTC_DEPRECATED bool StartRtcEventLog(rtc::PlatformFile file,
195 int64_t max_size_bytes) override;
Bjorn Tereliusde939432017-11-20 16:38:14196 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
197 int64_t output_period_ms) override;
ivoc14d5dbe2016-07-04 14:06:55198 void StopRtcEventLog() override;
199
deadbeefa67696b2015-09-29 18:56:26200 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36201
Steve Anton2d8609c2018-01-24 00:38:46202 // PeerConnectionInternal implementation.
203 rtc::Thread* network_thread() const override {
204 return factory_->network_thread();
205 }
206 rtc::Thread* worker_thread() const override {
207 return factory_->worker_thread();
208 }
209 rtc::Thread* signaling_thread() const override {
210 return factory_->signaling_thread();
perkjd61bf802016-03-24 10:16:19211 }
deadbeefab9b2d12015-10-14 18:33:11212
Steve Antonbe5e2082018-01-24 23:29:17213 std::string session_id() const override { return session_id_; }
Steve Anton75737c02017-11-06 18:37:17214
Steve Anton2d8609c2018-01-24 00:38:46215 bool initial_offerer() const override {
Zhi Huange830e682018-03-30 17:48:35216 return transport_controller_ && transport_controller_->initial_offerer();
Steve Anton2d8609c2018-01-24 00:38:46217 }
Steve Anton75737c02017-11-06 18:37:17218
Steve Anton2d8609c2018-01-24 00:38:46219 std::vector<
220 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 18:07:54221 GetTransceiversInternal() const override {
Steve Anton2d8609c2018-01-24 00:38:46222 return transceivers_;
223 }
224
225 bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override;
226 bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override;
227
228 sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
229 return SignalDataChannelCreated_;
230 }
231
232 cricket::RtpDataChannel* rtp_data_channel() const override {
Steve Anton75737c02017-11-06 18:37:17233 return rtp_data_channel_;
Steve Anton978b8762017-09-29 19:15:02234 }
Steve Anton2d8609c2018-01-24 00:38:46235
Steve Antonbe5e2082018-01-24 23:29:17236 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels()
Steve Anton2d8609c2018-01-24 00:38:46237 const override {
238 return sctp_data_channels_;
239 }
240
Danil Chapovalov66cadcc2018-06-19 14:47:43241 absl::optional<std::string> sctp_content_name() const override {
Zhi Huange830e682018-03-30 17:48:35242 return sctp_mid_;
Steve Anton75737c02017-11-06 18:37:17243 }
Steve Anton2d8609c2018-01-24 00:38:46244
Danil Chapovalov66cadcc2018-06-19 14:47:43245 absl::optional<std::string> sctp_transport_name() const override;
Steve Anton75737c02017-11-06 18:37:17246
Qingsi Wang72a43a12018-02-21 00:03:18247 cricket::CandidateStatsList GetPooledCandidateStats() const override;
Steve Anton5dfde182018-02-06 18:34:40248 std::map<std::string, std::string> GetTransportNamesByMid() const override;
249 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
250 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-24 00:38:46251 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 18:37:17252
Steve Anton2d8609c2018-01-24 00:38:46253 bool GetLocalCertificate(
254 const std::string& transport_name,
255 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
Taylor Brandstetterc3928662018-02-23 21:04:51256 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Steve Anton2d8609c2018-01-24 00:38:46257 const std::string& transport_name) override;
258 bool IceRestartPending(const std::string& content_name) const override;
259 bool NeedsIceRestart(const std::string& content_name) const override;
260 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Steve Anton7464fca2018-01-19 19:10:37261
Harald Alvestrand19793842018-06-25 10:03:50262 void ReturnHistogramVeryQuicklyForTesting() {
263 return_histogram_very_quickly_ = true;
264 }
Harald Alvestrand7a1c7f72018-08-01 08:50:16265 void RequestUsagePatternReportForTesting();
Harald Alvestrand19793842018-06-25 10:03:50266
henrike@webrtc.org28e20752013-07-10 00:45:36267 protected:
deadbeefa67696b2015-09-29 18:56:26268 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36269
270 private:
Henrik Boström31638672017-11-23 16:48:32271 class SetRemoteDescriptionObserverAdapter;
272 friend class SetRemoteDescriptionObserverAdapter;
273
Steve Anton4171afb2017-11-20 18:20:22274 struct RtpSenderInfo {
275 RtpSenderInfo() : first_ssrc(0) {}
Seth Hampson845e8782018-03-02 19:34:10276 RtpSenderInfo(const std::string& stream_id,
Steve Anton4171afb2017-11-20 18:20:22277 const std::string sender_id,
278 uint32_t ssrc)
Seth Hampson845e8782018-03-02 19:34:10279 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
Steve Anton4171afb2017-11-20 18:20:22280 bool operator==(const RtpSenderInfo& other) {
Seth Hampson845e8782018-03-02 19:34:10281 return this->stream_id == other.stream_id &&
Steve Anton4171afb2017-11-20 18:20:22282 this->sender_id == other.sender_id &&
283 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-09 01:13:40284 }
Seth Hampson845e8782018-03-02 19:34:10285 std::string stream_id;
Steve Anton4171afb2017-11-20 18:20:22286 std::string sender_id;
287 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
288 // for communicating with the lower layers.
289 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 18:33:11290 };
deadbeefab9b2d12015-10-14 18:33:11291
Steve Antonad182762018-09-05 20:22:40292 // Implements MessageHandler.
293 void OnMessage(rtc::Message* msg) override;
294
Steve Antonafb0bb72018-02-20 19:35:37295 // Plan B helpers for getting the voice/video media channels for the single
296 // audio/video transceiver, if it exists.
297 cricket::VoiceMediaChannel* voice_media_channel() const;
298 cricket::VideoMediaChannel* video_media_channel() const;
Steve Anton60776752018-01-10 19:51:34299
Steve Anton4171afb2017-11-20 18:20:22300 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
301 GetSendersInternal() const;
302 std::vector<
303 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
304 GetReceiversInternal() const;
305
306 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
307 GetAudioTransceiver() const;
308 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
309 GetVideoTransceiver() const;
310
Steve Antonafb0bb72018-02-20 19:35:37311 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
312 GetFirstAudioTransceiver() const;
313
deadbeefab9b2d12015-10-14 18:33:11314 void CreateAudioReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 18:20:22315 const RtpSenderInfo& remote_sender_info);
perkjf0dcfe22016-03-10 17:32:00316
deadbeefab9b2d12015-10-14 18:33:11317 void CreateVideoReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 18:20:22318 const RtpSenderInfo& remote_sender_info);
Henrik Boström933d8b02017-10-10 17:05:16319 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Steve Anton4171afb2017-11-20 18:20:22320 const RtpSenderInfo& remote_sender_info);
korniltsev.anatolyec390b52017-07-25 00:00:25321
322 // May be called either by AddStream/RemoveStream, or when a track is
323 // added/removed from a stream previously added via AddStream.
324 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream);
325 void RemoveAudioTrack(AudioTrackInterface* track,
326 MediaStreamInterface* stream);
327 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream);
328 void RemoveVideoTrack(VideoTrackInterface* track,
329 MediaStreamInterface* stream);
henrike@webrtc.org28e20752013-07-10 00:45:36330
Steve Antonf9381f02017-12-14 18:23:57331 // AddTrack implementation when Unified Plan is specified.
332 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
333 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:10334 const std::vector<std::string>& stream_ids);
Steve Antonf9381f02017-12-14 18:23:57335 // AddTrack implementation when Plan B is specified.
336 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
337 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:10338 const std::vector<std::string>& stream_ids);
Steve Antonf9381f02017-12-14 18:23:57339
340 // Returns the first RtpTransceiver suitable for a newly added track, if such
341 // transceiver is available.
342 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
343 FindFirstTransceiverForAddedTrack(
344 rtc::scoped_refptr<MediaStreamTrackInterface> track);
345
Steve Antonf9381f02017-12-14 18:23:57346 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
347 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender);
348
Steve Anton22da89f2018-01-25 21:58:07349 // Internal implementation for AddTransceiver family of methods. If
350 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
Steve Anton9158ef62017-11-27 21:01:52351 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
352 cricket::MediaType media_type,
353 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 21:58:07354 const RtpTransceiverInit& init,
355 bool fire_callback = true);
Steve Anton9158ef62017-11-27 21:01:52356
Steve Anton02ee47c2018-01-11 00:26:06357 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
358 CreateSender(cricket::MediaType media_type,
Steve Anton111fdfd2018-06-25 20:03:36359 const std::string& id,
Steve Anton02ee47c2018-01-11 00:26:06360 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Florent Castelli892acf02018-10-01 20:47:20361 const std::vector<std::string>& stream_ids,
362 const std::vector<RtpEncodingParameters>& send_encodings);
Steve Anton02ee47c2018-01-11 00:26:06363
364 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
365 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
366
Steve Antonf9381f02017-12-14 18:23:57367 // Create a new RtpTransceiver of the given type and add it to the list of
368 // transceivers.
369 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Steve Anton02ee47c2018-01-11 00:26:06370 CreateAndAddTransceiver(
371 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
372 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
373 receiver);
Steve Antonf9381f02017-12-14 18:23:57374
Steve Antonba818672017-11-06 18:21:57375 void SetIceConnectionState(IceConnectionState new_state);
376 // Called any time the IceGatheringState changes
377 void OnIceGatheringChange(IceGatheringState new_state);
378 // New ICE candidate has been gathered.
379 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate);
380 // Some local ICE candidates have been removed.
Honghai Zhang7fb69db2016-03-14 18:59:18381 void OnIceCandidatesRemoved(
Steve Antonba818672017-11-06 18:21:57382 const std::vector<cricket::Candidate>& candidates);
henrike@webrtc.org28e20752013-07-10 00:45:36383
Steve Antonba818672017-11-06 18:21:57384 // Update the state, signaling if necessary.
henrike@webrtc.org28e20752013-07-10 00:45:36385 void ChangeSignalingState(SignalingState signaling_state);
386
deadbeefeb459812015-12-16 03:24:43387 // Signals from MediaStreamObserver.
388 void OnAudioTrackAdded(AudioTrackInterface* track,
389 MediaStreamInterface* stream);
390 void OnAudioTrackRemoved(AudioTrackInterface* track,
391 MediaStreamInterface* stream);
392 void OnVideoTrackAdded(VideoTrackInterface* track,
393 MediaStreamInterface* stream);
394 void OnVideoTrackRemoved(VideoTrackInterface* track,
395 MediaStreamInterface* stream);
396
Henrik Boström31638672017-11-23 16:48:32397 void PostSetSessionDescriptionSuccess(
398 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36399 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
Steve Antonad182762018-09-05 20:22:40400 RTCError&& error);
deadbeefab9b2d12015-10-14 18:33:11401 void PostCreateSessionDescriptionFailure(
402 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 14:18:03403 RTCError error);
henrike@webrtc.org28e20752013-07-10 00:45:36404
Steve Anton8a006912017-12-04 23:25:56405 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
406 // that return an RTCError instead of invoking a callback.
407 RTCError ApplyLocalDescription(
408 std::unique_ptr<SessionDescriptionInterface> desc);
409 RTCError ApplyRemoteDescription(
410 std::unique_ptr<SessionDescriptionInterface> desc);
411
Steve Antondcc3c022017-12-23 00:02:54412 // Updates the local RtpTransceivers according to the JSEP rules. Called as
413 // part of setting the local/remote description.
414 RTCError UpdateTransceiversAndDataChannels(
415 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 23:33:48416 const SessionDescriptionInterface& new_session,
417 const SessionDescriptionInterface* old_local_description,
418 const SessionDescriptionInterface* old_remote_description);
Steve Antondcc3c022017-12-23 00:02:54419
420 // Either creates or destroys the transceiver's BaseChannel according to the
421 // given media section.
422 RTCError UpdateTransceiverChannel(
423 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
424 transceiver,
425 const cricket::ContentInfo& content,
426 const cricket::ContentGroup* bundle_group);
427
Steve Antonfa2260d2017-12-29 00:38:23428 // Either creates or destroys the local data channel according to the given
429 // media section.
430 RTCError UpdateDataChannel(cricket::ContentSource source,
431 const cricket::ContentInfo& content,
432 const cricket::ContentGroup* bundle_group);
433
Steve Antondcc3c022017-12-23 00:02:54434 // Associate the given transceiver according to the JSEP rules.
435 RTCErrorOr<
436 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
437 AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 23:33:48438 SdpType type,
Steve Antondcc3c022017-12-23 00:02:54439 size_t mline_index,
440 const cricket::ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 23:33:48441 const cricket::ContentInfo* old_local_content,
442 const cricket::ContentInfo* old_remote_content);
Steve Antondcc3c022017-12-23 00:02:54443
444 // Returns the RtpTransceiver, if found, that is associated to the given MID.
445 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
446 GetAssociatedTransceiver(const std::string& mid) const;
447
448 // Returns the RtpTransceiver, if found, that was assigned to the given mline
449 // index in CreateOffer.
450 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
451 GetTransceiverByMLineIndex(size_t mline_index) const;
452
453 // Returns an RtpTransciever, if available, that can be used to receive the
454 // given media type according to JSEP rules.
455 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
456 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
457
Steve Antoned10bd92017-12-05 18:52:59458 // Returns the media section in the given session description that is
459 // associated with the RtpTransceiver. Returns null if none found or this
460 // RtpTransceiver is not associated. Logic varies depending on the
461 // SdpSemantics specified in the configuration.
462 const cricket::ContentInfo* FindMediaSectionForTransceiver(
463 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
464 transceiver,
465 const SessionDescriptionInterface* sdesc) const;
466
Steve Anton0f5400a2018-07-17 21:25:36467 // Runs the algorithm **process the removal of a remote track** specified in
468 // the WebRTC specification.
469 // This method will update the following lists:
470 // |remove_list| is the list of transceivers for which the receiving track is
471 // being removed.
472 // |removed_streams| is the list of streams which no longer have a receiving
473 // track so should be removed.
474 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
475 void ProcessRemovalOfRemoteTrack(
476 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
477 transceiver,
478 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
479 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
480
Steve Anton52d86772018-02-20 23:48:12481 void OnNegotiationNeeded();
482
henrike@webrtc.org28e20752013-07-10 00:45:36483 bool IsClosed() const {
484 return signaling_state_ == PeerConnectionInterface::kClosed;
485 }
486
deadbeefab9b2d12015-10-14 18:33:11487 // Returns a MediaSessionOptions struct with options decided by |options|,
488 // the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-23 00:02:54489 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
490 offer_answer_options,
491 cricket::MediaSessionOptions* session_options);
492 void GetOptionsForPlanBOffer(
493 const PeerConnectionInterface::RTCOfferAnswerOptions&
494 offer_answer_options,
495 cricket::MediaSessionOptions* session_options);
496 void GetOptionsForUnifiedPlanOffer(
497 const PeerConnectionInterface::RTCOfferAnswerOptions&
498 offer_answer_options,
deadbeefab9b2d12015-10-14 18:33:11499 cricket::MediaSessionOptions* session_options);
500
Steve Anton22da89f2018-01-25 21:58:07501 RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options);
502 void RemoveRecvDirectionFromReceivingTransceiversOfType(
503 cricket::MediaType media_type);
504 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
505 std::vector<
506 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
507 GetReceivingTransceiversOfType(cricket::MediaType media_type);
508
deadbeefab9b2d12015-10-14 18:33:11509 // Returns a MediaSessionOptions struct with options decided by
510 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-23 00:02:54511 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
zhihuang1c378ed2017-08-17 21:10:50512 cricket::MediaSessionOptions* session_options);
Steve Antondcc3c022017-12-23 00:02:54513 void GetOptionsForPlanBAnswer(
514 const PeerConnectionInterface::RTCOfferAnswerOptions&
515 offer_answer_options,
516 cricket::MediaSessionOptions* session_options);
517 void GetOptionsForUnifiedPlanAnswer(
518 const PeerConnectionInterface::RTCOfferAnswerOptions&
519 offer_answer_options,
520 cricket::MediaSessionOptions* session_options);
htaa2a49d92016-03-04 10:51:39521
zhihuang1c378ed2017-08-17 21:10:50522 // Generates MediaDescriptionOptions for the |session_opts| based on existing
523 // local description or remote description.
524 void GenerateMediaDescriptionOptions(
525 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 22:30:09526 RtpTransceiverDirection audio_direction,
527 RtpTransceiverDirection video_direction,
Danil Chapovalov66cadcc2018-06-19 14:47:43528 absl::optional<size_t>* audio_index,
529 absl::optional<size_t>* video_index,
530 absl::optional<size_t>* data_index,
htaa2a49d92016-03-04 10:51:39531 cricket::MediaSessionOptions* session_options);
deadbeefab9b2d12015-10-14 18:33:11532
Steve Antonfa2260d2017-12-29 00:38:23533 // Generates the active MediaDescriptionOptions for the local data channel
534 // given the specified MID.
535 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
536 const std::string& mid) const;
537
538 // Generates the rejected MediaDescriptionOptions for the local data channel
539 // given the specified MID.
540 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
541 const std::string& mid) const;
542
543 // Returns the MID for the data section associated with either the
544 // RtpDataChannel or SCTP data channel, if it has been set. If no data
545 // channels are configured this will return nullopt.
Danil Chapovalov66cadcc2018-06-19 14:47:43546 absl::optional<std::string> GetDataMid() const;
Steve Antonfa2260d2017-12-29 00:38:23547
Steve Anton4171afb2017-11-20 18:20:22548 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 23:33:07549 // Called when a media type is rejected (m-line set to port 0).
Steve Anton4171afb2017-11-20 18:20:22550 void RemoveSenders(cricket::MediaType media_type);
deadbeeffaac4972015-11-12 23:33:07551
deadbeefbda7e0b2015-12-09 01:13:40552 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
553 // and existing MediaStreamTracks are removed if there is no corresponding
554 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
555 // is created if it doesn't exist; if false, it's removed if it exists.
556 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 18:33:11557 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 18:20:22558 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 18:33:11559 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-09 01:13:40560 bool default_track_needed,
deadbeefab9b2d12015-10-14 18:33:11561 cricket::MediaType media_type,
562 StreamCollection* new_streams);
563
Steve Anton4171afb2017-11-20 18:20:22564 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 18:33:11565 // session description. It creates a remote MediaStreamTrackInterface
566 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 18:20:22567 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
568 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 18:33:11569
Steve Anton4171afb2017-11-20 18:20:22570 // Triggered when a remote sender has been removed from a remote session
571 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 18:33:11572 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 18:20:22573 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
574 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 18:33:11575
576 // Finds remote MediaStreams without any tracks and removes them from
577 // |remote_streams_| and notifies the observer that the MediaStreams no longer
578 // exist.
579 void UpdateEndedRemoteMediaStreams();
580
deadbeefab9b2d12015-10-14 18:33:11581 // Loops through the vector of |streams| and finds added and removed
582 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 18:20:22583 // For each new or removed StreamParam, OnLocalSenderSeen or
584 // OnLocalSenderRemoved is invoked.
585 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
586 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 18:33:11587
Steve Anton4171afb2017-11-20 18:20:22588 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 18:33:11589 // session description.
590 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
591 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
592 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 18:20:22593 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
594 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 18:33:11595
Steve Anton4171afb2017-11-20 18:20:22596 // Triggered when a local sender has been removed from a local session
deadbeefab9b2d12015-10-14 18:33:11597 // description.
598 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
599 // has been removed from the local SessionDescription and the stream can be
600 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 18:20:22601 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
602 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 18:33:11603
604 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
605 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
606 void UpdateClosingRtpDataChannels(
607 const std::vector<std::string>& active_channels,
608 bool is_local_update);
609 void CreateRemoteRtpDataChannel(const std::string& label,
610 uint32_t remote_ssrc);
611
612 // Creates channel and adds it to the collection of DataChannels that will
613 // be offered in a SessionDescription.
614 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
615 const std::string& label,
616 const InternalDataChannelInit* config);
617
618 // Checks if any data channel has been added.
619 bool HasDataChannels() const;
620
621 void AllocateSctpSids(rtc::SSLRole role);
622 void OnSctpDataChannelClosed(DataChannel* channel);
623
deadbeefab9b2d12015-10-14 18:33:11624 void OnDataChannelDestroyed();
Steve Antonba818672017-11-06 18:21:57625 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 18:33:11626 void OnDataChannelOpenMessage(const std::string& label,
627 const InternalDataChannelInit& config);
628
Steve Anton4171afb2017-11-20 18:20:22629 // Returns true if the PeerConnection is configured to use Unified Plan
630 // semantics for creating offers/answers and setting local/remote
631 // descriptions. If this is true the RtpTransceiver API will also be available
632 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 18:25:56633 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
634 // sufficient time has passed.
635 bool IsUnifiedPlan() const {
636 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
637 }
Steve Anton4171afb2017-11-20 18:20:22638
639 // Is there an RtpSender of the given type?
zhihuang1c378ed2017-08-17 21:10:50640 bool HasRtpSender(cricket::MediaType type) const;
deadbeeffac06552015-11-25 19:26:01641
Steve Anton4171afb2017-11-20 18:20:22642 // Return the RtpSender with the given track attached.
643 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
644 FindSenderForTrack(MediaStreamTrackInterface* track) const;
deadbeef70ab1a12015-09-28 23:53:55645
Steve Anton4171afb2017-11-20 18:20:22646 // Return the RtpSender with the given id, or null if none exists.
647 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
648 FindSenderById(const std::string& sender_id) const;
649
650 // Return the RtpReceiver with the given id, or null if none exists.
651 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
652 FindReceiverById(const std::string& receiver_id) const;
653
654 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
655 cricket::MediaType media_type);
656 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
657 cricket::MediaType media_type);
658 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
Emircan Uysalerbc609eaa2018-03-27 21:57:18659 const std::string& stream_id,
Steve Anton4171afb2017-11-20 18:20:22660 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 18:33:11661
662 // Returns the specified SCTP DataChannel in sctp_data_channels_,
663 // or nullptr if not found.
664 DataChannel* FindDataChannelBySid(int sid) const;
665
Taylor Brandstettera1c30352016-05-13 15:15:11666 // Called when first configuring the port allocator.
Harald Alvestrandb2a74782018-06-28 11:54:07667 bool InitializePortAllocator_n(
668 const cricket::ServerAddresses& stun_servers,
669 const std::vector<cricket::RelayServerConfig>& turn_servers,
670 const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 20:28:30671 // Called when SetConfiguration is called to apply the supported subset
672 // of the configuration on the network thread.
673 bool ReconfigurePortAllocator_n(
674 const cricket::ServerAddresses& stun_servers,
675 const std::vector<cricket::RelayServerConfig>& turn_servers,
676 IceTransportsType type,
677 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 12:01:40678 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 22:45:49679 webrtc::TurnCustomizer* turn_customizer,
Danil Chapovalov66cadcc2018-06-19 14:47:43680 absl::optional<int> stun_candidate_keepalive_interval);
Taylor Brandstettera1c30352016-05-13 15:15:11681
Elad Alon99c3fe52017-10-13 14:29:40682 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 14:06:55683 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 16:38:14684 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
685 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 14:29:40686
Elad Alonacb24172017-10-06 12:32:13687 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 14:06:55688 // This function should only be called from the worker thread.
689 void StopRtcEventLog_w();
690
Steve Anton038834f2017-07-14 22:59:59691 // Ensures the configuration doesn't have any parameters with invalid values,
692 // or values that conflict with other parameters.
693 //
694 // Returns RTCError::OK() if there are no issues.
695 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
696
Steve Antonba818672017-11-06 18:21:57697 cricket::ChannelManager* channel_manager() const;
Steve Antonba818672017-11-06 18:21:57698
Steve Antonf8470812017-12-04 18:46:21699 enum class SessionError {
700 kNone, // No error.
701 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
702 kTransport, // Error from the underlying transport.
703 };
704
Steve Anton75737c02017-11-06 18:37:17705 // Returns the last error in the session. See the enum above for details.
Steve Antonf8470812017-12-04 18:46:21706 SessionError session_error() const { return session_error_; }
707 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 18:37:17708
Steve Anton75737c02017-11-06 18:37:17709 cricket::BaseChannel* GetChannel(const std::string& content_name);
710
711 // Get current SSL role used by SCTP's underlying transport.
712 bool GetSctpSslRole(rtc::SSLRole* role);
713
Steve Anton75737c02017-11-06 18:37:17714 cricket::IceConfig ParseIceConfig(
715 const PeerConnectionInterface::RTCConfiguration& config) const;
716
Steve Anton75737c02017-11-06 18:37:17717 // Implements DataChannelProviderInterface.
718 bool SendData(const cricket::SendDataParams& params,
719 const rtc::CopyOnWriteBuffer& payload,
720 cricket::SendDataResult* result) override;
721 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
722 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
723 void AddSctpDataStream(int sid) override;
724 void RemoveSctpDataStream(int sid) override;
725 bool ReadyToSendData() const override;
726
727 cricket::DataChannelType data_channel_type() const;
728
Steve Anton75737c02017-11-06 18:37:17729 // Called when an RTCCertificate is generated or retrieved by
730 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
731 void OnCertificateReady(
732 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
733 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
734
Steve Anton75737c02017-11-06 18:37:17735 // Non-const versions of local_description()/remote_description(), for use
736 // internally.
737 SessionDescriptionInterface* mutable_local_description() {
738 return pending_local_description_ ? pending_local_description_.get()
739 : current_local_description_.get();
740 }
741 SessionDescriptionInterface* mutable_remote_description() {
742 return pending_remote_description_ ? pending_remote_description_.get()
743 : current_remote_description_.get();
744 }
745
746 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 18:46:21747 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 18:37:17748
Zhi Huange830e682018-03-30 17:48:35749 RTCError UpdateSessionState(SdpType type,
750 cricket::ContentSource source,
751 const cricket::SessionDescription* description);
Steve Anton75737c02017-11-06 18:37:17752 // Push the media parts of the local or remote session description
753 // down to all of the channels.
Steve Anton3828c062017-12-06 18:34:51754 RTCError PushdownMediaDescription(SdpType type,
Steve Anton8a006912017-12-04 23:25:56755 cricket::ContentSource source);
Steve Anton75737c02017-11-06 18:37:17756 bool PushdownSctpParameters_n(cricket::ContentSource source);
757
Steve Anton8a006912017-12-04 23:25:56758 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 18:34:51759 SdpType type);
Steve Anton75737c02017-11-06 18:37:17760
761 // Returns true and the TransportInfo of the given |content_name|
762 // from |description|. Returns false if it's not available.
763 static bool GetTransportDescription(
764 const cricket::SessionDescription* description,
765 const std::string& content_name,
766 cricket::TransportDescription* info);
767
Steve Anton75737c02017-11-06 18:37:17768 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 18:52:59769 // This enables media to flow on all configured audio/video channels and the
770 // RtpDataChannel.
771 void EnableSending();
Steve Anton3fe1b152017-12-12 18:20:08772
Steve Anton8af21862017-12-15 19:20:13773 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
774 void DestroyAllChannels();
Steve Anton3fe1b152017-12-12 18:20:08775
Steve Anton75737c02017-11-06 18:37:17776 // Returns the media index for a local ice candidate given the content name.
777 // Returns false if the local session description does not have a media
778 // content called |content_name|.
779 bool GetLocalCandidateMediaIndex(const std::string& content_name,
780 int* sdp_mline_index);
781 // Uses all remote candidates in |remote_desc| in this session.
782 bool UseCandidatesInSessionDescription(
783 const SessionDescriptionInterface* remote_desc);
784 // Uses |candidate| in this session.
785 bool UseCandidate(const IceCandidateInterface* candidate);
786 // Deletes the corresponding channel of contents that don't exist in |desc|.
787 // |desc| can be null. This means that all channels are deleted.
788 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
789
790 // Allocates media channels based on the |desc|. If |desc| doesn't have
791 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
792 // This method will also delete any existing media channels before creating.
Steve Antondcc3c022017-12-23 00:02:54793 RTCError CreateChannels(const cricket::SessionDescription& desc);
794
795 // If the BUNDLE policy is max-bundle, then we know for sure that all
796 // transports will be bundled from the start. This method returns the BUNDLE
797 // group if that's the case, or null if BUNDLE will be negotiated later. An
798 // error is returned if max-bundle is specified but the session description
799 // does not have a BUNDLE group.
800 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
801 const cricket::SessionDescription& desc) const;
Steve Anton75737c02017-11-06 18:37:17802
803 // Helper methods to create media channels.
Zhi Huange830e682018-03-30 17:48:35804 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
805 cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
806 bool CreateDataChannel(const std::string& mid);
Steve Anton75737c02017-11-06 18:37:17807
Zhi Huange830e682018-03-30 17:48:35808 bool CreateSctpTransport_n(const std::string& mid);
Steve Anton75737c02017-11-06 18:37:17809 // For bundling.
Steve Anton75737c02017-11-06 18:37:17810 void DestroySctpTransport_n();
811 // SctpTransport signal handlers. Needed to marshal signals from the network
812 // to signaling thread.
813 void OnSctpTransportReadyToSendData_n();
814 // This may be called with "false" if the direction of the m= section causes
815 // us to tear down the SCTP connection.
816 void OnSctpTransportReadyToSendData_s(bool ready);
817 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
818 const rtc::CopyOnWriteBuffer& payload);
819 // Beyond just firing the signal to the signaling thread, listens to SCTP
820 // CONTROL messages on unused SIDs and processes them as OPEN messages.
821 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
822 const rtc::CopyOnWriteBuffer& payload);
Taylor Brandstettercdd05f02018-05-31 20:23:32823 void OnSctpClosingProcedureStartedRemotely_n(int sid);
824 void OnSctpClosingProcedureComplete_n(int sid);
Steve Anton75737c02017-11-06 18:37:17825
826 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
827 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
828 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 23:25:56829 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
830 cricket::ContentSource source);
Steve Anton75737c02017-11-06 18:37:17831
Steve Anton3828c062017-12-06 18:34:51832 // Check if a call to SetLocalDescription is acceptable with a session
833 // description of the given type.
834 bool ExpectSetLocalDescription(SdpType type);
835 // Check if a call to SetRemoteDescription is acceptable with a session
836 // description of the given type.
837 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 18:37:17838 // Verifies a=setup attribute as per RFC 5763.
839 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 18:34:51840 SdpType type);
Steve Anton75737c02017-11-06 18:37:17841
842 // Returns true if we are ready to push down the remote candidate.
843 // |remote_desc| is the new remote description, or NULL if the current remote
844 // description should be used. Output |valid| is true if the candidate media
845 // index is valid.
846 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
847 const SessionDescriptionInterface* remote_desc,
848 bool* valid);
849
850 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
851 // this session.
852 bool SrtpRequired() const;
853
Zhi Huange830e682018-03-30 17:48:35854 // JsepTransportController signal handlers.
Steve Anton75737c02017-11-06 18:37:17855 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
856 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
857 void OnTransportControllerCandidatesGathered(
858 const std::string& transport_name,
859 const std::vector<cricket::Candidate>& candidates);
860 void OnTransportControllerCandidatesRemoved(
861 const std::vector<cricket::Candidate>& candidates);
862 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
863
Steve Antonf8470812017-12-04 18:46:21864 const char* SessionErrorToString(SessionError error) const;
Steve Anton75737c02017-11-06 18:37:17865 std::string GetSessionErrorMsg();
866
Steve Anton8e20f172018-03-06 18:55:04867 // Report the UMA metric SdpFormatReceived for the given remote offer.
868 void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
869
Steve Anton0ffaaa22018-02-23 18:31:30870 // Report inferred negotiated SDP semantics from a local/remote answer to the
871 // UMA observer.
872 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
873
Steve Anton75737c02017-11-06 18:37:17874 // Invoked when TransportController connection completion is signaled.
875 // Reports stats for all transports in use.
876 void ReportTransportStats();
877
878 // Gather the usage of IPv4/IPv6 as best connection.
879 void ReportBestConnectionState(const cricket::TransportStats& stats);
880
Steve Antonc7b964c2018-02-01 22:39:45881 void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
882 const std::set<cricket::MediaType>& media_types);
Steve Anton75737c02017-11-06 18:37:17883
Harald Alvestrand8ebba742018-05-31 12:00:34884 void NoteUsageEvent(UsageEvent event);
885 void ReportUsagePattern() const;
886
Steve Anton75737c02017-11-06 18:37:17887 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
888
889 const std::string GetTransportName(const std::string& content_name);
890
Steve Anton6fec8802017-12-04 18:37:29891 // Destroys and clears the BaseChannel associated with the given transceiver,
892 // if such channel is set.
893 void DestroyTransceiverChannel(
894 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
895 transceiver);
896
897 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Steve Anton75737c02017-11-06 18:37:17898 void DestroyDataChannel();
899
Steve Anton6fec8802017-12-04 18:37:29900 // Destroys the given BaseChannel. The channel cannot be accessed after this
901 // method is called.
902 void DestroyBaseChannel(cricket::BaseChannel* channel);
903
Zhi Huang365381f2018-04-13 23:44:34904 // JsepTransportController::Observer override.
Taylor Brandstettercbaa2542018-04-16 23:42:14905 //
906 // Called by |transport_controller_| when processing transport information
907 // from a session description, and the mapping from m= sections to transports
908 // changed (as a result of BUNDLE negotiation, or m= sections being
909 // rejected).
910 bool OnTransportChanged(
Zhi Huang365381f2018-04-13 23:44:34911 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 23:42:14912 RtpTransportInternal* rtp_transport,
Zhi Huang365381f2018-04-13 23:44:34913 cricket::DtlsTransportInternal* dtls_transport) override;
Zhi Huange830e682018-03-30 17:48:35914
Harald Alvestrand7a1c7f72018-08-01 08:50:16915 // Returns the observer. Will crash on CHECK if the observer is removed.
916 PeerConnectionObserver* Observer() const;
917
Anton Sukhanov98a462c2018-10-17 20:15:42918 // Returns rtp transport, result can not be nullptr.
919 RtpTransportInternal* GetRtpTransport(const std::string& mid) {
920 auto rtp_transport = transport_controller_->GetRtpTransport(mid);
921 RTC_DCHECK(rtp_transport);
922 return rtp_transport;
923 }
924
925 // Returns media transport, if PeerConnection was created with configuration
926 // to use media transport. Otherwise returns nullptr.
927 MediaTransportInterface* GetMediaTransport(const std::string& mid) {
928 auto media_transport = transport_controller_->GetMediaTransport(mid);
929 RTC_DCHECK(configuration_.use_media_transport ==
Piotr (Peter) Slatala97fc11f2018-10-18 19:57:59930 (media_transport != nullptr))
931 << "configuration_.use_media_transport="
932 << configuration_.use_media_transport
933 << ", (media_transport != nullptr)=" << (media_transport != nullptr);
Anton Sukhanov98a462c2018-10-17 20:15:42934 return media_transport;
935 }
936
Steve Anton2d8609c2018-01-24 00:38:46937 sigslot::signal1<DataChannel*> SignalDataChannelCreated_;
938
henrike@webrtc.org28e20752013-07-10 00:45:36939 // Storing the factory as a scoped reference pointer ensures that the memory
940 // in the PeerConnectionFactoryImpl remains available as long as the
941 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
942 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 18:33:11943 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36944 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52945 rtc::scoped_refptr<PeerConnectionFactory> factory_;
Steve Antonba818672017-11-06 18:21:57946 PeerConnectionObserver* observer_ = nullptr;
terelius33860252017-05-13 06:37:18947
948 // The EventLog needs to outlive |call_| (and any other object that uses it).
949 std::unique_ptr<RtcEventLog> event_log_;
950
Steve Antonba818672017-11-06 18:21:57951 SignalingState signaling_state_ = kStable;
952 IceConnectionState ice_connection_state_ = kIceConnectionNew;
953 IceGatheringState ice_gathering_state_ = kIceGatheringNew;
deadbeef46c73892016-11-17 03:42:04954 PeerConnectionInterface::RTCConfiguration configuration_;
henrike@webrtc.org28e20752013-07-10 00:45:36955
Zach Steine20867f2018-08-02 20:20:15956 // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it
957 // is not injected. It should be required once chromium supplies it.
958 std::unique_ptr<AsyncResolverFactory> async_resolver_factory_;
kwibergd1fe2812016-04-27 13:47:29959 std::unique_ptr<cricket::PortAllocator> port_allocator_;
Benjamin Wrightd6f86e82018-05-08 20:12:25960 std::unique_ptr<rtc::SSLCertificateVerifier> tls_cert_verifier_;
Qingsi Wanga2d60672018-04-11 23:57:45961 int port_allocator_flags_ = 0;
deadbeefab9b2d12015-10-14 18:33:11962
zhihuang8f65cdf2016-05-07 01:40:30963 // One PeerConnection has only one RTCP CNAME.
964 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
965 std::string rtcp_cname_;
966
deadbeefab9b2d12015-10-14 18:33:11967 // Streams added via AddStream.
968 rtc::scoped_refptr<StreamCollection> local_streams_;
969 // Streams created as a result of SetRemoteDescription.
970 rtc::scoped_refptr<StreamCollection> remote_streams_;
971
kwibergd1fe2812016-04-27 13:47:29972 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_;
deadbeefeb459812015-12-16 03:24:43973
Steve Anton4171afb2017-11-20 18:20:22974 // These lists store sender info seen in local/remote descriptions.
975 std::vector<RtpSenderInfo> remote_audio_sender_infos_;
976 std::vector<RtpSenderInfo> remote_video_sender_infos_;
977 std::vector<RtpSenderInfo> local_audio_sender_infos_;
978 std::vector<RtpSenderInfo> local_video_sender_infos_;
deadbeefab9b2d12015-10-14 18:33:11979
980 SctpSidAllocator sid_allocator_;
981 // label -> DataChannel
982 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
983 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-15 02:15:29984 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 18:33:11985
deadbeefbda7e0b2015-12-09 01:13:40986 bool remote_peer_supports_msid_ = false;
deadbeef70ab1a12015-09-28 23:53:55987
terelius33860252017-05-13 06:37:18988 std::unique_ptr<Call> call_;
terelius33860252017-05-13 06:37:18989 std::unique_ptr<StatsCollector> stats_; // A pointer is passed to senders_
990 rtc::scoped_refptr<RTCStatsCollector> stats_collector_;
991
deadbeefa601f5c2016-06-06 21:27:39992 std::vector<
Steve Anton4171afb2017-11-20 18:20:22993 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
994 transceivers_;
Steve Antondcc3c022017-12-23 00:02:54995 // MIDs that have been seen either by SetLocalDescription or
996 // SetRemoteDescription over the life of the PeerConnection.
997 std::set<std::string> seen_mids_;
Steve Anton75737c02017-11-06 18:37:17998
Steve Antonf8470812017-12-04 18:46:21999 SessionError session_error_ = SessionError::kNone;
1000 std::string session_error_desc_;
Steve Anton75737c02017-11-06 18:37:171001
1002 std::string session_id_;
Steve Anton75737c02017-11-06 18:37:171003
Zhi Huange830e682018-03-30 17:48:351004 std::unique_ptr<JsepTransportController> transport_controller_;
Steve Anton75737c02017-11-06 18:37:171005 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
Steve Anton75737c02017-11-06 18:37:171006 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
1007 // when using SCTP.
1008 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
1009
1010 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
Zhi Huange830e682018-03-30 17:48:351011 // |sctp_mid_| is the content name (MID) in SDP.
Danil Chapovalov66cadcc2018-06-19 14:47:431012 absl::optional<std::string> sctp_mid_;
Steve Anton75737c02017-11-06 18:37:171013 // Value cached on signaling thread. Only updated when SctpReadyToSendData
1014 // fires on the signaling thread.
1015 bool sctp_ready_to_send_data_ = false;
1016 // Same as signals provided by SctpTransport, but these are guaranteed to
1017 // fire on the signaling thread, whereas SctpTransport fires on the networking
1018 // thread.
1019 // |sctp_invoker_| is used so that any signals queued on the signaling thread
1020 // from the network thread are immediately discarded if the SctpTransport is
1021 // destroyed (due to m= section being rejected).
1022 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
1023 // are marshalled to the right thread. Could almost use proxy.h for this,
1024 // but it doesn't have a mechanism for marshalling sigslot::signals
1025 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
1026 sigslot::signal1<bool> SignalSctpReadyToSendData;
1027 sigslot::signal2<const cricket::ReceiveDataParams&,
1028 const rtc::CopyOnWriteBuffer&>
1029 SignalSctpDataReceived;
Taylor Brandstettercdd05f02018-05-31 20:23:321030 sigslot::signal1<int> SignalSctpClosingProcedureStartedRemotely;
1031 sigslot::signal1<int> SignalSctpClosingProcedureComplete;
Steve Anton75737c02017-11-06 18:37:171032
1033 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
1034 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
1035 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
1036 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
1037 bool dtls_enabled_ = false;
1038 // Specifies which kind of data channel is allowed. This is controlled
1039 // by the chrome command-line flag and constraints:
1040 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
1041 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
1042 // not set or false, SCTP is allowed (DCT_SCTP);
1043 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
1044 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
1045 cricket::DataChannelType data_channel_type_ = cricket::DCT_NONE;
1046 // List of content names for which the remote side triggered an ICE restart.
1047 std::set<std::string> pending_ice_restarts_;
1048
1049 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
1050
1051 // Member variables for caching global options.
1052 cricket::AudioOptions audio_options_;
1053 cricket::VideoOptions video_options_;
Harald Alvestrand8ebba742018-05-31 12:00:341054
1055 int usage_event_accumulator_ = 0;
Harald Alvestrand19793842018-06-25 10:03:501056 bool return_histogram_very_quickly_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:361057};
1058
1059} // namespace webrtc
1060
Mirko Bonadei92ea95e2017-09-15 04:47:311061#endif // PC_PEERCONNECTION_H_