blob: 2942709253dea588a86c9f184731aec6345bded9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_PEERCONNECTION_H_
12#define PC_PEERCONNECTION_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
perkjd61bf802016-03-24 03:16:19 -070014#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070015#include <memory>
Steve Anton75737c02017-11-06 10:37:17 -080016#include <set>
17#include <string>
perkjd61bf802016-03-24 03:16:19 -070018#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/peerconnectioninterface.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020021#include "api/turncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "pc/iceserverparsing.h"
Zhi Huange830e682018-03-30 10:48:35 -070023#include "pc/jseptransportcontroller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "pc/peerconnectionfactory.h"
Steve Anton2d8609c2018-01-23 16:38:46 -080025#include "pc/peerconnectioninternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "pc/rtcstatscollector.h"
Steve Anton4171afb2017-11-20 10:20:22 -080027#include "pc/rtptransceiver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "pc/statscollector.h"
29#include "pc/streamcollection.h"
Steve Anton75737c02017-11-06 10:37:17 -080030#include "pc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
32namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
deadbeefeb459812015-12-15 19:24:43 -080034class MediaStreamObserver;
perkjf0dcfe22016-03-10 18:32:00 +010035class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 11:53:05 -070036class RtcEventLog;
deadbeefab9b2d12015-10-14 11:33:11 -070037
Steve Anton75737c02017-11-06 10:37:17 -080038// PeerConnection is the implementation of the PeerConnection object as defined
39// by the PeerConnectionInterface API surface.
40// The class currently is solely responsible for the following:
41// - Managing the session state machine (signaling state).
42// - Creating and initializing lower-level objects, like PortAllocator and
43// BaseChannels.
44// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
45// objects.
46// - Tracking the current and pending local/remote session descriptions.
47// The class currently is jointly responsible for the following:
48// - Parsing and interpreting SDP.
49// - Generating offers and answers based on the current state.
50// - The ICE state machine.
51// - Generating stats.
Steve Anton2d8609c2018-01-23 16:38:46 -080052class PeerConnection : public PeerConnectionInternal,
Steve Anton75737c02017-11-06 10:37:17 -080053 public DataChannelProviderInterface,
Zhi Huang365381f2018-04-13 16:44:34 -070054 public JsepTransportController::Observer,
Steve Antonad182762018-09-05 20:22:40 +000055 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 public sigslot::has_slots<> {
57 public:
Harald Alvestrand8ebba742018-05-31 14:00:34 +020058 enum class UsageEvent : int {
59 TURN_SERVER_ADDED = 0x01,
60 STUN_SERVER_ADDED = 0x02,
61 DATA_ADDED = 0x04,
62 AUDIO_ADDED = 0x08,
63 VIDEO_ADDED = 0x10,
64 SET_LOCAL_DESCRIPTION_CALLED = 0x20,
65 SET_REMOTE_DESCRIPTION_CALLED = 0x40,
66 CANDIDATE_COLLECTED = 0x80,
67 REMOTE_CANDIDATE_ADDED = 0x100,
68 ICE_STATE_CONNECTED = 0x200,
Qingsi Wang7fc821d2018-07-12 12:54:53 -070069 CLOSE_CALLED = 0x400,
Harald Alvestrand056d8112018-07-16 19:18:58 +020070 PRIVATE_CANDIDATE_COLLECTED = 0x800,
71 MAX_VALUE = 0x1000,
Harald Alvestrand8ebba742018-05-31 14:00:34 +020072 };
73
zhihuang38ede132017-06-15 12:52:32 -070074 explicit PeerConnection(PeerConnectionFactory* factory,
75 std::unique_ptr<RtcEventLog> event_log,
76 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
deadbeef653b8e02015-11-11 12:55:10 -080078 bool Initialize(
79 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab58882018-05-02 15:12:47 -070080 PeerConnectionDependencies dependencies);
deadbeef653b8e02015-11-11 12:55:10 -080081
deadbeefa67696b2015-09-29 11:56:26 -070082 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
83 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
84 bool AddStream(MediaStreamInterface* local_stream) override;
85 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
Steve Anton2d6c76a2018-01-05 17:10:52 -080087 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -080088 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -080089 const std::vector<std::string>& stream_ids) override;
deadbeefe1f9d832016-01-14 15:35:42 -080090 bool RemoveTrack(RtpSenderInterface* sender) override;
Steve Anton24db5732018-07-23 10:27:33 -070091 RTCError RemoveTrackNew(
92 rtc::scoped_refptr<RtpSenderInterface> sender) override;
deadbeefe1f9d832016-01-14 15:35:42 -080093
Steve Anton9158ef62017-11-27 13:01:52 -080094 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
95 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
96 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
97 rtc::scoped_refptr<MediaStreamTrackInterface> track,
98 const RtpTransceiverInit& init) override;
99 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
100 cricket::MediaType media_type) override;
101 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
102 cricket::MediaType media_type,
103 const RtpTransceiverInit& init) override;
104
Steve Anton8c0f7a72017-10-03 10:03:10 -0700105 // Gets the DTLS SSL certificate associated with the audio transport on the
106 // remote side. This will become populated once the DTLS connection with the
107 // peer has been completed, as indicated by the ICE connection state
108 // transitioning to kIceConnectionCompleted.
109 // Note that this will be removed once we implement RTCDtlsTransport which
110 // has standardized method for getting this information.
111 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
112 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
113
Zhi Huang70b820f2018-01-27 14:16:15 -0800114 // Version of the above method that returns the full certificate chain.
115 std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
116
deadbeeffac06552015-11-25 11:26:01 -0800117 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800118 const std::string& kind,
119 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800120
deadbeef70ab1a12015-09-28 16:53:55 -0700121 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
122 const override;
123 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
124 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800125 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
126 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700127
deadbeefa67696b2015-09-29 11:56:26 -0700128 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700130 const DataChannelInit* config) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100131 // WARNING: LEGACY. See peerconnectioninterface.h
deadbeefa67696b2015-09-29 11:56:26 -0700132 bool GetStats(StatsObserver* observer,
133 webrtc::MediaStreamTrackInterface* track,
134 StatsOutputLevel level) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100135 // Spec-complaint GetStats(). See peerconnectioninterface.h
hbos74e1a4f2016-09-15 23:33:01 -0700136 void GetStats(RTCStatsCollectorCallback* callback) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100137 void GetStats(
138 rtc::scoped_refptr<RtpSenderInterface> selector,
139 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
140 void GetStats(
141 rtc::scoped_refptr<RtpReceiverInterface> selector,
142 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100143 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
deadbeefa67696b2015-09-29 11:56:26 -0700145 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
deadbeefa67696b2015-09-29 11:56:26 -0700147 IceConnectionState ice_connection_state() override;
148 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149
deadbeefa67696b2015-09-29 11:56:26 -0700150 const SessionDescriptionInterface* local_description() const override;
151 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800152 const SessionDescriptionInterface* current_local_description() const override;
153 const SessionDescriptionInterface* current_remote_description()
154 const override;
155 const SessionDescriptionInterface* pending_local_description() const override;
156 const SessionDescriptionInterface* pending_remote_description()
157 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
159 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700160 void CreateOffer(CreateSessionDescriptionObserver* observer,
161 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800162 void CreateAnswer(CreateSessionDescriptionObserver* observer,
163 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700164 void SetLocalDescription(SetSessionDescriptionObserver* observer,
165 SessionDescriptionInterface* desc) override;
Henrik Boströma4ecf552017-11-23 14:17:07 +0000166 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
167 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 17:48:32 +0100168 void SetRemoteDescription(
169 std::unique_ptr<SessionDescriptionInterface> desc,
170 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
171 override;
deadbeef46c73892016-11-16 19:42:04 -0800172 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 11:56:26 -0700173 bool SetConfiguration(
deadbeef293e9262017-01-11 12:28:30 -0800174 const PeerConnectionInterface::RTCConfiguration& configuration,
175 RTCError* error) override;
176 bool SetConfiguration(
177 const PeerConnectionInterface::RTCConfiguration& configuration) override {
178 return SetConfiguration(configuration, nullptr);
179 }
deadbeefa67696b2015-09-29 11:56:26 -0700180 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700181 bool RemoveIceCandidates(
182 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183
Niels Möller0c4f7be2018-05-07 14:01:37 +0200184 RTCError SetBitrate(const BitrateSettings& bitrate) override;
zstein4b979802017-06-02 14:37:37 -0700185
Alex Narest78609d52017-10-20 10:37:47 +0200186 void SetBitrateAllocationStrategy(
187 std::unique_ptr<rtc::BitrateAllocationStrategy>
188 bitrate_allocation_strategy) override;
189
henrika5f6bf242017-11-01 11:06:56 +0100190 void SetAudioPlayout(bool playout) override;
191 void SetAudioRecording(bool recording) override;
192
Elad Alon99c3fe52017-10-13 16:29:40 +0200193 RTC_DEPRECATED bool StartRtcEventLog(rtc::PlatformFile file,
194 int64_t max_size_bytes) override;
Bjorn Tereliusde939432017-11-20 17:38:14 +0100195 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
196 int64_t output_period_ms) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700197 void StopRtcEventLog() override;
198
deadbeefa67696b2015-09-29 11:56:26 -0700199 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
Steve Anton2d8609c2018-01-23 16:38:46 -0800201 // PeerConnectionInternal implementation.
202 rtc::Thread* network_thread() const override {
203 return factory_->network_thread();
204 }
205 rtc::Thread* worker_thread() const override {
206 return factory_->worker_thread();
207 }
208 rtc::Thread* signaling_thread() const override {
209 return factory_->signaling_thread();
perkjd61bf802016-03-24 03:16:19 -0700210 }
deadbeefab9b2d12015-10-14 11:33:11 -0700211
Steve Antonbe5e2082018-01-24 15:29:17 -0800212 std::string session_id() const override { return session_id_; }
Steve Anton75737c02017-11-06 10:37:17 -0800213
Steve Anton2d8609c2018-01-23 16:38:46 -0800214 bool initial_offerer() const override {
Zhi Huange830e682018-03-30 10:48:35 -0700215 return transport_controller_ && transport_controller_->initial_offerer();
Steve Anton2d8609c2018-01-23 16:38:46 -0800216 }
Steve Anton75737c02017-11-06 10:37:17 -0800217
Steve Anton2d8609c2018-01-23 16:38:46 -0800218 std::vector<
219 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 10:07:54 -0800220 GetTransceiversInternal() const override {
Steve Anton2d8609c2018-01-23 16:38:46 -0800221 return transceivers_;
222 }
223
224 bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override;
225 bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id) override;
226
227 sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
228 return SignalDataChannelCreated_;
229 }
230
231 cricket::RtpDataChannel* rtp_data_channel() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800232 return rtp_data_channel_;
Steve Anton978b8762017-09-29 12:15:02 -0700233 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800234
Steve Antonbe5e2082018-01-24 15:29:17 -0800235 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels()
Steve Anton2d8609c2018-01-23 16:38:46 -0800236 const override {
237 return sctp_data_channels_;
238 }
239
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200240 absl::optional<std::string> sctp_content_name() const override {
Zhi Huange830e682018-03-30 10:48:35 -0700241 return sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -0800242 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800243
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200244 absl::optional<std::string> sctp_transport_name() const override;
Steve Anton75737c02017-11-06 10:37:17 -0800245
Qingsi Wang72a43a12018-02-20 16:03:18 -0800246 cricket::CandidateStatsList GetPooledCandidateStats() const override;
Steve Anton5dfde182018-02-06 10:34:40 -0800247 std::map<std::string, std::string> GetTransportNamesByMid() const override;
248 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
249 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800250 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 10:37:17 -0800251
Steve Anton2d8609c2018-01-23 16:38:46 -0800252 bool GetLocalCertificate(
253 const std::string& transport_name,
254 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800255 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Steve Anton2d8609c2018-01-23 16:38:46 -0800256 const std::string& transport_name) override;
257 bool IceRestartPending(const std::string& content_name) const override;
258 bool NeedsIceRestart(const std::string& content_name) const override;
259 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Steve Anton7464fca2018-01-19 11:10:37 -0800260
Harald Alvestrand19793842018-06-25 12:03:50 +0200261 void ReturnHistogramVeryQuicklyForTesting() {
262 return_histogram_very_quickly_ = true;
263 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200264 void RequestUsagePatternReportForTesting();
Harald Alvestrand19793842018-06-25 12:03:50 +0200265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700267 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268
269 private:
Henrik Boström31638672017-11-23 17:48:32 +0100270 class SetRemoteDescriptionObserverAdapter;
271 friend class SetRemoteDescriptionObserverAdapter;
272
Steve Anton4171afb2017-11-20 10:20:22 -0800273 struct RtpSenderInfo {
274 RtpSenderInfo() : first_ssrc(0) {}
Seth Hampson845e8782018-03-02 11:34:10 -0800275 RtpSenderInfo(const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800276 const std::string sender_id,
277 uint32_t ssrc)
Seth Hampson845e8782018-03-02 11:34:10 -0800278 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
Steve Anton4171afb2017-11-20 10:20:22 -0800279 bool operator==(const RtpSenderInfo& other) {
Seth Hampson845e8782018-03-02 11:34:10 -0800280 return this->stream_id == other.stream_id &&
Steve Anton4171afb2017-11-20 10:20:22 -0800281 this->sender_id == other.sender_id &&
282 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-08 17:13:40 -0800283 }
Seth Hampson845e8782018-03-02 11:34:10 -0800284 std::string stream_id;
Steve Anton4171afb2017-11-20 10:20:22 -0800285 std::string sender_id;
286 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
287 // for communicating with the lower layers.
288 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 11:33:11 -0700289 };
deadbeefab9b2d12015-10-14 11:33:11 -0700290
Steve Antonad182762018-09-05 20:22:40 +0000291 // Implements MessageHandler.
292 void OnMessage(rtc::Message* msg) override;
293
Steve Antonafb0bb72018-02-20 11:35:37 -0800294 // Plan B helpers for getting the voice/video media channels for the single
295 // audio/video transceiver, if it exists.
296 cricket::VoiceMediaChannel* voice_media_channel() const;
297 cricket::VideoMediaChannel* video_media_channel() const;
Steve Anton60776752018-01-10 11:51:34 -0800298
Steve Anton4171afb2017-11-20 10:20:22 -0800299 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
300 GetSendersInternal() const;
301 std::vector<
302 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
303 GetReceiversInternal() const;
304
305 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
306 GetAudioTransceiver() const;
307 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
308 GetVideoTransceiver() const;
309
Steve Antonafb0bb72018-02-20 11:35:37 -0800310 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
311 GetFirstAudioTransceiver() const;
312
deadbeefab9b2d12015-10-14 11:33:11 -0700313 void CreateAudioReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 10:20:22 -0800314 const RtpSenderInfo& remote_sender_info);
perkjf0dcfe22016-03-10 18:32:00 +0100315
deadbeefab9b2d12015-10-14 11:33:11 -0700316 void CreateVideoReceiver(MediaStreamInterface* stream,
Steve Anton4171afb2017-11-20 10:20:22 -0800317 const RtpSenderInfo& remote_sender_info);
Henrik Boström933d8b02017-10-10 10:05:16 -0700318 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Steve Anton4171afb2017-11-20 10:20:22 -0800319 const RtpSenderInfo& remote_sender_info);
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700320
321 // May be called either by AddStream/RemoveStream, or when a track is
322 // added/removed from a stream previously added via AddStream.
323 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream);
324 void RemoveAudioTrack(AudioTrackInterface* track,
325 MediaStreamInterface* stream);
326 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream);
327 void RemoveVideoTrack(VideoTrackInterface* track,
328 MediaStreamInterface* stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329
Steve Antonf9381f02017-12-14 10:23:57 -0800330 // AddTrack implementation when Unified Plan is specified.
331 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
332 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800333 const std::vector<std::string>& stream_ids);
Steve Antonf9381f02017-12-14 10:23:57 -0800334 // AddTrack implementation when Plan B is specified.
335 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
336 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800337 const std::vector<std::string>& stream_ids);
Steve Antonf9381f02017-12-14 10:23:57 -0800338
339 // Returns the first RtpTransceiver suitable for a newly added track, if such
340 // transceiver is available.
341 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
342 FindFirstTransceiverForAddedTrack(
343 rtc::scoped_refptr<MediaStreamTrackInterface> track);
344
Steve Antonf9381f02017-12-14 10:23:57 -0800345 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
346 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender);
347
Steve Anton22da89f2018-01-25 13:58:07 -0800348 // Internal implementation for AddTransceiver family of methods. If
349 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
Steve Anton9158ef62017-11-27 13:01:52 -0800350 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
351 cricket::MediaType media_type,
352 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 13:58:07 -0800353 const RtpTransceiverInit& init,
354 bool fire_callback = true);
Steve Anton9158ef62017-11-27 13:01:52 -0800355
Steve Anton02ee47c2018-01-10 16:26:06 -0800356 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
357 CreateSender(cricket::MediaType media_type,
Steve Anton111fdfd2018-06-25 13:03:36 -0700358 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -0800359 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800360 const std::vector<std::string>& stream_ids);
Steve Anton02ee47c2018-01-10 16:26:06 -0800361
362 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
363 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
364
Steve Antonf9381f02017-12-14 10:23:57 -0800365 // Create a new RtpTransceiver of the given type and add it to the list of
366 // transceivers.
367 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Steve Anton02ee47c2018-01-10 16:26:06 -0800368 CreateAndAddTransceiver(
369 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
370 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
371 receiver);
Steve Antonf9381f02017-12-14 10:23:57 -0800372
Steve Antonba818672017-11-06 10:21:57 -0800373 void SetIceConnectionState(IceConnectionState new_state);
374 // Called any time the IceGatheringState changes
375 void OnIceGatheringChange(IceGatheringState new_state);
376 // New ICE candidate has been gathered.
377 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate);
378 // Some local ICE candidates have been removed.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700379 void OnIceCandidatesRemoved(
Steve Antonba818672017-11-06 10:21:57 -0800380 const std::vector<cricket::Candidate>& candidates);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381
Steve Antonba818672017-11-06 10:21:57 -0800382 // Update the state, signaling if necessary.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 void ChangeSignalingState(SignalingState signaling_state);
384
deadbeefeb459812015-12-15 19:24:43 -0800385 // Signals from MediaStreamObserver.
386 void OnAudioTrackAdded(AudioTrackInterface* track,
387 MediaStreamInterface* stream);
388 void OnAudioTrackRemoved(AudioTrackInterface* track,
389 MediaStreamInterface* stream);
390 void OnVideoTrackAdded(VideoTrackInterface* track,
391 MediaStreamInterface* stream);
392 void OnVideoTrackRemoved(VideoTrackInterface* track,
393 MediaStreamInterface* stream);
394
Henrik Boström31638672017-11-23 17:48:32 +0100395 void PostSetSessionDescriptionSuccess(
396 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
Steve Antonad182762018-09-05 20:22:40 +0000398 RTCError&& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700399 void PostCreateSessionDescriptionFailure(
400 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100401 RTCError error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402
Steve Anton8a006912017-12-04 15:25:56 -0800403 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
404 // that return an RTCError instead of invoking a callback.
405 RTCError ApplyLocalDescription(
406 std::unique_ptr<SessionDescriptionInterface> desc);
407 RTCError ApplyRemoteDescription(
408 std::unique_ptr<SessionDescriptionInterface> desc);
409
Steve Antondcc3c022017-12-22 16:02:54 -0800410 // Updates the local RtpTransceivers according to the JSEP rules. Called as
411 // part of setting the local/remote description.
412 RTCError UpdateTransceiversAndDataChannels(
413 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800414 const SessionDescriptionInterface& new_session,
415 const SessionDescriptionInterface* old_local_description,
416 const SessionDescriptionInterface* old_remote_description);
Steve Antondcc3c022017-12-22 16:02:54 -0800417
418 // Either creates or destroys the transceiver's BaseChannel according to the
419 // given media section.
420 RTCError UpdateTransceiverChannel(
421 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
422 transceiver,
423 const cricket::ContentInfo& content,
424 const cricket::ContentGroup* bundle_group);
425
Steve Antonfa2260d2017-12-28 16:38:23 -0800426 // Either creates or destroys the local data channel according to the given
427 // media section.
428 RTCError UpdateDataChannel(cricket::ContentSource source,
429 const cricket::ContentInfo& content,
430 const cricket::ContentGroup* bundle_group);
431
Steve Antondcc3c022017-12-22 16:02:54 -0800432 // Associate the given transceiver according to the JSEP rules.
433 RTCErrorOr<
434 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
435 AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800436 SdpType type,
Steve Antondcc3c022017-12-22 16:02:54 -0800437 size_t mline_index,
438 const cricket::ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800439 const cricket::ContentInfo* old_local_content,
440 const cricket::ContentInfo* old_remote_content);
Steve Antondcc3c022017-12-22 16:02:54 -0800441
442 // Returns the RtpTransceiver, if found, that is associated to the given MID.
443 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
444 GetAssociatedTransceiver(const std::string& mid) const;
445
446 // Returns the RtpTransceiver, if found, that was assigned to the given mline
447 // index in CreateOffer.
448 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
449 GetTransceiverByMLineIndex(size_t mline_index) const;
450
451 // Returns an RtpTransciever, if available, that can be used to receive the
452 // given media type according to JSEP rules.
453 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
454 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
455
Steve Antoned10bd92017-12-05 10:52:59 -0800456 // Returns the media section in the given session description that is
457 // associated with the RtpTransceiver. Returns null if none found or this
458 // RtpTransceiver is not associated. Logic varies depending on the
459 // SdpSemantics specified in the configuration.
460 const cricket::ContentInfo* FindMediaSectionForTransceiver(
461 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
462 transceiver,
463 const SessionDescriptionInterface* sdesc) const;
464
Steve Anton0f5400a2018-07-17 14:25:36 -0700465 // Runs the algorithm **process the removal of a remote track** specified in
466 // the WebRTC specification.
467 // This method will update the following lists:
468 // |remove_list| is the list of transceivers for which the receiving track is
469 // being removed.
470 // |removed_streams| is the list of streams which no longer have a receiving
471 // track so should be removed.
472 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
473 void ProcessRemovalOfRemoteTrack(
474 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
475 transceiver,
476 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
477 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
478
Steve Anton52d86772018-02-20 15:48:12 -0800479 void OnNegotiationNeeded();
480
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 bool IsClosed() const {
482 return signaling_state_ == PeerConnectionInterface::kClosed;
483 }
484
deadbeefab9b2d12015-10-14 11:33:11 -0700485 // Returns a MediaSessionOptions struct with options decided by |options|,
486 // the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800487 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
488 offer_answer_options,
489 cricket::MediaSessionOptions* session_options);
490 void GetOptionsForPlanBOffer(
491 const PeerConnectionInterface::RTCOfferAnswerOptions&
492 offer_answer_options,
493 cricket::MediaSessionOptions* session_options);
494 void GetOptionsForUnifiedPlanOffer(
495 const PeerConnectionInterface::RTCOfferAnswerOptions&
496 offer_answer_options,
deadbeefab9b2d12015-10-14 11:33:11 -0700497 cricket::MediaSessionOptions* session_options);
498
Steve Anton22da89f2018-01-25 13:58:07 -0800499 RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options);
500 void RemoveRecvDirectionFromReceivingTransceiversOfType(
501 cricket::MediaType media_type);
502 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
503 std::vector<
504 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
505 GetReceivingTransceiversOfType(cricket::MediaType media_type);
506
deadbeefab9b2d12015-10-14 11:33:11 -0700507 // Returns a MediaSessionOptions struct with options decided by
508 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800509 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
zhihuang1c378ed2017-08-17 14:10:50 -0700510 cricket::MediaSessionOptions* session_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800511 void GetOptionsForPlanBAnswer(
512 const PeerConnectionInterface::RTCOfferAnswerOptions&
513 offer_answer_options,
514 cricket::MediaSessionOptions* session_options);
515 void GetOptionsForUnifiedPlanAnswer(
516 const PeerConnectionInterface::RTCOfferAnswerOptions&
517 offer_answer_options,
518 cricket::MediaSessionOptions* session_options);
htaa2a49d92016-03-04 02:51:39 -0800519
zhihuang1c378ed2017-08-17 14:10:50 -0700520 // Generates MediaDescriptionOptions for the |session_opts| based on existing
521 // local description or remote description.
522 void GenerateMediaDescriptionOptions(
523 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -0800524 RtpTransceiverDirection audio_direction,
525 RtpTransceiverDirection video_direction,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200526 absl::optional<size_t>* audio_index,
527 absl::optional<size_t>* video_index,
528 absl::optional<size_t>* data_index,
htaa2a49d92016-03-04 02:51:39 -0800529 cricket::MediaSessionOptions* session_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700530
Steve Antonfa2260d2017-12-28 16:38:23 -0800531 // Generates the active MediaDescriptionOptions for the local data channel
532 // given the specified MID.
533 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
534 const std::string& mid) const;
535
536 // Generates the rejected MediaDescriptionOptions for the local data channel
537 // given the specified MID.
538 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
539 const std::string& mid) const;
540
541 // Returns the MID for the data section associated with either the
542 // RtpDataChannel or SCTP data channel, if it has been set. If no data
543 // channels are configured this will return nullopt.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200544 absl::optional<std::string> GetDataMid() const;
Steve Antonfa2260d2017-12-28 16:38:23 -0800545
Steve Anton4171afb2017-11-20 10:20:22 -0800546 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 15:33:07 -0800547 // Called when a media type is rejected (m-line set to port 0).
Steve Anton4171afb2017-11-20 10:20:22 -0800548 void RemoveSenders(cricket::MediaType media_type);
deadbeeffaac4972015-11-12 15:33:07 -0800549
deadbeefbda7e0b2015-12-08 17:13:40 -0800550 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
551 // and existing MediaStreamTracks are removed if there is no corresponding
552 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
553 // is created if it doesn't exist; if false, it's removed if it exists.
554 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700555 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 10:20:22 -0800556 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -0700557 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800558 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700559 cricket::MediaType media_type,
560 StreamCollection* new_streams);
561
Steve Anton4171afb2017-11-20 10:20:22 -0800562 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700563 // session description. It creates a remote MediaStreamTrackInterface
564 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800565 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
566 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700567
Steve Anton4171afb2017-11-20 10:20:22 -0800568 // Triggered when a remote sender has been removed from a remote session
569 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700570 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800571 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
572 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700573
574 // Finds remote MediaStreams without any tracks and removes them from
575 // |remote_streams_| and notifies the observer that the MediaStreams no longer
576 // exist.
577 void UpdateEndedRemoteMediaStreams();
578
deadbeefab9b2d12015-10-14 11:33:11 -0700579 // Loops through the vector of |streams| and finds added and removed
580 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 10:20:22 -0800581 // For each new or removed StreamParam, OnLocalSenderSeen or
582 // OnLocalSenderRemoved is invoked.
583 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
584 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700585
Steve Anton4171afb2017-11-20 10:20:22 -0800586 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 11:33:11 -0700587 // session description.
588 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
589 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
590 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 10:20:22 -0800591 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
592 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700593
Steve Anton4171afb2017-11-20 10:20:22 -0800594 // Triggered when a local sender has been removed from a local session
deadbeefab9b2d12015-10-14 11:33:11 -0700595 // description.
596 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
597 // has been removed from the local SessionDescription and the stream can be
598 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 10:20:22 -0800599 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
600 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700601
602 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
603 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
604 void UpdateClosingRtpDataChannels(
605 const std::vector<std::string>& active_channels,
606 bool is_local_update);
607 void CreateRemoteRtpDataChannel(const std::string& label,
608 uint32_t remote_ssrc);
609
610 // Creates channel and adds it to the collection of DataChannels that will
611 // be offered in a SessionDescription.
612 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
613 const std::string& label,
614 const InternalDataChannelInit* config);
615
616 // Checks if any data channel has been added.
617 bool HasDataChannels() const;
618
619 void AllocateSctpSids(rtc::SSLRole role);
620 void OnSctpDataChannelClosed(DataChannel* channel);
621
deadbeefab9b2d12015-10-14 11:33:11 -0700622 void OnDataChannelDestroyed();
Steve Antonba818672017-11-06 10:21:57 -0800623 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700624 void OnDataChannelOpenMessage(const std::string& label,
625 const InternalDataChannelInit& config);
626
Steve Anton4171afb2017-11-20 10:20:22 -0800627 // Returns true if the PeerConnection is configured to use Unified Plan
628 // semantics for creating offers/answers and setting local/remote
629 // descriptions. If this is true the RtpTransceiver API will also be available
630 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 10:25:56 -0800631 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
632 // sufficient time has passed.
633 bool IsUnifiedPlan() const {
634 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
635 }
Steve Anton4171afb2017-11-20 10:20:22 -0800636
637 // Is there an RtpSender of the given type?
zhihuang1c378ed2017-08-17 14:10:50 -0700638 bool HasRtpSender(cricket::MediaType type) const;
deadbeeffac06552015-11-25 11:26:01 -0800639
Steve Anton4171afb2017-11-20 10:20:22 -0800640 // Return the RtpSender with the given track attached.
641 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
642 FindSenderForTrack(MediaStreamTrackInterface* track) const;
deadbeef70ab1a12015-09-28 16:53:55 -0700643
Steve Anton4171afb2017-11-20 10:20:22 -0800644 // Return the RtpSender with the given id, or null if none exists.
645 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
646 FindSenderById(const std::string& sender_id) const;
647
648 // Return the RtpReceiver with the given id, or null if none exists.
649 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
650 FindReceiverById(const std::string& receiver_id) const;
651
652 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
653 cricket::MediaType media_type);
654 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
655 cricket::MediaType media_type);
656 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
Emircan Uysalerbc609ea2018-03-27 21:57:18 +0000657 const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800658 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 11:33:11 -0700659
660 // Returns the specified SCTP DataChannel in sctp_data_channels_,
661 // or nullptr if not found.
662 DataChannel* FindDataChannelBySid(int sid) const;
663
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700664 // Called when first configuring the port allocator.
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200665 bool InitializePortAllocator_n(
666 const cricket::ServerAddresses& stun_servers,
667 const std::vector<cricket::RelayServerConfig>& turn_servers,
668 const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800669 // Called when SetConfiguration is called to apply the supported subset
670 // of the configuration on the network thread.
671 bool ReconfigurePortAllocator_n(
672 const cricket::ServerAddresses& stun_servers,
673 const std::vector<cricket::RelayServerConfig>& turn_servers,
674 IceTransportsType type,
675 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200676 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800677 webrtc::TurnCustomizer* turn_customizer,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200678 absl::optional<int> stun_candidate_keepalive_interval);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700679
Elad Alon99c3fe52017-10-13 16:29:40 +0200680 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700681 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100682 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
683 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200684
Elad Alonacb24172017-10-06 14:32:13 +0200685 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700686 // This function should only be called from the worker thread.
687 void StopRtcEventLog_w();
688
Steve Anton038834f2017-07-14 15:59:59 -0700689 // Ensures the configuration doesn't have any parameters with invalid values,
690 // or values that conflict with other parameters.
691 //
692 // Returns RTCError::OK() if there are no issues.
693 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
694
Steve Antonba818672017-11-06 10:21:57 -0800695 cricket::ChannelManager* channel_manager() const;
Steve Antonba818672017-11-06 10:21:57 -0800696
Steve Antonf8470812017-12-04 10:46:21 -0800697 enum class SessionError {
698 kNone, // No error.
699 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
700 kTransport, // Error from the underlying transport.
701 };
702
Steve Anton75737c02017-11-06 10:37:17 -0800703 // Returns the last error in the session. See the enum above for details.
Steve Antonf8470812017-12-04 10:46:21 -0800704 SessionError session_error() const { return session_error_; }
705 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 10:37:17 -0800706
Steve Anton75737c02017-11-06 10:37:17 -0800707 cricket::BaseChannel* GetChannel(const std::string& content_name);
708
709 // Get current SSL role used by SCTP's underlying transport.
710 bool GetSctpSslRole(rtc::SSLRole* role);
711
Steve Anton75737c02017-11-06 10:37:17 -0800712 cricket::IceConfig ParseIceConfig(
713 const PeerConnectionInterface::RTCConfiguration& config) const;
714
Steve Anton75737c02017-11-06 10:37:17 -0800715 // Implements DataChannelProviderInterface.
716 bool SendData(const cricket::SendDataParams& params,
717 const rtc::CopyOnWriteBuffer& payload,
718 cricket::SendDataResult* result) override;
719 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
720 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
721 void AddSctpDataStream(int sid) override;
722 void RemoveSctpDataStream(int sid) override;
723 bool ReadyToSendData() const override;
724
725 cricket::DataChannelType data_channel_type() const;
726
Steve Anton75737c02017-11-06 10:37:17 -0800727 // Called when an RTCCertificate is generated or retrieved by
728 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
729 void OnCertificateReady(
730 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
731 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
732
Steve Anton75737c02017-11-06 10:37:17 -0800733 // Non-const versions of local_description()/remote_description(), for use
734 // internally.
735 SessionDescriptionInterface* mutable_local_description() {
736 return pending_local_description_ ? pending_local_description_.get()
737 : current_local_description_.get();
738 }
739 SessionDescriptionInterface* mutable_remote_description() {
740 return pending_remote_description_ ? pending_remote_description_.get()
741 : current_remote_description_.get();
742 }
743
744 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 10:46:21 -0800745 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 10:37:17 -0800746
Zhi Huange830e682018-03-30 10:48:35 -0700747 RTCError UpdateSessionState(SdpType type,
748 cricket::ContentSource source,
749 const cricket::SessionDescription* description);
Steve Anton75737c02017-11-06 10:37:17 -0800750 // Push the media parts of the local or remote session description
751 // down to all of the channels.
Steve Anton3828c062017-12-06 10:34:51 -0800752 RTCError PushdownMediaDescription(SdpType type,
Steve Anton8a006912017-12-04 15:25:56 -0800753 cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800754 bool PushdownSctpParameters_n(cricket::ContentSource source);
755
Steve Anton8a006912017-12-04 15:25:56 -0800756 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -0800757 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800758
759 // Returns true and the TransportInfo of the given |content_name|
760 // from |description|. Returns false if it's not available.
761 static bool GetTransportDescription(
762 const cricket::SessionDescription* description,
763 const std::string& content_name,
764 cricket::TransportDescription* info);
765
Steve Anton75737c02017-11-06 10:37:17 -0800766 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 10:52:59 -0800767 // This enables media to flow on all configured audio/video channels and the
768 // RtpDataChannel.
769 void EnableSending();
Steve Anton3fe1b152017-12-12 10:20:08 -0800770
Steve Anton8af21862017-12-15 11:20:13 -0800771 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
772 void DestroyAllChannels();
Steve Anton3fe1b152017-12-12 10:20:08 -0800773
Steve Anton75737c02017-11-06 10:37:17 -0800774 // Returns the media index for a local ice candidate given the content name.
775 // Returns false if the local session description does not have a media
776 // content called |content_name|.
777 bool GetLocalCandidateMediaIndex(const std::string& content_name,
778 int* sdp_mline_index);
779 // Uses all remote candidates in |remote_desc| in this session.
780 bool UseCandidatesInSessionDescription(
781 const SessionDescriptionInterface* remote_desc);
782 // Uses |candidate| in this session.
783 bool UseCandidate(const IceCandidateInterface* candidate);
784 // Deletes the corresponding channel of contents that don't exist in |desc|.
785 // |desc| can be null. This means that all channels are deleted.
786 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
787
788 // Allocates media channels based on the |desc|. If |desc| doesn't have
789 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
790 // This method will also delete any existing media channels before creating.
Steve Antondcc3c022017-12-22 16:02:54 -0800791 RTCError CreateChannels(const cricket::SessionDescription& desc);
792
793 // If the BUNDLE policy is max-bundle, then we know for sure that all
794 // transports will be bundled from the start. This method returns the BUNDLE
795 // group if that's the case, or null if BUNDLE will be negotiated later. An
796 // error is returned if max-bundle is specified but the session description
797 // does not have a BUNDLE group.
798 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
799 const cricket::SessionDescription& desc) const;
Steve Anton75737c02017-11-06 10:37:17 -0800800
801 // Helper methods to create media channels.
Zhi Huange830e682018-03-30 10:48:35 -0700802 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
803 cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
804 bool CreateDataChannel(const std::string& mid);
Steve Anton75737c02017-11-06 10:37:17 -0800805
Zhi Huange830e682018-03-30 10:48:35 -0700806 bool CreateSctpTransport_n(const std::string& mid);
Steve Anton75737c02017-11-06 10:37:17 -0800807 // For bundling.
Steve Anton75737c02017-11-06 10:37:17 -0800808 void DestroySctpTransport_n();
809 // SctpTransport signal handlers. Needed to marshal signals from the network
810 // to signaling thread.
811 void OnSctpTransportReadyToSendData_n();
812 // This may be called with "false" if the direction of the m= section causes
813 // us to tear down the SCTP connection.
814 void OnSctpTransportReadyToSendData_s(bool ready);
815 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
816 const rtc::CopyOnWriteBuffer& payload);
817 // Beyond just firing the signal to the signaling thread, listens to SCTP
818 // CONTROL messages on unused SIDs and processes them as OPEN messages.
819 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
820 const rtc::CopyOnWriteBuffer& payload);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700821 void OnSctpClosingProcedureStartedRemotely_n(int sid);
822 void OnSctpClosingProcedureComplete_n(int sid);
Steve Anton75737c02017-11-06 10:37:17 -0800823
824 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
825 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
826 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 15:25:56 -0800827 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
828 cricket::ContentSource source);
Steve Anton75737c02017-11-06 10:37:17 -0800829
Steve Anton3828c062017-12-06 10:34:51 -0800830 // Check if a call to SetLocalDescription is acceptable with a session
831 // description of the given type.
832 bool ExpectSetLocalDescription(SdpType type);
833 // Check if a call to SetRemoteDescription is acceptable with a session
834 // description of the given type.
835 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800836 // Verifies a=setup attribute as per RFC 5763.
837 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 10:34:51 -0800838 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800839
840 // Returns true if we are ready to push down the remote candidate.
841 // |remote_desc| is the new remote description, or NULL if the current remote
842 // description should be used. Output |valid| is true if the candidate media
843 // index is valid.
844 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
845 const SessionDescriptionInterface* remote_desc,
846 bool* valid);
847
848 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
849 // this session.
850 bool SrtpRequired() const;
851
Zhi Huange830e682018-03-30 10:48:35 -0700852 // JsepTransportController signal handlers.
Steve Anton75737c02017-11-06 10:37:17 -0800853 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
854 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
855 void OnTransportControllerCandidatesGathered(
856 const std::string& transport_name,
857 const std::vector<cricket::Candidate>& candidates);
858 void OnTransportControllerCandidatesRemoved(
859 const std::vector<cricket::Candidate>& candidates);
860 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
861
Steve Antonf8470812017-12-04 10:46:21 -0800862 const char* SessionErrorToString(SessionError error) const;
Steve Anton75737c02017-11-06 10:37:17 -0800863 std::string GetSessionErrorMsg();
864
Steve Anton8e20f172018-03-06 10:55:04 -0800865 // Report the UMA metric SdpFormatReceived for the given remote offer.
866 void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
867
Steve Anton0ffaaa22018-02-23 10:31:30 -0800868 // Report inferred negotiated SDP semantics from a local/remote answer to the
869 // UMA observer.
870 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
871
Steve Anton75737c02017-11-06 10:37:17 -0800872 // Invoked when TransportController connection completion is signaled.
873 // Reports stats for all transports in use.
874 void ReportTransportStats();
875
876 // Gather the usage of IPv4/IPv6 as best connection.
877 void ReportBestConnectionState(const cricket::TransportStats& stats);
878
Steve Antonc7b964c2018-02-01 14:39:45 -0800879 void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
880 const std::set<cricket::MediaType>& media_types);
Steve Anton75737c02017-11-06 10:37:17 -0800881
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200882 void NoteUsageEvent(UsageEvent event);
883 void ReportUsagePattern() const;
884
Steve Anton75737c02017-11-06 10:37:17 -0800885 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
886
887 const std::string GetTransportName(const std::string& content_name);
888
Steve Anton6fec8802017-12-04 10:37:29 -0800889 // Destroys and clears the BaseChannel associated with the given transceiver,
890 // if such channel is set.
891 void DestroyTransceiverChannel(
892 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
893 transceiver);
894
895 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Steve Anton75737c02017-11-06 10:37:17 -0800896 void DestroyDataChannel();
897
Steve Anton6fec8802017-12-04 10:37:29 -0800898 // Destroys the given BaseChannel. The channel cannot be accessed after this
899 // method is called.
900 void DestroyBaseChannel(cricket::BaseChannel* channel);
901
Zhi Huang365381f2018-04-13 16:44:34 -0700902 // JsepTransportController::Observer override.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700903 //
904 // Called by |transport_controller_| when processing transport information
905 // from a session description, and the mapping from m= sections to transports
906 // changed (as a result of BUNDLE negotiation, or m= sections being
907 // rejected).
908 bool OnTransportChanged(
Zhi Huang365381f2018-04-13 16:44:34 -0700909 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700910 RtpTransportInternal* rtp_transport,
Zhi Huang365381f2018-04-13 16:44:34 -0700911 cricket::DtlsTransportInternal* dtls_transport) override;
Zhi Huange830e682018-03-30 10:48:35 -0700912
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200913 // Returns the observer. Will crash on CHECK if the observer is removed.
914 PeerConnectionObserver* Observer() const;
915
Steve Anton2d8609c2018-01-23 16:38:46 -0800916 sigslot::signal1<DataChannel*> SignalDataChannelCreated_;
917
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 // Storing the factory as a scoped reference pointer ensures that the memory
919 // in the PeerConnectionFactoryImpl remains available as long as the
920 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
921 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700922 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000924 rtc::scoped_refptr<PeerConnectionFactory> factory_;
Steve Antonba818672017-11-06 10:21:57 -0800925 PeerConnectionObserver* observer_ = nullptr;
terelius33860252017-05-12 23:37:18 -0700926
927 // The EventLog needs to outlive |call_| (and any other object that uses it).
928 std::unique_ptr<RtcEventLog> event_log_;
929
Steve Antonba818672017-11-06 10:21:57 -0800930 SignalingState signaling_state_ = kStable;
931 IceConnectionState ice_connection_state_ = kIceConnectionNew;
932 IceGatheringState ice_gathering_state_ = kIceGatheringNew;
deadbeef46c73892016-11-16 19:42:04 -0800933 PeerConnectionInterface::RTCConfiguration configuration_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934
Zach Steine20867f2018-08-02 13:20:15 -0700935 // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it
936 // is not injected. It should be required once chromium supplies it.
937 std::unique_ptr<AsyncResolverFactory> async_resolver_factory_;
kwibergd1fe2812016-04-27 06:47:29 -0700938 std::unique_ptr<cricket::PortAllocator> port_allocator_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700939 std::unique_ptr<rtc::SSLCertificateVerifier> tls_cert_verifier_;
Qingsi Wanga2d60672018-04-11 16:57:45 -0700940 int port_allocator_flags_ = 0;
deadbeefab9b2d12015-10-14 11:33:11 -0700941
zhihuang8f65cdf2016-05-06 18:40:30 -0700942 // One PeerConnection has only one RTCP CNAME.
943 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
944 std::string rtcp_cname_;
945
deadbeefab9b2d12015-10-14 11:33:11 -0700946 // Streams added via AddStream.
947 rtc::scoped_refptr<StreamCollection> local_streams_;
948 // Streams created as a result of SetRemoteDescription.
949 rtc::scoped_refptr<StreamCollection> remote_streams_;
950
kwibergd1fe2812016-04-27 06:47:29 -0700951 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_;
deadbeefeb459812015-12-15 19:24:43 -0800952
Steve Anton4171afb2017-11-20 10:20:22 -0800953 // These lists store sender info seen in local/remote descriptions.
954 std::vector<RtpSenderInfo> remote_audio_sender_infos_;
955 std::vector<RtpSenderInfo> remote_video_sender_infos_;
956 std::vector<RtpSenderInfo> local_audio_sender_infos_;
957 std::vector<RtpSenderInfo> local_video_sender_infos_;
deadbeefab9b2d12015-10-14 11:33:11 -0700958
959 SctpSidAllocator sid_allocator_;
960 // label -> DataChannel
961 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
962 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -0800963 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -0700964
deadbeefbda7e0b2015-12-08 17:13:40 -0800965 bool remote_peer_supports_msid_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -0700966
terelius33860252017-05-12 23:37:18 -0700967 std::unique_ptr<Call> call_;
terelius33860252017-05-12 23:37:18 -0700968 std::unique_ptr<StatsCollector> stats_; // A pointer is passed to senders_
969 rtc::scoped_refptr<RTCStatsCollector> stats_collector_;
970
deadbeefa601f5c2016-06-06 14:27:39 -0700971 std::vector<
Steve Anton4171afb2017-11-20 10:20:22 -0800972 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
973 transceivers_;
Steve Antondcc3c022017-12-22 16:02:54 -0800974 // MIDs that have been seen either by SetLocalDescription or
975 // SetRemoteDescription over the life of the PeerConnection.
976 std::set<std::string> seen_mids_;
Steve Anton75737c02017-11-06 10:37:17 -0800977
Steve Antonf8470812017-12-04 10:46:21 -0800978 SessionError session_error_ = SessionError::kNone;
979 std::string session_error_desc_;
Steve Anton75737c02017-11-06 10:37:17 -0800980
981 std::string session_id_;
Steve Anton75737c02017-11-06 10:37:17 -0800982
Zhi Huange830e682018-03-30 10:48:35 -0700983 std::unique_ptr<JsepTransportController> transport_controller_;
Steve Anton75737c02017-11-06 10:37:17 -0800984 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
Steve Anton75737c02017-11-06 10:37:17 -0800985 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
986 // when using SCTP.
987 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
988
989 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
Zhi Huange830e682018-03-30 10:48:35 -0700990 // |sctp_mid_| is the content name (MID) in SDP.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200991 absl::optional<std::string> sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -0800992 // Value cached on signaling thread. Only updated when SctpReadyToSendData
993 // fires on the signaling thread.
994 bool sctp_ready_to_send_data_ = false;
995 // Same as signals provided by SctpTransport, but these are guaranteed to
996 // fire on the signaling thread, whereas SctpTransport fires on the networking
997 // thread.
998 // |sctp_invoker_| is used so that any signals queued on the signaling thread
999 // from the network thread are immediately discarded if the SctpTransport is
1000 // destroyed (due to m= section being rejected).
1001 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
1002 // are marshalled to the right thread. Could almost use proxy.h for this,
1003 // but it doesn't have a mechanism for marshalling sigslot::signals
1004 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
1005 sigslot::signal1<bool> SignalSctpReadyToSendData;
1006 sigslot::signal2<const cricket::ReceiveDataParams&,
1007 const rtc::CopyOnWriteBuffer&>
1008 SignalSctpDataReceived;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -07001009 sigslot::signal1<int> SignalSctpClosingProcedureStartedRemotely;
1010 sigslot::signal1<int> SignalSctpClosingProcedureComplete;
Steve Anton75737c02017-11-06 10:37:17 -08001011
1012 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
1013 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
1014 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
1015 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
1016 bool dtls_enabled_ = false;
1017 // Specifies which kind of data channel is allowed. This is controlled
1018 // by the chrome command-line flag and constraints:
1019 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
1020 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
1021 // not set or false, SCTP is allowed (DCT_SCTP);
1022 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
1023 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
1024 cricket::DataChannelType data_channel_type_ = cricket::DCT_NONE;
1025 // List of content names for which the remote side triggered an ICE restart.
1026 std::set<std::string> pending_ice_restarts_;
1027
1028 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
1029
1030 // Member variables for caching global options.
1031 cricket::AudioOptions audio_options_;
1032 cricket::VideoOptions video_options_;
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001033
1034 int usage_event_accumulator_ = 0;
Harald Alvestrand19793842018-06-25 12:03:50 +02001035 bool return_histogram_very_quickly_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036};
1037
1038} // namespace webrtc
1039
Mirko Bonadei92ea95e2017-09-15 06:47:31 +02001040#endif // PC_PEERCONNECTION_H_