Only answer with non-stopped RTP header extensions

This extends the RTP header extension API usage to generating answers.

Also re-adds unit tests removed by the revert.

BUG=chromium:1051821

Change-Id: Ib754284e9a77cb49e22bea7072c475d240f2563b
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/298740
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Philipp Hancke <phancke@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#39800}
diff --git a/pc/media_session.cc b/pc/media_session.cc
index 8aff4d9..23a2ad1 100644
--- a/pc/media_session.cc
+++ b/pc/media_session.cc
@@ -1411,8 +1411,24 @@
       enable_encrypted_rtp_header_extensions
           ? webrtc::RtpExtension::Filter::kPreferEncryptedExtension
           : webrtc::RtpExtension::Filter::kDiscardEncryptedExtension;
+
+  // Filter local extensions by capabilities and direction.
+  RtpHeaderExtensions local_rtp_extensions_to_reply_with;
+  for (auto extension_with_id : local_rtp_extensions) {
+    for (const auto& extension : media_description_options.header_extensions) {
+      if (extension_with_id.uri == extension.uri) {
+        // TODO(crbug.com/1051821): Configure the extension direction from
+        // the information in the media_description_options extension
+        // capability. For now, do not include stopped extensions.
+        // See also crbug.com/webrtc/7477 about the general lack of direction.
+        if (extension.direction != RtpTransceiverDirection::kStopped) {
+          local_rtp_extensions_to_reply_with.push_back(extension_with_id);
+        }
+      }
+    }
+  }
   RtpHeaderExtensions negotiated_rtp_extensions;
-  NegotiateRtpHeaderExtensions(local_rtp_extensions,
+  NegotiateRtpHeaderExtensions(local_rtp_extensions_to_reply_with,
                                offer->rtp_header_extensions(),
                                extensions_filter, &negotiated_rtp_extensions);
   answer->set_rtp_header_extensions(negotiated_rtp_extensions);
@@ -2591,7 +2607,7 @@
     const SessionDescription* current_description,
     const TransportInfo* bundle_transport,
     const AudioCodecs& audio_codecs,
-    const RtpHeaderExtensions& default_audio_rtp_header_extensions,
+    const RtpHeaderExtensions& rtp_header_extensions,
     StreamParamsVec* current_streams,
     SessionDescription* answer,
     IceCredentialsIterator* ice_credentials) const {
@@ -2679,7 +2695,7 @@
   if (!CreateMediaContentAnswer(
           offer_audio_description, media_description_options, session_options,
           sdes_policy, GetCryptos(current_content),
-          filtered_rtp_header_extensions(default_audio_rtp_header_extensions),
+          filtered_rtp_header_extensions(rtp_header_extensions),
           ssrc_generator(), enable_encrypted_rtp_header_extensions_,
           current_streams, bundle_enabled, audio_answer.get())) {
     return false;  // Fails the session setup.