Replace explicit tq check in JsepTransport with a sequence checker

Bug: webrtc:360058654
Change-Id: If6854091095e2c068f74c390f79dc5ab3c86a63a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/427120
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#46251}
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index 809378b..ccaae5a 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -230,7 +230,6 @@
     "../api:rtc_error",
     "../api:scoped_refptr",
     "../api:sequence_checker",
-    "../api/task_queue",
     "../api/transport:datagram_transport_interface",
     "../call:payload_type_picker",
     "../media:rtc_data_sctp_transport_internal",
@@ -240,14 +239,13 @@
     "../p2p:p2p_transport_channel",
     "../p2p:transport_description",
     "../rtc_base:checks",
-    "../rtc_base:copy_on_write_buffer",
     "../rtc_base:event_tracer",
     "../rtc_base:logging",
     "../rtc_base:macromagic",
     "../rtc_base:ssl",
     "../rtc_base:ssl_adapter",
     "../rtc_base:stringutils",
-    "../rtc_base:threading",
+    "../rtc_base/system:no_unique_address",
     "//third_party/abseil-cpp/absl/functional:any_invocable",
   ]
 }
diff --git a/pc/jsep_transport.cc b/pc/jsep_transport.cc
index b128fba..ce80ef9 100644
--- a/pc/jsep_transport.cc
+++ b/pc/jsep_transport.cc
@@ -40,13 +40,11 @@
 #include "pc/session_description.h"
 #include "pc/transport_stats.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/copy_on_write_buffer.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/rtc_certificate.h"
 #include "rtc_base/ssl_fingerprint.h"
 #include "rtc_base/ssl_stream_adapter.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/thread.h"
 #include "rtc_base/trace_event.h"
 
 using webrtc::SdpType;
@@ -99,8 +97,7 @@
     std::unique_ptr<SctpTransportInternal> sctp_transport,
     absl::AnyInvocable<void()> rtcp_mux_active_callback,
     PayloadTypePicker& suggester)
-    : network_thread_(Thread::Current()),
-      mid_(mid),
+    : mid_(mid),
       local_certificate_(local_certificate),
       ice_transport_(std::move(ice_transport)),
       rtcp_ice_transport_(std::move(rtcp_ice_transport)),
@@ -159,7 +156,7 @@
     SdpType type) {
   RTCError error;
   TRACE_EVENT0("webrtc", "JsepTransport::SetLocalJsepTransportDescription");
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
 
   IceParameters ice_parameters =
       jsep_description.transport_desc.GetIceParameters();
@@ -233,7 +230,7 @@
   TRACE_EVENT0("webrtc", "JsepTransport::SetLocalJsepTransportDescription");
   RTCError error;
 
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
 
   IceParameters ice_parameters =
       jsep_description.transport_desc.GetIceParameters();
@@ -280,7 +277,7 @@
 }
 
 RTCError JsepTransport::AddRemoteCandidates(const Candidates& candidates) {
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   if (!remote_description_) {
     return RTCError(RTCErrorType::INVALID_STATE,
                     mid() +
@@ -304,7 +301,7 @@
 }
 
 void JsepTransport::SetNeedsIceRestartFlag() {
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   if (!needs_ice_restart_) {
     needs_ice_restart_ = true;
     RTC_LOG(LS_VERBOSE) << "needs-ice-restart flag set for transport " << mid();
@@ -312,7 +309,7 @@
 }
 
 std::optional<SSLRole> JsepTransport::GetDtlsRole() const {
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   RTC_DCHECK(rtp_dtls_transport_);
   RTC_DCHECK(rtp_dtls_transport_->internal());
   SSLRole dtls_role;
@@ -325,7 +322,7 @@
 
 bool JsepTransport::GetStats(TransportStats* stats) const {
   TRACE_EVENT0("webrtc", "JsepTransport::GetStats");
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   stats->transport_name = mid();
   stats->channel_stats.clear();
   RTC_DCHECK(rtp_dtls_transport_->internal());
@@ -344,7 +341,7 @@
     const RTCCertificate* certificate,
     const SSLFingerprint* fingerprint) const {
   TRACE_EVENT0("webrtc", "JsepTransport::VerifyCertificateFingerprint");
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   if (!fingerprint) {
     return RTCError(RTCErrorType::INVALID_PARAMETER, "No fingerprint");
   }
@@ -367,7 +364,7 @@
 }
 
 void JsepTransport::SetActiveResetSrtpParams(bool active_reset_srtp_params) {
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   if (dtls_srtp_transport_) {
     RTC_LOG(LS_INFO)
         << "Setting active_reset_srtp_params of DtlsSrtpTransport to: "
@@ -379,7 +376,7 @@
 RTCError JsepTransport::RecordPayloadTypes(bool local,
                                            SdpType type,
                                            const ContentInfo& content) {
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   if (local) {
     local_payload_types_.DisallowRedefinition();
   } else {
@@ -408,7 +405,7 @@
     const IceParameters& ice_parameters,
     IceTransportInternal* ice_transport) {
   TRACE_EVENT0("webrtc", "JsepTransport::SetRemoteIceParameters");
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   RTC_DCHECK(ice_transport);
   RTC_DCHECK(remote_description_);
   ice_transport->SetRemoteIceParameters(ice_parameters);
@@ -428,7 +425,7 @@
 bool JsepTransport::SetRtcpMux(bool enable,
                                SdpType type,
                                ContentSource source) {
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   bool ret = false;
   switch (type) {
     case SdpType::kOffer:
@@ -475,7 +472,7 @@
 
 RTCError JsepTransport::NegotiateAndSetDtlsParameters(
     SdpType local_description_type) {
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   if (!local_description_ || !remote_description_) {
     return RTCError(RTCErrorType::INVALID_STATE,
                     "Applying an answer transport description "
@@ -637,7 +634,7 @@
 bool JsepTransport::GetTransportStats(DtlsTransportInternal* dtls_transport,
                                       int component,
                                       TransportStats* stats) const {
-  RTC_DCHECK_RUN_ON(network_thread_);
+  RTC_DCHECK_RUN_ON(&transport_sequence_);
   RTC_DCHECK(dtls_transport);
   TransportChannelStats substats;
   substats.component = component;
diff --git a/pc/jsep_transport.h b/pc/jsep_transport.h
index 8190acf..a234aaa 100644
--- a/pc/jsep_transport.h
+++ b/pc/jsep_transport.h
@@ -22,7 +22,6 @@
 #include "api/rtc_error.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
-#include "api/task_queue/task_queue_base.h"
 #include "api/transport/data_channel_transport_interface.h"
 #include "call/payload_type_picker.h"
 #include "media/sctp/sctp_transport_internal.h"
@@ -40,6 +39,7 @@
 #include "rtc_base/rtc_certificate.h"
 #include "rtc_base/ssl_fingerprint.h"
 #include "rtc_base/ssl_stream_adapter.h"
+#include "rtc_base/system/no_unique_address.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace webrtc {
@@ -102,13 +102,13 @@
   // Needed in order to verify the local fingerprint.
   void SetLocalCertificate(
       const scoped_refptr<RTCCertificate>& local_certificate) {
-    RTC_DCHECK_RUN_ON(network_thread_);
+    RTC_DCHECK_RUN_ON(&transport_sequence_);
     local_certificate_ = local_certificate;
   }
 
   // Return the local certificate provided by SetLocalCertificate.
   scoped_refptr<RTCCertificate> GetLocalCertificate() const {
-    RTC_DCHECK_RUN_ON(network_thread_);
+    RTC_DCHECK_RUN_ON(&transport_sequence_);
     return local_certificate_;
   }
 
@@ -132,7 +132,7 @@
   // occurred yet for this transport (by applying a local description with
   // changed ufrag/password).
   bool needs_ice_restart() const {
-    RTC_DCHECK_RUN_ON(network_thread_);
+    RTC_DCHECK_RUN_ON(&transport_sequence_);
     return needs_ice_restart_;
   }
 
@@ -143,12 +143,12 @@
   bool GetStats(TransportStats* stats) const;
 
   const JsepTransportDescription* local_description() const {
-    RTC_DCHECK_RUN_ON(network_thread_);
+    RTC_DCHECK_RUN_ON(&transport_sequence_);
     return local_description_.get();
   }
 
   const JsepTransportDescription* remote_description() const {
-    RTC_DCHECK_RUN_ON(network_thread_);
+    RTC_DCHECK_RUN_ON(&transport_sequence_);
     return remote_description_.get();
   }
 
@@ -178,7 +178,7 @@
   }
 
   const DtlsTransportInternal* rtcp_dtls_transport() const {
-    RTC_DCHECK_RUN_ON(network_thread_);
+    RTC_DCHECK_RUN_ON(&transport_sequence_);
     if (rtcp_dtls_transport_) {
       return rtcp_dtls_transport_->internal();
     }
@@ -186,7 +186,7 @@
   }
 
   DtlsTransportInternal* rtcp_dtls_transport() {
-    RTC_DCHECK_RUN_ON(network_thread_);
+    RTC_DCHECK_RUN_ON(&transport_sequence_);
     if (rtcp_dtls_transport_) {
       return rtcp_dtls_transport_->internal();
     }
@@ -233,7 +233,7 @@
   }
   PayloadTypeRecorder& local_payload_types() { return local_payload_types_; }
   void CommitPayloadTypes() {
-    RTC_DCHECK_RUN_ON(network_thread_);
+    RTC_DCHECK_RUN_ON(&transport_sequence_);
     local_payload_types_.Commit();
     remote_payload_types_.Commit();
   }
@@ -241,7 +241,7 @@
  private:
   bool SetRtcpMux(bool enable, SdpType type, ContentSource source);
 
-  void ActivateRtcpMux() RTC_RUN_ON(network_thread_);
+  void ActivateRtcpMux() RTC_RUN_ON(transport_sequence_);
 
   // Negotiates and sets the DTLS parameters based on the current local and
   // remote transport description, such as the DTLS role to use, and whether
@@ -273,16 +273,16 @@
                          TransportStats* stats) const;
 
   // Owning thread, for safety checks
-  const TaskQueueBase* const network_thread_;
+  RTC_NO_UNIQUE_ADDRESS SequenceChecker transport_sequence_;
   const std::string mid_;
   // needs-ice-restart bit as described in JSEP.
-  bool needs_ice_restart_ RTC_GUARDED_BY(network_thread_) = false;
+  bool needs_ice_restart_ RTC_GUARDED_BY(transport_sequence_) = false;
   scoped_refptr<RTCCertificate> local_certificate_
-      RTC_GUARDED_BY(network_thread_);
+      RTC_GUARDED_BY(transport_sequence_);
   std::unique_ptr<JsepTransportDescription> local_description_
-      RTC_GUARDED_BY(network_thread_);
+      RTC_GUARDED_BY(transport_sequence_);
   std::unique_ptr<JsepTransportDescription> remote_description_
-      RTC_GUARDED_BY(network_thread_);
+      RTC_GUARDED_BY(transport_sequence_);
 
   // Ice transport which may be used by any of upper-layer transports (below).
   // Owned by JsepTransport and guaranteed to outlive the transports below.
@@ -298,17 +298,17 @@
   // The RTCP transport is const for all usages, except that it is cleared
   // when RTCP multiplexing is turned on; this happens on the network thread.
   scoped_refptr<DtlsTransport> rtcp_dtls_transport_
-      RTC_GUARDED_BY(network_thread_);
+      RTC_GUARDED_BY(transport_sequence_);
 
   const scoped_refptr<::webrtc::SctpTransport> sctp_transport_;
 
-  RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(network_thread_);
+  RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(transport_sequence_);
 
   // Cache the encrypted header extension IDs
   std::optional<std::vector<int>> send_extension_ids_
-      RTC_GUARDED_BY(network_thread_);
+      RTC_GUARDED_BY(transport_sequence_);
   std::optional<std::vector<int>> recv_extension_ids_
-      RTC_GUARDED_BY(network_thread_);
+      RTC_GUARDED_BY(transport_sequence_);
 
   // This is invoked when RTCP-mux becomes active and
   // `rtcp_dtls_transport_` is destroyed. The JsepTransportController will
@@ -316,9 +316,9 @@
   absl::AnyInvocable<void()> rtcp_mux_active_callback_;
 
   // Assigned PTs from the remote description, used when sending.
-  PayloadTypeRecorder remote_payload_types_ RTC_GUARDED_BY(network_thread_);
+  PayloadTypeRecorder remote_payload_types_ RTC_GUARDED_BY(transport_sequence_);
   // Assigned PTs from the local description, used when receiving.
-  PayloadTypeRecorder local_payload_types_ RTC_GUARDED_BY(network_thread_);
+  PayloadTypeRecorder local_payload_types_ RTC_GUARDED_BY(transport_sequence_);
 };
 
 }  //  namespace webrtc