Add utility method for consistency checking in BaseChannel classes.

This is a simple check for upcoming changes for media channels to
be able to check if the state on the network thread is consistent.

Bug: webrtc:11992
Change-Id: I8ed2d091ecf3869a66970fc4733aebf209c4ef82
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/246681
Reviewed-by: Niels Moller <nisse@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35706}
diff --git a/media/base/media_channel.cc b/media/base/media_channel.cc
index 5d59dba..1b11fcc 100644
--- a/media/base/media_channel.cc
+++ b/media/base/media_channel.cc
@@ -89,6 +89,11 @@
   return extmap_allow_mixed_;
 }
 
+bool MediaChannel::HasNetworkInterface() const {
+  RTC_DCHECK_RUN_ON(network_thread_);
+  return network_interface_ != nullptr;
+}
+
 void MediaChannel::SetEncoderToPacketizerFrameTransformer(
     uint32_t ssrc,
     rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) {}
diff --git a/media/base/media_channel.h b/media/base/media_channel.h
index 8f20616..2b0ef81 100644
--- a/media/base/media_channel.h
+++ b/media/base/media_channel.h
@@ -260,6 +260,10 @@
   void SetExtmapAllowMixed(bool extmap_allow_mixed);
   bool ExtmapAllowMixed() const;
 
+  // Returns `true` if a non-null NetworkInterface pointer is held.
+  // Must be called on the network thread.
+  bool HasNetworkInterface() const;
+
   virtual webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const = 0;
   virtual webrtc::RTCError SetRtpSendParameters(
       uint32_t ssrc,
diff --git a/pc/channel.cc b/pc/channel.cc
index e969d4c..95e7026 100644
--- a/pc/channel.cc
+++ b/pc/channel.cc
@@ -192,6 +192,7 @@
     SetRtpTransport(rtp_transport);
     // Both RTP and RTCP channels should be set, we can call SetInterface on
     // the media channel and it can set network options.
+    RTC_DCHECK(!media_channel_->HasNetworkInterface());
     media_channel_->SetInterface(this);
   });
 }
@@ -208,6 +209,7 @@
     if (rtp_transport_) {
       DisconnectFromRtpTransport_n();
     }
+    RTC_DCHECK(!network_initialized());
   });
 }
 
@@ -797,6 +799,7 @@
 
 void BaseChannel::SignalSentPacket_n(const rtc::SentPacket& sent_packet) {
   RTC_DCHECK_RUN_ON(network_thread());
+  RTC_DCHECK(network_initialized());
   media_channel()->OnPacketSent(sent_packet);
 }
 
diff --git a/pc/channel.h b/pc/channel.h
index 109e349..6c2fa1d 100644
--- a/pc/channel.h
+++ b/pc/channel.h
@@ -217,6 +217,10 @@
     return extensions_filter_;
   }
 
+  bool network_initialized() RTC_RUN_ON(network_thread()) {
+    return media_channel_->HasNetworkInterface();
+  }
+
   bool enabled() const RTC_RUN_ON(worker_thread()) { return enabled_; }
   rtc::Thread* signaling_thread() const { return signaling_thread_; }