Change SetLocalContent in channel classes to avoid Invoke.

With these changes, we now often have 0 invokes and at most 1 when
calling SetLocalContent on a channel. Before we had at least 1 and
typically 2.

Summary of changes.
* Updating RtpExtension::DeduplicateHeaderExtensions to return a sorted
  vector (+test) for easy detection of changes.
* Before updating the transport on the network thread, detect if
  actual changes to the demuxer criteria or changes to the rtp header
  extensions have been made.
* Consolidate both transport updates to a single call instead of two.
* Added DCHECK guards to catch regressions in number of invokes.

A possible upcoming improvement is to update the transport
asynchronously.

Bug: webrtc:13536
Change-Id: I71ef7b181635a796ffa1e3a02a0f661d28a4870c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/244700
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35638}
diff --git a/api/rtp_parameters.cc b/api/rtp_parameters.cc
index feba393..c48b8da 100644
--- a/api/rtp_parameters.cc
+++ b/api/rtp_parameters.cc
@@ -11,6 +11,7 @@
 
 #include <algorithm>
 #include <string>
+#include <tuple>
 #include <utility>
 
 #include "api/array_view.h"
@@ -280,6 +281,14 @@
     }
   }
 
+  // Sort the returned vector to make comparisons of header extensions reliable.
+  // In order of priority, we sort by uri first, then encrypt and id last.
+  std::sort(filtered.begin(), filtered.end(),
+            [](const RtpExtension& a, const RtpExtension& b) {
+              return std::tie(a.uri, a.encrypt, a.id) <
+                     std::tie(b.uri, b.encrypt, b.id);
+            });
+
   return filtered;
 }
 }  // namespace webrtc
diff --git a/api/rtp_parameters.h b/api/rtp_parameters.h
index 84f3a0e..45cedfd 100644
--- a/api/rtp_parameters.h
+++ b/api/rtp_parameters.h
@@ -286,6 +286,9 @@
       bool encrypt);
 
   // Returns a list of extensions where any extension URI is unique.
+  // The returned list will be sorted by uri first, then encrypt and id last.
+  // Having the list sorted allows the caller fo compare filtered lists for
+  // equality to detect when changes have been made.
   static const std::vector<RtpExtension> DeduplicateHeaderExtensions(
       const std::vector<RtpExtension>& extensions,
       Filter filter);
diff --git a/api/rtp_parameters_unittest.cc b/api/rtp_parameters_unittest.cc
index 51ad426..234c3c9 100644
--- a/api/rtp_parameters_unittest.cc
+++ b/api/rtp_parameters_unittest.cc
@@ -109,6 +109,38 @@
   EXPECT_EQ((std::vector<RtpExtension>{kExtension1Encrypted}), filtered);
 }
 
+// Test that the filtered vector is sorted so that for a given unsorted array of
+// extensions, the filtered vector will always be laied out the same (for easy
+// comparison).
+TEST(RtpExtensionTest, DeduplicateHeaderExtensionsSorted) {
+  const std::vector<RtpExtension> extensions = {
+      RtpExtension("cde1", 11, false), RtpExtension("cde2", 12, true),
+      RtpExtension("abc1", 3, false),  RtpExtension("abc2", 4, true),
+      RtpExtension("cde3", 9, true),   RtpExtension("cde4", 10, false),
+      RtpExtension("abc3", 1, true),   RtpExtension("abc4", 2, false),
+      RtpExtension("bcd3", 7, false),  RtpExtension("bcd1", 8, true),
+      RtpExtension("bcd2", 5, true),   RtpExtension("bcd4", 6, false),
+  };
+
+  auto encrypted = RtpExtension::DeduplicateHeaderExtensions(
+      extensions, RtpExtension::Filter::kRequireEncryptedExtension);
+
+  const std::vector<RtpExtension> expected_sorted_encrypted = {
+      RtpExtension("abc2", 4, true),  RtpExtension("abc3", 1, true),
+      RtpExtension("bcd1", 8, true),  RtpExtension("bcd2", 5, true),
+      RtpExtension("cde2", 12, true), RtpExtension("cde3", 9, true)};
+  EXPECT_EQ(expected_sorted_encrypted, encrypted);
+
+  auto unencypted = RtpExtension::DeduplicateHeaderExtensions(
+      extensions, RtpExtension::Filter::kDiscardEncryptedExtension);
+
+  const std::vector<RtpExtension> expected_sorted_unencrypted = {
+      RtpExtension("abc1", 3, false),  RtpExtension("abc4", 2, false),
+      RtpExtension("bcd3", 7, false),  RtpExtension("bcd4", 6, false),
+      RtpExtension("cde1", 11, false), RtpExtension("cde4", 10, false)};
+  EXPECT_EQ(expected_sorted_unencrypted, unencypted);
+}
+
 TEST(RtpExtensionTest, FindHeaderExtensionByUriAndEncryption) {
   std::vector<RtpExtension> extensions;