From 2001dc39db245973f757e1d5b389cf7ef9afe93e Mon Sep 17 00:00:00 2001 From: Tomas Gunnarsson Date: Tue, 6 Apr 2021 11:36:00 +0200 Subject: [PATCH] Remove unnecessary thread hop for reporting transport stats Bug: webrtc:12637 Change-Id: If00df716d30ac1db5faa83d2859f7c9787ad0ae9 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/213662 Commit-Queue: Tommi Reviewed-by: Harald Alvestrand Cr-Commit-Position: refs/heads/master@{#33637} --- pc/peer_connection.cc | 42 ++++++++++++++++++++++-------------------- pc/peer_connection.h | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc index 54713fef76..84803dafdb 100644 --- a/pc/peer_connection.cc +++ b/pc/peer_connection.cc @@ -675,6 +675,9 @@ void PeerConnection::InitializeTransportController_n( transport_controller_->SubscribeIceConnectionState( [this](cricket::IceConnectionState s) { RTC_DCHECK_RUN_ON(network_thread()); + if (s == cricket::kIceConnectionConnected) { + ReportTransportStats(); + } signaling_thread()->PostTask( ToQueuedTask(signaling_thread_safety_.flag(), [this, s]() { RTC_DCHECK_RUN_ON(signaling_thread()); @@ -2277,8 +2280,8 @@ void PeerConnection::OnTransportControllerConnectionState( SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); } SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); + NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); - ReportTransportStats(); break; default: RTC_NOTREACHED(); @@ -2638,6 +2641,7 @@ void PeerConnection::OnTransportControllerGatheringState( } } +// Runs on network_thread(). void PeerConnection::ReportTransportStats() { rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; std::map> @@ -2650,33 +2654,31 @@ void PeerConnection::ReportTransportStats() { transceiver->media_type()); } } + if (rtp_data_channel()) { media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( cricket::MEDIA_TYPE_DATA); } - absl::optional transport_name = sctp_transport_name(); - if (transport_name) { - media_types_by_transport_name[*transport_name].insert( - cricket::MEDIA_TYPE_DATA); + if (sctp_mid_n_) { + auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_n_); + if (dtls_transport) { + media_types_by_transport_name[dtls_transport->transport_name()].insert( + cricket::MEDIA_TYPE_DATA); + } } - // Run the loop that reports the state on the network thread since the - // transport controller requires the stats to be read there (GetStats()). - network_thread()->PostTask(ToQueuedTask( - network_thread_safety_, [this, media_types_by_transport_name = std::move( - media_types_by_transport_name)] { - for (const auto& entry : media_types_by_transport_name) { - const std::string& transport_name = entry.first; - const std::set media_types = entry.second; - cricket::TransportStats stats; - if (transport_controller_->GetStats(transport_name, &stats)) { - ReportBestConnectionState(stats); - ReportNegotiatedCiphers(dtls_enabled_, stats, media_types); - } - } - })); + for (const auto& entry : media_types_by_transport_name) { + const std::string& transport_name = entry.first; + const std::set media_types = entry.second; + cricket::TransportStats stats; + if (transport_controller_->GetStats(transport_name, &stats)) { + ReportBestConnectionState(stats); + ReportNegotiatedCiphers(dtls_enabled_, stats, media_types); + } + } } + // Walk through the ConnectionInfos to gather best connection usage // for IPv4 and IPv6. // static (no member state required) diff --git a/pc/peer_connection.h b/pc/peer_connection.h index 34234244e8..bd2e80a47b 100644 --- a/pc/peer_connection.h +++ b/pc/peer_connection.h @@ -577,7 +577,7 @@ class PeerConnection : public PeerConnectionInternal, // Invoked when TransportController connection completion is signaled. // Reports stats for all transports in use. - void ReportTransportStats() RTC_RUN_ON(signaling_thread()); + void ReportTransportStats() RTC_RUN_ON(network_thread()); // Gather the usage of IPv4/IPv6 as best connection. static void ReportBestConnectionState(const cricket::TransportStats& stats);