diff --git a/pc/data_channel_controller.cc b/pc/data_channel_controller.cc index 166e181cfe..7fea6c5e55 100644 --- a/pc/data_channel_controller.cc +++ b/pc/data_channel_controller.cc @@ -29,20 +29,13 @@ DataChannelController::~DataChannelController() { } bool DataChannelController::HasDataChannels() const { - auto has_channels = [&] { - RTC_DCHECK_RUN_ON(network_thread()); - return !sctp_data_channels_n_.empty(); - }; - - if (network_thread()->IsCurrent()) - return has_channels(); - - return network_thread()->BlockingCall(std::move(has_channels)); + RTC_DCHECK_RUN_ON(signaling_thread()); + return channel_usage_ == DataChannelUsage::kInUse; } bool DataChannelController::HasUsedDataChannels() const { RTC_DCHECK_RUN_ON(signaling_thread()); - return has_used_data_channels_; + return channel_usage_ != DataChannelUsage::kNeverUsed; } RTCError DataChannelController::SendData( @@ -80,11 +73,16 @@ void DataChannelController::OnChannelStateChanged( if (state == DataChannelInterface::DataState::kClosed) OnSctpDataChannelClosed(channel); - signaling_thread()->PostTask( - SafeTask(signaling_safety_.flag(), - [this, channel_id = channel->internal_id(), state = state] { - pc_->OnSctpDataChannelStateChanged(channel_id, state); - })); + DataChannelUsage channel_usage = sctp_data_channels_n_.empty() + ? DataChannelUsage::kHaveBeenUsed + : DataChannelUsage::kInUse; + signaling_thread()->PostTask(SafeTask( + signaling_safety_.flag(), [this, channel_id = channel->internal_id(), + state = state, channel_usage] { + RTC_DCHECK_RUN_ON(signaling_thread()); + channel_usage_ = channel_usage; + pc_->OnSctpDataChannelStateChanged(channel_id, state); + })); } void DataChannelController::OnDataReceived( @@ -169,6 +167,8 @@ void DataChannelController::SetupDataChannelTransport_n( void DataChannelController::PrepareForShutdown() { RTC_DCHECK_RUN_ON(signaling_thread()); signaling_safety_.reset(PendingTaskSafetyFlag::CreateDetachedInactive()); + if (channel_usage_ != DataChannelUsage::kNeverUsed) + channel_usage_ = DataChannelUsage::kHaveBeenUsed; } void DataChannelController::TeardownDataChannelTransport_n(RTCError error) { @@ -237,7 +237,7 @@ bool DataChannelController::HandleOpenMessage_n( void DataChannelController::OnDataChannelOpenMessage( rtc::scoped_refptr channel, bool ready_to_send) { - has_used_data_channels_ = true; + channel_usage_ = DataChannelUsage::kInUse; auto proxy = SctpDataChannel::CreateProxy(channel, signaling_safety_.flag()); pc_->Observer()->OnDataChannel(proxy); @@ -342,7 +342,7 @@ DataChannelController::InternalCreateDataChannelWithProxy( if (!ret.ok()) return ret.MoveError(); - has_used_data_channels_ = true; + channel_usage_ = DataChannelUsage::kInUse; return SctpDataChannel::CreateProxy(ret.MoveValue(), signaling_safety_.flag()); } diff --git a/pc/data_channel_controller.h b/pc/data_channel_controller.h index 1b5c8beadb..bf3ac03437 100644 --- a/pc/data_channel_controller.h +++ b/pc/data_channel_controller.h @@ -94,13 +94,13 @@ class DataChannelController : public SctpDataChannelControllerInterface, // At some point in time, a data channel has existed. bool HasUsedDataChannels() const; - void OnSctpDataChannelClosed(SctpDataChannel* channel); - protected: rtc::Thread* network_thread() const; rtc::Thread* signaling_thread() const; private: + void OnSctpDataChannelClosed(SctpDataChannel* channel); + // Creates a new SctpDataChannel object on the network thread. RTCErrorOr> CreateDataChannel( const std::string& label, @@ -143,7 +143,13 @@ class DataChannelController : public SctpDataChannelControllerInterface, SctpSidAllocator sid_allocator_ RTC_GUARDED_BY(network_thread()); std::vector> sctp_data_channels_n_ RTC_GUARDED_BY(network_thread()); - bool has_used_data_channels_ RTC_GUARDED_BY(signaling_thread()) = false; + enum class DataChannelUsage : uint8_t { + kNeverUsed = 0, + kHaveBeenUsed, + kInUse + }; + DataChannelUsage channel_usage_ RTC_GUARDED_BY(signaling_thread()) = + DataChannelUsage::kNeverUsed; // Owning PeerConnection. PeerConnectionInternal* const pc_; diff --git a/pc/sdp_offer_answer.cc b/pc/sdp_offer_answer.cc index 0fbc7add43..7f3db91a5c 100644 --- a/pc/sdp_offer_answer.cc +++ b/pc/sdp_offer_answer.cc @@ -4275,8 +4275,7 @@ void SdpOfferAnswerHandler::GetOptionsForUnifiedPlanOffer( } // Lastly, add a m-section if we have requested local data channels and an // m section does not already exist. - if (!pc_->GetDataMid() && data_channel_controller()->HasUsedDataChannels() && - data_channel_controller()->HasDataChannels()) { + if (!pc_->GetDataMid() && data_channel_controller()->HasDataChannels()) { // Attempt to recycle a stopped m-line. // TODO(crbug.com/1442604): GetDataMid() should return the mid if one was // ever created but rejected.