blob: cbff7e7050b5da1b5a56e8663701b75785b85188 [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
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/peer_connection_interface.h"
Niels Möller65f17ca2019-09-12 13:59:36 +020022#include "api/transport/data_channel_transport_interface.h"
23#include "api/transport/media/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#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"
Henrik Boströma3728d32019-10-28 12:09:49 +010037#include "rtc_base/operations_chain.h"
Karl Wiberg5966c502019-02-21 23:55:09 +010038#include "rtc_base/race_checker.h"
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080039#include "rtc_base/unique_id_generator.h"
Henrik Boströma3728d32019-10-28 12:09:49 +010040#include "rtc_base/weak_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
deadbeefeb459812015-12-15 19:24:43 -080044class MediaStreamObserver;
perkjf0dcfe22016-03-10 18:32:00 +010045class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 11:53:05 -070046class RtcEventLog;
deadbeefab9b2d12015-10-14 11:33:11 -070047
Steve Anton75737c02017-11-06 10:37:17 -080048// PeerConnection is the implementation of the PeerConnection object as defined
49// by the PeerConnectionInterface API surface.
50// The class currently is solely responsible for the following:
51// - Managing the session state machine (signaling state).
52// - Creating and initializing lower-level objects, like PortAllocator and
53// BaseChannels.
54// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
55// objects.
56// - Tracking the current and pending local/remote session descriptions.
57// The class currently is jointly responsible for the following:
58// - Parsing and interpreting SDP.
59// - Generating offers and answers based on the current state.
60// - The ICE state machine.
61// - Generating stats.
Steve Anton2d8609c2018-01-23 16:38:46 -080062class PeerConnection : public PeerConnectionInternal,
Steve Anton75737c02017-11-06 10:37:17 -080063 public DataChannelProviderInterface,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080064 public DataChannelSink,
Zhi Huang365381f2018-04-13 16:44:34 -070065 public JsepTransportController::Observer,
Guido Urdaneta1ff16c82019-05-20 19:31:53 +020066 public RtpSenderBase::SetStreamsObserver,
Steve Antonad182762018-09-05 20:22:40 +000067 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 public sigslot::has_slots<> {
69 public:
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070070 // A bit in the usage pattern is registered when its defining event occurs at
71 // least once.
Harald Alvestrand8ebba742018-05-31 14:00:34 +020072 enum class UsageEvent : int {
73 TURN_SERVER_ADDED = 0x01,
74 STUN_SERVER_ADDED = 0x02,
75 DATA_ADDED = 0x04,
76 AUDIO_ADDED = 0x08,
77 VIDEO_ADDED = 0x10,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070078 // |SetLocalDescription| returns successfully.
79 SET_LOCAL_DESCRIPTION_SUCCEEDED = 0x20,
80 // |SetRemoteDescription| returns successfully.
81 SET_REMOTE_DESCRIPTION_SUCCEEDED = 0x40,
82 // A local candidate (with type host, server-reflexive, or relay) is
83 // collected.
Harald Alvestrand8ebba742018-05-31 14:00:34 +020084 CANDIDATE_COLLECTED = 0x80,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070085 // A remote candidate is successfully added via |AddIceCandidate|.
86 ADD_ICE_CANDIDATE_SUCCEEDED = 0x100,
Harald Alvestrand8ebba742018-05-31 14:00:34 +020087 ICE_STATE_CONNECTED = 0x200,
Qingsi Wang7fc821d2018-07-12 12:54:53 -070088 CLOSE_CALLED = 0x400,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070089 // A local candidate with private IP is collected.
Harald Alvestrand056d8112018-07-16 19:18:58 +020090 PRIVATE_CANDIDATE_COLLECTED = 0x800,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070091 // A remote candidate with private IP is added, either via AddiceCandidate
92 // or from the remote description.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070093 REMOTE_PRIVATE_CANDIDATE_ADDED = 0x1000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070094 // A local mDNS candidate is collected.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070095 MDNS_CANDIDATE_COLLECTED = 0x2000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070096 // A remote mDNS candidate is added, either via AddIceCandidate or from the
97 // remote description.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070098 REMOTE_MDNS_CANDIDATE_ADDED = 0x4000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070099 // A local candidate with IPv6 address is collected.
100 IPV6_CANDIDATE_COLLECTED = 0x8000,
101 // A remote candidate with IPv6 address is added, either via AddIceCandidate
102 // or from the remote description.
103 REMOTE_IPV6_CANDIDATE_ADDED = 0x10000,
104 // A remote candidate (with type host, server-reflexive, or relay) is
105 // successfully added, either via AddIceCandidate or from the remote
106 // description.
107 REMOTE_CANDIDATE_ADDED = 0x20000,
Qingsi Wangcc46b102019-09-12 11:19:01 -0700108 // An explicit host-host candidate pair is selected, i.e. both the local and
109 // the remote candidates have the host type. This does not include candidate
110 // pairs formed with equivalent prflx remote candidates, e.g. a host-prflx
111 // pair where the prflx candidate has the same base as a host candidate of
112 // the remote peer.
113 DIRECT_CONNECTION_SELECTED = 0x40000,
114 MAX_VALUE = 0x80000,
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200115 };
116
zhihuang38ede132017-06-15 12:52:32 -0700117 explicit PeerConnection(PeerConnectionFactory* factory,
118 std::unique_ptr<RtcEventLog> event_log,
119 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120
deadbeef653b8e02015-11-11 12:55:10 -0800121 bool Initialize(
122 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab58882018-05-02 15:12:47 -0700123 PeerConnectionDependencies dependencies);
deadbeef653b8e02015-11-11 12:55:10 -0800124
deadbeefa67696b2015-09-29 11:56:26 -0700125 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
126 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
127 bool AddStream(MediaStreamInterface* local_stream) override;
128 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
Steve Anton2d6c76a2018-01-05 17:10:52 -0800130 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -0800131 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800132 const std::vector<std::string>& stream_ids) override;
deadbeefe1f9d832016-01-14 15:35:42 -0800133 bool RemoveTrack(RtpSenderInterface* sender) override;
Steve Anton24db5732018-07-23 10:27:33 -0700134 RTCError RemoveTrackNew(
135 rtc::scoped_refptr<RtpSenderInterface> sender) override;
deadbeefe1f9d832016-01-14 15:35:42 -0800136
Steve Anton9158ef62017-11-27 13:01:52 -0800137 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
138 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
139 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
140 rtc::scoped_refptr<MediaStreamTrackInterface> track,
141 const RtpTransceiverInit& init) override;
142 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
143 cricket::MediaType media_type) override;
144 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
145 cricket::MediaType media_type,
146 const RtpTransceiverInit& init) override;
147
Steve Anton8c0f7a72017-10-03 10:03:10 -0700148 // Gets the DTLS SSL certificate associated with the audio transport on the
149 // remote side. This will become populated once the DTLS connection with the
150 // peer has been completed, as indicated by the ICE connection state
151 // transitioning to kIceConnectionCompleted.
152 // Note that this will be removed once we implement RTCDtlsTransport which
153 // has standardized method for getting this information.
154 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
155 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
156
Zhi Huang70b820f2018-01-27 14:16:15 -0800157 // Version of the above method that returns the full certificate chain.
158 std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
159
deadbeeffac06552015-11-25 11:26:01 -0800160 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800161 const std::string& kind,
162 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800163
deadbeef70ab1a12015-09-28 16:53:55 -0700164 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
165 const override;
166 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
167 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800168 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
169 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700170
deadbeefa67696b2015-09-29 11:56:26 -0700171 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700173 const DataChannelInit* config) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100174 // WARNING: LEGACY. See peerconnectioninterface.h
deadbeefa67696b2015-09-29 11:56:26 -0700175 bool GetStats(StatsObserver* observer,
176 webrtc::MediaStreamTrackInterface* track,
177 StatsOutputLevel level) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100178 // Spec-complaint GetStats(). See peerconnectioninterface.h
hbos74e1a4f2016-09-15 23:33:01 -0700179 void GetStats(RTCStatsCollectorCallback* callback) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100180 void GetStats(
181 rtc::scoped_refptr<RtpSenderInterface> selector,
182 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
183 void GetStats(
184 rtc::scoped_refptr<RtpReceiverInterface> selector,
185 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100186 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
deadbeefa67696b2015-09-29 11:56:26 -0700188 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
deadbeefa67696b2015-09-29 11:56:26 -0700190 IceConnectionState ice_connection_state() override;
Jonas Olsson12046902018-12-06 11:25:14 +0100191 IceConnectionState standardized_ice_connection_state() override;
Jonas Olsson635474e2018-10-18 15:58:17 +0200192 PeerConnectionState peer_connection_state() override;
deadbeefa67696b2015-09-29 11:56:26 -0700193 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194
deadbeefa67696b2015-09-29 11:56:26 -0700195 const SessionDescriptionInterface* local_description() const override;
196 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800197 const SessionDescriptionInterface* current_local_description() const override;
198 const SessionDescriptionInterface* current_remote_description()
199 const override;
200 const SessionDescriptionInterface* pending_local_description() const override;
201 const SessionDescriptionInterface* pending_remote_description()
202 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203
Henrik Boström79b69802019-07-18 11:16:56 +0200204 void RestartIce() override;
205
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700207 void CreateOffer(CreateSessionDescriptionObserver* observer,
208 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800209 void CreateAnswer(CreateSessionDescriptionObserver* observer,
210 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700211 void SetLocalDescription(SetSessionDescriptionObserver* observer,
212 SessionDescriptionInterface* desc) override;
Henrik Boström4e196702019-10-30 10:35:50 +0100213 void SetLocalDescription(SetSessionDescriptionObserver* observer) override;
Henrik Boströma4ecf552017-11-23 14:17:07 +0000214 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
215 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 17:48:32 +0100216 void SetRemoteDescription(
217 std::unique_ptr<SessionDescriptionInterface> desc,
218 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
219 override;
deadbeef46c73892016-11-16 19:42:04 -0800220 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
Niels Möller2579f0c2019-08-19 09:58:17 +0200221 RTCError SetConfiguration(
222 const PeerConnectionInterface::RTCConfiguration& configuration) override;
deadbeefa67696b2015-09-29 11:56:26 -0700223 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Henrik Boströmee6f4f62019-11-06 12:36:12 +0100224 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
225 std::function<void(RTCError)> callback) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700226 bool RemoveIceCandidates(
227 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
Niels Möller0c4f7be2018-05-07 14:01:37 +0200229 RTCError SetBitrate(const BitrateSettings& bitrate) override;
zstein4b979802017-06-02 14:37:37 -0700230
henrika5f6bf242017-11-01 11:06:56 +0100231 void SetAudioPlayout(bool playout) override;
232 void SetAudioRecording(bool recording) override;
233
Harald Alvestrandad88c882018-11-28 16:47:46 +0100234 rtc::scoped_refptr<DtlsTransportInterface> LookupDtlsTransportByMid(
235 const std::string& mid) override;
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100236 rtc::scoped_refptr<DtlsTransport> LookupDtlsTransportByMidInternal(
237 const std::string& mid);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100238
Harald Alvestrandc85328f2019-02-28 07:51:00 +0100239 rtc::scoped_refptr<SctpTransportInterface> GetSctpTransport() const override;
240
Bjorn Tereliusde939432017-11-20 17:38:14 +0100241 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
242 int64_t output_period_ms) override;
Niels Möllerf00ca1a2019-05-10 11:33:12 +0200243 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700244 void StopRtcEventLog() override;
245
deadbeefa67696b2015-09-29 11:56:26 -0700246 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247
Steve Anton2d8609c2018-01-23 16:38:46 -0800248 // PeerConnectionInternal implementation.
Karl Wiberg4ae63472019-02-22 00:57:06 +0100249 rtc::Thread* network_thread() const final {
Steve Anton2d8609c2018-01-23 16:38:46 -0800250 return factory_->network_thread();
251 }
Karl Wiberg4ae63472019-02-22 00:57:06 +0100252 rtc::Thread* worker_thread() const final { return factory_->worker_thread(); }
253 rtc::Thread* signaling_thread() const final {
Steve Anton2d8609c2018-01-23 16:38:46 -0800254 return factory_->signaling_thread();
perkjd61bf802016-03-24 03:16:19 -0700255 }
deadbeefab9b2d12015-10-14 11:33:11 -0700256
Karl Wiberga58e1692019-03-26 13:33:43 +0100257 std::string session_id() const override {
258 RTC_DCHECK_RUN_ON(signaling_thread());
259 return session_id_;
260 }
Steve Anton75737c02017-11-06 10:37:17 -0800261
Steve Anton2d8609c2018-01-23 16:38:46 -0800262 bool initial_offerer() const override {
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200263 RTC_DCHECK_RUN_ON(signaling_thread());
Zhi Huange830e682018-03-30 10:48:35 -0700264 return transport_controller_ && transport_controller_->initial_offerer();
Steve Anton2d8609c2018-01-23 16:38:46 -0800265 }
Steve Anton75737c02017-11-06 10:37:17 -0800266
Steve Anton2d8609c2018-01-23 16:38:46 -0800267 std::vector<
268 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 10:07:54 -0800269 GetTransceiversInternal() const override {
Karl Wiberga58e1692019-03-26 13:33:43 +0100270 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -0800271 return transceivers_;
272 }
273
Steve Anton2d8609c2018-01-23 16:38:46 -0800274 sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
275 return SignalDataChannelCreated_;
276 }
277
278 cricket::RtpDataChannel* rtp_data_channel() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800279 return rtp_data_channel_;
Steve Anton978b8762017-09-29 12:15:02 -0700280 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800281
Steve Antonbe5e2082018-01-24 15:29:17 -0800282 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels()
Steve Anton2d8609c2018-01-23 16:38:46 -0800283 const override {
Karl Wiberg85a4a932019-03-22 15:29:58 +0100284 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -0800285 return sctp_data_channels_;
286 }
287
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200288 absl::optional<std::string> sctp_content_name() const override {
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200289 RTC_DCHECK_RUN_ON(signaling_thread());
Zhi Huange830e682018-03-30 10:48:35 -0700290 return sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -0800291 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800292
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200293 absl::optional<std::string> sctp_transport_name() const override;
Steve Anton75737c02017-11-06 10:37:17 -0800294
Qingsi Wang72a43a12018-02-20 16:03:18 -0800295 cricket::CandidateStatsList GetPooledCandidateStats() const override;
Steve Anton5dfde182018-02-06 10:34:40 -0800296 std::map<std::string, std::string> GetTransportNamesByMid() const override;
297 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
298 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800299 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 10:37:17 -0800300
Steve Anton2d8609c2018-01-23 16:38:46 -0800301 bool GetLocalCertificate(
302 const std::string& transport_name,
303 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800304 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Steve Anton2d8609c2018-01-23 16:38:46 -0800305 const std::string& transport_name) override;
306 bool IceRestartPending(const std::string& content_name) const override;
307 bool NeedsIceRestart(const std::string& content_name) const override;
308 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Steve Anton7464fca2018-01-19 11:10:37 -0800309
Harald Alvestrand19793842018-06-25 12:03:50 +0200310 void ReturnHistogramVeryQuicklyForTesting() {
Karl Wibergf73f7d62019-04-08 15:36:53 +0200311 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand19793842018-06-25 12:03:50 +0200312 return_histogram_very_quickly_ = true;
313 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200314 void RequestUsagePatternReportForTesting();
Harald Alvestrand19793842018-06-25 12:03:50 +0200315
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700317 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318
319 private:
Henrik Boström4e196702019-10-30 10:35:50 +0100320 class ImplicitCreateSessionDescriptionObserver;
321 friend class ImplicitCreateSessionDescriptionObserver;
Henrik Boström31638672017-11-23 17:48:32 +0100322 class SetRemoteDescriptionObserverAdapter;
323 friend class SetRemoteDescriptionObserverAdapter;
Henrik Boström79b69802019-07-18 11:16:56 +0200324 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
325 // It makes the next CreateOffer() produce new ICE credentials even if
326 // RTCOfferAnswerOptions::ice_restart is false.
327 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
328 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
329 // move this type of logic to JsepTransportController/JsepTransport.
330 class LocalIceCredentialsToReplace;
Henrik Boström31638672017-11-23 17:48:32 +0100331
Steve Anton4171afb2017-11-20 10:20:22 -0800332 struct RtpSenderInfo {
333 RtpSenderInfo() : first_ssrc(0) {}
Seth Hampson845e8782018-03-02 11:34:10 -0800334 RtpSenderInfo(const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800335 const std::string sender_id,
336 uint32_t ssrc)
Seth Hampson845e8782018-03-02 11:34:10 -0800337 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
Steve Anton4171afb2017-11-20 10:20:22 -0800338 bool operator==(const RtpSenderInfo& other) {
Seth Hampson845e8782018-03-02 11:34:10 -0800339 return this->stream_id == other.stream_id &&
Steve Anton4171afb2017-11-20 10:20:22 -0800340 this->sender_id == other.sender_id &&
341 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-08 17:13:40 -0800342 }
Seth Hampson845e8782018-03-02 11:34:10 -0800343 std::string stream_id;
Steve Anton4171afb2017-11-20 10:20:22 -0800344 std::string sender_id;
345 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
346 // for communicating with the lower layers.
347 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 11:33:11 -0700348 };
deadbeefab9b2d12015-10-14 11:33:11 -0700349
Bjorn A Mellem5985a042019-06-28 14:19:38 -0700350 // Field-trial based configuration for datagram transport.
351 struct DatagramTransportConfig {
352 explicit DatagramTransportConfig(const std::string& field_trial)
353 : enabled("enabled", true), default_value("default_value", false) {
354 ParseFieldTrial({&enabled, &default_value}, field_trial);
355 }
356
357 // Whether datagram transport support is enabled at all. Defaults to true,
358 // allowing datagram transport to be used if (a) the application provides a
359 // factory for it and (b) the configuration specifies its use. This flag
360 // provides a kill-switch to force-disable datagram transport across all
361 // applications, without code changes.
362 FieldTrialFlag enabled;
363
364 // Whether the datagram transport is enabled or disabled by default.
365 // Defaults to false, meaning that applications must configure use of
366 // datagram transport through RTCConfiguration. If set to true,
367 // applications will use the datagram transport by default (but may still
368 // explicitly configure themselves not to use it through RTCConfiguration).
369 FieldTrialFlag default_value;
370 };
371
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700372 // Field-trial based configuration for datagram transport data channels.
373 struct DatagramTransportDataChannelConfig {
374 explicit DatagramTransportDataChannelConfig(const std::string& field_trial)
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700375 : enabled("enabled", true),
376 default_value("default_value", false),
377 receive_only("receive_only", false) {
378 ParseFieldTrial({&enabled, &default_value, &receive_only}, field_trial);
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700379 }
380
381 // Whether datagram transport data channel support is enabled at all.
382 // Defaults to true, allowing datagram transport to be used if (a) the
383 // application provides a factory for it and (b) the configuration specifies
384 // its use. This flag provides a kill-switch to force-disable datagram
385 // transport across all applications, without code changes.
386 FieldTrialFlag enabled;
387
388 // Whether the datagram transport data channels are enabled or disabled by
389 // default. Defaults to false, meaning that applications must configure use
390 // of datagram transport through RTCConfiguration. If set to true,
391 // applications will use the datagram transport by default (but may still
392 // explicitly configure themselves not to use it through RTCConfiguration).
393 FieldTrialFlag default_value;
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700394
395 // Whether the datagram transport is enabled in receive-only mode. If true,
396 // and if the datagram transport is enabled, it will only be used when
397 // receiving incoming calls, not when placing outgoing calls.
398 FieldTrialFlag receive_only;
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700399 };
400
Eldar Rello5ab79e62019-10-09 18:29:44 +0300401 // Captures partial state to be used for rollback. Applicable only in
402 // Unified Plan.
403 class TransceiverStableState {
404 public:
405 TransceiverStableState() {}
406 TransceiverStableState(RtpTransceiverDirection direction,
407 absl::optional<std::string> mid,
408 absl::optional<size_t> mline_index,
409 bool newly_created)
410 : direction_(direction),
411 mid_(mid),
412 mline_index_(mline_index),
413 newly_created_(newly_created) {}
414 RtpTransceiverDirection direction() const { return direction_; }
415 absl::optional<std::string> mid() const { return mid_; }
416 absl::optional<size_t> mline_index() const { return mline_index_; }
417 bool newly_created() const { return newly_created_; }
418
419 private:
420 RtpTransceiverDirection direction_ = RtpTransceiverDirection::kRecvOnly;
421 absl::optional<std::string> mid_;
422 absl::optional<size_t> mline_index_;
423 // Indicates that the transceiver was created as part of applying a
424 // description to track potential need for removing transceiver during
425 // rollback.
426 bool newly_created_ = false;
427 };
428
Steve Antonad182762018-09-05 20:22:40 +0000429 // Implements MessageHandler.
430 void OnMessage(rtc::Message* msg) override;
431
Steve Antonafb0bb72018-02-20 11:35:37 -0800432 // Plan B helpers for getting the voice/video media channels for the single
433 // audio/video transceiver, if it exists.
Karl Wiberg5966c502019-02-21 23:55:09 +0100434 cricket::VoiceMediaChannel* voice_media_channel() const
435 RTC_RUN_ON(signaling_thread());
436 cricket::VideoMediaChannel* video_media_channel() const
437 RTC_RUN_ON(signaling_thread());
Steve Anton60776752018-01-10 11:51:34 -0800438
Steve Anton4171afb2017-11-20 10:20:22 -0800439 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100440 GetSendersInternal() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800441 std::vector<
442 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100443 GetReceiversInternal() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800444
445 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100446 GetAudioTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800447 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100448 GetVideoTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800449
Steve Antonafb0bb72018-02-20 11:35:37 -0800450 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100451 GetFirstAudioTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Antonafb0bb72018-02-20 11:35:37 -0800452
Henrik Boströma3728d32019-10-28 12:09:49 +0100453 // Implementation of the offer/answer exchange operations. These are chained
454 // onto the |operations_chain_| when the public CreateOffer(), CreateAnswer(),
455 // SetLocalDescription() and SetRemoteDescription() methods are invoked.
456 void DoCreateOffer(
457 const RTCOfferAnswerOptions& options,
458 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
459 void DoCreateAnswer(
460 const RTCOfferAnswerOptions& options,
461 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
462 void DoSetLocalDescription(
463 std::unique_ptr<SessionDescriptionInterface> desc,
464 rtc::scoped_refptr<SetSessionDescriptionObserver> observer);
465 void DoSetRemoteDescription(
466 std::unique_ptr<SessionDescriptionInterface> desc,
467 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
468
deadbeefab9b2d12015-10-14 11:33:11 -0700469 void CreateAudioReceiver(MediaStreamInterface* stream,
Karl Wiberg744310f2019-02-14 10:18:56 +0100470 const RtpSenderInfo& remote_sender_info)
471 RTC_RUN_ON(signaling_thread());
perkjf0dcfe22016-03-10 18:32:00 +0100472
deadbeefab9b2d12015-10-14 11:33:11 -0700473 void CreateVideoReceiver(MediaStreamInterface* stream,
Karl Wiberg744310f2019-02-14 10:18:56 +0100474 const RtpSenderInfo& remote_sender_info)
475 RTC_RUN_ON(signaling_thread());
Henrik Boström933d8b02017-10-10 10:05:16 -0700476 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Karl Wiberg5966c502019-02-21 23:55:09 +0100477 const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700478
479 // May be called either by AddStream/RemoveStream, or when a track is
480 // added/removed from a stream previously added via AddStream.
Karl Wiberg5966c502019-02-21 23:55:09 +0100481 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream)
482 RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700483 void RemoveAudioTrack(AudioTrackInterface* track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100484 MediaStreamInterface* stream)
485 RTC_RUN_ON(signaling_thread());
486 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream)
487 RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700488 void RemoveVideoTrack(VideoTrackInterface* track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100489 MediaStreamInterface* stream)
490 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
Steve Antonf9381f02017-12-14 10:23:57 -0800492 // AddTrack implementation when Unified Plan is specified.
493 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
494 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Karl Wiberga58e1692019-03-26 13:33:43 +0100495 const std::vector<std::string>& stream_ids)
496 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800497 // AddTrack implementation when Plan B is specified.
498 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
499 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100500 const std::vector<std::string>& stream_ids)
501 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800502
503 // Returns the first RtpTransceiver suitable for a newly added track, if such
504 // transceiver is available.
505 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
506 FindFirstTransceiverForAddedTrack(
Karl Wiberga58e1692019-03-26 13:33:43 +0100507 rtc::scoped_refptr<MediaStreamTrackInterface> track)
508 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800509
Steve Antonf9381f02017-12-14 10:23:57 -0800510 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100511 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender)
512 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800513
Steve Anton22da89f2018-01-25 13:58:07 -0800514 // Internal implementation for AddTransceiver family of methods. If
515 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
Steve Anton9158ef62017-11-27 13:01:52 -0800516 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
517 cricket::MediaType media_type,
518 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 13:58:07 -0800519 const RtpTransceiverInit& init,
Karl Wiberg744310f2019-02-14 10:18:56 +0100520 bool fire_callback = true) RTC_RUN_ON(signaling_thread());
Steve Anton9158ef62017-11-27 13:01:52 -0800521
Steve Anton02ee47c2018-01-10 16:26:06 -0800522 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
523 CreateSender(cricket::MediaType media_type,
Steve Anton111fdfd2018-06-25 13:03:36 -0700524 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -0800525 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Florent Castelli892acf02018-10-01 22:47:20 +0200526 const std::vector<std::string>& stream_ids,
527 const std::vector<RtpEncodingParameters>& send_encodings);
Steve Anton02ee47c2018-01-10 16:26:06 -0800528
529 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
530 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
531
Steve Antonf9381f02017-12-14 10:23:57 -0800532 // Create a new RtpTransceiver of the given type and add it to the list of
533 // transceivers.
534 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Steve Anton02ee47c2018-01-10 16:26:06 -0800535 CreateAndAddTransceiver(
536 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
537 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100538 receiver) RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800539
Karl Wiberg744310f2019-02-14 10:18:56 +0100540 void SetIceConnectionState(IceConnectionState new_state)
541 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200542 void SetStandardizedIceConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100543 PeerConnectionInterface::IceConnectionState new_state)
544 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200545 void SetConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100546 PeerConnectionInterface::PeerConnectionState new_state)
547 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200548
Eldar Relloda13ea22019-06-01 12:23:43 +0300549 // Called any time the IceGatheringState changes.
Karl Wiberg744310f2019-02-14 10:18:56 +0100550 void OnIceGatheringChange(IceGatheringState new_state)
551 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800552 // New ICE candidate has been gathered.
Karl Wiberg744310f2019-02-14 10:18:56 +0100553 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)
554 RTC_RUN_ON(signaling_thread());
Eldar Relloda13ea22019-06-01 12:23:43 +0300555 // Gathering of an ICE candidate failed.
556 void OnIceCandidateError(const std::string& host_candidate,
557 const std::string& url,
558 int error_code,
559 const std::string& error_text)
560 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800561 // Some local ICE candidates have been removed.
Karl Wiberg744310f2019-02-14 10:18:56 +0100562 void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates)
563 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564
Alex Drake00c7ecf2019-08-06 10:54:47 -0700565 void OnSelectedCandidatePairChanged(
566 const cricket::CandidatePairChangeEvent& event)
567 RTC_RUN_ON(signaling_thread());
568
Steve Antonba818672017-11-06 10:21:57 -0800569 // Update the state, signaling if necessary.
Karl Wiberg744310f2019-02-14 10:18:56 +0100570 void ChangeSignalingState(SignalingState signaling_state)
571 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572
deadbeefeb459812015-12-15 19:24:43 -0800573 // Signals from MediaStreamObserver.
574 void OnAudioTrackAdded(AudioTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100575 MediaStreamInterface* stream)
576 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800577 void OnAudioTrackRemoved(AudioTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100578 MediaStreamInterface* stream)
579 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800580 void OnVideoTrackAdded(VideoTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100581 MediaStreamInterface* stream)
582 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800583 void OnVideoTrackRemoved(VideoTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100584 MediaStreamInterface* stream)
585 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800586
Henrik Boström31638672017-11-23 17:48:32 +0100587 void PostSetSessionDescriptionSuccess(
588 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
Steve Antonad182762018-09-05 20:22:40 +0000590 RTCError&& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700591 void PostCreateSessionDescriptionFailure(
592 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100593 RTCError error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594
Steve Anton8a006912017-12-04 15:25:56 -0800595 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
596 // that return an RTCError instead of invoking a callback.
597 RTCError ApplyLocalDescription(
598 std::unique_ptr<SessionDescriptionInterface> desc);
599 RTCError ApplyRemoteDescription(
600 std::unique_ptr<SessionDescriptionInterface> desc);
601
Steve Antondcc3c022017-12-22 16:02:54 -0800602 // Updates the local RtpTransceivers according to the JSEP rules. Called as
603 // part of setting the local/remote description.
604 RTCError UpdateTransceiversAndDataChannels(
605 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800606 const SessionDescriptionInterface& new_session,
607 const SessionDescriptionInterface* old_local_description,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100608 const SessionDescriptionInterface* old_remote_description)
609 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800610
611 // Either creates or destroys the transceiver's BaseChannel according to the
612 // given media section.
613 RTCError UpdateTransceiverChannel(
614 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
615 transceiver,
616 const cricket::ContentInfo& content,
Karl Wiberg5966c502019-02-21 23:55:09 +0100617 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800618
Steve Antonfa2260d2017-12-28 16:38:23 -0800619 // Either creates or destroys the local data channel according to the given
620 // media section.
621 RTCError UpdateDataChannel(cricket::ContentSource source,
622 const cricket::ContentInfo& content,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100623 const cricket::ContentGroup* bundle_group)
624 RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800625
Steve Antondcc3c022017-12-22 16:02:54 -0800626 // Associate the given transceiver according to the JSEP rules.
627 RTCErrorOr<
628 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
629 AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800630 SdpType type,
Steve Antondcc3c022017-12-22 16:02:54 -0800631 size_t mline_index,
632 const cricket::ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800633 const cricket::ContentInfo* old_local_content,
Karl Wiberg5966c502019-02-21 23:55:09 +0100634 const cricket::ContentInfo* old_remote_content)
635 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800636
637 // Returns the RtpTransceiver, if found, that is associated to the given MID.
638 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100639 GetAssociatedTransceiver(const std::string& mid) const
640 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800641
642 // Returns the RtpTransceiver, if found, that was assigned to the given mline
643 // index in CreateOffer.
644 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100645 GetTransceiverByMLineIndex(size_t mline_index) const
646 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800647
648 // Returns an RtpTransciever, if available, that can be used to receive the
649 // given media type according to JSEP rules.
650 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100651 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const
652 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800653
Steve Antoned10bd92017-12-05 10:52:59 -0800654 // Returns the media section in the given session description that is
655 // associated with the RtpTransceiver. Returns null if none found or this
656 // RtpTransceiver is not associated. Logic varies depending on the
657 // SdpSemantics specified in the configuration.
658 const cricket::ContentInfo* FindMediaSectionForTransceiver(
659 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
660 transceiver,
Karl Wiberg5966c502019-02-21 23:55:09 +0100661 const SessionDescriptionInterface* sdesc) const
662 RTC_RUN_ON(signaling_thread());
Steve Antoned10bd92017-12-05 10:52:59 -0800663
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100664 // Runs the algorithm **set the associated remote streams** specified in
665 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
666 void SetAssociatedRemoteStreams(
667 rtc::scoped_refptr<RtpReceiverInternal> receiver,
668 const std::vector<std::string>& stream_ids,
669 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100670 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
671 RTC_RUN_ON(signaling_thread());
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100672
Steve Anton0f5400a2018-07-17 14:25:36 -0700673 // Runs the algorithm **process the removal of a remote track** specified in
674 // the WebRTC specification.
675 // This method will update the following lists:
676 // |remove_list| is the list of transceivers for which the receiving track is
677 // being removed.
678 // |removed_streams| is the list of streams which no longer have a receiving
679 // track so should be removed.
680 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
681 void ProcessRemovalOfRemoteTrack(
682 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
683 transceiver,
684 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100685 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
686 RTC_RUN_ON(signaling_thread());
Steve Anton0f5400a2018-07-17 14:25:36 -0700687
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100688 void RemoveRemoteStreamsIfEmpty(
689 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
690 remote_streams,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100691 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
692 RTC_RUN_ON(signaling_thread());
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100693
Steve Anton52d86772018-02-20 15:48:12 -0800694 void OnNegotiationNeeded();
695
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 bool IsClosed() const {
Karl Wiberg8d2e2282019-02-17 13:00:07 +0100697 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 return signaling_state_ == PeerConnectionInterface::kClosed;
699 }
700
deadbeefab9b2d12015-10-14 11:33:11 -0700701 // Returns a MediaSessionOptions struct with options decided by |options|,
702 // the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800703 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
704 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100705 cricket::MediaSessionOptions* session_options)
706 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800707 void GetOptionsForPlanBOffer(
708 const PeerConnectionInterface::RTCOfferAnswerOptions&
709 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100710 cricket::MediaSessionOptions* session_options)
711 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800712 void GetOptionsForUnifiedPlanOffer(
713 const PeerConnectionInterface::RTCOfferAnswerOptions&
714 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100715 cricket::MediaSessionOptions* session_options)
716 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700717
Karl Wiberg5966c502019-02-21 23:55:09 +0100718 RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options)
719 RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800720 void RemoveRecvDirectionFromReceivingTransceiversOfType(
Karl Wiberga58e1692019-03-26 13:33:43 +0100721 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800722 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
723 std::vector<
724 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100725 GetReceivingTransceiversOfType(cricket::MediaType media_type)
726 RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800727
deadbeefab9b2d12015-10-14 11:33:11 -0700728 // Returns a MediaSessionOptions struct with options decided by
729 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800730 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100731 cricket::MediaSessionOptions* session_options)
732 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800733 void GetOptionsForPlanBAnswer(
734 const PeerConnectionInterface::RTCOfferAnswerOptions&
735 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100736 cricket::MediaSessionOptions* session_options)
737 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800738 void GetOptionsForUnifiedPlanAnswer(
739 const PeerConnectionInterface::RTCOfferAnswerOptions&
740 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100741 cricket::MediaSessionOptions* session_options)
742 RTC_RUN_ON(signaling_thread());
htaa2a49d92016-03-04 02:51:39 -0800743
zhihuang1c378ed2017-08-17 14:10:50 -0700744 // Generates MediaDescriptionOptions for the |session_opts| based on existing
745 // local description or remote description.
746 void GenerateMediaDescriptionOptions(
747 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -0800748 RtpTransceiverDirection audio_direction,
749 RtpTransceiverDirection video_direction,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200750 absl::optional<size_t>* audio_index,
751 absl::optional<size_t>* video_index,
752 absl::optional<size_t>* data_index,
Karl Wiberg85a4a932019-03-22 15:29:58 +0100753 cricket::MediaSessionOptions* session_options)
754 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700755
Steve Antonfa2260d2017-12-28 16:38:23 -0800756 // Generates the active MediaDescriptionOptions for the local data channel
757 // given the specified MID.
758 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
Karl Wiberg85a4a932019-03-22 15:29:58 +0100759 const std::string& mid) const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800760
761 // Generates the rejected MediaDescriptionOptions for the local data channel
762 // given the specified MID.
763 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
Karl Wiberg85a4a932019-03-22 15:29:58 +0100764 const std::string& mid) const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800765
766 // Returns the MID for the data section associated with either the
767 // RtpDataChannel or SCTP data channel, if it has been set. If no data
768 // channels are configured this will return nullopt.
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200769 absl::optional<std::string> GetDataMid() const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800770
Steve Anton4171afb2017-11-20 10:20:22 -0800771 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 15:33:07 -0800772 // Called when a media type is rejected (m-line set to port 0).
Karl Wiberg744310f2019-02-14 10:18:56 +0100773 void RemoveSenders(cricket::MediaType media_type)
774 RTC_RUN_ON(signaling_thread());
deadbeeffaac4972015-11-12 15:33:07 -0800775
deadbeefbda7e0b2015-12-08 17:13:40 -0800776 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
777 // and existing MediaStreamTracks are removed if there is no corresponding
778 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
779 // is created if it doesn't exist; if false, it's removed if it exists.
780 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700781 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 10:20:22 -0800782 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -0700783 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800784 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700785 cricket::MediaType media_type,
Karl Wiberg744310f2019-02-14 10:18:56 +0100786 StreamCollection* new_streams) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700787
Steve Anton4171afb2017-11-20 10:20:22 -0800788 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700789 // session description. It creates a remote MediaStreamTrackInterface
790 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800791 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
Karl Wiberg744310f2019-02-14 10:18:56 +0100792 cricket::MediaType media_type)
793 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700794
Steve Anton4171afb2017-11-20 10:20:22 -0800795 // Triggered when a remote sender has been removed from a remote session
796 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700797 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800798 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
Karl Wiberg744310f2019-02-14 10:18:56 +0100799 cricket::MediaType media_type)
800 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700801
802 // Finds remote MediaStreams without any tracks and removes them from
803 // |remote_streams_| and notifies the observer that the MediaStreams no longer
804 // exist.
Karl Wiberg744310f2019-02-14 10:18:56 +0100805 void UpdateEndedRemoteMediaStreams() RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700806
deadbeefab9b2d12015-10-14 11:33:11 -0700807 // Loops through the vector of |streams| and finds added and removed
808 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 10:20:22 -0800809 // For each new or removed StreamParam, OnLocalSenderSeen or
810 // OnLocalSenderRemoved is invoked.
811 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
Karl Wiberg5966c502019-02-21 23:55:09 +0100812 cricket::MediaType media_type)
813 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700814
Steve Anton4171afb2017-11-20 10:20:22 -0800815 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 11:33:11 -0700816 // session description.
817 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
818 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
819 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 10:20:22 -0800820 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
Karl Wiberg5966c502019-02-21 23:55:09 +0100821 cricket::MediaType media_type)
822 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700823
Steve Anton4171afb2017-11-20 10:20:22 -0800824 // Triggered when a local sender has been removed from a local session
deadbeefab9b2d12015-10-14 11:33:11 -0700825 // description.
826 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
827 // has been removed from the local SessionDescription and the stream can be
828 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 10:20:22 -0800829 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
Karl Wiberga58e1692019-03-26 13:33:43 +0100830 cricket::MediaType media_type)
831 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700832
Karl Wiberg85a4a932019-03-22 15:29:58 +0100833 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams)
834 RTC_RUN_ON(signaling_thread());
Karl Wiberg106d92d2019-02-14 10:17:47 +0100835 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams)
836 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700837 void UpdateClosingRtpDataChannels(
838 const std::vector<std::string>& active_channels,
Karl Wiberg85a4a932019-03-22 15:29:58 +0100839 bool is_local_update) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700840 void CreateRemoteRtpDataChannel(const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100841 uint32_t remote_ssrc)
842 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700843
844 // Creates channel and adds it to the collection of DataChannels that will
845 // be offered in a SessionDescription.
846 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
847 const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100848 const InternalDataChannelInit* config) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700849
850 // Checks if any data channel has been added.
Karl Wiberg85a4a932019-03-22 15:29:58 +0100851 bool HasDataChannels() const RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700852
Karl Wiberg85a4a932019-03-22 15:29:58 +0100853 void AllocateSctpSids(rtc::SSLRole role) RTC_RUN_ON(signaling_thread());
854 void OnSctpDataChannelClosed(DataChannel* channel)
855 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700856
Karl Wiberg85a4a932019-03-22 15:29:58 +0100857 void OnDataChannelDestroyed() RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800858 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700859 void OnDataChannelOpenMessage(const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100860 const InternalDataChannelInit& config)
861 RTC_RUN_ON(signaling_thread());
862
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800863 // Parses and handles open messages. Returns true if the message is an open
864 // message, false otherwise.
865 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
866 const rtc::CopyOnWriteBuffer& buffer)
867 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700868
Steve Anton4171afb2017-11-20 10:20:22 -0800869 // Returns true if the PeerConnection is configured to use Unified Plan
870 // semantics for creating offers/answers and setting local/remote
871 // descriptions. If this is true the RtpTransceiver API will also be available
872 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 10:25:56 -0800873 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
874 // sufficient time has passed.
Karl Wiberg5966c502019-02-21 23:55:09 +0100875 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread()) {
Steve Anton79e79602017-11-20 10:25:56 -0800876 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
877 }
Steve Anton4171afb2017-11-20 10:20:22 -0800878
Steve Anton06817cd2018-12-18 15:55:30 -0800879 // The offer/answer machinery assumes the media section MID is present and
880 // unique. To support legacy end points that do not supply a=mid lines, this
881 // method will modify the session description to add MIDs generated according
882 // to the SDP semantics.
Karl Wiberg5966c502019-02-21 23:55:09 +0100883 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description)
884 RTC_RUN_ON(signaling_thread());
Steve Anton06817cd2018-12-18 15:55:30 -0800885
Steve Anton4171afb2017-11-20 10:20:22 -0800886 // Is there an RtpSender of the given type?
Karl Wiberg5966c502019-02-21 23:55:09 +0100887 bool HasRtpSender(cricket::MediaType type) const
888 RTC_RUN_ON(signaling_thread());
deadbeeffac06552015-11-25 11:26:01 -0800889
Steve Anton4171afb2017-11-20 10:20:22 -0800890 // Return the RtpSender with the given track attached.
891 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100892 FindSenderForTrack(MediaStreamTrackInterface* track) const
893 RTC_RUN_ON(signaling_thread());
deadbeef70ab1a12015-09-28 16:53:55 -0700894
Steve Anton4171afb2017-11-20 10:20:22 -0800895 // Return the RtpSender with the given id, or null if none exists.
896 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100897 FindSenderById(const std::string& sender_id) const
898 RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800899
900 // Return the RtpReceiver with the given id, or null if none exists.
901 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100902 FindReceiverById(const std::string& receiver_id) const
903 RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800904
905 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
906 cricket::MediaType media_type);
907 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
908 cricket::MediaType media_type);
909 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
Emircan Uysalerbc609ea2018-03-27 21:57:18 +0000910 const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800911 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 11:33:11 -0700912
913 // Returns the specified SCTP DataChannel in sctp_data_channels_,
914 // or nullptr if not found.
Karl Wiberg85a4a932019-03-22 15:29:58 +0100915 DataChannel* FindDataChannelBySid(int sid) const
916 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700917
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700918 // Called when first configuring the port allocator.
Karl Wibergfb3be392019-03-22 14:13:22 +0100919 struct InitializePortAllocatorResult {
920 bool enable_ipv6;
921 };
922 InitializePortAllocatorResult InitializePortAllocator_n(
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200923 const cricket::ServerAddresses& stun_servers,
924 const std::vector<cricket::RelayServerConfig>& turn_servers,
925 const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800926 // Called when SetConfiguration is called to apply the supported subset
927 // of the configuration on the network thread.
928 bool ReconfigurePortAllocator_n(
929 const cricket::ServerAddresses& stun_servers,
930 const std::vector<cricket::RelayServerConfig>& turn_servers,
931 IceTransportsType type,
932 int candidate_pool_size,
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700933 PortPrunePolicy turn_port_prune_policy,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800934 webrtc::TurnCustomizer* turn_customizer,
Karl Wiberg739506e2019-04-03 11:37:28 +0200935 absl::optional<int> stun_candidate_keepalive_interval,
936 bool have_local_description);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700937
Elad Alon99c3fe52017-10-13 16:29:40 +0200938 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700939 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100940 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
941 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200942
Elad Alonacb24172017-10-06 14:32:13 +0200943 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700944 // This function should only be called from the worker thread.
945 void StopRtcEventLog_w();
946
Steve Anton038834f2017-07-14 15:59:59 -0700947 // Ensures the configuration doesn't have any parameters with invalid values,
948 // or values that conflict with other parameters.
949 //
950 // Returns RTCError::OK() if there are no issues.
951 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
952
Steve Antonba818672017-11-06 10:21:57 -0800953 cricket::ChannelManager* channel_manager() const;
Steve Antonba818672017-11-06 10:21:57 -0800954
Steve Antonf8470812017-12-04 10:46:21 -0800955 enum class SessionError {
956 kNone, // No error.
957 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
958 kTransport, // Error from the underlying transport.
959 };
960
Steve Anton75737c02017-11-06 10:37:17 -0800961 // Returns the last error in the session. See the enum above for details.
Karl Wiberga58e1692019-03-26 13:33:43 +0100962 SessionError session_error() const RTC_RUN_ON(signaling_thread()) {
963 return session_error_;
964 }
Steve Antonf8470812017-12-04 10:46:21 -0800965 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 10:37:17 -0800966
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800967 cricket::ChannelInterface* GetChannel(const std::string& content_name);
Steve Anton75737c02017-11-06 10:37:17 -0800968
969 // Get current SSL role used by SCTP's underlying transport.
970 bool GetSctpSslRole(rtc::SSLRole* role);
971
Steve Anton75737c02017-11-06 10:37:17 -0800972 cricket::IceConfig ParseIceConfig(
973 const PeerConnectionInterface::RTCConfiguration& config) const;
974
Steve Anton75737c02017-11-06 10:37:17 -0800975 // Implements DataChannelProviderInterface.
976 bool SendData(const cricket::SendDataParams& params,
977 const rtc::CopyOnWriteBuffer& payload,
978 cricket::SendDataResult* result) override;
979 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
980 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
981 void AddSctpDataStream(int sid) override;
982 void RemoveSctpDataStream(int sid) override;
983 bool ReadyToSendData() const override;
984
985 cricket::DataChannelType data_channel_type() const;
986
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800987 // Implements DataChannelSink.
988 void OnDataReceived(int channel_id,
989 DataMessageType type,
990 const rtc::CopyOnWriteBuffer& buffer) override;
991 void OnChannelClosing(int channel_id) override;
992 void OnChannelClosed(int channel_id) override;
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700993 void OnReadyToSend() override;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800994
Steve Anton75737c02017-11-06 10:37:17 -0800995 // Called when an RTCCertificate is generated or retrieved by
996 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
997 void OnCertificateReady(
998 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
999 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
1000
Steve Anton75737c02017-11-06 10:37:17 -08001001 // Non-const versions of local_description()/remote_description(), for use
1002 // internally.
Karl Wiberg739506e2019-04-03 11:37:28 +02001003 SessionDescriptionInterface* mutable_local_description()
1004 RTC_RUN_ON(signaling_thread()) {
Steve Anton75737c02017-11-06 10:37:17 -08001005 return pending_local_description_ ? pending_local_description_.get()
1006 : current_local_description_.get();
1007 }
Karl Wiberg739506e2019-04-03 11:37:28 +02001008 SessionDescriptionInterface* mutable_remote_description()
1009 RTC_RUN_ON(signaling_thread()) {
Steve Anton75737c02017-11-06 10:37:17 -08001010 return pending_remote_description_ ? pending_remote_description_.get()
1011 : current_remote_description_.get();
1012 }
1013
1014 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 10:46:21 -08001015 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 10:37:17 -08001016
Zhi Huange830e682018-03-30 10:48:35 -07001017 RTCError UpdateSessionState(SdpType type,
1018 cricket::ContentSource source,
1019 const cricket::SessionDescription* description);
Steve Anton75737c02017-11-06 10:37:17 -08001020 // Push the media parts of the local or remote session description
1021 // down to all of the channels.
Karl Wiberg5966c502019-02-21 23:55:09 +01001022 RTCError PushdownMediaDescription(SdpType type, cricket::ContentSource source)
1023 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001024
Steve Anton8a006912017-12-04 15:25:56 -08001025 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -08001026 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -08001027
1028 // Returns true and the TransportInfo of the given |content_name|
1029 // from |description|. Returns false if it's not available.
1030 static bool GetTransportDescription(
1031 const cricket::SessionDescription* description,
1032 const std::string& content_name,
1033 cricket::TransportDescription* info);
1034
Steve Anton75737c02017-11-06 10:37:17 -08001035 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 10:52:59 -08001036 // This enables media to flow on all configured audio/video channels and the
1037 // RtpDataChannel.
Karl Wiberga58e1692019-03-26 13:33:43 +01001038 void EnableSending() RTC_RUN_ON(signaling_thread());
Steve Anton3fe1b152017-12-12 10:20:08 -08001039
Steve Anton8af21862017-12-15 11:20:13 -08001040 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
Karl Wiberg85a4a932019-03-22 15:29:58 +01001041 void DestroyAllChannels() RTC_RUN_ON(signaling_thread());
Steve Anton3fe1b152017-12-12 10:20:08 -08001042
Steve Anton75737c02017-11-06 10:37:17 -08001043 // Returns the media index for a local ice candidate given the content name.
1044 // Returns false if the local session description does not have a media
1045 // content called |content_name|.
1046 bool GetLocalCandidateMediaIndex(const std::string& content_name,
Karl Wiberg739506e2019-04-03 11:37:28 +02001047 int* sdp_mline_index)
1048 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001049 // Uses all remote candidates in |remote_desc| in this session.
1050 bool UseCandidatesInSessionDescription(
Karl Wiberg744310f2019-02-14 10:18:56 +01001051 const SessionDescriptionInterface* remote_desc)
1052 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001053 // Uses |candidate| in this session.
Karl Wiberg744310f2019-02-14 10:18:56 +01001054 bool UseCandidate(const IceCandidateInterface* candidate)
1055 RTC_RUN_ON(signaling_thread());
Guido Urdaneta41633172019-05-23 20:12:29 +02001056 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
1057 const SessionDescriptionInterface* description,
1058 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001059 // Deletes the corresponding channel of contents that don't exist in |desc|.
1060 // |desc| can be null. This means that all channels are deleted.
Karl Wiberg5966c502019-02-21 23:55:09 +01001061 void RemoveUnusedChannels(const cricket::SessionDescription* desc)
1062 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001063
1064 // Allocates media channels based on the |desc|. If |desc| doesn't have
1065 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
1066 // This method will also delete any existing media channels before creating.
Karl Wiberg5966c502019-02-21 23:55:09 +01001067 RTCError CreateChannels(const cricket::SessionDescription& desc)
1068 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -08001069
1070 // If the BUNDLE policy is max-bundle, then we know for sure that all
1071 // transports will be bundled from the start. This method returns the BUNDLE
1072 // group if that's the case, or null if BUNDLE will be negotiated later. An
1073 // error is returned if max-bundle is specified but the session description
1074 // does not have a BUNDLE group.
1075 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
Karl Wiberg5966c502019-02-21 23:55:09 +01001076 const cricket::SessionDescription& desc) const
1077 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001078
1079 // Helper methods to create media channels.
Karl Wiberg5966c502019-02-21 23:55:09 +01001080 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid)
1081 RTC_RUN_ON(signaling_thread());
1082 cricket::VideoChannel* CreateVideoChannel(const std::string& mid)
1083 RTC_RUN_ON(signaling_thread());
1084 bool CreateDataChannel(const std::string& mid) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001085
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001086 bool SetupDataChannelTransport_n(const std::string& mid)
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001087 RTC_RUN_ON(network_thread());
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001088 void TeardownDataChannelTransport_n() RTC_RUN_ON(network_thread());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001089
Steve Anton75737c02017-11-06 10:37:17 -08001090 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
1091 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
1092 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 15:25:56 -08001093 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
Karl Wiberg5966c502019-02-21 23:55:09 +01001094 cricket::ContentSource source)
1095 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001096
Steve Anton3828c062017-12-06 10:34:51 -08001097 // Check if a call to SetLocalDescription is acceptable with a session
1098 // description of the given type.
1099 bool ExpectSetLocalDescription(SdpType type);
1100 // Check if a call to SetRemoteDescription is acceptable with a session
1101 // description of the given type.
1102 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -08001103 // Verifies a=setup attribute as per RFC 5763.
1104 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 10:34:51 -08001105 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -08001106
1107 // Returns true if we are ready to push down the remote candidate.
1108 // |remote_desc| is the new remote description, or NULL if the current remote
1109 // description should be used. Output |valid| is true if the candidate media
1110 // index is valid.
1111 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
1112 const SessionDescriptionInterface* remote_desc,
Karl Wiberga58e1692019-03-26 13:33:43 +01001113 bool* valid) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001114
1115 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
1116 // this session.
Karl Wiberg739506e2019-04-03 11:37:28 +02001117 bool SrtpRequired() const RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001118
Zhi Huange830e682018-03-30 10:48:35 -07001119 // JsepTransportController signal handlers.
Karl Wiberg744310f2019-02-14 10:18:56 +01001120 void OnTransportControllerConnectionState(cricket::IceConnectionState state)
1121 RTC_RUN_ON(signaling_thread());
1122 void OnTransportControllerGatheringState(cricket::IceGatheringState state)
1123 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001124 void OnTransportControllerCandidatesGathered(
1125 const std::string& transport_name,
Karl Wiberg744310f2019-02-14 10:18:56 +01001126 const std::vector<cricket::Candidate>& candidates)
1127 RTC_RUN_ON(signaling_thread());
Eldar Relloda13ea22019-06-01 12:23:43 +03001128 void OnTransportControllerCandidateError(
1129 const cricket::IceCandidateErrorEvent& event)
1130 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001131 void OnTransportControllerCandidatesRemoved(
Karl Wiberg744310f2019-02-14 10:18:56 +01001132 const std::vector<cricket::Candidate>& candidates)
1133 RTC_RUN_ON(signaling_thread());
Alex Drake00c7ecf2019-08-06 10:54:47 -07001134 void OnTransportControllerCandidateChanged(
1135 const cricket::CandidatePairChangeEvent& event)
1136 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001137 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
1138
Steve Antonf8470812017-12-04 10:46:21 -08001139 const char* SessionErrorToString(SessionError error) const;
Karl Wiberga58e1692019-03-26 13:33:43 +01001140 std::string GetSessionErrorMsg() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001141
Steve Anton8e20f172018-03-06 10:55:04 -08001142 // Report the UMA metric SdpFormatReceived for the given remote offer.
1143 void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
1144
Steve Anton0ffaaa22018-02-23 10:31:30 -08001145 // Report inferred negotiated SDP semantics from a local/remote answer to the
1146 // UMA observer.
1147 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
1148
Steve Anton75737c02017-11-06 10:37:17 -08001149 // Invoked when TransportController connection completion is signaled.
1150 // Reports stats for all transports in use.
Karl Wiberga58e1692019-03-26 13:33:43 +01001151 void ReportTransportStats() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001152
1153 // Gather the usage of IPv4/IPv6 as best connection.
1154 void ReportBestConnectionState(const cricket::TransportStats& stats);
1155
Steve Antonc7b964c2018-02-01 14:39:45 -08001156 void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
Karl Wiberg739506e2019-04-03 11:37:28 +02001157 const std::set<cricket::MediaType>& media_types)
1158 RTC_RUN_ON(signaling_thread());
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07001159 void ReportIceCandidateCollected(const cricket::Candidate& candidate)
1160 RTC_RUN_ON(signaling_thread());
1161 void ReportRemoteIceCandidateAdded(const cricket::Candidate& candidate)
1162 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001163
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001164 void NoteUsageEvent(UsageEvent event);
Karl Wiberg744310f2019-02-14 10:18:56 +01001165 void ReportUsagePattern() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001166
Steve Anton75737c02017-11-06 10:37:17 -08001167 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
1168
Karl Wiberga58e1692019-03-26 13:33:43 +01001169 const std::string GetTransportName(const std::string& content_name)
1170 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001171
Steve Anton6fec8802017-12-04 10:37:29 -08001172 // Destroys and clears the BaseChannel associated with the given transceiver,
1173 // if such channel is set.
1174 void DestroyTransceiverChannel(
1175 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1176 transceiver);
1177
1178 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Karl Wiberg85a4a932019-03-22 15:29:58 +01001179 void DestroyDataChannel() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001180
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001181 // Destroys the given ChannelInterface.
1182 // The channel cannot be accessed after this method is called.
1183 void DestroyChannelInterface(cricket::ChannelInterface* channel);
Steve Anton6fec8802017-12-04 10:37:29 -08001184
Zhi Huang365381f2018-04-13 16:44:34 -07001185 // JsepTransportController::Observer override.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07001186 //
1187 // Called by |transport_controller_| when processing transport information
1188 // from a session description, and the mapping from m= sections to transports
1189 // changed (as a result of BUNDLE negotiation, or m= sections being
1190 // rejected).
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001191 bool OnTransportChanged(
1192 const std::string& mid,
1193 RtpTransportInternal* rtp_transport,
1194 rtc::scoped_refptr<DtlsTransport> dtls_transport,
1195 MediaTransportInterface* media_transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001196 DataChannelTransportInterface* data_channel_transport) override;
Zhi Huange830e682018-03-30 10:48:35 -07001197
Guido Urdaneta1ff16c82019-05-20 19:31:53 +02001198 // RtpSenderBase::SetStreamsObserver override.
1199 void OnSetStreams() override;
1200
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02001201 // Returns the observer. Will crash on CHECK if the observer is removed.
Karl Wiberg744310f2019-02-14 10:18:56 +01001202 PeerConnectionObserver* Observer() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02001203
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001204 // Returns the CryptoOptions for this PeerConnection. This will always
1205 // return the RTCConfiguration.crypto_options if set and will only default
1206 // back to the PeerConnectionFactory settings if nothing was set.
Karl Wiberg5966c502019-02-21 23:55:09 +01001207 CryptoOptions GetCryptoOptions() RTC_RUN_ON(signaling_thread());
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001208
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001209 // Returns rtp transport, result can not be nullptr.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001210 RtpTransportInternal* GetRtpTransport(const std::string& mid)
1211 RTC_RUN_ON(signaling_thread()) {
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001212 auto rtp_transport = transport_controller_->GetRtpTransport(mid);
1213 RTC_DCHECK(rtp_transport);
1214 return rtp_transport;
1215 }
1216
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001217 void UpdateNegotiationNeeded();
1218 bool CheckIfNegotiationIsNeeded();
Eldar Relloead0ec92019-10-21 23:01:31 +03001219
1220 // | sdp_type | is the type of the SDP that caused the rollback.
1221 RTCError Rollback(SdpType sdp_type);
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001222
Karl Wiberg106d92d2019-02-14 10:17:47 +01001223 sigslot::signal1<DataChannel*> SignalDataChannelCreated_
1224 RTC_GUARDED_BY(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -08001225
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 // Storing the factory as a scoped reference pointer ensures that the memory
1227 // in the PeerConnectionFactoryImpl remains available as long as the
1228 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
1229 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -07001230 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 // will refer to the same reference count.
Karl Wiberg744310f2019-02-14 10:18:56 +01001232 const rtc::scoped_refptr<PeerConnectionFactory> factory_;
1233 PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
1234 nullptr;
terelius33860252017-05-12 23:37:18 -07001235
1236 // The EventLog needs to outlive |call_| (and any other object that uses it).
Karl Wibergb03ab712019-02-14 11:59:57 +01001237 std::unique_ptr<RtcEventLog> event_log_ RTC_GUARDED_BY(worker_thread());
1238
1239 // Points to the same thing as `event_log_`. Since it's const, we may read the
1240 // pointer (but not touch the object) from any thread.
1241 RtcEventLog* const event_log_ptr_ RTC_PT_GUARDED_BY(worker_thread());
terelius33860252017-05-12 23:37:18 -07001242
Henrik Boströma3728d32019-10-28 12:09:49 +01001243 // The operations chain is used by the offer/answer exchange methods to ensure
1244 // they are executed in the right order. For example, if
1245 // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
1246 // SRD operation will not start until CreateOffer() has completed. See
1247 // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
1248 rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
1249 RTC_GUARDED_BY(signaling_thread());
1250
Karl Wiberg8d2e2282019-02-17 13:00:07 +01001251 SignalingState signaling_state_ RTC_GUARDED_BY(signaling_thread()) = kStable;
1252 IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) =
1253 kIceConnectionNew;
1254 PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_
1255 RTC_GUARDED_BY(signaling_thread()) = kIceConnectionNew;
1256 PeerConnectionInterface::PeerConnectionState connection_state_
1257 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionState::kNew;
Jonas Olsson635474e2018-10-18 15:58:17 +02001258
Karl Wiberg8d2e2282019-02-17 13:00:07 +01001259 IceGatheringState ice_gathering_state_ RTC_GUARDED_BY(signaling_thread()) =
1260 kIceGatheringNew;
Karl Wiberg5966c502019-02-21 23:55:09 +01001261 PeerConnectionInterface::RTCConfiguration configuration_
1262 RTC_GUARDED_BY(signaling_thread());
1263
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001264 // Field-trial based configuration for datagram transport.
1265 const DatagramTransportConfig datagram_transport_config_;
1266
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001267 // Field-trial based configuration for datagram transport data channels.
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07001268 const DatagramTransportDataChannelConfig
1269 datagram_transport_data_channel_config_;
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001270
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001271 // Final, resolved value for whether datagram transport is in use.
1272 bool use_datagram_transport_ RTC_GUARDED_BY(signaling_thread()) = false;
1273
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001274 // Equivalent of |use_datagram_transport_|, but for its use with data
1275 // channels.
1276 bool use_datagram_transport_for_data_channels_
1277 RTC_GUARDED_BY(signaling_thread()) = false;
1278
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07001279 // Resolved value of whether to use data channels only for incoming calls.
1280 bool use_datagram_transport_for_data_channels_receive_only_
1281 RTC_GUARDED_BY(signaling_thread()) = false;
1282
Karl Wiberg5966c502019-02-21 23:55:09 +01001283 // Cache configuration_.use_media_transport so that we can access it from
1284 // other threads.
1285 // TODO(bugs.webrtc.org/9987): Caching just this bool and allowing the data
1286 // it's derived from to change is not necessarily sound. Stop doing it.
1287 rtc::RaceChecker use_media_transport_race_checker_;
1288 bool use_media_transport_ RTC_GUARDED_BY(use_media_transport_race_checker_) =
1289 configuration_.use_media_transport;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290
Zach Steine20867f2018-08-02 13:20:15 -07001291 // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it
1292 // is not injected. It should be required once chromium supplies it.
Karl Wibergfb3be392019-03-22 14:13:22 +01001293 std::unique_ptr<AsyncResolverFactory> async_resolver_factory_
1294 RTC_GUARDED_BY(signaling_thread());
1295 std::unique_ptr<cricket::PortAllocator>
1296 port_allocator_; // TODO(bugs.webrtc.org/9987): Accessed on both
1297 // signaling and network thread.
Qingsi Wang25ec8882019-11-15 12:33:05 -08001298 std::unique_ptr<webrtc::IceTransportFactory>
1299 ice_transport_factory_; // TODO(bugs.webrtc.org/9987): Accessed on the
1300 // signaling thread but the underlying raw
1301 // pointer is given to
1302 // |jsep_transport_controller_| and used on the
1303 // network thread.
Karl Wibergfb3be392019-03-22 14:13:22 +01001304 std::unique_ptr<rtc::SSLCertificateVerifier>
1305 tls_cert_verifier_; // TODO(bugs.webrtc.org/9987): Accessed on both
1306 // signaling and network thread.
deadbeefab9b2d12015-10-14 11:33:11 -07001307
zhihuang8f65cdf2016-05-06 18:40:30 -07001308 // One PeerConnection has only one RTCP CNAME.
1309 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
Karl Wibergfb3be392019-03-22 14:13:22 +01001310 const std::string rtcp_cname_;
zhihuang8f65cdf2016-05-06 18:40:30 -07001311
deadbeefab9b2d12015-10-14 11:33:11 -07001312 // Streams added via AddStream.
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001313 const rtc::scoped_refptr<StreamCollection> local_streams_
1314 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001315 // Streams created as a result of SetRemoteDescription.
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001316 const rtc::scoped_refptr<StreamCollection> remote_streams_
1317 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001318
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001319 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
1320 RTC_GUARDED_BY(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -08001321
Steve Anton4171afb2017-11-20 10:20:22 -08001322 // These lists store sender info seen in local/remote descriptions.
Karl Wibergc70999e2019-03-22 15:14:27 +01001323 std::vector<RtpSenderInfo> remote_audio_sender_infos_
1324 RTC_GUARDED_BY(signaling_thread());
1325 std::vector<RtpSenderInfo> remote_video_sender_infos_
1326 RTC_GUARDED_BY(signaling_thread());
1327 std::vector<RtpSenderInfo> local_audio_sender_infos_
1328 RTC_GUARDED_BY(signaling_thread());
1329 std::vector<RtpSenderInfo> local_video_sender_infos_
1330 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001331
Karl Wiberg85a4a932019-03-22 15:29:58 +01001332 SctpSidAllocator sid_allocator_ RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001333 // label -> DataChannel
Karl Wiberg85a4a932019-03-22 15:29:58 +01001334 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_
1335 RTC_GUARDED_BY(signaling_thread());
1336 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_
1337 RTC_GUARDED_BY(signaling_thread());
1338 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_
1339 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001340
Karl Wiberg85a4a932019-03-22 15:29:58 +01001341 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
deadbeef70ab1a12015-09-28 16:53:55 -07001342
Karl Wibergac025892019-03-26 13:08:37 +01001343 // The unique_ptr belongs to the worker thread, but the Call object manages
1344 // its own thread safety.
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001345 std::unique_ptr<Call> call_ RTC_GUARDED_BY(worker_thread());
1346
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +02001347 rtc::AsyncInvoker rtcp_invoker_ RTC_GUARDED_BY(network_thread());
1348
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001349 // Points to the same thing as `call_`. Since it's const, we may read the
Karl Wibergac025892019-03-26 13:08:37 +01001350 // pointer from any thread.
1351 Call* const call_ptr_;
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001352
1353 std::unique_ptr<StatsCollector> stats_
1354 RTC_GUARDED_BY(signaling_thread()); // A pointer is passed to senders_
1355 rtc::scoped_refptr<RTCStatsCollector> stats_collector_
1356 RTC_GUARDED_BY(signaling_thread());
Eldar Rello5ab79e62019-10-09 18:29:44 +03001357 // Holds changes made to transceivers during applying descriptors for
1358 // potential rollback. Gets cleared once signaling state goes to stable.
1359 std::map<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>,
1360 TransceiverStableState>
1361 transceiver_stable_states_by_transceivers_;
deadbeefa601f5c2016-06-06 14:27:39 -07001362 std::vector<
Steve Anton4171afb2017-11-20 10:20:22 -08001363 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001364 transceivers_; // TODO(bugs.webrtc.org/9987): Accessed on both signaling
1365 // and network thread.
1366
Henrik Boström5b147782018-12-04 11:25:05 +01001367 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
1368 // line we create and use a stream with a random ID for our receivers. This is
1369 // to support legacy endpoints that do not support the a=msid attribute (as
1370 // opposed to streamless tracks with "a=msid:-").
Karl Wiberga58e1692019-03-26 13:33:43 +01001371 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
1372 RTC_GUARDED_BY(signaling_thread());
Amit Hilbuchae3df542019-01-07 12:13:08 -08001373 // MIDs will be generated using this generator which will keep track of
1374 // all the MIDs that have been seen over the life of the PeerConnection.
Karl Wiberga58e1692019-03-26 13:33:43 +01001375 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001376
Karl Wiberga58e1692019-03-26 13:33:43 +01001377 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
1378 SessionError::kNone;
1379 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001380
Karl Wiberga58e1692019-03-26 13:33:43 +01001381 std::string session_id_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001382
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001383 std::unique_ptr<JsepTransportController>
1384 transport_controller_; // TODO(bugs.webrtc.org/9987): Accessed on both
1385 // signaling and network thread.
1386 std::unique_ptr<cricket::SctpTransportInternalFactory>
1387 sctp_factory_; // TODO(bugs.webrtc.org/9987): Accessed on both
1388 // signaling and network thread.
Steve Anton75737c02017-11-06 10:37:17 -08001389 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
1390 // when using SCTP.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001391 cricket::RtpDataChannel* rtp_data_channel_ =
1392 nullptr; // TODO(bugs.webrtc.org/9987): Accessed on both
1393 // signaling and some other thread.
Steve Anton75737c02017-11-06 10:37:17 -08001394
Zhi Huange830e682018-03-30 10:48:35 -07001395 // |sctp_mid_| is the content name (MID) in SDP.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001396 // Note: this is used as the data channel MID by both SCTP and data channel
1397 // transports. It is set when either transport is initialized and unset when
1398 // both transports are deleted.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001399 absl::optional<std::string>
1400 sctp_mid_; // TODO(bugs.webrtc.org/9987): Accessed on both signaling
1401 // and network thread.
1402
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001403 // Whether this peer is the caller. Set when the local description is applied.
1404 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
1405
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001406 // Plugin transport used for data channels. Pointer may be accessed and
1407 // checked from any thread, but the object may only be touched on the
1408 // network thread.
1409 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network thread.
1410 DataChannelTransportInterface* data_channel_transport_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001411
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001412 // Cached value of whether the data channel transport is ready to send.
1413 bool data_channel_transport_ready_to_send_
1414 RTC_GUARDED_BY(signaling_thread()) = false;
1415
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001416 // Used to invoke data channel transport signals on the signaling thread.
1417 std::unique_ptr<rtc::AsyncInvoker> data_channel_transport_invoker_
Karl Wiberg739506e2019-04-03 11:37:28 +02001418 RTC_GUARDED_BY(network_thread());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001419
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001420 // Signals from |data_channel_transport_|. These are invoked on the signaling
1421 // thread.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001422 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001423 RTC_GUARDED_BY(signaling_thread());
1424 sigslot::signal2<const cricket::ReceiveDataParams&,
1425 const rtc::CopyOnWriteBuffer&>
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001426 SignalDataChannelTransportReceivedData_s
1427 RTC_GUARDED_BY(signaling_thread());
1428 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001429 RTC_GUARDED_BY(signaling_thread());
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001430 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001431 RTC_GUARDED_BY(signaling_thread());
1432
Karl Wiberg739506e2019-04-03 11:37:28 +02001433 std::unique_ptr<SessionDescriptionInterface> current_local_description_
1434 RTC_GUARDED_BY(signaling_thread());
1435 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
1436 RTC_GUARDED_BY(signaling_thread());
1437 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
1438 RTC_GUARDED_BY(signaling_thread());
1439 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
1440 RTC_GUARDED_BY(signaling_thread());
1441 bool dtls_enabled_ RTC_GUARDED_BY(signaling_thread()) = false;
Steve Anton75737c02017-11-06 10:37:17 -08001442 // Specifies which kind of data channel is allowed. This is controlled
1443 // by the chrome command-line flag and constraints:
1444 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
1445 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
1446 // not set or false, SCTP is allowed (DCT_SCTP);
1447 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
1448 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
Karl Wibergf73f7d62019-04-08 15:36:53 +02001449 cricket::DataChannelType data_channel_type_ =
1450 cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both
1451 // signaling and network thread.
Steve Anton75737c02017-11-06 10:37:17 -08001452
Karl Wibergf73f7d62019-04-08 15:36:53 +02001453 // List of content names for which the remote side triggered an ICE restart.
1454 std::set<std::string> pending_ice_restarts_
1455 RTC_GUARDED_BY(signaling_thread());
1456
1457 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
1458 RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001459
1460 // Member variables for caching global options.
Karl Wibergf73f7d62019-04-08 15:36:53 +02001461 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
1462 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001463
Karl Wibergf73f7d62019-04-08 15:36:53 +02001464 int usage_event_accumulator_ RTC_GUARDED_BY(signaling_thread()) = 0;
1465 bool return_histogram_very_quickly_ RTC_GUARDED_BY(signaling_thread()) =
1466 false;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001467
1468 // This object should be used to generate any SSRC that is not explicitly
1469 // specified by the user (or by the remote party).
1470 // The generator is not used directly, instead it is passed on to the
1471 // channel manager and the session description factory.
Karl Wibergf73f7d62019-04-08 15:36:53 +02001472 rtc::UniqueRandomIdGenerator ssrc_generator_
1473 RTC_GUARDED_BY(signaling_thread());
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +02001474
1475 // A video bitrate allocator factory.
1476 // This can injected using the PeerConnectionDependencies,
1477 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
1478 // Note that one can still choose to override this in a MediaEngine
1479 // if one wants too.
1480 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
1481 video_bitrate_allocator_factory_;
1482
Henrik Boström79b69802019-07-18 11:16:56 +02001483 std::unique_ptr<LocalIceCredentialsToReplace>
1484 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001485 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
Henrik Boströma3728d32019-10-28 12:09:49 +01001486
1487 rtc::WeakPtrFactory<PeerConnection> weak_ptr_factory_
1488 RTC_GUARDED_BY(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489};
1490
1491} // namespace webrtc
1492
Steve Anton10542f22019-01-11 09:11:00 -08001493#endif // PC_PEER_CONNECTION_H_