diff --git a/pc/data_channel_controller.cc b/pc/data_channel_controller.cc index 5ff3b1cc4d..85e43c72a6 100644 --- a/pc/data_channel_controller.cc +++ b/pc/data_channel_controller.cc @@ -100,11 +100,9 @@ void DataChannelController::OnDataReceived( RTC_DCHECK_RUN_ON(signaling_thread()); // TODO(bugs.webrtc.org/11547): The data being received should be // delivered on the network thread. - auto copy = sctp_data_channels_; - for (const auto& channel : copy) { - if (channel->id() == channel_id) - channel->OnDataReceived(type, buffer); - } + auto it = FindChannel(StreamId(channel_id)); + if (it != sctp_data_channels_.end()) + (*it)->OnDataReceived(type, buffer); })); } @@ -114,11 +112,9 @@ void DataChannelController::OnChannelClosing(int channel_id) { SafeTask(signaling_safety_.flag(), [this, channel_id] { RTC_DCHECK_RUN_ON(signaling_thread()); // TODO(bugs.webrtc.org/11547): Should run on the network thread. - auto copy = sctp_data_channels_; - for (const auto& channel : copy) { - if (channel->id() == channel_id) - channel->OnClosingProcedureStartedRemotely(); - } + auto it = FindChannel(StreamId(channel_id)); + if (it != sctp_data_channels_.end()) + (*it)->OnClosingProcedureStartedRemotely(); })); } @@ -127,10 +123,7 @@ void DataChannelController::OnChannelClosed(int channel_id) { signaling_thread()->PostTask( SafeTask(signaling_safety_.flag(), [this, channel_id] { RTC_DCHECK_RUN_ON(signaling_thread()); - auto it = absl::c_find_if(sctp_data_channels_, [&](const auto& c) { - return c->id() == channel_id; - }); - + auto it = FindChannel(StreamId(channel_id)); // Remove the channel from our list, close it and free up resources. if (it != sctp_data_channels_.end()) { rtc::scoped_refptr channel = std::move(*it); @@ -418,6 +411,13 @@ void DataChannelController::NotifyDataChannelsOfTransportCreated() { })); } +std::vector>::iterator +DataChannelController::FindChannel(StreamId stream_id) { + RTC_DCHECK_RUN_ON(signaling_thread()); + return absl::c_find_if(sctp_data_channels_, + [&](const auto& c) { return c->sid() == stream_id; }); +} + rtc::Thread* DataChannelController::network_thread() const { return pc_->network_thread(); } diff --git a/pc/data_channel_controller.h b/pc/data_channel_controller.h index 33c7463731..704dcd39b0 100644 --- a/pc/data_channel_controller.h +++ b/pc/data_channel_controller.h @@ -134,6 +134,9 @@ class DataChannelController : public SctpDataChannelControllerInterface, // (calls OnTransportChannelCreated on the signaling thread). void NotifyDataChannelsOfTransportCreated(); + std::vector>::iterator FindChannel( + StreamId stream_id); + rtc::Thread* network_thread() const; rtc::Thread* signaling_thread() const;