blob: c783ae9e218de5c73dad14b86829df5fd8329d09 [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"
Karl Wiberg5966c502019-02-21 23:55:09 +010037#include "rtc_base/race_checker.h"
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080038#include "rtc_base/unique_id_generator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
40namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
deadbeefeb459812015-12-15 19:24:43 -080042class MediaStreamObserver;
perkjf0dcfe22016-03-10 18:32:00 +010043class VideoRtpReceiver;
skvlad11a9cbf2016-10-07 11:53:05 -070044class RtcEventLog;
deadbeefab9b2d12015-10-14 11:33:11 -070045
Steve Anton75737c02017-11-06 10:37:17 -080046// PeerConnection is the implementation of the PeerConnection object as defined
47// by the PeerConnectionInterface API surface.
48// The class currently is solely responsible for the following:
49// - Managing the session state machine (signaling state).
50// - Creating and initializing lower-level objects, like PortAllocator and
51// BaseChannels.
52// - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
53// objects.
54// - Tracking the current and pending local/remote session descriptions.
55// The class currently is jointly responsible for the following:
56// - Parsing and interpreting SDP.
57// - Generating offers and answers based on the current state.
58// - The ICE state machine.
59// - Generating stats.
Steve Anton2d8609c2018-01-23 16:38:46 -080060class PeerConnection : public PeerConnectionInternal,
Steve Anton75737c02017-11-06 10:37:17 -080061 public DataChannelProviderInterface,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080062 public DataChannelSink,
Zhi Huang365381f2018-04-13 16:44:34 -070063 public JsepTransportController::Observer,
Guido Urdaneta1ff16c82019-05-20 19:31:53 +020064 public RtpSenderBase::SetStreamsObserver,
Steve Antonad182762018-09-05 20:22:40 +000065 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 public sigslot::has_slots<> {
67 public:
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070068 // A bit in the usage pattern is registered when its defining event occurs at
69 // least once.
Harald Alvestrand8ebba742018-05-31 14:00:34 +020070 enum class UsageEvent : int {
71 TURN_SERVER_ADDED = 0x01,
72 STUN_SERVER_ADDED = 0x02,
73 DATA_ADDED = 0x04,
74 AUDIO_ADDED = 0x08,
75 VIDEO_ADDED = 0x10,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070076 // |SetLocalDescription| returns successfully.
77 SET_LOCAL_DESCRIPTION_SUCCEEDED = 0x20,
78 // |SetRemoteDescription| returns successfully.
79 SET_REMOTE_DESCRIPTION_SUCCEEDED = 0x40,
80 // A local candidate (with type host, server-reflexive, or relay) is
81 // collected.
Harald Alvestrand8ebba742018-05-31 14:00:34 +020082 CANDIDATE_COLLECTED = 0x80,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070083 // A remote candidate is successfully added via |AddIceCandidate|.
84 ADD_ICE_CANDIDATE_SUCCEEDED = 0x100,
Harald Alvestrand8ebba742018-05-31 14:00:34 +020085 ICE_STATE_CONNECTED = 0x200,
Qingsi Wang7fc821d2018-07-12 12:54:53 -070086 CLOSE_CALLED = 0x400,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070087 // A local candidate with private IP is collected.
Harald Alvestrand056d8112018-07-16 19:18:58 +020088 PRIVATE_CANDIDATE_COLLECTED = 0x800,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070089 // A remote candidate with private IP is added, either via AddiceCandidate
90 // or from the remote description.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070091 REMOTE_PRIVATE_CANDIDATE_ADDED = 0x1000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070092 // A local mDNS candidate is collected.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070093 MDNS_CANDIDATE_COLLECTED = 0x2000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070094 // A remote mDNS candidate is added, either via AddIceCandidate or from the
95 // remote description.
Jeroen de Borstaf242c82019-04-24 13:13:48 -070096 REMOTE_MDNS_CANDIDATE_ADDED = 0x4000,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070097 // A local candidate with IPv6 address is collected.
98 IPV6_CANDIDATE_COLLECTED = 0x8000,
99 // A remote candidate with IPv6 address is added, either via AddIceCandidate
100 // or from the remote description.
101 REMOTE_IPV6_CANDIDATE_ADDED = 0x10000,
102 // A remote candidate (with type host, server-reflexive, or relay) is
103 // successfully added, either via AddIceCandidate or from the remote
104 // description.
105 REMOTE_CANDIDATE_ADDED = 0x20000,
Qingsi Wangcc46b102019-09-12 11:19:01 -0700106 // An explicit host-host candidate pair is selected, i.e. both the local and
107 // the remote candidates have the host type. This does not include candidate
108 // pairs formed with equivalent prflx remote candidates, e.g. a host-prflx
109 // pair where the prflx candidate has the same base as a host candidate of
110 // the remote peer.
111 DIRECT_CONNECTION_SELECTED = 0x40000,
112 MAX_VALUE = 0x80000,
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200113 };
114
zhihuang38ede132017-06-15 12:52:32 -0700115 explicit PeerConnection(PeerConnectionFactory* factory,
116 std::unique_ptr<RtcEventLog> event_log,
117 std::unique_ptr<Call> call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
deadbeef653b8e02015-11-11 12:55:10 -0800119 bool Initialize(
120 const PeerConnectionInterface::RTCConfiguration& configuration,
Benjamin Wrightcab58882018-05-02 15:12:47 -0700121 PeerConnectionDependencies dependencies);
deadbeef653b8e02015-11-11 12:55:10 -0800122
deadbeefa67696b2015-09-29 11:56:26 -0700123 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
124 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
125 bool AddStream(MediaStreamInterface* local_stream) override;
126 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
Steve Anton2d6c76a2018-01-05 17:10:52 -0800128 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
Steve Antonf9381f02017-12-14 10:23:57 -0800129 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800130 const std::vector<std::string>& stream_ids) override;
deadbeefe1f9d832016-01-14 15:35:42 -0800131 bool RemoveTrack(RtpSenderInterface* sender) override;
Steve Anton24db5732018-07-23 10:27:33 -0700132 RTCError RemoveTrackNew(
133 rtc::scoped_refptr<RtpSenderInterface> sender) override;
deadbeefe1f9d832016-01-14 15:35:42 -0800134
Steve Anton9158ef62017-11-27 13:01:52 -0800135 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
136 rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
137 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
138 rtc::scoped_refptr<MediaStreamTrackInterface> track,
139 const RtpTransceiverInit& init) override;
140 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
141 cricket::MediaType media_type) override;
142 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
143 cricket::MediaType media_type,
144 const RtpTransceiverInit& init) override;
145
Steve Anton8c0f7a72017-10-03 10:03:10 -0700146 // Gets the DTLS SSL certificate associated with the audio transport on the
147 // remote side. This will become populated once the DTLS connection with the
148 // peer has been completed, as indicated by the ICE connection state
149 // transitioning to kIceConnectionCompleted.
150 // Note that this will be removed once we implement RTCDtlsTransport which
151 // has standardized method for getting this information.
152 // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
153 std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
154
Zhi Huang70b820f2018-01-27 14:16:15 -0800155 // Version of the above method that returns the full certificate chain.
156 std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
157
deadbeeffac06552015-11-25 11:26:01 -0800158 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -0800159 const std::string& kind,
160 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -0800161
deadbeef70ab1a12015-09-28 16:53:55 -0700162 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
163 const override;
164 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
165 const override;
Steve Anton9158ef62017-11-27 13:01:52 -0800166 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
167 const override;
deadbeef70ab1a12015-09-28 16:53:55 -0700168
deadbeefa67696b2015-09-29 11:56:26 -0700169 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700171 const DataChannelInit* config) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100172 // WARNING: LEGACY. See peerconnectioninterface.h
deadbeefa67696b2015-09-29 11:56:26 -0700173 bool GetStats(StatsObserver* observer,
174 webrtc::MediaStreamTrackInterface* track,
175 StatsOutputLevel level) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100176 // Spec-complaint GetStats(). See peerconnectioninterface.h
hbos74e1a4f2016-09-15 23:33:01 -0700177 void GetStats(RTCStatsCollectorCallback* callback) override;
Henrik Boström1df1bf82018-03-20 13:24:20 +0100178 void GetStats(
179 rtc::scoped_refptr<RtpSenderInterface> selector,
180 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
181 void GetStats(
182 rtc::scoped_refptr<RtpReceiverInterface> selector,
183 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
Harald Alvestrand89061872018-01-02 14:08:34 +0100184 void ClearStatsCache() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185
deadbeefa67696b2015-09-29 11:56:26 -0700186 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
deadbeefa67696b2015-09-29 11:56:26 -0700188 IceConnectionState ice_connection_state() override;
Jonas Olsson12046902018-12-06 11:25:14 +0100189 IceConnectionState standardized_ice_connection_state() override;
Jonas Olsson635474e2018-10-18 15:58:17 +0200190 PeerConnectionState peer_connection_state() override;
deadbeefa67696b2015-09-29 11:56:26 -0700191 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
deadbeefa67696b2015-09-29 11:56:26 -0700193 const SessionDescriptionInterface* local_description() const override;
194 const SessionDescriptionInterface* remote_description() const override;
deadbeeffe4a8a42016-12-20 17:56:17 -0800195 const SessionDescriptionInterface* current_local_description() const override;
196 const SessionDescriptionInterface* current_remote_description()
197 const override;
198 const SessionDescriptionInterface* pending_local_description() const override;
199 const SessionDescriptionInterface* pending_remote_description()
200 const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201
Henrik Boström79b69802019-07-18 11:16:56 +0200202 void RestartIce() override;
203
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700205 void CreateOffer(CreateSessionDescriptionObserver* observer,
206 const RTCOfferAnswerOptions& options) override;
htaa2a49d92016-03-04 02:51:39 -0800207 void CreateAnswer(CreateSessionDescriptionObserver* observer,
208 const RTCOfferAnswerOptions& options) override;
deadbeefa67696b2015-09-29 11:56:26 -0700209 void SetLocalDescription(SetSessionDescriptionObserver* observer,
210 SessionDescriptionInterface* desc) override;
Henrik Boströma4ecf552017-11-23 14:17:07 +0000211 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
212 SessionDescriptionInterface* desc) override;
Henrik Boström31638672017-11-23 17:48:32 +0100213 void SetRemoteDescription(
214 std::unique_ptr<SessionDescriptionInterface> desc,
215 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
216 override;
deadbeef46c73892016-11-16 19:42:04 -0800217 PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
Niels Möller2579f0c2019-08-19 09:58:17 +0200218 RTCError SetConfiguration(
219 const PeerConnectionInterface::RTCConfiguration& configuration) override;
deadbeefa67696b2015-09-29 11:56:26 -0700220 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700221 bool RemoveIceCandidates(
222 const std::vector<cricket::Candidate>& candidates) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223
Niels Möller0c4f7be2018-05-07 14:01:37 +0200224 RTCError SetBitrate(const BitrateSettings& bitrate) override;
zstein4b979802017-06-02 14:37:37 -0700225
henrika5f6bf242017-11-01 11:06:56 +0100226 void SetAudioPlayout(bool playout) override;
227 void SetAudioRecording(bool recording) override;
228
Harald Alvestrandad88c882018-11-28 16:47:46 +0100229 rtc::scoped_refptr<DtlsTransportInterface> LookupDtlsTransportByMid(
230 const std::string& mid) override;
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100231 rtc::scoped_refptr<DtlsTransport> LookupDtlsTransportByMidInternal(
232 const std::string& mid);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100233
Harald Alvestrandc85328f2019-02-28 07:51:00 +0100234 rtc::scoped_refptr<SctpTransportInterface> GetSctpTransport() const override;
235
Bjorn Tereliusde939432017-11-20 17:38:14 +0100236 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
237 int64_t output_period_ms) override;
Niels Möllerf00ca1a2019-05-10 11:33:12 +0200238 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override;
ivoc14d5dbe2016-07-04 07:06:55 -0700239 void StopRtcEventLog() override;
240
deadbeefa67696b2015-09-29 11:56:26 -0700241 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242
Steve Anton2d8609c2018-01-23 16:38:46 -0800243 // PeerConnectionInternal implementation.
Karl Wiberg4ae63472019-02-22 00:57:06 +0100244 rtc::Thread* network_thread() const final {
Steve Anton2d8609c2018-01-23 16:38:46 -0800245 return factory_->network_thread();
246 }
Karl Wiberg4ae63472019-02-22 00:57:06 +0100247 rtc::Thread* worker_thread() const final { return factory_->worker_thread(); }
248 rtc::Thread* signaling_thread() const final {
Steve Anton2d8609c2018-01-23 16:38:46 -0800249 return factory_->signaling_thread();
perkjd61bf802016-03-24 03:16:19 -0700250 }
deadbeefab9b2d12015-10-14 11:33:11 -0700251
Karl Wiberga58e1692019-03-26 13:33:43 +0100252 std::string session_id() const override {
253 RTC_DCHECK_RUN_ON(signaling_thread());
254 return session_id_;
255 }
Steve Anton75737c02017-11-06 10:37:17 -0800256
Steve Anton2d8609c2018-01-23 16:38:46 -0800257 bool initial_offerer() const override {
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200258 RTC_DCHECK_RUN_ON(signaling_thread());
Zhi Huange830e682018-03-30 10:48:35 -0700259 return transport_controller_ && transport_controller_->initial_offerer();
Steve Anton2d8609c2018-01-23 16:38:46 -0800260 }
Steve Anton75737c02017-11-06 10:37:17 -0800261
Steve Anton2d8609c2018-01-23 16:38:46 -0800262 std::vector<
263 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Steve Antonb8867112018-02-13 10:07:54 -0800264 GetTransceiversInternal() const override {
Karl Wiberga58e1692019-03-26 13:33:43 +0100265 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -0800266 return transceivers_;
267 }
268
Steve Anton2d8609c2018-01-23 16:38:46 -0800269 sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
270 return SignalDataChannelCreated_;
271 }
272
273 cricket::RtpDataChannel* rtp_data_channel() const override {
Steve Anton75737c02017-11-06 10:37:17 -0800274 return rtp_data_channel_;
Steve Anton978b8762017-09-29 12:15:02 -0700275 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800276
Steve Antonbe5e2082018-01-24 15:29:17 -0800277 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels()
Steve Anton2d8609c2018-01-23 16:38:46 -0800278 const override {
Karl Wiberg85a4a932019-03-22 15:29:58 +0100279 RTC_DCHECK_RUN_ON(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -0800280 return sctp_data_channels_;
281 }
282
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200283 absl::optional<std::string> sctp_content_name() const override {
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200284 RTC_DCHECK_RUN_ON(signaling_thread());
Zhi Huange830e682018-03-30 10:48:35 -0700285 return sctp_mid_;
Steve Anton75737c02017-11-06 10:37:17 -0800286 }
Steve Anton2d8609c2018-01-23 16:38:46 -0800287
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200288 absl::optional<std::string> sctp_transport_name() const override;
Steve Anton75737c02017-11-06 10:37:17 -0800289
Qingsi Wang72a43a12018-02-20 16:03:18 -0800290 cricket::CandidateStatsList GetPooledCandidateStats() const override;
Steve Anton5dfde182018-02-06 10:34:40 -0800291 std::map<std::string, std::string> GetTransportNamesByMid() const override;
292 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
293 const std::set<std::string>& transport_names) override;
Steve Anton2d8609c2018-01-23 16:38:46 -0800294 Call::Stats GetCallStats() override;
Steve Anton75737c02017-11-06 10:37:17 -0800295
Steve Anton2d8609c2018-01-23 16:38:46 -0800296 bool GetLocalCertificate(
297 const std::string& transport_name,
298 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800299 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
Steve Anton2d8609c2018-01-23 16:38:46 -0800300 const std::string& transport_name) override;
301 bool IceRestartPending(const std::string& content_name) const override;
302 bool NeedsIceRestart(const std::string& content_name) const override;
303 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
Steve Anton7464fca2018-01-19 11:10:37 -0800304
Harald Alvestrand19793842018-06-25 12:03:50 +0200305 void ReturnHistogramVeryQuicklyForTesting() {
Karl Wibergf73f7d62019-04-08 15:36:53 +0200306 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand19793842018-06-25 12:03:50 +0200307 return_histogram_very_quickly_ = true;
308 }
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200309 void RequestUsagePatternReportForTesting();
Harald Alvestrand19793842018-06-25 12:03:50 +0200310
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700312 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313
314 private:
Henrik Boström31638672017-11-23 17:48:32 +0100315 class SetRemoteDescriptionObserverAdapter;
316 friend class SetRemoteDescriptionObserverAdapter;
Henrik Boström79b69802019-07-18 11:16:56 +0200317 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
318 // It makes the next CreateOffer() produce new ICE credentials even if
319 // RTCOfferAnswerOptions::ice_restart is false.
320 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
321 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
322 // move this type of logic to JsepTransportController/JsepTransport.
323 class LocalIceCredentialsToReplace;
Henrik Boström31638672017-11-23 17:48:32 +0100324
Steve Anton4171afb2017-11-20 10:20:22 -0800325 struct RtpSenderInfo {
326 RtpSenderInfo() : first_ssrc(0) {}
Seth Hampson845e8782018-03-02 11:34:10 -0800327 RtpSenderInfo(const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800328 const std::string sender_id,
329 uint32_t ssrc)
Seth Hampson845e8782018-03-02 11:34:10 -0800330 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
Steve Anton4171afb2017-11-20 10:20:22 -0800331 bool operator==(const RtpSenderInfo& other) {
Seth Hampson845e8782018-03-02 11:34:10 -0800332 return this->stream_id == other.stream_id &&
Steve Anton4171afb2017-11-20 10:20:22 -0800333 this->sender_id == other.sender_id &&
334 this->first_ssrc == other.first_ssrc;
deadbeefbda7e0b2015-12-08 17:13:40 -0800335 }
Seth Hampson845e8782018-03-02 11:34:10 -0800336 std::string stream_id;
Steve Anton4171afb2017-11-20 10:20:22 -0800337 std::string sender_id;
338 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
339 // for communicating with the lower layers.
340 uint32_t first_ssrc;
deadbeefab9b2d12015-10-14 11:33:11 -0700341 };
deadbeefab9b2d12015-10-14 11:33:11 -0700342
Bjorn A Mellem5985a042019-06-28 14:19:38 -0700343 // Field-trial based configuration for datagram transport.
344 struct DatagramTransportConfig {
345 explicit DatagramTransportConfig(const std::string& field_trial)
346 : enabled("enabled", true), default_value("default_value", false) {
347 ParseFieldTrial({&enabled, &default_value}, field_trial);
348 }
349
350 // Whether datagram transport support is enabled at all. Defaults to true,
351 // allowing datagram transport to be used if (a) the application provides a
352 // factory for it and (b) the configuration specifies its use. This flag
353 // provides a kill-switch to force-disable datagram transport across all
354 // applications, without code changes.
355 FieldTrialFlag enabled;
356
357 // Whether the datagram transport is enabled or disabled by default.
358 // Defaults to false, meaning that applications must configure use of
359 // datagram transport through RTCConfiguration. If set to true,
360 // applications will use the datagram transport by default (but may still
361 // explicitly configure themselves not to use it through RTCConfiguration).
362 FieldTrialFlag default_value;
363 };
364
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700365 // Field-trial based configuration for datagram transport data channels.
366 struct DatagramTransportDataChannelConfig {
367 explicit DatagramTransportDataChannelConfig(const std::string& field_trial)
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700368 : enabled("enabled", true),
369 default_value("default_value", false),
370 receive_only("receive_only", false) {
371 ParseFieldTrial({&enabled, &default_value, &receive_only}, field_trial);
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700372 }
373
374 // Whether datagram transport data channel support is enabled at all.
375 // Defaults to true, allowing datagram transport to be used if (a) the
376 // application provides a factory for it and (b) the configuration specifies
377 // its use. This flag provides a kill-switch to force-disable datagram
378 // transport across all applications, without code changes.
379 FieldTrialFlag enabled;
380
381 // Whether the datagram transport data channels are enabled or disabled by
382 // default. Defaults to false, meaning that applications must configure use
383 // of datagram transport through RTCConfiguration. If set to true,
384 // applications will use the datagram transport by default (but may still
385 // explicitly configure themselves not to use it through RTCConfiguration).
386 FieldTrialFlag default_value;
Bjorn A Mellem7da4e562019-09-26 11:02:11 -0700387
388 // Whether the datagram transport is enabled in receive-only mode. If true,
389 // and if the datagram transport is enabled, it will only be used when
390 // receiving incoming calls, not when placing outgoing calls.
391 FieldTrialFlag receive_only;
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700392 };
393
Steve Antonad182762018-09-05 20:22:40 +0000394 // Implements MessageHandler.
395 void OnMessage(rtc::Message* msg) override;
396
Steve Antonafb0bb72018-02-20 11:35:37 -0800397 // Plan B helpers for getting the voice/video media channels for the single
398 // audio/video transceiver, if it exists.
Karl Wiberg5966c502019-02-21 23:55:09 +0100399 cricket::VoiceMediaChannel* voice_media_channel() const
400 RTC_RUN_ON(signaling_thread());
401 cricket::VideoMediaChannel* video_media_channel() const
402 RTC_RUN_ON(signaling_thread());
Steve Anton60776752018-01-10 11:51:34 -0800403
Steve Anton4171afb2017-11-20 10:20:22 -0800404 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100405 GetSendersInternal() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800406 std::vector<
407 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100408 GetReceiversInternal() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800409
410 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100411 GetAudioTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800412 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100413 GetVideoTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800414
Steve Antonafb0bb72018-02-20 11:35:37 -0800415 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100416 GetFirstAudioTransceiver() const RTC_RUN_ON(signaling_thread());
Steve Antonafb0bb72018-02-20 11:35:37 -0800417
deadbeefab9b2d12015-10-14 11:33:11 -0700418 void CreateAudioReceiver(MediaStreamInterface* stream,
Karl Wiberg744310f2019-02-14 10:18:56 +0100419 const RtpSenderInfo& remote_sender_info)
420 RTC_RUN_ON(signaling_thread());
perkjf0dcfe22016-03-10 18:32:00 +0100421
deadbeefab9b2d12015-10-14 11:33:11 -0700422 void CreateVideoReceiver(MediaStreamInterface* stream,
Karl Wiberg744310f2019-02-14 10:18:56 +0100423 const RtpSenderInfo& remote_sender_info)
424 RTC_RUN_ON(signaling_thread());
Henrik Boström933d8b02017-10-10 10:05:16 -0700425 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
Karl Wiberg5966c502019-02-21 23:55:09 +0100426 const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700427
428 // May be called either by AddStream/RemoveStream, or when a track is
429 // added/removed from a stream previously added via AddStream.
Karl Wiberg5966c502019-02-21 23:55:09 +0100430 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream)
431 RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700432 void RemoveAudioTrack(AudioTrackInterface* track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100433 MediaStreamInterface* stream)
434 RTC_RUN_ON(signaling_thread());
435 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream)
436 RTC_RUN_ON(signaling_thread());
korniltsev.anatolyec390b52017-07-24 17:00:25 -0700437 void RemoveVideoTrack(VideoTrackInterface* track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100438 MediaStreamInterface* stream)
439 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440
Steve Antonf9381f02017-12-14 10:23:57 -0800441 // AddTrack implementation when Unified Plan is specified.
442 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
443 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Karl Wiberga58e1692019-03-26 13:33:43 +0100444 const std::vector<std::string>& stream_ids)
445 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800446 // AddTrack implementation when Plan B is specified.
447 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
448 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Karl Wiberg5966c502019-02-21 23:55:09 +0100449 const std::vector<std::string>& stream_ids)
450 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800451
452 // Returns the first RtpTransceiver suitable for a newly added track, if such
453 // transceiver is available.
454 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
455 FindFirstTransceiverForAddedTrack(
Karl Wiberga58e1692019-03-26 13:33:43 +0100456 rtc::scoped_refptr<MediaStreamTrackInterface> track)
457 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800458
Steve Antonf9381f02017-12-14 10:23:57 -0800459 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100460 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender)
461 RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800462
Steve Anton22da89f2018-01-25 13:58:07 -0800463 // Internal implementation for AddTransceiver family of methods. If
464 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
Steve Anton9158ef62017-11-27 13:01:52 -0800465 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
466 cricket::MediaType media_type,
467 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton22da89f2018-01-25 13:58:07 -0800468 const RtpTransceiverInit& init,
Karl Wiberg744310f2019-02-14 10:18:56 +0100469 bool fire_callback = true) RTC_RUN_ON(signaling_thread());
Steve Anton9158ef62017-11-27 13:01:52 -0800470
Steve Anton02ee47c2018-01-10 16:26:06 -0800471 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
472 CreateSender(cricket::MediaType media_type,
Steve Anton111fdfd2018-06-25 13:03:36 -0700473 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -0800474 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Florent Castelli892acf02018-10-01 22:47:20 +0200475 const std::vector<std::string>& stream_ids,
476 const std::vector<RtpEncodingParameters>& send_encodings);
Steve Anton02ee47c2018-01-10 16:26:06 -0800477
478 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
479 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
480
Steve Antonf9381f02017-12-14 10:23:57 -0800481 // Create a new RtpTransceiver of the given type and add it to the list of
482 // transceivers.
483 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Steve Anton02ee47c2018-01-10 16:26:06 -0800484 CreateAndAddTransceiver(
485 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
486 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100487 receiver) RTC_RUN_ON(signaling_thread());
Steve Antonf9381f02017-12-14 10:23:57 -0800488
Karl Wiberg744310f2019-02-14 10:18:56 +0100489 void SetIceConnectionState(IceConnectionState new_state)
490 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200491 void SetStandardizedIceConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100492 PeerConnectionInterface::IceConnectionState new_state)
493 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200494 void SetConnectionState(
Karl Wiberg744310f2019-02-14 10:18:56 +0100495 PeerConnectionInterface::PeerConnectionState new_state)
496 RTC_RUN_ON(signaling_thread());
Jonas Olsson635474e2018-10-18 15:58:17 +0200497
Eldar Relloda13ea22019-06-01 12:23:43 +0300498 // Called any time the IceGatheringState changes.
Karl Wiberg744310f2019-02-14 10:18:56 +0100499 void OnIceGatheringChange(IceGatheringState new_state)
500 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800501 // New ICE candidate has been gathered.
Karl Wiberg744310f2019-02-14 10:18:56 +0100502 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)
503 RTC_RUN_ON(signaling_thread());
Eldar Relloda13ea22019-06-01 12:23:43 +0300504 // Gathering of an ICE candidate failed.
505 void OnIceCandidateError(const std::string& host_candidate,
506 const std::string& url,
507 int error_code,
508 const std::string& error_text)
509 RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800510 // Some local ICE candidates have been removed.
Karl Wiberg744310f2019-02-14 10:18:56 +0100511 void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates)
512 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513
Alex Drake00c7ecf2019-08-06 10:54:47 -0700514 void OnSelectedCandidatePairChanged(
515 const cricket::CandidatePairChangeEvent& event)
516 RTC_RUN_ON(signaling_thread());
517
Steve Antonba818672017-11-06 10:21:57 -0800518 // Update the state, signaling if necessary.
Karl Wiberg744310f2019-02-14 10:18:56 +0100519 void ChangeSignalingState(SignalingState signaling_state)
520 RTC_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521
deadbeefeb459812015-12-15 19:24:43 -0800522 // Signals from MediaStreamObserver.
523 void OnAudioTrackAdded(AudioTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100524 MediaStreamInterface* stream)
525 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800526 void OnAudioTrackRemoved(AudioTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100527 MediaStreamInterface* stream)
528 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800529 void OnVideoTrackAdded(VideoTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100530 MediaStreamInterface* stream)
531 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800532 void OnVideoTrackRemoved(VideoTrackInterface* track,
Karl Wiberg744310f2019-02-14 10:18:56 +0100533 MediaStreamInterface* stream)
534 RTC_RUN_ON(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -0800535
Henrik Boström31638672017-11-23 17:48:32 +0100536 void PostSetSessionDescriptionSuccess(
537 SetSessionDescriptionObserver* observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
Steve Antonad182762018-09-05 20:22:40 +0000539 RTCError&& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700540 void PostCreateSessionDescriptionFailure(
541 CreateSessionDescriptionObserver* observer,
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100542 RTCError error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543
Steve Anton8a006912017-12-04 15:25:56 -0800544 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
545 // that return an RTCError instead of invoking a callback.
546 RTCError ApplyLocalDescription(
547 std::unique_ptr<SessionDescriptionInterface> desc);
548 RTCError ApplyRemoteDescription(
549 std::unique_ptr<SessionDescriptionInterface> desc);
550
Steve Antondcc3c022017-12-22 16:02:54 -0800551 // Updates the local RtpTransceivers according to the JSEP rules. Called as
552 // part of setting the local/remote description.
553 RTCError UpdateTransceiversAndDataChannels(
554 cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800555 const SessionDescriptionInterface& new_session,
556 const SessionDescriptionInterface* old_local_description,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100557 const SessionDescriptionInterface* old_remote_description)
558 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800559
560 // Either creates or destroys the transceiver's BaseChannel according to the
561 // given media section.
562 RTCError UpdateTransceiverChannel(
563 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
564 transceiver,
565 const cricket::ContentInfo& content,
Karl Wiberg5966c502019-02-21 23:55:09 +0100566 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800567
Steve Antonfa2260d2017-12-28 16:38:23 -0800568 // Either creates or destroys the local data channel according to the given
569 // media section.
570 RTCError UpdateDataChannel(cricket::ContentSource source,
571 const cricket::ContentInfo& content,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100572 const cricket::ContentGroup* bundle_group)
573 RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800574
Steve Antondcc3c022017-12-22 16:02:54 -0800575 // Associate the given transceiver according to the JSEP rules.
576 RTCErrorOr<
577 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
578 AssociateTransceiver(cricket::ContentSource source,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800579 SdpType type,
Steve Antondcc3c022017-12-22 16:02:54 -0800580 size_t mline_index,
581 const cricket::ContentInfo& content,
Seth Hampsonae8a90a2018-02-13 15:33:48 -0800582 const cricket::ContentInfo* old_local_content,
Karl Wiberg5966c502019-02-21 23:55:09 +0100583 const cricket::ContentInfo* old_remote_content)
584 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800585
586 // Returns the RtpTransceiver, if found, that is associated to the given MID.
587 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100588 GetAssociatedTransceiver(const std::string& mid) const
589 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800590
591 // Returns the RtpTransceiver, if found, that was assigned to the given mline
592 // index in CreateOffer.
593 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100594 GetTransceiverByMLineIndex(size_t mline_index) const
595 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800596
597 // Returns an RtpTransciever, if available, that can be used to receive the
598 // given media type according to JSEP rules.
599 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Karl Wiberg5966c502019-02-21 23:55:09 +0100600 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const
601 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800602
Steve Antoned10bd92017-12-05 10:52:59 -0800603 // Returns the media section in the given session description that is
604 // associated with the RtpTransceiver. Returns null if none found or this
605 // RtpTransceiver is not associated. Logic varies depending on the
606 // SdpSemantics specified in the configuration.
607 const cricket::ContentInfo* FindMediaSectionForTransceiver(
608 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
609 transceiver,
Karl Wiberg5966c502019-02-21 23:55:09 +0100610 const SessionDescriptionInterface* sdesc) const
611 RTC_RUN_ON(signaling_thread());
Steve Antoned10bd92017-12-05 10:52:59 -0800612
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100613 // Runs the algorithm **set the associated remote streams** specified in
614 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
615 void SetAssociatedRemoteStreams(
616 rtc::scoped_refptr<RtpReceiverInternal> receiver,
617 const std::vector<std::string>& stream_ids,
618 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100619 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
620 RTC_RUN_ON(signaling_thread());
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100621
Steve Anton0f5400a2018-07-17 14:25:36 -0700622 // Runs the algorithm **process the removal of a remote track** specified in
623 // the WebRTC specification.
624 // This method will update the following lists:
625 // |remove_list| is the list of transceivers for which the receiving track is
626 // being removed.
627 // |removed_streams| is the list of streams which no longer have a receiving
628 // track so should be removed.
629 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
630 void ProcessRemovalOfRemoteTrack(
631 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
632 transceiver,
633 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100634 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
635 RTC_RUN_ON(signaling_thread());
Steve Anton0f5400a2018-07-17 14:25:36 -0700636
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100637 void RemoveRemoteStreamsIfEmpty(
638 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
639 remote_streams,
Karl Wiberg1e1e1022019-03-22 14:27:22 +0100640 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
641 RTC_RUN_ON(signaling_thread());
Henrik Boströmafa07dd2018-12-20 11:06:02 +0100642
Steve Anton52d86772018-02-20 15:48:12 -0800643 void OnNegotiationNeeded();
644
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 bool IsClosed() const {
Karl Wiberg8d2e2282019-02-17 13:00:07 +0100646 RTC_DCHECK_RUN_ON(signaling_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 return signaling_state_ == PeerConnectionInterface::kClosed;
648 }
649
deadbeefab9b2d12015-10-14 11:33:11 -0700650 // Returns a MediaSessionOptions struct with options decided by |options|,
651 // the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800652 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
653 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100654 cricket::MediaSessionOptions* session_options)
655 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800656 void GetOptionsForPlanBOffer(
657 const PeerConnectionInterface::RTCOfferAnswerOptions&
658 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100659 cricket::MediaSessionOptions* session_options)
660 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800661 void GetOptionsForUnifiedPlanOffer(
662 const PeerConnectionInterface::RTCOfferAnswerOptions&
663 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100664 cricket::MediaSessionOptions* session_options)
665 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700666
Karl Wiberg5966c502019-02-21 23:55:09 +0100667 RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options)
668 RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800669 void RemoveRecvDirectionFromReceivingTransceiversOfType(
Karl Wiberga58e1692019-03-26 13:33:43 +0100670 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800671 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
672 std::vector<
673 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100674 GetReceivingTransceiversOfType(cricket::MediaType media_type)
675 RTC_RUN_ON(signaling_thread());
Steve Anton22da89f2018-01-25 13:58:07 -0800676
deadbeefab9b2d12015-10-14 11:33:11 -0700677 // Returns a MediaSessionOptions struct with options decided by
678 // |constraints|, the local MediaStreams and DataChannels.
Steve Antondcc3c022017-12-22 16:02:54 -0800679 void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100680 cricket::MediaSessionOptions* session_options)
681 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800682 void GetOptionsForPlanBAnswer(
683 const PeerConnectionInterface::RTCOfferAnswerOptions&
684 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100685 cricket::MediaSessionOptions* session_options)
686 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -0800687 void GetOptionsForUnifiedPlanAnswer(
688 const PeerConnectionInterface::RTCOfferAnswerOptions&
689 offer_answer_options,
Karl Wiberg5966c502019-02-21 23:55:09 +0100690 cricket::MediaSessionOptions* session_options)
691 RTC_RUN_ON(signaling_thread());
htaa2a49d92016-03-04 02:51:39 -0800692
zhihuang1c378ed2017-08-17 14:10:50 -0700693 // Generates MediaDescriptionOptions for the |session_opts| based on existing
694 // local description or remote description.
695 void GenerateMediaDescriptionOptions(
696 const SessionDescriptionInterface* session_desc,
Steve Anton1d03a752017-11-27 14:30:09 -0800697 RtpTransceiverDirection audio_direction,
698 RtpTransceiverDirection video_direction,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200699 absl::optional<size_t>* audio_index,
700 absl::optional<size_t>* video_index,
701 absl::optional<size_t>* data_index,
Karl Wiberg85a4a932019-03-22 15:29:58 +0100702 cricket::MediaSessionOptions* session_options)
703 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700704
Steve Antonfa2260d2017-12-28 16:38:23 -0800705 // Generates the active MediaDescriptionOptions for the local data channel
706 // given the specified MID.
707 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
Karl Wiberg85a4a932019-03-22 15:29:58 +0100708 const std::string& mid) const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800709
710 // Generates the rejected MediaDescriptionOptions for the local data channel
711 // given the specified MID.
712 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
Karl Wiberg85a4a932019-03-22 15:29:58 +0100713 const std::string& mid) const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800714
715 // Returns the MID for the data section associated with either the
716 // RtpDataChannel or SCTP data channel, if it has been set. If no data
717 // channels are configured this will return nullopt.
Karl Wiberg2cc368f2019-04-02 11:31:56 +0200718 absl::optional<std::string> GetDataMid() const RTC_RUN_ON(signaling_thread());
Steve Antonfa2260d2017-12-28 16:38:23 -0800719
Steve Anton4171afb2017-11-20 10:20:22 -0800720 // Remove all local and remote senders of type |media_type|.
deadbeeffaac4972015-11-12 15:33:07 -0800721 // Called when a media type is rejected (m-line set to port 0).
Karl Wiberg744310f2019-02-14 10:18:56 +0100722 void RemoveSenders(cricket::MediaType media_type)
723 RTC_RUN_ON(signaling_thread());
deadbeeffaac4972015-11-12 15:33:07 -0800724
deadbeefbda7e0b2015-12-08 17:13:40 -0800725 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
726 // and existing MediaStreamTracks are removed if there is no corresponding
727 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
728 // is created if it doesn't exist; if false, it's removed if it exists.
729 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700730 // If a new MediaStream is created it is added to |new_streams|.
Steve Anton4171afb2017-11-20 10:20:22 -0800731 void UpdateRemoteSendersList(
deadbeefab9b2d12015-10-14 11:33:11 -0700732 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800733 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700734 cricket::MediaType media_type,
Karl Wiberg744310f2019-02-14 10:18:56 +0100735 StreamCollection* new_streams) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700736
Steve Anton4171afb2017-11-20 10:20:22 -0800737 // Triggered when a remote sender has been seen for the first time in a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700738 // session description. It creates a remote MediaStreamTrackInterface
739 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800740 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
Karl Wiberg744310f2019-02-14 10:18:56 +0100741 cricket::MediaType media_type)
742 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700743
Steve Anton4171afb2017-11-20 10:20:22 -0800744 // Triggered when a remote sender has been removed from a remote session
745 // description. It removes the remote sender with id |sender_id| from a remote
deadbeefab9b2d12015-10-14 11:33:11 -0700746 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
Steve Anton4171afb2017-11-20 10:20:22 -0800747 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
Karl Wiberg744310f2019-02-14 10:18:56 +0100748 cricket::MediaType media_type)
749 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700750
751 // Finds remote MediaStreams without any tracks and removes them from
752 // |remote_streams_| and notifies the observer that the MediaStreams no longer
753 // exist.
Karl Wiberg744310f2019-02-14 10:18:56 +0100754 void UpdateEndedRemoteMediaStreams() RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700755
deadbeefab9b2d12015-10-14 11:33:11 -0700756 // Loops through the vector of |streams| and finds added and removed
757 // StreamParams since last time this method was called.
Steve Anton4171afb2017-11-20 10:20:22 -0800758 // For each new or removed StreamParam, OnLocalSenderSeen or
759 // OnLocalSenderRemoved is invoked.
760 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
Karl Wiberg5966c502019-02-21 23:55:09 +0100761 cricket::MediaType media_type)
762 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700763
Steve Anton4171afb2017-11-20 10:20:22 -0800764 // Triggered when a local sender has been seen for the first time in a local
deadbeefab9b2d12015-10-14 11:33:11 -0700765 // session description.
766 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
767 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
768 // in a MediaStream in |local_streams_|
Steve Anton4171afb2017-11-20 10:20:22 -0800769 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
Karl Wiberg5966c502019-02-21 23:55:09 +0100770 cricket::MediaType media_type)
771 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700772
Steve Anton4171afb2017-11-20 10:20:22 -0800773 // Triggered when a local sender has been removed from a local session
deadbeefab9b2d12015-10-14 11:33:11 -0700774 // description.
775 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
776 // has been removed from the local SessionDescription and the stream can be
777 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
Steve Anton4171afb2017-11-20 10:20:22 -0800778 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
Karl Wiberga58e1692019-03-26 13:33:43 +0100779 cricket::MediaType media_type)
780 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700781
Karl Wiberg85a4a932019-03-22 15:29:58 +0100782 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams)
783 RTC_RUN_ON(signaling_thread());
Karl Wiberg106d92d2019-02-14 10:17:47 +0100784 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams)
785 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700786 void UpdateClosingRtpDataChannels(
787 const std::vector<std::string>& active_channels,
Karl Wiberg85a4a932019-03-22 15:29:58 +0100788 bool is_local_update) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700789 void CreateRemoteRtpDataChannel(const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100790 uint32_t remote_ssrc)
791 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700792
793 // Creates channel and adds it to the collection of DataChannels that will
794 // be offered in a SessionDescription.
795 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
796 const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100797 const InternalDataChannelInit* config) RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700798
799 // Checks if any data channel has been added.
Karl Wiberg85a4a932019-03-22 15:29:58 +0100800 bool HasDataChannels() const RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700801
Karl Wiberg85a4a932019-03-22 15:29:58 +0100802 void AllocateSctpSids(rtc::SSLRole role) RTC_RUN_ON(signaling_thread());
803 void OnSctpDataChannelClosed(DataChannel* channel)
804 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700805
Karl Wiberg85a4a932019-03-22 15:29:58 +0100806 void OnDataChannelDestroyed() RTC_RUN_ON(signaling_thread());
Steve Antonba818672017-11-06 10:21:57 -0800807 // Called when a valid data channel OPEN message is received.
deadbeefab9b2d12015-10-14 11:33:11 -0700808 void OnDataChannelOpenMessage(const std::string& label,
Karl Wiberg106d92d2019-02-14 10:17:47 +0100809 const InternalDataChannelInit& config)
810 RTC_RUN_ON(signaling_thread());
811
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800812 // Parses and handles open messages. Returns true if the message is an open
813 // message, false otherwise.
814 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
815 const rtc::CopyOnWriteBuffer& buffer)
816 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700817
Steve Anton4171afb2017-11-20 10:20:22 -0800818 // Returns true if the PeerConnection is configured to use Unified Plan
819 // semantics for creating offers/answers and setting local/remote
820 // descriptions. If this is true the RtpTransceiver API will also be available
821 // to the user. If this is false, Plan B semantics are assumed.
Steve Anton79e79602017-11-20 10:25:56 -0800822 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
823 // sufficient time has passed.
Karl Wiberg5966c502019-02-21 23:55:09 +0100824 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread()) {
Steve Anton79e79602017-11-20 10:25:56 -0800825 return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
826 }
Steve Anton4171afb2017-11-20 10:20:22 -0800827
Steve Anton06817cd2018-12-18 15:55:30 -0800828 // The offer/answer machinery assumes the media section MID is present and
829 // unique. To support legacy end points that do not supply a=mid lines, this
830 // method will modify the session description to add MIDs generated according
831 // to the SDP semantics.
Karl Wiberg5966c502019-02-21 23:55:09 +0100832 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description)
833 RTC_RUN_ON(signaling_thread());
Steve Anton06817cd2018-12-18 15:55:30 -0800834
Steve Anton4171afb2017-11-20 10:20:22 -0800835 // Is there an RtpSender of the given type?
Karl Wiberg5966c502019-02-21 23:55:09 +0100836 bool HasRtpSender(cricket::MediaType type) const
837 RTC_RUN_ON(signaling_thread());
deadbeeffac06552015-11-25 11:26:01 -0800838
Steve Anton4171afb2017-11-20 10:20:22 -0800839 // Return the RtpSender with the given track attached.
840 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100841 FindSenderForTrack(MediaStreamTrackInterface* track) const
842 RTC_RUN_ON(signaling_thread());
deadbeef70ab1a12015-09-28 16:53:55 -0700843
Steve Anton4171afb2017-11-20 10:20:22 -0800844 // Return the RtpSender with the given id, or null if none exists.
845 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100846 FindSenderById(const std::string& sender_id) const
847 RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800848
849 // Return the RtpReceiver with the given id, or null if none exists.
850 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Karl Wiberga58e1692019-03-26 13:33:43 +0100851 FindReceiverById(const std::string& receiver_id) const
852 RTC_RUN_ON(signaling_thread());
Steve Anton4171afb2017-11-20 10:20:22 -0800853
854 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
855 cricket::MediaType media_type);
856 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
857 cricket::MediaType media_type);
858 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
Emircan Uysalerbc609ea2018-03-27 21:57:18 +0000859 const std::string& stream_id,
Steve Anton4171afb2017-11-20 10:20:22 -0800860 const std::string sender_id) const;
deadbeefab9b2d12015-10-14 11:33:11 -0700861
862 // Returns the specified SCTP DataChannel in sctp_data_channels_,
863 // or nullptr if not found.
Karl Wiberg85a4a932019-03-22 15:29:58 +0100864 DataChannel* FindDataChannelBySid(int sid) const
865 RTC_RUN_ON(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -0700866
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700867 // Called when first configuring the port allocator.
Karl Wibergfb3be392019-03-22 14:13:22 +0100868 struct InitializePortAllocatorResult {
869 bool enable_ipv6;
870 };
871 InitializePortAllocatorResult InitializePortAllocator_n(
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200872 const cricket::ServerAddresses& stun_servers,
873 const std::vector<cricket::RelayServerConfig>& turn_servers,
874 const RTCConfiguration& configuration);
deadbeef293e9262017-01-11 12:28:30 -0800875 // Called when SetConfiguration is called to apply the supported subset
876 // of the configuration on the network thread.
877 bool ReconfigurePortAllocator_n(
878 const cricket::ServerAddresses& stun_servers,
879 const std::vector<cricket::RelayServerConfig>& turn_servers,
880 IceTransportsType type,
881 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200882 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800883 webrtc::TurnCustomizer* turn_customizer,
Karl Wiberg739506e2019-04-03 11:37:28 +0200884 absl::optional<int> stun_candidate_keepalive_interval,
885 bool have_local_description);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700886
Elad Alon99c3fe52017-10-13 16:29:40 +0200887 // Starts output of an RTC event log to the given output object.
ivoc14d5dbe2016-07-04 07:06:55 -0700888 // This function should only be called from the worker thread.
Bjorn Tereliusde939432017-11-20 17:38:14 +0100889 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
890 int64_t output_period_ms);
Elad Alon99c3fe52017-10-13 16:29:40 +0200891
Elad Alonacb24172017-10-06 14:32:13 +0200892 // Stops recording an RTC event log.
ivoc14d5dbe2016-07-04 07:06:55 -0700893 // This function should only be called from the worker thread.
894 void StopRtcEventLog_w();
895
Steve Anton038834f2017-07-14 15:59:59 -0700896 // Ensures the configuration doesn't have any parameters with invalid values,
897 // or values that conflict with other parameters.
898 //
899 // Returns RTCError::OK() if there are no issues.
900 RTCError ValidateConfiguration(const RTCConfiguration& config) const;
901
Steve Antonba818672017-11-06 10:21:57 -0800902 cricket::ChannelManager* channel_manager() const;
Steve Antonba818672017-11-06 10:21:57 -0800903
Steve Antonf8470812017-12-04 10:46:21 -0800904 enum class SessionError {
905 kNone, // No error.
906 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
907 kTransport, // Error from the underlying transport.
908 };
909
Steve Anton75737c02017-11-06 10:37:17 -0800910 // Returns the last error in the session. See the enum above for details.
Karl Wiberga58e1692019-03-26 13:33:43 +0100911 SessionError session_error() const RTC_RUN_ON(signaling_thread()) {
912 return session_error_;
913 }
Steve Antonf8470812017-12-04 10:46:21 -0800914 const std::string& session_error_desc() const { return session_error_desc_; }
Steve Anton75737c02017-11-06 10:37:17 -0800915
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800916 cricket::ChannelInterface* GetChannel(const std::string& content_name);
Steve Anton75737c02017-11-06 10:37:17 -0800917
918 // Get current SSL role used by SCTP's underlying transport.
919 bool GetSctpSslRole(rtc::SSLRole* role);
920
Steve Anton75737c02017-11-06 10:37:17 -0800921 cricket::IceConfig ParseIceConfig(
922 const PeerConnectionInterface::RTCConfiguration& config) const;
923
Steve Anton75737c02017-11-06 10:37:17 -0800924 // Implements DataChannelProviderInterface.
925 bool SendData(const cricket::SendDataParams& params,
926 const rtc::CopyOnWriteBuffer& payload,
927 cricket::SendDataResult* result) override;
928 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
929 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
930 void AddSctpDataStream(int sid) override;
931 void RemoveSctpDataStream(int sid) override;
932 bool ReadyToSendData() const override;
933
934 cricket::DataChannelType data_channel_type() const;
935
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800936 // Implements DataChannelSink.
937 void OnDataReceived(int channel_id,
938 DataMessageType type,
939 const rtc::CopyOnWriteBuffer& buffer) override;
940 void OnChannelClosing(int channel_id) override;
941 void OnChannelClosed(int channel_id) override;
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700942 void OnReadyToSend() override;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800943
Steve Anton75737c02017-11-06 10:37:17 -0800944 // Called when an RTCCertificate is generated or retrieved by
945 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
946 void OnCertificateReady(
947 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
948 void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
949
Steve Anton75737c02017-11-06 10:37:17 -0800950 // Non-const versions of local_description()/remote_description(), for use
951 // internally.
Karl Wiberg739506e2019-04-03 11:37:28 +0200952 SessionDescriptionInterface* mutable_local_description()
953 RTC_RUN_ON(signaling_thread()) {
Steve Anton75737c02017-11-06 10:37:17 -0800954 return pending_local_description_ ? pending_local_description_.get()
955 : current_local_description_.get();
956 }
Karl Wiberg739506e2019-04-03 11:37:28 +0200957 SessionDescriptionInterface* mutable_remote_description()
958 RTC_RUN_ON(signaling_thread()) {
Steve Anton75737c02017-11-06 10:37:17 -0800959 return pending_remote_description_ ? pending_remote_description_.get()
960 : current_remote_description_.get();
961 }
962
963 // Updates the error state, signaling if necessary.
Steve Antonf8470812017-12-04 10:46:21 -0800964 void SetSessionError(SessionError error, const std::string& error_desc);
Steve Anton75737c02017-11-06 10:37:17 -0800965
Zhi Huange830e682018-03-30 10:48:35 -0700966 RTCError UpdateSessionState(SdpType type,
967 cricket::ContentSource source,
968 const cricket::SessionDescription* description);
Steve Anton75737c02017-11-06 10:37:17 -0800969 // Push the media parts of the local or remote session description
970 // down to all of the channels.
Karl Wiberg5966c502019-02-21 23:55:09 +0100971 RTCError PushdownMediaDescription(SdpType type, cricket::ContentSource source)
972 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800973
Steve Anton8a006912017-12-04 15:25:56 -0800974 RTCError PushdownTransportDescription(cricket::ContentSource source,
Steve Anton3828c062017-12-06 10:34:51 -0800975 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -0800976
977 // Returns true and the TransportInfo of the given |content_name|
978 // from |description|. Returns false if it's not available.
979 static bool GetTransportDescription(
980 const cricket::SessionDescription* description,
981 const std::string& content_name,
982 cricket::TransportDescription* info);
983
Steve Anton75737c02017-11-06 10:37:17 -0800984 // Enables media channels to allow sending of media.
Steve Antoned10bd92017-12-05 10:52:59 -0800985 // This enables media to flow on all configured audio/video channels and the
986 // RtpDataChannel.
Karl Wiberga58e1692019-03-26 13:33:43 +0100987 void EnableSending() RTC_RUN_ON(signaling_thread());
Steve Anton3fe1b152017-12-12 10:20:08 -0800988
Steve Anton8af21862017-12-15 11:20:13 -0800989 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
Karl Wiberg85a4a932019-03-22 15:29:58 +0100990 void DestroyAllChannels() RTC_RUN_ON(signaling_thread());
Steve Anton3fe1b152017-12-12 10:20:08 -0800991
Steve Anton75737c02017-11-06 10:37:17 -0800992 // Returns the media index for a local ice candidate given the content name.
993 // Returns false if the local session description does not have a media
994 // content called |content_name|.
995 bool GetLocalCandidateMediaIndex(const std::string& content_name,
Karl Wiberg739506e2019-04-03 11:37:28 +0200996 int* sdp_mline_index)
997 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -0800998 // Uses all remote candidates in |remote_desc| in this session.
999 bool UseCandidatesInSessionDescription(
Karl Wiberg744310f2019-02-14 10:18:56 +01001000 const SessionDescriptionInterface* remote_desc)
1001 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001002 // Uses |candidate| in this session.
Karl Wiberg744310f2019-02-14 10:18:56 +01001003 bool UseCandidate(const IceCandidateInterface* candidate)
1004 RTC_RUN_ON(signaling_thread());
Guido Urdaneta41633172019-05-23 20:12:29 +02001005 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
1006 const SessionDescriptionInterface* description,
1007 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001008 // Deletes the corresponding channel of contents that don't exist in |desc|.
1009 // |desc| can be null. This means that all channels are deleted.
Karl Wiberg5966c502019-02-21 23:55:09 +01001010 void RemoveUnusedChannels(const cricket::SessionDescription* desc)
1011 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001012
1013 // Allocates media channels based on the |desc|. If |desc| doesn't have
1014 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
1015 // This method will also delete any existing media channels before creating.
Karl Wiberg5966c502019-02-21 23:55:09 +01001016 RTCError CreateChannels(const cricket::SessionDescription& desc)
1017 RTC_RUN_ON(signaling_thread());
Steve Antondcc3c022017-12-22 16:02:54 -08001018
1019 // If the BUNDLE policy is max-bundle, then we know for sure that all
1020 // transports will be bundled from the start. This method returns the BUNDLE
1021 // group if that's the case, or null if BUNDLE will be negotiated later. An
1022 // error is returned if max-bundle is specified but the session description
1023 // does not have a BUNDLE group.
1024 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
Karl Wiberg5966c502019-02-21 23:55:09 +01001025 const cricket::SessionDescription& desc) const
1026 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001027
1028 // Helper methods to create media channels.
Karl Wiberg5966c502019-02-21 23:55:09 +01001029 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid)
1030 RTC_RUN_ON(signaling_thread());
1031 cricket::VideoChannel* CreateVideoChannel(const std::string& mid)
1032 RTC_RUN_ON(signaling_thread());
1033 bool CreateDataChannel(const std::string& mid) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001034
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001035 bool SetupDataChannelTransport_n(const std::string& mid)
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001036 RTC_RUN_ON(network_thread());
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001037 void TeardownDataChannelTransport_n() RTC_RUN_ON(network_thread());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001038
Steve Anton75737c02017-11-06 10:37:17 -08001039 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
1040 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
1041 // Below methods are helper methods which verifies SDP.
Steve Anton8a006912017-12-04 15:25:56 -08001042 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
Karl Wiberg5966c502019-02-21 23:55:09 +01001043 cricket::ContentSource source)
1044 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001045
Steve Anton3828c062017-12-06 10:34:51 -08001046 // Check if a call to SetLocalDescription is acceptable with a session
1047 // description of the given type.
1048 bool ExpectSetLocalDescription(SdpType type);
1049 // Check if a call to SetRemoteDescription is acceptable with a session
1050 // description of the given type.
1051 bool ExpectSetRemoteDescription(SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -08001052 // Verifies a=setup attribute as per RFC 5763.
1053 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
Steve Anton3828c062017-12-06 10:34:51 -08001054 SdpType type);
Steve Anton75737c02017-11-06 10:37:17 -08001055
1056 // Returns true if we are ready to push down the remote candidate.
1057 // |remote_desc| is the new remote description, or NULL if the current remote
1058 // description should be used. Output |valid| is true if the candidate media
1059 // index is valid.
1060 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
1061 const SessionDescriptionInterface* remote_desc,
Karl Wiberga58e1692019-03-26 13:33:43 +01001062 bool* valid) RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001063
1064 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
1065 // this session.
Karl Wiberg739506e2019-04-03 11:37:28 +02001066 bool SrtpRequired() const RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001067
Zhi Huange830e682018-03-30 10:48:35 -07001068 // JsepTransportController signal handlers.
Karl Wiberg744310f2019-02-14 10:18:56 +01001069 void OnTransportControllerConnectionState(cricket::IceConnectionState state)
1070 RTC_RUN_ON(signaling_thread());
1071 void OnTransportControllerGatheringState(cricket::IceGatheringState state)
1072 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001073 void OnTransportControllerCandidatesGathered(
1074 const std::string& transport_name,
Karl Wiberg744310f2019-02-14 10:18:56 +01001075 const std::vector<cricket::Candidate>& candidates)
1076 RTC_RUN_ON(signaling_thread());
Eldar Relloda13ea22019-06-01 12:23:43 +03001077 void OnTransportControllerCandidateError(
1078 const cricket::IceCandidateErrorEvent& event)
1079 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001080 void OnTransportControllerCandidatesRemoved(
Karl Wiberg744310f2019-02-14 10:18:56 +01001081 const std::vector<cricket::Candidate>& candidates)
1082 RTC_RUN_ON(signaling_thread());
Alex Drake00c7ecf2019-08-06 10:54:47 -07001083 void OnTransportControllerCandidateChanged(
1084 const cricket::CandidatePairChangeEvent& event)
1085 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001086 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
1087
Steve Antonf8470812017-12-04 10:46:21 -08001088 const char* SessionErrorToString(SessionError error) const;
Karl Wiberga58e1692019-03-26 13:33:43 +01001089 std::string GetSessionErrorMsg() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001090
Steve Anton8e20f172018-03-06 10:55:04 -08001091 // Report the UMA metric SdpFormatReceived for the given remote offer.
1092 void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
1093
Steve Anton0ffaaa22018-02-23 10:31:30 -08001094 // Report inferred negotiated SDP semantics from a local/remote answer to the
1095 // UMA observer.
1096 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
1097
Steve Anton75737c02017-11-06 10:37:17 -08001098 // Invoked when TransportController connection completion is signaled.
1099 // Reports stats for all transports in use.
Karl Wiberga58e1692019-03-26 13:33:43 +01001100 void ReportTransportStats() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001101
1102 // Gather the usage of IPv4/IPv6 as best connection.
1103 void ReportBestConnectionState(const cricket::TransportStats& stats);
1104
Steve Antonc7b964c2018-02-01 14:39:45 -08001105 void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
Karl Wiberg739506e2019-04-03 11:37:28 +02001106 const std::set<cricket::MediaType>& media_types)
1107 RTC_RUN_ON(signaling_thread());
Qingsi Wang1ba5dec2019-08-19 11:57:17 -07001108 void ReportIceCandidateCollected(const cricket::Candidate& candidate)
1109 RTC_RUN_ON(signaling_thread());
1110 void ReportRemoteIceCandidateAdded(const cricket::Candidate& candidate)
1111 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001112
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001113 void NoteUsageEvent(UsageEvent event);
Karl Wiberg744310f2019-02-14 10:18:56 +01001114 void ReportUsagePattern() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001115
Steve Anton75737c02017-11-06 10:37:17 -08001116 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
1117
Karl Wiberga58e1692019-03-26 13:33:43 +01001118 const std::string GetTransportName(const std::string& content_name)
1119 RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001120
Steve Anton6fec8802017-12-04 10:37:29 -08001121 // Destroys and clears the BaseChannel associated with the given transceiver,
1122 // if such channel is set.
1123 void DestroyTransceiverChannel(
1124 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
1125 transceiver);
1126
1127 // Destroys the RTP data channel and/or the SCTP data channel and clears it.
Karl Wiberg85a4a932019-03-22 15:29:58 +01001128 void DestroyDataChannel() RTC_RUN_ON(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001129
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001130 // Destroys the given ChannelInterface.
1131 // The channel cannot be accessed after this method is called.
1132 void DestroyChannelInterface(cricket::ChannelInterface* channel);
Steve Anton6fec8802017-12-04 10:37:29 -08001133
Zhi Huang365381f2018-04-13 16:44:34 -07001134 // JsepTransportController::Observer override.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07001135 //
1136 // Called by |transport_controller_| when processing transport information
1137 // from a session description, and the mapping from m= sections to transports
1138 // changed (as a result of BUNDLE negotiation, or m= sections being
1139 // rejected).
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001140 bool OnTransportChanged(
1141 const std::string& mid,
1142 RtpTransportInternal* rtp_transport,
1143 rtc::scoped_refptr<DtlsTransport> dtls_transport,
1144 MediaTransportInterface* media_transport,
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001145 DataChannelTransportInterface* data_channel_transport) override;
Zhi Huange830e682018-03-30 10:48:35 -07001146
Guido Urdaneta1ff16c82019-05-20 19:31:53 +02001147 // RtpSenderBase::SetStreamsObserver override.
1148 void OnSetStreams() override;
1149
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02001150 // Returns the observer. Will crash on CHECK if the observer is removed.
Karl Wiberg744310f2019-02-14 10:18:56 +01001151 PeerConnectionObserver* Observer() const RTC_RUN_ON(signaling_thread());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +02001152
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001153 // Returns the CryptoOptions for this PeerConnection. This will always
1154 // return the RTCConfiguration.crypto_options if set and will only default
1155 // back to the PeerConnectionFactory settings if nothing was set.
Karl Wiberg5966c502019-02-21 23:55:09 +01001156 CryptoOptions GetCryptoOptions() RTC_RUN_ON(signaling_thread());
Benjamin Wright8c27cca2018-10-25 10:16:44 -07001157
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001158 // Returns rtp transport, result can not be nullptr.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001159 RtpTransportInternal* GetRtpTransport(const std::string& mid)
1160 RTC_RUN_ON(signaling_thread()) {
Anton Sukhanov98a462c2018-10-17 13:15:42 -07001161 auto rtp_transport = transport_controller_->GetRtpTransport(mid);
1162 RTC_DCHECK(rtp_transport);
1163 return rtp_transport;
1164 }
1165
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001166 void UpdateNegotiationNeeded();
1167 bool CheckIfNegotiationIsNeeded();
1168
Karl Wiberg106d92d2019-02-14 10:17:47 +01001169 sigslot::signal1<DataChannel*> SignalDataChannelCreated_
1170 RTC_GUARDED_BY(signaling_thread());
Steve Anton2d8609c2018-01-23 16:38:46 -08001171
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 // Storing the factory as a scoped reference pointer ensures that the memory
1173 // in the PeerConnectionFactoryImpl remains available as long as the
1174 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
1175 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -07001176 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 // will refer to the same reference count.
Karl Wiberg744310f2019-02-14 10:18:56 +01001178 const rtc::scoped_refptr<PeerConnectionFactory> factory_;
1179 PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
1180 nullptr;
terelius33860252017-05-12 23:37:18 -07001181
1182 // The EventLog needs to outlive |call_| (and any other object that uses it).
Karl Wibergb03ab712019-02-14 11:59:57 +01001183 std::unique_ptr<RtcEventLog> event_log_ RTC_GUARDED_BY(worker_thread());
1184
1185 // Points to the same thing as `event_log_`. Since it's const, we may read the
1186 // pointer (but not touch the object) from any thread.
1187 RtcEventLog* const event_log_ptr_ RTC_PT_GUARDED_BY(worker_thread());
terelius33860252017-05-12 23:37:18 -07001188
Karl Wiberg8d2e2282019-02-17 13:00:07 +01001189 SignalingState signaling_state_ RTC_GUARDED_BY(signaling_thread()) = kStable;
1190 IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) =
1191 kIceConnectionNew;
1192 PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_
1193 RTC_GUARDED_BY(signaling_thread()) = kIceConnectionNew;
1194 PeerConnectionInterface::PeerConnectionState connection_state_
1195 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionState::kNew;
Jonas Olsson635474e2018-10-18 15:58:17 +02001196
Karl Wiberg8d2e2282019-02-17 13:00:07 +01001197 IceGatheringState ice_gathering_state_ RTC_GUARDED_BY(signaling_thread()) =
1198 kIceGatheringNew;
Karl Wiberg5966c502019-02-21 23:55:09 +01001199 PeerConnectionInterface::RTCConfiguration configuration_
1200 RTC_GUARDED_BY(signaling_thread());
1201
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001202 // Field-trial based configuration for datagram transport.
1203 const DatagramTransportConfig datagram_transport_config_;
1204
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001205 // Field-trial based configuration for datagram transport data channels.
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07001206 const DatagramTransportDataChannelConfig
1207 datagram_transport_data_channel_config_;
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001208
Bjorn A Mellem5985a042019-06-28 14:19:38 -07001209 // Final, resolved value for whether datagram transport is in use.
1210 bool use_datagram_transport_ RTC_GUARDED_BY(signaling_thread()) = false;
1211
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001212 // Equivalent of |use_datagram_transport_|, but for its use with data
1213 // channels.
1214 bool use_datagram_transport_for_data_channels_
1215 RTC_GUARDED_BY(signaling_thread()) = false;
1216
Bjorn A Mellem7da4e562019-09-26 11:02:11 -07001217 // Resolved value of whether to use data channels only for incoming calls.
1218 bool use_datagram_transport_for_data_channels_receive_only_
1219 RTC_GUARDED_BY(signaling_thread()) = false;
1220
Karl Wiberg5966c502019-02-21 23:55:09 +01001221 // Cache configuration_.use_media_transport so that we can access it from
1222 // other threads.
1223 // TODO(bugs.webrtc.org/9987): Caching just this bool and allowing the data
1224 // it's derived from to change is not necessarily sound. Stop doing it.
1225 rtc::RaceChecker use_media_transport_race_checker_;
1226 bool use_media_transport_ RTC_GUARDED_BY(use_media_transport_race_checker_) =
1227 configuration_.use_media_transport;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228
Zach Steine20867f2018-08-02 13:20:15 -07001229 // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it
1230 // is not injected. It should be required once chromium supplies it.
Karl Wibergfb3be392019-03-22 14:13:22 +01001231 std::unique_ptr<AsyncResolverFactory> async_resolver_factory_
1232 RTC_GUARDED_BY(signaling_thread());
1233 std::unique_ptr<cricket::PortAllocator>
1234 port_allocator_; // TODO(bugs.webrtc.org/9987): Accessed on both
1235 // signaling and network thread.
1236 std::unique_ptr<rtc::SSLCertificateVerifier>
1237 tls_cert_verifier_; // TODO(bugs.webrtc.org/9987): Accessed on both
1238 // signaling and network thread.
deadbeefab9b2d12015-10-14 11:33:11 -07001239
zhihuang8f65cdf2016-05-06 18:40:30 -07001240 // One PeerConnection has only one RTCP CNAME.
1241 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
Karl Wibergfb3be392019-03-22 14:13:22 +01001242 const std::string rtcp_cname_;
zhihuang8f65cdf2016-05-06 18:40:30 -07001243
deadbeefab9b2d12015-10-14 11:33:11 -07001244 // Streams added via AddStream.
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001245 const rtc::scoped_refptr<StreamCollection> local_streams_
1246 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001247 // Streams created as a result of SetRemoteDescription.
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001248 const rtc::scoped_refptr<StreamCollection> remote_streams_
1249 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001250
Karl Wiberg1e1e1022019-03-22 14:27:22 +01001251 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
1252 RTC_GUARDED_BY(signaling_thread());
deadbeefeb459812015-12-15 19:24:43 -08001253
Steve Anton4171afb2017-11-20 10:20:22 -08001254 // These lists store sender info seen in local/remote descriptions.
Karl Wibergc70999e2019-03-22 15:14:27 +01001255 std::vector<RtpSenderInfo> remote_audio_sender_infos_
1256 RTC_GUARDED_BY(signaling_thread());
1257 std::vector<RtpSenderInfo> remote_video_sender_infos_
1258 RTC_GUARDED_BY(signaling_thread());
1259 std::vector<RtpSenderInfo> local_audio_sender_infos_
1260 RTC_GUARDED_BY(signaling_thread());
1261 std::vector<RtpSenderInfo> local_video_sender_infos_
1262 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001263
Karl Wiberg85a4a932019-03-22 15:29:58 +01001264 SctpSidAllocator sid_allocator_ RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001265 // label -> DataChannel
Karl Wiberg85a4a932019-03-22 15:29:58 +01001266 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_
1267 RTC_GUARDED_BY(signaling_thread());
1268 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_
1269 RTC_GUARDED_BY(signaling_thread());
1270 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_
1271 RTC_GUARDED_BY(signaling_thread());
deadbeefab9b2d12015-10-14 11:33:11 -07001272
Karl Wiberg85a4a932019-03-22 15:29:58 +01001273 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
deadbeef70ab1a12015-09-28 16:53:55 -07001274
Karl Wibergac025892019-03-26 13:08:37 +01001275 // The unique_ptr belongs to the worker thread, but the Call object manages
1276 // its own thread safety.
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001277 std::unique_ptr<Call> call_ RTC_GUARDED_BY(worker_thread());
1278
Sebastian Jansson1b83a9e2019-09-18 18:22:12 +02001279 rtc::AsyncInvoker rtcp_invoker_ RTC_GUARDED_BY(network_thread());
1280
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001281 // Points to the same thing as `call_`. Since it's const, we may read the
Karl Wibergac025892019-03-26 13:08:37 +01001282 // pointer from any thread.
1283 Call* const call_ptr_;
Karl Wiberg6cab5c82019-03-26 09:57:01 +01001284
1285 std::unique_ptr<StatsCollector> stats_
1286 RTC_GUARDED_BY(signaling_thread()); // A pointer is passed to senders_
1287 rtc::scoped_refptr<RTCStatsCollector> stats_collector_
1288 RTC_GUARDED_BY(signaling_thread());
terelius33860252017-05-12 23:37:18 -07001289
deadbeefa601f5c2016-06-06 14:27:39 -07001290 std::vector<
Steve Anton4171afb2017-11-20 10:20:22 -08001291 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001292 transceivers_; // TODO(bugs.webrtc.org/9987): Accessed on both signaling
1293 // and network thread.
1294
Henrik Boström5b147782018-12-04 11:25:05 +01001295 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
1296 // line we create and use a stream with a random ID for our receivers. This is
1297 // to support legacy endpoints that do not support the a=msid attribute (as
1298 // opposed to streamless tracks with "a=msid:-").
Karl Wiberga58e1692019-03-26 13:33:43 +01001299 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
1300 RTC_GUARDED_BY(signaling_thread());
Amit Hilbuchae3df542019-01-07 12:13:08 -08001301 // MIDs will be generated using this generator which will keep track of
1302 // all the MIDs that have been seen over the life of the PeerConnection.
Karl Wiberga58e1692019-03-26 13:33:43 +01001303 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001304
Karl Wiberga58e1692019-03-26 13:33:43 +01001305 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
1306 SessionError::kNone;
1307 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001308
Karl Wiberga58e1692019-03-26 13:33:43 +01001309 std::string session_id_ RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001310
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001311 std::unique_ptr<JsepTransportController>
1312 transport_controller_; // TODO(bugs.webrtc.org/9987): Accessed on both
1313 // signaling and network thread.
1314 std::unique_ptr<cricket::SctpTransportInternalFactory>
1315 sctp_factory_; // TODO(bugs.webrtc.org/9987): Accessed on both
1316 // signaling and network thread.
Steve Anton75737c02017-11-06 10:37:17 -08001317 // |rtp_data_channel_| is used if in RTP data channel mode, |sctp_transport_|
1318 // when using SCTP.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001319 cricket::RtpDataChannel* rtp_data_channel_ =
1320 nullptr; // TODO(bugs.webrtc.org/9987): Accessed on both
1321 // signaling and some other thread.
Steve Anton75737c02017-11-06 10:37:17 -08001322
Zhi Huange830e682018-03-30 10:48:35 -07001323 // |sctp_mid_| is the content name (MID) in SDP.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001324 // Note: this is used as the data channel MID by both SCTP and data channel
1325 // transports. It is set when either transport is initialized and unset when
1326 // both transports are deleted.
Karl Wiberg2cc368f2019-04-02 11:31:56 +02001327 absl::optional<std::string>
1328 sctp_mid_; // TODO(bugs.webrtc.org/9987): Accessed on both signaling
1329 // and network thread.
1330
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001331 // Whether this peer is the caller. Set when the local description is applied.
1332 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
1333
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001334 // Plugin transport used for data channels. Pointer may be accessed and
1335 // checked from any thread, but the object may only be touched on the
1336 // network thread.
1337 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network thread.
1338 DataChannelTransportInterface* data_channel_transport_;
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001339
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001340 // Cached value of whether the data channel transport is ready to send.
1341 bool data_channel_transport_ready_to_send_
1342 RTC_GUARDED_BY(signaling_thread()) = false;
1343
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001344 // Used to invoke data channel transport signals on the signaling thread.
1345 std::unique_ptr<rtc::AsyncInvoker> data_channel_transport_invoker_
Karl Wiberg739506e2019-04-03 11:37:28 +02001346 RTC_GUARDED_BY(network_thread());
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001347
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -07001348 // Signals from |data_channel_transport_|. These are invoked on the signaling
1349 // thread.
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001350 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001351 RTC_GUARDED_BY(signaling_thread());
1352 sigslot::signal2<const cricket::ReceiveDataParams&,
1353 const rtc::CopyOnWriteBuffer&>
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001354 SignalDataChannelTransportReceivedData_s
1355 RTC_GUARDED_BY(signaling_thread());
1356 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001357 RTC_GUARDED_BY(signaling_thread());
Bjorn A Mellemb689af42019-08-21 10:44:59 -07001358 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001359 RTC_GUARDED_BY(signaling_thread());
1360
Karl Wiberg739506e2019-04-03 11:37:28 +02001361 std::unique_ptr<SessionDescriptionInterface> current_local_description_
1362 RTC_GUARDED_BY(signaling_thread());
1363 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
1364 RTC_GUARDED_BY(signaling_thread());
1365 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
1366 RTC_GUARDED_BY(signaling_thread());
1367 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
1368 RTC_GUARDED_BY(signaling_thread());
1369 bool dtls_enabled_ RTC_GUARDED_BY(signaling_thread()) = false;
Steve Anton75737c02017-11-06 10:37:17 -08001370 // Specifies which kind of data channel is allowed. This is controlled
1371 // by the chrome command-line flag and constraints:
1372 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
1373 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
1374 // not set or false, SCTP is allowed (DCT_SCTP);
1375 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
1376 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
Karl Wibergf73f7d62019-04-08 15:36:53 +02001377 cricket::DataChannelType data_channel_type_ =
1378 cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both
1379 // signaling and network thread.
Steve Anton75737c02017-11-06 10:37:17 -08001380
Karl Wibergf73f7d62019-04-08 15:36:53 +02001381 // List of content names for which the remote side triggered an ICE restart.
1382 std::set<std::string> pending_ice_restarts_
1383 RTC_GUARDED_BY(signaling_thread());
1384
1385 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
1386 RTC_GUARDED_BY(signaling_thread());
Steve Anton75737c02017-11-06 10:37:17 -08001387
1388 // Member variables for caching global options.
Karl Wibergf73f7d62019-04-08 15:36:53 +02001389 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
1390 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand8ebba742018-05-31 14:00:34 +02001391
Karl Wibergf73f7d62019-04-08 15:36:53 +02001392 int usage_event_accumulator_ RTC_GUARDED_BY(signaling_thread()) = 0;
1393 bool return_histogram_very_quickly_ RTC_GUARDED_BY(signaling_thread()) =
1394 false;
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001395
1396 // This object should be used to generate any SSRC that is not explicitly
1397 // specified by the user (or by the remote party).
1398 // The generator is not used directly, instead it is passed on to the
1399 // channel manager and the session description factory.
Karl Wibergf73f7d62019-04-08 15:36:53 +02001400 rtc::UniqueRandomIdGenerator ssrc_generator_
1401 RTC_GUARDED_BY(signaling_thread());
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +02001402
1403 // A video bitrate allocator factory.
1404 // This can injected using the PeerConnectionDependencies,
1405 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
1406 // Note that one can still choose to override this in a MediaEngine
1407 // if one wants too.
1408 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
1409 video_bitrate_allocator_factory_;
1410
Henrik Boström79b69802019-07-18 11:16:56 +02001411 std::unique_ptr<LocalIceCredentialsToReplace>
1412 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
Guido Urdaneta70c2db12019-04-16 12:24:14 +02001413 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414};
1415
1416} // namespace webrtc
1417
Steve Anton10542f22019-01-11 09:11:00 -08001418#endif // PC_PEER_CONNECTION_H_