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