Propagate webrtc::Environment through objc VideoDecoderFactory

Bug: webrtc:15791
Change-Id: I9e9206c6e2f7be2d2d59f80241cafcc27b9e6ad6
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/339864
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41767}
diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn
index a044c46..206bd13 100644
--- a/sdk/BUILD.gn
+++ b/sdk/BUILD.gn
@@ -1173,6 +1173,7 @@
             "../api:scoped_refptr",
             "../api/audio_codecs:builtin_audio_decoder_factory",
             "../api/audio_codecs:builtin_audio_encoder_factory",
+            "../api/environment:environment_factory",
             "../api/task_queue:default_task_queue_factory",
             "../api/video:video_frame",
             "../api/video_codecs:video_codecs_api",
@@ -1539,6 +1540,9 @@
 
     rtc_library("wrapped_native_codec_objc") {
       sources = [
+        "objc/api/video_codec/RTCNativeVideoDecoder.h",
+        "objc/api/video_codec/RTCNativeVideoDecoder.mm",
+        "objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h",
         "objc/api/video_codec/RTCWrappedNativeVideoDecoder.h",
         "objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm",
         "objc/api/video_codec/RTCWrappedNativeVideoEncoder.h",
@@ -1551,6 +1555,7 @@
       deps = [
         ":base_objc",
         ":helpers_objc",
+        "../api/environment",
         "../api/video_codecs:video_codecs_api",
         "../media:codec",
         "../media:rtc_media_base",
@@ -1662,6 +1667,7 @@
         ":vpx_codec_constants",
         ":wrapped_native_codec_objc",
         "../api:make_ref_counted",
+        "../api/environment",
         "../api/video:video_frame",
         "../api/video:video_rtp_headers",
         "../api/video_codecs:video_codecs_api",
diff --git a/sdk/objc/api/video_codec/RTCNativeVideoDecoder.h b/sdk/objc/api/video_codec/RTCNativeVideoDecoder.h
new file mode 100644
index 0000000..c88642f
--- /dev/null
+++ b/sdk/objc/api/video_codec/RTCNativeVideoDecoder.h
@@ -0,0 +1,20 @@
+/*
+ *  Copyright (c) 2024 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "base/RTCMacros.h"
+#import "base/RTCVideoDecoder.h"
+
+// NativeVideoDecoder pretends to conform to RTCVideoDecoder protocol, but
+// expects its methods won't be called.
+@interface RTC_OBJC_TYPE (RTCNativeVideoDecoder) : NSObject <RTC_OBJC_TYPE (RTCVideoDecoder)>
+
+@end
diff --git a/sdk/objc/api/video_codec/RTCNativeVideoDecoder.mm b/sdk/objc/api/video_codec/RTCNativeVideoDecoder.mm
new file mode 100644
index 0000000..8ade15b
--- /dev/null
+++ b/sdk/objc/api/video_codec/RTCNativeVideoDecoder.mm
@@ -0,0 +1,47 @@
+/*
+ *  Copyright (c) 2024 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "RTCWrappedNativeVideoDecoder.h"
+#import "base/RTCMacros.h"
+#import "helpers/NSString+StdString.h"
+
+@implementation RTC_OBJC_TYPE (RTCNativeVideoDecoder)
+
+- (void)setCallback:(RTCVideoDecoderCallback)callback {
+  RTC_DCHECK_NOTREACHED();
+}
+
+- (NSInteger)startDecodeWithNumberOfCores:(int)numberOfCores {
+  RTC_DCHECK_NOTREACHED();
+  return 0;
+}
+
+- (NSInteger)releaseDecoder {
+  RTC_DCHECK_NOTREACHED();
+  return 0;
+}
+
+// TODO: bugs.webrtc.org/15444 - Remove obsolete missingFrames param.
+- (NSInteger)decode:(RTC_OBJC_TYPE(RTCEncodedImage) *)encodedImage
+        missingFrames:(BOOL)missingFrames
+    codecSpecificInfo:(nullable id<RTC_OBJC_TYPE(RTCCodecSpecificInfo)>)info
+         renderTimeMs:(int64_t)renderTimeMs {
+  RTC_DCHECK_NOTREACHED();
+  return 0;
+}
+
+- (NSString *)implementationName {
+  RTC_DCHECK_NOTREACHED();
+  return nil;
+}
+
+@end
diff --git a/sdk/objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h b/sdk/objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h
new file mode 100644
index 0000000..e19343d
--- /dev/null
+++ b/sdk/objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h
@@ -0,0 +1,25 @@
+/*
+ *  Copyright (c) 2024 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "base/RTCMacros.h"
+
+#include <memory>
+
+#include "api/environment/environment.h"
+#include "api/video_codecs/video_decoder.h"
+
+@protocol RTC_OBJC_TYPE
+(RTCNativeVideoDecoderBuilder)
+
+    - (std::unique_ptr<webrtc::VideoDecoder>)build : (const webrtc::Environment&)env;
+
+@end
diff --git a/sdk/objc/api/video_codec/RTCVideoDecoderVP8.mm b/sdk/objc/api/video_codec/RTCVideoDecoderVP8.mm
index c150cf6..66d3d0f 100644
--- a/sdk/objc/api/video_codec/RTCVideoDecoderVP8.mm
+++ b/sdk/objc/api/video_codec/RTCVideoDecoderVP8.mm
@@ -12,16 +12,28 @@
 #import <Foundation/Foundation.h>
 
 #import "RTCMacros.h"
+#import "RTCNativeVideoDecoder.h"
+#import "RTCNativeVideoDecoderBuilder+Native.h"
 #import "RTCVideoDecoderVP8.h"
-#import "RTCWrappedNativeVideoDecoder.h"
 
 #include "modules/video_coding/codecs/vp8/include/vp8.h"
 
-@implementation RTC_OBJC_TYPE (RTCVideoDecoderVP8)
-
-+ (id<RTC_OBJC_TYPE(RTCVideoDecoder)>)vp8Decoder {
-  return [[RTC_OBJC_TYPE(RTCWrappedNativeVideoDecoder) alloc]
-      initWithNativeDecoder:std::unique_ptr<webrtc::VideoDecoder>(webrtc::VP8Decoder::Create())];
-}
-
+@interface RTC_OBJC_TYPE (RTCVideoDecoderVP8Builder)
+    : RTC_OBJC_TYPE(RTCNativeVideoDecoder) <RTC_OBJC_TYPE (RTCNativeVideoDecoderBuilder)>
 @end
+
+    @implementation RTC_OBJC_TYPE (RTCVideoDecoderVP8Builder)
+
+    - (std::unique_ptr<webrtc::VideoDecoder>)build:(const webrtc::Environment&)env {
+      return webrtc::CreateVp8Decoder(env);
+    }
+
+    @end
+
+    @implementation RTC_OBJC_TYPE (RTCVideoDecoderVP8)
+
+    + (id<RTC_OBJC_TYPE(RTCVideoDecoder)>)vp8Decoder {
+      return [[RTC_OBJC_TYPE(RTCVideoDecoderVP8Builder) alloc] init];
+    }
+
+    @end
diff --git a/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h b/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h
index 3a9b39e..93ffa2f 100644
--- a/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h
+++ b/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h
@@ -10,13 +10,15 @@
 
 #import <Foundation/Foundation.h>
 
+#import "RTCNativeVideoDecoder.h"
 #import "base/RTCMacros.h"
 #import "base/RTCVideoDecoder.h"
 
 #include "api/video_codecs/video_decoder.h"
 #include "media/base/codec.h"
 
-@interface RTC_OBJC_TYPE (RTCWrappedNativeVideoDecoder) : NSObject <RTC_OBJC_TYPE (RTCVideoDecoder)>
+// TODO: bugs.webrtc.org/15791 - Remove in favor of the RTCNativeVideoDecoderBuilder
+@interface RTC_OBJC_TYPE (RTCWrappedNativeVideoDecoder) : RTC_OBJC_TYPE (RTCNativeVideoDecoder)
 
 - (instancetype)initWithNativeDecoder:(std::unique_ptr<webrtc::VideoDecoder>)decoder;
 
diff --git a/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm b/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm
index 29d2265..b812d94 100644
--- a/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm
+++ b/sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.mm
@@ -12,7 +12,6 @@
 
 #import "RTCWrappedNativeVideoDecoder.h"
 #import "base/RTCMacros.h"
-#import "helpers/NSString+StdString.h"
 
 @implementation RTC_OBJC_TYPE (RTCWrappedNativeVideoDecoder) {
   std::unique_ptr<webrtc::VideoDecoder> _wrappedDecoder;
@@ -30,34 +29,4 @@
   return std::move(_wrappedDecoder);
 }
 
-#pragma mark - RTC_OBJC_TYPE(RTCVideoDecoder)
-
-- (void)setCallback:(RTCVideoDecoderCallback)callback {
-  RTC_DCHECK_NOTREACHED();
-}
-
-- (NSInteger)startDecodeWithNumberOfCores:(int)numberOfCores {
-  RTC_DCHECK_NOTREACHED();
-  return 0;
-}
-
-- (NSInteger)releaseDecoder {
-  RTC_DCHECK_NOTREACHED();
-  return 0;
-}
-
-// TODO(bugs.webrtc.org/15444): Remove obsolete missingFrames param.
-- (NSInteger)decode:(RTC_OBJC_TYPE(RTCEncodedImage) *)encodedImage
-        missingFrames:(BOOL)missingFrames
-    codecSpecificInfo:(nullable id<RTC_OBJC_TYPE(RTCCodecSpecificInfo)>)info
-         renderTimeMs:(int64_t)renderTimeMs {
-  RTC_DCHECK_NOTREACHED();
-  return 0;
-}
-
-- (NSString *)implementationName {
-  RTC_DCHECK_NOTREACHED();
-  return nil;
-}
-
 @end
diff --git a/sdk/objc/native/src/objc_video_decoder_factory.h b/sdk/objc/native/src/objc_video_decoder_factory.h
index 19c997e..de10fbc 100644
--- a/sdk/objc/native/src/objc_video_decoder_factory.h
+++ b/sdk/objc/native/src/objc_video_decoder_factory.h
@@ -13,6 +13,7 @@
 
 #import "base/RTCMacros.h"
 
+#include "api/environment/environment.h"
 #include "api/video_codecs/video_decoder_factory.h"
 #include "media/base/codec.h"
 
@@ -29,7 +30,8 @@
   id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> wrapped_decoder_factory() const;
 
   std::vector<SdpVideoFormat> GetSupportedFormats() const override;
-  std::unique_ptr<VideoDecoder> CreateVideoDecoder(const SdpVideoFormat& format) override;
+  std::unique_ptr<VideoDecoder> Create(const Environment& env,
+                                       const SdpVideoFormat& format) override;
 
  private:
   id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> decoder_factory_;
diff --git a/sdk/objc/native/src/objc_video_decoder_factory.mm b/sdk/objc/native/src/objc_video_decoder_factory.mm
index bf5e898..8939940 100644
--- a/sdk/objc/native/src/objc_video_decoder_factory.mm
+++ b/sdk/objc/native/src/objc_video_decoder_factory.mm
@@ -18,6 +18,7 @@
 #import "components/video_codec/RTCCodecSpecificInfoH264.h"
 #import "sdk/objc/api/peerconnection/RTCEncodedImage+Private.h"
 #import "sdk/objc/api/peerconnection/RTCVideoCodecInfo+Private.h"
+#import "sdk/objc/api/video_codec/RTCNativeVideoDecoderBuilder+Native.h"
 #import "sdk/objc/api/video_codec/RTCWrappedNativeVideoDecoder.h"
 #import "sdk/objc/helpers/NSString+StdString.h"
 
@@ -42,8 +43,7 @@
         [decoder_ startDecodeWithNumberOfCores:settings.number_of_cores()] == WEBRTC_VIDEO_CODEC_OK;
   }
 
-  int32_t Decode(const EncodedImage &input_image,
-                 int64_t render_time_ms = -1) override {
+  int32_t Decode(const EncodedImage &input_image, int64_t render_time_ms = -1) override {
     RTC_OBJC_TYPE(RTCEncodedImage) *encodedImage =
         [[RTC_OBJC_TYPE(RTCEncodedImage) alloc] initWithNativeEncodedImage:input_image];
 
@@ -91,14 +91,16 @@
   return decoder_factory_;
 }
 
-std::unique_ptr<VideoDecoder> ObjCVideoDecoderFactory::CreateVideoDecoder(
-    const SdpVideoFormat &format) {
+std::unique_ptr<VideoDecoder> ObjCVideoDecoderFactory::Create(const Environment &env,
+                                                              const SdpVideoFormat &format) {
   NSString *codecName = [NSString stringWithUTF8String:format.name.c_str()];
   for (RTC_OBJC_TYPE(RTCVideoCodecInfo) * codecInfo in decoder_factory_.supportedCodecs) {
     if ([codecName isEqualToString:codecInfo.name]) {
       id<RTC_OBJC_TYPE(RTCVideoDecoder)> decoder = [decoder_factory_ createDecoder:codecInfo];
 
-      if ([decoder isKindOfClass:[RTC_OBJC_TYPE(RTCWrappedNativeVideoDecoder) class]]) {
+      if ([decoder conformsToProtocol:@protocol(RTC_OBJC_TYPE(RTCNativeVideoDecoderBuilder))]) {
+        return [((id<RTC_OBJC_TYPE(RTCNativeVideoDecoderBuilder)>)decoder) build:env];
+      } else if ([decoder isKindOfClass:[RTC_OBJC_TYPE(RTCWrappedNativeVideoDecoder) class]]) {
         return [(RTC_OBJC_TYPE(RTCWrappedNativeVideoDecoder) *)decoder releaseWrappedDecoder];
       } else {
         return std::unique_ptr<ObjCVideoDecoder>(new ObjCVideoDecoder(decoder));
diff --git a/sdk/objc/unittests/objc_video_decoder_factory_tests.mm b/sdk/objc/unittests/objc_video_decoder_factory_tests.mm
index f44d831..5b2b401 100644
--- a/sdk/objc/unittests/objc_video_decoder_factory_tests.mm
+++ b/sdk/objc/unittests/objc_video_decoder_factory_tests.mm
@@ -14,6 +14,7 @@
 
 #include "sdk/objc/native/src/objc_video_decoder_factory.h"
 
+#include "api/environment/environment_factory.h"
 #import "base/RTCMacros.h"
 #import "base/RTCVideoDecoder.h"
 #import "base/RTCVideoDecoderFactory.h"
@@ -26,9 +27,9 @@
   id decoderMock = OCMProtocolMock(@protocol(RTC_OBJC_TYPE(RTCVideoDecoder)));
   OCMStub([decoderMock startDecodeWithNumberOfCores:1]).andReturn(return_code);
   OCMStub([decoderMock decode:[OCMArg any]
-                    missingFrames:NO
-                codecSpecificInfo:[OCMArg any]
-                     renderTimeMs:0])
+                  missingFrames:NO
+              codecSpecificInfo:[OCMArg any]
+                   renderTimeMs:0])
       .andReturn(return_code);
   OCMStub([decoderMock releaseDecoder]).andReturn(return_code);
 
@@ -51,7 +52,8 @@
 std::unique_ptr<webrtc::VideoDecoder> GetObjCDecoder(
     id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> factory) {
   webrtc::ObjCVideoDecoderFactory decoder_factory(factory);
-  return decoder_factory.CreateVideoDecoder(webrtc::SdpVideoFormat(cricket::kH264CodecName));
+  return decoder_factory.Create(webrtc::CreateEnvironment(),
+                                webrtc::SdpVideoFormat(cricket::kH264CodecName));
 }
 
 #pragma mark -