WebRTC-DeprecateGlobalFieldTrialString/Enabled/ - part 7/inf

Convert audio/ and collateral (audio encoder copy red).

Bug: webrtc:10335
Change-Id: Iac54c0cfd2f62f4402f3deec35ae2725ec35b81a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/255820
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36229}
diff --git a/audio/BUILD.gn b/audio/BUILD.gn
index b31ab04..dcafe3b 100644
--- a/audio/BUILD.gn
+++ b/audio/BUILD.gn
@@ -49,6 +49,7 @@
     "../api:scoped_refptr",
     "../api:sequence_checker",
     "../api:transport_api",
+    "../api:webrtc_key_value_config",
     "../api/audio:aec3_factory",
     "../api/audio:audio_frame_api",
     "../api/audio:audio_frame_processor",
@@ -190,6 +191,7 @@
       "../test:mock_transformable_frame",
       "../test:mock_transport",
       "../test:rtp_test_utils",
+      "../test:scoped_key_value_config",
       "../test:test_common",
       "../test:test_support",
       "utility:utility_tests",
diff --git a/audio/audio_send_stream.cc b/audio/audio_send_stream.cc
index 08bb4e6..6aa0ec9 100644
--- a/audio/audio_send_stream.cc
+++ b/audio/audio_send_stream.cc
@@ -39,7 +39,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/strings/audio_format_to_string.h"
 #include "rtc_base/task_queue.h"
-#include "system_wrappers/include/field_trial.h"
 
 namespace webrtc {
 namespace {
@@ -88,8 +87,9 @@
       "rate_prio", &bitrate_priority);
 }
 
-AudioAllocationConfig::AudioAllocationConfig() {
-  Parser()->Parse(field_trial::FindFullName(kKey));
+AudioAllocationConfig::AudioAllocationConfig(
+    const WebRtcKeyValueConfig& field_trials) {
+  Parser()->Parse(field_trials.Lookup(kKey));
   if (priority_bitrate_raw && !priority_bitrate.IsZero()) {
     RTC_LOG(LS_WARNING) << "'priority_bitrate' and '_raw' are mutually "
                            "exclusive but both were configured.";
@@ -106,28 +106,31 @@
     BitrateAllocatorInterface* bitrate_allocator,
     RtcEventLog* event_log,
     RtcpRttStats* rtcp_rtt_stats,
-    const absl::optional<RtpState>& suspended_rtp_state)
-    : AudioSendStream(clock,
-                      config,
-                      audio_state,
-                      task_queue_factory,
-                      rtp_transport,
-                      bitrate_allocator,
-                      event_log,
-                      suspended_rtp_state,
-                      voe::CreateChannelSend(
-                          clock,
-                          task_queue_factory,
-                          config.send_transport,
-                          rtcp_rtt_stats,
-                          event_log,
-                          config.frame_encryptor,
-                          config.crypto_options,
-                          config.rtp.extmap_allow_mixed,
-                          config.rtcp_report_interval_ms,
-                          config.rtp.ssrc,
-                          config.frame_transformer,
-                          rtp_transport->transport_feedback_observer())) {}
+    const absl::optional<RtpState>& suspended_rtp_state,
+    const WebRtcKeyValueConfig& field_trials)
+    : AudioSendStream(
+          clock,
+          config,
+          audio_state,
+          task_queue_factory,
+          rtp_transport,
+          bitrate_allocator,
+          event_log,
+          suspended_rtp_state,
+          voe::CreateChannelSend(clock,
+                                 task_queue_factory,
+                                 config.send_transport,
+                                 rtcp_rtt_stats,
+                                 event_log,
+                                 config.frame_encryptor,
+                                 config.crypto_options,
+                                 config.rtp.extmap_allow_mixed,
+                                 config.rtcp_report_interval_ms,
+                                 config.rtp.ssrc,
+                                 config.frame_transformer,
+                                 rtp_transport->transport_feedback_observer(),
+                                 field_trials),
+          field_trials) {}
 
 AudioSendStream::AudioSendStream(
     Clock* clock,
@@ -138,21 +141,24 @@
     BitrateAllocatorInterface* bitrate_allocator,
     RtcEventLog* event_log,
     const absl::optional<RtpState>& suspended_rtp_state,
-    std::unique_ptr<voe::ChannelSendInterface> channel_send)
+    std::unique_ptr<voe::ChannelSendInterface> channel_send,
+    const WebRtcKeyValueConfig& field_trials)
     : clock_(clock),
+      field_trials_(field_trials),
       rtp_transport_queue_(rtp_transport->GetWorkerQueue()),
       allocate_audio_without_feedback_(
-          field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC")),
+          field_trials_.IsEnabled("WebRTC-Audio-ABWENoTWCC")),
       enable_audio_alr_probing_(
-          !field_trial::IsDisabled("WebRTC-Audio-AlrProbing")),
+          !field_trials_.IsDisabled("WebRTC-Audio-AlrProbing")),
       send_side_bwe_with_overhead_(
-          !field_trial::IsDisabled("WebRTC-SendSideBwe-WithOverhead")),
+          !field_trials_.IsDisabled("WebRTC-SendSideBwe-WithOverhead")),
+      allocation_settings_(field_trials_),
       config_(Config(/*send_transport=*/nullptr)),
       audio_state_(audio_state),
       channel_send_(std::move(channel_send)),
       event_log_(event_log),
       use_legacy_overhead_calculation_(
-          field_trial::IsEnabled("WebRTC-Audio-LegacyOverhead")),
+          field_trials_.IsEnabled("WebRTC-Audio-LegacyOverhead")),
       bitrate_allocator_(bitrate_allocator),
       rtp_transport_(rtp_transport),
       rtp_rtcp_module_(channel_send_->GetRtpRtcp()),
@@ -640,7 +646,8 @@
     AudioEncoderCopyRed::Config red_config;
     red_config.payload_type = *spec.red_payload_type;
     red_config.speech_encoder = std::move(encoder);
-    encoder = std::make_unique<AudioEncoderCopyRed>(std::move(red_config));
+    encoder = std::make_unique<AudioEncoderCopyRed>(std::move(red_config),
+                                                    field_trials_);
   }
 
   // Set currently known overhead (used in ANA, opus only).
diff --git a/audio/audio_send_stream.h b/audio/audio_send_stream.h
index b407508..ad40a89 100644
--- a/audio/audio_send_stream.h
+++ b/audio/audio_send_stream.h
@@ -16,6 +16,7 @@
 #include <vector>
 
 #include "api/sequence_checker.h"
+#include "api/webrtc_key_value_config.h"
 #include "audio/audio_level.h"
 #include "audio/channel_send.h"
 #include "call/audio_send_stream.h"
@@ -46,7 +47,7 @@
   absl::optional<double> bitrate_priority;
 
   std::unique_ptr<StructParametersParser> Parser();
-  AudioAllocationConfig();
+  explicit AudioAllocationConfig(const WebRtcKeyValueConfig& field_trials);
 };
 namespace internal {
 class AudioState;
@@ -62,7 +63,8 @@
                   BitrateAllocatorInterface* bitrate_allocator,
                   RtcEventLog* event_log,
                   RtcpRttStats* rtcp_rtt_stats,
-                  const absl::optional<RtpState>& suspended_rtp_state);
+                  const absl::optional<RtpState>& suspended_rtp_state,
+                  const WebRtcKeyValueConfig& field_trials);
   // For unit tests, which need to supply a mock ChannelSend.
   AudioSendStream(Clock* clock,
                   const webrtc::AudioSendStream::Config& config,
@@ -72,7 +74,8 @@
                   BitrateAllocatorInterface* bitrate_allocator,
                   RtcEventLog* event_log,
                   const absl::optional<RtpState>& suspended_rtp_state,
-                  std::unique_ptr<voe::ChannelSendInterface> channel_send);
+                  std::unique_ptr<voe::ChannelSendInterface> channel_send,
+                  const WebRtcKeyValueConfig& field_trials);
 
   AudioSendStream() = delete;
   AudioSendStream(const AudioSendStream&) = delete;
@@ -160,6 +163,7 @@
       RTC_RUN_ON(worker_thread_checker_);
 
   Clock* clock_;
+  const WebRtcKeyValueConfig& field_trials_;
 
   SequenceChecker worker_thread_checker_;
   SequenceChecker pacer_thread_checker_;
diff --git a/audio/audio_send_stream_unittest.cc b/audio/audio_send_stream_unittest.cc
index 9228611..2d2e64c 100644
--- a/audio/audio_send_stream_unittest.cc
+++ b/audio/audio_send_stream_unittest.cc
@@ -32,10 +32,10 @@
 #include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
 #include "rtc_base/task_queue_for_test.h"
 #include "system_wrappers/include/clock.h"
-#include "test/field_trial.h"
 #include "test/gtest.h"
 #include "test/mock_audio_encoder.h"
 #include "test/mock_audio_encoder_factory.h"
+#include "test/scoped_key_value_config.h"
 
 namespace webrtc {
 namespace test {
@@ -196,7 +196,8 @@
             Clock::GetRealTimeClock(), stream_config_, audio_state_,
             task_queue_factory_.get(), &rtp_transport_, &bitrate_allocator_,
             &event_log_, absl::nullopt,
-            std::unique_ptr<voe::ChannelSendInterface>(channel_send_)));
+            std::unique_ptr<voe::ChannelSendInterface>(channel_send_),
+            field_trials));
   }
 
   AudioSendStream::Config& config() { return stream_config_; }
@@ -321,6 +322,8 @@
 
   TaskQueueForTest* worker() { return &worker_queue_; }
 
+  test::ScopedKeyValueConfig field_trials;
+
  private:
   SimulatedClock clock_;
   std::unique_ptr<TaskQueueFactory> task_queue_factory_;
@@ -659,10 +662,10 @@
 }
 
 TEST(AudioSendStreamTest, SSBweFieldTrialMinRespected) {
-  ScopedFieldTrials field_trials(
-      "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
   for (bool use_null_audio_processing : {false, true}) {
     ConfigHelper helper(true, true, use_null_audio_processing);
+    ScopedKeyValueConfig field_trials(
+        helper.field_trials, "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
     auto send_stream = helper.CreateAudioSendStream();
     EXPECT_CALL(
         *helper.channel_send(),
@@ -676,10 +679,10 @@
 }
 
 TEST(AudioSendStreamTest, SSBweFieldTrialMaxRespected) {
-  ScopedFieldTrials field_trials(
-      "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
   for (bool use_null_audio_processing : {false, true}) {
     ConfigHelper helper(true, true, use_null_audio_processing);
+    ScopedKeyValueConfig field_trials(
+        helper.field_trials, "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
     auto send_stream = helper.CreateAudioSendStream();
     EXPECT_CALL(
         *helper.channel_send(),
@@ -693,10 +696,10 @@
 }
 
 TEST(AudioSendStreamTest, SSBweWithOverhead) {
-  ScopedFieldTrials field_trials(
-      "WebRTC-Audio-LegacyOverhead/Disabled/");
   for (bool use_null_audio_processing : {false, true}) {
     ConfigHelper helper(true, true, use_null_audio_processing);
+    ScopedKeyValueConfig field_trials(helper.field_trials,
+                                      "WebRTC-Audio-LegacyOverhead/Disabled/");
     EXPECT_CALL(*helper.rtp_rtcp(), ExpectedPerPacketOverhead)
         .WillRepeatedly(Return(kOverheadPerPacket.bytes<size_t>()));
     auto send_stream = helper.CreateAudioSendStream();
@@ -714,11 +717,12 @@
 }
 
 TEST(AudioSendStreamTest, SSBweWithOverheadMinRespected) {
-  ScopedFieldTrials field_trials(
-      "WebRTC-Audio-LegacyOverhead/Disabled/"
-      "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
   for (bool use_null_audio_processing : {false, true}) {
     ConfigHelper helper(true, true, use_null_audio_processing);
+    ScopedKeyValueConfig field_trials(
+        helper.field_trials,
+        "WebRTC-Audio-LegacyOverhead/Disabled/"
+        "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
     EXPECT_CALL(*helper.rtp_rtcp(), ExpectedPerPacketOverhead)
         .WillRepeatedly(Return(kOverheadPerPacket.bytes<size_t>()));
     auto send_stream = helper.CreateAudioSendStream();
@@ -734,11 +738,12 @@
 }
 
 TEST(AudioSendStreamTest, SSBweWithOverheadMaxRespected) {
-  ScopedFieldTrials field_trials(
-      "WebRTC-Audio-LegacyOverhead/Disabled/"
-      "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
   for (bool use_null_audio_processing : {false, true}) {
     ConfigHelper helper(true, true, use_null_audio_processing);
+    ScopedKeyValueConfig field_trials(
+        helper.field_trials,
+        "WebRTC-Audio-LegacyOverhead/Disabled/"
+        "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
     EXPECT_CALL(*helper.rtp_rtcp(), ExpectedPerPacketOverhead)
         .WillRepeatedly(Return(kOverheadPerPacket.bytes<size_t>()));
     auto send_stream = helper.CreateAudioSendStream();
diff --git a/audio/channel_send.cc b/audio/channel_send.cc
index d1135e5..d6b5823 100644
--- a/audio/channel_send.cc
+++ b/audio/channel_send.cc
@@ -43,7 +43,6 @@
 #include "rtc_base/task_queue.h"
 #include "rtc_base/time_utils.h"
 #include "system_wrappers/include/clock.h"
-#include "system_wrappers/include/field_trial.h"
 #include "system_wrappers/include/metrics.h"
 
 namespace webrtc {
@@ -78,7 +77,8 @@
               int rtcp_report_interval_ms,
               uint32_t ssrc,
               rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
-              TransportFeedbackObserver* feedback_observer);
+              TransportFeedbackObserver* feedback_observer,
+              const WebRtcKeyValueConfig& field_trials);
 
   ~ChannelSend() override;
 
@@ -458,7 +458,8 @@
     int rtcp_report_interval_ms,
     uint32_t ssrc,
     rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
-    TransportFeedbackObserver* feedback_observer)
+    TransportFeedbackObserver* feedback_observer,
+    const WebRtcKeyValueConfig& field_trials)
     : ssrc_(ssrc),
       event_log_(rtc_event_log),
       _timeStamp(0),  // This is just an offset, RTP module will add it's own
@@ -477,7 +478,7 @@
           "AudioEncoder",
           TaskQueueFactory::Priority::NORMAL)),
       fixing_timestamp_stall_(
-          !field_trial::IsDisabled("WebRTC-Audio-FixTimestampStall")) {
+          field_trials.IsDisabled("WebRTC-Audio-FixTimestampStall")) {
   audio_coding_.reset(AudioCodingModule::Create(AudioCodingModule::Config()));
 
   RtpRtcpInterface::Configuration configuration;
@@ -948,12 +949,13 @@
     int rtcp_report_interval_ms,
     uint32_t ssrc,
     rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
-    TransportFeedbackObserver* feedback_observer) {
+    TransportFeedbackObserver* feedback_observer,
+    const WebRtcKeyValueConfig& field_trials) {
   return std::make_unique<ChannelSend>(
       clock, task_queue_factory, rtp_transport, rtcp_rtt_stats, rtc_event_log,
       frame_encryptor, crypto_options, extmap_allow_mixed,
       rtcp_report_interval_ms, ssrc, std::move(frame_transformer),
-      feedback_observer);
+      feedback_observer, field_trials);
 }
 
 }  // namespace voe
diff --git a/audio/channel_send.h b/audio/channel_send.h
index e100725..bfbfbee 100644
--- a/audio/channel_send.h
+++ b/audio/channel_send.h
@@ -21,6 +21,7 @@
 #include "api/frame_transformer_interface.h"
 #include "api/function_view.h"
 #include "api/task_queue/task_queue_factory.h"
+#include "api/webrtc_key_value_config.h"
 #include "modules/rtp_rtcp/include/report_block_data.h"
 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
 #include "modules/rtp_rtcp/source/rtp_sender_audio.h"
@@ -135,7 +136,8 @@
     int rtcp_report_interval_ms,
     uint32_t ssrc,
     rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
-    TransportFeedbackObserver* feedback_observer);
+    TransportFeedbackObserver* feedback_observer,
+    const WebRtcKeyValueConfig& field_trials);
 
 }  // namespace voe
 }  // namespace webrtc
diff --git a/call/call.cc b/call/call.cc
index 16f7b90..f2101cf 100644
--- a/call/call.cc
+++ b/call/call.cc
@@ -915,7 +915,7 @@
   AudioSendStream* send_stream = new AudioSendStream(
       clock_, config, config_.audio_state, task_queue_factory_,
       transport_send_.get(), bitrate_allocator_.get(), event_log_,
-      call_stats_->AsRtcpRttStats(), suspended_rtp_state);
+      call_stats_->AsRtcpRttStats(), suspended_rtp_state, trials());
   RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) ==
              audio_send_ssrcs_.end());
   audio_send_ssrcs_[config.rtp.ssrc] = send_stream;
diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn
index c4814ce..af76a11 100644
--- a/modules/audio_coding/BUILD.gn
+++ b/modules/audio_coding/BUILD.gn
@@ -118,12 +118,12 @@
 
   deps = [
     "../../api:array_view",
+    "../../api:webrtc_key_value_config",
     "../../api/audio_codecs:audio_codecs_api",
     "../../api/units:time_delta",
     "../../common_audio",
     "../../rtc_base:checks",
     "../../rtc_base:rtc_base_approved",
-    "../../system_wrappers:field_trial",
   ]
   absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
 }
@@ -2079,6 +2079,7 @@
         "../../test:fileutils",
         "../../test:rtc_expect_death",
         "../../test:rtp_test_utils",
+        "../../test:scoped_key_value_config",
         "../../test:test_common",
         "../../test:test_support",
         "codecs/opus/test",
diff --git a/modules/audio_coding/codecs/red/audio_encoder_copy_red.cc b/modules/audio_coding/codecs/red/audio_encoder_copy_red.cc
index 9643c7b..c8a26e8 100644
--- a/modules/audio_coding/codecs/red/audio_encoder_copy_red.cc
+++ b/modules/audio_coding/codecs/red/audio_encoder_copy_red.cc
@@ -18,7 +18,6 @@
 #include "rtc_base/byte_order.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
-#include "system_wrappers/include/field_trial.h"
 
 namespace webrtc {
 static constexpr const int kRedMaxPacketSize =
@@ -40,9 +39,10 @@
 AudioEncoderCopyRed::Config::Config(Config&&) = default;
 AudioEncoderCopyRed::Config::~Config() = default;
 
-size_t GetMaxRedundancyFromFieldTrial() {
+size_t GetMaxRedundancyFromFieldTrial(
+    const WebRtcKeyValueConfig& field_trials) {
   const std::string red_trial =
-      webrtc::field_trial::FindFullName("WebRTC-Audio-Red-For-Opus");
+      field_trials.Lookup("WebRTC-Audio-Red-For-Opus");
   size_t redundancy = 0;
   if (sscanf(red_trial.c_str(), "Enabled-%zu", &redundancy) != 1 ||
       redundancy > 9) {
@@ -51,14 +51,17 @@
   return redundancy;
 }
 
-AudioEncoderCopyRed::AudioEncoderCopyRed(Config&& config)
+AudioEncoderCopyRed::AudioEncoderCopyRed(
+    Config&& config,
+    const WebRtcKeyValueConfig& field_trials)
     : speech_encoder_(std::move(config.speech_encoder)),
       primary_encoded_(0, kAudioMaxRtpPacketLen),
       max_packet_length_(kAudioMaxRtpPacketLen),
       red_payload_type_(config.payload_type) {
   RTC_CHECK(speech_encoder_) << "Speech encoder not provided.";
 
-  auto number_of_redundant_encodings = GetMaxRedundancyFromFieldTrial();
+  auto number_of_redundant_encodings =
+      GetMaxRedundancyFromFieldTrial(field_trials);
   for (size_t i = 0; i < number_of_redundant_encodings; i++) {
     std::pair<EncodedInfo, rtc::Buffer> redundant;
     redundant.second.EnsureCapacity(kAudioMaxRtpPacketLen);
diff --git a/modules/audio_coding/codecs/red/audio_encoder_copy_red.h b/modules/audio_coding/codecs/red/audio_encoder_copy_red.h
index d163193..e7471b3 100644
--- a/modules/audio_coding/codecs/red/audio_encoder_copy_red.h
+++ b/modules/audio_coding/codecs/red/audio_encoder_copy_red.h
@@ -22,6 +22,7 @@
 #include "api/array_view.h"
 #include "api/audio_codecs/audio_encoder.h"
 #include "api/units/time_delta.h"
+#include "api/webrtc_key_value_config.h"
 #include "rtc_base/buffer.h"
 
 namespace webrtc {
@@ -42,7 +43,8 @@
     std::unique_ptr<AudioEncoder> speech_encoder;
   };
 
-  explicit AudioEncoderCopyRed(Config&& config);
+  AudioEncoderCopyRed(Config&& config,
+                      const WebRtcKeyValueConfig& field_trials);
 
   ~AudioEncoderCopyRed() override;
 
diff --git a/modules/audio_coding/codecs/red/audio_encoder_copy_red_unittest.cc b/modules/audio_coding/codecs/red/audio_encoder_copy_red_unittest.cc
index 0eeac01..795a996 100644
--- a/modules/audio_coding/codecs/red/audio_encoder_copy_red_unittest.cc
+++ b/modules/audio_coding/codecs/red/audio_encoder_copy_red_unittest.cc
@@ -18,6 +18,7 @@
 #include "test/field_trial.h"
 #include "test/gtest.h"
 #include "test/mock_audio_encoder.h"
+#include "test/scoped_key_value_config.h"
 #include "test/testsupport/rtc_expect_death.h"
 
 using ::testing::_;
@@ -49,7 +50,7 @@
     AudioEncoderCopyRed::Config config;
     config.payload_type = red_payload_type_;
     config.speech_encoder = std::unique_ptr<AudioEncoder>(mock_encoder_);
-    red_.reset(new AudioEncoderCopyRed(std::move(config)));
+    red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials_));
     memset(audio_, 0, sizeof(audio_));
     EXPECT_CALL(*mock_encoder_, NumChannels()).WillRepeatedly(Return(1U));
     EXPECT_CALL(*mock_encoder_, SampleRateHz())
@@ -68,6 +69,7 @@
     timestamp_ += rtc::checked_cast<uint32_t>(num_audio_samples_10ms);
   }
 
+  test::ScopedKeyValueConfig field_trials_;
   MockAudioEncoder* mock_encoder_;
   std::unique_ptr<AudioEncoderCopyRed> red_;
   uint32_t timestamp_;
@@ -198,13 +200,13 @@
 // Checks that the correct payload sizes are populated into the redundancy
 // information for a redundancy level of 0.
 TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes0) {
-  webrtc::test::ScopedFieldTrials field_trials(
-      "WebRTC-Audio-Red-For-Opus/Enabled-0/");
+  webrtc::test::ScopedKeyValueConfig field_trials(
+      field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-0/");
   // Recreate the RED encoder to take the new field trial setting into account.
   AudioEncoderCopyRed::Config config;
   config.payload_type = red_payload_type_;
   config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
-  red_.reset(new AudioEncoderCopyRed(std::move(config)));
+  red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
 
   // Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
   // of calls.
@@ -224,13 +226,13 @@
 // Checks that the correct payload sizes are populated into the redundancy
 // information for a redundancy level of 2.
 TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes2) {
-  webrtc::test::ScopedFieldTrials field_trials(
-      "WebRTC-Audio-Red-For-Opus/Enabled-2/");
+  webrtc::test::ScopedKeyValueConfig field_trials(
+      field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-2/");
   // Recreate the RED encoder to take the new field trial setting into account.
   AudioEncoderCopyRed::Config config;
   config.payload_type = red_payload_type_;
   config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
-  red_.reset(new AudioEncoderCopyRed(std::move(config)));
+  red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
 
   // Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
   // of calls.
@@ -266,13 +268,13 @@
 // Checks that the correct payload sizes are populated into the redundancy
 // information for a redundancy level of 3.
 TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes3) {
-  webrtc::test::ScopedFieldTrials field_trials(
-      "WebRTC-Audio-Red-For-Opus/Enabled-3/");
+  webrtc::test::ScopedKeyValueConfig field_trials(
+      field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-3/");
   // Recreate the RED encoder to take the new field trial setting into account.
   AudioEncoderCopyRed::Config config;
   config.payload_type = red_payload_type_;
   config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
-  red_.reset(new AudioEncoderCopyRed(std::move(config)));
+  red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials_));
 
   // Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
   // of calls.
@@ -463,13 +465,13 @@
 
 // Variant with a redundancy of 0.
 TEST_F(AudioEncoderCopyRedTest, CheckRFC2198Header0) {
-  webrtc::test::ScopedFieldTrials field_trials(
-      "WebRTC-Audio-Red-For-Opus/Enabled-0/");
+  webrtc::test::ScopedKeyValueConfig field_trials(
+      field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-0/");
   // Recreate the RED encoder to take the new field trial setting into account.
   AudioEncoderCopyRed::Config config;
   config.payload_type = red_payload_type_;
   config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
-  red_.reset(new AudioEncoderCopyRed(std::move(config)));
+  red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
 
   const int primary_payload_type = red_payload_type_ + 1;
   AudioEncoder::EncodedInfo info;
@@ -491,13 +493,13 @@
 }
 // Variant with a redundancy of 2.
 TEST_F(AudioEncoderCopyRedTest, CheckRFC2198Header2) {
-  webrtc::test::ScopedFieldTrials field_trials(
-      "WebRTC-Audio-Red-For-Opus/Enabled-2/");
+  webrtc::test::ScopedKeyValueConfig field_trials(
+      field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-2/");
   // Recreate the RED encoder to take the new field trial setting into account.
   AudioEncoderCopyRed::Config config;
   config.payload_type = red_payload_type_;
   config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
-  red_.reset(new AudioEncoderCopyRed(std::move(config)));
+  red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
 
   const int primary_payload_type = red_payload_type_ + 1;
   AudioEncoder::EncodedInfo info;
@@ -623,11 +625,13 @@
 }
 
 TEST_F(AudioEncoderCopyRedDeathTest, NullSpeechEncoder) {
+  test::ScopedKeyValueConfig field_trials;
   AudioEncoderCopyRed* red = NULL;
   AudioEncoderCopyRed::Config config;
   config.speech_encoder = NULL;
-  RTC_EXPECT_DEATH(red = new AudioEncoderCopyRed(std::move(config)),
-                   "Speech encoder not provided.");
+  RTC_EXPECT_DEATH(
+      red = new AudioEncoderCopyRed(std::move(config), field_trials),
+      "Speech encoder not provided.");
   // The delete operation is needed to avoid leak reports from memcheck.
   delete red;
 }
diff --git a/modules/audio_coding/test/TestRedFec.cc b/modules/audio_coding/test/TestRedFec.cc
index d2c8d8a..892fbc8 100644
--- a/modules/audio_coding/test/TestRedFec.cc
+++ b/modules/audio_coding/test/TestRedFec.cc
@@ -190,7 +190,8 @@
       AudioEncoderCopyRed::Config config;
       config.payload_type = red_payload_type;
       config.speech_encoder = std::move(encoder);
-      encoder = std::make_unique<AudioEncoderCopyRed>(std::move(config));
+      encoder = std::make_unique<AudioEncoderCopyRed>(std::move(config),
+                                                      field_trials_);
       receive_codecs.emplace(
           std::make_pair(red_payload_type,
                          SdpAudioFormat("red", codec_format.clockrate_hz, 1)));
diff --git a/modules/audio_coding/test/TestRedFec.h b/modules/audio_coding/test/TestRedFec.h
index 0e92d27..dbadd88 100644
--- a/modules/audio_coding/test/TestRedFec.h
+++ b/modules/audio_coding/test/TestRedFec.h
@@ -19,6 +19,7 @@
 #include "common_audio/vad/include/vad.h"
 #include "modules/audio_coding/test/Channel.h"
 #include "modules/audio_coding/test/PCMFile.h"
+#include "test/scoped_key_value_config.h"
 
 namespace webrtc {
 
@@ -37,6 +38,7 @@
   void Run();
   void OpenOutFile(int16_t testNumber);
 
+  test::ScopedKeyValueConfig field_trials_;
   const rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_;
   const rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
   std::unique_ptr<AudioCodingModule> _acmA;