Make libaom_av1_encoder always build the libaom encoder.

Currently `CreateLibaomAv1Encoder` will either return an actual libaom AV1 encoder or a nullptr depening on whether the build flag `enable_libaom` was configured to true or not. This CL updates the `libaom_av1_encoder` build target to no longer depend on `enable_libaom` so that `CreateLibaomAv1Encoder` will always return an encoder instance.

Added `CreateLibaomAv1EncoderIfSupported` as a replacement to the old `CreateLibaomAv1Encoder`.

Bug: webrtc:13573
Change-Id: Ibdcd52c609acd79feefa2b86f19d1b4ca3e91d0a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/242360
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Peter Hanspers <peterhanspers@webrtc.org>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Commit-Queue: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35763}
diff --git a/media/BUILD.gn b/media/BUILD.gn
index 89a5d46..6ae156b 100644
--- a/media/BUILD.gn
+++ b/media/BUILD.gn
@@ -211,7 +211,7 @@
     "../modules/video_coding:webrtc_vp8",
     "../modules/video_coding:webrtc_vp9",
     "../modules/video_coding/codecs/av1:libaom_av1_decoder",
-    "../modules/video_coding/codecs/av1:libaom_av1_encoder",
+    "../modules/video_coding/codecs/av1:libaom_av1_encoder_if_supported",
     "../rtc_base:checks",
     "../rtc_base:rtc_base_approved",
     "../rtc_base/system:rtc_export",
@@ -622,7 +622,7 @@
         "../modules/video_coding:webrtc_h264",
         "../modules/video_coding:webrtc_vp8",
         "../modules/video_coding/codecs/av1:libaom_av1_decoder",
-        "../modules/video_coding/codecs/av1:libaom_av1_encoder",
+        "../modules/video_coding/codecs/av1:libaom_av1_encoder_if_supported",
         "../p2p:p2p_test_utils",
         "../rtc_base",
         "../rtc_base:checks",
diff --git a/media/engine/internal_encoder_factory.cc b/media/engine/internal_encoder_factory.cc
index bb55066..5ccb93d 100644
--- a/media/engine/internal_encoder_factory.cc
+++ b/media/engine/internal_encoder_factory.cc
@@ -16,7 +16,7 @@
 #include "api/video_codecs/sdp_video_format.h"
 #include "media/base/codec.h"
 #include "media/base/media_constants.h"
-#include "modules/video_coding/codecs/av1/libaom_av1_encoder.h"
+#include "modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h"
 #include "modules/video_coding/codecs/h264/include/h264.h"
 #include "modules/video_coding/codecs/vp8/include/vp8.h"
 #include "modules/video_coding/codecs/vp9/include/vp9.h"
@@ -51,7 +51,7 @@
     return H264Encoder::Create(cricket::VideoCodec(format));
   if (kIsLibaomAv1EncoderSupported &&
       absl::EqualsIgnoreCase(format.name, cricket::kAv1CodecName))
-    return CreateLibaomAv1Encoder();
+    return CreateLibaomAv1EncoderIfSupported();
   RTC_LOG(LS_ERROR) << "Trying to created encoder of unsupported format "
                     << format.name;
   return nullptr;
diff --git a/media/engine/internal_encoder_factory_unittest.cc b/media/engine/internal_encoder_factory_unittest.cc
index 3365e59..c3a667e 100644
--- a/media/engine/internal_encoder_factory_unittest.cc
+++ b/media/engine/internal_encoder_factory_unittest.cc
@@ -14,7 +14,7 @@
 #include "api/video_codecs/video_encoder.h"
 #include "api/video_codecs/vp9_profile.h"
 #include "media/base/media_constants.h"
-#include "modules/video_coding/codecs/av1/libaom_av1_encoder.h"
+#include "modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 
diff --git a/modules/video_coding/codecs/av1/BUILD.gn b/modules/video_coding/codecs/av1/BUILD.gn
index 7106ca4..2e4d824 100644
--- a/modules/video_coding/codecs/av1/BUILD.gn
+++ b/modules/video_coding/codecs/av1/BUILD.gn
@@ -71,32 +71,46 @@
   visibility = [ "*" ]
   poisonous = [ "software_video_codecs" ]
   public = [ "libaom_av1_encoder.h" ]
+  sources = [ "libaom_av1_encoder.cc" ]
+  deps = [
+    "../..:video_codec_interface",
+    "../../../../api:scoped_refptr",
+    "../../../../api/video:encoded_image",
+    "../../../../api/video:video_frame",
+    "../../../../api/video_codecs:video_codecs_api",
+    "../../../../common_video",
+    "../../../../rtc_base:checks",
+    "../../../../rtc_base:logging",
+    "../../svc:scalability_structures",
+    "../../svc:scalable_video_controller",
+    "//third_party/libaom",
+  ]
+  absl_deps = [
+    "//third_party/abseil-cpp/absl/algorithm:container",
+    "//third_party/abseil-cpp/absl/strings:strings",
+    "//third_party/abseil-cpp/absl/types:optional",
+  ]
+}
+
+rtc_library("libaom_av1_encoder_if_supported") {
+  visibility = [ "*" ]
+  poisonous = [ "software_video_codecs" ]
+  public = [ "libaom_av1_encoder_supported.h" ]
+  sources = [ "libaom_av1_encoder_supported.cc" ]
   deps = [
     "../../../../api/video_codecs:video_codecs_api",
     "../../svc:scalability_structures",
     "../../svc:scalable_video_controller",
   ]
   absl_deps = [
-    "//third_party/abseil-cpp/absl/algorithm:container",
     "//third_party/abseil-cpp/absl/base:core_headers",
     "//third_party/abseil-cpp/absl/strings:strings",
-    "//third_party/abseil-cpp/absl/types:optional",
   ]
 
+  defines = []
   if (enable_libaom) {
-    sources = [ "libaom_av1_encoder.cc" ]
-    deps += [
-      "../..:video_codec_interface",
-      "../../../../api:scoped_refptr",
-      "../../../../api/video:encoded_image",
-      "../../../../api/video:video_frame",
-      "../../../../common_video",
-      "../../../../rtc_base:checks",
-      "../../../../rtc_base:logging",
-      "//third_party/libaom",
-    ]
-  } else {
-    sources = [ "libaom_av1_encoder_absent.cc" ]
+    defines += [ "RTC_USE_LIBAOM_AV1_ENCODER" ]
+    deps += [ ":libaom_av1_encoder" ]
   }
 }
 
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
index 967ec7d..79a31d9 100644
--- a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
@@ -820,17 +820,8 @@
 
 }  // namespace
 
-const bool kIsLibaomAv1EncoderSupported = true;
-
 std::unique_ptr<VideoEncoder> CreateLibaomAv1Encoder() {
   return std::make_unique<LibaomAv1Encoder>();
 }
 
-bool LibaomAv1EncoderSupportsScalabilityMode(
-    absl::string_view scalability_mode) {
-  // For AV1, the scalability mode is supported if we can create the scalability
-  // structure.
-  return ScalabilityStructureConfig(scalability_mode) != absl::nullopt;
-}
-
 }  // namespace webrtc
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder.h b/modules/video_coding/codecs/av1/libaom_av1_encoder.h
index 0d81c9c..e69df9e 100644
--- a/modules/video_coding/codecs/av1/libaom_av1_encoder.h
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder.h
@@ -12,18 +12,11 @@
 
 #include <memory>
 
-#include "absl/base/attributes.h"
 #include "absl/strings/string_view.h"
 #include "api/video_codecs/video_encoder.h"
 
 namespace webrtc {
-
-ABSL_CONST_INIT extern const bool kIsLibaomAv1EncoderSupported;
-
 std::unique_ptr<VideoEncoder> CreateLibaomAv1Encoder();
-bool LibaomAv1EncoderSupportsScalabilityMode(
-    absl::string_view scalability_mode);
-
 }  // namespace webrtc
 
 #endif  // MODULES_VIDEO_CODING_CODECS_AV1_LIBAOM_AV1_ENCODER_H_
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder_absent.cc b/modules/video_coding/codecs/av1/libaom_av1_encoder_absent.cc
deleted file mode 100644
index fff1dd9..0000000
--- a/modules/video_coding/codecs/av1/libaom_av1_encoder_absent.cc
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- *  Copyright (c) 2020 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.
- */
-#include "modules/video_coding/codecs/av1/libaom_av1_encoder.h"
-
-#include <memory>
-
-#include "api/video_codecs/video_encoder.h"
-
-namespace webrtc {
-
-const bool kIsLibaomAv1EncoderSupported = false;
-
-std::unique_ptr<VideoEncoder> CreateLibaomAv1Encoder() {
-  return nullptr;
-}
-
-bool LibaomAv1EncoderSupportsScalabilityMode(
-    absl::string_view scalability_mode) {
-  return false;
-}
-
-}  // namespace webrtc
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder_supported.cc b/modules/video_coding/codecs/av1/libaom_av1_encoder_supported.cc
new file mode 100644
index 0000000..0bb3108
--- /dev/null
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder_supported.cc
@@ -0,0 +1,41 @@
+/*
+ *  Copyright (c) 2021 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.
+ */
+#include "modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h"
+
+#include "modules/video_coding/svc/create_scalability_structure.h"
+
+#if defined(RTC_USE_LIBAOM_AV1_ENCODER)
+#include "modules/video_coding/codecs/av1/libaom_av1_encoder.h"  // nogncheck
+#endif
+
+namespace webrtc {
+#if defined(RTC_USE_LIBAOM_AV1_ENCODER)
+const bool kIsLibaomAv1EncoderSupported = true;
+std::unique_ptr<VideoEncoder> CreateLibaomAv1EncoderIfSupported() {
+  return CreateLibaomAv1Encoder();
+}
+bool LibaomAv1EncoderSupportsScalabilityMode(
+    absl::string_view scalability_mode) {
+  // For libaom AV1, the scalability mode is supported if we can create the
+  // scalability structure.
+  return ScalabilityStructureConfig(scalability_mode) != absl::nullopt;
+}
+#else
+const bool kIsLibaomAv1EncoderSupported = false;
+std::unique_ptr<VideoEncoder> CreateLibaomAv1EncoderIfSupported() {
+  return nullptr;
+}
+bool LibaomAv1EncoderSupportsScalabilityMode(
+    absl::string_view scalability_mode) {
+  return false;
+}
+#endif
+
+}  // namespace webrtc
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h b/modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h
new file mode 100644
index 0000000..84dd8d6
--- /dev/null
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h
@@ -0,0 +1,29 @@
+/*
+ *  Copyright (c) 2022 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.
+ */
+#ifndef MODULES_VIDEO_CODING_CODECS_AV1_LIBAOM_AV1_ENCODER_SUPPORTED_H_
+#define MODULES_VIDEO_CODING_CODECS_AV1_LIBAOM_AV1_ENCODER_SUPPORTED_H_
+
+#include <memory>
+
+#include "absl/base/attributes.h"
+#include "absl/strings/string_view.h"
+#include "api/video_codecs/video_encoder.h"
+
+namespace webrtc {
+
+ABSL_CONST_INIT extern const bool kIsLibaomAv1EncoderSupported;
+
+std::unique_ptr<VideoEncoder> CreateLibaomAv1EncoderIfSupported();
+bool LibaomAv1EncoderSupportsScalabilityMode(
+    absl::string_view scalability_mode);
+
+}  // namespace webrtc
+
+#endif  // MODULES_VIDEO_CODING_CODECS_AV1_LIBAOM_AV1_ENCODER_SUPPORTED_H_
diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn
index 8f76c52..718d927 100644
--- a/sdk/BUILD.gn
+++ b/sdk/BUILD.gn
@@ -730,7 +730,7 @@
         ":wrapped_native_codec_objc",
         "../media:rtc_media_base",
         "../modules/video_coding/codecs/av1:libaom_av1_decoder",
-        "../modules/video_coding/codecs/av1:libaom_av1_encoder",
+        "../modules/video_coding/codecs/av1:libaom_av1_encoder_if_supported",
       ]
     }
 
diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn
index ff3f551..561a33d 100644
--- a/sdk/android/BUILD.gn
+++ b/sdk/android/BUILD.gn
@@ -876,7 +876,7 @@
       ":generated_libaom_av1_jni",
       ":video_jni",
       "../../modules/video_coding/codecs/av1:libaom_av1_decoder",
-      "../../modules/video_coding/codecs/av1:libaom_av1_encoder",
+      "../../modules/video_coding/codecs/av1:libaom_av1_encoder_if_supported",
     ]
   }
 
diff --git a/sdk/android/src/jni/libaom_av1_codec.cc b/sdk/android/src/jni/libaom_av1_codec.cc
index 02070f7..e4332a6 100644
--- a/sdk/android/src/jni/libaom_av1_codec.cc
+++ b/sdk/android/src/jni/libaom_av1_codec.cc
@@ -11,7 +11,7 @@
 #include <jni.h>
 
 #include "modules/video_coding/codecs/av1/libaom_av1_decoder.h"
-#include "modules/video_coding/codecs/av1/libaom_av1_encoder.h"
+#include "modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h"
 #include "sdk/android/generated_libaom_av1_jni/LibaomAv1Decoder_jni.h"
 #include "sdk/android/generated_libaom_av1_jni/LibaomAv1Encoder_jni.h"
 #include "sdk/android/src/jni/jni_helpers.h"
@@ -20,7 +20,8 @@
 namespace jni {
 
 static jlong JNI_LibaomAv1Encoder_CreateEncoder(JNIEnv* jni) {
-  return jlongFromPointer(webrtc::CreateLibaomAv1Encoder().release());
+  return jlongFromPointer(
+      webrtc::CreateLibaomAv1EncoderIfSupported().release());
 }
 
 static jboolean JNI_LibaomAv1Encoder_IsSupported(JNIEnv* jni) {
diff --git a/sdk/objc/api/video_codec/RTCVideoEncoderAV1.mm b/sdk/objc/api/video_codec/RTCVideoEncoderAV1.mm
index 92e924a..853ffed 100644
--- a/sdk/objc/api/video_codec/RTCVideoEncoderAV1.mm
+++ b/sdk/objc/api/video_codec/RTCVideoEncoderAV1.mm
@@ -15,12 +15,12 @@
 #import "RTCVideoEncoderAV1.h"
 #import "RTCWrappedNativeVideoEncoder.h"
 
-#include "modules/video_coding/codecs/av1/libaom_av1_encoder.h"
+#include "modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h"
 
 @implementation RTC_OBJC_TYPE (RTCVideoEncoderAV1)
 
 + (id<RTC_OBJC_TYPE(RTCVideoEncoder)>)av1Encoder {
-  std::unique_ptr<webrtc::VideoEncoder> nativeEncoder(webrtc::CreateLibaomAv1Encoder());
+  std::unique_ptr<webrtc::VideoEncoder> nativeEncoder(webrtc::CreateLibaomAv1EncoderIfSupported());
   if (nativeEncoder == nullptr) {
     return nil;
   }
diff --git a/video/BUILD.gn b/video/BUILD.gn
index 0f9b669..92f5dd4 100644
--- a/video/BUILD.gn
+++ b/video/BUILD.gn
@@ -736,7 +736,7 @@
       "../modules/video_coding:webrtc_vp8",
       "../modules/video_coding:webrtc_vp9",
       "../modules/video_coding:webrtc_vp9_helpers",
-      "../modules/video_coding/codecs/av1:libaom_av1_encoder",
+      "../modules/video_coding/codecs/av1:libaom_av1_encoder_if_supported",
       "../rtc_base",
       "../rtc_base:checks",
       "../rtc_base:gunit_helpers",
diff --git a/video/video_stream_encoder_unittest.cc b/video/video_stream_encoder_unittest.cc
index 3a81ec2..1b0597e 100644
--- a/video/video_stream_encoder_unittest.cc
+++ b/video/video_stream_encoder_unittest.cc
@@ -40,7 +40,7 @@
 #include "common_video/include/video_frame_buffer.h"
 #include "media/base/video_adapter.h"
 #include "media/engine/webrtc_video_engine.h"
-#include "modules/video_coding/codecs/av1/libaom_av1_encoder.h"
+#include "modules/video_coding/codecs/av1/libaom_av1_encoder_supported.h"
 #include "modules/video_coding/codecs/h264/include/h264.h"
 #include "modules/video_coding/codecs/multiplex/include/multiplex_encoder_adapter.h"
 #include "modules/video_coding/codecs/vp8/include/vp8.h"
@@ -8439,7 +8439,7 @@
         encoder = VP9Encoder::Create();
         break;
       case kVideoCodecAV1:
-        encoder = CreateLibaomAv1Encoder();
+        encoder = CreateLibaomAv1EncoderIfSupported();
         break;
       case kVideoCodecH264:
         encoder =