red: enable opus-red by default

turning the current field trial into a killswitch.

Note that RED is not used by default since it is listed after opus in the SDP.
To enable RED for opus the setCodecPreferences can be used to change
the order of codecs.

BUG=webrtc:11640

Change-Id: I248f4340ca0a3f7c4ea6d6a41b566bc92ab6f19d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/228426
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Reviewed-by: Per Ã…hgren <peah@webrtc.org>
Commit-Queue: Henrik Lundin <henrik.lundin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34781}
diff --git a/media/engine/webrtc_voice_engine.cc b/media/engine/webrtc_voice_engine.cc
index cbc6abf..0cf17ed 100644
--- a/media/engine/webrtc_voice_engine.cc
+++ b/media/engine/webrtc_voice_engine.cc
@@ -210,6 +210,11 @@
   return absl::StartsWith(config.Lookup(trial), "Enabled");
 }
 
+bool IsDisabled(const webrtc::WebRtcKeyValueConfig& config,
+                absl::string_view trial) {
+  return absl::StartsWith(config.Lookup(trial), "Disabled");
+}
+
 struct AdaptivePtimeConfig {
   bool enabled = false;
   webrtc::DataRate min_payload_bitrate = webrtc::DataRate::KilobitsPerSec(16);
@@ -304,8 +309,8 @@
       audio_mixer_(audio_mixer),
       apm_(audio_processing),
       audio_frame_processor_(audio_frame_processor),
-      audio_red_for_opus_trial_enabled_(
-          IsEnabled(trials, "WebRTC-Audio-Red-For-Opus")),
+      audio_red_for_opus_enabled_(
+          !IsDisabled(trials, "WebRTC-Audio-Red-For-Opus")),
       minimized_remsampling_on_mobile_trial_enabled_(
           IsEnabled(trials, "WebRTC-Audio-MinimizeResamplingOnMobile")) {
   // This may be called from any thread, so detach thread checkers.
@@ -769,7 +774,7 @@
 
       out.push_back(codec);
 
-      if (codec.name == kOpusCodecName && audio_red_for_opus_trial_enabled_) {
+      if (codec.name == kOpusCodecName && audio_red_for_opus_enabled_) {
         map_format({kRedCodecName, 48000, 2}, &out);
       }
     }
@@ -1336,8 +1341,8 @@
       call_(call),
       audio_config_(config.audio),
       crypto_options_(crypto_options),
-      audio_red_for_opus_trial_enabled_(
-          IsEnabled(call->trials(), "WebRTC-Audio-Red-For-Opus")) {
+      audio_red_for_opus_enabled_(
+          !IsDisabled(call->trials(), "WebRTC-Audio-Red-For-Opus")) {
   network_thread_checker_.Detach();
   RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
   RTC_DCHECK(call);
@@ -1598,8 +1603,7 @@
     }
     auto format = AudioCodecToSdpAudioFormat(codec);
     if (!IsCodec(codec, kCnCodecName) && !IsCodec(codec, kDtmfCodecName) &&
-        (!audio_red_for_opus_trial_enabled_ ||
-         !IsCodec(codec, kRedCodecName)) &&
+        (!audio_red_for_opus_enabled_ || !IsCodec(codec, kRedCodecName)) &&
         !engine()->decoder_factory_->IsSupportedDecoder(format)) {
       RTC_LOG(LS_ERROR) << "Unsupported codec: " << rtc::ToString(format);
       return false;
@@ -1753,7 +1757,7 @@
     }
   }
 
-  if (audio_red_for_opus_trial_enabled_) {
+  if (audio_red_for_opus_enabled_) {
     // Loop through the codecs to find the RED codec that matches opus
     // with respect to clockrate and number of channels.
     size_t red_codec_position = 0;
diff --git a/media/engine/webrtc_voice_engine.h b/media/engine/webrtc_voice_engine.h
index 147688b..a7c2f39 100644
--- a/media/engine/webrtc_voice_engine.h
+++ b/media/engine/webrtc_voice_engine.h
@@ -133,9 +133,9 @@
   int audio_jitter_buffer_min_delay_ms_ = 0;
   bool audio_jitter_buffer_enable_rtx_handling_ = false;
 
-  // If this field trial is enabled, we will negotiate and use RFC 2198
+  // If this field is enabled, we will negotiate and use RFC 2198
   // redundancy for opus audio.
-  const bool audio_red_for_opus_trial_enabled_;
+  const bool audio_red_for_opus_enabled_;
   const bool minimized_remsampling_on_mobile_trial_enabled_;
 };
 
@@ -328,7 +328,7 @@
   rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
       unsignaled_frame_decryptor_;
 
-  const bool audio_red_for_opus_trial_enabled_;
+  const bool audio_red_for_opus_enabled_;
 };
 }  // namespace cricket
 
diff --git a/media/engine/webrtc_voice_engine_unittest.cc b/media/engine/webrtc_voice_engine_unittest.cc
index 1fd2480..4b127af 100644
--- a/media/engine/webrtc_voice_engine_unittest.cc
+++ b/media/engine/webrtc_voice_engine_unittest.cc
@@ -1022,11 +1022,8 @@
   EXPECT_TRUE(channel_->SetRecvParameters(parameters));
 }
 
-// Test that we set Opus/Red under the field trial.
-TEST_P(WebRtcVoiceEngineTestFake, RecvRed) {
-  webrtc::test::ScopedFieldTrials override_field_trials(
-      "WebRTC-Audio-Red-For-Opus/Enabled/");
-
+// Test that we do allow setting Opus/Red by default.
+TEST_P(WebRtcVoiceEngineTestFake, RecvRedDefault) {
   EXPECT_TRUE(SetupRecvStream());
   cricket::AudioRecvParameters parameters;
   parameters.codecs.push_back(kOpusCodec);
@@ -1037,8 +1034,11 @@
                   {{111, {"opus", 48000, 2}}, {112, {"red", 48000, 2}}})));
 }
 
-// Test that we do not allow setting Opus/Red by default.
-TEST_P(WebRtcVoiceEngineTestFake, RecvRedDefault) {
+// Test that we disable Opus/Red with the kill switch.
+TEST_P(WebRtcVoiceEngineTestFake, RecvRed) {
+  webrtc::test::ScopedFieldTrials override_field_trials(
+      "WebRTC-Audio-Red-For-Opus/Disabled/");
+
   EXPECT_TRUE(SetupRecvStream());
   cricket::AudioRecvParameters parameters;
   parameters.codecs.push_back(kOpusCodec);