Extend test::FunctionVideoDecoderFactory to propagate Environment

To reduce number calls to the CreateVideoDecoder

Bug: webrtc:15791
Change-Id: I5d6ecc2e5e68165d4e012b3ad7edb6eaa40e1913
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/336420
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41706}
diff --git a/api/test/video/BUILD.gn b/api/test/video/BUILD.gn
index 0eae85a..cf6dd59 100644
--- a/api/test/video/BUILD.gn
+++ b/api/test/video/BUILD.gn
@@ -18,6 +18,7 @@
 
   deps = [
     "../../../rtc_base:checks",
+    "../../environment",
     "../../video_codecs:video_codecs_api",
   ]
 }
diff --git a/api/test/video/function_video_decoder_factory.h b/api/test/video/function_video_decoder_factory.h
index 2145c71..2f2eeb5 100644
--- a/api/test/video/function_video_decoder_factory.h
+++ b/api/test/video/function_video_decoder_factory.h
@@ -16,6 +16,7 @@
 #include <utility>
 #include <vector>
 
+#include "api/environment/environment.h"
 #include "api/video_codecs/sdp_video_format.h"
 #include "api/video_codecs/video_decoder.h"
 #include "api/video_codecs/video_decoder_factory.h"
@@ -29,17 +30,20 @@
  public:
   explicit FunctionVideoDecoderFactory(
       std::function<std::unique_ptr<VideoDecoder>()> create)
-      : create_([create = std::move(create)](const SdpVideoFormat&) {
+      : create_([create = std::move(create)](const Environment&,
+                                             const SdpVideoFormat&) {
           return create();
         }) {}
   explicit FunctionVideoDecoderFactory(
-      std::function<std::unique_ptr<VideoDecoder>(const SdpVideoFormat&)>
+      std::function<std::unique_ptr<VideoDecoder>(const Environment&,
+                                                  const SdpVideoFormat&)>
           create)
       : create_(std::move(create)) {}
   FunctionVideoDecoderFactory(
       std::function<std::unique_ptr<VideoDecoder>()> create,
       std::vector<SdpVideoFormat> sdp_video_formats)
-      : create_([create = std::move(create)](const SdpVideoFormat&) {
+      : create_([create = std::move(create)](const Environment&,
+                                             const SdpVideoFormat&) {
           return create();
         }),
         sdp_video_formats_(std::move(sdp_video_formats)) {}
@@ -48,13 +52,14 @@
     return sdp_video_formats_;
   }
 
-  std::unique_ptr<VideoDecoder> CreateVideoDecoder(
-      const SdpVideoFormat& format) override {
-    return create_(format);
+  std::unique_ptr<VideoDecoder> Create(const Environment& env,
+                                       const SdpVideoFormat& format) override {
+    return create_(env, format);
   }
 
  private:
-  const std::function<std::unique_ptr<VideoDecoder>(const SdpVideoFormat&)>
+  const std::function<std::unique_ptr<VideoDecoder>(const Environment& env,
+                                                    const SdpVideoFormat&)>
       create_;
   const std::vector<SdpVideoFormat> sdp_video_formats_;
 };
diff --git a/modules/video_coding/BUILD.gn b/modules/video_coding/BUILD.gn
index a31d759..5256c02 100644
--- a/modules/video_coding/BUILD.gn
+++ b/modules/video_coding/BUILD.gn
@@ -820,6 +820,8 @@
       "../../api:mock_video_decoder",
       "../../api:mock_video_encoder",
       "../../api:simulcast_test_fixture_api",
+      "../../api/environment",
+      "../../api/environment:environment_factory",
       "../../api/video:encoded_image",
       "../../api/video:video_frame",
       "../../api/video:video_rtp_headers",
diff --git a/modules/video_coding/utility/simulcast_test_fixture_impl.cc b/modules/video_coding/utility/simulcast_test_fixture_impl.cc
index c6e51e8..ac076fd 100644
--- a/modules/video_coding/utility/simulcast_test_fixture_impl.cc
+++ b/modules/video_coding/utility/simulcast_test_fixture_impl.cc
@@ -15,6 +15,8 @@
 #include <memory>
 #include <vector>
 
+#include "api/environment/environment.h"
+#include "api/environment/environment_factory.h"
 #include "api/video/encoded_image.h"
 #include "api/video_codecs/sdp_video_format.h"
 #include "api/video_codecs/video_encoder.h"
@@ -258,8 +260,9 @@
     std::unique_ptr<VideoDecoderFactory> decoder_factory,
     SdpVideoFormat video_format)
     : codec_type_(PayloadStringToCodecType(video_format.name)) {
+  Environment env = CreateEnvironment();
   encoder_ = encoder_factory->CreateVideoEncoder(video_format);
-  decoder_ = decoder_factory->CreateVideoDecoder(video_format);
+  decoder_ = decoder_factory->Create(env, video_format);
   SetUpCodec((codec_type_ == kVideoCodecVP8 || codec_type_ == kVideoCodecH264)
                  ? kDefaultTemporalLayerProfile
                  : kNoTemporalLayerProfile);
diff --git a/video/BUILD.gn b/video/BUILD.gn
index 5a114c5..e4e1f27 100644
--- a/video/BUILD.gn
+++ b/video/BUILD.gn
@@ -519,6 +519,7 @@
         "../api:rtc_event_log_output_file",
         "../api:test_dependency_factory",
         "../api:video_quality_test_fixture_api",
+        "../api/environment",
         "../api/numerics",
         "../api/rtc_event_log:rtc_event_log_factory",
         "../api/task_queue",
diff --git a/video/end_to_end_tests/multi_codec_receive_tests.cc b/video/end_to_end_tests/multi_codec_receive_tests.cc
index 307b508..e236af0 100644
--- a/video/end_to_end_tests/multi_codec_receive_tests.cc
+++ b/video/end_to_end_tests/multi_codec_receive_tests.cc
@@ -213,7 +213,8 @@
         return nullptr;
       });
   test::FunctionVideoDecoderFactory decoder_factory(
-      [](const SdpVideoFormat& format) -> std::unique_ptr<VideoDecoder> {
+      [](const Environment& env,
+         const SdpVideoFormat& format) -> std::unique_ptr<VideoDecoder> {
         if (format.name == "VP8") {
           return VP8Decoder::Create();
         }
diff --git a/video/video_quality_test.cc b/video/video_quality_test.cc
index 99b7223..ebfc241 100644
--- a/video/video_quality_test.cc
+++ b/video/video_quality_test.cc
@@ -294,6 +294,7 @@
 }  // namespace
 
 std::unique_ptr<VideoDecoder> VideoQualityTest::CreateVideoDecoder(
+    const Environment& env,
     const SdpVideoFormat& format) {
   std::unique_ptr<VideoDecoder> decoder;
   if (format.name == "multiplex") {
@@ -302,7 +303,7 @@
   } else if (format.name == "FakeCodec") {
     decoder = webrtc::FakeVideoDecoderFactory::CreateVideoDecoder();
   } else {
-    decoder = decoder_factory_->CreateVideoDecoder(format);
+    decoder = decoder_factory_->Create(env, format);
   }
   if (!params_.logging.encoded_frame_base_path.empty()) {
     rtc::StringBuilder str;
@@ -375,9 +376,10 @@
     std::unique_ptr<InjectionComponents> injection_components)
     : clock_(Clock::GetRealTimeClock()),
       task_queue_factory_(CreateDefaultTaskQueueFactory()),
-      video_decoder_factory_([this](const SdpVideoFormat& format) {
-        return this->CreateVideoDecoder(format);
-      }),
+      video_decoder_factory_(
+          [this](const Environment& env, const SdpVideoFormat& format) {
+            return this->CreateVideoDecoder(env, format);
+          }),
       video_encoder_factory_([this](const SdpVideoFormat& format) {
         return this->CreateVideoEncoder(format, nullptr);
       }),
diff --git a/video/video_quality_test.h b/video/video_quality_test.h
index 63fa481..c5e63dd 100644
--- a/video/video_quality_test.h
+++ b/video/video_quality_test.h
@@ -15,6 +15,7 @@
 #include <string>
 #include <vector>
 
+#include "api/environment/environment.h"
 #include "api/fec_controller.h"
 #include "api/rtc_event_log/rtc_event_log_factory.h"
 #include "api/task_queue/task_queue_base.h"
@@ -79,6 +80,7 @@
       size_t video_idx);
   void SetupThumbnailCapturers(size_t num_thumbnail_streams);
   std::unique_ptr<VideoDecoder> CreateVideoDecoder(
+      const Environment& env,
       const SdpVideoFormat& format);
   std::unique_ptr<VideoEncoder> CreateVideoEncoder(const SdpVideoFormat& format,
                                                    VideoAnalyzer* analyzer);