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