Move ssrcs_ and payload_types_ to the network thread Moves the remaining demuxer criteria related variables in the channel classes, ssrcs_ and payload_types_, to the network thread. State updates are now passed as optional parameters and inlined into `MaybeUpdateDemuxerAndRtpExtensions_w` and `RegisterRtpDemuxerSink_w`. These methods apply the modifications atomically within a blocking call to the network thread. Specific changes include: * Updating annotations to `RTC_GUARDED_BY(network_thread())`. * Removing `MaybeAddHandledPayloadType` to construct payloads prior to the thread hop. * Restricting `demuxer_criteria()` to run only on the network thread. Bug: webrtc:42222117 Change-Id: If5a14bd5626536118e3caa7c87237a94d7ba2230 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/456300 Reviewed-by: Henrik Boström <hbos@webrtc.org> Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org> Cr-Commit-Position: refs/heads/main@{#47179}
diff --git a/pc/channel.cc b/pc/channel.cc index 3f70792..f170537 100644 --- a/pc/channel.cc +++ b/pc/channel.cc
@@ -52,7 +52,6 @@ #include "rtc_base/socket.h" #include "rtc_base/strings/string_format.h" #include "rtc_base/thread.h" -#include "rtc_base/thread_annotations.h" #include "rtc_base/trace_event.h" #include "rtc_base/unique_id_generator.h" @@ -384,15 +383,7 @@ })); } - // TODO: bugs.webrtc.org/42222117 - payload_types_ is guarded by the worker - // thread, but we read it here on the network thread. This is safe because - // this method is called synchronously during SDP signaling, during which no - // tasks that modify payload_types_ can be running on the worker thread. - auto has_payload_types = [this]() RTC_NO_THREAD_SAFETY_ANALYSIS { - return !payload_types_.empty(); - }; - - if (has_payload_types()) { + if (!payload_types_.empty()) { if (!rtp_transport_) { return false; } @@ -586,7 +577,9 @@ bool BaseChannel::MaybeUpdateDemuxerAndRtpExtensions_w( bool update_demuxer, + std::optional<flat_set<uint8_t>> payload_types, const RtpHeaderExtensions& extensions, + std::optional<flat_set<uint32_t>> ssrcs, std::string& error_desc) { bool update_extensions = true; if (rtp_header_extensions_ == extensions) { @@ -609,12 +602,14 @@ } } - if (!update_demuxer && !update_extensions) + if (!update_demuxer && !update_extensions && !payload_types.has_value() && + !ssrcs.has_value()) { return true; // No update needed. + } // TODO(bugs.webrtc.org/13536): See if we can do this asynchronously. - if (update_demuxer) { + if (update_demuxer || payload_types.has_value() || ssrcs.has_value()) { media_receive_channel()->OnDemuxerCriteriaUpdatePending(); } @@ -631,6 +626,20 @@ rtp_transport_->RegisterRtpHeaderExtensionMap(mid(), extensions); } + if (payload_types) { + if (payload_types_ != *payload_types) { + payload_types_ = std::move(*payload_types); + update_demuxer = true; + } + } + + if (ssrcs) { + if (ssrcs_ != *ssrcs) { + ssrcs_ = std::move(*ssrcs); + update_demuxer = true; + } + } + if (!update_demuxer) return true; @@ -644,13 +653,15 @@ return true; }); - if (update_demuxer) + if (update_demuxer || payload_types.has_value() || ssrcs.has_value()) media_receive_channel()->OnDemuxerCriteriaUpdateComplete(); return success; } -bool BaseChannel::RegisterRtpDemuxerSink_w() { +bool BaseChannel::RegisterRtpDemuxerSink_w( + bool clear_payload_types, + std::optional<flat_set<uint32_t>> ssrcs) { media_receive_channel()->OnDemuxerCriteriaUpdatePending(); bool ret = network_thread_->BlockingCall([&] { RTC_DCHECK_RUN_ON(network_thread()); @@ -660,6 +671,25 @@ // See chromium:1295469 for an example. return false; } + + bool needs_re_registration = false; + + if (clear_payload_types && !payload_types_.empty()) { + payload_types_.clear(); + needs_re_registration = true; + } + + if (ssrcs) { + if (ssrcs_ != *ssrcs) { + ssrcs_ = std::move(*ssrcs); + needs_re_registration = true; + } + } + + if (!needs_re_registration) { + return true; + } + // Note that RegisterRtpDemuxerSink first unregisters the sink if // already registered. So this will change the state of the class // whether the call succeeds or not. @@ -822,15 +852,12 @@ SdpType type, std::string& error_desc) { RTC_LOG_THREAD_BLOCK_COUNT(); - bool needs_re_registration = false; + bool clear_payload_types = false; if (!RtpTransceiverDirectionHasSend(content->direction())) { RTC_DLOG(LS_VERBOSE) << "UpdateRemoteStreams_w: remote side will not send " "- disable payload type demuxing for " << ToString(); - if (!payload_types_.empty()) { - payload_types_.clear(); - needs_re_registration = true; - } + clear_payload_types = true; } const std::vector<StreamParams>& streams = content->streams(); @@ -888,15 +915,10 @@ ssrcs.insert(new_stream.ssrcs.begin(), new_stream.ssrcs.end()); } - if (ssrcs_ != ssrcs) { - ssrcs_ = std::move(ssrcs); - needs_re_registration = true; - } - RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0); // Re-register the sink to update after changing the demuxer criteria. - if (needs_re_registration && !RegisterRtpDemuxerSink_w()) { + if (!RegisterRtpDemuxerSink_w(clear_payload_types, std::move(ssrcs))) { error_desc = StringFormat("Failed to set up audio demuxing for mid='%s'.", mid().c_str()); return false; @@ -918,12 +940,6 @@ extensions_filter_); } -bool BaseChannel::MaybeAddHandledPayloadType(int payload_type) { - // Even if payload type demuxing is currently disabled, we need to remember - // the payload types in case it's re-enabled later. - return payload_types_.insert(static_cast<uint8_t>(payload_type)).second; -} - bool BaseChannel::CheckRtpExtensionValidity( const RtpHeaderExtensions& extensions, std::string& error_desc) const { @@ -1046,12 +1062,11 @@ return false; } - bool criteria_modified = false; + std::optional<flat_set<uint8_t>> payload_types; if (RtpTransceiverDirectionHasRecv(content->direction())) { + payload_types.emplace(); for (const Codec& codec : content->codecs()) { - if (MaybeAddHandledPayloadType(codec.id)) { - criteria_modified = true; - } + payload_types->insert(codec.id); } } @@ -1084,7 +1099,9 @@ // RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0); bool success = MaybeUpdateDemuxerAndRtpExtensions_w( - criteria_modified, recv_params.extensions, error_desc); + /*update_demuxer=*/false, std::move(payload_types), + recv_params.extensions, + /*ssrcs=*/std::nullopt, error_desc); // RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1); @@ -1148,7 +1165,8 @@ bool success = true; if (type == SdpType::kAnswer || type == SdpType::kPrAnswer) { success = MaybeUpdateDemuxerAndRtpExtensions_w( - /*update_demuxer=*/false, last_recv_params_.extensions, error_desc); + /*update_demuxer=*/false, /*payload_types=*/std::nullopt, + last_recv_params_.extensions, /*ssrcs=*/std::nullopt, error_desc); } return success; @@ -1241,11 +1259,11 @@ return false; } - bool criteria_modified = false; + std::optional<flat_set<uint8_t>> payload_types; if (RtpTransceiverDirectionHasRecv(content->direction())) { + payload_types.emplace(); for (const Codec& codec : content->codecs()) { - if (MaybeAddHandledPayloadType(codec.id)) - criteria_modified = true; + payload_types->insert(codec.id); } } @@ -1272,7 +1290,8 @@ RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0); bool success = MaybeUpdateDemuxerAndRtpExtensions_w( - criteria_modified, recv_params.extensions, error_desc); + /*update_demuxer=*/false, std::move(payload_types), + recv_params.extensions, /*ssrcs=*/std::nullopt, error_desc); RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1); @@ -1336,7 +1355,8 @@ if (type == SdpType::kAnswer || type == SdpType::kPrAnswer) { bool success = MaybeUpdateDemuxerAndRtpExtensions_w( - /*update_demuxer=*/false, last_recv_params_.extensions, error_desc); + /*update_demuxer=*/false, /*payload_types=*/std::nullopt, + last_recv_params_.extensions, /*ssrcs=*/std::nullopt, error_desc); if (!success) { return false; }
diff --git a/pc/channel.h b/pc/channel.h index c53c1b4..f4488cd 100644 --- a/pc/channel.h +++ b/pc/channel.h
@@ -278,12 +278,6 @@ RtpHeaderExtensions GetDeduplicatedRtpHeaderExtensions( const RtpHeaderExtensions& extensions); - // Add `payload_type` to `demuxer_criteria_` if payload type demuxing is - // enabled. - // Returns true if the demuxer payload type changed and a re-registration - // is needed. - bool MaybeAddHandledPayloadType(int payload_type) RTC_RUN_ON(worker_thread()); - // Checks that the provided RTP header extensions are valid. // This verifies that all extension IDs are within the valid range, // that there are no duplicate IDs, and that no existing extension ID @@ -297,13 +291,17 @@ // failed, which needs to be treated as an error. bool MaybeUpdateDemuxerAndRtpExtensions_w( bool update_demuxer, + std::optional<flat_set<uint8_t>> payload_types, const RtpHeaderExtensions& extensions, + std::optional<flat_set<uint32_t>> ssrcs, std::string& error_desc) RTC_RUN_ON(worker_thread()); // Registers a demuxer criteria with the transport, on the network thread. // This function will fail if there's no transport of if a sink is already // registered for this channel's demuxer_critera(). - bool RegisterRtpDemuxerSink_w() RTC_RUN_ON(worker_thread()); + bool RegisterRtpDemuxerSink_w(bool clear_payload_types, + std::optional<flat_set<uint32_t>> ssrcs) + RTC_RUN_ON(worker_thread()); // Return description of media channel to facilitate logging std::string ToString() const; @@ -316,11 +314,8 @@ RTC_RUN_ON(network_thread()); void DisconnectFromRtpTransport_n() RTC_RUN_ON(network_thread()); void SignalSentPacket_n(const SentPacketInfo& sent_packet); - // Only called on the network thread, so should be marked as - // `RTC_RUN_ON(network_thread());` but currently accesses variables - // that are associated with the worker thread. Those operations are - // done in a synchronous way, so they're safe, but not ideal. - RtpDemuxerCriteria demuxer_criteria() const RTC_NO_THREAD_SAFETY_ANALYSIS; + // Only called on the network thread. + RtpDemuxerCriteria demuxer_criteria() const RTC_RUN_ON(network_thread()); TaskQueueBase* const worker_thread_; Thread* const network_thread_; @@ -367,7 +362,7 @@ RTC_GUARDED_BY(worker_thread()) = RtpTransceiverDirection::kInactive; // Cached list of payload types, used if payload type demuxing is re-enabled. - flat_set<uint8_t> payload_types_ RTC_GUARDED_BY(worker_thread()); + flat_set<uint8_t> payload_types_ RTC_GUARDED_BY(network_thread()); // A stored copy of the rtp header extensions as applied to the transport. RtpHeaderExtensions rtp_header_extensions_ RTC_GUARDED_BY(worker_thread()); @@ -377,7 +372,7 @@ RTC_GUARDED_BY(worker_thread()); const std::string mid_; - flat_set<uint32_t> ssrcs_ RTC_GUARDED_BY(worker_thread()); + flat_set<uint32_t> ssrcs_ RTC_GUARDED_BY(network_thread()); // This generator is used to generate SSRCs for local streams. // This is needed in cases where SSRCs are not negotiated or set explicitly // like in Simulcast.