Propagate field trials to VideoEncoderSoftwareFallbackWrapper with Environment

Bug: webrtc:15860
Change-Id: Ief6a2eeab1713a371bc0350f6bdb5a18fb01945b
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/345660
Reviewed-by: Sergey Silkin <ssilkin@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42051}
diff --git a/api/video_codecs/BUILD.gn b/api/video_codecs/BUILD.gn
index bcf11cb..444a8fc 100644
--- a/api/video_codecs/BUILD.gn
+++ b/api/video_codecs/BUILD.gn
@@ -322,7 +322,6 @@
     "../../rtc_base:logging",
     "../../rtc_base/experiments:field_trial_parser",
     "../../rtc_base/system:rtc_export",
-    "../../system_wrappers:field_trial",
     "../../system_wrappers:metrics",
     "../video:encoded_image",
     "../video:video_bitrate_allocation",
diff --git a/api/video_codecs/test/video_encoder_software_fallback_wrapper_unittest.cc b/api/video_codecs/test/video_encoder_software_fallback_wrapper_unittest.cc
index 6a21ecf..9438b0d 100644
--- a/api/video_codecs/test/video_encoder_software_fallback_wrapper_unittest.cc
+++ b/api/video_codecs/test/video_encoder_software_fallback_wrapper_unittest.cc
@@ -18,6 +18,8 @@
 #include <vector>
 
 #include "absl/types/optional.h"
+#include "api/environment/environment.h"
+#include "api/environment/environment_factory.h"
 #include "api/fec_controller_override.h"
 #include "api/scoped_refptr.h"
 #include "api/test/mock_video_encoder.h"
@@ -34,18 +36,20 @@
 #include "modules/video_coding/include/video_error_codes.h"
 #include "modules/video_coding/utility/simulcast_rate_allocator.h"
 #include "rtc_base/fake_clock.h"
+#include "test/explicit_key_value_config.h"
 #include "test/fake_encoder.h"
 #include "test/fake_texture_frame.h"
-#include "test/field_trial.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 
 namespace webrtc {
+namespace {
+
+using test::ExplicitKeyValueConfig;
 using ::testing::_;
 using ::testing::Return;
 using ::testing::ValuesIn;
 
-namespace {
 const int kWidth = 320;
 const int kHeight = 240;
 const int kNumCores = 2;
@@ -87,12 +91,12 @@
 class VideoEncoderSoftwareFallbackWrapperTestBase : public ::testing::Test {
  protected:
   VideoEncoderSoftwareFallbackWrapperTestBase(
-      const std::string& field_trials,
+      const Environment& env,
       std::unique_ptr<VideoEncoder> sw_encoder)
-      : override_field_trials_(field_trials),
-        fake_encoder_(new CountingFakeEncoder()),
+      : fake_encoder_(new CountingFakeEncoder()),
         wrapper_initialized_(false),
         fallback_wrapper_(CreateVideoEncoderSoftwareFallbackWrapper(
+            env,
             std::move(sw_encoder),
             std::unique_ptr<VideoEncoder>(fake_encoder_),
             false)) {}
@@ -168,7 +172,6 @@
               fallback_wrapper_->GetEncoderInfo().implementation_name);
   }
 
-  test::ScopedFieldTrials override_field_trials_;
   FakeEncodedImageCallback callback_;
   // `fake_encoder_` is owned and released by `fallback_wrapper_`.
   CountingFakeEncoder* fake_encoder_;
@@ -188,7 +191,7 @@
   explicit VideoEncoderSoftwareFallbackWrapperTest(
       CountingFakeEncoder* fake_sw_encoder)
       : VideoEncoderSoftwareFallbackWrapperTestBase(
-            "",
+            CreateEnvironment(),
             std::unique_ptr<VideoEncoder>(fake_sw_encoder)),
         fake_sw_encoder_(fake_sw_encoder) {
     fake_sw_encoder_->implementation_name_ = "fake_sw_encoder";
@@ -485,9 +488,9 @@
 
 class ForcedFallbackTest : public VideoEncoderSoftwareFallbackWrapperTestBase {
  public:
-  explicit ForcedFallbackTest(const std::string& field_trials)
-      : VideoEncoderSoftwareFallbackWrapperTestBase(field_trials,
-                                                    VP8Encoder::Create()) {}
+  explicit ForcedFallbackTest(const Environment& env)
+      : VideoEncoderSoftwareFallbackWrapperTestBase(env,
+                                                    CreateVp8Encoder(env)) {}
 
   ~ForcedFallbackTest() override {}
 
@@ -550,15 +553,19 @@
 class ForcedFallbackTestEnabled : public ForcedFallbackTest {
  public:
   ForcedFallbackTestEnabled()
-      : ForcedFallbackTest(std::string(kFieldTrial) + "/Enabled-" +
-                           std::to_string(kMinPixelsPerFrame) + "," +
-                           std::to_string(kWidth * kHeight) + ",30000/") {}
+      : ForcedFallbackTest(
+            CreateEnvironment(std::make_unique<ExplicitKeyValueConfig>(
+                std::string(kFieldTrial) + "/Enabled-" +
+                std::to_string(kMinPixelsPerFrame) + "," +
+                std::to_string(kWidth * kHeight) + ",30000/"))) {}
 };
 
 class ForcedFallbackTestDisabled : public ForcedFallbackTest {
  public:
   ForcedFallbackTestDisabled()
-      : ForcedFallbackTest(std::string(kFieldTrial) + "/Disabled/") {}
+      : ForcedFallbackTest(
+            CreateEnvironment(std::make_unique<ExplicitKeyValueConfig>(
+                std::string(kFieldTrial) + "/Disabled/"))) {}
 };
 
 TEST_F(ForcedFallbackTestDisabled, NoFallbackWithoutFieldTrial) {
@@ -704,8 +711,9 @@
 
   std::unique_ptr<VideoEncoder> wrapper =
       CreateVideoEncoderSoftwareFallbackWrapper(
-          std::unique_ptr<VideoEncoder>(sw_encoder),
-          std::unique_ptr<VideoEncoder>(hw_encoder));
+          CreateEnvironment(), std::unique_ptr<VideoEncoder>(sw_encoder),
+          std::unique_ptr<VideoEncoder>(hw_encoder),
+          /*prefer_temporal_support=*/false);
   EXPECT_FALSE(wrapper->GetEncoderInfo().has_trusted_rate_controller);
 }
 
@@ -719,8 +727,9 @@
 
   std::unique_ptr<VideoEncoder> wrapper =
       CreateVideoEncoderSoftwareFallbackWrapper(
-          std::unique_ptr<VideoEncoder>(sw_encoder),
-          std::unique_ptr<VideoEncoder>(hw_encoder));
+          CreateEnvironment(), std::unique_ptr<VideoEncoder>(sw_encoder),
+          std::unique_ptr<VideoEncoder>(hw_encoder),
+          /*prefer_temporal_support=*/false);
   EXPECT_FALSE(wrapper->GetEncoderInfo().has_trusted_rate_controller);
 }
 
@@ -734,8 +743,9 @@
 
   std::unique_ptr<VideoEncoder> wrapper =
       CreateVideoEncoderSoftwareFallbackWrapper(
-          std::unique_ptr<VideoEncoder>(sw_encoder),
-          std::unique_ptr<VideoEncoder>(hw_encoder));
+          CreateEnvironment(), std::unique_ptr<VideoEncoder>(sw_encoder),
+          std::unique_ptr<VideoEncoder>(hw_encoder),
+          /*prefer_temporal_support=*/false);
   EXPECT_TRUE(wrapper->GetEncoderInfo().has_trusted_rate_controller);
 
   VideoCodec codec_ = {};
@@ -764,8 +774,9 @@
 
   std::unique_ptr<VideoEncoder> wrapper =
       CreateVideoEncoderSoftwareFallbackWrapper(
-          std::unique_ptr<VideoEncoder>(sw_encoder),
-          std::unique_ptr<VideoEncoder>(hw_encoder));
+          CreateEnvironment(), std::unique_ptr<VideoEncoder>(sw_encoder),
+          std::unique_ptr<VideoEncoder>(hw_encoder),
+          /*prefer_temporal_support=*/false);
   EXPECT_TRUE(wrapper->GetEncoderInfo().has_trusted_rate_controller);
 }
 
@@ -779,8 +790,9 @@
 
   std::unique_ptr<VideoEncoder> wrapper =
       CreateVideoEncoderSoftwareFallbackWrapper(
-          std::unique_ptr<VideoEncoder>(sw_encoder),
-          std::unique_ptr<VideoEncoder>(hw_encoder));
+          CreateEnvironment(), std::unique_ptr<VideoEncoder>(sw_encoder),
+          std::unique_ptr<VideoEncoder>(hw_encoder),
+          /*prefer_temporal_support=*/false);
   EXPECT_TRUE(wrapper->GetEncoderInfo().is_hardware_accelerated);
 
   VideoCodec codec_ = {};
@@ -808,8 +820,9 @@
 
   std::unique_ptr<VideoEncoder> wrapper =
       CreateVideoEncoderSoftwareFallbackWrapper(
-          std::unique_ptr<VideoEncoder>(sw_encoder),
-          std::unique_ptr<VideoEncoder>(hw_encoder));
+          CreateEnvironment(), std::unique_ptr<VideoEncoder>(sw_encoder),
+          std::unique_ptr<VideoEncoder>(hw_encoder),
+          /*prefer_temporal_support=*/false);
   EXPECT_TRUE(wrapper->GetEncoderInfo().is_hardware_accelerated);
 
   // Initialize the encoder. When HW attempt fails we fallback to SW.
@@ -850,7 +863,8 @@
         .WillRepeatedly(Return(WEBRTC_VIDEO_CODEC_OK));
 
     wrapper_ = CreateVideoEncoderSoftwareFallbackWrapper(
-        std::unique_ptr<VideoEncoder>(sw_), std::unique_ptr<VideoEncoder>(hw_),
+        CreateEnvironment(), std::unique_ptr<VideoEncoder>(sw_),
+        std::unique_ptr<VideoEncoder>(hw_),
         /*prefer_temporal_support=*/true);
 
     codec_settings.codecType = kVideoCodecVP8;
@@ -1123,15 +1137,15 @@
 
 TEST_P(ResolutionBasedFallbackTest, VerifyForcedEncoderFallback) {
   const ResolutionBasedFallbackTestParams& params = GetParam();
-  test::ScopedFieldTrials field_trials(params.field_trials);
-  auto primary = new test::FakeEncoder(Clock::GetRealTimeClock());
-  auto fallback = new test::FakeEncoder(Clock::GetRealTimeClock());
-  auto encoder = CreateVideoEncoderSoftwareFallbackWrapper(
-      std::unique_ptr<VideoEncoder>(fallback),
-      std::unique_ptr<VideoEncoder>(primary),
-      /*prefer_temporal_support=*/false);
+  const Environment env = CreateEnvironment(
+      std::make_unique<ExplicitKeyValueConfig>(params.field_trials));
+  auto primary = std::make_unique<test::FakeEncoder>(env);
   primary->SetImplementationName("primary");
+  auto fallback = std::make_unique<test::FakeEncoder>(env);
   fallback->SetImplementationName("fallback");
+  auto encoder = CreateVideoEncoderSoftwareFallbackWrapper(
+      env, std::move(fallback), std::move(primary),
+      /*prefer_temporal_support=*/false);
   VideoCodec codec;
   codec.codecType = params.codec_type;
   codec.width = params.width;
diff --git a/api/video_codecs/video_encoder_software_fallback_wrapper.cc b/api/video_codecs/video_encoder_software_fallback_wrapper.cc
index e50b508..00d7053 100644
--- a/api/video_codecs/video_encoder_software_fallback_wrapper.cc
+++ b/api/video_codecs/video_encoder_software_fallback_wrapper.cc
@@ -19,7 +19,9 @@
 
 #include "absl/strings/match.h"
 #include "absl/types/optional.h"
+#include "api/environment/environment.h"
 #include "api/fec_controller_override.h"
+#include "api/field_trials_view.h"
 #include "api/transport/field_trial_based_config.h"
 #include "api/video/i420_buffer.h"
 #include "api/video/video_bitrate_allocation.h"
@@ -33,7 +35,6 @@
 #include "rtc_base/checks.h"
 #include "rtc_base/experiments/field_trial_parser.h"
 #include "rtc_base/logging.h"
-#include "system_wrappers/include/field_trial.h"
 
 namespace webrtc {
 
@@ -81,13 +82,13 @@
     "WebRTC-VP8-Forced-Fallback-Encoder-v2";
 
 absl::optional<ForcedFallbackParams> ParseFallbackParamsFromFieldTrials(
+    const FieldTrialsView& field_trials,
     const VideoEncoder& main_encoder) {
   // Ignore WebRTC-VP8-Forced-Fallback-Encoder-v2 if
   // WebRTC-Video-EncoderFallbackSettings is present.
   FieldTrialOptional<int> resolution_threshold_px("resolution_threshold_px");
-  ParseFieldTrial(
-      {&resolution_threshold_px},
-      FieldTrialBasedConfig().Lookup("WebRTC-Video-EncoderFallbackSettings"));
+  ParseFieldTrial({&resolution_threshold_px},
+                  field_trials.Lookup("WebRTC-Video-EncoderFallbackSettings"));
   if (resolution_threshold_px) {
     ForcedFallbackParams params;
     params.enable_resolution_based_switch = true;
@@ -96,7 +97,7 @@
   }
 
   const std::string field_trial =
-      webrtc::field_trial::FindFullName(kVp8ForceFallbackEncoderFieldTrial);
+      field_trials.Lookup(kVp8ForceFallbackEncoderFieldTrial);
   if (!absl::StartsWith(field_trial, "Enabled")) {
     return absl::nullopt;
   }
@@ -125,10 +126,11 @@
 }
 
 absl::optional<ForcedFallbackParams> GetForcedFallbackParams(
+    const FieldTrialsView& field_trials,
     bool prefer_temporal_support,
     const VideoEncoder& main_encoder) {
   absl::optional<ForcedFallbackParams> params =
-      ParseFallbackParamsFromFieldTrials(main_encoder);
+      ParseFallbackParamsFromFieldTrials(field_trials, main_encoder);
   if (prefer_temporal_support) {
     if (!params.has_value()) {
       params.emplace();
@@ -141,6 +143,7 @@
 class VideoEncoderSoftwareFallbackWrapper final : public VideoEncoder {
  public:
   VideoEncoderSoftwareFallbackWrapper(
+      const FieldTrialsView& field_trials,
       std::unique_ptr<webrtc::VideoEncoder> sw_encoder,
       std::unique_ptr<webrtc::VideoEncoder> hw_encoder,
       bool prefer_temporal_support);
@@ -227,6 +230,7 @@
 };
 
 VideoEncoderSoftwareFallbackWrapper::VideoEncoderSoftwareFallbackWrapper(
+    const FieldTrialsView& field_trials,
     std::unique_ptr<webrtc::VideoEncoder> sw_encoder,
     std::unique_ptr<webrtc::VideoEncoder> hw_encoder,
     bool prefer_temporal_support)
@@ -234,8 +238,9 @@
       encoder_(std::move(hw_encoder)),
       fallback_encoder_(std::move(sw_encoder)),
       callback_(nullptr),
-      fallback_params_(
-          GetForcedFallbackParams(prefer_temporal_support, *encoder_)) {
+      fallback_params_(GetForcedFallbackParams(field_trials,
+                                               prefer_temporal_support,
+                                               *encoder_)) {
   RTC_DCHECK(fallback_encoder_);
 }
 
@@ -537,12 +542,22 @@
 }  // namespace
 
 std::unique_ptr<VideoEncoder> CreateVideoEncoderSoftwareFallbackWrapper(
+    const Environment& env,
     std::unique_ptr<VideoEncoder> sw_fallback_encoder,
     std::unique_ptr<VideoEncoder> hw_encoder,
     bool prefer_temporal_support) {
   return std::make_unique<VideoEncoderSoftwareFallbackWrapper>(
-      std::move(sw_fallback_encoder), std::move(hw_encoder),
+      env.field_trials(), std::move(sw_fallback_encoder), std::move(hw_encoder),
       prefer_temporal_support);
 }
 
+std::unique_ptr<VideoEncoder> CreateVideoEncoderSoftwareFallbackWrapper(
+    std::unique_ptr<VideoEncoder> sw_fallback_encoder,
+    std::unique_ptr<VideoEncoder> hw_encoder,
+    bool prefer_temporal_support) {
+  return std::make_unique<VideoEncoderSoftwareFallbackWrapper>(
+      FieldTrialBasedConfig(), std::move(sw_fallback_encoder),
+      std::move(hw_encoder), prefer_temporal_support);
+}
+
 }  // namespace webrtc
diff --git a/api/video_codecs/video_encoder_software_fallback_wrapper.h b/api/video_codecs/video_encoder_software_fallback_wrapper.h
index 6e6902e..45118e2 100644
--- a/api/video_codecs/video_encoder_software_fallback_wrapper.h
+++ b/api/video_codecs/video_encoder_software_fallback_wrapper.h
@@ -14,6 +14,7 @@
 #include <memory>
 #include <utility>
 
+#include "api/environment/environment.h"
 #include "api/video_codecs/video_encoder.h"
 #include "rtc_base/system/rtc_export.h"
 
@@ -25,22 +26,19 @@
 // |bool prefer_temporal_support| indicates that if the software fallback
 // encoder supports temporal layers but the hardware encoder does not, a
 // fallback should be forced even if the encoder otherwise works.
-RTC_EXPORT std::unique_ptr<VideoEncoder>
+// TODO: bugs.webrtc.org/15860 - Delete after 2024-04-22.
+[[deprecated]] RTC_EXPORT std::unique_ptr<VideoEncoder>
 CreateVideoEncoderSoftwareFallbackWrapper(
     std::unique_ptr<VideoEncoder> sw_fallback_encoder,
     std::unique_ptr<VideoEncoder> hw_encoder,
     bool prefer_temporal_support);
 
-// Default fallback for call-sites not yet updated with
-// `prefer_temporal_support`.
-// TODO(sprang): Remove when usage is gone.
-RTC_EXPORT inline std::unique_ptr<VideoEncoder>
+RTC_EXPORT std::unique_ptr<VideoEncoder>
 CreateVideoEncoderSoftwareFallbackWrapper(
+    const Environment& env,
     std::unique_ptr<VideoEncoder> sw_fallback_encoder,
-    std::unique_ptr<VideoEncoder> hw_encoder) {
-  return CreateVideoEncoderSoftwareFallbackWrapper(
-      std::move(sw_fallback_encoder), std::move(hw_encoder), false);
-}
+    std::unique_ptr<VideoEncoder> hw_encoder,
+    bool prefer_temporal_support);
 
 }  // namespace webrtc
 
diff --git a/media/engine/simulcast_encoder_adapter.cc b/media/engine/simulcast_encoder_adapter.cc
index f95ccb0..5ca2a03 100644
--- a/media/engine/simulcast_encoder_adapter.cc
+++ b/media/engine/simulcast_encoder_adapter.cc
@@ -756,7 +756,7 @@
         encoder = std::move(primary_encoder);
       } else {
         encoder = CreateVideoEncoderSoftwareFallbackWrapper(
-            std::move(fallback_encoder), std::move(primary_encoder),
+            env_, std::move(fallback_encoder), std::move(primary_encoder),
             prefer_temporal_support);
       }
     } else if (fallback_encoder != nullptr) {
diff --git a/sdk/android/src/jni/video_encoder_fallback.cc b/sdk/android/src/jni/video_encoder_fallback.cc
index dcca70a..fa1fbc9 100644
--- a/sdk/android/src/jni/video_encoder_fallback.cc
+++ b/sdk/android/src/jni/video_encoder_fallback.cc
@@ -29,11 +29,12 @@
   std::unique_ptr<VideoEncoder> primary_encoder =
       JavaToNativeVideoEncoder(jni, j_primary_encoder, j_webrtc_env_ref);
 
-  return NativeToJavaPointer(CreateVideoEncoderSoftwareFallbackWrapper(
-                                 std::move(fallback_encoder),
-                                 std::move(primary_encoder),
-                                 /*prefer_temporal_support=*/false)
-                                 .release());
+  return NativeToJavaPointer(
+      CreateVideoEncoderSoftwareFallbackWrapper(
+          *reinterpret_cast<const Environment*>(j_webrtc_env_ref),
+          std::move(fallback_encoder), std::move(primary_encoder),
+          /*prefer_temporal_support=*/false)
+          .release());
 }
 
 }  // namespace jni