Skip to content

Commit

Permalink
[M114] Move transceiver iteration loop over to the signaling thread.
Browse files Browse the repository at this point in the history
This is required for ReportTransportStats since iterating over the
transceiver list from the network thread is not safe.

(cherry picked from commit dba22d3)

No-Try: true
Bug: chromium:1446274, webrtc:12692
Change-Id: I7c514df9f029112c4b1da85826af91217850fb26
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/307340
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Original-Commit-Position: refs/heads/main@{#40197}
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/308001
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/branch-heads/5735@{#3}
Cr-Branched-From: df7df19-refs/heads/main@{#39949}
  • Loading branch information
Tommi authored and cloudwebrtc committed Jul 12, 2023
1 parent 6cba6f0 commit abfb833
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
35 changes: 22 additions & 13 deletions pc/peer_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -735,9 +735,6 @@ JsepTransportController* PeerConnection::InitializeTransportController_n(
transport_controller_->SubscribeIceConnectionState(
[this](cricket::IceConnectionState s) {
RTC_DCHECK_RUN_ON(network_thread());
if (s == cricket::kIceConnectionConnected) {
ReportTransportStats();
}
signaling_thread()->PostTask(
SafeTask(signaling_thread_safety_.flag(), [this, s]() {
RTC_DCHECK_RUN_ON(signaling_thread());
Expand Down Expand Up @@ -2401,6 +2398,20 @@ void PeerConnection::OnTransportControllerConnectionState(
case cricket::kIceConnectionConnected:
RTC_LOG(LS_INFO) << "Changing to ICE connected state because "
"all transports are writable.";
{
std::vector<RtpTransceiverProxyRefPtr> transceivers;
if (ConfiguredForMedia()) {
transceivers = rtp_manager()->transceivers()->List();
}

network_thread()->PostTask(
SafeTask(network_thread_safety_,
[this, transceivers = std::move(transceivers)] {
RTC_DCHECK_RUN_ON(network_thread());
ReportTransportStats(std::move(transceivers));
}));
}

SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED);
break;
Expand Down Expand Up @@ -2743,20 +2754,18 @@ void PeerConnection::OnTransportControllerGatheringState(
}

// Runs on network_thread().
void PeerConnection::ReportTransportStats() {
void PeerConnection::ReportTransportStats(
std::vector<RtpTransceiverProxyRefPtr> transceivers) {
TRACE_EVENT0("webrtc", "PeerConnection::ReportTransportStats");
rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
std::map<std::string, std::set<cricket::MediaType>>
media_types_by_transport_name;
if (ConfiguredForMedia()) {
for (const auto& transceiver :
rtp_manager()->transceivers()->UnsafeList()) {
if (transceiver->internal()->channel()) {
std::string transport_name(
transceiver->internal()->channel()->transport_name());
media_types_by_transport_name[transport_name].insert(
transceiver->media_type());
}
for (const auto& transceiver : transceivers) {
if (transceiver->internal()->channel()) {
std::string transport_name(
transceiver->internal()->channel()->transport_name());
media_types_by_transport_name[transport_name].insert(
transceiver->media_type());
}
}

Expand Down
3 changes: 2 additions & 1 deletion pc/peer_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@ class PeerConnection : public PeerConnectionInternal,

// Invoked when TransportController connection completion is signaled.
// Reports stats for all transports in use.
void ReportTransportStats() RTC_RUN_ON(network_thread());
void ReportTransportStats(std::vector<RtpTransceiverProxyRefPtr> transceivers)
RTC_RUN_ON(network_thread());

// Gather the usage of IPv4/IPv6 as best connection.
static void ReportBestConnectionState(const cricket::TransportStats& stats);
Expand Down
8 changes: 8 additions & 0 deletions pc/peer_connection_integrationtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,10 @@ TEST_P(PeerConnectionIntegrationTest,
EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
callee()->ice_connection_state(), kDefaultTimeout);

// Part of reporting the stats will occur on the network thread, so flush it
// before checking NumEvents.
SendTask(network_thread(), [] {});

EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
"WebRTC.PeerConnection.CandidatePairType_UDP",
webrtc::kIceCandidatePairHostNameHostName));
Expand Down Expand Up @@ -1959,6 +1963,10 @@ TEST_P(PeerConnectionIntegrationIceStatesTest, MAYBE_VerifyBestConnection) {
EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
callee()->ice_connection_state(), kDefaultTimeout);

// Part of reporting the stats will occur on the network thread, so flush it
// before checking NumEvents.
SendTask(network_thread(), [] {});

// TODO(bugs.webrtc.org/9456): Fix it.
const int num_best_ipv4 = webrtc::metrics::NumEvents(
"WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
Expand Down

0 comments on commit abfb833

Please sign in to comment.