peer_connection.h 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 <functional>
  13. #include <map>
  14. #include <memory>
  15. #include <set>
  16. #include <string>
  17. #include <utility>
  18. #include <vector>
  19. #include "api/peer_connection_interface.h"
  20. #include "api/transport/data_channel_transport_interface.h"
  21. #include "api/turn_customizer.h"
  22. #include "pc/data_channel_controller.h"
  23. #include "pc/ice_server_parsing.h"
  24. #include "pc/jsep_transport_controller.h"
  25. #include "pc/peer_connection_factory.h"
  26. #include "pc/peer_connection_internal.h"
  27. #include "pc/rtc_stats_collector.h"
  28. #include "pc/rtp_sender.h"
  29. #include "pc/rtp_transceiver.h"
  30. #include "pc/sctp_transport.h"
  31. #include "pc/sdp_offer_answer.h"
  32. #include "pc/stats_collector.h"
  33. #include "pc/stream_collection.h"
  34. #include "pc/transceiver_list.h"
  35. #include "pc/webrtc_session_description_factory.h"
  36. #include "rtc_base/experiments/field_trial_parser.h"
  37. #include "rtc_base/operations_chain.h"
  38. #include "rtc_base/race_checker.h"
  39. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  40. #include "rtc_base/unique_id_generator.h"
  41. #include "rtc_base/weak_ptr.h"
  42. namespace webrtc {
  43. class MediaStreamObserver;
  44. class VideoRtpReceiver;
  45. class RtcEventLog;
  46. class SdpOfferAnswerHandler;
  47. // PeerConnection is the implementation of the PeerConnection object as defined
  48. // by the PeerConnectionInterface API surface.
  49. // The class currently is solely responsible for the following:
  50. // - Managing the session state machine (signaling state).
  51. // - Creating and initializing lower-level objects, like PortAllocator and
  52. // BaseChannels.
  53. // - Owning and managing the life cycle of the RtpSender/RtpReceiver and track
  54. // objects.
  55. // - Tracking the current and pending local/remote session descriptions.
  56. // The class currently is jointly responsible for the following:
  57. // - Parsing and interpreting SDP.
  58. // - Generating offers and answers based on the current state.
  59. // - The ICE state machine.
  60. // - Generating stats.
  61. class PeerConnection : public PeerConnectionInternal,
  62. public JsepTransportController::Observer,
  63. public RtpSenderBase::SetStreamsObserver,
  64. public rtc::MessageHandler,
  65. public sigslot::has_slots<> {
  66. public:
  67. // A bit in the usage pattern is registered when its defining event occurs at
  68. // least once.
  69. enum class UsageEvent : int {
  70. TURN_SERVER_ADDED = 0x01,
  71. STUN_SERVER_ADDED = 0x02,
  72. DATA_ADDED = 0x04,
  73. AUDIO_ADDED = 0x08,
  74. VIDEO_ADDED = 0x10,
  75. // |SetLocalDescription| returns successfully.
  76. SET_LOCAL_DESCRIPTION_SUCCEEDED = 0x20,
  77. // |SetRemoteDescription| returns successfully.
  78. SET_REMOTE_DESCRIPTION_SUCCEEDED = 0x40,
  79. // A local candidate (with type host, server-reflexive, or relay) is
  80. // collected.
  81. CANDIDATE_COLLECTED = 0x80,
  82. // A remote candidate is successfully added via |AddIceCandidate|.
  83. ADD_ICE_CANDIDATE_SUCCEEDED = 0x100,
  84. ICE_STATE_CONNECTED = 0x200,
  85. CLOSE_CALLED = 0x400,
  86. // A local candidate with private IP is collected.
  87. PRIVATE_CANDIDATE_COLLECTED = 0x800,
  88. // A remote candidate with private IP is added, either via AddiceCandidate
  89. // or from the remote description.
  90. REMOTE_PRIVATE_CANDIDATE_ADDED = 0x1000,
  91. // A local mDNS candidate is collected.
  92. MDNS_CANDIDATE_COLLECTED = 0x2000,
  93. // A remote mDNS candidate is added, either via AddIceCandidate or from the
  94. // remote description.
  95. REMOTE_MDNS_CANDIDATE_ADDED = 0x4000,
  96. // A local candidate with IPv6 address is collected.
  97. IPV6_CANDIDATE_COLLECTED = 0x8000,
  98. // A remote candidate with IPv6 address is added, either via AddIceCandidate
  99. // or from the remote description.
  100. REMOTE_IPV6_CANDIDATE_ADDED = 0x10000,
  101. // A remote candidate (with type host, server-reflexive, or relay) is
  102. // successfully added, either via AddIceCandidate or from the remote
  103. // description.
  104. REMOTE_CANDIDATE_ADDED = 0x20000,
  105. // An explicit host-host candidate pair is selected, i.e. both the local and
  106. // the remote candidates have the host type. This does not include candidate
  107. // pairs formed with equivalent prflx remote candidates, e.g. a host-prflx
  108. // pair where the prflx candidate has the same base as a host candidate of
  109. // the remote peer.
  110. DIRECT_CONNECTION_SELECTED = 0x40000,
  111. MAX_VALUE = 0x80000,
  112. };
  113. explicit PeerConnection(PeerConnectionFactory* factory,
  114. std::unique_ptr<RtcEventLog> event_log,
  115. std::unique_ptr<Call> call);
  116. bool Initialize(
  117. const PeerConnectionInterface::RTCConfiguration& configuration,
  118. PeerConnectionDependencies dependencies);
  119. rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
  120. rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
  121. bool AddStream(MediaStreamInterface* local_stream) override;
  122. void RemoveStream(MediaStreamInterface* local_stream) override;
  123. RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
  124. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  125. const std::vector<std::string>& stream_ids) override;
  126. bool RemoveTrack(RtpSenderInterface* sender) override;
  127. RTCError RemoveTrackNew(
  128. rtc::scoped_refptr<RtpSenderInterface> sender) override;
  129. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  130. rtc::scoped_refptr<MediaStreamTrackInterface> track) override;
  131. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  132. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  133. const RtpTransceiverInit& init) override;
  134. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  135. cricket::MediaType media_type) override;
  136. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  137. cricket::MediaType media_type,
  138. const RtpTransceiverInit& init) override;
  139. // Gets the DTLS SSL certificate associated with the audio transport on the
  140. // remote side. This will become populated once the DTLS connection with the
  141. // peer has been completed, as indicated by the ICE connection state
  142. // transitioning to kIceConnectionCompleted.
  143. // Note that this will be removed once we implement RTCDtlsTransport which
  144. // has standardized method for getting this information.
  145. // See https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface
  146. std::unique_ptr<rtc::SSLCertificate> GetRemoteAudioSSLCertificate();
  147. // Version of the above method that returns the full certificate chain.
  148. std::unique_ptr<rtc::SSLCertChain> GetRemoteAudioSSLCertChain();
  149. rtc::scoped_refptr<RtpSenderInterface> CreateSender(
  150. const std::string& kind,
  151. const std::string& stream_id) override;
  152. std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
  153. const override;
  154. std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
  155. const override;
  156. std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers()
  157. const override;
  158. rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
  159. const std::string& label,
  160. const DataChannelInit* config) override;
  161. // WARNING: LEGACY. See peerconnectioninterface.h
  162. bool GetStats(StatsObserver* observer,
  163. webrtc::MediaStreamTrackInterface* track,
  164. StatsOutputLevel level) override;
  165. // Spec-complaint GetStats(). See peerconnectioninterface.h
  166. void GetStats(RTCStatsCollectorCallback* callback) override;
  167. void GetStats(
  168. rtc::scoped_refptr<RtpSenderInterface> selector,
  169. rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
  170. void GetStats(
  171. rtc::scoped_refptr<RtpReceiverInterface> selector,
  172. rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override;
  173. void ClearStatsCache() override;
  174. SignalingState signaling_state() override;
  175. IceConnectionState ice_connection_state() override;
  176. IceConnectionState standardized_ice_connection_state() override;
  177. PeerConnectionState peer_connection_state() override;
  178. IceGatheringState ice_gathering_state() override;
  179. absl::optional<bool> can_trickle_ice_candidates() override;
  180. const SessionDescriptionInterface* local_description() const override;
  181. const SessionDescriptionInterface* remote_description() const override;
  182. const SessionDescriptionInterface* current_local_description() const override;
  183. const SessionDescriptionInterface* current_remote_description()
  184. const override;
  185. const SessionDescriptionInterface* pending_local_description() const override;
  186. const SessionDescriptionInterface* pending_remote_description()
  187. const override;
  188. void RestartIce() override;
  189. // JSEP01
  190. void CreateOffer(CreateSessionDescriptionObserver* observer,
  191. const RTCOfferAnswerOptions& options) override;
  192. void CreateAnswer(CreateSessionDescriptionObserver* observer,
  193. const RTCOfferAnswerOptions& options) override;
  194. void SetLocalDescription(
  195. std::unique_ptr<SessionDescriptionInterface> desc,
  196. rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer)
  197. override;
  198. void SetLocalDescription(
  199. rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer)
  200. override;
  201. // TODO(https://crbug.com/webrtc/11798): Delete these methods in favor of the
  202. // ones taking SetLocalDescriptionObserverInterface as argument.
  203. void SetLocalDescription(SetSessionDescriptionObserver* observer,
  204. SessionDescriptionInterface* desc) override;
  205. void SetLocalDescription(SetSessionDescriptionObserver* observer) override;
  206. void SetRemoteDescription(
  207. std::unique_ptr<SessionDescriptionInterface> desc,
  208. rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)
  209. override;
  210. // TODO(https://crbug.com/webrtc/11798): Delete this methods in favor of the
  211. // ones taking SetRemoteDescriptionObserverInterface as argument.
  212. void SetRemoteDescription(SetSessionDescriptionObserver* observer,
  213. SessionDescriptionInterface* desc) override;
  214. PeerConnectionInterface::RTCConfiguration GetConfiguration() override;
  215. RTCError SetConfiguration(
  216. const PeerConnectionInterface::RTCConfiguration& configuration) override;
  217. bool AddIceCandidate(const IceCandidateInterface* candidate) override;
  218. void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
  219. std::function<void(RTCError)> callback) override;
  220. bool RemoveIceCandidates(
  221. const std::vector<cricket::Candidate>& candidates) override;
  222. RTCError SetBitrate(const BitrateSettings& bitrate) override;
  223. void SetAudioPlayout(bool playout) override;
  224. void SetAudioRecording(bool recording) override;
  225. rtc::scoped_refptr<DtlsTransportInterface> LookupDtlsTransportByMid(
  226. const std::string& mid) override;
  227. rtc::scoped_refptr<DtlsTransport> LookupDtlsTransportByMidInternal(
  228. const std::string& mid);
  229. rtc::scoped_refptr<SctpTransportInterface> GetSctpTransport() const override;
  230. void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override;
  231. bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output,
  232. int64_t output_period_ms) override;
  233. bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override;
  234. void StopRtcEventLog() override;
  235. void Close() override;
  236. rtc::Thread* signaling_thread() const final {
  237. return factory_->signaling_thread();
  238. }
  239. // PeerConnectionInternal implementation.
  240. rtc::Thread* network_thread() const final {
  241. return factory_->network_thread();
  242. }
  243. rtc::Thread* worker_thread() const final { return factory_->worker_thread(); }
  244. std::string session_id() const override {
  245. RTC_DCHECK_RUN_ON(signaling_thread());
  246. return session_id_;
  247. }
  248. bool initial_offerer() const override {
  249. RTC_DCHECK_RUN_ON(signaling_thread());
  250. return transport_controller_ && transport_controller_->initial_offerer();
  251. }
  252. std::vector<
  253. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
  254. GetTransceiversInternal() const override {
  255. RTC_DCHECK_RUN_ON(signaling_thread());
  256. return transceivers_.List();
  257. }
  258. sigslot::signal1<RtpDataChannel*>& SignalRtpDataChannelCreated() override {
  259. return data_channel_controller_.SignalRtpDataChannelCreated();
  260. }
  261. sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() override {
  262. return data_channel_controller_.SignalSctpDataChannelCreated();
  263. }
  264. cricket::RtpDataChannel* rtp_data_channel() const override {
  265. return data_channel_controller_.rtp_data_channel();
  266. }
  267. std::vector<DataChannelStats> GetDataChannelStats() const override;
  268. absl::optional<std::string> sctp_transport_name() const override;
  269. cricket::CandidateStatsList GetPooledCandidateStats() const override;
  270. std::map<std::string, std::string> GetTransportNamesByMid() const override;
  271. std::map<std::string, cricket::TransportStats> GetTransportStatsByNames(
  272. const std::set<std::string>& transport_names) override;
  273. Call::Stats GetCallStats() override;
  274. bool GetLocalCertificate(
  275. const std::string& transport_name,
  276. rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override;
  277. std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain(
  278. const std::string& transport_name) override;
  279. bool IceRestartPending(const std::string& content_name) const override;
  280. bool NeedsIceRestart(const std::string& content_name) const override;
  281. bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override;
  282. // Functions needed by DataChannelController
  283. void NoteDataAddedEvent() { NoteUsageEvent(UsageEvent::DATA_ADDED); }
  284. // Returns the observer. Will crash on CHECK if the observer is removed.
  285. PeerConnectionObserver* Observer() const;
  286. bool IsClosed() const {
  287. RTC_DCHECK_RUN_ON(signaling_thread());
  288. return sdp_handler_.signaling_state() == PeerConnectionInterface::kClosed;
  289. }
  290. // Get current SSL role used by SCTP's underlying transport.
  291. bool GetSctpSslRole(rtc::SSLRole* role);
  292. // Handler for the "channel closed" signal
  293. void OnSctpDataChannelClosed(DataChannelInterface* channel);
  294. bool ShouldFireNegotiationNeededEvent(uint32_t event_id) override;
  295. // Functions needed by SdpOfferAnswerHandler
  296. StatsCollector* stats() {
  297. RTC_DCHECK_RUN_ON(signaling_thread());
  298. return stats_.get();
  299. }
  300. DataChannelController* data_channel_controller() {
  301. RTC_DCHECK_RUN_ON(signaling_thread());
  302. return &data_channel_controller_;
  303. }
  304. bool dtls_enabled() const {
  305. RTC_DCHECK_RUN_ON(signaling_thread());
  306. return dtls_enabled_;
  307. }
  308. const PeerConnectionInterface::RTCConfiguration* configuration() const {
  309. RTC_DCHECK_RUN_ON(signaling_thread());
  310. return &configuration_;
  311. }
  312. rtc::scoped_refptr<StreamCollection> remote_streams_internal() const {
  313. RTC_DCHECK_RUN_ON(signaling_thread());
  314. return remote_streams_;
  315. }
  316. rtc::UniqueStringGenerator* mid_generator() {
  317. RTC_DCHECK_RUN_ON(signaling_thread());
  318. return &mid_generator_;
  319. }
  320. // Functions made public for testing.
  321. void ReturnHistogramVeryQuicklyForTesting() {
  322. RTC_DCHECK_RUN_ON(signaling_thread());
  323. return_histogram_very_quickly_ = true;
  324. }
  325. void RequestUsagePatternReportForTesting();
  326. absl::optional<std::string> sctp_mid() {
  327. RTC_DCHECK_RUN_ON(signaling_thread());
  328. return sctp_mid_s_;
  329. }
  330. protected:
  331. ~PeerConnection() override;
  332. private:
  333. // While refactoring: Allow access from SDP negotiation
  334. // TOOD(https://bugs.webrtc.org/11995): Remove friendship.
  335. friend class SdpOfferAnswerHandler;
  336. struct RtpSenderInfo {
  337. RtpSenderInfo() : first_ssrc(0) {}
  338. RtpSenderInfo(const std::string& stream_id,
  339. const std::string sender_id,
  340. uint32_t ssrc)
  341. : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
  342. bool operator==(const RtpSenderInfo& other) {
  343. return this->stream_id == other.stream_id &&
  344. this->sender_id == other.sender_id &&
  345. this->first_ssrc == other.first_ssrc;
  346. }
  347. std::string stream_id;
  348. std::string sender_id;
  349. // An RtpSender can have many SSRCs. The first one is used as a sort of ID
  350. // for communicating with the lower layers.
  351. uint32_t first_ssrc;
  352. };
  353. // Implements MessageHandler.
  354. void OnMessage(rtc::Message* msg) override;
  355. // Plan B helpers for getting the voice/video media channels for the single
  356. // audio/video transceiver, if it exists.
  357. cricket::VoiceMediaChannel* voice_media_channel() const
  358. RTC_RUN_ON(signaling_thread());
  359. cricket::VideoMediaChannel* video_media_channel() const
  360. RTC_RUN_ON(signaling_thread());
  361. std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
  362. GetSendersInternal() const;
  363. std::vector<
  364. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
  365. GetReceiversInternal() const RTC_RUN_ON(signaling_thread());
  366. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  367. GetAudioTransceiver() const;
  368. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  369. GetVideoTransceiver() const;
  370. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  371. GetFirstAudioTransceiver() const RTC_RUN_ON(signaling_thread());
  372. // Helper function to remove stopped transceivers.
  373. void RemoveStoppedTransceivers();
  374. void CreateAudioReceiver(MediaStreamInterface* stream,
  375. const RtpSenderInfo& remote_sender_info)
  376. RTC_RUN_ON(signaling_thread());
  377. void CreateVideoReceiver(MediaStreamInterface* stream,
  378. const RtpSenderInfo& remote_sender_info)
  379. RTC_RUN_ON(signaling_thread());
  380. rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
  381. const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread());
  382. // May be called either by AddStream/RemoveStream, or when a track is
  383. // added/removed from a stream previously added via AddStream.
  384. void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream)
  385. RTC_RUN_ON(signaling_thread());
  386. void RemoveAudioTrack(AudioTrackInterface* track,
  387. MediaStreamInterface* stream)
  388. RTC_RUN_ON(signaling_thread());
  389. void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream)
  390. RTC_RUN_ON(signaling_thread());
  391. void RemoveVideoTrack(VideoTrackInterface* track,
  392. MediaStreamInterface* stream)
  393. RTC_RUN_ON(signaling_thread());
  394. // AddTrack implementation when Unified Plan is specified.
  395. RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan(
  396. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  397. const std::vector<std::string>& stream_ids)
  398. RTC_RUN_ON(signaling_thread());
  399. // AddTrack implementation when Plan B is specified.
  400. RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
  401. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  402. const std::vector<std::string>& stream_ids)
  403. RTC_RUN_ON(signaling_thread());
  404. // Returns the first RtpTransceiver suitable for a newly added track, if such
  405. // transceiver is available.
  406. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  407. FindFirstTransceiverForAddedTrack(
  408. rtc::scoped_refptr<MediaStreamTrackInterface> track)
  409. RTC_RUN_ON(signaling_thread());
  410. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  411. FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender)
  412. RTC_RUN_ON(signaling_thread());
  413. // Internal implementation for AddTransceiver family of methods. If
  414. // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful.
  415. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver(
  416. cricket::MediaType media_type,
  417. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  418. const RtpTransceiverInit& init,
  419. bool fire_callback = true) RTC_RUN_ON(signaling_thread());
  420. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
  421. CreateSender(cricket::MediaType media_type,
  422. const std::string& id,
  423. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  424. const std::vector<std::string>& stream_ids,
  425. const std::vector<RtpEncodingParameters>& send_encodings);
  426. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  427. CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
  428. // Create a new RtpTransceiver of the given type and add it to the list of
  429. // transceivers.
  430. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  431. CreateAndAddTransceiver(
  432. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
  433. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  434. receiver);
  435. void SetIceConnectionState(IceConnectionState new_state);
  436. void SetStandardizedIceConnectionState(
  437. PeerConnectionInterface::IceConnectionState new_state)
  438. RTC_RUN_ON(signaling_thread());
  439. void SetConnectionState(
  440. PeerConnectionInterface::PeerConnectionState new_state)
  441. RTC_RUN_ON(signaling_thread());
  442. // Called any time the IceGatheringState changes.
  443. void OnIceGatheringChange(IceGatheringState new_state)
  444. RTC_RUN_ON(signaling_thread());
  445. // New ICE candidate has been gathered.
  446. void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)
  447. RTC_RUN_ON(signaling_thread());
  448. // Gathering of an ICE candidate failed.
  449. void OnIceCandidateError(const std::string& address,
  450. int port,
  451. const std::string& url,
  452. int error_code,
  453. const std::string& error_text)
  454. RTC_RUN_ON(signaling_thread());
  455. // Some local ICE candidates have been removed.
  456. void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates)
  457. RTC_RUN_ON(signaling_thread());
  458. void OnSelectedCandidatePairChanged(
  459. const cricket::CandidatePairChangeEvent& event)
  460. RTC_RUN_ON(signaling_thread());
  461. // Signals from MediaStreamObserver.
  462. void OnAudioTrackAdded(AudioTrackInterface* track,
  463. MediaStreamInterface* stream)
  464. RTC_RUN_ON(signaling_thread());
  465. void OnAudioTrackRemoved(AudioTrackInterface* track,
  466. MediaStreamInterface* stream)
  467. RTC_RUN_ON(signaling_thread());
  468. void OnVideoTrackAdded(VideoTrackInterface* track,
  469. MediaStreamInterface* stream)
  470. RTC_RUN_ON(signaling_thread());
  471. void OnVideoTrackRemoved(VideoTrackInterface* track,
  472. MediaStreamInterface* stream)
  473. RTC_RUN_ON(signaling_thread());
  474. void PostSetSessionDescriptionSuccess(
  475. SetSessionDescriptionObserver* observer);
  476. void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
  477. RTCError&& error);
  478. void PostCreateSessionDescriptionFailure(
  479. CreateSessionDescriptionObserver* observer,
  480. RTCError error);
  481. // Returns the RtpTransceiver, if found, that is associated to the given MID.
  482. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  483. GetAssociatedTransceiver(const std::string& mid) const;
  484. // Returns the RtpTransceiver, if found, that was assigned to the given mline
  485. // index in CreateOffer.
  486. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  487. GetTransceiverByMLineIndex(size_t mline_index) const;
  488. // Runs the algorithm **process the removal of a remote track** specified in
  489. // the WebRTC specification.
  490. // This method will update the following lists:
  491. // |remove_list| is the list of transceivers for which the receiving track is
  492. // being removed.
  493. // |removed_streams| is the list of streams which no longer have a receiving
  494. // track so should be removed.
  495. // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
  496. void ProcessRemovalOfRemoteTrack(
  497. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  498. transceiver,
  499. std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
  500. std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
  501. void RemoveRemoteStreamsIfEmpty(
  502. const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
  503. remote_streams,
  504. std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
  505. void OnNegotiationNeeded();
  506. RTCError HandleLegacyOfferOptions(const RTCOfferAnswerOptions& options);
  507. void RemoveRecvDirectionFromReceivingTransceiversOfType(
  508. cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
  509. void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
  510. std::vector<
  511. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
  512. GetReceivingTransceiversOfType(cricket::MediaType media_type)
  513. RTC_RUN_ON(signaling_thread());
  514. // Generates MediaDescriptionOptions for the |session_opts| based on existing
  515. // local description or remote description.
  516. void GenerateMediaDescriptionOptions(
  517. const SessionDescriptionInterface* session_desc,
  518. RtpTransceiverDirection audio_direction,
  519. RtpTransceiverDirection video_direction,
  520. absl::optional<size_t>* audio_index,
  521. absl::optional<size_t>* video_index,
  522. absl::optional<size_t>* data_index,
  523. cricket::MediaSessionOptions* session_options);
  524. // Generates the active MediaDescriptionOptions for the local data channel
  525. // given the specified MID.
  526. cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
  527. const std::string& mid) const;
  528. // Generates the rejected MediaDescriptionOptions for the local data channel
  529. // given the specified MID.
  530. cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
  531. const std::string& mid) const;
  532. // Returns the MID for the data section associated with either the
  533. // RtpDataChannel or SCTP data channel, if it has been set. If no data
  534. // channels are configured this will return nullopt.
  535. absl::optional<std::string> GetDataMid() const;
  536. // Remove all local and remote senders of type |media_type|.
  537. // Called when a media type is rejected (m-line set to port 0).
  538. void RemoveSenders(cricket::MediaType media_type);
  539. // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
  540. // and existing MediaStreamTracks are removed if there is no corresponding
  541. // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
  542. // is created if it doesn't exist; if false, it's removed if it exists.
  543. // |media_type| is the type of the |streams| and can be either audio or video.
  544. // If a new MediaStream is created it is added to |new_streams|.
  545. void UpdateRemoteSendersList(
  546. const std::vector<cricket::StreamParams>& streams,
  547. bool default_track_needed,
  548. cricket::MediaType media_type,
  549. StreamCollection* new_streams);
  550. // Triggered when a remote sender has been seen for the first time in a remote
  551. // session description. It creates a remote MediaStreamTrackInterface
  552. // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
  553. void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
  554. cricket::MediaType media_type)
  555. RTC_RUN_ON(signaling_thread());
  556. // Triggered when a remote sender has been removed from a remote session
  557. // description. It removes the remote sender with id |sender_id| from a remote
  558. // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
  559. void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
  560. cricket::MediaType media_type)
  561. RTC_RUN_ON(signaling_thread());
  562. // Finds remote MediaStreams without any tracks and removes them from
  563. // |remote_streams_| and notifies the observer that the MediaStreams no longer
  564. // exist.
  565. void UpdateEndedRemoteMediaStreams();
  566. // Loops through the vector of |streams| and finds added and removed
  567. // StreamParams since last time this method was called.
  568. // For each new or removed StreamParam, OnLocalSenderSeen or
  569. // OnLocalSenderRemoved is invoked.
  570. void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
  571. cricket::MediaType media_type);
  572. // Triggered when a local sender has been seen for the first time in a local
  573. // session description.
  574. // This method triggers CreateAudioSender or CreateVideoSender if the rtp
  575. // streams in the local SessionDescription can be mapped to a MediaStreamTrack
  576. // in a MediaStream in |local_streams_|
  577. void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
  578. cricket::MediaType media_type)
  579. RTC_RUN_ON(signaling_thread());
  580. // Triggered when a local sender has been removed from a local session
  581. // description.
  582. // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
  583. // has been removed from the local SessionDescription and the stream can be
  584. // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
  585. void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
  586. cricket::MediaType media_type)
  587. RTC_RUN_ON(signaling_thread());
  588. // Returns true if the PeerConnection is configured to use Unified Plan
  589. // semantics for creating offers/answers and setting local/remote
  590. // descriptions. If this is true the RtpTransceiver API will also be available
  591. // to the user. If this is false, Plan B semantics are assumed.
  592. // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once
  593. // sufficient time has passed.
  594. bool IsUnifiedPlan() const {
  595. RTC_DCHECK_RUN_ON(signaling_thread());
  596. return configuration_.sdp_semantics == SdpSemantics::kUnifiedPlan;
  597. }
  598. // Return the RtpSender with the given track attached.
  599. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
  600. FindSenderForTrack(MediaStreamTrackInterface* track) const
  601. RTC_RUN_ON(signaling_thread());
  602. // Return the RtpSender with the given id, or null if none exists.
  603. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
  604. FindSenderById(const std::string& sender_id) const
  605. RTC_RUN_ON(signaling_thread());
  606. // Return the RtpReceiver with the given id, or null if none exists.
  607. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  608. FindReceiverById(const std::string& receiver_id) const
  609. RTC_RUN_ON(signaling_thread());
  610. std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
  611. cricket::MediaType media_type);
  612. std::vector<RtpSenderInfo>* GetLocalSenderInfos(
  613. cricket::MediaType media_type);
  614. const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
  615. const std::string& stream_id,
  616. const std::string sender_id) const;
  617. // Returns the specified SCTP DataChannel in sctp_data_channels_,
  618. // or nullptr if not found.
  619. SctpDataChannel* FindDataChannelBySid(int sid) const
  620. RTC_RUN_ON(signaling_thread());
  621. // Called when first configuring the port allocator.
  622. struct InitializePortAllocatorResult {
  623. bool enable_ipv6;
  624. };
  625. InitializePortAllocatorResult InitializePortAllocator_n(
  626. const cricket::ServerAddresses& stun_servers,
  627. const std::vector<cricket::RelayServerConfig>& turn_servers,
  628. const RTCConfiguration& configuration);
  629. // Called when SetConfiguration is called to apply the supported subset
  630. // of the configuration on the network thread.
  631. bool ReconfigurePortAllocator_n(
  632. const cricket::ServerAddresses& stun_servers,
  633. const std::vector<cricket::RelayServerConfig>& turn_servers,
  634. IceTransportsType type,
  635. int candidate_pool_size,
  636. PortPrunePolicy turn_port_prune_policy,
  637. webrtc::TurnCustomizer* turn_customizer,
  638. absl::optional<int> stun_candidate_keepalive_interval,
  639. bool have_local_description);
  640. // Starts output of an RTC event log to the given output object.
  641. // This function should only be called from the worker thread.
  642. bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output,
  643. int64_t output_period_ms);
  644. // Stops recording an RTC event log.
  645. // This function should only be called from the worker thread.
  646. void StopRtcEventLog_w();
  647. // Ensures the configuration doesn't have any parameters with invalid values,
  648. // or values that conflict with other parameters.
  649. //
  650. // Returns RTCError::OK() if there are no issues.
  651. RTCError ValidateConfiguration(const RTCConfiguration& config) const;
  652. cricket::ChannelManager* channel_manager() const;
  653. enum class SessionError {
  654. kNone, // No error.
  655. kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
  656. kTransport, // Error from the underlying transport.
  657. };
  658. // Returns the last error in the session. See the enum above for details.
  659. SessionError session_error() const {
  660. RTC_DCHECK_RUN_ON(signaling_thread());
  661. return session_error_;
  662. }
  663. const std::string& session_error_desc() const { return session_error_desc_; }
  664. cricket::ChannelInterface* GetChannel(const std::string& content_name);
  665. cricket::IceConfig ParseIceConfig(
  666. const PeerConnectionInterface::RTCConfiguration& config) const;
  667. cricket::DataChannelType data_channel_type() const;
  668. // Called when an RTCCertificate is generated or retrieved by
  669. // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
  670. void OnCertificateReady(
  671. const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
  672. // Updates the error state, signaling if necessary.
  673. void SetSessionError(SessionError error, const std::string& error_desc);
  674. // Based on number of transceivers per media type, enabled or disable
  675. // payload type based demuxing in the affected channels.
  676. bool UpdatePayloadTypeDemuxingState(cricket::ContentSource source)
  677. RTC_RUN_ON(signaling_thread());
  678. // Push the media parts of the local or remote session description
  679. // down to all of the channels.
  680. RTCError PushdownMediaDescription(SdpType type,
  681. cricket::ContentSource source);
  682. RTCError PushdownTransportDescription(cricket::ContentSource source,
  683. SdpType type);
  684. // Returns true and the TransportInfo of the given |content_name|
  685. // from |description|. Returns false if it's not available.
  686. static bool GetTransportDescription(
  687. const cricket::SessionDescription* description,
  688. const std::string& content_name,
  689. cricket::TransportDescription* info);
  690. // Enables media channels to allow sending of media.
  691. // This enables media to flow on all configured audio/video channels and the
  692. // RtpDataChannel.
  693. void EnableSending();
  694. // Destroys all BaseChannels and destroys the SCTP data channel, if present.
  695. void DestroyAllChannels() RTC_RUN_ON(signaling_thread());
  696. // Returns the media index for a local ice candidate given the content name.
  697. // Returns false if the local session description does not have a media
  698. // content called |content_name|.
  699. bool GetLocalCandidateMediaIndex(const std::string& content_name,
  700. int* sdp_mline_index)
  701. RTC_RUN_ON(signaling_thread());
  702. // Uses all remote candidates in |remote_desc| in this session.
  703. bool UseCandidatesInSessionDescription(
  704. const SessionDescriptionInterface* remote_desc);
  705. // Uses |candidate| in this session.
  706. bool UseCandidate(const IceCandidateInterface* candidate);
  707. RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
  708. const SessionDescriptionInterface* description,
  709. const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
  710. // Deletes the corresponding channel of contents that don't exist in |desc|.
  711. // |desc| can be null. This means that all channels are deleted.
  712. void RemoveUnusedChannels(const cricket::SessionDescription* desc);
  713. // Allocates media channels based on the |desc|. If |desc| doesn't have
  714. // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
  715. // This method will also delete any existing media channels before creating.
  716. RTCError CreateChannels(const cricket::SessionDescription& desc);
  717. // Helper methods to create media channels.
  718. cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
  719. cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
  720. bool CreateDataChannel(const std::string& mid);
  721. bool SetupDataChannelTransport_n(const std::string& mid)
  722. RTC_RUN_ON(network_thread());
  723. void TeardownDataChannelTransport_n() RTC_RUN_ON(network_thread());
  724. bool ValidateBundleSettings(const cricket::SessionDescription* desc);
  725. bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
  726. // Verifies a=setup attribute as per RFC 5763.
  727. bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
  728. SdpType type);
  729. // Returns true if we are ready to push down the remote candidate.
  730. // |remote_desc| is the new remote description, or NULL if the current remote
  731. // description should be used. Output |valid| is true if the candidate media
  732. // index is valid.
  733. bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
  734. const SessionDescriptionInterface* remote_desc,
  735. bool* valid);
  736. // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by
  737. // this session.
  738. bool SrtpRequired() const RTC_RUN_ON(signaling_thread());
  739. // JsepTransportController signal handlers.
  740. void OnTransportControllerConnectionState(cricket::IceConnectionState state)
  741. RTC_RUN_ON(signaling_thread());
  742. void OnTransportControllerGatheringState(cricket::IceGatheringState state)
  743. RTC_RUN_ON(signaling_thread());
  744. void OnTransportControllerCandidatesGathered(
  745. const std::string& transport_name,
  746. const std::vector<cricket::Candidate>& candidates)
  747. RTC_RUN_ON(signaling_thread());
  748. void OnTransportControllerCandidateError(
  749. const cricket::IceCandidateErrorEvent& event)
  750. RTC_RUN_ON(signaling_thread());
  751. void OnTransportControllerCandidatesRemoved(
  752. const std::vector<cricket::Candidate>& candidates)
  753. RTC_RUN_ON(signaling_thread());
  754. void OnTransportControllerCandidateChanged(
  755. const cricket::CandidatePairChangeEvent& event)
  756. RTC_RUN_ON(signaling_thread());
  757. void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
  758. const char* SessionErrorToString(SessionError error) const;
  759. std::string GetSessionErrorMsg();
  760. // Report the UMA metric SdpFormatReceived for the given remote offer.
  761. void ReportSdpFormatReceived(const SessionDescriptionInterface& remote_offer);
  762. // Report inferred negotiated SDP semantics from a local/remote answer to the
  763. // UMA observer.
  764. void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
  765. // Invoked when TransportController connection completion is signaled.
  766. // Reports stats for all transports in use.
  767. void ReportTransportStats() RTC_RUN_ON(signaling_thread());
  768. // Gather the usage of IPv4/IPv6 as best connection.
  769. void ReportBestConnectionState(const cricket::TransportStats& stats);
  770. void ReportNegotiatedCiphers(const cricket::TransportStats& stats,
  771. const std::set<cricket::MediaType>& media_types)
  772. RTC_RUN_ON(signaling_thread());
  773. void ReportIceCandidateCollected(const cricket::Candidate& candidate)
  774. RTC_RUN_ON(signaling_thread());
  775. void ReportRemoteIceCandidateAdded(const cricket::Candidate& candidate)
  776. RTC_RUN_ON(signaling_thread());
  777. void NoteUsageEvent(UsageEvent event);
  778. void ReportUsagePattern() const RTC_RUN_ON(signaling_thread());
  779. void OnSentPacket_w(const rtc::SentPacket& sent_packet);
  780. const std::string GetTransportName(const std::string& content_name)
  781. RTC_RUN_ON(signaling_thread());
  782. // Functions for dealing with transports.
  783. // Note that cricket code uses the term "channel" for what other code
  784. // refers to as "transport".
  785. // Destroys and clears the BaseChannel associated with the given transceiver,
  786. // if such channel is set.
  787. void DestroyTransceiverChannel(
  788. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  789. transceiver);
  790. // Destroys the RTP data channel transport and/or the SCTP data channel
  791. // transport and clears it.
  792. void DestroyDataChannelTransport();
  793. // Destroys the given ChannelInterface.
  794. // The channel cannot be accessed after this method is called.
  795. void DestroyChannelInterface(cricket::ChannelInterface* channel);
  796. // JsepTransportController::Observer override.
  797. //
  798. // Called by |transport_controller_| when processing transport information
  799. // from a session description, and the mapping from m= sections to transports
  800. // changed (as a result of BUNDLE negotiation, or m= sections being
  801. // rejected).
  802. bool OnTransportChanged(
  803. const std::string& mid,
  804. RtpTransportInternal* rtp_transport,
  805. rtc::scoped_refptr<DtlsTransport> dtls_transport,
  806. DataChannelTransportInterface* data_channel_transport) override;
  807. // RtpSenderBase::SetStreamsObserver override.
  808. void OnSetStreams() override;
  809. // Returns the CryptoOptions for this PeerConnection. This will always
  810. // return the RTCConfiguration.crypto_options if set and will only default
  811. // back to the PeerConnectionFactory settings if nothing was set.
  812. CryptoOptions GetCryptoOptions();
  813. // Returns rtp transport, result can not be nullptr.
  814. RtpTransportInternal* GetRtpTransport(const std::string& mid)
  815. RTC_RUN_ON(signaling_thread()) {
  816. auto rtp_transport = transport_controller_->GetRtpTransport(mid);
  817. RTC_DCHECK(rtp_transport);
  818. return rtp_transport;
  819. }
  820. std::function<void(const rtc::CopyOnWriteBuffer& packet,
  821. int64_t packet_time_us)>
  822. InitializeRtcpCallback();
  823. // Storing the factory as a scoped reference pointer ensures that the memory
  824. // in the PeerConnectionFactoryImpl remains available as long as the
  825. // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
  826. // However, since the reference counting is done in the
  827. // PeerConnectionFactoryInterface all instances created using the raw pointer
  828. // will refer to the same reference count.
  829. const rtc::scoped_refptr<PeerConnectionFactory> factory_;
  830. PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
  831. nullptr;
  832. // The EventLog needs to outlive |call_| (and any other object that uses it).
  833. std::unique_ptr<RtcEventLog> event_log_ RTC_GUARDED_BY(worker_thread());
  834. // Points to the same thing as `event_log_`. Since it's const, we may read the
  835. // pointer (but not touch the object) from any thread.
  836. RtcEventLog* const event_log_ptr_ RTC_PT_GUARDED_BY(worker_thread());
  837. IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) =
  838. kIceConnectionNew;
  839. PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_
  840. RTC_GUARDED_BY(signaling_thread()) = kIceConnectionNew;
  841. PeerConnectionInterface::PeerConnectionState connection_state_
  842. RTC_GUARDED_BY(signaling_thread()) = PeerConnectionState::kNew;
  843. IceGatheringState ice_gathering_state_ RTC_GUARDED_BY(signaling_thread()) =
  844. kIceGatheringNew;
  845. PeerConnectionInterface::RTCConfiguration configuration_
  846. RTC_GUARDED_BY(signaling_thread());
  847. // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it
  848. // is not injected. It should be required once chromium supplies it.
  849. std::unique_ptr<AsyncResolverFactory> async_resolver_factory_
  850. RTC_GUARDED_BY(signaling_thread());
  851. std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_;
  852. std::unique_ptr<cricket::PortAllocator>
  853. port_allocator_; // TODO(bugs.webrtc.org/9987): Accessed on both
  854. // signaling and network thread.
  855. std::unique_ptr<webrtc::IceTransportFactory>
  856. ice_transport_factory_; // TODO(bugs.webrtc.org/9987): Accessed on the
  857. // signaling thread but the underlying raw
  858. // pointer is given to
  859. // |jsep_transport_controller_| and used on the
  860. // network thread.
  861. std::unique_ptr<rtc::SSLCertificateVerifier>
  862. tls_cert_verifier_; // TODO(bugs.webrtc.org/9987): Accessed on both
  863. // signaling and network thread.
  864. // One PeerConnection has only one RTCP CNAME.
  865. // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
  866. const std::string rtcp_cname_;
  867. // Streams added via AddStream.
  868. const rtc::scoped_refptr<StreamCollection> local_streams_
  869. RTC_GUARDED_BY(signaling_thread());
  870. // Streams created as a result of SetRemoteDescription.
  871. const rtc::scoped_refptr<StreamCollection> remote_streams_
  872. RTC_GUARDED_BY(signaling_thread());
  873. std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
  874. RTC_GUARDED_BY(signaling_thread());
  875. // These lists store sender info seen in local/remote descriptions.
  876. std::vector<RtpSenderInfo> remote_audio_sender_infos_
  877. RTC_GUARDED_BY(signaling_thread());
  878. std::vector<RtpSenderInfo> remote_video_sender_infos_
  879. RTC_GUARDED_BY(signaling_thread());
  880. std::vector<RtpSenderInfo> local_audio_sender_infos_
  881. RTC_GUARDED_BY(signaling_thread());
  882. std::vector<RtpSenderInfo> local_video_sender_infos_
  883. RTC_GUARDED_BY(signaling_thread());
  884. // The unique_ptr belongs to the worker thread, but the Call object manages
  885. // its own thread safety.
  886. std::unique_ptr<Call> call_ RTC_GUARDED_BY(worker_thread());
  887. std::unique_ptr<ScopedTaskSafety> call_safety_
  888. RTC_GUARDED_BY(worker_thread());
  889. // Points to the same thing as `call_`. Since it's const, we may read the
  890. // pointer from any thread.
  891. // TODO(bugs.webrtc.org/11992): Remove this workaround (and potential dangling
  892. // pointer).
  893. Call* const call_ptr_;
  894. std::unique_ptr<StatsCollector> stats_
  895. RTC_GUARDED_BY(signaling_thread()); // A pointer is passed to senders_
  896. rtc::scoped_refptr<RTCStatsCollector> stats_collector_
  897. RTC_GUARDED_BY(signaling_thread());
  898. TransceiverList transceivers_;
  899. // MIDs will be generated using this generator which will keep track of
  900. // all the MIDs that have been seen over the life of the PeerConnection.
  901. rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
  902. SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
  903. SessionError::kNone;
  904. std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
  905. std::string session_id_ RTC_GUARDED_BY(signaling_thread());
  906. std::unique_ptr<JsepTransportController>
  907. transport_controller_; // TODO(bugs.webrtc.org/9987): Accessed on both
  908. // signaling and network thread.
  909. // |sctp_mid_| is the content name (MID) in SDP.
  910. // Note: this is used as the data channel MID by both SCTP and data channel
  911. // transports. It is set when either transport is initialized and unset when
  912. // both transports are deleted.
  913. // There is one copy on the signaling thread and another copy on the
  914. // networking thread. Changes are always initiated from the signaling
  915. // thread, but applied first on the networking thread via an invoke().
  916. absl::optional<std::string> sctp_mid_s_ RTC_GUARDED_BY(signaling_thread());
  917. absl::optional<std::string> sctp_mid_n_ RTC_GUARDED_BY(network_thread());
  918. // The machinery for handling offers and answers.
  919. SdpOfferAnswerHandler sdp_handler_ RTC_GUARDED_BY(signaling_thread());
  920. bool dtls_enabled_ RTC_GUARDED_BY(signaling_thread()) = false;
  921. // Member variables for caching global options.
  922. cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
  923. cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
  924. int usage_event_accumulator_ RTC_GUARDED_BY(signaling_thread()) = 0;
  925. bool return_histogram_very_quickly_ RTC_GUARDED_BY(signaling_thread()) =
  926. false;
  927. // This object should be used to generate any SSRC that is not explicitly
  928. // specified by the user (or by the remote party).
  929. // The generator is not used directly, instead it is passed on to the
  930. // channel manager and the session description factory.
  931. rtc::UniqueRandomIdGenerator ssrc_generator_
  932. RTC_GUARDED_BY(signaling_thread());
  933. // A video bitrate allocator factory.
  934. // This can injected using the PeerConnectionDependencies,
  935. // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
  936. // Note that one can still choose to override this in a MediaEngine
  937. // if one wants too.
  938. std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
  939. video_bitrate_allocator_factory_;
  940. DataChannelController data_channel_controller_;
  941. };
  942. } // namespace webrtc
  943. #endif // PC_PEER_CONNECTION_H_