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