peer_connection.h 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /*
  2. * Copyright 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * 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.
  9. */
  10. #ifndef PC_PEER_CONNECTION_H_
  11. #define PC_PEER_CONNECTION_H_
  12. #include <map>
  13. #include <memory>
  14. #include <set>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #include "api/peer_connection_interface.h"
  19. #include "api/transport/data_channel_transport_interface.h"
  20. #include "api/turn_customizer.h"
  21. #include "pc/data_channel_controller.h"
  22. #include "pc/ice_server_parsing.h"
  23. #include "pc/jsep_transport_controller.h"
  24. #include "pc/peer_connection_factory.h"
  25. #include "pc/peer_connection_internal.h"
  26. #include "pc/rtc_stats_collector.h"
  27. #include "pc/rtp_sender.h"
  28. #include "pc/rtp_transceiver.h"
  29. #include "pc/sctp_transport.h"
  30. #include "pc/stats_collector.h"
  31. #include "pc/stream_collection.h"
  32. #include "pc/webrtc_session_description_factory.h"
  33. #include "rtc_base/experiments/field_trial_parser.h"
  34. #include "rtc_base/operations_chain.h"
  35. #include "rtc_base/race_checker.h"
  36. #include "rtc_base/unique_id_generator.h"
  37. #include "rtc_base/weak_ptr.h"
  38. namespace webrtc {
  39. class MediaStreamObserver;
  40. class VideoRtpReceiver;
  41. class RtcEventLog;
  42. // PeerConnection is the implementation of the PeerConnection object as defined
  43. // by the PeerConnectionInterface API surface.
  44. // The class currently is solely responsible for the following:
  45. // - Managing the session state machine (signaling state).
  46. // - Creating and initializing lower-level objects, like PortAllocator and
  47. // BaseChannels.
  48. // - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
  49. // objects.
  50. // - Tracking the current and pending local/remote session descriptions.
  51. // The class currently is jointly responsible for the following:
  52. // - Parsing and interpreting SDP.
  53. // - Generating offers and answers based on the current state.
  54. // - The ICE state machine.
  55. // - Generating stats.
  56. class PeerConnection : public PeerConnectionInternal,
  57. public JsepTransportController::Observer,
  58. public RtpSenderBase::SetStreamsObserver,
  59. public rtc::MessageHandler,
  60. public sigslot::has_slots<> {
  61. public:
  62. // A bit in the usage pattern is registered when its defining event occurs at
  63. // least once.
  64. enum class UsageEvent : int {
  65. TURN_SERVER_ADDED = 0x01,
  66. STUN_SERVER_ADDED = 0x02,
  67. DATA_ADDED = 0x04,
  68. AUDIO_ADDED = 0x08,
  69. VIDEO_ADDED = 0x10,
  70. // |SetLocalDescription| returns successfully.
  71. SET_LOCAL_DESCRIPTION_SUCCEEDED = 0x20,
  72. // |SetRemoteDescription| returns successfully.
  73. SET_REMOTE_DESCRIPTION_SUCCEEDED = 0x40,
  74. // A local candidate (with type host, server-reflexive, or relay) is
  75. // collected.
  76. CANDIDATE_COLLECTED = 0x80,
  77. // A remote candidate is successfully added via |AddIceCandidate|.
  78. ADD_ICE_CANDIDATE_SUCCEEDED = 0x100,
  79. ICE_STATE_CONNECTED = 0x200,
  80. CLOSE_CALLED = 0x400,
  81. // A local candidate with private IP is collected.
  82. PRIVATE_CANDIDATE_COLLECTED = 0x800,
  83. // A remote candidate with private IP is added, either via AddiceCandidate
  84. // or from the remote description.
  85. REMOTE_PRIVATE_CANDIDATE_ADDED = 0x1000,
  86. // A local mDNS candidate is collected.
  87. MDNS_CANDIDATE_COLLECTED = 0x2000,
  88. // A remote mDNS candidate is added, either via AddIceCandidate or from the
  89. // remote description.
  90. REMOTE_MDNS_CANDIDATE_ADDED = 0x4000,
  91. // A local candidate with IPv6 address is collected.
  92. IPV6_CANDIDATE_COLLECTED = 0x8000,
  93. // A remote candidate with IPv6 address is added, either via AddIceCandidate
  94. // or from the remote description.
  95. REMOTE_IPV6_CANDIDATE_ADDED = 0x10000,
  96. // A remote candidate (with type host, server-reflexive, or relay) is
  97. // successfully added, either via AddIceCandidate or from the remote
  98. // description.
  99. REMOTE_CANDIDATE_ADDED = 0x20000,
  100. // An explicit host-host candidate pair is selected, i.e. both the local and
  101. // the remote candidates have the host type. This does not include candidate
  102. // pairs formed with equivalent prflx remote candidates, e.g. a host-prflx
  103. // pair where the prflx candidate has the same base as a host candidate of
  104. // the remote peer.
  105. DIRECT_CONNECTION_SELECTED = 0x40000,
  106. MAX_VALUE = 0x80000,
  107. };
  108. explicit PeerConnection(PeerConnectionFactory* factory,
  109. std::unique_ptr<RtcEventLog> event_log,
  110. std::unique_ptr<Call> call);
  111. bool Initialize(
  112. const PeerConnectionInterface::RTCConfiguration& configuration,
  113. PeerConnectionDependencies dependencies);
  114. rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
  115. rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
  116. bool AddStream(MediaStreamInterface* local_stream) override;
  117. void RemoveStream(MediaStreamInterface* local_stream) override;
  118. RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
  119. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  120. const std::vector<std::string>& stream_ids) override;
  121. bool RemoveTrack(RtpSenderInterface* sender) override;
  122. RTCError RemoveTrackNew(
  123. rtc::scoped_refptr<RtpSenderInterface> sender) override;
  124. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  125. rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
  126. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  127. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  128. const RtpTransceiverInit& init) override;
  129. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  130. cricket::MediaType media_type) override;
  131. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  132. cricket::MediaType media_type,
  133. const RtpTransceiverInit& init) override;
  134. // Gets the DTLS SSL certificate associated with the audio transport on the
  135. // remote side. This will become populated once the DTLS connection with the
  136. // peer has been completed, as indicated by the ICE connection state
  137. // transitioning to kIceConnectionCompleted.
  138. // Note that this will be removed once we implement RTCDtlsTransport which
  139. // has standardized method for getting this information.
  140. // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
  141. std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
  142. // Version of the above method that returns the full certificate chain.
  143. std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
  144. rtc::scoped_refptr<RtpSenderInterface> CreateSender(
  145. const std::string& kind,
  146. const std::string& stream_id) override;
  147. std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
  148. const override;
  149. std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
  150. const override;
  151. std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
  152. const override;
  153. rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
  154. const std::string& label,
  155. const DataChannelInit* config) override;
  156. // WARNING: LEGACY. See peerconnectioninterface.h
  157. bool GetStats(StatsObserver* observer,
  158. webrtc::MediaStreamTrackInterface* track,
  159. StatsOutputLevel level) override;
  160. // Spec-complaint GetStats(). See peerconnectioninterface.h
  161. void GetStats(RTCStatsCollectorCallback* callback) override;
  162. void GetStats(
  163. rtc::scoped_refptr<RtpSenderInterface> selector,
  164. rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
  165. void GetStats(
  166. rtc::scoped_refptr<RtpReceiverInterface> selector,
  167. rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
  168. void ClearStatsCache() override;
  169. SignalingState signaling_state() override;
  170. IceConnectionState ice_connection_state() override;
  171. IceConnectionState standardized_ice_connection_state() override;
  172. PeerConnectionState peer_connection_state() override;
  173. IceGatheringState ice_gathering_state() override;
  174. absl::optional<bool> can_trickle_ice_candidates() override;
  175. const SessionDescriptionInterface* local_description() const override;
  176. const SessionDescriptionInterface* remote_description() const override;
  177. const SessionDescriptionInterface* current_local_description() const override;
  178. const SessionDescriptionInterface* current_remote_description()
  179. const override;
  180. const SessionDescriptionInterface* pending_local_description() const override;
  181. const SessionDescriptionInterface* pending_remote_description()
  182. const override;
  183. void RestartIce() override;
  184. // JSEP01
  185. void CreateOffer(CreateSessionDescriptionObserver* observer,
  186. const RTCOfferAnswerOptions& options) override;
  187. void CreateAnswer(CreateSessionDescriptionObserver* observer,
  188. const RTCOfferAnswerOptions& options) override;
  189. void SetLocalDescription(SetSessionDescriptionObserver* observer,
  190. SessionDescriptionInterface* desc) override;
  191. void SetLocalDescription(SetSessionDescriptionObserver* observer) override;
  192. void SetRemoteDescription(SetSessionDescriptionObserver* observer,
  193. SessionDescriptionInterface* desc) override;
  194. void SetRemoteDescription(
  195. std::unique_ptr<SessionDescriptionInterface> desc,
  196. rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
  197. override;
  198. PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
  199. RTCError SetConfiguration(
  200. const PeerConnectionInterface::RTCConfiguration& configuration) override;
  201. bool AddIceCandidate(const IceCandidateInterface* candidate) override;
  202. void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
  203. std::function<void(RTCError)> callback) override;
  204. bool RemoveIceCandidates(
  205. const std::vector<cricket::Candidate>& candidates) override;
  206. RTCError SetBitrate(const BitrateSettings& bitrate) override;
  207. void SetAudioPlayout(bool playout) override;
  208. void SetAudioRecording(bool recording) override;
  209. rtc::scoped_refptr<DtlsTransportInterface> LookupDtlsTransportByMid(
  210. const std::string& mid) override;
  211. rtc::scoped_refptr<DtlsTransport> LookupDtlsTransportByMidInternal(
  212. const std::string& mid);
  213. rtc::scoped_refptr<SctpTransportInterface> GetSctpTransport() const override;
  214. void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
  215. bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
  216. int64_t output_period_ms) override;
  217. bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override;
  218. void StopRtcEventLog() override;
  219. void Close() override;
  220. // PeerConnectionInternal implementation.
  221. rtc::Thread* network_thread() const final {
  222. return factory_->network_thread();
  223. }
  224. rtc::Thread* worker_thread() const final { return factory_->worker_thread(); }
  225. rtc::Thread* signaling_thread() const final {
  226. return factory_->signaling_thread();
  227. }
  228. std::string session_id() const override {
  229. RTC_DCHECK_RUN_ON(signaling_thread());
  230. return session_id_;
  231. }
  232. bool initial_offerer() const override {
  233. RTC_DCHECK_RUN_ON(signaling_thread());
  234. return transport_controller_ && transport_controller_->initial_offerer();
  235. }
  236. std::vector<
  237. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
  238. GetTransceiversInternal() const override {
  239. RTC_DCHECK_RUN_ON(signaling_thread());
  240. return transceivers_;
  241. }
  242. sigslot::signal1<DataChannel*>& SignalDataChannelCreated() override {
  243. return data_channel_controller_.SignalDataChannelCreated();
  244. }
  245. cricket::RtpDataChannel* rtp_data_channel() const override {
  246. return data_channel_controller_.rtp_data_channel();
  247. }
  248. std::vector<DataChannel::Stats> GetDataChannelStats() const override;
  249. absl::optional<std::string> sctp_transport_name() const override;
  250. cricket::CandidateStatsList GetPooledCandidateStats() const override;
  251. std::map<std::string, std::string> GetTransportNamesByMid() const override;
  252. std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
  253. const std::set<std::string>& transport_names) override;
  254. Call::Stats GetCallStats() override;
  255. bool GetLocalCertificate(
  256. const std::string& transport_name,
  257. rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
  258. std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
  259. const std::string& transport_name) override;
  260. bool IceRestartPending(const std::string& content_name) const override;
  261. bool NeedsIceRestart(const std::string& content_name) const override;
  262. bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
  263. // Functions needed by DataChannelController
  264. void NoteDataAddedEvent() { NoteUsageEvent(UsageEvent::DATA_ADDED); }
  265. // Returns the observer. Will crash on CHECK if the observer is removed.
  266. PeerConnectionObserver* Observer() const;
  267. bool IsClosed() const {
  268. RTC_DCHECK_RUN_ON(signaling_thread());
  269. return signaling_state_ == PeerConnectionInterface::kClosed;
  270. }
  271. // Get current SSL role used by SCTP's underlying transport.
  272. bool GetSctpSslRole(rtc::SSLRole* role);
  273. // Handler for the "channel closed" signal
  274. void OnSctpDataChannelClosed(DataChannel* channel);
  275. // Functions made public for testing.
  276. void ReturnHistogramVeryQuicklyForTesting() {
  277. RTC_DCHECK_RUN_ON(signaling_thread());
  278. return_histogram_very_quickly_ = true;
  279. }
  280. void RequestUsagePatternReportForTesting();
  281. absl::optional<std::string> sctp_mid() {
  282. RTC_DCHECK_RUN_ON(signaling_thread());
  283. return sctp_mid_s_;
  284. }
  285. protected:
  286. ~PeerConnection() override;
  287. private:
  288. class ImplicitCreateSessionDescriptionObserver;
  289. friend class ImplicitCreateSessionDescriptionObserver;
  290. class SetRemoteDescriptionObserverAdapter;
  291. friend class SetRemoteDescriptionObserverAdapter;
  292. // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
  293. // It makes the next CreateOffer() produce new ICE credentials even if
  294. // RTCOfferAnswerOptions::ice_restart is false.
  295. // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
  296. // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
  297. // move this type of logic to JsepTransportController/JsepTransport.
  298. class LocalIceCredentialsToReplace;
  299. struct RtpSenderInfo {
  300. RtpSenderInfo() : first_ssrc(0) {}
  301. RtpSenderInfo(const std::string& stream_id,
  302. const std::string sender_id,
  303. uint32_t ssrc)
  304. : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
  305. bool operator==(const RtpSenderInfo& other) {
  306. return this->stream_id == other.stream_id &&
  307. this->sender_id == other.sender_id &&
  308. this->first_ssrc == other.first_ssrc;
  309. }
  310. std::string stream_id;
  311. std::string sender_id;
  312. // An RtpSender can have many SSRCs. The first one is used as a sort of ID
  313. // for communicating with the lower layers.
  314. uint32_t first_ssrc;
  315. };
  316. // Field-trial based configuration for datagram transport.
  317. struct DatagramTransportConfig {
  318. explicit DatagramTransportConfig(const std::string& field_trial)
  319. : enabled("enabled", true), default_value("default_value", false) {
  320. ParseFieldTrial({&enabled, &default_value}, field_trial);
  321. }
  322. // Whether datagram transport support is enabled at all. Defaults to true,
  323. // allowing datagram transport to be used if (a) the application provides a
  324. // factory for it and (b) the configuration specifies its use. This flag
  325. // provides a kill-switch to force-disable datagram transport across all
  326. // applications, without code changes.
  327. FieldTrialFlag enabled;
  328. // Whether the datagram transport is enabled or disabled by default.
  329. // Defaults to false, meaning that applications must configure use of
  330. // datagram transport through RTCConfiguration. If set to true,
  331. // applications will use the datagram transport by default (but may still
  332. // explicitly configure themselves not to use it through RTCConfiguration).
  333. FieldTrialFlag default_value;
  334. };
  335. // Field-trial based configuration for datagram transport data channels.
  336. struct DatagramTransportDataChannelConfig {
  337. explicit DatagramTransportDataChannelConfig(const std::string& field_trial)
  338. : enabled("enabled", true),
  339. default_value("default_value", false),
  340. receive_only("receive_only", false) {
  341. ParseFieldTrial({&enabled, &default_value, &receive_only}, field_trial);
  342. }
  343. // Whether datagram transport data channel support is enabled at all.
  344. // Defaults to true, allowing datagram transport to be used if (a) the
  345. // application provides a factory for it and (b) the configuration specifies
  346. // its use. This flag provides a kill-switch to force-disable datagram
  347. // transport across all applications, without code changes.
  348. FieldTrialFlag enabled;
  349. // Whether the datagram transport data channels are enabled or disabled by
  350. // default. Defaults to false, meaning that applications must configure use
  351. // of datagram transport through RTCConfiguration. If set to true,
  352. // applications will use the datagram transport by default (but may still
  353. // explicitly configure themselves not to use it through RTCConfiguration).
  354. FieldTrialFlag default_value;
  355. // Whether the datagram transport is enabled in receive-only mode. If true,
  356. // and if the datagram transport is enabled, it will only be used when
  357. // receiving incoming calls, not when placing outgoing calls.
  358. FieldTrialFlag receive_only;
  359. };
  360. // Captures partial state to be used for rollback. Applicable only in
  361. // Unified Plan.
  362. class TransceiverStableState {
  363. public:
  364. TransceiverStableState() {}
  365. void set_newly_created();
  366. void SetMSectionIfUnset(absl::optional<std::string> mid,
  367. absl::optional<size_t> mline_index);
  368. void SetRemoteStreamIdsIfUnset(const std::vector<std::string>& ids);
  369. absl::optional<std::string> mid() const { return mid_; }
  370. absl::optional<size_t> mline_index() const { return mline_index_; }
  371. absl::optional<std::vector<std::string>> remote_stream_ids() const {
  372. return remote_stream_ids_;
  373. }
  374. bool has_m_section() const { return has_m_section_; }
  375. bool newly_created() const { return newly_created_; }
  376. private:
  377. absl::optional<std::string> mid_;
  378. absl::optional<size_t> mline_index_;
  379. absl::optional<std::vector<std::string>> remote_stream_ids_;
  380. // Indicates that mid value from stable state has been captured and
  381. // that rollback has to restore the transceiver. Also protects against
  382. // subsequent overwrites.
  383. bool has_m_section_ = false;
  384. // Indicates that the transceiver was created as part of applying a
  385. // description to track potential need for removing transceiver during
  386. // rollback.
  387. bool newly_created_ = false;
  388. };
  389. // Implements MessageHandler.
  390. void OnMessage(rtc::Message* msg) override;
  391. // Plan B helpers for getting the voice/video media channels for the single
  392. // audio/video transceiver, if it exists.
  393. cricket::VoiceMediaChannel* voice_media_channel() const
  394. RTC_RUN_ON(signaling_thread());
  395. cricket::VideoMediaChannel* video_media_channel() const
  396. RTC_RUN_ON(signaling_thread());
  397. std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
  398. GetSendersInternal() const RTC_RUN_ON(signaling_thread());
  399. std::vector<
  400. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
  401. GetReceiversInternal() const RTC_RUN_ON(signaling_thread());
  402. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  403. GetAudioTransceiver() const RTC_RUN_ON(signaling_thread());
  404. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  405. GetVideoTransceiver() const RTC_RUN_ON(signaling_thread());
  406. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  407. GetFirstAudioTransceiver() const RTC_RUN_ON(signaling_thread());
  408. // Implementation of the offer/answer exchange operations. These are chained
  409. // onto the |operations_chain_| when the public CreateOffer(), CreateAnswer(),
  410. // SetLocalDescription() and SetRemoteDescription() methods are invoked.
  411. void DoCreateOffer(
  412. const RTCOfferAnswerOptions& options,
  413. rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
  414. void DoCreateAnswer(
  415. const RTCOfferAnswerOptions& options,
  416. rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
  417. void DoSetLocalDescription(
  418. std::unique_ptr<SessionDescriptionInterface> desc,
  419. rtc::scoped_refptr<SetSessionDescriptionObserver> observer);
  420. void DoSetRemoteDescription(
  421. std::unique_ptr<SessionDescriptionInterface> desc,
  422. rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
  423. void CreateAudioReceiver(MediaStreamInterface* stream,
  424. const RtpSenderInfo& remote_sender_info)
  425. RTC_RUN_ON(signaling_thread());
  426. void CreateVideoReceiver(MediaStreamInterface* stream,
  427. const RtpSenderInfo& remote_sender_info)
  428. RTC_RUN_ON(signaling_thread());
  429. rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
  430. const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread());
  431. // May be called either by AddStream/RemoveStream, or when a track is
  432. // added/removed from a stream previously added via AddStream.
  433. void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream)
  434. RTC_RUN_ON(signaling_thread());
  435. void RemoveAudioTrack(AudioTrackInterface* track,
  436. MediaStreamInterface* stream)
  437. RTC_RUN_ON(signaling_thread());
  438. void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream)
  439. RTC_RUN_ON(signaling_thread());
  440. void RemoveVideoTrack(VideoTrackInterface* track,
  441. MediaStreamInterface* stream)
  442. RTC_RUN_ON(signaling_thread());
  443. // AddTrack implementation when Unified Plan is specified.
  444. RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
  445. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  446. const std::vector<std::string>& stream_ids)
  447. RTC_RUN_ON(signaling_thread());
  448. // AddTrack implementation when Plan B is specified.
  449. RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
  450. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  451. const std::vector<std::string>& stream_ids)
  452. RTC_RUN_ON(signaling_thread());
  453. // Returns the first RtpTransceiver suitable for a newly added track, if such
  454. // transceiver is available.
  455. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  456. FindFirstTransceiverForAddedTrack(
  457. rtc::scoped_refptr<MediaStreamTrackInterface> track)
  458. RTC_RUN_ON(signaling_thread());
  459. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  460. FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender)
  461. RTC_RUN_ON(signaling_thread());
  462. // Internal implementation for AddTransceiver family of methods. If
  463. // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
  464. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  465. cricket::MediaType media_type,
  466. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  467. const RtpTransceiverInit& init,
  468. bool fire_callback = true) RTC_RUN_ON(signaling_thread());
  469. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
  470. CreateSender(cricket::MediaType media_type,
  471. const std::string& id,
  472. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  473. const std::vector<std::string>& stream_ids,
  474. const std::vector<RtpEncodingParameters>& send_encodings);
  475. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  476. CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
  477. // Create a new RtpTransceiver of the given type and add it to the list of
  478. // transceivers.
  479. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  480. CreateAndAddTransceiver(
  481. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
  482. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  483. receiver) RTC_RUN_ON(signaling_thread());
  484. void SetIceConnectionState(IceConnectionState new_state)
  485. RTC_RUN_ON(signaling_thread());
  486. void SetStandardizedIceConnectionState(
  487. PeerConnectionInterface::IceConnectionState new_state)
  488. RTC_RUN_ON(signaling_thread());
  489. void SetConnectionState(
  490. PeerConnectionInterface::PeerConnectionState new_state)
  491. RTC_RUN_ON(signaling_thread());
  492. // Called any time the IceGatheringState changes.
  493. void OnIceGatheringChange(IceGatheringState new_state)
  494. RTC_RUN_ON(signaling_thread());
  495. // New ICE candidate has been gathered.
  496. void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)
  497. RTC_RUN_ON(signaling_thread());
  498. // Gathering of an ICE candidate failed.
  499. void OnIceCandidateError(const std::string& address,
  500. int port,
  501. const std::string& url,
  502. int error_code,
  503. const std::string& error_text)
  504. RTC_RUN_ON(signaling_thread());
  505. // Some local ICE candidates have been removed.
  506. void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates)
  507. RTC_RUN_ON(signaling_thread());
  508. void OnSelectedCandidatePairChanged(
  509. const cricket::CandidatePairChangeEvent& event)
  510. RTC_RUN_ON(signaling_thread());
  511. // Update the state, signaling if necessary.
  512. void ChangeSignalingState(SignalingState signaling_state)
  513. RTC_RUN_ON(signaling_thread());
  514. // Signals from MediaStreamObserver.
  515. void OnAudioTrackAdded(AudioTrackInterface* track,
  516. MediaStreamInterface* stream)
  517. RTC_RUN_ON(signaling_thread());
  518. void OnAudioTrackRemoved(AudioTrackInterface* track,
  519. MediaStreamInterface* stream)
  520. RTC_RUN_ON(signaling_thread());
  521. void OnVideoTrackAdded(VideoTrackInterface* track,
  522. MediaStreamInterface* stream)
  523. RTC_RUN_ON(signaling_thread());
  524. void OnVideoTrackRemoved(VideoTrackInterface* track,
  525. MediaStreamInterface* stream)
  526. RTC_RUN_ON(signaling_thread());
  527. void PostSetSessionDescriptionSuccess(
  528. SetSessionDescriptionObserver* observer);
  529. void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
  530. RTCError&& error);
  531. void PostCreateSessionDescriptionFailure(
  532. CreateSessionDescriptionObserver* observer,
  533. RTCError error);
  534. // Synchronous implementations of SetLocalDescription/SetRemoteDescription
  535. // that return an RTCError instead of invoking a callback.
  536. RTCError ApplyLocalDescription(
  537. std::unique_ptr<SessionDescriptionInterface> desc);
  538. RTCError ApplyRemoteDescription(
  539. std::unique_ptr<SessionDescriptionInterface> desc);
  540. // Updates the local RtpTransceivers according to the JSEP rules. Called as
  541. // part of setting the local/remote description.
  542. RTCError UpdateTransceiversAndDataChannels(
  543. cricket::ContentSource source,
  544. const SessionDescriptionInterface& new_session,
  545. const SessionDescriptionInterface* old_local_description,
  546. const SessionDescriptionInterface* old_remote_description)
  547. RTC_RUN_ON(signaling_thread());
  548. // Either creates or destroys the transceiver's BaseChannel according to the
  549. // given media section.
  550. RTCError UpdateTransceiverChannel(
  551. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  552. transceiver,
  553. const cricket::ContentInfo& content,
  554. const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
  555. // Either creates or destroys the local data channel according to the given
  556. // media section.
  557. RTCError UpdateDataChannel(cricket::ContentSource source,
  558. const cricket::ContentInfo& content,
  559. const cricket::ContentGroup* bundle_group)
  560. RTC_RUN_ON(signaling_thread());
  561. // Associate the given transceiver according to the JSEP rules.
  562. RTCErrorOr<
  563. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
  564. AssociateTransceiver(cricket::ContentSource source,
  565. SdpType type,
  566. size_t mline_index,
  567. const cricket::ContentInfo& content,
  568. const cricket::ContentInfo* old_local_content,
  569. const cricket::ContentInfo* old_remote_content)
  570. RTC_RUN_ON(signaling_thread());
  571. // Returns the RtpTransceiver, if found, that is associated to the given MID.
  572. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  573. GetAssociatedTransceiver(const std::string& mid) const
  574. RTC_RUN_ON(signaling_thread());
  575. // Returns the RtpTransceiver, if found, that was assigned to the given mline
  576. // index in CreateOffer.
  577. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  578. GetTransceiverByMLineIndex(size_t mline_index) const
  579. RTC_RUN_ON(signaling_thread());
  580. // Returns an RtpTransciever, if available, that can be used to receive the
  581. // given media type according to JSEP rules.
  582. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  583. FindAvailableTransceiverToReceive(cricket::MediaType media_type) const
  584. RTC_RUN_ON(signaling_thread());
  585. // Returns the media section in the given session description that is
  586. // associated with the RtpTransceiver. Returns null if none found or this
  587. // RtpTransceiver is not associated. Logic varies depending on the
  588. // SdpSemantics specified in the configuration.
  589. const cricket::ContentInfo* FindMediaSectionForTransceiver(
  590. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  591. transceiver,
  592. const SessionDescriptionInterface* sdesc) const
  593. RTC_RUN_ON(signaling_thread());
  594. // Runs the algorithm **set the associated remote streams** specified in
  595. // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
  596. void SetAssociatedRemoteStreams(
  597. rtc::scoped_refptr<RtpReceiverInternal> receiver,
  598. const std::vector<std::string>& stream_ids,
  599. std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
  600. std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
  601. RTC_RUN_ON(signaling_thread());
  602. // Runs the algorithm **process the removal of a remote track** specified in
  603. // the WebRTC specification.
  604. // This method will update the following lists:
  605. // |remove_list| is the list of transceivers for which the receiving track is
  606. // being removed.
  607. // |removed_streams| is the list of streams which no longer have a receiving
  608. // track so should be removed.
  609. // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
  610. void ProcessRemovalOfRemoteTrack(
  611. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  612. transceiver,
  613. std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
  614. std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
  615. RTC_RUN_ON(signaling_thread());
  616. void RemoveRemoteStreamsIfEmpty(
  617. const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
  618. remote_streams,
  619. std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams)
  620. RTC_RUN_ON(signaling_thread());
  621. void OnNegotiationNeeded();
  622. // Returns a MediaSessionOptions struct with options decided by |options|,
  623. // the local MediaStreams and DataChannels.
  624. void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
  625. offer_answer_options,
  626. cricket::MediaSessionOptions* session_options)
  627. RTC_RUN_ON(signaling_thread());
  628. void GetOptionsForPlanBOffer(
  629. const PeerConnectionInterface::RTCOfferAnswerOptions&
  630. offer_answer_options,
  631. cricket::MediaSessionOptions* session_options)
  632. RTC_RUN_ON(signaling_thread());
  633. void GetOptionsForUnifiedPlanOffer(
  634. const PeerConnectionInterface::RTCOfferAnswerOptions&
  635. offer_answer_options,
  636. cricket::MediaSessionOptions* session_options)
  637. RTC_RUN_ON(signaling_thread());
  638. RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options)
  639. RTC_RUN_ON(signaling_thread());
  640. void RemoveRecvDirectionFromReceivingTransceiversOfType(
  641. cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
  642. void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
  643. std::vector<
  644. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
  645. GetReceivingTransceiversOfType(cricket::MediaType media_type)
  646. RTC_RUN_ON(signaling_thread());
  647. // Returns a MediaSessionOptions struct with options decided by
  648. // |constraints|, the local MediaStreams and DataChannels.
  649. void GetOptionsForAnswer(const RTCOfferAnswerOptions& offer_answer_options,
  650. cricket::MediaSessionOptions* session_options)
  651. RTC_RUN_ON(signaling_thread());
  652. void GetOptionsForPlanBAnswer(
  653. const PeerConnectionInterface::RTCOfferAnswerOptions&
  654. offer_answer_options,
  655. cricket::MediaSessionOptions* session_options)
  656. RTC_RUN_ON(signaling_thread());
  657. void GetOptionsForUnifiedPlanAnswer(
  658. const PeerConnectionInterface::RTCOfferAnswerOptions&
  659. offer_answer_options,
  660. cricket::MediaSessionOptions* session_options)
  661. RTC_RUN_ON(signaling_thread());
  662. // Generates MediaDescriptionOptions for the |session_opts| based on existing
  663. // local description or remote description.
  664. void GenerateMediaDescriptionOptions(
  665. const SessionDescriptionInterface* session_desc,
  666. RtpTransceiverDirection audio_direction,
  667. RtpTransceiverDirection video_direction,
  668. absl::optional<size_t>* audio_index,
  669. absl::optional<size_t>* video_index,
  670. absl::optional<size_t>* data_index,
  671. cricket::MediaSessionOptions* session_options)
  672. RTC_RUN_ON(signaling_thread());
  673. // Generates the active MediaDescriptionOptions for the local data channel
  674. // given the specified MID.
  675. cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
  676. const std::string& mid) const RTC_RUN_ON(signaling_thread());
  677. // Generates the rejected MediaDescriptionOptions for the local data channel
  678. // given the specified MID.
  679. cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
  680. const std::string& mid) const RTC_RUN_ON(signaling_thread());
  681. // Returns the MID for the data section associated with either the
  682. // RtpDataChannel or SCTP data channel, if it has been set. If no data
  683. // channels are configured this will return nullopt.
  684. absl::optional<std::string> GetDataMid() const RTC_RUN_ON(signaling_thread());
  685. // Remove all local and remote senders of type |media_type|.
  686. // Called when a media type is rejected (m-line set to port 0).
  687. void RemoveSenders(cricket::MediaType media_type)
  688. RTC_RUN_ON(signaling_thread());
  689. // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
  690. // and existing MediaStreamTracks are removed if there is no corresponding
  691. // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
  692. // is created if it doesn't exist; if false, it's removed if it exists.
  693. // |media_type| is the type of the |streams| and can be either audio or video.
  694. // If a new MediaStream is created it is added to |new_streams|.
  695. void UpdateRemoteSendersList(
  696. const std::vector<cricket::StreamParams>& streams,
  697. bool default_track_needed,
  698. cricket::MediaType media_type,
  699. StreamCollection* new_streams) RTC_RUN_ON(signaling_thread());
  700. // Triggered when a remote sender has been seen for the first time in a remote
  701. // session description. It creates a remote MediaStreamTrackInterface
  702. // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
  703. void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
  704. cricket::MediaType media_type)
  705. RTC_RUN_ON(signaling_thread());
  706. // Triggered when a remote sender has been removed from a remote session
  707. // description. It removes the remote sender with id |sender_id| from a remote
  708. // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
  709. void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
  710. cricket::MediaType media_type)
  711. RTC_RUN_ON(signaling_thread());
  712. // Finds remote MediaStreams without any tracks and removes them from
  713. // |remote_streams_| and notifies the observer that the MediaStreams no longer
  714. // exist.
  715. void UpdateEndedRemoteMediaStreams() RTC_RUN_ON(signaling_thread());
  716. // Loops through the vector of |streams| and finds added and removed
  717. // StreamParams since last time this method was called.
  718. // For each new or removed StreamParam, OnLocalSenderSeen or
  719. // OnLocalSenderRemoved is invoked.
  720. void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
  721. cricket::MediaType media_type)
  722. RTC_RUN_ON(signaling_thread());
  723. // Triggered when a local sender has been seen for the first time in a local
  724. // session description.
  725. // This method triggers CreateAudioSender or CreateVideoSender if the rtp
  726. // streams in the local SessionDescription can be mapped to a MediaStreamTrack
  727. // in a MediaStream in |local_streams_|
  728. void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
  729. cricket::MediaType media_type)
  730. RTC_RUN_ON(signaling_thread());
  731. // Triggered when a local sender has been removed from a local session
  732. // description.
  733. // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
  734. // has been removed from the local SessionDescription and the stream can be
  735. // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
  736. void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
  737. cricket::MediaType media_type)
  738. RTC_RUN_ON(signaling_thread());
  739. // Returns true if the PeerConnection is configured to use Unified Plan
  740. // semantics for creating offers/answers and setting local/remote
  741. // descriptions. If this is true the RtpTransceiver API will also be available
  742. // to the user. If this is false, Plan B semantics are assumed.
  743. // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
  744. // sufficient time has passed.
  745. bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread()) {
  746. return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
  747. }
  748. // The offer/answer machinery assumes the media section MID is present and
  749. // unique. To support legacy end points that do not supply a=mid lines, this
  750. // method will modify the session description to add MIDs generated according
  751. // to the SDP semantics.
  752. void FillInMissingRemoteMids(cricket::SessionDescription* remote_description)
  753. RTC_RUN_ON(signaling_thread());
  754. // Is there an RtpSender of the given type?
  755. bool HasRtpSender(cricket::MediaType type) const
  756. RTC_RUN_ON(signaling_thread());
  757. // Return the RtpSender with the given track attached.
  758. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
  759. FindSenderForTrack(MediaStreamTrackInterface* track) const
  760. RTC_RUN_ON(signaling_thread());
  761. // Return the RtpSender with the given id, or null if none exists.
  762. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
  763. FindSenderById(const std::string& sender_id) const
  764. RTC_RUN_ON(signaling_thread());
  765. // Return the RtpReceiver with the given id, or null if none exists.
  766. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  767. FindReceiverById(const std::string& receiver_id) const
  768. RTC_RUN_ON(signaling_thread());
  769. std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
  770. cricket::MediaType media_type);
  771. std::vector<RtpSenderInfo>* GetLocalSenderInfos(
  772. cricket::MediaType media_type);
  773. const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
  774. const std::string& stream_id,
  775. const std::string sender_id) const;
  776. // Returns the specified SCTP DataChannel in sctp_data_channels_,
  777. // or nullptr if not found.
  778. DataChannel* FindDataChannelBySid(int sid) const
  779. RTC_RUN_ON(signaling_thread());
  780. // Called when first configuring the port allocator.
  781. struct InitializePortAllocatorResult {
  782. bool enable_ipv6;
  783. };
  784. InitializePortAllocatorResult InitializePortAllocator_n(
  785. const cricket::ServerAddresses& stun_servers,
  786. const std::vector<cricket::RelayServerConfig>& turn_servers,
  787. const RTCConfiguration& configuration);
  788. // Called when SetConfiguration is called to apply the supported subset
  789. // of the configuration on the network thread.
  790. bool ReconfigurePortAllocator_n(
  791. const cricket::ServerAddresses& stun_servers,
  792. const std::vector<cricket::RelayServerConfig>& turn_servers,
  793. IceTransportsType type,
  794. int candidate_pool_size,
  795. PortPrunePolicy turn_port_prune_policy,
  796. webrtc::TurnCustomizer* turn_customizer,
  797. absl::optional<int> stun_candidate_keepalive_interval,
  798. bool have_local_description);
  799. // Starts output of an RTC event log to the given output object.
  800. // This function should only be called from the worker thread.
  801. bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
  802. int64_t output_period_ms);
  803. // Stops recording an RTC event log.
  804. // This function should only be called from the worker thread.
  805. void StopRtcEventLog_w();
  806. // Ensures the configuration doesn't have any parameters with invalid values,
  807. // or values that conflict with other parameters.
  808. //
  809. // Returns RTCError::OK() if there are no issues.
  810. RTCError ValidateConfiguration(const RTCConfiguration& config) const;
  811. cricket::ChannelManager* channel_manager() const;
  812. enum class SessionError {
  813. kNone, // No error.
  814. kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
  815. kTransport, // Error from the underlying transport.
  816. };
  817. // Returns the last error in the session. See the enum above for details.
  818. SessionError session_error() const RTC_RUN_ON(signaling_thread()) {
  819. return session_error_;
  820. }
  821. const std::string& session_error_desc() const { return session_error_desc_; }
  822. cricket::ChannelInterface* GetChannel(const std::string& content_name);
  823. cricket::IceConfig ParseIceConfig(
  824. const PeerConnectionInterface::RTCConfiguration& config) const;
  825. cricket::DataChannelType data_channel_type() const;
  826. // Called when an RTCCertificate is generated or retrieved by
  827. // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
  828. void OnCertificateReady(
  829. const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
  830. void OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp);
  831. // Non-const versions of local_description()/remote_description(), for use
  832. // internally.
  833. SessionDescriptionInterface* mutable_local_description()
  834. RTC_RUN_ON(signaling_thread()) {
  835. return pending_local_description_ ? pending_local_description_.get()
  836. : current_local_description_.get();
  837. }
  838. SessionDescriptionInterface* mutable_remote_description()
  839. RTC_RUN_ON(signaling_thread()) {
  840. return pending_remote_description_ ? pending_remote_description_.get()
  841. : current_remote_description_.get();
  842. }
  843. // Updates the error state, signaling if necessary.
  844. void SetSessionError(SessionError error, const std::string& error_desc);
  845. RTCError UpdateSessionState(SdpType type,
  846. cricket::ContentSource source,
  847. const cricket::SessionDescription* description);
  848. // Push the media parts of the local or remote session description
  849. // down to all of the channels.
  850. RTCError PushdownMediaDescription(SdpType type, cricket::ContentSource source)
  851. RTC_RUN_ON(signaling_thread());
  852. RTCError PushdownTransportDescription(cricket::ContentSource source,
  853. SdpType type);
  854. // Returns true and the TransportInfo of the given |content_name|
  855. // from |description|. Returns false if it's not available.
  856. static bool GetTransportDescription(
  857. const cricket::SessionDescription* description,
  858. const std::string& content_name,
  859. cricket::TransportDescription* info);
  860. // Enables media channels to allow sending of media.
  861. // This enables media to flow on all configured audio/video channels and the
  862. // RtpDataChannel.
  863. void EnableSending() RTC_RUN_ON(signaling_thread());
  864. // Destroys all BaseChannels and destroys the SCTP data channel, if present.
  865. void DestroyAllChannels() RTC_RUN_ON(signaling_thread());
  866. // Returns the media index for a local ice candidate given the content name.
  867. // Returns false if the local session description does not have a media
  868. // content called |content_name|.
  869. bool GetLocalCandidateMediaIndex(const std::string& content_name,
  870. int* sdp_mline_index)
  871. RTC_RUN_ON(signaling_thread());
  872. // Uses all remote candidates in |remote_desc| in this session.
  873. bool UseCandidatesInSessionDescription(
  874. const SessionDescriptionInterface* remote_desc)
  875. RTC_RUN_ON(signaling_thread());
  876. // Uses |candidate| in this session.
  877. bool UseCandidate(const IceCandidateInterface* candidate)
  878. RTC_RUN_ON(signaling_thread());
  879. RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
  880. const SessionDescriptionInterface* description,
  881. const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
  882. // Deletes the corresponding channel of contents that don't exist in |desc|.
  883. // |desc| can be null. This means that all channels are deleted.
  884. void RemoveUnusedChannels(const cricket::SessionDescription* desc)
  885. RTC_RUN_ON(signaling_thread());
  886. // Allocates media channels based on the |desc|. If |desc| doesn't have
  887. // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
  888. // This method will also delete any existing media channels before creating.
  889. RTCError CreateChannels(const cricket::SessionDescription& desc)
  890. RTC_RUN_ON(signaling_thread());
  891. // If the BUNDLE policy is max-bundle, then we know for sure that all
  892. // transports will be bundled from the start. This method returns the BUNDLE
  893. // group if that's the case, or null if BUNDLE will be negotiated later. An
  894. // error is returned if max-bundle is specified but the session description
  895. // does not have a BUNDLE group.
  896. RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
  897. const cricket::SessionDescription& desc) const
  898. RTC_RUN_ON(signaling_thread());
  899. // Helper methods to create media channels.
  900. cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid)
  901. RTC_RUN_ON(signaling_thread());
  902. cricket::VideoChannel* CreateVideoChannel(const std::string& mid)
  903. RTC_RUN_ON(signaling_thread());
  904. bool CreateDataChannel(const std::string& mid) RTC_RUN_ON(signaling_thread());
  905. bool SetupDataChannelTransport_n(const std::string& mid)
  906. RTC_RUN_ON(network_thread());
  907. void TeardownDataChannelTransport_n() RTC_RUN_ON(network_thread());
  908. bool ValidateBundleSettings(const cricket::SessionDescription* desc);
  909. bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
  910. // Below methods are helper methods which verifies SDP.
  911. RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
  912. cricket::ContentSource source)
  913. RTC_RUN_ON(signaling_thread());
  914. // Check if a call to SetLocalDescription is acceptable with a session
  915. // description of the given type.
  916. bool ExpectSetLocalDescription(SdpType type);
  917. // Check if a call to SetRemoteDescription is acceptable with a session
  918. // description of the given type.
  919. bool ExpectSetRemoteDescription(SdpType type);
  920. // Verifies a=setup attribute as per RFC 5763.
  921. bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
  922. SdpType type);
  923. // Returns true if we are ready to push down the remote candidate.
  924. // |remote_desc| is the new remote description, or NULL if the current remote
  925. // description should be used. Output |valid| is true if the candidate media
  926. // index is valid.
  927. bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
  928. const SessionDescriptionInterface* remote_desc,
  929. bool* valid) RTC_RUN_ON(signaling_thread());
  930. // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
  931. // this session.
  932. bool SrtpRequired() const RTC_RUN_ON(signaling_thread());
  933. // JsepTransportController signal handlers.
  934. void OnTransportControllerConnectionState(cricket::IceConnectionState state)
  935. RTC_RUN_ON(signaling_thread());
  936. void OnTransportControllerGatheringState(cricket::IceGatheringState state)
  937. RTC_RUN_ON(signaling_thread());
  938. void OnTransportControllerCandidatesGathered(
  939. const std::string& transport_name,
  940. const std::vector<cricket::Candidate>& candidates)
  941. RTC_RUN_ON(signaling_thread());
  942. void OnTransportControllerCandidateError(
  943. const cricket::IceCandidateErrorEvent& event)
  944. RTC_RUN_ON(signaling_thread());
  945. void OnTransportControllerCandidatesRemoved(
  946. const std::vector<cricket::Candidate>& candidates)
  947. RTC_RUN_ON(signaling_thread());
  948. void OnTransportControllerCandidateChanged(
  949. const cricket::CandidatePairChangeEvent& event)
  950. RTC_RUN_ON(signaling_thread());
  951. void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
  952. const char* SessionErrorToString(SessionError error) const;
  953. std::string GetSessionErrorMsg() RTC_RUN_ON(signaling_thread());
  954. // Report the UMA metric SdpFormatReceived for the given remote offer.
  955. void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
  956. // Report inferred negotiated SDP semantics from a local/remote answer to the
  957. // UMA observer.
  958. void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
  959. // Invoked when TransportController connection completion is signaled.
  960. // Reports stats for all transports in use.
  961. void ReportTransportStats() RTC_RUN_ON(signaling_thread());
  962. // Gather the usage of IPv4/IPv6 as best connection.
  963. void ReportBestConnectionState(const cricket::TransportStats& stats);
  964. void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
  965. const std::set<cricket::MediaType>& media_types)
  966. RTC_RUN_ON(signaling_thread());
  967. void ReportIceCandidateCollected(const cricket::Candidate& candidate)
  968. RTC_RUN_ON(signaling_thread());
  969. void ReportRemoteIceCandidateAdded(const cricket::Candidate& candidate)
  970. RTC_RUN_ON(signaling_thread());
  971. void NoteUsageEvent(UsageEvent event);
  972. void ReportUsagePattern() const RTC_RUN_ON(signaling_thread());
  973. void OnSentPacket_w(const rtc::SentPacket& sent_packet);
  974. const std::string GetTransportName(const std::string& content_name)
  975. RTC_RUN_ON(signaling_thread());
  976. // Functions for dealing with transports.
  977. // Note that cricket code uses the term "channel" for what other code
  978. // refers to as "transport".
  979. // Destroys and clears the BaseChannel associated with the given transceiver,
  980. // if such channel is set.
  981. void DestroyTransceiverChannel(
  982. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  983. transceiver);
  984. // Destroys the RTP data channel transport and/or the SCTP data channel
  985. // transport and clears it.
  986. void DestroyDataChannelTransport() RTC_RUN_ON(signaling_thread());
  987. // Destroys the given ChannelInterface.
  988. // The channel cannot be accessed after this method is called.
  989. void DestroyChannelInterface(cricket::ChannelInterface* channel);
  990. // JsepTransportController::Observer override.
  991. //
  992. // Called by |transport_controller_| when processing transport information
  993. // from a session description, and the mapping from m= sections to transports
  994. // changed (as a result of BUNDLE negotiation, or m= sections being
  995. // rejected).
  996. bool OnTransportChanged(
  997. const std::string& mid,
  998. RtpTransportInternal* rtp_transport,
  999. rtc::scoped_refptr<DtlsTransport> dtls_transport,
  1000. DataChannelTransportInterface* data_channel_transport) override;
  1001. // RtpSenderBase::SetStreamsObserver override.
  1002. void OnSetStreams() override;
  1003. // Returns the CryptoOptions for this PeerConnection. This will always
  1004. // return the RTCConfiguration.crypto_options if set and will only default
  1005. // back to the PeerConnectionFactory settings if nothing was set.
  1006. CryptoOptions GetCryptoOptions() RTC_RUN_ON(signaling_thread());
  1007. // Returns rtp transport, result can not be nullptr.
  1008. RtpTransportInternal* GetRtpTransport(const std::string& mid)
  1009. RTC_RUN_ON(signaling_thread()) {
  1010. auto rtp_transport = transport_controller_->GetRtpTransport(mid);
  1011. RTC_DCHECK(rtp_transport);
  1012. return rtp_transport;
  1013. }
  1014. void UpdateNegotiationNeeded();
  1015. bool CheckIfNegotiationIsNeeded();
  1016. // | sdp_type | is the type of the SDP that caused the rollback.
  1017. RTCError Rollback(SdpType sdp_type);
  1018. // Storing the factory as a scoped reference pointer ensures that the memory
  1019. // in the PeerConnectionFactoryImpl remains available as long as the
  1020. // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
  1021. // However, since the reference counting is done in the
  1022. // PeerConnectionFactoryInterface all instances created using the raw pointer
  1023. // will refer to the same reference count.
  1024. const rtc::scoped_refptr<PeerConnectionFactory> factory_;
  1025. PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
  1026. nullptr;
  1027. // The EventLog needs to outlive |call_| (and any other object that uses it).
  1028. std::unique_ptr<RtcEventLog> event_log_ RTC_GUARDED_BY(worker_thread());
  1029. // Points to the same thing as `event_log_`. Since it's const, we may read the
  1030. // pointer (but not touch the object) from any thread.
  1031. RtcEventLog* const event_log_ptr_ RTC_PT_GUARDED_BY(worker_thread());
  1032. // The operations chain is used by the offer/answer exchange methods to ensure
  1033. // they are executed in the right order. For example, if
  1034. // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
  1035. // SRD operation will not start until CreateOffer() has completed. See
  1036. // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
  1037. rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
  1038. RTC_GUARDED_BY(signaling_thread());
  1039. SignalingState signaling_state_ RTC_GUARDED_BY(signaling_thread()) = kStable;
  1040. IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) =
  1041. kIceConnectionNew;
  1042. PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_
  1043. RTC_GUARDED_BY(signaling_thread()) = kIceConnectionNew;
  1044. PeerConnectionInterface::PeerConnectionState connection_state_
  1045. RTC_GUARDED_BY(signaling_thread()) = PeerConnectionState::kNew;
  1046. IceGatheringState ice_gathering_state_ RTC_GUARDED_BY(signaling_thread()) =
  1047. kIceGatheringNew;
  1048. PeerConnectionInterface::RTCConfiguration configuration_
  1049. RTC_GUARDED_BY(signaling_thread());
  1050. // Field-trial based configuration for datagram transport.
  1051. const DatagramTransportConfig datagram_transport_config_;
  1052. // Field-trial based configuration for datagram transport data channels.
  1053. const DatagramTransportDataChannelConfig
  1054. datagram_transport_data_channel_config_;
  1055. // Final, resolved value for whether datagram transport is in use.
  1056. bool use_datagram_transport_ RTC_GUARDED_BY(signaling_thread()) = false;
  1057. // Equivalent of |use_datagram_transport_|, but for its use with data
  1058. // channels.
  1059. bool use_datagram_transport_for_data_channels_
  1060. RTC_GUARDED_BY(signaling_thread()) = false;
  1061. // Resolved value of whether to use data channels only for incoming calls.
  1062. bool use_datagram_transport_for_data_channels_receive_only_
  1063. RTC_GUARDED_BY(signaling_thread()) = false;
  1064. // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it
  1065. // is not injected. It should be required once chromium supplies it.
  1066. std::unique_ptr<AsyncResolverFactory> async_resolver_factory_
  1067. RTC_GUARDED_BY(signaling_thread());
  1068. std::unique_ptr<cricket::PortAllocator>
  1069. port_allocator_; // TODO(bugs.webrtc.org/9987): Accessed on both
  1070. // signaling and network thread.
  1071. std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_;
  1072. std::unique_ptr<webrtc::IceTransportFactory>
  1073. ice_transport_factory_; // TODO(bugs.webrtc.org/9987): Accessed on the
  1074. // signaling thread but the underlying raw
  1075. // pointer is given to
  1076. // |jsep_transport_controller_| and used on the
  1077. // network thread.
  1078. std::unique_ptr<rtc::SSLCertificateVerifier>
  1079. tls_cert_verifier_; // TODO(bugs.webrtc.org/9987): Accessed on both
  1080. // signaling and network thread.
  1081. // One PeerConnection has only one RTCP CNAME.
  1082. // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
  1083. const std::string rtcp_cname_;
  1084. // Streams added via AddStream.
  1085. const rtc::scoped_refptr<StreamCollection> local_streams_
  1086. RTC_GUARDED_BY(signaling_thread());
  1087. // Streams created as a result of SetRemoteDescription.
  1088. const rtc::scoped_refptr<StreamCollection> remote_streams_
  1089. RTC_GUARDED_BY(signaling_thread());
  1090. std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
  1091. RTC_GUARDED_BY(signaling_thread());
  1092. // These lists store sender info seen in local/remote descriptions.
  1093. std::vector<RtpSenderInfo> remote_audio_sender_infos_
  1094. RTC_GUARDED_BY(signaling_thread());
  1095. std::vector<RtpSenderInfo> remote_video_sender_infos_
  1096. RTC_GUARDED_BY(signaling_thread());
  1097. std::vector<RtpSenderInfo> local_audio_sender_infos_
  1098. RTC_GUARDED_BY(signaling_thread());
  1099. std::vector<RtpSenderInfo> local_video_sender_infos_
  1100. RTC_GUARDED_BY(signaling_thread());
  1101. bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
  1102. // The unique_ptr belongs to the worker thread, but the Call object manages
  1103. // its own thread safety.
  1104. std::unique_ptr<Call> call_ RTC_GUARDED_BY(worker_thread());
  1105. rtc::AsyncInvoker rtcp_invoker_ RTC_GUARDED_BY(network_thread());
  1106. // Points to the same thing as `call_`. Since it's const, we may read the
  1107. // pointer from any thread.
  1108. Call* const call_ptr_;
  1109. std::unique_ptr<StatsCollector> stats_
  1110. RTC_GUARDED_BY(signaling_thread()); // A pointer is passed to senders_
  1111. rtc::scoped_refptr<RTCStatsCollector> stats_collector_
  1112. RTC_GUARDED_BY(signaling_thread());
  1113. // Holds changes made to transceivers during applying descriptors for
  1114. // potential rollback. Gets cleared once signaling state goes to stable.
  1115. std::map<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>,
  1116. TransceiverStableState>
  1117. transceiver_stable_states_by_transceivers_;
  1118. // Used when rolling back RTP data channels.
  1119. bool have_pending_rtp_data_channel_ RTC_GUARDED_BY(signaling_thread()) =
  1120. false;
  1121. // Holds remote stream ids for transceivers from stable state.
  1122. std::map<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>,
  1123. std::vector<std::string>>
  1124. remote_stream_ids_by_transceivers_;
  1125. std::vector<
  1126. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
  1127. transceivers_; // TODO(bugs.webrtc.org/9987): Accessed on both signaling
  1128. // and network thread.
  1129. // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
  1130. // line we create and use a stream with a random ID for our receivers. This is
  1131. // to support legacy endpoints that do not support the a=msid attribute (as
  1132. // opposed to streamless tracks with "a=msid:-").
  1133. rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
  1134. RTC_GUARDED_BY(signaling_thread());
  1135. // MIDs will be generated using this generator which will keep track of
  1136. // all the MIDs that have been seen over the life of the PeerConnection.
  1137. rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
  1138. SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
  1139. SessionError::kNone;
  1140. std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
  1141. std::string session_id_ RTC_GUARDED_BY(signaling_thread());
  1142. std::unique_ptr<JsepTransportController>
  1143. transport_controller_; // TODO(bugs.webrtc.org/9987): Accessed on both
  1144. // signaling and network thread.
  1145. std::unique_ptr<cricket::SctpTransportInternalFactory>
  1146. sctp_factory_; // TODO(bugs.webrtc.org/9987): Accessed on both
  1147. // signaling and network thread.
  1148. // |sctp_mid_| is the content name (MID) in SDP.
  1149. // Note: this is used as the data channel MID by both SCTP and data channel
  1150. // transports. It is set when either transport is initialized and unset when
  1151. // both transports are deleted.
  1152. // There is one copy on the signaling thread and another copy on the
  1153. // networking thread. Changes are always initiated from the signaling
  1154. // thread, but applied first on the networking thread via an invoke().
  1155. absl::optional<std::string> sctp_mid_s_ RTC_GUARDED_BY(signaling_thread());
  1156. absl::optional<std::string> sctp_mid_n_ RTC_GUARDED_BY(network_thread());
  1157. // Whether this peer is the caller. Set when the local description is applied.
  1158. absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
  1159. std::unique_ptr<SessionDescriptionInterface> current_local_description_
  1160. RTC_GUARDED_BY(signaling_thread());
  1161. std::unique_ptr<SessionDescriptionInterface> pending_local_description_
  1162. RTC_GUARDED_BY(signaling_thread());
  1163. std::unique_ptr<SessionDescriptionInterface> current_remote_description_
  1164. RTC_GUARDED_BY(signaling_thread());
  1165. std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
  1166. RTC_GUARDED_BY(signaling_thread());
  1167. bool dtls_enabled_ RTC_GUARDED_BY(signaling_thread()) = false;
  1168. // List of content names for which the remote side triggered an ICE restart.
  1169. std::set<std::string> pending_ice_restarts_
  1170. RTC_GUARDED_BY(signaling_thread());
  1171. std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
  1172. RTC_GUARDED_BY(signaling_thread());
  1173. // Member variables for caching global options.
  1174. cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
  1175. cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
  1176. int usage_event_accumulator_ RTC_GUARDED_BY(signaling_thread()) = 0;
  1177. bool return_histogram_very_quickly_ RTC_GUARDED_BY(signaling_thread()) =
  1178. false;
  1179. // This object should be used to generate any SSRC that is not explicitly
  1180. // specified by the user (or by the remote party).
  1181. // The generator is not used directly, instead it is passed on to the
  1182. // channel manager and the session description factory.
  1183. rtc::UniqueRandomIdGenerator ssrc_generator_
  1184. RTC_GUARDED_BY(signaling_thread());
  1185. // A video bitrate allocator factory.
  1186. // This can injected using the PeerConnectionDependencies,
  1187. // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
  1188. // Note that one can still choose to override this in a MediaEngine
  1189. // if one wants too.
  1190. std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
  1191. video_bitrate_allocator_factory_;
  1192. std::unique_ptr<LocalIceCredentialsToReplace>
  1193. local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
  1194. bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
  1195. DataChannelController data_channel_controller_;
  1196. rtc::WeakPtrFactory<PeerConnection> weak_ptr_factory_
  1197. RTC_GUARDED_BY(signaling_thread());
  1198. };
  1199. } // namespace webrtc
  1200. #endif // PC_PEER_CONNECTION_H_