xref: /aosp_15_r20/external/webrtc/api/video_codecs/test/video_decoder_factory_template_tests.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2022 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 "api/test/mock_video_decoder.h"
12 #include "api/video_codecs/video_decoder_factory_template.h"
13 #include "api/video_codecs/video_decoder_factory_template_dav1d_adapter.h"
14 #include "api/video_codecs/video_decoder_factory_template_libvpx_vp8_adapter.h"
15 #include "api/video_codecs/video_decoder_factory_template_libvpx_vp9_adapter.h"
16 #include "api/video_codecs/video_decoder_factory_template_open_h264_adapter.h"
17 #include "test/gmock.h"
18 #include "test/gtest.h"
19 
20 using ::testing::Contains;
21 using ::testing::Each;
22 using ::testing::Eq;
23 using ::testing::Field;
24 using ::testing::IsEmpty;
25 using ::testing::Ne;
26 using ::testing::Not;
27 using ::testing::UnorderedElementsAre;
28 
29 namespace webrtc {
30 namespace {
31 const SdpVideoFormat kFooSdp("Foo");
32 const SdpVideoFormat kBarLowSdp("Bar", {{"profile", "low"}});
33 const SdpVideoFormat kBarHighSdp("Bar", {{"profile", "high"}});
34 
35 struct FooDecoderTemplateAdapter {
SupportedFormatswebrtc::__anonb785c24f0111::FooDecoderTemplateAdapter36   static std::vector<SdpVideoFormat> SupportedFormats() { return {kFooSdp}; }
37 
CreateDecoderwebrtc::__anonb785c24f0111::FooDecoderTemplateAdapter38   static std::unique_ptr<VideoDecoder> CreateDecoder(
39       const SdpVideoFormat& format) {
40     auto decoder = std::make_unique<testing::StrictMock<MockVideoDecoder>>();
41     EXPECT_CALL(*decoder, Destruct);
42     return decoder;
43   }
44 };
45 
46 struct BarDecoderTemplateAdapter {
SupportedFormatswebrtc::__anonb785c24f0111::BarDecoderTemplateAdapter47   static std::vector<SdpVideoFormat> SupportedFormats() {
48     return {kBarLowSdp, kBarHighSdp};
49   }
50 
CreateDecoderwebrtc::__anonb785c24f0111::BarDecoderTemplateAdapter51   static std::unique_ptr<VideoDecoder> CreateDecoder(
52       const SdpVideoFormat& format) {
53     auto decoder = std::make_unique<testing::StrictMock<MockVideoDecoder>>();
54     EXPECT_CALL(*decoder, Destruct);
55     return decoder;
56   }
57 };
58 
TEST(VideoDecoderFactoryTemplate,OneTemplateAdapterCreateDecoder)59 TEST(VideoDecoderFactoryTemplate, OneTemplateAdapterCreateDecoder) {
60   VideoDecoderFactoryTemplate<FooDecoderTemplateAdapter> factory;
61   EXPECT_THAT(factory.GetSupportedFormats(), UnorderedElementsAre(kFooSdp));
62   EXPECT_THAT(factory.CreateVideoDecoder(kFooSdp), Ne(nullptr));
63   EXPECT_THAT(factory.CreateVideoDecoder(SdpVideoFormat("FooX")), Eq(nullptr));
64 }
65 
TEST(VideoDecoderFactoryTemplate,TwoTemplateAdaptersNoDuplicates)66 TEST(VideoDecoderFactoryTemplate, TwoTemplateAdaptersNoDuplicates) {
67   VideoDecoderFactoryTemplate<FooDecoderTemplateAdapter,
68                               FooDecoderTemplateAdapter>
69       factory;
70   EXPECT_THAT(factory.GetSupportedFormats(), UnorderedElementsAre(kFooSdp));
71 }
72 
TEST(VideoDecoderFactoryTemplate,TwoTemplateAdaptersCreateDecoders)73 TEST(VideoDecoderFactoryTemplate, TwoTemplateAdaptersCreateDecoders) {
74   VideoDecoderFactoryTemplate<FooDecoderTemplateAdapter,
75                               BarDecoderTemplateAdapter>
76       factory;
77   EXPECT_THAT(factory.GetSupportedFormats(),
78               UnorderedElementsAre(kFooSdp, kBarLowSdp, kBarHighSdp));
79   EXPECT_THAT(factory.CreateVideoDecoder(kFooSdp), Ne(nullptr));
80   EXPECT_THAT(factory.CreateVideoDecoder(kBarLowSdp), Ne(nullptr));
81   EXPECT_THAT(factory.CreateVideoDecoder(kBarHighSdp), Ne(nullptr));
82   EXPECT_THAT(factory.CreateVideoDecoder(SdpVideoFormat("FooX")), Eq(nullptr));
83   EXPECT_THAT(factory.CreateVideoDecoder(SdpVideoFormat("Bar")), Eq(nullptr));
84 }
85 
TEST(VideoDecoderFactoryTemplate,LibvpxVp8)86 TEST(VideoDecoderFactoryTemplate, LibvpxVp8) {
87   VideoDecoderFactoryTemplate<LibvpxVp8DecoderTemplateAdapter> factory;
88   auto formats = factory.GetSupportedFormats();
89   EXPECT_THAT(formats.size(), 1);
90   EXPECT_THAT(formats[0], Field(&SdpVideoFormat::name, "VP8"));
91   EXPECT_THAT(factory.CreateVideoDecoder(formats[0]), Ne(nullptr));
92 }
93 
TEST(VideoDecoderFactoryTemplate,LibvpxVp9)94 TEST(VideoDecoderFactoryTemplate, LibvpxVp9) {
95   VideoDecoderFactoryTemplate<LibvpxVp9DecoderTemplateAdapter> factory;
96   auto formats = factory.GetSupportedFormats();
97   EXPECT_THAT(formats, Not(IsEmpty()));
98   EXPECT_THAT(formats, Each(Field(&SdpVideoFormat::name, "VP9")));
99   EXPECT_THAT(factory.CreateVideoDecoder(formats[0]), Ne(nullptr));
100 }
101 
102 // TODO(bugs.webrtc.org/13573): When OpenH264 is no longer a conditional build
103 //                              target remove this #ifdef.
104 #if defined(WEBRTC_USE_H264)
TEST(VideoDecoderFactoryTemplate,OpenH264)105 TEST(VideoDecoderFactoryTemplate, OpenH264) {
106   VideoDecoderFactoryTemplate<OpenH264DecoderTemplateAdapter> factory;
107   auto formats = factory.GetSupportedFormats();
108   EXPECT_THAT(formats, Not(IsEmpty()));
109   EXPECT_THAT(formats, Each(Field(&SdpVideoFormat::name, "H264")));
110   EXPECT_THAT(factory.CreateVideoDecoder(formats[0]), Ne(nullptr));
111 }
112 #endif  // defined(WEBRTC_USE_H264)
113 
TEST(VideoDecoderFactoryTemplate,Dav1d)114 TEST(VideoDecoderFactoryTemplate, Dav1d) {
115   VideoDecoderFactoryTemplate<Dav1dDecoderTemplateAdapter> factory;
116   auto formats = factory.GetSupportedFormats();
117   EXPECT_THAT(formats.size(), 1);
118   EXPECT_THAT(formats[0], Field(&SdpVideoFormat::name, "AV1"));
119   EXPECT_THAT(factory.CreateVideoDecoder(formats[0]), Ne(nullptr));
120 }
121 
122 }  // namespace
123 }  // namespace webrtc
124