blob: 3328a921ef7013a1de3def0d67eb569e1c8a585c [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>
Henrik Boström79b69802019-07-18 11:16:56 +020018#include <utility>
perkjd61bf802016-03-24 03:16:19 -070019#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
Bjorn A Mellemb689af42019-08-21 10:44:59 -070021#include "api/data_channel_transport_interface.h"
Piotr (Peter) Slatalae0c2e972018-10-08 09:43:21 -070022#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/peer_connection_interface.h"
24#include "api/turn_customizer.h"
25#include "pc/ice_server_parsing.h"
26#include "pc/jsep_transport_controller.h"
27#include "pc/peer_connection_factory.h"
28#include "pc/peer_connection_internal.h"
29#include "pc/rtc_stats_collector.h"
Guido Urdaneta1ff16c82019-05-20 19:31:53 +020030#include "pc/rtp_sender.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "pc/rtp_transceiver.h"
Harald Alvestrandc85328f2019-02-28 07:51:00 +010032#include "pc/sctp_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "pc/stats_collector.h"
34#include "pc/stream_collection.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "pc/webrtc_session_description_factory.h"
Jonas Olsson0182a032019-07-09 12:31:20 +020036#include "rtc_base/experiments/field_trial_parser.h"
Karl Wiberg5966c502019-02-21 23:55:09 +010037#include "rtc_base/race_checker.h"
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080038#include "rtc_base/unique_id_generator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
40namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
deadbeefeb459812015-12-15 19:24:43 -080042class MediaStreamObserver;
perkjf0dcfe22016-03-10 18:32:00 +010043class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 11:53:05 -070044class RtcEventLog;
deadbeefab9b2d12015-10-14 11:33:11 -070045
Steve Anton75737c02017-11-06 10:37:17 -080046// PeerConnection is the implementation of the PeerConnection object as defined
47// by the PeerConnectionInterface API surface.
48// The class currently is solely responsible for the following:
49// - Managing the session state machine (signaling state).
50// - Creating and initializing lower-level objects, like PortAllocator and
51// BaseChannels.
52// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
53// objects.
54// - Tracking the current and pending local/remote session descriptions.
55// The class currently is jointly responsible for the following:
56// - Parsing and interpreting SDP.
57// - Generating offers and answers based on the current state.
58// - The ICE state machine.
59// - Generating stats.
Steve Anton2d8609c2018-01-23 16:38:46 -080060class PeerConnection : public PeerConnectionInternal,
Steve Anton75737c02017-11-06 10:37:17 -080061 public DataChannelProviderInterface,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080062 public DataChannelSink,
Zhi Huang365381f2018-04-13 16:44:34 -070063 public JsepTransportController::Observer,
Guido Urdaneta1ff16c82019-05-20 19:31:53 +020064 public RtpSenderBase::SetStreamsObserver,
Steve Antonad182762018-09-05 20:22:40 +000065 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 public sigslot::has_slots<> {
67 public:
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070068 // A bit in the usage pattern is registered when its defining event occurs at
69 // least once.
Harald Alvestrand8ebba742018-05-31 14:00:34 +020070 enum class UsageEvent : int {
71 TURN_SERVER_ADDED = 0x01,
72 STUN_SERVER_ADDED = 0x02,
73 DATA_ADDED = 0x04,
74 AUDIO_ADDED = 0x08,
75 VIDEO_ADDED = 0x10,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070076 // |SetLocalDescription| returns successfully.
77 SET_LOCAL_DESCRIPTION_SUCCEEDED = 0x20,
78 // |SetRemoteDescription| returns successfully.
79 SET_REMOTE_DESCRIPTION_SUCCEEDED = 0x40,
80 // A local candidate (with type host, server-reflexive, or relay) is
81 // collected.
Harald Alvestrand8ebba742018-05-31 14:00:34 +020082 CANDIDATE_COLLECTED = 0x80,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070083 // A remote candidate is successfully added via |AddIceCandidate|.
84 ADD_ICE_CANDIDATE_SUCCEEDED = 0x100,
Harald Alvestrand8ebba742018-05-31 14:00:34 +020085 ICE_STATE_CONNECTED = 0x200,
Qingsi Wang7fc821d2018-07-12 12:54:53 -070086 CLOSE_CALLED = 0x400,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070087 // A local candidate with private IP is collected.
Harald Alvestrand056d8112018-07-16 19:18:58 +020088 PRIVATE_CANDIDATE_COLLECTED = 0x800,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070089 // A remote candidate with private IP is added, either via AddiceCandidate
90 // or from the remote description.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070091 REMOTE_PRIVATE_CANDIDATE_ADDED = 0x1000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070092 // A local mDNS candidate is collected.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070093 MDNS_CANDIDATE_COLLECTED = 0x2000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070094 // A remote mDNS candidate is added, either via AddIceCandidate or from the
95 // remote description.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070096 REMOTE_MDNS_CANDIDATE_ADDED = 0x4000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070097 // A local candidate with IPv6 address is collected.
98 IPV6_CANDIDATE_COLLECTED = 0x8000,
99 // A remote candidate with IPv6 address is added, either via AddIceCandidate
100 // or from the remote description.
101 REMOTE_IPV6_CANDIDATE_ADDED = 0x10000,
102 // A remote candidate (with type host, server-reflexive, or relay) is
103 // successfully added, either via AddIceCandidate or from the remote
104 // description.
105 REMOTE_CANDIDATE_ADDED = 0x20000,
106 MAX_VALUE = 0x40000,
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200107 };
108
zhihuang38ede132017-06-15 12:52:32 -0700109 explicit PeerConnection(PeerConnectionFactory* factory,
110 std::unique_ptr<RtcEventLog> event_log,
111 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
deadbeef653b8e02015-11-11 12:55:10 -0800113 bool Initialize(
114 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab58882018-05-02 15:12:47 -0700115 PeerConnectionDependencies dependencies);
deadbeef653b8e02015-11-11 12:55:10 -0800116
deadbeefa67696b2015-09-29 11:56:26 -0700117 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
118 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
119 bool AddStream(MediaStreamInterface* local_stream) override;
120 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
Steve Anton2d6c76a2018-01-05 17:10:52 -0800122 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -0800123 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800124 const std::vector<std::string>& stream_ids) override;
deadbeefe1f9d832016-01-14 15:35:42 -0800125 bool RemoveTrack(RtpSenderInterface* sender) override;
Steve Anton24db5732018-07-23 10:27:33 -0700126 RTCError RemoveTrackNew(
127 rtc::scoped_refptr<RtpSenderInterface> sender) override;
deadbeefe1f9d832016-01-14 15:35:42 -0800128
Steve Anton9158ef62017-11-27 13:01:52 -0800129 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
130 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
131 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
132 rtc::scoped_refptr<MediaStreamTrackInterface> track,
133 const RtpTransceiverInit& init) override;
134 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
135 cricket::MediaType media_type) override;
136 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
137 cricket::MediaType media_type,
138 const RtpTransceiverInit& init) override;
139
Steve Anton8c0f7a72017-10-03 10:03:10 -0700140 // Gets the DTLS SSL certificate associated with the audio transport on the
141 // remote side. This will become populated once the DTLS connection with the
142 // peer has been completed, as indicated by the ICE connection state
143 // transitioning to kIceConnectionCompleted.
144 // Note that this will be removed once we implement RTCDtlsTransport which
145 // has standardized method for getting this information.
146 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
147 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
148
Zhi Huang70b820f2018-01-27 14:16:15 -0800149 // Version of the above method that returns the full certificate chain.
150 std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
151
deadbeeffac06552015-11-25 11:26:01 -0800152 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800153 const std::string& kind,
154 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800155
deadbeef70ab1a12015-09-28 16:53:55 -0700156 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
157 const override;
158 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
159 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800160 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
161 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700162
deadbeefa67696b2015-09-29 11:56:26 -0700163 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700165 const DataChannelInit* config) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100166 // WARNING: LEGACY. See peerconnectioninterface.h
deadbeefa67696b2015-09-29 11:56:26 -0700167 bool GetStats(StatsObserver* observer,
168 webrtc::MediaStreamTrackInterface* track,
169 StatsOutputLevel level) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100170 // Spec-complaint GetStats(). See peerconnectioninterface.h
hbos74e1a4f2016-09-15 23:33:01 -0700171 void GetStats(RTCStatsCollectorCallback* callback) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100172 void GetStats(
173 rtc::scoped_refptr<RtpSenderInterface> selector,
174 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
175 void GetStats(
176 rtc::scoped_refptr<RtpReceiverInterface> selector,
177 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100178 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179
deadbeefa67696b2015-09-29 11:56:26 -0700180 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181
deadbeefa67696b2015-09-29 11:56:26 -0700182 IceConnectionState ice_connection_state() override;
Jonas Olsson12046902018-12-06 11:25:14 +0100183 IceConnectionState standardized_ice_connection_state() override;
Jonas Olsson635474e2018-10-18 15:58:17 +0200184 PeerConnectionState peer_connection_state() override;
deadbeefa67696b2015-09-29 11:56:26 -0700185 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
deadbeefa67696b2015-09-29 11:56:26 -0700187 const SessionDescriptionInterface* local_description() const override;
188 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800189 const SessionDescriptionInterface* current_local_description() const override;
190 const SessionDescriptionInterface* current_remote_description()
191 const override;
192 const SessionDescriptionInterface* pending_local_description() const override;
193 const SessionDescriptionInterface* pending_remote_description()
194 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195
Henrik Boström79b69802019-07-18 11:16:56 +0200196 void RestartIce() override;
197
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700199 void CreateOffer(CreateSessionDescriptionObserver* observer,
200 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800201 void CreateAnswer(CreateSessionDescriptionObserver* observer,
202 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700203 void SetLocalDescription(SetSessionDescriptionObserver* observer,
204 SessionDescriptionInterface* desc) override;
Henrik Boströma4ecf552017-11-23 14:17:07 +0000205 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
206 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 17:48:32 +0100207 void SetRemoteDescription(
208 std::unique_ptr<SessionDescriptionInterface> desc,
209 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
210 override;
deadbeef46c73892016-11-16 19:42:04 -0800211 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
deadbeefa67696b2015-09-29 11:56:26 -0700212 bool SetConfiguration(
deadbeef293e9262017-01-11 12:28:30 -0800213 const PeerConnectionInterface::RTCConfiguration& configuration,
214 RTCError* error) override;
Niels Möller2579f0c2019-08-19 09:58:17 +0200215 RTCError SetConfiguration(
216 const PeerConnectionInterface::RTCConfiguration& configuration) override;
deadbeefa67696b2015-09-29 11:56:26 -0700217 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700218 bool RemoveIceCandidates(
219 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220
Niels Möller0c4f7be2018-05-07 14:01:37 +0200221 RTCError SetBitrate(const BitrateSettings& bitrate) override;
zstein4b979802017-06-02 14:37:37 -0700222
henrika5f6bf242017-11-01 11:06:56 +0100223 void SetAudioPlayout(bool playout) override;
224 void SetAudioRecording(bool recording) override;
225
Harald Alvestrandad88c882018-11-28 16:47:46 +0100226 rtc::scoped_refptr<DtlsTransportInterface> LookupDtlsTransportByMid(
227 const std::string& mid) override;
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100228 rtc::scoped_refptr<DtlsTransport> LookupDtlsTransportByMidInternal(
229 const std::string& mid);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100230
Harald Alvestrandc85328f2019-02-28 07:51:00 +0100231 rtc::scoped_refptr<SctpTransportInterface> GetSctpTransport() const override;
232
Bjorn Tereliusde939432017-11-20 17:38:14 +0100233 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
234 int64_t output_period_ms) override;
Niels Möllerf00ca1a2019-05-10 11:33:12 +0200235 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700236 void StopRtcEventLog() override;
237
deadbeefa67696b2015-09-29 11:56:26 -0700238 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
Steve Anton2d8609c2018-01-23 16:38:46 -0800240 // PeerConnectionInternal implementation.
Karl Wiberg4ae63472019-02-22 00:57:06 +0100241 rtc::Thread* network_thread() const final {
Steve Anton2d8609c2018-01-23 16:38:46 -0800242 return factory_->network_thread();
243 }
Karl Wiberg4ae63472019-02-22 00:57:06 +0100244 rtc::Thread* worker_thread() const final { return factory_->worker_thread(); }
245 rtc::Thread* signaling_thread() const final {
Steve Anton2d8609c2018-01-23 16:38:46 -0800246 return factory_->signaling_thread();
perkjd61bf802016-03-24 03:16:19 -0700247 }
deadbeefab9b2d12015-10-14 11:33:11 -0700248
Karl Wiberga58e1692019-03-26 13:33:43 +0100249 std::string session_id() const override {
250 RTC_DCHECK_RUN_ON(signaling_thread());
251 return session_id_;
252 }
Steve Anton75737c02017-11-06 10:37:17 -0800253
Steve Anton2d8609c2018-01-23 16:38:46 -0800254 bool initial_offerer() const override {
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200255 RTC_DCHECK_RUN_ON(signaling_thread());
Zhi Huange830e682018-03-30 10:48:35 -0700256 return transport_controller_ && transport_controller_->initial_offerer();
Steve Anton2d8609c2018-01-23 16:38:46 -0800257 }
Steve Anton75737c02017-11-06 10:37:17 -0800258
Steve Anton2d8609c2018-01-23 16:38:46 -0800259 std::vector<
260 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 10:07:54 -0800261 GetTransceiversInternal() const override {
Karl Wiberga58e1692019-03-26 13:33:43 +0100262 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -0800263 return transceivers_;
264 }
265
Steve Anton2d8609c2018-01-23 16:38:46 -0800266 sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
267 return SignalDataChannelCreated_;
268 }
269
270 cricket::RtpDataChannel* rtp_data_channel() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800271 return rtp_data_channel_;
Steve Anton978b8762017-09-29 12:15:02 -0700272 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800273
Steve Antonbe5e2082018-01-24 15:29:17 -0800274 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels()
Steve Anton2d8609c2018-01-23 16:38:46 -0800275 const override {
Karl Wiberg85a4a932019-03-22 15:29:58 +0100276 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -0800277 return sctp_data_channels_;
278 }
279
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200280 absl::optional<std::string> sctp_content_name() const override {
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200281 RTC_DCHECK_RUN_ON(signaling_thread());
Zhi Huange830e682018-03-30 10:48:35 -0700282 return sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -0800283 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800284
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200285 absl::optional<std::string> sctp_transport_name() const override;
Steve Anton75737c02017-11-06 10:37:17 -0800286
Qingsi Wang72a43a12018-02-20 16:03:18 -0800287 cricket::CandidateStatsList GetPooledCandidateStats() const override;
Steve Anton5dfde182018-02-06 10:34:40 -0800288 std::map<std::string, std::string> GetTransportNamesByMid() const override;
289 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
290 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800291 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 10:37:17 -0800292
Steve Anton2d8609c2018-01-23 16:38:46 -0800293 bool GetLocalCertificate(
294 const std::string& transport_name,
295 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800296 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Steve Anton2d8609c2018-01-23 16:38:46 -0800297 const std::string& transport_name) override;
298 bool IceRestartPending(const std::string& content_name) const override;
299 bool NeedsIceRestart(const std::string& content_name) const override;
300 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Steve Anton7464fca2018-01-19 11:10:37 -0800301
Harald Alvestrand19793842018-06-25 12:03:50 +0200302 void ReturnHistogramVeryQuicklyForTesting() {
Karl Wibergf73f7d62019-04-08 15:36:53 +0200303 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand19793842018-06-25 12:03:50 +0200304 return_histogram_very_quickly_ = true;
305 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200306 void RequestUsagePatternReportForTesting();
Harald Alvestrand19793842018-06-25 12:03:50 +0200307
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700309 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310
311 private:
Henrik Boström31638672017-11-23 17:48:32 +0100312 class SetRemoteDescriptionObserverAdapter;
313 friend class SetRemoteDescriptionObserverAdapter;
Henrik Boström79b69802019-07-18 11:16:56 +0200314 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
315 // It makes the next CreateOffer() produce new ICE credentials even if
316 // RTCOfferAnswerOptions::ice_restart is false.
317 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
318 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
319 // move this type of logic to JsepTransportController/JsepTransport.
320 class LocalIceCredentialsToReplace;
Henrik Boström31638672017-11-23 17:48:32 +0100321
Steve Anton4171afb2017-11-20 10:20:22 -0800322 struct RtpSenderInfo {
323 RtpSenderInfo() : first_ssrc(0) {}
Seth Hampson845e8782018-03-02 11:34:10 -0800324 RtpSenderInfo(const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800325 const std::string sender_id,
326 uint32_t ssrc)
Seth Hampson845e8782018-03-02 11:34:10 -0800327 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
Steve Anton4171afb2017-11-20 10:20:22 -0800328 bool operator==(const RtpSenderInfo& other) {
Seth Hampson845e8782018-03-02 11:34:10 -0800329 return this->stream_id == other.stream_id &&
Steve Anton4171afb2017-11-20 10:20:22 -0800330 this->sender_id == other.sender_id &&
331 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-08 17:13:40 -0800332 }
Seth Hampson845e8782018-03-02 11:34:10 -0800333 std::string stream_id;
Steve Anton4171afb2017-11-20 10:20:22 -0800334 std::string sender_id;
335 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
336 // for communicating with the lower layers.
337 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 11:33:11 -0700338 };
deadbeefab9b2d12015-10-14 11:33:11 -0700339
Bjorn A Mellem5985a042019-06-28 14:19:38 -0700340 // Field-trial based configuration for datagram transport.
341 struct DatagramTransportConfig {
342 explicit DatagramTransportConfig(const std::string& field_trial)
343 : enabled("enabled", true), default_value("default_value", false) {
344 ParseFieldTrial({&enabled, &default_value}, field_trial);
345 }
346
347 // Whether datagram transport support is enabled at all. Defaults to true,
348 // allowing datagram transport to be used if (a) the application provides a
349 // factory for it and (b) the configuration specifies its use. This flag
350 // provides a kill-switch to force-disable datagram transport across all
351 // applications, without code changes.
352 FieldTrialFlag enabled;
353
354 // Whether the datagram transport is enabled or disabled by default.
355 // Defaults to false, meaning that applications must configure use of
356 // datagram transport through RTCConfiguration. If set to true,
357 // applications will use the datagram transport by default (but may still
358 // explicitly configure themselves not to use it through RTCConfiguration).
359 FieldTrialFlag default_value;
360 };
361
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700362 // Field-trial based configuration for datagram transport data channels.
363 struct DatagramTransportDataChannelConfig {
364 explicit DatagramTransportDataChannelConfig(const std::string& field_trial)
365 : enabled("enabled", true), default_value("default_value", false) {
366 ParseFieldTrial({&enabled, &default_value}, field_trial);
367 }
368
369 // Whether datagram transport data channel support is enabled at all.
370 // Defaults to true, allowing datagram transport to be used if (a) the
371 // application provides a factory for it and (b) the configuration specifies
372 // its use. This flag provides a kill-switch to force-disable datagram
373 // transport across all applications, without code changes.
374 FieldTrialFlag enabled;
375
376 // Whether the datagram transport data channels are enabled or disabled by
377 // default. Defaults to false, meaning that applications must configure use
378 // of datagram transport through RTCConfiguration. If set to true,
379 // applications will use the datagram transport by default (but may still
380 // explicitly configure themselves not to use it through RTCConfiguration).
381 FieldTrialFlag default_value;
382 };
383
Steve Antonad182762018-09-05 20:22:40 +0000384 // Implements MessageHandler.
385 void OnMessage(rtc::Message* msg) override;
386
Steve Antonafb0bb72018-02-20 11:35:37 -0800387 // Plan B helpers for getting the voice/video media channels for the single
388 // audio/video transceiver, if it exists.
Karl Wiberg5966c502019-02-21 23:55:09 +0100389 cricket::VoiceMediaChannel* voice_media_channel() const
390 RTC_RUN_ON(signaling_thread());
391 cricket::VideoMediaChannel* video_media_channel() const
392 RTC_RUN_ON(signaling_thread());
Steve Anton60776752018-01-10 11:51:34 -0800393
Steve Anton4171afb2017-11-20 10:20:22 -0800394 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100395 GetSendersInternal() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800396 std::vector<
397 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100398 GetReceiversInternal() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800399
400 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100401 GetAudioTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800402 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100403 GetVideoTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800404
Steve Antonafb0bb72018-02-20 11:35:37 -0800405 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100406 GetFirstAudioTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Antonafb0bb72018-02-20 11:35:37 -0800407
deadbeefab9b2d12015-10-14 11:33:11 -0700408 void CreateAudioReceiver(MediaStreamInterface* stream,
Karl Wiberg744310f2019-02-14 10:18:56 +0100409 const RtpSenderInfo& remote_sender_info)
410 RTC_RUN_ON(signaling_thread());
perkjf0dcfe22016-03-10 18:32:00 +0100411
deadbeefab9b2d12015-10-14 11:33:11 -0700412 void CreateVideoReceiver(MediaStreamInterface* stream,
Karl Wiberg744310f2019-02-14 10:18:56 +0100413 const RtpSenderInfo& remote_sender_info)
414 RTC_RUN_ON(signaling_thread());
Henrik Boström933d8b02017-10-10 10:05:16 -0700415 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Karl Wiberg5966c502019-02-21 23:55:09 +0100416 const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700417
418 // May be called either by AddStream/RemoveStream, or when a track is
419 // added/removed from a stream previously added via AddStream.
Karl Wiberg5966c502019-02-21 23:55:09 +0100420 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream)
421 RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700422 void RemoveAudioTrack(AudioTrackInterface* track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100423 MediaStreamInterface* stream)
424 RTC_RUN_ON(signaling_thread());
425 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream)
426 RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700427 void RemoveVideoTrack(VideoTrackInterface* track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100428 MediaStreamInterface* stream)
429 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430
Steve Antonf9381f02017-12-14 10:23:57 -0800431 // AddTrack implementation when Unified Plan is specified.
432 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
433 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Karl Wiberga58e1692019-03-26 13:33:43 +0100434 const std::vector<std::string>& stream_ids)
435 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800436 // AddTrack implementation when Plan B is specified.
437 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
438 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100439 const std::vector<std::string>& stream_ids)
440 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800441
442 // Returns the first RtpTransceiver suitable for a newly added track, if such
443 // transceiver is available.
444 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
445 FindFirstTransceiverForAddedTrack(
Karl Wiberga58e1692019-03-26 13:33:43 +0100446 rtc::scoped_refptr<MediaStreamTrackInterface> track)
447 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800448
Steve Antonf9381f02017-12-14 10:23:57 -0800449 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100450 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender)
451 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800452
Steve Anton22da89f2018-01-25 13:58:07 -0800453 // Internal implementation for AddTransceiver family of methods. If
454 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
Steve Anton9158ef62017-11-27 13:01:52 -0800455 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
456 cricket::MediaType media_type,
457 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 13:58:07 -0800458 const RtpTransceiverInit& init,
Karl Wiberg744310f2019-02-14 10:18:56 +0100459 bool fire_callback = true) RTC_RUN_ON(signaling_thread());
Steve Anton9158ef62017-11-27 13:01:52 -0800460
Steve Anton02ee47c2018-01-10 16:26:06 -0800461 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
462 CreateSender(cricket::MediaType media_type,
Steve Anton111fdfd2018-06-25 13:03:36 -0700463 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -0800464 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Florent Castelli892acf02018-10-01 22:47:20 +0200465 const std::vector<std::string>& stream_ids,
466 const std::vector<RtpEncodingParameters>& send_encodings);
Steve Anton02ee47c2018-01-10 16:26:06 -0800467
468 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
469 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
470
Steve Antonf9381f02017-12-14 10:23:57 -0800471 // Create a new RtpTransceiver of the given type and add it to the list of
472 // transceivers.
473 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Steve Anton02ee47c2018-01-10 16:26:06 -0800474 CreateAndAddTransceiver(
475 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
476 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100477 receiver) RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800478
Karl Wiberg744310f2019-02-14 10:18:56 +0100479 void SetIceConnectionState(IceConnectionState new_state)
480 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200481 void SetStandardizedIceConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100482 PeerConnectionInterface::IceConnectionState new_state)
483 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200484 void SetConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100485 PeerConnectionInterface::PeerConnectionState new_state)
486 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200487
Eldar Relloda13ea22019-06-01 12:23:43 +0300488 // Called any time the IceGatheringState changes.
Karl Wiberg744310f2019-02-14 10:18:56 +0100489 void OnIceGatheringChange(IceGatheringState new_state)
490 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800491 // New ICE candidate has been gathered.
Karl Wiberg744310f2019-02-14 10:18:56 +0100492 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)
493 RTC_RUN_ON(signaling_thread());
Eldar Relloda13ea22019-06-01 12:23:43 +0300494 // Gathering of an ICE candidate failed.
495 void OnIceCandidateError(const std::string& host_candidate,
496 const std::string& url,
497 int error_code,
498 const std::string& error_text)
499 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800500 // Some local ICE candidates have been removed.
Karl Wiberg744310f2019-02-14 10:18:56 +0100501 void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates)
502 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503
Alex Drake00c7ecf2019-08-06 10:54:47 -0700504 void OnSelectedCandidatePairChanged(
505 const cricket::CandidatePairChangeEvent& event)
506 RTC_RUN_ON(signaling_thread());
507
Steve Antonba818672017-11-06 10:21:57 -0800508 // Update the state, signaling if necessary.
Karl Wiberg744310f2019-02-14 10:18:56 +0100509 void ChangeSignalingState(SignalingState signaling_state)
510 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
deadbeefeb459812015-12-15 19:24:43 -0800512 // Signals from MediaStreamObserver.
513 void OnAudioTrackAdded(AudioTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100514 MediaStreamInterface* stream)
515 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800516 void OnAudioTrackRemoved(AudioTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100517 MediaStreamInterface* stream)
518 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800519 void OnVideoTrackAdded(VideoTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100520 MediaStreamInterface* stream)
521 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800522 void OnVideoTrackRemoved(VideoTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100523 MediaStreamInterface* stream)
524 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800525
Henrik Boström31638672017-11-23 17:48:32 +0100526 void PostSetSessionDescriptionSuccess(
527 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
Steve Antonad182762018-09-05 20:22:40 +0000529 RTCError&& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700530 void PostCreateSessionDescriptionFailure(
531 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100532 RTCError error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533
Steve Anton8a006912017-12-04 15:25:56 -0800534 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
535 // that return an RTCError instead of invoking a callback.
536 RTCError ApplyLocalDescription(
537 std::unique_ptr<SessionDescriptionInterface> desc);
538 RTCError ApplyRemoteDescription(
539 std::unique_ptr<SessionDescriptionInterface> desc);
540
Steve Antondcc3c022017-12-22 16:02:54 -0800541 // Updates the local RtpTransceivers according to the JSEP rules. Called as
542 // part of setting the local/remote description.
543 RTCError UpdateTransceiversAndDataChannels(
544 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800545 const SessionDescriptionInterface& new_session,
546 const SessionDescriptionInterface* old_local_description,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100547 const SessionDescriptionInterface* old_remote_description)
548 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800549
550 // Either creates or destroys the transceiver's BaseChannel according to the
551 // given media section.
552 RTCError UpdateTransceiverChannel(
553 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
554 transceiver,
555 const cricket::ContentInfo& content,
Karl Wiberg5966c502019-02-21 23:55:09 +0100556 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800557
Steve Antonfa2260d2017-12-28 16:38:23 -0800558 // Either creates or destroys the local data channel according to the given
559 // media section.
560 RTCError UpdateDataChannel(cricket::ContentSource source,
561 const cricket::ContentInfo& content,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100562 const cricket::ContentGroup* bundle_group)
563 RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800564
Steve Antondcc3c022017-12-22 16:02:54 -0800565 // Associate the given transceiver according to the JSEP rules.
566 RTCErrorOr<
567 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
568 AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800569 SdpType type,
Steve Antondcc3c022017-12-22 16:02:54 -0800570 size_t mline_index,
571 const cricket::ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800572 const cricket::ContentInfo* old_local_content,
Karl Wiberg5966c502019-02-21 23:55:09 +0100573 const cricket::ContentInfo* old_remote_content)
574 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800575
576 // Returns the RtpTransceiver, if found, that is associated to the given MID.
577 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100578 GetAssociatedTransceiver(const std::string& mid) const
579 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800580
581 // Returns the RtpTransceiver, if found, that was assigned to the given mline
582 // index in CreateOffer.
583 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100584 GetTransceiverByMLineIndex(size_t mline_index) const
585 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800586
587 // Returns an RtpTransciever, if available, that can be used to receive the
588 // given media type according to JSEP rules.
589 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100590 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const
591 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800592
Steve Antoned10bd92017-12-05 10:52:59 -0800593 // Returns the media section in the given session description that is
594 // associated with the RtpTransceiver. Returns null if none found or this
595 // RtpTransceiver is not associated. Logic varies depending on the
596 // SdpSemantics specified in the configuration.
597 const cricket::ContentInfo* FindMediaSectionForTransceiver(
598 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
599 transceiver,
Karl Wiberg5966c502019-02-21 23:55:09 +0100600 const SessionDescriptionInterface* sdesc) const
601 RTC_RUN_ON(signaling_thread());
Steve Antoned10bd92017-12-05 10:52:59 -0800602
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100603 // Runs the algorithm **set the associated remote streams** specified in
604 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
605 void SetAssociatedRemoteStreams(
606 rtc::scoped_refptr<RtpReceiverInternal> receiver,
607 const std::vector<std::string>& stream_ids,
608 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100609 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
610 RTC_RUN_ON(signaling_thread());
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100611
Steve Anton0f5400a2018-07-17 14:25:36 -0700612 // Runs the algorithm **process the removal of a remote track** specified in
613 // the WebRTC specification.
614 // This method will update the following lists:
615 // |remove_list| is the list of transceivers for which the receiving track is
616 // being removed.
617 // |removed_streams| is the list of streams which no longer have a receiving
618 // track so should be removed.
619 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
620 void ProcessRemovalOfRemoteTrack(
621 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
622 transceiver,
623 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100624 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
625 RTC_RUN_ON(signaling_thread());
Steve Anton0f5400a2018-07-17 14:25:36 -0700626
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100627 void RemoveRemoteStreamsIfEmpty(
628 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
629 remote_streams,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100630 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
631 RTC_RUN_ON(signaling_thread());
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100632
Steve Anton52d86772018-02-20 15:48:12 -0800633 void OnNegotiationNeeded();
634
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 bool IsClosed() const {
Karl Wiberg8d2e2282019-02-17 13:00:07 +0100636 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 return signaling_state_ == PeerConnectionInterface::kClosed;
638 }
639
deadbeefab9b2d12015-10-14 11:33:11 -0700640 // Returns a MediaSessionOptions struct with options decided by |options|,
641 // the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800642 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
643 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100644 cricket::MediaSessionOptions* session_options)
645 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800646 void GetOptionsForPlanBOffer(
647 const PeerConnectionInterface::RTCOfferAnswerOptions&
648 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100649 cricket::MediaSessionOptions* session_options)
650 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800651 void GetOptionsForUnifiedPlanOffer(
652 const PeerConnectionInterface::RTCOfferAnswerOptions&
653 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100654 cricket::MediaSessionOptions* session_options)
655 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700656
Karl Wiberg5966c502019-02-21 23:55:09 +0100657 RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options)
658 RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800659 void RemoveRecvDirectionFromReceivingTransceiversOfType(
Karl Wiberga58e1692019-03-26 13:33:43 +0100660 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800661 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
662 std::vector<
663 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100664 GetReceivingTransceiversOfType(cricket::MediaType media_type)
665 RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800666
deadbeefab9b2d12015-10-14 11:33:11 -0700667 // Returns a MediaSessionOptions struct with options decided by
668 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800669 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100670 cricket::MediaSessionOptions* session_options)
671 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800672 void GetOptionsForPlanBAnswer(
673 const PeerConnectionInterface::RTCOfferAnswerOptions&
674 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100675 cricket::MediaSessionOptions* session_options)
676 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800677 void GetOptionsForUnifiedPlanAnswer(
678 const PeerConnectionInterface::RTCOfferAnswerOptions&
679 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100680 cricket::MediaSessionOptions* session_options)
681 RTC_RUN_ON(signaling_thread());
htaa2a49d92016-03-04 02:51:39 -0800682
zhihuang1c378ed2017-08-17 14:10:50 -0700683 // Generates MediaDescriptionOptions for the |session_opts| based on existing
684 // local description or remote description.
685 void GenerateMediaDescriptionOptions(
686 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -0800687 RtpTransceiverDirection audio_direction,
688 RtpTransceiverDirection video_direction,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200689 absl::optional<size_t>* audio_index,
690 absl::optional<size_t>* video_index,
691 absl::optional<size_t>* data_index,
Karl Wiberg85a4a932019-03-22 15:29:58 +0100692 cricket::MediaSessionOptions* session_options)
693 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700694
Steve Antonfa2260d2017-12-28 16:38:23 -0800695 // Generates the active MediaDescriptionOptions for the local data channel
696 // given the specified MID.
697 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
Karl Wiberg85a4a932019-03-22 15:29:58 +0100698 const std::string& mid) const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800699
700 // Generates the rejected MediaDescriptionOptions for the local data channel
701 // given the specified MID.
702 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
Karl Wiberg85a4a932019-03-22 15:29:58 +0100703 const std::string& mid) const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800704
705 // Returns the MID for the data section associated with either the
706 // RtpDataChannel or SCTP data channel, if it has been set. If no data
707 // channels are configured this will return nullopt.
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200708 absl::optional<std::string> GetDataMid() const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800709
Steve Anton4171afb2017-11-20 10:20:22 -0800710 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 15:33:07 -0800711 // Called when a media type is rejected (m-line set to port 0).
Karl Wiberg744310f2019-02-14 10:18:56 +0100712 void RemoveSenders(cricket::MediaType media_type)
713 RTC_RUN_ON(signaling_thread());
deadbeeffaac4972015-11-12 15:33:07 -0800714
deadbeefbda7e0b2015-12-08 17:13:40 -0800715 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
716 // and existing MediaStreamTracks are removed if there is no corresponding
717 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
718 // is created if it doesn't exist; if false, it's removed if it exists.
719 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700720 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 10:20:22 -0800721 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -0700722 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800723 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700724 cricket::MediaType media_type,
Karl Wiberg744310f2019-02-14 10:18:56 +0100725 StreamCollection* new_streams) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700726
Steve Anton4171afb2017-11-20 10:20:22 -0800727 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700728 // session description. It creates a remote MediaStreamTrackInterface
729 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800730 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
Karl Wiberg744310f2019-02-14 10:18:56 +0100731 cricket::MediaType media_type)
732 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700733
Steve Anton4171afb2017-11-20 10:20:22 -0800734 // Triggered when a remote sender has been removed from a remote session
735 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700736 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800737 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
Karl Wiberg744310f2019-02-14 10:18:56 +0100738 cricket::MediaType media_type)
739 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700740
741 // Finds remote MediaStreams without any tracks and removes them from
742 // |remote_streams_| and notifies the observer that the MediaStreams no longer
743 // exist.
Karl Wiberg744310f2019-02-14 10:18:56 +0100744 void UpdateEndedRemoteMediaStreams() RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700745
deadbeefab9b2d12015-10-14 11:33:11 -0700746 // Loops through the vector of |streams| and finds added and removed
747 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 10:20:22 -0800748 // For each new or removed StreamParam, OnLocalSenderSeen or
749 // OnLocalSenderRemoved is invoked.
750 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
Karl Wiberg5966c502019-02-21 23:55:09 +0100751 cricket::MediaType media_type)
752 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700753
Steve Anton4171afb2017-11-20 10:20:22 -0800754 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 11:33:11 -0700755 // session description.
756 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
757 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
758 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 10:20:22 -0800759 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
Karl Wiberg5966c502019-02-21 23:55:09 +0100760 cricket::MediaType media_type)
761 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700762
Steve Anton4171afb2017-11-20 10:20:22 -0800763 // Triggered when a local sender has been removed from a local session
deadbeefab9b2d12015-10-14 11:33:11 -0700764 // description.
765 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
766 // has been removed from the local SessionDescription and the stream can be
767 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 10:20:22 -0800768 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
Karl Wiberga58e1692019-03-26 13:33:43 +0100769 cricket::MediaType media_type)
770 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700771
Karl Wiberg85a4a932019-03-22 15:29:58 +0100772 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams)
773 RTC_RUN_ON(signaling_thread());
Karl Wiberg106d92d2019-02-14 10:17:47 +0100774 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams)
775 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700776 void UpdateClosingRtpDataChannels(
777 const std::vector<std::string>& active_channels,
Karl Wiberg85a4a932019-03-22 15:29:58 +0100778 bool is_local_update) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700779 void CreateRemoteRtpDataChannel(const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100780 uint32_t remote_ssrc)
781 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700782
783 // Creates channel and adds it to the collection of DataChannels that will
784 // be offered in a SessionDescription.
785 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
786 const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100787 const InternalDataChannelInit* config) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700788
789 // Checks if any data channel has been added.
Karl Wiberg85a4a932019-03-22 15:29:58 +0100790 bool HasDataChannels() const RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700791
Karl Wiberg85a4a932019-03-22 15:29:58 +0100792 void AllocateSctpSids(rtc::SSLRole role) RTC_RUN_ON(signaling_thread());
793 void OnSctpDataChannelClosed(DataChannel* channel)
794 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700795
Karl Wiberg85a4a932019-03-22 15:29:58 +0100796 void OnDataChannelDestroyed() RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800797 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700798 void OnDataChannelOpenMessage(const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100799 const InternalDataChannelInit& config)
800 RTC_RUN_ON(signaling_thread());
801
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800802 // Parses and handles open messages. Returns true if the message is an open
803 // message, false otherwise.
804 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
805 const rtc::CopyOnWriteBuffer& buffer)
806 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700807
Steve Anton4171afb2017-11-20 10:20:22 -0800808 // Returns true if the PeerConnection is configured to use Unified Plan
809 // semantics for creating offers/answers and setting local/remote
810 // descriptions. If this is true the RtpTransceiver API will also be available
811 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 10:25:56 -0800812 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
813 // sufficient time has passed.
Karl Wiberg5966c502019-02-21 23:55:09 +0100814 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread()) {
Steve Anton79e79602017-11-20 10:25:56 -0800815 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
816 }
Steve Anton4171afb2017-11-20 10:20:22 -0800817
Steve Anton06817cd2018-12-18 15:55:30 -0800818 // The offer/answer machinery assumes the media section MID is present and
819 // unique. To support legacy end points that do not supply a=mid lines, this
820 // method will modify the session description to add MIDs generated according
821 // to the SDP semantics.
Karl Wiberg5966c502019-02-21 23:55:09 +0100822 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description)
823 RTC_RUN_ON(signaling_thread());
Steve Anton06817cd2018-12-18 15:55:30 -0800824
Steve Anton4171afb2017-11-20 10:20:22 -0800825 // Is there an RtpSender of the given type?
Karl Wiberg5966c502019-02-21 23:55:09 +0100826 bool HasRtpSender(cricket::MediaType type) const
827 RTC_RUN_ON(signaling_thread());
deadbeeffac06552015-11-25 11:26:01 -0800828
Steve Anton4171afb2017-11-20 10:20:22 -0800829 // Return the RtpSender with the given track attached.
830 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100831 FindSenderForTrack(MediaStreamTrackInterface* track) const
832 RTC_RUN_ON(signaling_thread());
deadbeef70ab1a12015-09-28 16:53:55 -0700833
Steve Anton4171afb2017-11-20 10:20:22 -0800834 // Return the RtpSender with the given id, or null if none exists.
835 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100836 FindSenderById(const std::string& sender_id) const
837 RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800838
839 // Return the RtpReceiver with the given id, or null if none exists.
840 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100841 FindReceiverById(const std::string& receiver_id) const
842 RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800843
844 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
845 cricket::MediaType media_type);
846 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
847 cricket::MediaType media_type);
848 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
Emircan Uysalerbc609ea2018-03-27 21:57:18 +0000849 const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800850 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 11:33:11 -0700851
852 // Returns the specified SCTP DataChannel in sctp_data_channels_,
853 // or nullptr if not found.
Karl Wiberg85a4a932019-03-22 15:29:58 +0100854 DataChannel* FindDataChannelBySid(int sid) const
855 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700856
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700857 // Called when first configuring the port allocator.
Karl Wibergfb3be392019-03-22 14:13:22 +0100858 struct InitializePortAllocatorResult {
859 bool enable_ipv6;
860 };
861 InitializePortAllocatorResult InitializePortAllocator_n(
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200862 const cricket::ServerAddresses& stun_servers,
863 const std::vector<cricket::RelayServerConfig>& turn_servers,
864 const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800865 // Called when SetConfiguration is called to apply the supported subset
866 // of the configuration on the network thread.
867 bool ReconfigurePortAllocator_n(
868 const cricket::ServerAddresses& stun_servers,
869 const std::vector<cricket::RelayServerConfig>& turn_servers,
870 IceTransportsType type,
871 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200872 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800873 webrtc::TurnCustomizer* turn_customizer,
Karl Wiberg739506e2019-04-03 11:37:28 +0200874 absl::optional<int> stun_candidate_keepalive_interval,
875 bool have_local_description);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700876
Elad Alon99c3fe52017-10-13 16:29:40 +0200877 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700878 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100879 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
880 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200881
Elad Alonacb24172017-10-06 14:32:13 +0200882 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700883 // This function should only be called from the worker thread.
884 void StopRtcEventLog_w();
885
Steve Anton038834f2017-07-14 15:59:59 -0700886 // Ensures the configuration doesn't have any parameters with invalid values,
887 // or values that conflict with other parameters.
888 //
889 // Returns RTCError::OK() if there are no issues.
890 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
891
Steve Antonba818672017-11-06 10:21:57 -0800892 cricket::ChannelManager* channel_manager() const;
Steve Antonba818672017-11-06 10:21:57 -0800893
Steve Antonf8470812017-12-04 10:46:21 -0800894 enum class SessionError {
895 kNone, // No error.
896 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
897 kTransport, // Error from the underlying transport.
898 };
899
Steve Anton75737c02017-11-06 10:37:17 -0800900 // Returns the last error in the session. See the enum above for details.
Karl Wiberga58e1692019-03-26 13:33:43 +0100901 SessionError session_error() const RTC_RUN_ON(signaling_thread()) {
902 return session_error_;
903 }
Steve Antonf8470812017-12-04 10:46:21 -0800904 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 10:37:17 -0800905
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800906 cricket::ChannelInterface* GetChannel(const std::string& content_name);
Steve Anton75737c02017-11-06 10:37:17 -0800907
908 // Get current SSL role used by SCTP's underlying transport.
909 bool GetSctpSslRole(rtc::SSLRole* role);
910
Steve Anton75737c02017-11-06 10:37:17 -0800911 cricket::IceConfig ParseIceConfig(
912 const PeerConnectionInterface::RTCConfiguration& config) const;
913
Steve Anton75737c02017-11-06 10:37:17 -0800914 // Implements DataChannelProviderInterface.
915 bool SendData(const cricket::SendDataParams& params,
916 const rtc::CopyOnWriteBuffer& payload,
917 cricket::SendDataResult* result) override;
918 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
919 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
920 void AddSctpDataStream(int sid) override;
921 void RemoveSctpDataStream(int sid) override;
922 bool ReadyToSendData() const override;
923
924 cricket::DataChannelType data_channel_type() const;
925
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800926 // Implements DataChannelSink.
927 void OnDataReceived(int channel_id,
928 DataMessageType type,
929 const rtc::CopyOnWriteBuffer& buffer) override;
930 void OnChannelClosing(int channel_id) override;
931 void OnChannelClosed(int channel_id) override;
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700932 void OnReadyToSend() override;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800933
Steve Anton75737c02017-11-06 10:37:17 -0800934 // Called when an RTCCertificate is generated or retrieved by
935 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
936 void OnCertificateReady(
937 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
938 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
939
Steve Anton75737c02017-11-06 10:37:17 -0800940 // Non-const versions of local_description()/remote_description(), for use
941 // internally.
Karl Wiberg739506e2019-04-03 11:37:28 +0200942 SessionDescriptionInterface* mutable_local_description()
943 RTC_RUN_ON(signaling_thread()) {
Steve Anton75737c02017-11-06 10:37:17 -0800944 return pending_local_description_ ? pending_local_description_.get()
945 : current_local_description_.get();
946 }
Karl Wiberg739506e2019-04-03 11:37:28 +0200947 SessionDescriptionInterface* mutable_remote_description()
948 RTC_RUN_ON(signaling_thread()) {
Steve Anton75737c02017-11-06 10:37:17 -0800949 return pending_remote_description_ ? pending_remote_description_.get()
950 : current_remote_description_.get();
951 }
952
953 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 10:46:21 -0800954 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 10:37:17 -0800955
Zhi Huange830e682018-03-30 10:48:35 -0700956 RTCError UpdateSessionState(SdpType type,
957 cricket::ContentSource source,
958 const cricket::SessionDescription* description);
Steve Anton75737c02017-11-06 10:37:17 -0800959 // Push the media parts of the local or remote session description
960 // down to all of the channels.
Karl Wiberg5966c502019-02-21 23:55:09 +0100961 RTCError PushdownMediaDescription(SdpType type, cricket::ContentSource source)
962 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800963
Steve Anton8a006912017-12-04 15:25:56 -0800964 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -0800965 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800966
967 // Returns true and the TransportInfo of the given |content_name|
968 // from |description|. Returns false if it's not available.
969 static bool GetTransportDescription(
970 const cricket::SessionDescription* description,
971 const std::string& content_name,
972 cricket::TransportDescription* info);
973
Steve Anton75737c02017-11-06 10:37:17 -0800974 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 10:52:59 -0800975 // This enables media to flow on all configured audio/video channels and the
976 // RtpDataChannel.
Karl Wiberga58e1692019-03-26 13:33:43 +0100977 void EnableSending() RTC_RUN_ON(signaling_thread());
Steve Anton3fe1b152017-12-12 10:20:08 -0800978
Steve Anton8af21862017-12-15 11:20:13 -0800979 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
Karl Wiberg85a4a932019-03-22 15:29:58 +0100980 void DestroyAllChannels() RTC_RUN_ON(signaling_thread());
Steve Anton3fe1b152017-12-12 10:20:08 -0800981
Steve Anton75737c02017-11-06 10:37:17 -0800982 // Returns the media index for a local ice candidate given the content name.
983 // Returns false if the local session description does not have a media
984 // content called |content_name|.
985 bool GetLocalCandidateMediaIndex(const std::string& content_name,
Karl Wiberg739506e2019-04-03 11:37:28 +0200986 int* sdp_mline_index)
987 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800988 // Uses all remote candidates in |remote_desc| in this session.
989 bool UseCandidatesInSessionDescription(
Karl Wiberg744310f2019-02-14 10:18:56 +0100990 const SessionDescriptionInterface* remote_desc)
991 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800992 // Uses |candidate| in this session.
Karl Wiberg744310f2019-02-14 10:18:56 +0100993 bool UseCandidate(const IceCandidateInterface* candidate)
994 RTC_RUN_ON(signaling_thread());
Guido Urdaneta41633172019-05-23 20:12:29 +0200995 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
996 const SessionDescriptionInterface* description,
997 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800998 // Deletes the corresponding channel of contents that don't exist in |desc|.
999 // |desc| can be null. This means that all channels are deleted.
Karl Wiberg5966c502019-02-21 23:55:09 +01001000 void RemoveUnusedChannels(const cricket::SessionDescription* desc)
1001 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001002
1003 // Allocates media channels based on the |desc|. If |desc| doesn't have
1004 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
1005 // This method will also delete any existing media channels before creating.
Karl Wiberg5966c502019-02-21 23:55:09 +01001006 RTCError CreateChannels(const cricket::SessionDescription& desc)
1007 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -08001008
1009 // If the BUNDLE policy is max-bundle, then we know for sure that all
1010 // transports will be bundled from the start. This method returns the BUNDLE
1011 // group if that's the case, or null if BUNDLE will be negotiated later. An
1012 // error is returned if max-bundle is specified but the session description
1013 // does not have a BUNDLE group.
1014 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
Karl Wiberg5966c502019-02-21 23:55:09 +01001015 const cricket::SessionDescription& desc) const
1016 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001017
1018 // Helper methods to create media channels.
Karl Wiberg5966c502019-02-21 23:55:09 +01001019 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid)
1020 RTC_RUN_ON(signaling_thread());
1021 cricket::VideoChannel* CreateVideoChannel(const std::string& mid)
1022 RTC_RUN_ON(signaling_thread());
1023 bool CreateDataChannel(const std::string& mid) RTC_RUN_ON(signaling_thread());
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001024 bool CreateSctpDataChannel(const std::string& mid)
1025 RTC_RUN_ON(signaling_thread());
1026 bool SetupDataChannelTransport(const std::string& mid)
1027 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001028
Zhi Huange830e682018-03-30 10:48:35 -07001029 bool CreateSctpTransport_n(const std::string& mid);
Steve Anton75737c02017-11-06 10:37:17 -08001030 // For bundling.
Steve Anton75737c02017-11-06 10:37:17 -08001031 void DestroySctpTransport_n();
1032 // SctpTransport signal handlers. Needed to marshal signals from the network
1033 // to signaling thread.
1034 void OnSctpTransportReadyToSendData_n();
1035 // This may be called with "false" if the direction of the m= section causes
1036 // us to tear down the SCTP connection.
1037 void OnSctpTransportReadyToSendData_s(bool ready);
1038 void OnSctpTransportDataReceived_n(const cricket::ReceiveDataParams& params,
1039 const rtc::CopyOnWriteBuffer& payload);
1040 // Beyond just firing the signal to the signaling thread, listens to SCTP
1041 // CONTROL messages on unused SIDs and processes them as OPEN messages.
1042 void OnSctpTransportDataReceived_s(const cricket::ReceiveDataParams& params,
1043 const rtc::CopyOnWriteBuffer& payload);
Taylor Brandstettercdd05f02018-05-31 13:23:32 -07001044 void OnSctpClosingProcedureStartedRemotely_n(int sid);
1045 void OnSctpClosingProcedureComplete_n(int sid);
Steve Anton75737c02017-11-06 10:37:17 -08001046
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001047 bool SetupDataChannelTransport_n(const std::string& mid)
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001048 RTC_RUN_ON(network_thread());
1049 void OnMediaTransportStateChanged_n() RTC_RUN_ON(network_thread());
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001050 void TeardownDataChannelTransport_n() RTC_RUN_ON(network_thread());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001051
Steve Anton75737c02017-11-06 10:37:17 -08001052 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
1053 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
1054 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 15:25:56 -08001055 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
Karl Wiberg5966c502019-02-21 23:55:09 +01001056 cricket::ContentSource source)
1057 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001058
Steve Anton3828c062017-12-06 10:34:51 -08001059 // Check if a call to SetLocalDescription is acceptable with a session
1060 // description of the given type.
1061 bool ExpectSetLocalDescription(SdpType type);
1062 // Check if a call to SetRemoteDescription is acceptable with a session
1063 // description of the given type.
1064 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -08001065 // Verifies a=setup attribute as per RFC 5763.
1066 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 10:34:51 -08001067 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -08001068
1069 // Returns true if we are ready to push down the remote candidate.
1070 // |remote_desc| is the new remote description, or NULL if the current remote
1071 // description should be used. Output |valid| is true if the candidate media
1072 // index is valid.
1073 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
1074 const SessionDescriptionInterface* remote_desc,
Karl Wiberga58e1692019-03-26 13:33:43 +01001075 bool* valid) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001076
1077 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
1078 // this session.
Karl Wiberg739506e2019-04-03 11:37:28 +02001079 bool SrtpRequired() const RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001080
Zhi Huange830e682018-03-30 10:48:35 -07001081 // JsepTransportController signal handlers.
Karl Wiberg744310f2019-02-14 10:18:56 +01001082 void OnTransportControllerConnectionState(cricket::IceConnectionState state)
1083 RTC_RUN_ON(signaling_thread());
1084 void OnTransportControllerGatheringState(cricket::IceGatheringState state)
1085 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001086 void OnTransportControllerCandidatesGathered(
1087 const std::string& transport_name,
Karl Wiberg744310f2019-02-14 10:18:56 +01001088 const std::vector<cricket::Candidate>& candidates)
1089 RTC_RUN_ON(signaling_thread());
Eldar Relloda13ea22019-06-01 12:23:43 +03001090 void OnTransportControllerCandidateError(
1091 const cricket::IceCandidateErrorEvent& event)
1092 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001093 void OnTransportControllerCandidatesRemoved(
Karl Wiberg744310f2019-02-14 10:18:56 +01001094 const std::vector<cricket::Candidate>& candidates)
1095 RTC_RUN_ON(signaling_thread());
Alex Drake00c7ecf2019-08-06 10:54:47 -07001096 void OnTransportControllerCandidateChanged(
1097 const cricket::CandidatePairChangeEvent& event)
1098 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001099 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
1100
Steve Antonf8470812017-12-04 10:46:21 -08001101 const char* SessionErrorToString(SessionError error) const;
Karl Wiberga58e1692019-03-26 13:33:43 +01001102 std::string GetSessionErrorMsg() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001103
Steve Anton8e20f172018-03-06 10:55:04 -08001104 // Report the UMA metric SdpFormatReceived for the given remote offer.
1105 void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
1106
Steve Anton0ffaaa22018-02-23 10:31:30 -08001107 // Report inferred negotiated SDP semantics from a local/remote answer to the
1108 // UMA observer.
1109 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
1110
Steve Anton75737c02017-11-06 10:37:17 -08001111 // Invoked when TransportController connection completion is signaled.
1112 // Reports stats for all transports in use.
Karl Wiberga58e1692019-03-26 13:33:43 +01001113 void ReportTransportStats() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001114
1115 // Gather the usage of IPv4/IPv6 as best connection.
1116 void ReportBestConnectionState(const cricket::TransportStats& stats);
1117
Steve Antonc7b964c2018-02-01 14:39:45 -08001118 void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
Karl Wiberg739506e2019-04-03 11:37:28 +02001119 const std::set<cricket::MediaType>& media_types)
1120 RTC_RUN_ON(signaling_thread());
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07001121 void ReportIceCandidateCollected(const cricket::Candidate& candidate)
1122 RTC_RUN_ON(signaling_thread());
1123 void ReportRemoteIceCandidateAdded(const cricket::Candidate& candidate)
1124 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001125
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001126 void NoteUsageEvent(UsageEvent event);
Karl Wiberg744310f2019-02-14 10:18:56 +01001127 void ReportUsagePattern() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001128
Steve Anton75737c02017-11-06 10:37:17 -08001129 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
1130
Karl Wiberga58e1692019-03-26 13:33:43 +01001131 const std::string GetTransportName(const std::string& content_name)
1132 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001133
Steve Anton6fec8802017-12-04 10:37:29 -08001134 // Destroys and clears the BaseChannel associated with the given transceiver,
1135 // if such channel is set.
1136 void DestroyTransceiverChannel(
1137 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1138 transceiver);
1139
1140 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Karl Wiberg85a4a932019-03-22 15:29:58 +01001141 void DestroyDataChannel() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001142
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001143 // Destroys the given ChannelInterface.
1144 // The channel cannot be accessed after this method is called.
1145 void DestroyChannelInterface(cricket::ChannelInterface* channel);
Steve Anton6fec8802017-12-04 10:37:29 -08001146
Zhi Huang365381f2018-04-13 16:44:34 -07001147 // JsepTransportController::Observer override.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07001148 //
1149 // Called by |transport_controller_| when processing transport information
1150 // from a session description, and the mapping from m= sections to transports
1151 // changed (as a result of BUNDLE negotiation, or m= sections being
1152 // rejected).
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001153 bool OnTransportChanged(
1154 const std::string& mid,
1155 RtpTransportInternal* rtp_transport,
1156 rtc::scoped_refptr<DtlsTransport> dtls_transport,
1157 MediaTransportInterface* media_transport,
1158 DataChannelTransportInterface* data_channel_transport,
1159 JsepTransportController::NegotiationState negotiation_state) override;
Zhi Huange830e682018-03-30 10:48:35 -07001160
Guido Urdaneta1ff16c82019-05-20 19:31:53 +02001161 // RtpSenderBase::SetStreamsObserver override.
1162 void OnSetStreams() override;
1163
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02001164 // Returns the observer. Will crash on CHECK if the observer is removed.
Karl Wiberg744310f2019-02-14 10:18:56 +01001165 PeerConnectionObserver* Observer() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02001166
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001167 // Returns the CryptoOptions for this PeerConnection. This will always
1168 // return the RTCConfiguration.crypto_options if set and will only default
1169 // back to the PeerConnectionFactory settings if nothing was set.
Karl Wiberg5966c502019-02-21 23:55:09 +01001170 CryptoOptions GetCryptoOptions() RTC_RUN_ON(signaling_thread());
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001171
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001172 // Returns rtp transport, result can not be nullptr.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001173 RtpTransportInternal* GetRtpTransport(const std::string& mid)
1174 RTC_RUN_ON(signaling_thread()) {
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001175 auto rtp_transport = transport_controller_->GetRtpTransport(mid);
1176 RTC_DCHECK(rtp_transport);
1177 return rtp_transport;
1178 }
1179
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001180 void UpdateNegotiationNeeded();
1181 bool CheckIfNegotiationIsNeeded();
1182
Karl Wiberg106d92d2019-02-14 10:17:47 +01001183 sigslot::signal1<DataChannel*> SignalDataChannelCreated_
1184 RTC_GUARDED_BY(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -08001185
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 // Storing the factory as a scoped reference pointer ensures that the memory
1187 // in the PeerConnectionFactoryImpl remains available as long as the
1188 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
1189 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -07001190 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 // will refer to the same reference count.
Karl Wiberg744310f2019-02-14 10:18:56 +01001192 const rtc::scoped_refptr<PeerConnectionFactory> factory_;
1193 PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
1194 nullptr;
terelius33860252017-05-12 23:37:18 -07001195
1196 // The EventLog needs to outlive |call_| (and any other object that uses it).
Karl Wibergb03ab712019-02-14 11:59:57 +01001197 std::unique_ptr<RtcEventLog> event_log_ RTC_GUARDED_BY(worker_thread());
1198
1199 // Points to the same thing as `event_log_`. Since it's const, we may read the
1200 // pointer (but not touch the object) from any thread.
1201 RtcEventLog* const event_log_ptr_ RTC_PT_GUARDED_BY(worker_thread());
terelius33860252017-05-12 23:37:18 -07001202
Karl Wiberg8d2e2282019-02-17 13:00:07 +01001203 SignalingState signaling_state_ RTC_GUARDED_BY(signaling_thread()) = kStable;
1204 IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) =
1205 kIceConnectionNew;
1206 PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_
1207 RTC_GUARDED_BY(signaling_thread()) = kIceConnectionNew;
1208 PeerConnectionInterface::PeerConnectionState connection_state_
1209 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionState::kNew;
Jonas Olsson635474e2018-10-18 15:58:17 +02001210
Karl Wiberg8d2e2282019-02-17 13:00:07 +01001211 IceGatheringState ice_gathering_state_ RTC_GUARDED_BY(signaling_thread()) =
1212 kIceGatheringNew;
Karl Wiberg5966c502019-02-21 23:55:09 +01001213 PeerConnectionInterface::RTCConfiguration configuration_
1214 RTC_GUARDED_BY(signaling_thread());
1215
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001216 // Field-trial based configuration for datagram transport.
1217 const DatagramTransportConfig datagram_transport_config_;
1218
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001219 // Field-trial based configuration for datagram transport data channels.
1220 const DatagramTransportConfig datagram_transport_data_channel_config_;
1221
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001222 // Final, resolved value for whether datagram transport is in use.
1223 bool use_datagram_transport_ RTC_GUARDED_BY(signaling_thread()) = false;
1224
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001225 // Equivalent of |use_datagram_transport_|, but for its use with data
1226 // channels.
1227 bool use_datagram_transport_for_data_channels_
1228 RTC_GUARDED_BY(signaling_thread()) = false;
1229
Karl Wiberg5966c502019-02-21 23:55:09 +01001230 // Cache configuration_.use_media_transport so that we can access it from
1231 // other threads.
1232 // TODO(bugs.webrtc.org/9987): Caching just this bool and allowing the data
1233 // it's derived from to change is not necessarily sound. Stop doing it.
1234 rtc::RaceChecker use_media_transport_race_checker_;
1235 bool use_media_transport_ RTC_GUARDED_BY(use_media_transport_race_checker_) =
1236 configuration_.use_media_transport;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237
Zach Steine20867f2018-08-02 13:20:15 -07001238 // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it
1239 // is not injected. It should be required once chromium supplies it.
Karl Wibergfb3be392019-03-22 14:13:22 +01001240 std::unique_ptr<AsyncResolverFactory> async_resolver_factory_
1241 RTC_GUARDED_BY(signaling_thread());
1242 std::unique_ptr<cricket::PortAllocator>
1243 port_allocator_; // TODO(bugs.webrtc.org/9987): Accessed on both
1244 // signaling and network thread.
1245 std::unique_ptr<rtc::SSLCertificateVerifier>
1246 tls_cert_verifier_; // TODO(bugs.webrtc.org/9987): Accessed on both
1247 // signaling and network thread.
deadbeefab9b2d12015-10-14 11:33:11 -07001248
zhihuang8f65cdf2016-05-06 18:40:30 -07001249 // One PeerConnection has only one RTCP CNAME.
1250 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
Karl Wibergfb3be392019-03-22 14:13:22 +01001251 const std::string rtcp_cname_;
zhihuang8f65cdf2016-05-06 18:40:30 -07001252
deadbeefab9b2d12015-10-14 11:33:11 -07001253 // Streams added via AddStream.
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001254 const rtc::scoped_refptr<StreamCollection> local_streams_
1255 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001256 // Streams created as a result of SetRemoteDescription.
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001257 const rtc::scoped_refptr<StreamCollection> remote_streams_
1258 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001259
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001260 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
1261 RTC_GUARDED_BY(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -08001262
Steve Anton4171afb2017-11-20 10:20:22 -08001263 // These lists store sender info seen in local/remote descriptions.
Karl Wibergc70999e2019-03-22 15:14:27 +01001264 std::vector<RtpSenderInfo> remote_audio_sender_infos_
1265 RTC_GUARDED_BY(signaling_thread());
1266 std::vector<RtpSenderInfo> remote_video_sender_infos_
1267 RTC_GUARDED_BY(signaling_thread());
1268 std::vector<RtpSenderInfo> local_audio_sender_infos_
1269 RTC_GUARDED_BY(signaling_thread());
1270 std::vector<RtpSenderInfo> local_video_sender_infos_
1271 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001272
Karl Wiberg85a4a932019-03-22 15:29:58 +01001273 SctpSidAllocator sid_allocator_ RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001274 // label -> DataChannel
Karl Wiberg85a4a932019-03-22 15:29:58 +01001275 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_
1276 RTC_GUARDED_BY(signaling_thread());
1277 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_
1278 RTC_GUARDED_BY(signaling_thread());
1279 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_
1280 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001281
Karl Wiberg85a4a932019-03-22 15:29:58 +01001282 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
deadbeef70ab1a12015-09-28 16:53:55 -07001283
Karl Wibergac025892019-03-26 13:08:37 +01001284 // The unique_ptr belongs to the worker thread, but the Call object manages
1285 // its own thread safety.
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001286 std::unique_ptr<Call> call_ RTC_GUARDED_BY(worker_thread());
1287
1288 // Points to the same thing as `call_`. Since it's const, we may read the
Karl Wibergac025892019-03-26 13:08:37 +01001289 // pointer from any thread.
1290 Call* const call_ptr_;
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001291
1292 std::unique_ptr<StatsCollector> stats_
1293 RTC_GUARDED_BY(signaling_thread()); // A pointer is passed to senders_
1294 rtc::scoped_refptr<RTCStatsCollector> stats_collector_
1295 RTC_GUARDED_BY(signaling_thread());
terelius33860252017-05-12 23:37:18 -07001296
deadbeefa601f5c2016-06-06 14:27:39 -07001297 std::vector<
Steve Anton4171afb2017-11-20 10:20:22 -08001298 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001299 transceivers_; // TODO(bugs.webrtc.org/9987): Accessed on both signaling
1300 // and network thread.
1301
Henrik Boström5b147782018-12-04 11:25:05 +01001302 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
1303 // line we create and use a stream with a random ID for our receivers. This is
1304 // to support legacy endpoints that do not support the a=msid attribute (as
1305 // opposed to streamless tracks with "a=msid:-").
Karl Wiberga58e1692019-03-26 13:33:43 +01001306 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
1307 RTC_GUARDED_BY(signaling_thread());
Amit Hilbuchae3df542019-01-07 12:13:08 -08001308 // MIDs will be generated using this generator which will keep track of
1309 // all the MIDs that have been seen over the life of the PeerConnection.
Karl Wiberga58e1692019-03-26 13:33:43 +01001310 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001311
Karl Wiberga58e1692019-03-26 13:33:43 +01001312 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
1313 SessionError::kNone;
1314 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001315
Karl Wiberga58e1692019-03-26 13:33:43 +01001316 std::string session_id_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001317
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001318 std::unique_ptr<JsepTransportController>
1319 transport_controller_; // TODO(bugs.webrtc.org/9987): Accessed on both
1320 // signaling and network thread.
1321 std::unique_ptr<cricket::SctpTransportInternalFactory>
1322 sctp_factory_; // TODO(bugs.webrtc.org/9987): Accessed on both
1323 // signaling and network thread.
Steve Anton75737c02017-11-06 10:37:17 -08001324 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
1325 // when using SCTP.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001326 cricket::RtpDataChannel* rtp_data_channel_ =
1327 nullptr; // TODO(bugs.webrtc.org/9987): Accessed on both
1328 // signaling and some other thread.
Steve Anton75737c02017-11-06 10:37:17 -08001329
Harald Alvestrandc85328f2019-02-28 07:51:00 +01001330 cricket::SctpTransportInternal* cricket_sctp_transport() {
1331 return sctp_transport_->internal();
1332 }
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001333 rtc::scoped_refptr<SctpTransport>
1334 sctp_transport_; // TODO(bugs.webrtc.org/9987): Accessed on both
1335 // signaling and network thread.
1336
Zhi Huange830e682018-03-30 10:48:35 -07001337 // |sctp_mid_| is the content name (MID) in SDP.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001338 // Note: this is used as the data channel MID by both SCTP and data channel
1339 // transports. It is set when either transport is initialized and unset when
1340 // both transports are deleted.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001341 absl::optional<std::string>
1342 sctp_mid_; // TODO(bugs.webrtc.org/9987): Accessed on both signaling
1343 // and network thread.
1344
Steve Anton75737c02017-11-06 10:37:17 -08001345 // Value cached on signaling thread. Only updated when SctpReadyToSendData
1346 // fires on the signaling thread.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001347 bool sctp_ready_to_send_data_ RTC_GUARDED_BY(signaling_thread()) = false;
1348
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001349 // Whether the use of SCTP has been successfully negotiated.
1350 bool sctp_negotiated_ RTC_GUARDED_BY(signaling_thread()) = false;
1351
Steve Anton75737c02017-11-06 10:37:17 -08001352 // Same as signals provided by SctpTransport, but these are guaranteed to
1353 // fire on the signaling thread, whereas SctpTransport fires on the networking
1354 // thread.
1355 // |sctp_invoker_| is used so that any signals queued on the signaling thread
1356 // from the network thread are immediately discarded if the SctpTransport is
1357 // destroyed (due to m= section being rejected).
1358 // TODO(deadbeef): Use a proxy object to ensure that method calls/signals
1359 // are marshalled to the right thread. Could almost use proxy.h for this,
1360 // but it doesn't have a mechanism for marshalling sigslot::signals
Karl Wiberg7a651c6e2019-04-02 13:00:46 +02001361 std::unique_ptr<rtc::AsyncInvoker> sctp_invoker_
1362 RTC_GUARDED_BY(network_thread());
1363 sigslot::signal1<bool> SignalSctpReadyToSendData
1364 RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001365 sigslot::signal2<const cricket::ReceiveDataParams&,
1366 const rtc::CopyOnWriteBuffer&>
Karl Wiberg7a651c6e2019-04-02 13:00:46 +02001367 SignalSctpDataReceived RTC_GUARDED_BY(signaling_thread());
1368 sigslot::signal1<int> SignalSctpClosingProcedureStartedRemotely
1369 RTC_GUARDED_BY(signaling_thread());
1370 sigslot::signal1<int> SignalSctpClosingProcedureComplete
1371 RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001372
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001373 // Whether this peer is the caller. Set when the local description is applied.
1374 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
1375
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001376 // Plugin transport used for data channels. Thread-safe.
1377 DataChannelTransportInterface* data_channel_transport_ =
Karl Wibergd9bf7202019-04-02 19:36:24 +02001378 nullptr; // TODO(bugs.webrtc.org/9987): Object is thread safe, but
1379 // pointer accessed on both signaling and network thread.
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001380
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001381 // Cached value of whether the data channel transport is ready to send.
1382 bool data_channel_transport_ready_to_send_
1383 RTC_GUARDED_BY(signaling_thread()) = false;
1384
1385 // Whether the use of the data channel transport has been successfully
1386 // negotiated.
1387 bool data_channel_transport_negotiated_ RTC_GUARDED_BY(signaling_thread()) =
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001388 false;
1389
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001390 // Used to invoke data channel transport signals on the signaling thread.
1391 std::unique_ptr<rtc::AsyncInvoker> data_channel_transport_invoker_
Karl Wiberg739506e2019-04-03 11:37:28 +02001392 RTC_GUARDED_BY(network_thread());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001393
1394 // Identical to the signals for SCTP, but from media transport:
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001395 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001396 RTC_GUARDED_BY(signaling_thread());
1397 sigslot::signal2<const cricket::ReceiveDataParams&,
1398 const rtc::CopyOnWriteBuffer&>
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001399 SignalDataChannelTransportReceivedData_s
1400 RTC_GUARDED_BY(signaling_thread());
1401 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001402 RTC_GUARDED_BY(signaling_thread());
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001403 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001404 RTC_GUARDED_BY(signaling_thread());
1405
Karl Wiberg739506e2019-04-03 11:37:28 +02001406 std::unique_ptr<SessionDescriptionInterface> current_local_description_
1407 RTC_GUARDED_BY(signaling_thread());
1408 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
1409 RTC_GUARDED_BY(signaling_thread());
1410 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
1411 RTC_GUARDED_BY(signaling_thread());
1412 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
1413 RTC_GUARDED_BY(signaling_thread());
1414 bool dtls_enabled_ RTC_GUARDED_BY(signaling_thread()) = false;
Steve Anton75737c02017-11-06 10:37:17 -08001415 // Specifies which kind of data channel is allowed. This is controlled
1416 // by the chrome command-line flag and constraints:
1417 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
1418 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
1419 // not set or false, SCTP is allowed (DCT_SCTP);
1420 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
1421 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
Karl Wibergf73f7d62019-04-08 15:36:53 +02001422 cricket::DataChannelType data_channel_type_ =
1423 cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both
1424 // signaling and network thread.
Steve Anton75737c02017-11-06 10:37:17 -08001425
Karl Wibergf73f7d62019-04-08 15:36:53 +02001426 // List of content names for which the remote side triggered an ICE restart.
1427 std::set<std::string> pending_ice_restarts_
1428 RTC_GUARDED_BY(signaling_thread());
1429
1430 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
1431 RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001432
1433 // Member variables for caching global options.
Karl Wibergf73f7d62019-04-08 15:36:53 +02001434 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
1435 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001436
Karl Wibergf73f7d62019-04-08 15:36:53 +02001437 int usage_event_accumulator_ RTC_GUARDED_BY(signaling_thread()) = 0;
1438 bool return_histogram_very_quickly_ RTC_GUARDED_BY(signaling_thread()) =
1439 false;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001440
1441 // This object should be used to generate any SSRC that is not explicitly
1442 // specified by the user (or by the remote party).
1443 // The generator is not used directly, instead it is passed on to the
1444 // channel manager and the session description factory.
Karl Wibergf73f7d62019-04-08 15:36:53 +02001445 rtc::UniqueRandomIdGenerator ssrc_generator_
1446 RTC_GUARDED_BY(signaling_thread());
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +02001447
1448 // A video bitrate allocator factory.
1449 // This can injected using the PeerConnectionDependencies,
1450 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
1451 // Note that one can still choose to override this in a MediaEngine
1452 // if one wants too.
1453 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
1454 video_bitrate_allocator_factory_;
1455
Henrik Boström79b69802019-07-18 11:16:56 +02001456 std::unique_ptr<LocalIceCredentialsToReplace>
1457 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001458 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459};
1460
1461} // namespace webrtc
1462
Steve Anton10542f22019-01-11 09:11:00 -08001463#endif // PC_PEER_CONNECTION_H_