blob: 376dcc0c4e6485d75d56f8d6b5fc9f7a9b2e301c [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_PEER_CONNECTION_H_
12#define PC_PEER_CONNECTION_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
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070020#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/peer_connection_interface.h"
22#include "api/turn_customizer.h"
23#include "pc/ice_server_parsing.h"
24#include "pc/jsep_transport_controller.h"
25#include "pc/peer_connection_factory.h"
26#include "pc/peer_connection_internal.h"
27#include "pc/rtc_stats_collector.h"
28#include "pc/rtp_transceiver.h"
29#include "pc/stats_collector.h"
30#include "pc/stream_collection.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "pc/webrtc_session_description_factory.h"
Karl Wiberg5966c502019-02-21 23:55:09 +010032#include "rtc_base/race_checker.h"
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080033#include "rtc_base/unique_id_generator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
deadbeefeb459812015-12-15 19:24:43 -080037class MediaStreamObserver;
perkjf0dcfe22016-03-10 18:32:00 +010038class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 11:53:05 -070039class RtcEventLog;
deadbeefab9b2d12015-10-14 11:33:11 -070040
Steve Anton75737c02017-11-06 10:37:17 -080041// PeerConnection is the implementation of the PeerConnection object as defined
42// by the PeerConnectionInterface API surface.
43// The class currently is solely responsible for the following:
44// - Managing the session state machine (signaling state).
45// - Creating and initializing lower-level objects, like PortAllocator and
46// BaseChannels.
47// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
48// objects.
49// - Tracking the current and pending local/remote session descriptions.
50// The class currently is jointly responsible for the following:
51// - Parsing and interpreting SDP.
52// - Generating offers and answers based on the current state.
53// - The ICE state machine.
54// - Generating stats.
Steve Anton2d8609c2018-01-23 16:38:46 -080055class PeerConnection : public PeerConnectionInternal,
Steve Anton75737c02017-11-06 10:37:17 -080056 public DataChannelProviderInterface,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080057 public DataChannelSink,
Zhi Huang365381f2018-04-13 16:44:34 -070058 public JsepTransportController::Observer,
Steve Antonad182762018-09-05 20:22:40 +000059 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 public sigslot::has_slots<> {
61 public:
Harald Alvestrand8ebba742018-05-31 14:00:34 +020062 enum class UsageEvent : int {
63 TURN_SERVER_ADDED = 0x01,
64 STUN_SERVER_ADDED = 0x02,
65 DATA_ADDED = 0x04,
66 AUDIO_ADDED = 0x08,
67 VIDEO_ADDED = 0x10,
68 SET_LOCAL_DESCRIPTION_CALLED = 0x20,
69 SET_REMOTE_DESCRIPTION_CALLED = 0x40,
70 CANDIDATE_COLLECTED = 0x80,
71 REMOTE_CANDIDATE_ADDED = 0x100,
72 ICE_STATE_CONNECTED = 0x200,
Qingsi Wang7fc821d2018-07-12 12:54:53 -070073 CLOSE_CALLED = 0x400,
Harald Alvestrand056d8112018-07-16 19:18:58 +020074 PRIVATE_CANDIDATE_COLLECTED = 0x800,
75 MAX_VALUE = 0x1000,
Harald Alvestrand8ebba742018-05-31 14:00:34 +020076 };
77
zhihuang38ede132017-06-15 12:52:32 -070078 explicit PeerConnection(PeerConnectionFactory* factory,
79 std::unique_ptr<RtcEventLog> event_log,
80 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
deadbeef653b8e02015-11-11 12:55:10 -080082 bool Initialize(
83 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab58882018-05-02 15:12:47 -070084 PeerConnectionDependencies dependencies);
deadbeef653b8e02015-11-11 12:55:10 -080085
deadbeefa67696b2015-09-29 11:56:26 -070086 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
87 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
88 bool AddStream(MediaStreamInterface* local_stream) override;
89 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
Steve Anton2d6c76a2018-01-05 17:10:52 -080091 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -080092 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -080093 const std::vector<std::string>& stream_ids) override;
deadbeefe1f9d832016-01-14 15:35:42 -080094 bool RemoveTrack(RtpSenderInterface* sender) override;
Steve Anton24db5732018-07-23 10:27:33 -070095 RTCError RemoveTrackNew(
96 rtc::scoped_refptr<RtpSenderInterface> sender) override;
deadbeefe1f9d832016-01-14 15:35:42 -080097
Steve Anton9158ef62017-11-27 13:01:52 -080098 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
99 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
100 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
101 rtc::scoped_refptr<MediaStreamTrackInterface> track,
102 const RtpTransceiverInit& init) override;
103 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
104 cricket::MediaType media_type) override;
105 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
106 cricket::MediaType media_type,
107 const RtpTransceiverInit& init) override;
108
Steve Anton8c0f7a72017-10-03 10:03:10 -0700109 // Gets the DTLS SSL certificate associated with the audio transport on the
110 // remote side. This will become populated once the DTLS connection with the
111 // peer has been completed, as indicated by the ICE connection state
112 // transitioning to kIceConnectionCompleted.
113 // Note that this will be removed once we implement RTCDtlsTransport which
114 // has standardized method for getting this information.
115 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
116 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
117
Zhi Huang70b820f2018-01-27 14:16:15 -0800118 // Version of the above method that returns the full certificate chain.
119 std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
120
deadbeeffac06552015-11-25 11:26:01 -0800121 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800122 const std::string& kind,
123 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800124
deadbeef70ab1a12015-09-28 16:53:55 -0700125 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
126 const override;
127 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
128 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800129 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
130 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700131
deadbeefa67696b2015-09-29 11:56:26 -0700132 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700134 const DataChannelInit* config) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100135 // WARNING: LEGACY. See peerconnectioninterface.h
deadbeefa67696b2015-09-29 11:56:26 -0700136 bool GetStats(StatsObserver* observer,
137 webrtc::MediaStreamTrackInterface* track,
138 StatsOutputLevel level) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100139 // Spec-complaint GetStats(). See peerconnectioninterface.h
hbos74e1a4f2016-09-15 23:33:01 -0700140 void GetStats(RTCStatsCollectorCallback* callback) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100141 void GetStats(
142 rtc::scoped_refptr<RtpSenderInterface> selector,
143 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
144 void GetStats(
145 rtc::scoped_refptr<RtpReceiverInterface> selector,
146 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100147 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
deadbeefa67696b2015-09-29 11:56:26 -0700149 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150
deadbeefa67696b2015-09-29 11:56:26 -0700151 IceConnectionState ice_connection_state() override;
Jonas Olsson12046902018-12-06 11:25:14 +0100152 IceConnectionState standardized_ice_connection_state() override;
Jonas Olsson635474e2018-10-18 15:58:17 +0200153 PeerConnectionState peer_connection_state() override;
deadbeefa67696b2015-09-29 11:56:26 -0700154 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
deadbeefa67696b2015-09-29 11:56:26 -0700156 const SessionDescriptionInterface* local_description() const override;
157 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800158 const SessionDescriptionInterface* current_local_description() const override;
159 const SessionDescriptionInterface* current_remote_description()
160 const override;
161 const SessionDescriptionInterface* pending_local_description() const override;
162 const SessionDescriptionInterface* pending_remote_description()
163 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164
165 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700166 void CreateOffer(CreateSessionDescriptionObserver* observer,
167 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800168 void CreateAnswer(CreateSessionDescriptionObserver* observer,
169 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700170 void SetLocalDescription(SetSessionDescriptionObserver* observer,
171 SessionDescriptionInterface* desc) override;
Henrik Boströma4ecf552017-11-23 14:17:07 +0000172 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
173 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 17:48:32 +0100174 void SetRemoteDescription(
175 std::unique_ptr<SessionDescriptionInterface> desc,
176 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
177 override;
deadbeef46c73892016-11-16 19:42:04 -0800178 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 11:56:26 -0700179 bool SetConfiguration(
deadbeef293e9262017-01-11 12:28:30 -0800180 const PeerConnectionInterface::RTCConfiguration& configuration,
181 RTCError* error) override;
182 bool SetConfiguration(
183 const PeerConnectionInterface::RTCConfiguration& configuration) override {
184 return SetConfiguration(configuration, nullptr);
185 }
deadbeefa67696b2015-09-29 11:56:26 -0700186 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700187 bool RemoveIceCandidates(
188 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
Niels Möller0c4f7be2018-05-07 14:01:37 +0200190 RTCError SetBitrate(const BitrateSettings& bitrate) override;
zstein4b979802017-06-02 14:37:37 -0700191
Alex Narest78609d52017-10-20 10:37:47 +0200192 void SetBitrateAllocationStrategy(
193 std::unique_ptr<rtc::BitrateAllocationStrategy>
194 bitrate_allocation_strategy) override;
195
henrika5f6bf242017-11-01 11:06:56 +0100196 void SetAudioPlayout(bool playout) override;
197 void SetAudioRecording(bool recording) override;
198
Harald Alvestrandad88c882018-11-28 16:47:46 +0100199 rtc::scoped_refptr<DtlsTransportInterface> LookupDtlsTransportByMid(
200 const std::string& mid) override;
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100201 rtc::scoped_refptr<DtlsTransport> LookupDtlsTransportByMidInternal(
202 const std::string& mid);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100203
Elad Alon99c3fe52017-10-13 16:29:40 +0200204 RTC_DEPRECATED bool StartRtcEventLog(rtc::PlatformFile file,
205 int64_t max_size_bytes) override;
Bjorn Tereliusde939432017-11-20 17:38:14 +0100206 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
207 int64_t output_period_ms) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700208 void StopRtcEventLog() override;
209
deadbeefa67696b2015-09-29 11:56:26 -0700210 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211
Steve Anton2d8609c2018-01-23 16:38:46 -0800212 // PeerConnectionInternal implementation.
Karl Wiberg4ae63472019-02-22 00:57:06 +0100213 rtc::Thread* network_thread() const final {
Steve Anton2d8609c2018-01-23 16:38:46 -0800214 return factory_->network_thread();
215 }
Karl Wiberg4ae63472019-02-22 00:57:06 +0100216 rtc::Thread* worker_thread() const final { return factory_->worker_thread(); }
217 rtc::Thread* signaling_thread() const final {
Steve Anton2d8609c2018-01-23 16:38:46 -0800218 return factory_->signaling_thread();
perkjd61bf802016-03-24 03:16:19 -0700219 }
deadbeefab9b2d12015-10-14 11:33:11 -0700220
Steve Antonbe5e2082018-01-24 15:29:17 -0800221 std::string session_id() const override { return session_id_; }
Steve Anton75737c02017-11-06 10:37:17 -0800222
Steve Anton2d8609c2018-01-23 16:38:46 -0800223 bool initial_offerer() const override {
Zhi Huange830e682018-03-30 10:48:35 -0700224 return transport_controller_ && transport_controller_->initial_offerer();
Steve Anton2d8609c2018-01-23 16:38:46 -0800225 }
Steve Anton75737c02017-11-06 10:37:17 -0800226
Steve Anton2d8609c2018-01-23 16:38:46 -0800227 std::vector<
228 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 10:07:54 -0800229 GetTransceiversInternal() const override {
Steve Anton2d8609c2018-01-23 16:38:46 -0800230 return transceivers_;
231 }
232
Steve Antona41959e2018-11-28 11:15:33 -0800233 absl::string_view GetLocalTrackIdBySsrc(uint32_t ssrc) override;
234 absl::string_view GetRemoteTrackIdBySsrc(uint32_t ssrc) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800235
236 sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
237 return SignalDataChannelCreated_;
238 }
239
240 cricket::RtpDataChannel* rtp_data_channel() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800241 return rtp_data_channel_;
Steve Anton978b8762017-09-29 12:15:02 -0700242 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800243
Steve Antonbe5e2082018-01-24 15:29:17 -0800244 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels()
Steve Anton2d8609c2018-01-23 16:38:46 -0800245 const override {
246 return sctp_data_channels_;
247 }
248
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200249 absl::optional<std::string> sctp_content_name() const override {
Zhi Huange830e682018-03-30 10:48:35 -0700250 return sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -0800251 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800252
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200253 absl::optional<std::string> sctp_transport_name() const override;
Steve Anton75737c02017-11-06 10:37:17 -0800254
Qingsi Wang72a43a12018-02-20 16:03:18 -0800255 cricket::CandidateStatsList GetPooledCandidateStats() const override;
Steve Anton5dfde182018-02-06 10:34:40 -0800256 std::map<std::string, std::string> GetTransportNamesByMid() const override;
257 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
258 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800259 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 10:37:17 -0800260
Steve Anton2d8609c2018-01-23 16:38:46 -0800261 bool GetLocalCertificate(
262 const std::string& transport_name,
263 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800264 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Steve Anton2d8609c2018-01-23 16:38:46 -0800265 const std::string& transport_name) override;
266 bool IceRestartPending(const std::string& content_name) const override;
267 bool NeedsIceRestart(const std::string& content_name) const override;
268 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Steve Anton7464fca2018-01-19 11:10:37 -0800269
Harald Alvestrand19793842018-06-25 12:03:50 +0200270 void ReturnHistogramVeryQuicklyForTesting() {
271 return_histogram_very_quickly_ = true;
272 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200273 void RequestUsagePatternReportForTesting();
Harald Alvestrand19793842018-06-25 12:03:50 +0200274
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700276 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277
278 private:
Henrik Boström31638672017-11-23 17:48:32 +0100279 class SetRemoteDescriptionObserverAdapter;
280 friend class SetRemoteDescriptionObserverAdapter;
281
Steve Anton4171afb2017-11-20 10:20:22 -0800282 struct RtpSenderInfo {
283 RtpSenderInfo() : first_ssrc(0) {}
Seth Hampson845e8782018-03-02 11:34:10 -0800284 RtpSenderInfo(const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800285 const std::string sender_id,
286 uint32_t ssrc)
Seth Hampson845e8782018-03-02 11:34:10 -0800287 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
Steve Anton4171afb2017-11-20 10:20:22 -0800288 bool operator==(const RtpSenderInfo& other) {
Seth Hampson845e8782018-03-02 11:34:10 -0800289 return this->stream_id == other.stream_id &&
Steve Anton4171afb2017-11-20 10:20:22 -0800290 this->sender_id == other.sender_id &&
291 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-08 17:13:40 -0800292 }
Seth Hampson845e8782018-03-02 11:34:10 -0800293 std::string stream_id;
Steve Anton4171afb2017-11-20 10:20:22 -0800294 std::string sender_id;
295 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
296 // for communicating with the lower layers.
297 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 11:33:11 -0700298 };
deadbeefab9b2d12015-10-14 11:33:11 -0700299
Steve Antonad182762018-09-05 20:22:40 +0000300 // Implements MessageHandler.
301 void OnMessage(rtc::Message* msg) override;
302
Steve Antonafb0bb72018-02-20 11:35:37 -0800303 // Plan B helpers for getting the voice/video media channels for the single
304 // audio/video transceiver, if it exists.
Karl Wiberg5966c502019-02-21 23:55:09 +0100305 cricket::VoiceMediaChannel* voice_media_channel() const
306 RTC_RUN_ON(signaling_thread());
307 cricket::VideoMediaChannel* video_media_channel() const
308 RTC_RUN_ON(signaling_thread());
Steve Anton60776752018-01-10 11:51:34 -0800309
Steve Anton4171afb2017-11-20 10:20:22 -0800310 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
311 GetSendersInternal() const;
312 std::vector<
313 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
314 GetReceiversInternal() const;
315
316 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100317 GetAudioTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800318 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100319 GetVideoTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800320
Steve Antonafb0bb72018-02-20 11:35:37 -0800321 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
322 GetFirstAudioTransceiver() const;
323
deadbeefab9b2d12015-10-14 11:33:11 -0700324 void CreateAudioReceiver(MediaStreamInterface* stream,
Karl Wiberg744310f2019-02-14 10:18:56 +0100325 const RtpSenderInfo& remote_sender_info)
326 RTC_RUN_ON(signaling_thread());
perkjf0dcfe22016-03-10 18:32:00 +0100327
deadbeefab9b2d12015-10-14 11:33:11 -0700328 void CreateVideoReceiver(MediaStreamInterface* stream,
Karl Wiberg744310f2019-02-14 10:18:56 +0100329 const RtpSenderInfo& remote_sender_info)
330 RTC_RUN_ON(signaling_thread());
Henrik Boström933d8b02017-10-10 10:05:16 -0700331 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Karl Wiberg5966c502019-02-21 23:55:09 +0100332 const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700333
334 // May be called either by AddStream/RemoveStream, or when a track is
335 // added/removed from a stream previously added via AddStream.
Karl Wiberg5966c502019-02-21 23:55:09 +0100336 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream)
337 RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700338 void RemoveAudioTrack(AudioTrackInterface* track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100339 MediaStreamInterface* stream)
340 RTC_RUN_ON(signaling_thread());
341 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream)
342 RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700343 void RemoveVideoTrack(VideoTrackInterface* track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100344 MediaStreamInterface* stream)
345 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346
Steve Antonf9381f02017-12-14 10:23:57 -0800347 // AddTrack implementation when Unified Plan is specified.
348 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
349 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800350 const std::vector<std::string>& stream_ids);
Steve Antonf9381f02017-12-14 10:23:57 -0800351 // AddTrack implementation when Plan B is specified.
352 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
353 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100354 const std::vector<std::string>& stream_ids)
355 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800356
357 // Returns the first RtpTransceiver suitable for a newly added track, if such
358 // transceiver is available.
359 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
360 FindFirstTransceiverForAddedTrack(
361 rtc::scoped_refptr<MediaStreamTrackInterface> track);
362
Steve Antonf9381f02017-12-14 10:23:57 -0800363 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
364 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender);
365
Steve Anton22da89f2018-01-25 13:58:07 -0800366 // Internal implementation for AddTransceiver family of methods. If
367 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
Steve Anton9158ef62017-11-27 13:01:52 -0800368 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
369 cricket::MediaType media_type,
370 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 13:58:07 -0800371 const RtpTransceiverInit& init,
Karl Wiberg744310f2019-02-14 10:18:56 +0100372 bool fire_callback = true) RTC_RUN_ON(signaling_thread());
Steve Anton9158ef62017-11-27 13:01:52 -0800373
Steve Anton02ee47c2018-01-10 16:26:06 -0800374 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
375 CreateSender(cricket::MediaType media_type,
Steve Anton111fdfd2018-06-25 13:03:36 -0700376 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -0800377 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Florent Castelli892acf02018-10-01 22:47:20 +0200378 const std::vector<std::string>& stream_ids,
379 const std::vector<RtpEncodingParameters>& send_encodings);
Steve Anton02ee47c2018-01-10 16:26:06 -0800380
381 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
382 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
383
Steve Antonf9381f02017-12-14 10:23:57 -0800384 // Create a new RtpTransceiver of the given type and add it to the list of
385 // transceivers.
386 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Steve Anton02ee47c2018-01-10 16:26:06 -0800387 CreateAndAddTransceiver(
388 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
389 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
390 receiver);
Steve Antonf9381f02017-12-14 10:23:57 -0800391
Karl Wiberg744310f2019-02-14 10:18:56 +0100392 void SetIceConnectionState(IceConnectionState new_state)
393 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200394 void SetStandardizedIceConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100395 PeerConnectionInterface::IceConnectionState new_state)
396 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200397 void SetConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100398 PeerConnectionInterface::PeerConnectionState new_state)
399 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200400
Steve Antonba818672017-11-06 10:21:57 -0800401 // Called any time the IceGatheringState changes
Karl Wiberg744310f2019-02-14 10:18:56 +0100402 void OnIceGatheringChange(IceGatheringState new_state)
403 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800404 // New ICE candidate has been gathered.
Karl Wiberg744310f2019-02-14 10:18:56 +0100405 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)
406 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800407 // Some local ICE candidates have been removed.
Karl Wiberg744310f2019-02-14 10:18:56 +0100408 void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates)
409 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410
Steve Antonba818672017-11-06 10:21:57 -0800411 // Update the state, signaling if necessary.
Karl Wiberg744310f2019-02-14 10:18:56 +0100412 void ChangeSignalingState(SignalingState signaling_state)
413 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414
deadbeefeb459812015-12-15 19:24:43 -0800415 // Signals from MediaStreamObserver.
416 void OnAudioTrackAdded(AudioTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100417 MediaStreamInterface* stream)
418 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800419 void OnAudioTrackRemoved(AudioTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100420 MediaStreamInterface* stream)
421 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800422 void OnVideoTrackAdded(VideoTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100423 MediaStreamInterface* stream)
424 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800425 void OnVideoTrackRemoved(VideoTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100426 MediaStreamInterface* stream)
427 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800428
Henrik Boström31638672017-11-23 17:48:32 +0100429 void PostSetSessionDescriptionSuccess(
430 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
Steve Antonad182762018-09-05 20:22:40 +0000432 RTCError&& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700433 void PostCreateSessionDescriptionFailure(
434 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100435 RTCError error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436
Steve Anton8a006912017-12-04 15:25:56 -0800437 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
438 // that return an RTCError instead of invoking a callback.
439 RTCError ApplyLocalDescription(
440 std::unique_ptr<SessionDescriptionInterface> desc);
441 RTCError ApplyRemoteDescription(
442 std::unique_ptr<SessionDescriptionInterface> desc);
443
Steve Antondcc3c022017-12-22 16:02:54 -0800444 // Updates the local RtpTransceivers according to the JSEP rules. Called as
445 // part of setting the local/remote description.
446 RTCError UpdateTransceiversAndDataChannels(
447 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800448 const SessionDescriptionInterface& new_session,
449 const SessionDescriptionInterface* old_local_description,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100450 const SessionDescriptionInterface* old_remote_description)
451 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800452
453 // Either creates or destroys the transceiver's BaseChannel according to the
454 // given media section.
455 RTCError UpdateTransceiverChannel(
456 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
457 transceiver,
458 const cricket::ContentInfo& content,
Karl Wiberg5966c502019-02-21 23:55:09 +0100459 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800460
Steve Antonfa2260d2017-12-28 16:38:23 -0800461 // Either creates or destroys the local data channel according to the given
462 // media section.
463 RTCError UpdateDataChannel(cricket::ContentSource source,
464 const cricket::ContentInfo& content,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100465 const cricket::ContentGroup* bundle_group)
466 RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800467
Steve Antondcc3c022017-12-22 16:02:54 -0800468 // Associate the given transceiver according to the JSEP rules.
469 RTCErrorOr<
470 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
471 AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800472 SdpType type,
Steve Antondcc3c022017-12-22 16:02:54 -0800473 size_t mline_index,
474 const cricket::ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800475 const cricket::ContentInfo* old_local_content,
Karl Wiberg5966c502019-02-21 23:55:09 +0100476 const cricket::ContentInfo* old_remote_content)
477 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800478
479 // Returns the RtpTransceiver, if found, that is associated to the given MID.
480 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100481 GetAssociatedTransceiver(const std::string& mid) const
482 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800483
484 // Returns the RtpTransceiver, if found, that was assigned to the given mline
485 // index in CreateOffer.
486 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100487 GetTransceiverByMLineIndex(size_t mline_index) const
488 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800489
490 // Returns an RtpTransciever, if available, that can be used to receive the
491 // given media type according to JSEP rules.
492 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100493 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const
494 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800495
Steve Antoned10bd92017-12-05 10:52:59 -0800496 // Returns the media section in the given session description that is
497 // associated with the RtpTransceiver. Returns null if none found or this
498 // RtpTransceiver is not associated. Logic varies depending on the
499 // SdpSemantics specified in the configuration.
500 const cricket::ContentInfo* FindMediaSectionForTransceiver(
501 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
502 transceiver,
Karl Wiberg5966c502019-02-21 23:55:09 +0100503 const SessionDescriptionInterface* sdesc) const
504 RTC_RUN_ON(signaling_thread());
Steve Antoned10bd92017-12-05 10:52:59 -0800505
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100506 // Runs the algorithm **set the associated remote streams** specified in
507 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
508 void SetAssociatedRemoteStreams(
509 rtc::scoped_refptr<RtpReceiverInternal> receiver,
510 const std::vector<std::string>& stream_ids,
511 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
512 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
513
Steve Anton0f5400a2018-07-17 14:25:36 -0700514 // Runs the algorithm **process the removal of a remote track** specified in
515 // the WebRTC specification.
516 // This method will update the following lists:
517 // |remove_list| is the list of transceivers for which the receiving track is
518 // being removed.
519 // |removed_streams| is the list of streams which no longer have a receiving
520 // track so should be removed.
521 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
522 void ProcessRemovalOfRemoteTrack(
523 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
524 transceiver,
525 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
526 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
527
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100528 void RemoveRemoteStreamsIfEmpty(
529 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
530 remote_streams,
531 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
532
Steve Anton52d86772018-02-20 15:48:12 -0800533 void OnNegotiationNeeded();
534
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 bool IsClosed() const {
Karl Wiberg8d2e2282019-02-17 13:00:07 +0100536 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 return signaling_state_ == PeerConnectionInterface::kClosed;
538 }
539
deadbeefab9b2d12015-10-14 11:33:11 -0700540 // Returns a MediaSessionOptions struct with options decided by |options|,
541 // the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800542 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
543 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100544 cricket::MediaSessionOptions* session_options)
545 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800546 void GetOptionsForPlanBOffer(
547 const PeerConnectionInterface::RTCOfferAnswerOptions&
548 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100549 cricket::MediaSessionOptions* session_options)
550 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800551 void GetOptionsForUnifiedPlanOffer(
552 const PeerConnectionInterface::RTCOfferAnswerOptions&
553 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100554 cricket::MediaSessionOptions* session_options)
555 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700556
Karl Wiberg5966c502019-02-21 23:55:09 +0100557 RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options)
558 RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800559 void RemoveRecvDirectionFromReceivingTransceiversOfType(
560 cricket::MediaType media_type);
561 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
562 std::vector<
563 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
564 GetReceivingTransceiversOfType(cricket::MediaType media_type);
565
deadbeefab9b2d12015-10-14 11:33:11 -0700566 // Returns a MediaSessionOptions struct with options decided by
567 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800568 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100569 cricket::MediaSessionOptions* session_options)
570 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800571 void GetOptionsForPlanBAnswer(
572 const PeerConnectionInterface::RTCOfferAnswerOptions&
573 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100574 cricket::MediaSessionOptions* session_options)
575 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800576 void GetOptionsForUnifiedPlanAnswer(
577 const PeerConnectionInterface::RTCOfferAnswerOptions&
578 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100579 cricket::MediaSessionOptions* session_options)
580 RTC_RUN_ON(signaling_thread());
htaa2a49d92016-03-04 02:51:39 -0800581
zhihuang1c378ed2017-08-17 14:10:50 -0700582 // Generates MediaDescriptionOptions for the |session_opts| based on existing
583 // local description or remote description.
584 void GenerateMediaDescriptionOptions(
585 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -0800586 RtpTransceiverDirection audio_direction,
587 RtpTransceiverDirection video_direction,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200588 absl::optional<size_t>* audio_index,
589 absl::optional<size_t>* video_index,
590 absl::optional<size_t>* data_index,
htaa2a49d92016-03-04 02:51:39 -0800591 cricket::MediaSessionOptions* session_options);
deadbeefab9b2d12015-10-14 11:33:11 -0700592
Steve Antonfa2260d2017-12-28 16:38:23 -0800593 // Generates the active MediaDescriptionOptions for the local data channel
594 // given the specified MID.
595 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
596 const std::string& mid) const;
597
598 // Generates the rejected MediaDescriptionOptions for the local data channel
599 // given the specified MID.
600 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
601 const std::string& mid) const;
602
603 // Returns the MID for the data section associated with either the
604 // RtpDataChannel or SCTP data channel, if it has been set. If no data
605 // channels are configured this will return nullopt.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200606 absl::optional<std::string> GetDataMid() const;
Steve Antonfa2260d2017-12-28 16:38:23 -0800607
Steve Anton4171afb2017-11-20 10:20:22 -0800608 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 15:33:07 -0800609 // Called when a media type is rejected (m-line set to port 0).
Karl Wiberg744310f2019-02-14 10:18:56 +0100610 void RemoveSenders(cricket::MediaType media_type)
611 RTC_RUN_ON(signaling_thread());
deadbeeffaac4972015-11-12 15:33:07 -0800612
deadbeefbda7e0b2015-12-08 17:13:40 -0800613 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
614 // and existing MediaStreamTracks are removed if there is no corresponding
615 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
616 // is created if it doesn't exist; if false, it's removed if it exists.
617 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700618 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 10:20:22 -0800619 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -0700620 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800621 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700622 cricket::MediaType media_type,
Karl Wiberg744310f2019-02-14 10:18:56 +0100623 StreamCollection* new_streams) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700624
Steve Anton4171afb2017-11-20 10:20:22 -0800625 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700626 // session description. It creates a remote MediaStreamTrackInterface
627 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800628 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
Karl Wiberg744310f2019-02-14 10:18:56 +0100629 cricket::MediaType media_type)
630 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700631
Steve Anton4171afb2017-11-20 10:20:22 -0800632 // Triggered when a remote sender has been removed from a remote session
633 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700634 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800635 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
Karl Wiberg744310f2019-02-14 10:18:56 +0100636 cricket::MediaType media_type)
637 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700638
639 // Finds remote MediaStreams without any tracks and removes them from
640 // |remote_streams_| and notifies the observer that the MediaStreams no longer
641 // exist.
Karl Wiberg744310f2019-02-14 10:18:56 +0100642 void UpdateEndedRemoteMediaStreams() RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700643
deadbeefab9b2d12015-10-14 11:33:11 -0700644 // Loops through the vector of |streams| and finds added and removed
645 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 10:20:22 -0800646 // For each new or removed StreamParam, OnLocalSenderSeen or
647 // OnLocalSenderRemoved is invoked.
648 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
Karl Wiberg5966c502019-02-21 23:55:09 +0100649 cricket::MediaType media_type)
650 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700651
Steve Anton4171afb2017-11-20 10:20:22 -0800652 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 11:33:11 -0700653 // session description.
654 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
655 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
656 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 10:20:22 -0800657 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
Karl Wiberg5966c502019-02-21 23:55:09 +0100658 cricket::MediaType media_type)
659 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700660
Steve Anton4171afb2017-11-20 10:20:22 -0800661 // Triggered when a local sender has been removed from a local session
deadbeefab9b2d12015-10-14 11:33:11 -0700662 // description.
663 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
664 // has been removed from the local SessionDescription and the stream can be
665 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 10:20:22 -0800666 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
667 cricket::MediaType media_type);
deadbeefab9b2d12015-10-14 11:33:11 -0700668
669 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
Karl Wiberg106d92d2019-02-14 10:17:47 +0100670 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams)
671 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700672 void UpdateClosingRtpDataChannels(
673 const std::vector<std::string>& active_channels,
674 bool is_local_update);
675 void CreateRemoteRtpDataChannel(const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100676 uint32_t remote_ssrc)
677 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700678
679 // Creates channel and adds it to the collection of DataChannels that will
680 // be offered in a SessionDescription.
681 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
682 const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100683 const InternalDataChannelInit* config) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700684
685 // Checks if any data channel has been added.
686 bool HasDataChannels() const;
687
688 void AllocateSctpSids(rtc::SSLRole role);
689 void OnSctpDataChannelClosed(DataChannel* channel);
690
deadbeefab9b2d12015-10-14 11:33:11 -0700691 void OnDataChannelDestroyed();
Steve Antonba818672017-11-06 10:21:57 -0800692 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700693 void OnDataChannelOpenMessage(const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100694 const InternalDataChannelInit& config)
695 RTC_RUN_ON(signaling_thread());
696
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800697 // Parses and handles open messages. Returns true if the message is an open
698 // message, false otherwise.
699 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
700 const rtc::CopyOnWriteBuffer& buffer)
701 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700702
Steve Anton4171afb2017-11-20 10:20:22 -0800703 // Returns true if the PeerConnection is configured to use Unified Plan
704 // semantics for creating offers/answers and setting local/remote
705 // descriptions. If this is true the RtpTransceiver API will also be available
706 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 10:25:56 -0800707 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
708 // sufficient time has passed.
Karl Wiberg5966c502019-02-21 23:55:09 +0100709 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread()) {
Steve Anton79e79602017-11-20 10:25:56 -0800710 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
711 }
Steve Anton4171afb2017-11-20 10:20:22 -0800712
Steve Anton06817cd2018-12-18 15:55:30 -0800713 // The offer/answer machinery assumes the media section MID is present and
714 // unique. To support legacy end points that do not supply a=mid lines, this
715 // method will modify the session description to add MIDs generated according
716 // to the SDP semantics.
Karl Wiberg5966c502019-02-21 23:55:09 +0100717 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description)
718 RTC_RUN_ON(signaling_thread());
Steve Anton06817cd2018-12-18 15:55:30 -0800719
Steve Anton4171afb2017-11-20 10:20:22 -0800720 // Is there an RtpSender of the given type?
Karl Wiberg5966c502019-02-21 23:55:09 +0100721 bool HasRtpSender(cricket::MediaType type) const
722 RTC_RUN_ON(signaling_thread());
deadbeeffac06552015-11-25 11:26:01 -0800723
Steve Anton4171afb2017-11-20 10:20:22 -0800724 // Return the RtpSender with the given track attached.
725 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
726 FindSenderForTrack(MediaStreamTrackInterface* track) const;
deadbeef70ab1a12015-09-28 16:53:55 -0700727
Steve Anton4171afb2017-11-20 10:20:22 -0800728 // Return the RtpSender with the given id, or null if none exists.
729 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
730 FindSenderById(const std::string& sender_id) const;
731
732 // Return the RtpReceiver with the given id, or null if none exists.
733 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
734 FindReceiverById(const std::string& receiver_id) const;
735
736 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
737 cricket::MediaType media_type);
738 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
739 cricket::MediaType media_type);
740 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
Emircan Uysalerbc609ea2018-03-27 21:57:18 +0000741 const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800742 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 11:33:11 -0700743
744 // Returns the specified SCTP DataChannel in sctp_data_channels_,
745 // or nullptr if not found.
746 DataChannel* FindDataChannelBySid(int sid) const;
747
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700748 // Called when first configuring the port allocator.
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200749 bool InitializePortAllocator_n(
750 const cricket::ServerAddresses& stun_servers,
751 const std::vector<cricket::RelayServerConfig>& turn_servers,
752 const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800753 // Called when SetConfiguration is called to apply the supported subset
754 // of the configuration on the network thread.
755 bool ReconfigurePortAllocator_n(
756 const cricket::ServerAddresses& stun_servers,
757 const std::vector<cricket::RelayServerConfig>& turn_servers,
758 IceTransportsType type,
759 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200760 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800761 webrtc::TurnCustomizer* turn_customizer,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200762 absl::optional<int> stun_candidate_keepalive_interval);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700763
Elad Alon99c3fe52017-10-13 16:29:40 +0200764 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700765 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100766 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
767 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200768
Elad Alonacb24172017-10-06 14:32:13 +0200769 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700770 // This function should only be called from the worker thread.
771 void StopRtcEventLog_w();
772
Steve Anton038834f2017-07-14 15:59:59 -0700773 // Ensures the configuration doesn't have any parameters with invalid values,
774 // or values that conflict with other parameters.
775 //
776 // Returns RTCError::OK() if there are no issues.
777 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
778
Steve Antonba818672017-11-06 10:21:57 -0800779 cricket::ChannelManager* channel_manager() const;
Steve Antonba818672017-11-06 10:21:57 -0800780
Steve Antonf8470812017-12-04 10:46:21 -0800781 enum class SessionError {
782 kNone, // No error.
783 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
784 kTransport, // Error from the underlying transport.
785 };
786
Steve Anton75737c02017-11-06 10:37:17 -0800787 // Returns the last error in the session. See the enum above for details.
Steve Antonf8470812017-12-04 10:46:21 -0800788 SessionError session_error() const { return session_error_; }
789 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 10:37:17 -0800790
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800791 cricket::ChannelInterface* GetChannel(const std::string& content_name);
Steve Anton75737c02017-11-06 10:37:17 -0800792
793 // Get current SSL role used by SCTP's underlying transport.
794 bool GetSctpSslRole(rtc::SSLRole* role);
795
Steve Anton75737c02017-11-06 10:37:17 -0800796 cricket::IceConfig ParseIceConfig(
797 const PeerConnectionInterface::RTCConfiguration& config) const;
798
Steve Anton75737c02017-11-06 10:37:17 -0800799 // Implements DataChannelProviderInterface.
800 bool SendData(const cricket::SendDataParams& params,
801 const rtc::CopyOnWriteBuffer& payload,
802 cricket::SendDataResult* result) override;
803 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
804 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
805 void AddSctpDataStream(int sid) override;
806 void RemoveSctpDataStream(int sid) override;
807 bool ReadyToSendData() const override;
808
809 cricket::DataChannelType data_channel_type() const;
810
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800811 // Implements DataChannelSink.
812 void OnDataReceived(int channel_id,
813 DataMessageType type,
814 const rtc::CopyOnWriteBuffer& buffer) override;
815 void OnChannelClosing(int channel_id) override;
816 void OnChannelClosed(int channel_id) override;
817
Steve Anton75737c02017-11-06 10:37:17 -0800818 // Called when an RTCCertificate is generated or retrieved by
819 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
820 void OnCertificateReady(
821 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
822 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
823
Steve Anton75737c02017-11-06 10:37:17 -0800824 // Non-const versions of local_description()/remote_description(), for use
825 // internally.
826 SessionDescriptionInterface* mutable_local_description() {
827 return pending_local_description_ ? pending_local_description_.get()
828 : current_local_description_.get();
829 }
830 SessionDescriptionInterface* mutable_remote_description() {
831 return pending_remote_description_ ? pending_remote_description_.get()
832 : current_remote_description_.get();
833 }
834
835 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 10:46:21 -0800836 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 10:37:17 -0800837
Zhi Huange830e682018-03-30 10:48:35 -0700838 RTCError UpdateSessionState(SdpType type,
839 cricket::ContentSource source,
840 const cricket::SessionDescription* description);
Steve Anton75737c02017-11-06 10:37:17 -0800841 // Push the media parts of the local or remote session description
842 // down to all of the channels.
Karl Wiberg5966c502019-02-21 23:55:09 +0100843 RTCError PushdownMediaDescription(SdpType type, cricket::ContentSource source)
844 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800845 bool PushdownSctpParameters_n(cricket::ContentSource source);
846
Steve Anton8a006912017-12-04 15:25:56 -0800847 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -0800848 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800849
850 // Returns true and the TransportInfo of the given |content_name|
851 // from |description|. Returns false if it's not available.
852 static bool GetTransportDescription(
853 const cricket::SessionDescription* description,
854 const std::string& content_name,
855 cricket::TransportDescription* info);
856
Steve Anton75737c02017-11-06 10:37:17 -0800857 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 10:52:59 -0800858 // This enables media to flow on all configured audio/video channels and the
859 // RtpDataChannel.
860 void EnableSending();
Steve Anton3fe1b152017-12-12 10:20:08 -0800861
Steve Anton8af21862017-12-15 11:20:13 -0800862 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
863 void DestroyAllChannels();
Steve Anton3fe1b152017-12-12 10:20:08 -0800864
Steve Anton75737c02017-11-06 10:37:17 -0800865 // Returns the media index for a local ice candidate given the content name.
866 // Returns false if the local session description does not have a media
867 // content called |content_name|.
868 bool GetLocalCandidateMediaIndex(const std::string& content_name,
869 int* sdp_mline_index);
870 // Uses all remote candidates in |remote_desc| in this session.
871 bool UseCandidatesInSessionDescription(
Karl Wiberg744310f2019-02-14 10:18:56 +0100872 const SessionDescriptionInterface* remote_desc)
873 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800874 // Uses |candidate| in this session.
Karl Wiberg744310f2019-02-14 10:18:56 +0100875 bool UseCandidate(const IceCandidateInterface* candidate)
876 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800877 // Deletes the corresponding channel of contents that don't exist in |desc|.
878 // |desc| can be null. This means that all channels are deleted.
Karl Wiberg5966c502019-02-21 23:55:09 +0100879 void RemoveUnusedChannels(const cricket::SessionDescription* desc)
880 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800881
882 // Allocates media channels based on the |desc|. If |desc| doesn't have
883 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
884 // This method will also delete any existing media channels before creating.
Karl Wiberg5966c502019-02-21 23:55:09 +0100885 RTCError CreateChannels(const cricket::SessionDescription& desc)
886 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800887
888 // If the BUNDLE policy is max-bundle, then we know for sure that all
889 // transports will be bundled from the start. This method returns the BUNDLE
890 // group if that's the case, or null if BUNDLE will be negotiated later. An
891 // error is returned if max-bundle is specified but the session description
892 // does not have a BUNDLE group.
893 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
Karl Wiberg5966c502019-02-21 23:55:09 +0100894 const cricket::SessionDescription& desc) const
895 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800896
897 // Helper methods to create media channels.
Karl Wiberg5966c502019-02-21 23:55:09 +0100898 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid)
899 RTC_RUN_ON(signaling_thread());
900 cricket::VideoChannel* CreateVideoChannel(const std::string& mid)
901 RTC_RUN_ON(signaling_thread());
902 bool CreateDataChannel(const std::string& mid) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800903
Zhi Huange830e682018-03-30 10:48:35 -0700904 bool CreateSctpTransport_n(const std::string& mid);
Steve Anton75737c02017-11-06 10:37:17 -0800905 // For bundling.
Steve Anton75737c02017-11-06 10:37:17 -0800906 void DestroySctpTransport_n();
907 // SctpTransport signal handlers. Needed to marshal signals from the network
908 // to signaling thread.
909 void OnSctpTransportReadyToSendData_n();
910 // This may be called with "false" if the direction of the m= section causes
911 // us to tear down the SCTP connection.
912 void OnSctpTransportReadyToSendData_s(bool ready);
913 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
914 const rtc::CopyOnWriteBuffer& payload);
915 // Beyond just firing the signal to the signaling thread, listens to SCTP
916 // CONTROL messages on unused SIDs and processes them as OPEN messages.
917 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
918 const rtc::CopyOnWriteBuffer& payload);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -0700919 void OnSctpClosingProcedureStartedRemotely_n(int sid);
920 void OnSctpClosingProcedureComplete_n(int sid);
Steve Anton75737c02017-11-06 10:37:17 -0800921
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800922 bool SetupMediaTransportForDataChannels_n(const std::string& mid)
923 RTC_RUN_ON(network_thread());
924 void OnMediaTransportStateChanged_n() RTC_RUN_ON(network_thread());
925 void TeardownMediaTransportForDataChannels_n() RTC_RUN_ON(network_thread());
926
Steve Anton75737c02017-11-06 10:37:17 -0800927 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
928 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
929 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 15:25:56 -0800930 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
Karl Wiberg5966c502019-02-21 23:55:09 +0100931 cricket::ContentSource source)
932 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800933
Steve Anton3828c062017-12-06 10:34:51 -0800934 // Check if a call to SetLocalDescription is acceptable with a session
935 // description of the given type.
936 bool ExpectSetLocalDescription(SdpType type);
937 // Check if a call to SetRemoteDescription is acceptable with a session
938 // description of the given type.
939 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800940 // Verifies a=setup attribute as per RFC 5763.
941 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 10:34:51 -0800942 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800943
944 // Returns true if we are ready to push down the remote candidate.
945 // |remote_desc| is the new remote description, or NULL if the current remote
946 // description should be used. Output |valid| is true if the candidate media
947 // index is valid.
948 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
949 const SessionDescriptionInterface* remote_desc,
950 bool* valid);
951
952 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
953 // this session.
954 bool SrtpRequired() const;
955
Zhi Huange830e682018-03-30 10:48:35 -0700956 // JsepTransportController signal handlers.
Karl Wiberg744310f2019-02-14 10:18:56 +0100957 void OnTransportControllerConnectionState(cricket::IceConnectionState state)
958 RTC_RUN_ON(signaling_thread());
959 void OnTransportControllerGatheringState(cricket::IceGatheringState state)
960 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800961 void OnTransportControllerCandidatesGathered(
962 const std::string& transport_name,
Karl Wiberg744310f2019-02-14 10:18:56 +0100963 const std::vector<cricket::Candidate>& candidates)
964 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800965 void OnTransportControllerCandidatesRemoved(
Karl Wiberg744310f2019-02-14 10:18:56 +0100966 const std::vector<cricket::Candidate>& candidates)
967 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800968 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
969
Steve Antonf8470812017-12-04 10:46:21 -0800970 const char* SessionErrorToString(SessionError error) const;
Steve Anton75737c02017-11-06 10:37:17 -0800971 std::string GetSessionErrorMsg();
972
Steve Anton8e20f172018-03-06 10:55:04 -0800973 // Report the UMA metric SdpFormatReceived for the given remote offer.
974 void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
975
Steve Anton0ffaaa22018-02-23 10:31:30 -0800976 // Report inferred negotiated SDP semantics from a local/remote answer to the
977 // UMA observer.
978 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
979
Steve Anton75737c02017-11-06 10:37:17 -0800980 // Invoked when TransportController connection completion is signaled.
981 // Reports stats for all transports in use.
982 void ReportTransportStats();
983
984 // Gather the usage of IPv4/IPv6 as best connection.
985 void ReportBestConnectionState(const cricket::TransportStats& stats);
986
Steve Antonc7b964c2018-02-01 14:39:45 -0800987 void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
988 const std::set<cricket::MediaType>& media_types);
Steve Anton75737c02017-11-06 10:37:17 -0800989
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200990 void NoteUsageEvent(UsageEvent event);
Karl Wiberg744310f2019-02-14 10:18:56 +0100991 void ReportUsagePattern() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200992
Steve Anton75737c02017-11-06 10:37:17 -0800993 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
994
995 const std::string GetTransportName(const std::string& content_name);
996
Steve Anton6fec8802017-12-04 10:37:29 -0800997 // Destroys and clears the BaseChannel associated with the given transceiver,
998 // if such channel is set.
999 void DestroyTransceiverChannel(
1000 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1001 transceiver);
1002
1003 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Steve Anton75737c02017-11-06 10:37:17 -08001004 void DestroyDataChannel();
1005
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001006 // Destroys the given ChannelInterface.
1007 // The channel cannot be accessed after this method is called.
1008 void DestroyChannelInterface(cricket::ChannelInterface* channel);
Steve Anton6fec8802017-12-04 10:37:29 -08001009
Zhi Huang365381f2018-04-13 16:44:34 -07001010 // JsepTransportController::Observer override.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07001011 //
1012 // Called by |transport_controller_| when processing transport information
1013 // from a session description, and the mapping from m= sections to transports
1014 // changed (as a result of BUNDLE negotiation, or m= sections being
1015 // rejected).
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001016 bool OnTransportChanged(const std::string& mid,
1017 RtpTransportInternal* rtp_transport,
1018 cricket::DtlsTransportInternal* dtls_transport,
1019 MediaTransportInterface* media_transport) override;
Zhi Huange830e682018-03-30 10:48:35 -07001020
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02001021 // Returns the observer. Will crash on CHECK if the observer is removed.
Karl Wiberg744310f2019-02-14 10:18:56 +01001022 PeerConnectionObserver* Observer() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02001023
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001024 // Returns the CryptoOptions for this PeerConnection. This will always
1025 // return the RTCConfiguration.crypto_options if set and will only default
1026 // back to the PeerConnectionFactory settings if nothing was set.
Karl Wiberg5966c502019-02-21 23:55:09 +01001027 CryptoOptions GetCryptoOptions() RTC_RUN_ON(signaling_thread());
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001028
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001029 // Returns rtp transport, result can not be nullptr.
1030 RtpTransportInternal* GetRtpTransport(const std::string& mid) {
1031 auto rtp_transport = transport_controller_->GetRtpTransport(mid);
1032 RTC_DCHECK(rtp_transport);
1033 return rtp_transport;
1034 }
1035
1036 // Returns media transport, if PeerConnection was created with configuration
1037 // to use media transport. Otherwise returns nullptr.
Karl Wiberg5966c502019-02-21 23:55:09 +01001038 MediaTransportInterface* GetMediaTransport(const std::string& mid)
1039 RTC_RUN_ON(signaling_thread()) {
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001040 auto media_transport = transport_controller_->GetMediaTransport(mid);
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001041 RTC_DCHECK((configuration_.use_media_transport ||
1042 configuration_.use_media_transport_for_data_channels) ==
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -07001043 (media_transport != nullptr))
1044 << "configuration_.use_media_transport="
1045 << configuration_.use_media_transport
Bjorn Mellema9bbd862018-11-02 09:07:48 -07001046 << ", configuration_.use_media_transport_for_data_channels="
1047 << configuration_.use_media_transport_for_data_channels
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -07001048 << ", (media_transport != nullptr)=" << (media_transport != nullptr);
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001049 return media_transport;
1050 }
1051
Karl Wiberg106d92d2019-02-14 10:17:47 +01001052 sigslot::signal1<DataChannel*> SignalDataChannelCreated_
1053 RTC_GUARDED_BY(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -08001054
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 // Storing the factory as a scoped reference pointer ensures that the memory
1056 // in the PeerConnectionFactoryImpl remains available as long as the
1057 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
1058 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -07001059 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 // will refer to the same reference count.
Karl Wiberg744310f2019-02-14 10:18:56 +01001061 const rtc::scoped_refptr<PeerConnectionFactory> factory_;
1062 PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
1063 nullptr;
terelius33860252017-05-12 23:37:18 -07001064
1065 // The EventLog needs to outlive |call_| (and any other object that uses it).
Karl Wibergb03ab712019-02-14 11:59:57 +01001066 std::unique_ptr<RtcEventLog> event_log_ RTC_GUARDED_BY(worker_thread());
1067
1068 // Points to the same thing as `event_log_`. Since it's const, we may read the
1069 // pointer (but not touch the object) from any thread.
1070 RtcEventLog* const event_log_ptr_ RTC_PT_GUARDED_BY(worker_thread());
terelius33860252017-05-12 23:37:18 -07001071
Karl Wiberg8d2e2282019-02-17 13:00:07 +01001072 SignalingState signaling_state_ RTC_GUARDED_BY(signaling_thread()) = kStable;
1073 IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) =
1074 kIceConnectionNew;
1075 PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_
1076 RTC_GUARDED_BY(signaling_thread()) = kIceConnectionNew;
1077 PeerConnectionInterface::PeerConnectionState connection_state_
1078 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionState::kNew;
Jonas Olsson635474e2018-10-18 15:58:17 +02001079
Karl Wiberg8d2e2282019-02-17 13:00:07 +01001080 IceGatheringState ice_gathering_state_ RTC_GUARDED_BY(signaling_thread()) =
1081 kIceGatheringNew;
Karl Wiberg5966c502019-02-21 23:55:09 +01001082 PeerConnectionInterface::RTCConfiguration configuration_
1083 RTC_GUARDED_BY(signaling_thread());
1084
1085 // Cache configuration_.use_media_transport so that we can access it from
1086 // other threads.
1087 // TODO(bugs.webrtc.org/9987): Caching just this bool and allowing the data
1088 // it's derived from to change is not necessarily sound. Stop doing it.
1089 rtc::RaceChecker use_media_transport_race_checker_;
1090 bool use_media_transport_ RTC_GUARDED_BY(use_media_transport_race_checker_) =
1091 configuration_.use_media_transport;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092
Zach Steine20867f2018-08-02 13:20:15 -07001093 // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it
1094 // is not injected. It should be required once chromium supplies it.
1095 std::unique_ptr<AsyncResolverFactory> async_resolver_factory_;
kwibergd1fe2812016-04-27 06:47:29 -07001096 std::unique_ptr<cricket::PortAllocator> port_allocator_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001097 std::unique_ptr<rtc::SSLCertificateVerifier> tls_cert_verifier_;
Qingsi Wanga2d60672018-04-11 16:57:45 -07001098 int port_allocator_flags_ = 0;
deadbeefab9b2d12015-10-14 11:33:11 -07001099
zhihuang8f65cdf2016-05-06 18:40:30 -07001100 // One PeerConnection has only one RTCP CNAME.
1101 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
1102 std::string rtcp_cname_;
1103
deadbeefab9b2d12015-10-14 11:33:11 -07001104 // Streams added via AddStream.
1105 rtc::scoped_refptr<StreamCollection> local_streams_;
1106 // Streams created as a result of SetRemoteDescription.
1107 rtc::scoped_refptr<StreamCollection> remote_streams_;
1108
kwibergd1fe2812016-04-27 06:47:29 -07001109 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_;
deadbeefeb459812015-12-15 19:24:43 -08001110
Steve Anton4171afb2017-11-20 10:20:22 -08001111 // These lists store sender info seen in local/remote descriptions.
1112 std::vector<RtpSenderInfo> remote_audio_sender_infos_;
1113 std::vector<RtpSenderInfo> remote_video_sender_infos_;
1114 std::vector<RtpSenderInfo> local_audio_sender_infos_;
1115 std::vector<RtpSenderInfo> local_video_sender_infos_;
deadbeefab9b2d12015-10-14 11:33:11 -07001116
1117 SctpSidAllocator sid_allocator_;
1118 // label -> DataChannel
1119 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
1120 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -08001121 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -07001122
deadbeefbda7e0b2015-12-08 17:13:40 -08001123 bool remote_peer_supports_msid_ = false;
deadbeef70ab1a12015-09-28 16:53:55 -07001124
terelius33860252017-05-12 23:37:18 -07001125 std::unique_ptr<Call> call_;
terelius33860252017-05-12 23:37:18 -07001126 std::unique_ptr<StatsCollector> stats_; // A pointer is passed to senders_
1127 rtc::scoped_refptr<RTCStatsCollector> stats_collector_;
1128
deadbeefa601f5c2016-06-06 14:27:39 -07001129 std::vector<
Steve Anton4171afb2017-11-20 10:20:22 -08001130 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
1131 transceivers_;
Henrik Boström5b147782018-12-04 11:25:05 +01001132 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
1133 // line we create and use a stream with a random ID for our receivers. This is
1134 // to support legacy endpoints that do not support the a=msid attribute (as
1135 // opposed to streamless tracks with "a=msid:-").
1136 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_;
Amit Hilbuchae3df542019-01-07 12:13:08 -08001137 // MIDs will be generated using this generator which will keep track of
1138 // all the MIDs that have been seen over the life of the PeerConnection.
Amit Hilbuchdbb49df2019-01-23 14:54:24 -08001139 rtc::UniqueStringGenerator mid_generator_;
Steve Anton75737c02017-11-06 10:37:17 -08001140
Steve Antonf8470812017-12-04 10:46:21 -08001141 SessionError session_error_ = SessionError::kNone;
1142 std::string session_error_desc_;
Steve Anton75737c02017-11-06 10:37:17 -08001143
1144 std::string session_id_;
Steve Anton75737c02017-11-06 10:37:17 -08001145
Zhi Huange830e682018-03-30 10:48:35 -07001146 std::unique_ptr<JsepTransportController> transport_controller_;
Steve Anton75737c02017-11-06 10:37:17 -08001147 std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory_;
Steve Anton75737c02017-11-06 10:37:17 -08001148 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
1149 // when using SCTP.
1150 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
1151
1152 std::unique_ptr<cricket::SctpTransportInternal> sctp_transport_;
Zhi Huange830e682018-03-30 10:48:35 -07001153 // |sctp_mid_| is the content name (MID) in SDP.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02001154 absl::optional<std::string> sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -08001155 // Value cached on signaling thread. Only updated when SctpReadyToSendData
1156 // fires on the signaling thread.
1157 bool sctp_ready_to_send_data_ = false;
1158 // Same as signals provided by SctpTransport, but these are guaranteed to
1159 // fire on the signaling thread, whereas SctpTransport fires on the networking
1160 // thread.
1161 // |sctp_invoker_| is used so that any signals queued on the signaling thread
1162 // from the network thread are immediately discarded if the SctpTransport is
1163 // destroyed (due to m= section being rejected).
1164 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
1165 // are marshalled to the right thread. Could almost use proxy.h for this,
1166 // but it doesn't have a mechanism for marshalling sigslot::signals
1167 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_;
1168 sigslot::signal1<bool> SignalSctpReadyToSendData;
1169 sigslot::signal2<const cricket::ReceiveDataParams&,
1170 const rtc::CopyOnWriteBuffer&>
1171 SignalSctpDataReceived;
Taylor Brandstettercdd05f02018-05-31 13:23:32 -07001172 sigslot::signal1<int> SignalSctpClosingProcedureStartedRemotely;
1173 sigslot::signal1<int> SignalSctpClosingProcedureComplete;
Steve Anton75737c02017-11-06 10:37:17 -08001174
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001175 // Whether this peer is the caller. Set when the local description is applied.
1176 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
1177
1178 // Content name (MID) for media transport data channels in SDP.
1179 absl::optional<std::string> media_transport_data_mid_;
1180
1181 // Media transport used for data channels. Thread-safe.
1182 MediaTransportInterface* media_transport_ = nullptr;
1183
1184 // Cached value of whether the media transport is ready to send.
1185 bool media_transport_ready_to_send_data_ RTC_GUARDED_BY(signaling_thread()) =
1186 false;
1187
1188 // Used to invoke media transport signals on the signaling thread.
1189 std::unique_ptr<rtc::AsyncInvoker> media_transport_invoker_;
1190
1191 // Identical to the signals for SCTP, but from media transport:
1192 sigslot::signal1<bool> SignalMediaTransportWritable_s
1193 RTC_GUARDED_BY(signaling_thread());
1194 sigslot::signal2<const cricket::ReceiveDataParams&,
1195 const rtc::CopyOnWriteBuffer&>
1196 SignalMediaTransportReceivedData_s RTC_GUARDED_BY(signaling_thread());
1197 sigslot::signal1<int> SignalMediaTransportChannelClosing_s
1198 RTC_GUARDED_BY(signaling_thread());
1199 sigslot::signal1<int> SignalMediaTransportChannelClosed_s
1200 RTC_GUARDED_BY(signaling_thread());
1201
Steve Anton75737c02017-11-06 10:37:17 -08001202 std::unique_ptr<SessionDescriptionInterface> current_local_description_;
1203 std::unique_ptr<SessionDescriptionInterface> pending_local_description_;
1204 std::unique_ptr<SessionDescriptionInterface> current_remote_description_;
1205 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_;
1206 bool dtls_enabled_ = false;
1207 // Specifies which kind of data channel is allowed. This is controlled
1208 // by the chrome command-line flag and constraints:
1209 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
1210 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
1211 // not set or false, SCTP is allowed (DCT_SCTP);
1212 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
1213 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
1214 cricket::DataChannelType data_channel_type_ = cricket::DCT_NONE;
1215 // List of content names for which the remote side triggered an ICE restart.
1216 std::set<std::string> pending_ice_restarts_;
1217
1218 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
1219
1220 // Member variables for caching global options.
1221 cricket::AudioOptions audio_options_;
1222 cricket::VideoOptions video_options_;
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001223
1224 int usage_event_accumulator_ = 0;
Harald Alvestrand19793842018-06-25 12:03:50 +02001225 bool return_histogram_very_quickly_ = false;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001226
1227 // This object should be used to generate any SSRC that is not explicitly
1228 // specified by the user (or by the remote party).
1229 // The generator is not used directly, instead it is passed on to the
1230 // channel manager and the session description factory.
1231 rtc::UniqueRandomIdGenerator ssrc_generator_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232};
1233
1234} // namespace webrtc
1235
Steve Anton10542f22019-01-11 09:11:00 -08001236#endif // PC_PEER_CONNECTION_H_