blob: 2907cb565bfbc442d0f9cb5473ff3f2731344c7f [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
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000014#include <stdint.h>
Artem Titovd15a5752021-02-10 14:31:24 +010015
Tomas Gunnarsson1e40a0c2020-09-28 10:39:31 +020016#include <functional>
perkjd61bf802016-03-24 03:16:19 -070017#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070018#include <memory>
Steve Anton75737c02017-11-06 10:37:17 -080019#include <set>
20#include <string>
perkjd61bf802016-03-24 03:16:19 -070021#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000023#include "absl/types/optional.h"
24#include "api/adaptation/resource.h"
Harald Alvestrand0ccfbd22021-04-08 07:25:04 +000025#include "api/async_dns_resolver.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000026#include "api/candidate.h"
27#include "api/crypto/crypto_options.h"
28#include "api/data_channel_interface.h"
29#include "api/dtls_transport_interface.h"
Jonas Orelande62c2f22022-03-29 11:04:48 +020030#include "api/field_trials_view.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000031#include "api/ice_transport_interface.h"
32#include "api/jsep.h"
33#include "api/media_stream_interface.h"
34#include "api/media_types.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "api/peer_connection_interface.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000036#include "api/rtc_error.h"
37#include "api/rtc_event_log/rtc_event_log.h"
38#include "api/rtc_event_log_output.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000039#include "api/rtp_receiver_interface.h"
40#include "api/rtp_sender_interface.h"
41#include "api/rtp_transceiver_interface.h"
42#include "api/scoped_refptr.h"
43#include "api/sctp_transport_interface.h"
Artem Titovd15a5752021-02-10 14:31:24 +010044#include "api/sequence_checker.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000045#include "api/set_local_description_observer_interface.h"
46#include "api/set_remote_description_observer_interface.h"
47#include "api/stats/rtc_stats_collector_callback.h"
48#include "api/transport/bitrate_settings.h"
Niels Möller65f17ca2019-09-12 13:59:36 +020049#include "api/transport/data_channel_transport_interface.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000050#include "api/transport/enums.h"
Steve Anton10542f22019-01-11 09:11:00 -080051#include "api/turn_customizer.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000052#include "call/call.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000053#include "p2p/base/ice_transport_internal.h"
54#include "p2p/base/port.h"
55#include "p2p/base/port_allocator.h"
56#include "p2p/base/transport_description.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000057#include "pc/channel_interface.h"
Harald Alvestranda39689c2020-10-15 08:34:31 +000058#include "pc/connection_context.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010059#include "pc/data_channel_controller.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000060#include "pc/data_channel_utils.h"
61#include "pc/dtls_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080062#include "pc/jsep_transport_controller.h"
Steve Anton10542f22019-01-11 09:11:00 -080063#include "pc/peer_connection_internal.h"
Harald Alvestrand1090e442020-10-05 07:01:09 +000064#include "pc/peer_connection_message_handler.h"
Steve Anton10542f22019-01-11 09:11:00 -080065#include "pc/rtc_stats_collector.h"
66#include "pc/rtp_transceiver.h"
Harald Alvestrande15fb152020-10-19 13:28:05 +000067#include "pc/rtp_transmission_manager.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000068#include "pc/rtp_transport_internal.h"
69#include "pc/sctp_data_channel.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000070#include "pc/sdp_offer_answer.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000071#include "pc/session_description.h"
Steve Anton10542f22019-01-11 09:11:00 -080072#include "pc/stats_collector.h"
Harald Alvestrand38b768c2020-09-29 11:54:05 +000073#include "pc/transceiver_list.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000074#include "pc/transport_stats.h"
Harald Alvestrand44d0dff2020-10-09 05:43:53 +000075#include "pc/usage_pattern.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000076#include "rtc_base/checks.h"
77#include "rtc_base/copy_on_write_buffer.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000078#include "rtc_base/rtc_certificate.h"
79#include "rtc_base/ssl_certificate.h"
80#include "rtc_base/ssl_stream_adapter.h"
Tomas Gunnarsson1e40a0c2020-09-28 10:39:31 +020081#include "rtc_base/task_utils/pending_task_safety_flag.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000082#include "rtc_base/third_party/sigslot/sigslot.h"
83#include "rtc_base/thread.h"
84#include "rtc_base/thread_annotations.h"
Harald Alvestrand85466662021-04-19 21:21:36 +000085#include "rtc_base/weak_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
Harald Alvestrand9e334b72022-05-04 13:38:31 +000087namespace cricket {
88class ChannelManager;
89}
90
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092
Steve Anton75737c02017-11-06 10:37:17 -080093// PeerConnection is the implementation of the PeerConnection object as defined
94// by the PeerConnectionInterface API surface.
95// The class currently is solely responsible for the following:
96// - Managing the session state machine (signaling state).
97// - Creating and initializing lower-level objects, like PortAllocator and
98// BaseChannels.
99// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
100// objects.
101// - Tracking the current and pending local/remote session descriptions.
102// The class currently is jointly responsible for the following:
103// - Parsing and interpreting SDP.
104// - Generating offers and answers based on the current state.
105// - The ICE state machine.
106// - Generating stats.
Steve Anton2d8609c2018-01-23 16:38:46 -0800107class PeerConnection : public PeerConnectionInternal,
Harald Alvestrand5b84f382022-02-08 10:49:09 +0000108 public JsepTransportController::Observer {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 public:
Harald Alvestrand62166932020-10-26 08:30:41 +0000110 // Creates a PeerConnection and initializes it with the given values.
111 // If the initialization fails, the function releases the PeerConnection
112 // and returns nullptr.
113 //
114 // Note that the function takes ownership of dependencies, and will
115 // either use them or release them, whether it succeeds or fails.
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000116 static RTCErrorOr<rtc::scoped_refptr<PeerConnection>> Create(
Harald Alvestrand62166932020-10-26 08:30:41 +0000117 rtc::scoped_refptr<ConnectionContext> context,
Harald Alvestrand4da4a872020-11-04 10:34:21 +0000118 const PeerConnectionFactoryInterface::Options& options,
Harald Alvestrand62166932020-10-26 08:30:41 +0000119 std::unique_ptr<RtcEventLog> event_log,
120 std::unique_ptr<Call> call,
deadbeef653b8e02015-11-11 12:55:10 -0800121 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab58882018-05-02 15:12:47 -0700122 PeerConnectionDependencies dependencies);
deadbeef653b8e02015-11-11 12:55:10 -0800123
deadbeefa67696b2015-09-29 11:56:26 -0700124 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
125 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
126 bool AddStream(MediaStreamInterface* local_stream) override;
127 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
Steve Anton2d6c76a2018-01-05 17:10:52 -0800129 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -0800130 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800131 const std::vector<std::string>& stream_ids) override;
Harald Alvestrand09a0d012022-01-04 19:42:07 +0000132 RTCError RemoveTrackOrError(
Steve Anton24db5732018-07-23 10:27:33 -0700133 rtc::scoped_refptr<RtpSenderInterface> sender) override;
deadbeefe1f9d832016-01-14 15:35:42 -0800134
Steve Anton9158ef62017-11-27 13:01:52 -0800135 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
136 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
137 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
138 rtc::scoped_refptr<MediaStreamTrackInterface> track,
139 const RtpTransceiverInit& init) override;
140 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
141 cricket::MediaType media_type) override;
142 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
143 cricket::MediaType media_type,
144 const RtpTransceiverInit& init) override;
145
deadbeeffac06552015-11-25 11:26:01 -0800146 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800147 const std::string& kind,
148 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800149
deadbeef70ab1a12015-09-28 16:53:55 -0700150 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
151 const override;
152 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
153 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800154 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
155 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700156
Harald Alvestranda9af50f2021-05-21 13:33:51 +0000157 RTCErrorOr<rtc::scoped_refptr<DataChannelInterface>> CreateDataChannelOrError(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700159 const DataChannelInit* config) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100160 // WARNING: LEGACY. See peerconnectioninterface.h
deadbeefa67696b2015-09-29 11:56:26 -0700161 bool GetStats(StatsObserver* observer,
162 webrtc::MediaStreamTrackInterface* track,
163 StatsOutputLevel level) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100164 // Spec-complaint GetStats(). See peerconnectioninterface.h
hbos74e1a4f2016-09-15 23:33:01 -0700165 void GetStats(RTCStatsCollectorCallback* callback) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100166 void GetStats(
167 rtc::scoped_refptr<RtpSenderInterface> selector,
168 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
169 void GetStats(
170 rtc::scoped_refptr<RtpReceiverInterface> selector,
171 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100172 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173
deadbeefa67696b2015-09-29 11:56:26 -0700174 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175
deadbeefa67696b2015-09-29 11:56:26 -0700176 IceConnectionState ice_connection_state() override;
Harald Alvestrand5b661302022-01-28 13:08:34 +0000177 IceConnectionState ice_connection_state_internal() override {
178 return ice_connection_state();
179 }
Jonas Olsson12046902018-12-06 11:25:14 +0100180 IceConnectionState standardized_ice_connection_state() override;
Jonas Olsson635474e2018-10-18 15:58:17 +0200181 PeerConnectionState peer_connection_state() override;
deadbeefa67696b2015-09-29 11:56:26 -0700182 IceGatheringState ice_gathering_state() override;
Harald Alvestrand61f74d92020-03-02 11:20:00 +0100183 absl::optional<bool> can_trickle_ice_candidates() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184
deadbeefa67696b2015-09-29 11:56:26 -0700185 const SessionDescriptionInterface* local_description() const override;
186 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800187 const SessionDescriptionInterface* current_local_description() const override;
188 const SessionDescriptionInterface* current_remote_description()
189 const override;
190 const SessionDescriptionInterface* pending_local_description() const override;
191 const SessionDescriptionInterface* pending_remote_description()
192 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193
Henrik Boström79b69802019-07-18 11:16:56 +0200194 void RestartIce() override;
195
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700197 void CreateOffer(CreateSessionDescriptionObserver* observer,
198 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800199 void CreateAnswer(CreateSessionDescriptionObserver* observer,
200 const RTCOfferAnswerOptions& options) override;
Henrik Boström831ae4e2020-07-29 12:04:00 +0200201
202 void SetLocalDescription(
203 std::unique_ptr<SessionDescriptionInterface> desc,
204 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer)
205 override;
206 void SetLocalDescription(
207 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer)
208 override;
209 // TODO(https://crbug.com/webrtc/11798): Delete these methods in favor of the
210 // ones taking SetLocalDescriptionObserverInterface as argument.
deadbeefa67696b2015-09-29 11:56:26 -0700211 void SetLocalDescription(SetSessionDescriptionObserver* observer,
212 SessionDescriptionInterface* desc) override;
Henrik Boström4e196702019-10-30 10:35:50 +0100213 void SetLocalDescription(SetSessionDescriptionObserver* observer) override;
Henrik Boström831ae4e2020-07-29 12:04:00 +0200214
Henrik Boström31638672017-11-23 17:48:32 +0100215 void SetRemoteDescription(
216 std::unique_ptr<SessionDescriptionInterface> desc,
217 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
218 override;
Henrik Boström831ae4e2020-07-29 12:04:00 +0200219 // TODO(https://crbug.com/webrtc/11798): Delete this methods in favor of the
220 // ones taking SetRemoteDescriptionObserverInterface as argument.
221 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
222 SessionDescriptionInterface* desc) override;
223
deadbeef46c73892016-11-16 19:42:04 -0800224 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
Niels Möller2579f0c2019-08-19 09:58:17 +0200225 RTCError SetConfiguration(
226 const PeerConnectionInterface::RTCConfiguration& configuration) override;
deadbeefa67696b2015-09-29 11:56:26 -0700227 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Henrik Boströmee6f4f62019-11-06 12:36:12 +0100228 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
229 std::function<void(RTCError)> callback) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700230 bool RemoveIceCandidates(
231 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232
Niels Möller0c4f7be2018-05-07 14:01:37 +0200233 RTCError SetBitrate(const BitrateSettings& bitrate) override;
zstein4b979802017-06-02 14:37:37 -0700234
henrika5f6bf242017-11-01 11:06:56 +0100235 void SetAudioPlayout(bool playout) override;
236 void SetAudioRecording(bool recording) override;
237
Harald Alvestrandad88c882018-11-28 16:47:46 +0100238 rtc::scoped_refptr<DtlsTransportInterface> LookupDtlsTransportByMid(
239 const std::string& mid) override;
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100240 rtc::scoped_refptr<DtlsTransport> LookupDtlsTransportByMidInternal(
241 const std::string& mid);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100242
Harald Alvestrandc85328f2019-02-28 07:51:00 +0100243 rtc::scoped_refptr<SctpTransportInterface> GetSctpTransport() const override;
244
Henrik Boström4c1e7cc2020-06-11 12:26:53 +0200245 void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
246
Bjorn Tereliusde939432017-11-20 17:38:14 +0100247 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
248 int64_t output_period_ms) override;
Niels Möllerf00ca1a2019-05-10 11:33:12 +0200249 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700250 void StopRtcEventLog() override;
251
deadbeefa67696b2015-09-29 11:56:26 -0700252 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
Taylor Brandstetterc88fe702020-08-03 16:36:16 -0700254 rtc::Thread* signaling_thread() const final {
Harald Alvestranda39689c2020-10-15 08:34:31 +0000255 return context_->signaling_thread();
Taylor Brandstetterc88fe702020-08-03 16:36:16 -0700256 }
257
Karl Wiberg4ae63472019-02-22 00:57:06 +0100258 rtc::Thread* network_thread() const final {
Harald Alvestranda39689c2020-10-15 08:34:31 +0000259 return context_->network_thread();
Steve Anton2d8609c2018-01-23 16:38:46 -0800260 }
Harald Alvestranda39689c2020-10-15 08:34:31 +0000261 rtc::Thread* worker_thread() const final { return context_->worker_thread(); }
deadbeefab9b2d12015-10-14 11:33:11 -0700262
Karl Wiberga58e1692019-03-26 13:33:43 +0100263 std::string session_id() const override {
Karl Wiberga58e1692019-03-26 13:33:43 +0100264 return session_id_;
265 }
Steve Anton75737c02017-11-06 10:37:17 -0800266
Steve Anton2d8609c2018-01-23 16:38:46 -0800267 bool initial_offerer() const override {
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200268 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000269 return sdp_handler_->initial_offerer();
Steve Anton2d8609c2018-01-23 16:38:46 -0800270 }
Steve Anton75737c02017-11-06 10:37:17 -0800271
Steve Anton2d8609c2018-01-23 16:38:46 -0800272 std::vector<
273 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 10:07:54 -0800274 GetTransceiversInternal() const override {
Karl Wiberga58e1692019-03-26 13:33:43 +0100275 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrande15fb152020-10-19 13:28:05 +0000276 return rtp_manager()->transceivers()->List();
Steve Anton2d8609c2018-01-23 16:38:46 -0800277 }
278
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000279 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() override {
280 RTC_DCHECK_RUN_ON(signaling_thread());
281 return data_channel_controller_.SignalSctpDataChannelCreated();
282 }
283
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700284 std::vector<DataChannelStats> GetDataChannelStats() const override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800285
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200286 absl::optional<std::string> sctp_transport_name() const override;
Tomas Gunnarssonbfd9ba82021-04-18 11:55:57 +0200287 absl::optional<std::string> sctp_mid() const override;
Steve Anton75737c02017-11-06 10:37:17 -0800288
Qingsi Wang72a43a12018-02-20 16:03:18 -0800289 cricket::CandidateStatsList GetPooledCandidateStats() const override;
Steve Anton5dfde182018-02-06 10:34:40 -0800290 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
291 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800292 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 10:37:17 -0800293
Steve Anton2d8609c2018-01-23 16:38:46 -0800294 bool GetLocalCertificate(
295 const std::string& transport_name,
296 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800297 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Steve Anton2d8609c2018-01-23 16:38:46 -0800298 const std::string& transport_name) override;
299 bool IceRestartPending(const std::string& content_name) const override;
300 bool NeedsIceRestart(const std::string& content_name) const override;
301 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000302
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100303 // Functions needed by DataChannelController
Harald Alvestrand5b84f382022-02-08 10:49:09 +0000304 void NoteDataAddedEvent() override { NoteUsageEvent(UsageEvent::DATA_ADDED); }
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100305 // Returns the observer. Will crash on CHECK if the observer is removed.
Harald Alvestrand5b661302022-01-28 13:08:34 +0000306 PeerConnectionObserver* Observer() const override;
307 bool IsClosed() const override {
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100308 RTC_DCHECK_RUN_ON(signaling_thread());
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200309 return !sdp_handler_ ||
310 sdp_handler_->signaling_state() == PeerConnectionInterface::kClosed;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100311 }
312 // Get current SSL role used by SCTP's underlying transport.
Harald Alvestrand5b661302022-01-28 13:08:34 +0000313 bool GetSctpSslRole(rtc::SSLRole* role) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100314 // Handler for the "channel closed" signal
Harald Alvestrand5b84f382022-02-08 10:49:09 +0000315 void OnSctpDataChannelClosed(DataChannelInterface* channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100316
Henrik Boströme574a312020-08-25 10:20:11 +0200317 bool ShouldFireNegotiationNeededEvent(uint32_t event_id) override;
318
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000319 // Functions needed by SdpOfferAnswerHandler
Harald Alvestrand5b661302022-01-28 13:08:34 +0000320 StatsCollector* stats() override {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000321 RTC_DCHECK_RUN_ON(signaling_thread());
322 return stats_.get();
323 }
Harald Alvestrand5b661302022-01-28 13:08:34 +0000324 DataChannelController* data_channel_controller() override {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000325 RTC_DCHECK_RUN_ON(signaling_thread());
326 return &data_channel_controller_;
327 }
Harald Alvestrand5b661302022-01-28 13:08:34 +0000328 bool dtls_enabled() const override {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000329 RTC_DCHECK_RUN_ON(signaling_thread());
330 return dtls_enabled_;
331 }
Harald Alvestrand5b661302022-01-28 13:08:34 +0000332 const PeerConnectionInterface::RTCConfiguration* configuration()
333 const override {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000334 RTC_DCHECK_RUN_ON(signaling_thread());
335 return &configuration_;
336 }
Harald Alvestrand5b661302022-01-28 13:08:34 +0000337 PeerConnectionMessageHandler* message_handler() override {
Harald Alvestrand1090e442020-10-05 07:01:09 +0000338 RTC_DCHECK_RUN_ON(signaling_thread());
339 return &message_handler_;
340 }
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000341
Harald Alvestrand5b661302022-01-28 13:08:34 +0000342 RtpTransmissionManager* rtp_manager() override { return rtp_manager_.get(); }
343 const RtpTransmissionManager* rtp_manager() const override {
Harald Alvestrande15fb152020-10-19 13:28:05 +0000344 return rtp_manager_.get();
345 }
Harald Alvestrand653429c2020-10-19 16:05:20 +0000346
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000347 JsepTransportController* transport_controller_s() override {
348 RTC_DCHECK_RUN_ON(signaling_thread());
349 return transport_controller_copy_;
350 }
351 JsepTransportController* transport_controller_n() override {
352 RTC_DCHECK_RUN_ON(network_thread());
Harald Alvestrand653429c2020-10-19 16:05:20 +0000353 return transport_controller_.get();
354 }
Harald Alvestrand5b661302022-01-28 13:08:34 +0000355 cricket::PortAllocator* port_allocator() override {
356 return port_allocator_.get();
357 }
358 Call* call_ptr() override { return call_ptr_; }
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000359
360 ConnectionContext* context() { return context_.get(); }
Harald Alvestrand5b661302022-01-28 13:08:34 +0000361 const PeerConnectionFactoryInterface::Options* options() const override {
Harald Alvestrand4da4a872020-11-04 10:34:21 +0000362 return &options_;
363 }
Harald Alvestrand5b661302022-01-28 13:08:34 +0000364 void SetIceConnectionState(IceConnectionState new_state) override;
365 void NoteUsageEvent(UsageEvent event) override;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000366
Tomas Gunnarsson8cb97062021-02-08 18:57:04 +0100367 // Asynchronously adds a remote candidate on the network thread.
368 void AddRemoteCandidate(const std::string& mid,
Harald Alvestrand5b661302022-01-28 13:08:34 +0000369 const cricket::Candidate& candidate) override;
Tomas Gunnarsson8cb97062021-02-08 18:57:04 +0100370
Philipp Hancke844c7592021-01-18 11:25:31 +0100371 // Report the UMA metric SdpFormatReceived for the given remote description.
372 void ReportSdpFormatReceived(
Harald Alvestrand5b661302022-01-28 13:08:34 +0000373 const SessionDescriptionInterface& remote_description) override;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000374
Philipp Hancke54b925c2021-01-28 09:56:39 +0100375 // Report the UMA metric BundleUsage for the given remote description.
376 void ReportSdpBundleUsage(
Harald Alvestrand5b661302022-01-28 13:08:34 +0000377 const SessionDescriptionInterface& remote_description) override;
Philipp Hancke54b925c2021-01-28 09:56:39 +0100378
Harald Alvestrand653429c2020-10-19 16:05:20 +0000379 // Returns true if the PeerConnection is configured to use Unified Plan
380 // semantics for creating offers/answers and setting local/remote
381 // descriptions. If this is true the RtpTransceiver API will also be available
382 // to the user. If this is false, Plan B semantics are assumed.
383 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
384 // sufficient time has passed.
Harald Alvestrand5b661302022-01-28 13:08:34 +0000385 bool IsUnifiedPlan() const override {
Harald Alvestrand653429c2020-10-19 16:05:20 +0000386 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand62166932020-10-26 08:30:41 +0000387 return is_unified_plan_;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000388 }
Henrik Boströmf8187e02021-04-26 21:04:26 +0200389 bool ValidateBundleSettings(
390 const cricket::SessionDescription* desc,
391 const std::map<std::string, const cricket::ContentGroup*>&
Harald Alvestrand5b661302022-01-28 13:08:34 +0000392 bundle_groups_by_mid) override;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000393
Harald Alvestrand7af57c62021-04-16 11:12:14 +0000394 // Returns the MID for the data section associated with the
395 // SCTP data channel, if it has been set. If no data
Harald Alvestrand653429c2020-10-19 16:05:20 +0000396 // channels are configured this will return nullopt.
Harald Alvestrand5b661302022-01-28 13:08:34 +0000397 absl::optional<std::string> GetDataMid() const override;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000398
Harald Alvestrand5b661302022-01-28 13:08:34 +0000399 void SetSctpDataMid(const std::string& mid) override;
Tomas Gunnarsson92eebef2021-02-10 13:05:44 +0100400
Harald Alvestrand5b661302022-01-28 13:08:34 +0000401 void ResetSctpDataMid() override;
Tomas Gunnarsson92eebef2021-02-10 13:05:44 +0100402
403 // Asynchronously calls SctpTransport::Start() on the network thread for
Artem Titov880fa812021-07-30 22:30:23 +0200404 // `sctp_mid()` if set. Called as part of setting the local description.
Tomas Gunnarsson92eebef2021-02-10 13:05:44 +0100405 void StartSctpTransport(int local_port,
406 int remote_port,
Harald Alvestrand5b661302022-01-28 13:08:34 +0000407 int max_message_size) override;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000408
409 // Returns the CryptoOptions for this PeerConnection. This will always
410 // return the RTCConfiguration.crypto_options if set and will only default
411 // back to the PeerConnectionFactory settings if nothing was set.
Harald Alvestrand5b661302022-01-28 13:08:34 +0000412 CryptoOptions GetCryptoOptions() override;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000413
414 // Internal implementation for AddTransceiver family of methods. If
Artem Titov880fa812021-07-30 22:30:23 +0200415 // `fire_callback` is set, fires OnRenegotiationNeeded callback if successful.
Harald Alvestrand653429c2020-10-19 16:05:20 +0000416 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
417 cricket::MediaType media_type,
418 rtc::scoped_refptr<MediaStreamTrackInterface> track,
419 const RtpTransceiverInit& init,
Harald Alvestrand5b661302022-01-28 13:08:34 +0000420 bool fire_callback = true) override;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000421
422 // Returns rtp transport, result can not be nullptr.
Tomas Gunnarsson92eebef2021-02-10 13:05:44 +0100423 RtpTransportInternal* GetRtpTransport(const std::string& mid);
Harald Alvestrand653429c2020-10-19 16:05:20 +0000424
425 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
426 // this session.
Harald Alvestrand5b661302022-01-28 13:08:34 +0000427 bool SrtpRequired() const override;
Harald Alvestrand653429c2020-10-19 16:05:20 +0000428
Harald Alvestrand5b661302022-01-28 13:08:34 +0000429 bool SetupDataChannelTransport_n(const std::string& mid) override
Harald Alvestrand653429c2020-10-19 16:05:20 +0000430 RTC_RUN_ON(network_thread());
Harald Alvestrand5b661302022-01-28 13:08:34 +0000431 void TeardownDataChannelTransport_n() override RTC_RUN_ON(network_thread());
Harald Alvestrande15fb152020-10-19 13:28:05 +0000432
Harald Alvestrand35ba0c52022-05-05 07:37:41 +0000433 const FieldTrialsView& trials() const override { return *trials_; }
434
435 bool ConfiguredForMedia() const;
436
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100437 // Functions made public for testing.
Harald Alvestrand19793842018-06-25 12:03:50 +0200438 void ReturnHistogramVeryQuicklyForTesting() {
Karl Wibergf73f7d62019-04-08 15:36:53 +0200439 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand19793842018-06-25 12:03:50 +0200440 return_histogram_very_quickly_ = true;
441 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200442 void RequestUsagePatternReportForTesting();
Harald Alvestrand19793842018-06-25 12:03:50 +0200443
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 protected:
Harald Alvestrand62166932020-10-26 08:30:41 +0000445 // Available for rtc::scoped_refptr creation
Harald Alvestrand4da4a872020-11-04 10:34:21 +0000446 PeerConnection(rtc::scoped_refptr<ConnectionContext> context,
447 const PeerConnectionFactoryInterface::Options& options,
448 bool is_unified_plan,
449 std::unique_ptr<RtcEventLog> event_log,
450 std::unique_ptr<Call> call,
Tommic3257d02021-02-10 17:40:08 +0000451 PeerConnectionDependencies& dependencies,
452 bool dtls_enabled);
Harald Alvestrand62166932020-10-26 08:30:41 +0000453
deadbeefa67696b2015-09-29 11:56:26 -0700454 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455
456 private:
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000457 RTCError Initialize(
Harald Alvestrand62166932020-10-26 08:30:41 +0000458 const PeerConnectionInterface::RTCConfiguration& configuration,
459 PeerConnectionDependencies dependencies);
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000460 JsepTransportController* InitializeTransportController_n(
Tommic3257d02021-02-10 17:40:08 +0000461 const RTCConfiguration& configuration,
462 const PeerConnectionDependencies& dependencies)
463 RTC_RUN_ON(network_thread());
Harald Alvestrand62166932020-10-26 08:30:41 +0000464
Steve Antonf9381f02017-12-14 10:23:57 -0800465 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100466 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender)
467 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800468
Jonas Olsson635474e2018-10-18 15:58:17 +0200469 void SetStandardizedIceConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100470 PeerConnectionInterface::IceConnectionState new_state)
471 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200472 void SetConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100473 PeerConnectionInterface::PeerConnectionState new_state)
474 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200475
Eldar Relloda13ea22019-06-01 12:23:43 +0300476 // Called any time the IceGatheringState changes.
Karl Wiberg744310f2019-02-14 10:18:56 +0100477 void OnIceGatheringChange(IceGatheringState new_state)
478 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800479 // New ICE candidate has been gathered.
Karl Wiberg744310f2019-02-14 10:18:56 +0100480 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)
481 RTC_RUN_ON(signaling_thread());
Eldar Relloda13ea22019-06-01 12:23:43 +0300482 // Gathering of an ICE candidate failed.
Eldar Rello0095d372019-12-02 22:22:07 +0200483 void OnIceCandidateError(const std::string& address,
484 int port,
Eldar Relloda13ea22019-06-01 12:23:43 +0300485 const std::string& url,
486 int error_code,
487 const std::string& error_text)
488 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800489 // Some local ICE candidates have been removed.
Karl Wiberg744310f2019-02-14 10:18:56 +0100490 void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates)
491 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492
Alex Drake00c7ecf2019-08-06 10:54:47 -0700493 void OnSelectedCandidatePairChanged(
494 const cricket::CandidatePairChangeEvent& event)
495 RTC_RUN_ON(signaling_thread());
496
Steve Anton52d86772018-02-20 15:48:12 -0800497 void OnNegotiationNeeded();
498
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700499 // Called when first configuring the port allocator.
Karl Wibergfb3be392019-03-22 14:13:22 +0100500 struct InitializePortAllocatorResult {
501 bool enable_ipv6;
502 };
503 InitializePortAllocatorResult InitializePortAllocator_n(
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200504 const cricket::ServerAddresses& stun_servers,
505 const std::vector<cricket::RelayServerConfig>& turn_servers,
506 const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800507 // Called when SetConfiguration is called to apply the supported subset
508 // of the configuration on the network thread.
509 bool ReconfigurePortAllocator_n(
510 const cricket::ServerAddresses& stun_servers,
511 const std::vector<cricket::RelayServerConfig>& turn_servers,
512 IceTransportsType type,
513 int candidate_pool_size,
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700514 PortPrunePolicy turn_port_prune_policy,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800515 webrtc::TurnCustomizer* turn_customizer,
Karl Wiberg739506e2019-04-03 11:37:28 +0200516 absl::optional<int> stun_candidate_keepalive_interval,
517 bool have_local_description);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700518
Elad Alon99c3fe52017-10-13 16:29:40 +0200519 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700520 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100521 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
522 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200523
Elad Alonacb24172017-10-06 14:32:13 +0200524 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700525 // This function should only be called from the worker thread.
526 void StopRtcEventLog_w();
527
Artem Titov880fa812021-07-30 22:30:23 +0200528 // Returns true and the TransportInfo of the given `content_name`
529 // from `description`. Returns false if it's not available.
Steve Anton75737c02017-11-06 10:37:17 -0800530 static bool GetTransportDescription(
531 const cricket::SessionDescription* description,
532 const std::string& content_name,
533 cricket::TransportDescription* info);
534
Steve Anton75737c02017-11-06 10:37:17 -0800535 // Returns the media index for a local ice candidate given the content name.
536 // Returns false if the local session description does not have a media
Artem Titov880fa812021-07-30 22:30:23 +0200537 // content called `content_name`.
Steve Anton75737c02017-11-06 10:37:17 -0800538 bool GetLocalCandidateMediaIndex(const std::string& content_name,
Karl Wiberg739506e2019-04-03 11:37:28 +0200539 int* sdp_mline_index)
540 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800541
Zhi Huange830e682018-03-30 10:48:35 -0700542 // JsepTransportController signal handlers.
Karl Wiberg744310f2019-02-14 10:18:56 +0100543 void OnTransportControllerConnectionState(cricket::IceConnectionState state)
544 RTC_RUN_ON(signaling_thread());
545 void OnTransportControllerGatheringState(cricket::IceGatheringState state)
546 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800547 void OnTransportControllerCandidatesGathered(
548 const std::string& transport_name,
Karl Wiberg744310f2019-02-14 10:18:56 +0100549 const std::vector<cricket::Candidate>& candidates)
550 RTC_RUN_ON(signaling_thread());
Eldar Relloda13ea22019-06-01 12:23:43 +0300551 void OnTransportControllerCandidateError(
552 const cricket::IceCandidateErrorEvent& event)
553 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800554 void OnTransportControllerCandidatesRemoved(
Karl Wiberg744310f2019-02-14 10:18:56 +0100555 const std::vector<cricket::Candidate>& candidates)
556 RTC_RUN_ON(signaling_thread());
Alex Drake00c7ecf2019-08-06 10:54:47 -0700557 void OnTransportControllerCandidateChanged(
558 const cricket::CandidatePairChangeEvent& event)
559 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800560 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
561
Steve Anton75737c02017-11-06 10:37:17 -0800562 // Invoked when TransportController connection completion is signaled.
563 // Reports stats for all transports in use.
Tomas Gunnarsson2001dc32021-04-06 11:36:00 +0200564 void ReportTransportStats() RTC_RUN_ON(network_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800565
566 // Gather the usage of IPv4/IPv6 as best connection.
Tommic3257d02021-02-10 17:40:08 +0000567 static void ReportBestConnectionState(const cricket::TransportStats& stats);
Steve Anton75737c02017-11-06 10:37:17 -0800568
Tommic3257d02021-02-10 17:40:08 +0000569 static void ReportNegotiatedCiphers(
570 bool dtls_enabled,
571 const cricket::TransportStats& stats,
572 const std::set<cricket::MediaType>& media_types);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700573 void ReportIceCandidateCollected(const cricket::Candidate& candidate)
574 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800575
Karl Wiberg744310f2019-02-14 10:18:56 +0100576 void ReportUsagePattern() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200577
Tomas Gunnarsson8cb97062021-02-08 18:57:04 +0100578 void ReportRemoteIceCandidateAdded(const cricket::Candidate& candidate);
579
Zhi Huang365381f2018-04-13 16:44:34 -0700580 // JsepTransportController::Observer override.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700581 //
Artem Titov880fa812021-07-30 22:30:23 +0200582 // Called by `transport_controller_` when processing transport information
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700583 // from a session description, and the mapping from m= sections to transports
584 // changed (as a result of BUNDLE negotiation, or m= sections being
585 // rejected).
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700586 bool OnTransportChanged(
587 const std::string& mid,
588 RtpTransportInternal* rtp_transport,
589 rtc::scoped_refptr<DtlsTransport> dtls_transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700590 DataChannelTransportInterface* data_channel_transport) override;
Zhi Huange830e682018-03-30 10:48:35 -0700591
Tomas Gunnarsson1e40a0c2020-09-28 10:39:31 +0200592 std::function<void(const rtc::CopyOnWriteBuffer& packet,
593 int64_t packet_time_us)>
594 InitializeRtcpCallback();
595
Harald Alvestrand9e334b72022-05-04 13:38:31 +0000596 cricket::ChannelManager* channel_manager() {
597 return context_->channel_manager();
598 }
599
Harald Alvestranda39689c2020-10-15 08:34:31 +0000600 const rtc::scoped_refptr<ConnectionContext> context_;
Jonas Oreland6c7f9842022-04-19 17:24:10 +0200601 // Field trials active for this PeerConnection is the first of:
602 // a) Specified in PeerConnectionDependencies (owned).
603 // b) Accessed via ConnectionContext (e.g PeerConnectionFactoryDependencies>
604 // c) Created as Default (FieldTrialBasedConfig).
605 const webrtc::AlwaysValidPointer<const FieldTrialsView, FieldTrialBasedConfig>
606 trials_;
Harald Alvestrand4da4a872020-11-04 10:34:21 +0000607 const PeerConnectionFactoryInterface::Options options_;
Karl Wiberg744310f2019-02-14 10:18:56 +0100608 PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
609 nullptr;
terelius33860252017-05-12 23:37:18 -0700610
Harald Alvestrand62166932020-10-26 08:30:41 +0000611 const bool is_unified_plan_;
612
Artem Titov880fa812021-07-30 22:30:23 +0200613 // The EventLog needs to outlive `call_` (and any other object that uses it).
Karl Wibergb03ab712019-02-14 11:59:57 +0100614 std::unique_ptr<RtcEventLog> event_log_ RTC_GUARDED_BY(worker_thread());
615
616 // Points to the same thing as `event_log_`. Since it's const, we may read the
617 // pointer (but not touch the object) from any thread.
618 RtcEventLog* const event_log_ptr_ RTC_PT_GUARDED_BY(worker_thread());
terelius33860252017-05-12 23:37:18 -0700619
Karl Wiberg8d2e2282019-02-17 13:00:07 +0100620 IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) =
621 kIceConnectionNew;
622 PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_
623 RTC_GUARDED_BY(signaling_thread()) = kIceConnectionNew;
624 PeerConnectionInterface::PeerConnectionState connection_state_
625 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionState::kNew;
Jonas Olsson635474e2018-10-18 15:58:17 +0200626
Karl Wiberg8d2e2282019-02-17 13:00:07 +0100627 IceGatheringState ice_gathering_state_ RTC_GUARDED_BY(signaling_thread()) =
628 kIceGatheringNew;
Karl Wiberg5966c502019-02-21 23:55:09 +0100629 PeerConnectionInterface::RTCConfiguration configuration_
630 RTC_GUARDED_BY(signaling_thread());
631
Harald Alvestrand0ccfbd22021-04-08 07:25:04 +0000632 const std::unique_ptr<AsyncDnsResolverFactoryInterface>
633 async_dns_resolver_factory_;
Karl Wibergfb3be392019-03-22 14:13:22 +0100634 std::unique_ptr<cricket::PortAllocator>
635 port_allocator_; // TODO(bugs.webrtc.org/9987): Accessed on both
636 // signaling and network thread.
Harald Alvestrandfd9a8f82020-10-26 14:17:02 +0000637 const std::unique_ptr<webrtc::IceTransportFactory>
Qingsi Wang25ec8882019-11-15 12:33:05 -0800638 ice_transport_factory_; // TODO(bugs.webrtc.org/9987): Accessed on the
639 // signaling thread but the underlying raw
640 // pointer is given to
Artem Titov880fa812021-07-30 22:30:23 +0200641 // `jsep_transport_controller_` and used on the
Qingsi Wang25ec8882019-11-15 12:33:05 -0800642 // network thread.
Harald Alvestrandf598e492020-11-04 05:54:10 +0000643 const std::unique_ptr<rtc::SSLCertificateVerifier> tls_cert_verifier_
644 RTC_GUARDED_BY(network_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700645
Karl Wibergac025892019-03-26 13:08:37 +0100646 // The unique_ptr belongs to the worker thread, but the Call object manages
647 // its own thread safety.
Karl Wiberg6cab5c82019-03-26 09:57:01 +0100648 std::unique_ptr<Call> call_ RTC_GUARDED_BY(worker_thread());
Tomas Gunnarsson92eebef2021-02-10 13:05:44 +0100649 ScopedTaskSafety signaling_thread_safety_;
650 rtc::scoped_refptr<PendingTaskSafetyFlag> network_thread_safety_;
Tommic3257d02021-02-10 17:40:08 +0000651 rtc::scoped_refptr<PendingTaskSafetyFlag> worker_thread_safety_;
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +0200652
Karl Wiberg6cab5c82019-03-26 09:57:01 +0100653 // Points to the same thing as `call_`. Since it's const, we may read the
Karl Wibergac025892019-03-26 13:08:37 +0100654 // pointer from any thread.
Tomas Gunnarsson1e40a0c2020-09-28 10:39:31 +0200655 // TODO(bugs.webrtc.org/11992): Remove this workaround (and potential dangling
656 // pointer).
Karl Wibergac025892019-03-26 13:08:37 +0100657 Call* const call_ptr_;
Karl Wiberg6cab5c82019-03-26 09:57:01 +0100658
659 std::unique_ptr<StatsCollector> stats_
660 RTC_GUARDED_BY(signaling_thread()); // A pointer is passed to senders_
661 rtc::scoped_refptr<RTCStatsCollector> stats_collector_
662 RTC_GUARDED_BY(signaling_thread());
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200663
Tomas Gunnarsson97a387d2021-03-29 21:04:29 +0200664 const std::string session_id_;
Steve Anton75737c02017-11-06 10:37:17 -0800665
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000666 // The transport controller is set and used on the network thread.
667 // Some functions pass the value of the transport_controller_ pointer
668 // around as arguments while running on the signaling thread; these
669 // use the transport_controller_copy.
670 std::unique_ptr<JsepTransportController> transport_controller_
671 RTC_GUARDED_BY(network_thread());
672 JsepTransportController* transport_controller_copy_
673 RTC_GUARDED_BY(signaling_thread()) = nullptr;
Steve Anton75737c02017-11-06 10:37:17 -0800674
Artem Titov880fa812021-07-30 22:30:23 +0200675 // `sctp_mid_` is the content name (MID) in SDP.
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700676 // Note: this is used as the data channel MID by both SCTP and data channel
677 // transports. It is set when either transport is initialized and unset when
678 // both transports are deleted.
Harald Alvestrand7a829a82020-02-12 07:38:21 +0100679 // There is one copy on the signaling thread and another copy on the
680 // networking thread. Changes are always initiated from the signaling
681 // thread, but applied first on the networking thread via an invoke().
682 absl::optional<std::string> sctp_mid_s_ RTC_GUARDED_BY(signaling_thread());
683 absl::optional<std::string> sctp_mid_n_ RTC_GUARDED_BY(network_thread());
Tomas Gunnarsson92eebef2021-02-10 13:05:44 +0100684 std::string sctp_transport_name_s_ RTC_GUARDED_BY(signaling_thread());
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200685
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000686 // The machinery for handling offers and answers. Const after initialization.
687 std::unique_ptr<SdpOfferAnswerHandler> sdp_handler_
Harald Alvestrand00c62ed2021-10-20 08:52:12 +0000688 RTC_GUARDED_BY(signaling_thread()) RTC_PT_GUARDED_BY(signaling_thread());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800689
Tommic3257d02021-02-10 17:40:08 +0000690 const bool dtls_enabled_;
Steve Anton75737c02017-11-06 10:37:17 -0800691
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000692 UsagePattern usage_pattern_ RTC_GUARDED_BY(signaling_thread());
Karl Wibergf73f7d62019-04-08 15:36:53 +0200693 bool return_histogram_very_quickly_ RTC_GUARDED_BY(signaling_thread()) =
694 false;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800695
Harald Alvestrand00c62ed2021-10-20 08:52:12 +0000696 // The DataChannelController is accessed from both the signaling thread
697 // and networking thread. It is a thread-aware object.
Harald Alvestrand00cf34c2019-12-02 09:56:02 +0100698 DataChannelController data_channel_controller_;
Harald Alvestrand1090e442020-10-05 07:01:09 +0000699
700 // Machinery for handling messages posted to oneself
Harald Alvestrand00c62ed2021-10-20 08:52:12 +0000701 PeerConnectionMessageHandler message_handler_
702 RTC_GUARDED_BY(signaling_thread());
Harald Alvestrande15fb152020-10-19 13:28:05 +0000703
704 // Administration of senders, receivers and transceivers
705 // Accessed on both signaling and network thread. Const after Initialize().
706 std::unique_ptr<RtpTransmissionManager> rtp_manager_;
Lahiru Ginnaliya Gamathige70f9e242021-01-27 23:32:46 -0800707
Philipp Hanckecd0373f2021-02-24 11:04:30 +0100708 // Did the connectionState ever change to `connected`?
709 // Used to gather metrics only the first such state change.
710 bool was_ever_connected_ RTC_GUARDED_BY(signaling_thread()) = false;
Harald Alvestrand00c62ed2021-10-20 08:52:12 +0000711
712 // This variable needs to be the last one in the class.
713 rtc::WeakPtrFactory<PeerConnection> weak_factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714};
715
716} // namespace webrtc
717
Steve Anton10542f22019-01-11 09:11:00 -0800718#endif // PC_PEER_CONNECTION_H_