xref: /aosp_15_r20/external/webrtc/media/engine/internal_encoder_factory_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "media/engine/internal_encoder_factory.h"
12 
13 #include "api/video_codecs/sdp_video_format.h"
14 #include "api/video_codecs/video_encoder.h"
15 #include "api/video_codecs/vp9_profile.h"
16 #include "media/base/media_constants.h"
17 #include "test/gmock.h"
18 #include "test/gtest.h"
19 
20 namespace webrtc {
21 namespace {
22 using ::testing::Contains;
23 using ::testing::Field;
24 using ::testing::Not;
25 
26 #ifdef RTC_ENABLE_VP9
27 constexpr bool kVp9Enabled = true;
28 #else
29 constexpr bool kVp9Enabled = false;
30 #endif
31 #ifdef WEBRTC_USE_H264
32 constexpr bool kH264Enabled = true;
33 #else
34 constexpr bool kH264Enabled = false;
35 #endif
36 constexpr VideoEncoderFactory::CodecSupport kSupported = {
37     /*is_supported=*/true, /*is_power_efficient=*/false};
38 constexpr VideoEncoderFactory::CodecSupport kUnsupported = {
39     /*is_supported=*/false, /*is_power_efficient=*/false};
40 
41 MATCHER_P(Support, expected, "") {
42   return arg.is_supported == expected.is_supported &&
43          arg.is_power_efficient == expected.is_power_efficient;
44 }
45 
TEST(InternalEncoderFactoryTest,Vp8)46 TEST(InternalEncoderFactoryTest, Vp8) {
47   InternalEncoderFactory factory;
48   std::unique_ptr<VideoEncoder> encoder =
49       factory.CreateVideoEncoder(SdpVideoFormat(cricket::kVp8CodecName));
50   EXPECT_TRUE(encoder);
51 }
52 
TEST(InternalEncoderFactoryTest,Vp9Profile0)53 TEST(InternalEncoderFactoryTest, Vp9Profile0) {
54   InternalEncoderFactory factory;
55   if (kVp9Enabled) {
56     std::unique_ptr<VideoEncoder> encoder =
57         factory.CreateVideoEncoder(SdpVideoFormat(
58             cricket::kVp9CodecName,
59             {{kVP9FmtpProfileId, VP9ProfileToString(VP9Profile::kProfile0)}}));
60     EXPECT_TRUE(encoder);
61   } else {
62     EXPECT_THAT(
63         factory.GetSupportedFormats(),
64         Not(Contains(Field(&SdpVideoFormat::name, cricket::kVp9CodecName))));
65   }
66 }
67 
TEST(InternalEncoderFactoryTest,H264)68 TEST(InternalEncoderFactoryTest, H264) {
69   InternalEncoderFactory factory;
70   if (kH264Enabled) {
71     std::unique_ptr<VideoEncoder> encoder =
72         factory.CreateVideoEncoder(SdpVideoFormat(cricket::kH264CodecName));
73     EXPECT_TRUE(encoder);
74   } else {
75     EXPECT_THAT(
76         factory.GetSupportedFormats(),
77         Not(Contains(Field(&SdpVideoFormat::name, cricket::kH264CodecName))));
78   }
79 }
80 
TEST(InternalEncoderFactoryTest,QueryCodecSupportWithScalabilityMode)81 TEST(InternalEncoderFactoryTest, QueryCodecSupportWithScalabilityMode) {
82   InternalEncoderFactory factory;
83   // VP8 and VP9 supported for singles spatial layers.
84   EXPECT_THAT(
85       factory.QueryCodecSupport(SdpVideoFormat(cricket::kVp8CodecName), "L1T2"),
86       Support(kSupported));
87   EXPECT_THAT(
88       factory.QueryCodecSupport(SdpVideoFormat(cricket::kVp9CodecName), "L1T3"),
89       Support(kVp9Enabled ? kSupported : kUnsupported));
90 
91   // VP9 support for spatial layers.
92   EXPECT_THAT(
93       factory.QueryCodecSupport(SdpVideoFormat(cricket::kVp9CodecName), "L3T3"),
94       Support(kVp9Enabled ? kSupported : kUnsupported));
95 
96   // Invalid scalability modes even though VP8 and H264 are supported.
97   EXPECT_THAT(factory.QueryCodecSupport(SdpVideoFormat(cricket::kH264CodecName),
98                                         "L2T2"),
99               Support(kUnsupported));
100   EXPECT_THAT(
101       factory.QueryCodecSupport(SdpVideoFormat(cricket::kVp8CodecName), "L3T3"),
102       Support(kUnsupported));
103 }
104 
105 #if defined(RTC_USE_LIBAOM_AV1_ENCODER)
TEST(InternalEncoderFactoryTest,Av1)106 TEST(InternalEncoderFactoryTest, Av1) {
107   InternalEncoderFactory factory;
108   EXPECT_THAT(factory.GetSupportedFormats(),
109               Contains(Field(&SdpVideoFormat::name, cricket::kAv1CodecName)));
110   EXPECT_TRUE(
111       factory.CreateVideoEncoder(SdpVideoFormat(cricket::kAv1CodecName)));
112 }
113 
TEST(InternalEncoderFactoryTest,QueryCodecSupportNoScalabilityModeAv1)114 TEST(InternalEncoderFactoryTest, QueryCodecSupportNoScalabilityModeAv1) {
115   InternalEncoderFactory factory;
116   EXPECT_THAT(factory.QueryCodecSupport(SdpVideoFormat(cricket::kAv1CodecName),
117                                         /*scalability_mode=*/absl::nullopt),
118               Support(kSupported));
119 }
120 
TEST(InternalEncoderFactoryTest,QueryCodecSupportNoScalabilityMode)121 TEST(InternalEncoderFactoryTest, QueryCodecSupportNoScalabilityMode) {
122   InternalEncoderFactory factory;
123   EXPECT_THAT(factory.QueryCodecSupport(SdpVideoFormat(cricket::kVp8CodecName),
124                                         /*scalability_mode=*/absl::nullopt),
125               Support(kSupported));
126   EXPECT_THAT(factory.QueryCodecSupport(SdpVideoFormat(cricket::kVp9CodecName),
127                                         /*scalability_mode=*/absl::nullopt),
128               Support(kVp9Enabled ? kSupported : kUnsupported));
129 }
130 
TEST(InternalEncoderFactoryTest,QueryCodecSupportWithScalabilityModeAv1)131 TEST(InternalEncoderFactoryTest, QueryCodecSupportWithScalabilityModeAv1) {
132   InternalEncoderFactory factory;
133   EXPECT_THAT(
134       factory.QueryCodecSupport(SdpVideoFormat(cricket::kAv1CodecName), "L2T1"),
135       Support(kSupported));
136 }
137 #endif  // defined(RTC_USE_LIBAOM_AV1_ENCODER)
138 
139 }  // namespace
140 }  // namespace webrtc
141