Update SetPayloadTypeDemuxingEnabled_w to avoid an unnecessary Invoke.

If a call to BaseChannel::SetPayloadTypeDemuxingEnabled does not result
in a demuxer criteria configuration change, we can avoid an Invoke
and speed up negotiations.

Bug: webrtc:11993
Change-Id: I80c894d7cc2a733ab84c1f4978f0c58c09a123a5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/244920
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35649}
diff --git a/pc/channel.cc b/pc/channel.cc
index 6716c06..c60cce5 100644
--- a/pc/channel.cc
+++ b/pc/channel.cc
@@ -572,10 +572,16 @@
 }
 
 bool BaseChannel::SetPayloadTypeDemuxingEnabled_w(bool enabled) {
+  RTC_LOG_THREAD_BLOCK_COUNT();
+
   if (enabled == payload_type_demuxing_enabled_) {
     return true;
   }
+
   payload_type_demuxing_enabled_ = enabled;
+
+  bool config_changed = false;
+
   if (!enabled) {
     // TODO(crbug.com/11477): This will remove *all* unsignaled streams (those
     // without an explicitly signaled SSRC), which may include streams that
@@ -583,14 +589,25 @@
     // streams that were matched based on payload type alone, but currently
     // there is no straightforward way to identify those streams.
     media_channel()->ResetUnsignaledRecvStream();
-    demuxer_criteria_.payload_types().clear();
+    if (!demuxer_criteria_.payload_types().empty()) {
+      config_changed = true;
+      demuxer_criteria_.payload_types().clear();
+    }
   } else if (!payload_types_.empty()) {
-    // TODO(tommi): Instead of 'insert', should this simply overwrite the value
-    // of the criteria?
-    demuxer_criteria_.payload_types().insert(payload_types_.begin(),
-                                             payload_types_.end());
+    for (const auto& type : payload_types_) {
+      if (demuxer_criteria_.payload_types().insert(type).second) {
+        config_changed = true;
+      }
+    }
+  } else {
+    RTC_DCHECK(demuxer_criteria_.payload_types().empty());
   }
 
+  RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0);
+
+  if (!config_changed)
+    return true;
+
   // Note: This synchronously hops to the network thread.
   return RegisterRtpDemuxerSink_w();
 }