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 11 #ifndef PC_PEER_CONNECTION_H_ 12 #define PC_PEER_CONNECTION_H_ 13 14 #include <stdint.h> 15 16 #include <functional> 17 #include <map> 18 #include <memory> 19 #include <set> 20 #include <string> 21 #include <utility> 22 #include <vector> 23 24 #include "absl/types/optional.h" 25 #include "api/adaptation/resource.h" 26 #include "api/async_resolver_factory.h" 27 #include "api/audio_options.h" 28 #include "api/candidate.h" 29 #include "api/crypto/crypto_options.h" 30 #include "api/data_channel_interface.h" 31 #include "api/dtls_transport_interface.h" 32 #include "api/ice_transport_interface.h" 33 #include "api/jsep.h" 34 #include "api/media_stream_interface.h" 35 #include "api/media_types.h" 36 #include "api/packet_socket_factory.h" 37 #include "api/peer_connection_interface.h" 38 #include "api/rtc_error.h" 39 #include "api/rtc_event_log/rtc_event_log.h" 40 #include "api/rtc_event_log_output.h" 41 #include "api/rtp_parameters.h" 42 #include "api/rtp_receiver_interface.h" 43 #include "api/rtp_sender_interface.h" 44 #include "api/rtp_transceiver_interface.h" 45 #include "api/scoped_refptr.h" 46 #include "api/sctp_transport_interface.h" 47 #include "api/sequence_checker.h" 48 #include "api/set_local_description_observer_interface.h" 49 #include "api/set_remote_description_observer_interface.h" 50 #include "api/stats/rtc_stats_collector_callback.h" 51 #include "api/transport/bitrate_settings.h" 52 #include "api/transport/data_channel_transport_interface.h" 53 #include "api/transport/enums.h" 54 #include "api/turn_customizer.h" 55 #include "api/video/video_bitrate_allocator_factory.h" 56 #include "call/call.h" 57 #include "media/base/media_channel.h" 58 #include "media/base/media_engine.h" 59 #include "p2p/base/ice_transport_internal.h" 60 #include "p2p/base/port.h" 61 #include "p2p/base/port_allocator.h" 62 #include "p2p/base/transport_description.h" 63 #include "pc/channel.h" 64 #include "pc/channel_interface.h" 65 #include "pc/channel_manager.h" 66 #include "pc/connection_context.h" 67 #include "pc/data_channel_controller.h" 68 #include "pc/data_channel_utils.h" 69 #include "pc/dtls_transport.h" 70 #include "pc/jsep_transport_controller.h" 71 #include "pc/peer_connection_internal.h" 72 #include "pc/peer_connection_message_handler.h" 73 #include "pc/rtc_stats_collector.h" 74 #include "pc/rtp_data_channel.h" 75 #include "pc/rtp_receiver.h" 76 #include "pc/rtp_sender.h" 77 #include "pc/rtp_transceiver.h" 78 #include "pc/rtp_transmission_manager.h" 79 #include "pc/rtp_transport_internal.h" 80 #include "pc/sctp_data_channel.h" 81 #include "pc/sctp_transport.h" 82 #include "pc/sdp_offer_answer.h" 83 #include "pc/session_description.h" 84 #include "pc/stats_collector.h" 85 #include "pc/stream_collection.h" 86 #include "pc/transceiver_list.h" 87 #include "pc/transport_stats.h" 88 #include "pc/usage_pattern.h" 89 #include "rtc_base/checks.h" 90 #include "rtc_base/copy_on_write_buffer.h" 91 #include "rtc_base/network/sent_packet.h" 92 #include "rtc_base/rtc_certificate.h" 93 #include "rtc_base/ssl_certificate.h" 94 #include "rtc_base/ssl_stream_adapter.h" 95 #include "rtc_base/task_utils/pending_task_safety_flag.h" 96 #include "rtc_base/third_party/sigslot/sigslot.h" 97 #include "rtc_base/thread.h" 98 #include "rtc_base/thread_annotations.h" 99 #include "rtc_base/unique_id_generator.h" 100 101 namespace webrtc { 102 103 // PeerConnection is the implementation of the PeerConnection object as defined 104 // by the PeerConnectionInterface API surface. 105 // The class currently is solely responsible for the following: 106 // - Managing the session state machine (signaling state). 107 // - Creating and initializing lower-level objects, like PortAllocator and 108 // BaseChannels. 109 // - Owning and managing the life cycle of the RtpSender/RtpReceiver and track 110 // objects. 111 // - Tracking the current and pending local/remote session descriptions. 112 // The class currently is jointly responsible for the following: 113 // - Parsing and interpreting SDP. 114 // - Generating offers and answers based on the current state. 115 // - The ICE state machine. 116 // - Generating stats. 117 class PeerConnection : public PeerConnectionInternal, 118 public JsepTransportController::Observer, 119 public sigslot::has_slots<> { 120 public: 121 // Creates a PeerConnection and initializes it with the given values. 122 // If the initialization fails, the function releases the PeerConnection 123 // and returns nullptr. 124 // 125 // Note that the function takes ownership of dependencies, and will 126 // either use them or release them, whether it succeeds or fails. 127 static RTCErrorOr<rtc::scoped_refptr<PeerConnection>> Create( 128 rtc::scoped_refptr<ConnectionContext> context, 129 const PeerConnectionFactoryInterface::Options& options, 130 std::unique_ptr<RtcEventLog> event_log, 131 std::unique_ptr<Call> call, 132 const PeerConnectionInterface::RTCConfiguration& configuration, 133 PeerConnectionDependencies dependencies); 134 135 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override; 136 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override; 137 bool AddStream(MediaStreamInterface* local_stream) override; 138 void RemoveStream(MediaStreamInterface* local_stream) override; 139 140 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack( 141 rtc::scoped_refptr<MediaStreamTrackInterface> track, 142 const std::vector<std::string>& stream_ids) override; 143 bool RemoveTrack(RtpSenderInterface* sender) override; 144 RTCError RemoveTrackNew( 145 rtc::scoped_refptr<RtpSenderInterface> sender) override; 146 147 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver( 148 rtc::scoped_refptr<MediaStreamTrackInterface> track) override; 149 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver( 150 rtc::scoped_refptr<MediaStreamTrackInterface> track, 151 const RtpTransceiverInit& init) override; 152 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver( 153 cricket::MediaType media_type) override; 154 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver( 155 cricket::MediaType media_type, 156 const RtpTransceiverInit& init) override; 157 158 rtc::scoped_refptr<RtpSenderInterface> CreateSender( 159 const std::string& kind, 160 const std::string& stream_id) override; 161 162 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders() 163 const override; 164 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers() 165 const override; 166 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> GetTransceivers() 167 const override; 168 169 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( 170 const std::string& label, 171 const DataChannelInit* config) override; 172 // WARNING: LEGACY. See peerconnectioninterface.h 173 bool GetStats(StatsObserver* observer, 174 webrtc::MediaStreamTrackInterface* track, 175 StatsOutputLevel level) override; 176 // Spec-complaint GetStats(). See peerconnectioninterface.h 177 void GetStats(RTCStatsCollectorCallback* callback) override; 178 void GetStats( 179 rtc::scoped_refptr<RtpSenderInterface> selector, 180 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override; 181 void GetStats( 182 rtc::scoped_refptr<RtpReceiverInterface> selector, 183 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) override; 184 void ClearStatsCache() override; 185 186 SignalingState signaling_state() override; 187 188 IceConnectionState ice_connection_state() override; 189 IceConnectionState standardized_ice_connection_state() override; 190 PeerConnectionState peer_connection_state() override; 191 IceGatheringState ice_gathering_state() override; 192 absl::optional<bool> can_trickle_ice_candidates() override; 193 194 const SessionDescriptionInterface* local_description() const override; 195 const SessionDescriptionInterface* remote_description() const override; 196 const SessionDescriptionInterface* current_local_description() const override; 197 const SessionDescriptionInterface* current_remote_description() 198 const override; 199 const SessionDescriptionInterface* pending_local_description() const override; 200 const SessionDescriptionInterface* pending_remote_description() 201 const override; 202 203 void RestartIce() override; 204 205 // JSEP01 206 void CreateOffer(CreateSessionDescriptionObserver* observer, 207 const RTCOfferAnswerOptions& options) override; 208 void CreateAnswer(CreateSessionDescriptionObserver* observer, 209 const RTCOfferAnswerOptions& options) override; 210 211 void SetLocalDescription( 212 std::unique_ptr<SessionDescriptionInterface> desc, 213 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) 214 override; 215 void SetLocalDescription( 216 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) 217 override; 218 // TODO(https://crbug.com/webrtc/11798): Delete these methods in favor of the 219 // ones taking SetLocalDescriptionObserverInterface as argument. 220 void SetLocalDescription(SetSessionDescriptionObserver* observer, 221 SessionDescriptionInterface* desc) override; 222 void SetLocalDescription(SetSessionDescriptionObserver* observer) override; 223 224 void SetRemoteDescription( 225 std::unique_ptr<SessionDescriptionInterface> desc, 226 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) 227 override; 228 // TODO(https://crbug.com/webrtc/11798): Delete this methods in favor of the 229 // ones taking SetRemoteDescriptionObserverInterface as argument. 230 void SetRemoteDescription(SetSessionDescriptionObserver* observer, 231 SessionDescriptionInterface* desc) override; 232 233 PeerConnectionInterface::RTCConfiguration GetConfiguration() override; 234 RTCError SetConfiguration( 235 const PeerConnectionInterface::RTCConfiguration& configuration) override; 236 bool AddIceCandidate(const IceCandidateInterface* candidate) override; 237 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate, 238 std::function<void(RTCError)> callback) override; 239 bool RemoveIceCandidates( 240 const std::vector<cricket::Candidate>& candidates) override; 241 242 RTCError SetBitrate(const BitrateSettings& bitrate) override; 243 244 void SetAudioPlayout(bool playout) override; 245 void SetAudioRecording(bool recording) override; 246 247 rtc::scoped_refptr<DtlsTransportInterface> LookupDtlsTransportByMid( 248 const std::string& mid) override; 249 rtc::scoped_refptr<DtlsTransport> LookupDtlsTransportByMidInternal( 250 const std::string& mid); 251 252 rtc::scoped_refptr<SctpTransportInterface> GetSctpTransport() const override; 253 254 void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override; 255 256 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, 257 int64_t output_period_ms) override; 258 bool StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output) override; 259 void StopRtcEventLog() override; 260 261 void Close() override; 262 signaling_thread()263 rtc::Thread* signaling_thread() const final { 264 return context_->signaling_thread(); 265 } 266 267 // PeerConnectionInternal implementation. network_thread()268 rtc::Thread* network_thread() const final { 269 return context_->network_thread(); 270 } worker_thread()271 rtc::Thread* worker_thread() const final { return context_->worker_thread(); } 272 session_id()273 std::string session_id() const override { 274 return session_id_; 275 } 276 initial_offerer()277 bool initial_offerer() const override { 278 RTC_DCHECK_RUN_ON(signaling_thread()); 279 return transport_controller_ && transport_controller_->initial_offerer(); 280 } 281 282 std::vector< 283 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> GetTransceiversInternal()284 GetTransceiversInternal() const override { 285 RTC_DCHECK_RUN_ON(signaling_thread()); 286 return rtp_manager()->transceivers()->List(); 287 } 288 SignalRtpDataChannelCreated()289 sigslot::signal1<RtpDataChannel*>& SignalRtpDataChannelCreated() override { 290 return data_channel_controller_.SignalRtpDataChannelCreated(); 291 } 292 SignalSctpDataChannelCreated()293 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() override { 294 return data_channel_controller_.SignalSctpDataChannelCreated(); 295 } 296 rtp_data_channel()297 cricket::RtpDataChannel* rtp_data_channel() const override { 298 return data_channel_controller_.rtp_data_channel(); 299 } 300 301 std::vector<DataChannelStats> GetDataChannelStats() const override; 302 303 absl::optional<std::string> sctp_transport_name() const override; 304 305 cricket::CandidateStatsList GetPooledCandidateStats() const override; 306 std::map<std::string, std::string> GetTransportNamesByMid() const override; 307 std::map<std::string, cricket::TransportStats> GetTransportStatsByNames( 308 const std::set<std::string>& transport_names) override; 309 Call::Stats GetCallStats() override; 310 311 bool GetLocalCertificate( 312 const std::string& transport_name, 313 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override; 314 std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain( 315 const std::string& transport_name) override; 316 bool IceRestartPending(const std::string& content_name) const override; 317 bool NeedsIceRestart(const std::string& content_name) const override; 318 bool GetSslRole(const std::string& content_name, rtc::SSLRole* role) override; 319 320 // Functions needed by DataChannelController NoteDataAddedEvent()321 void NoteDataAddedEvent() { NoteUsageEvent(UsageEvent::DATA_ADDED); } 322 // Returns the observer. Will crash on CHECK if the observer is removed. 323 PeerConnectionObserver* Observer() const; IsClosed()324 bool IsClosed() const { 325 RTC_DCHECK_RUN_ON(signaling_thread()); 326 return !sdp_handler_ || 327 sdp_handler_->signaling_state() == PeerConnectionInterface::kClosed; 328 } 329 // Get current SSL role used by SCTP's underlying transport. 330 bool GetSctpSslRole(rtc::SSLRole* role); 331 // Handler for the "channel closed" signal 332 void OnSctpDataChannelClosed(DataChannelInterface* channel); 333 334 bool ShouldFireNegotiationNeededEvent(uint32_t event_id) override; 335 336 // Functions needed by SdpOfferAnswerHandler stats()337 StatsCollector* stats() { 338 RTC_DCHECK_RUN_ON(signaling_thread()); 339 return stats_.get(); 340 } data_channel_controller()341 DataChannelController* data_channel_controller() { 342 RTC_DCHECK_RUN_ON(signaling_thread()); 343 return &data_channel_controller_; 344 } dtls_enabled()345 bool dtls_enabled() const { 346 RTC_DCHECK_RUN_ON(signaling_thread()); 347 return dtls_enabled_; 348 } configuration()349 const PeerConnectionInterface::RTCConfiguration* configuration() const { 350 RTC_DCHECK_RUN_ON(signaling_thread()); 351 return &configuration_; 352 } sctp_mid()353 absl::optional<std::string> sctp_mid() { 354 RTC_DCHECK_RUN_ON(signaling_thread()); 355 return sctp_mid_s_; 356 } message_handler()357 PeerConnectionMessageHandler* message_handler() { 358 RTC_DCHECK_RUN_ON(signaling_thread()); 359 return &message_handler_; 360 } 361 rtp_manager()362 RtpTransmissionManager* rtp_manager() { return rtp_manager_.get(); } rtp_manager()363 const RtpTransmissionManager* rtp_manager() const { 364 return rtp_manager_.get(); 365 } 366 cricket::ChannelManager* channel_manager() const; 367 transport_controller()368 JsepTransportController* transport_controller() { 369 return transport_controller_.get(); 370 } port_allocator()371 cricket::PortAllocator* port_allocator() { return port_allocator_.get(); } call_ptr()372 Call* call_ptr() { return call_ptr_; } 373 context()374 ConnectionContext* context() { return context_.get(); } options()375 const PeerConnectionFactoryInterface::Options* options() const { 376 return &options_; 377 } 378 cricket::DataChannelType data_channel_type() const; 379 void SetIceConnectionState(IceConnectionState new_state); 380 void NoteUsageEvent(UsageEvent event); 381 382 // Asynchronously adds a remote candidate on the network thread. 383 void AddRemoteCandidate(const std::string& mid, 384 const cricket::Candidate& candidate); 385 386 // Report the UMA metric SdpFormatReceived for the given remote description. 387 void ReportSdpFormatReceived( 388 const SessionDescriptionInterface& remote_description); 389 390 // Report the UMA metric BundleUsage for the given remote description. 391 void ReportSdpBundleUsage( 392 const SessionDescriptionInterface& remote_description); 393 394 // Returns true if the PeerConnection is configured to use Unified Plan 395 // semantics for creating offers/answers and setting local/remote 396 // descriptions. If this is true the RtpTransceiver API will also be available 397 // to the user. If this is false, Plan B semantics are assumed. 398 // TODO(bugs.webrtc.org/8530): Flip the default to be Unified Plan once 399 // sufficient time has passed. IsUnifiedPlan()400 bool IsUnifiedPlan() const { 401 RTC_DCHECK_RUN_ON(signaling_thread()); 402 return is_unified_plan_; 403 } 404 bool ValidateBundleSettings(const cricket::SessionDescription* desc); 405 406 // Returns the MID for the data section associated with either the 407 // RtpDataChannel or SCTP data channel, if it has been set. If no data 408 // channels are configured this will return nullopt. 409 absl::optional<std::string> GetDataMid() const; 410 411 void SetSctpDataMid(const std::string& mid); 412 413 void ResetSctpDataMid(); 414 415 // Asynchronously calls SctpTransport::Start() on the network thread for 416 // |sctp_mid()| if set. Called as part of setting the local description. 417 void StartSctpTransport(int local_port, 418 int remote_port, 419 int max_message_size); 420 421 // Returns the CryptoOptions for this PeerConnection. This will always 422 // return the RTCConfiguration.crypto_options if set and will only default 423 // back to the PeerConnectionFactory settings if nothing was set. 424 CryptoOptions GetCryptoOptions(); 425 426 // Internal implementation for AddTransceiver family of methods. If 427 // |fire_callback| is set, fires OnRenegotiationNeeded callback if successful. 428 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> AddTransceiver( 429 cricket::MediaType media_type, 430 rtc::scoped_refptr<MediaStreamTrackInterface> track, 431 const RtpTransceiverInit& init, 432 bool fire_callback = true); 433 434 // Returns rtp transport, result can not be nullptr. 435 RtpTransportInternal* GetRtpTransport(const std::string& mid); 436 437 // Returns true if SRTP (either using DTLS-SRTP or SDES) is required by 438 // this session. 439 bool SrtpRequired() const; 440 441 void OnSentPacket_w(const rtc::SentPacket& sent_packet); 442 443 bool SetupDataChannelTransport_n(const std::string& mid) 444 RTC_RUN_ON(network_thread()); 445 void SetupRtpDataChannelTransport_n(cricket::RtpDataChannel* data_channel) 446 RTC_RUN_ON(network_thread()); 447 void TeardownDataChannelTransport_n() RTC_RUN_ON(network_thread()); 448 cricket::ChannelInterface* GetChannel(const std::string& content_name); 449 450 // Functions made public for testing. ReturnHistogramVeryQuicklyForTesting()451 void ReturnHistogramVeryQuicklyForTesting() { 452 RTC_DCHECK_RUN_ON(signaling_thread()); 453 return_histogram_very_quickly_ = true; 454 } 455 void RequestUsagePatternReportForTesting(); 456 457 protected: 458 // Available for rtc::scoped_refptr creation 459 PeerConnection(rtc::scoped_refptr<ConnectionContext> context, 460 const PeerConnectionFactoryInterface::Options& options, 461 bool is_unified_plan, 462 std::unique_ptr<RtcEventLog> event_log, 463 std::unique_ptr<Call> call, 464 PeerConnectionDependencies& dependencies, 465 bool dtls_enabled); 466 467 ~PeerConnection() override; 468 469 private: 470 RTCError Initialize( 471 const PeerConnectionInterface::RTCConfiguration& configuration, 472 PeerConnectionDependencies dependencies); 473 void InitializeTransportController_n( 474 const RTCConfiguration& configuration, 475 const PeerConnectionDependencies& dependencies) 476 RTC_RUN_ON(network_thread()); 477 478 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> 479 FindTransceiverBySender(rtc::scoped_refptr<RtpSenderInterface> sender) 480 RTC_RUN_ON(signaling_thread()); 481 482 void SetStandardizedIceConnectionState( 483 PeerConnectionInterface::IceConnectionState new_state) 484 RTC_RUN_ON(signaling_thread()); 485 void SetConnectionState( 486 PeerConnectionInterface::PeerConnectionState new_state) 487 RTC_RUN_ON(signaling_thread()); 488 489 // Called any time the IceGatheringState changes. 490 void OnIceGatheringChange(IceGatheringState new_state) 491 RTC_RUN_ON(signaling_thread()); 492 // New ICE candidate has been gathered. 493 void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate) 494 RTC_RUN_ON(signaling_thread()); 495 // Gathering of an ICE candidate failed. 496 void OnIceCandidateError(const std::string& address, 497 int port, 498 const std::string& url, 499 int error_code, 500 const std::string& error_text) 501 RTC_RUN_ON(signaling_thread()); 502 // Some local ICE candidates have been removed. 503 void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates) 504 RTC_RUN_ON(signaling_thread()); 505 506 void OnSelectedCandidatePairChanged( 507 const cricket::CandidatePairChangeEvent& event) 508 RTC_RUN_ON(signaling_thread()); 509 510 void OnNegotiationNeeded(); 511 512 // Returns the specified SCTP DataChannel in sctp_data_channels_, 513 // or nullptr if not found. 514 SctpDataChannel* FindDataChannelBySid(int sid) const 515 RTC_RUN_ON(signaling_thread()); 516 517 // Called when first configuring the port allocator. 518 struct InitializePortAllocatorResult { 519 bool enable_ipv6; 520 }; 521 InitializePortAllocatorResult InitializePortAllocator_n( 522 const cricket::ServerAddresses& stun_servers, 523 const std::vector<cricket::RelayServerConfig>& turn_servers, 524 const RTCConfiguration& configuration); 525 // Called when SetConfiguration is called to apply the supported subset 526 // of the configuration on the network thread. 527 bool ReconfigurePortAllocator_n( 528 const cricket::ServerAddresses& stun_servers, 529 const std::vector<cricket::RelayServerConfig>& turn_servers, 530 IceTransportsType type, 531 int candidate_pool_size, 532 PortPrunePolicy turn_port_prune_policy, 533 webrtc::TurnCustomizer* turn_customizer, 534 absl::optional<int> stun_candidate_keepalive_interval, 535 bool have_local_description); 536 537 // Starts output of an RTC event log to the given output object. 538 // This function should only be called from the worker thread. 539 bool StartRtcEventLog_w(std::unique_ptr<RtcEventLogOutput> output, 540 int64_t output_period_ms); 541 542 // Stops recording an RTC event log. 543 // This function should only be called from the worker thread. 544 void StopRtcEventLog_w(); 545 546 // Returns true and the TransportInfo of the given |content_name| 547 // from |description|. Returns false if it's not available. 548 static bool GetTransportDescription( 549 const cricket::SessionDescription* description, 550 const std::string& content_name, 551 cricket::TransportDescription* info); 552 553 // Returns the media index for a local ice candidate given the content name. 554 // Returns false if the local session description does not have a media 555 // content called |content_name|. 556 bool GetLocalCandidateMediaIndex(const std::string& content_name, 557 int* sdp_mline_index) 558 RTC_RUN_ON(signaling_thread()); 559 560 // JsepTransportController signal handlers. 561 void OnTransportControllerConnectionState(cricket::IceConnectionState state) 562 RTC_RUN_ON(signaling_thread()); 563 void OnTransportControllerGatheringState(cricket::IceGatheringState state) 564 RTC_RUN_ON(signaling_thread()); 565 void OnTransportControllerCandidatesGathered( 566 const std::string& transport_name, 567 const std::vector<cricket::Candidate>& candidates) 568 RTC_RUN_ON(signaling_thread()); 569 void OnTransportControllerCandidateError( 570 const cricket::IceCandidateErrorEvent& event) 571 RTC_RUN_ON(signaling_thread()); 572 void OnTransportControllerCandidatesRemoved( 573 const std::vector<cricket::Candidate>& candidates) 574 RTC_RUN_ON(signaling_thread()); 575 void OnTransportControllerCandidateChanged( 576 const cricket::CandidatePairChangeEvent& event) 577 RTC_RUN_ON(signaling_thread()); 578 void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error); 579 580 // Invoked when TransportController connection completion is signaled. 581 // Reports stats for all transports in use. 582 void ReportTransportStats() RTC_RUN_ON(network_thread()); 583 584 // Gather the usage of IPv4/IPv6 as best connection. 585 static void ReportBestConnectionState(const cricket::TransportStats& stats); 586 587 static void ReportNegotiatedCiphers( 588 bool dtls_enabled, 589 const cricket::TransportStats& stats, 590 const std::set<cricket::MediaType>& media_types); 591 void ReportIceCandidateCollected(const cricket::Candidate& candidate) 592 RTC_RUN_ON(signaling_thread()); 593 594 void ReportUsagePattern() const RTC_RUN_ON(signaling_thread()); 595 596 void ReportRemoteIceCandidateAdded(const cricket::Candidate& candidate); 597 598 // JsepTransportController::Observer override. 599 // 600 // Called by |transport_controller_| when processing transport information 601 // from a session description, and the mapping from m= sections to transports 602 // changed (as a result of BUNDLE negotiation, or m= sections being 603 // rejected). 604 bool OnTransportChanged( 605 const std::string& mid, 606 RtpTransportInternal* rtp_transport, 607 rtc::scoped_refptr<DtlsTransport> dtls_transport, 608 DataChannelTransportInterface* data_channel_transport) override; 609 610 std::function<void(const rtc::CopyOnWriteBuffer& packet, 611 int64_t packet_time_us)> 612 InitializeRtcpCallback(); 613 614 const rtc::scoped_refptr<ConnectionContext> context_; 615 const PeerConnectionFactoryInterface::Options options_; 616 PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) = 617 nullptr; 618 619 const bool is_unified_plan_; 620 621 // The EventLog needs to outlive |call_| (and any other object that uses it). 622 std::unique_ptr<RtcEventLog> event_log_ RTC_GUARDED_BY(worker_thread()); 623 624 // Points to the same thing as `event_log_`. Since it's const, we may read the 625 // pointer (but not touch the object) from any thread. 626 RtcEventLog* const event_log_ptr_ RTC_PT_GUARDED_BY(worker_thread()); 627 628 IceConnectionState ice_connection_state_ RTC_GUARDED_BY(signaling_thread()) = 629 kIceConnectionNew; 630 PeerConnectionInterface::IceConnectionState standardized_ice_connection_state_ 631 RTC_GUARDED_BY(signaling_thread()) = kIceConnectionNew; 632 PeerConnectionInterface::PeerConnectionState connection_state_ 633 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionState::kNew; 634 635 IceGatheringState ice_gathering_state_ RTC_GUARDED_BY(signaling_thread()) = 636 kIceGatheringNew; 637 PeerConnectionInterface::RTCConfiguration configuration_ 638 RTC_GUARDED_BY(signaling_thread()); 639 640 // TODO(zstein): |async_resolver_factory_| can currently be nullptr if it 641 // is not injected. It should be required once chromium supplies it. 642 // This member variable is only used by JsepTransportController so we should 643 // consider moving ownership to there. 644 const std::unique_ptr<AsyncResolverFactory> async_resolver_factory_; 645 std::unique_ptr<cricket::PortAllocator> 646 port_allocator_; // TODO(bugs.webrtc.org/9987): Accessed on both 647 // signaling and network thread. 648 const std::unique_ptr<webrtc::IceTransportFactory> 649 ice_transport_factory_; // TODO(bugs.webrtc.org/9987): Accessed on the 650 // signaling thread but the underlying raw 651 // pointer is given to 652 // |jsep_transport_controller_| and used on the 653 // network thread. 654 const std::unique_ptr<rtc::SSLCertificateVerifier> tls_cert_verifier_ 655 RTC_GUARDED_BY(network_thread()); 656 657 // The unique_ptr belongs to the worker thread, but the Call object manages 658 // its own thread safety. 659 std::unique_ptr<Call> call_ RTC_GUARDED_BY(worker_thread()); 660 ScopedTaskSafety signaling_thread_safety_; 661 rtc::scoped_refptr<PendingTaskSafetyFlag> network_thread_safety_; 662 rtc::scoped_refptr<PendingTaskSafetyFlag> worker_thread_safety_; 663 664 // Points to the same thing as `call_`. Since it's const, we may read the 665 // pointer from any thread. 666 // TODO(bugs.webrtc.org/11992): Remove this workaround (and potential dangling 667 // pointer). 668 Call* const call_ptr_; 669 670 std::unique_ptr<StatsCollector> stats_ 671 RTC_GUARDED_BY(signaling_thread()); // A pointer is passed to senders_ 672 rtc::scoped_refptr<RTCStatsCollector> stats_collector_ 673 RTC_GUARDED_BY(signaling_thread()); 674 675 const std::string session_id_; 676 677 std::unique_ptr<JsepTransportController> 678 transport_controller_; // TODO(bugs.webrtc.org/9987): Accessed on both 679 // signaling and network thread. 680 681 // |sctp_mid_| is the content name (MID) in SDP. 682 // Note: this is used as the data channel MID by both SCTP and data channel 683 // transports. It is set when either transport is initialized and unset when 684 // both transports are deleted. 685 // There is one copy on the signaling thread and another copy on the 686 // networking thread. Changes are always initiated from the signaling 687 // thread, but applied first on the networking thread via an invoke(). 688 absl::optional<std::string> sctp_mid_s_ RTC_GUARDED_BY(signaling_thread()); 689 absl::optional<std::string> sctp_mid_n_ RTC_GUARDED_BY(network_thread()); 690 std::string sctp_transport_name_s_ RTC_GUARDED_BY(signaling_thread()); 691 692 // The machinery for handling offers and answers. Const after initialization. 693 std::unique_ptr<SdpOfferAnswerHandler> sdp_handler_ 694 RTC_GUARDED_BY(signaling_thread()); 695 696 const bool dtls_enabled_; 697 698 UsagePattern usage_pattern_ RTC_GUARDED_BY(signaling_thread()); 699 bool return_histogram_very_quickly_ RTC_GUARDED_BY(signaling_thread()) = 700 false; 701 702 DataChannelController data_channel_controller_; 703 704 // Machinery for handling messages posted to oneself 705 PeerConnectionMessageHandler message_handler_; 706 707 // Administration of senders, receivers and transceivers 708 // Accessed on both signaling and network thread. Const after Initialize(). 709 std::unique_ptr<RtpTransmissionManager> rtp_manager_; 710 711 rtc::WeakPtrFactory<PeerConnection> weak_factory_; 712 713 // Did the connectionState ever change to `connected`? 714 // Used to gather metrics only the first such state change. 715 bool was_ever_connected_ RTC_GUARDED_BY(signaling_thread()) = false; 716 }; 717 718 } // namespace webrtc 719 720 #endif // PC_PEER_CONNECTION_H_ 721