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/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;