• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef VIDEO_POST_PROCESSOR_FACTORY_H
17 #define VIDEO_POST_PROCESSOR_FACTORY_H
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "base_video_post_processor.h"
25 
26 namespace OHOS {
27 namespace Media {
28 using VideoPostProcessorInstanceGenerator = std::function<std::shared_ptr<BaseVideoPostProcessor>()>;
29 using VideoPostProcessorSupportChecker = std::function<bool(const std::shared_ptr<Meta>& meta)>;
30 
31 class VideoPostProcessorFactory {
32 public:
33     ~VideoPostProcessorFactory() = default;
34 
35     VideoPostProcessorFactory(const VideoPostProcessorFactory&) = delete;
36 
37     VideoPostProcessorFactory operator=(const VideoPostProcessorFactory&) = delete;
38 
39     static VideoPostProcessorFactory& Instance();
40 
41     template <typename T>
CreateVideoPostProcessor(const VideoPostProcessorType type)42     std::shared_ptr<T> CreateVideoPostProcessor(const VideoPostProcessorType type)
43     {
44         auto processor = CreateVideoPostProcessorPriv(type);
45         auto typedProcessor = ReinterpretPointerCast<T>(processor);
46         return typedProcessor;
47     }
48 
IsPostProcessorSupported(const VideoPostProcessorType type,const std::shared_ptr<Meta> & meta)49     bool IsPostProcessorSupported(const VideoPostProcessorType type, const std::shared_ptr<Meta>& meta)
50     {
51         return IsPostProcessorSupportedPriv(type, meta);
52     }
53 
54     template <typename T>
55     void RegisterPostProcessor(const VideoPostProcessorType type,
56         const VideoPostProcessorInstanceGenerator& generator = nullptr)
57     {
58         RegisterPostProcessorPriv<T>(type, generator);
59     }
60 
61     void RegisterChecker(const VideoPostProcessorType type, const VideoPostProcessorSupportChecker& checker = nullptr)
62     {
63         RegisterCheckerPriv(type, checker);
64     }
65 
66 private:
67     VideoPostProcessorFactory() = default;
68 
69     std::shared_ptr<BaseVideoPostProcessor> CreateVideoPostProcessorPriv(const VideoPostProcessorType type);
70     bool IsPostProcessorSupportedPriv(const VideoPostProcessorType type, const std::shared_ptr<Meta>& meta);
71 
72     template <typename T>
RegisterPostProcessorPriv(const VideoPostProcessorType type,const VideoPostProcessorInstanceGenerator & generator)73     void RegisterPostProcessorPriv(const VideoPostProcessorType type,
74         const VideoPostProcessorInstanceGenerator& generator)
75     {
76         if (generator == nullptr) {
77             auto result = generators_.emplace(
78                 type, []() {
79                     return std::make_shared<T>();
80                 });
81             if (!result.second) {
82                 result.first->second = generator;
83             }
84         } else {
85             auto result = generators_.emplace(type, generator);
86             if (!result.second) {
87                 result.first->second = generator;
88             }
89         }
90     }
91 
RegisterCheckerPriv(const VideoPostProcessorType type,const VideoPostProcessorSupportChecker & checker)92     void RegisterCheckerPriv(const VideoPostProcessorType type, const VideoPostProcessorSupportChecker& checker)
93     {
94         if (checker == nullptr) {
95             auto result = checkers_.emplace(
96                 type, [](const std::shared_ptr<Meta>& meta) {
97                     return true;
98                 });
99             if (!result.second) {
100                 result.first->second = checker;
101             }
102         } else {
103             auto result = checkers_.emplace(type, checker);
104             if (!result.second) {
105                 result.first->second = checker;
106             }
107         }
108     }
109 
110     std::unordered_map<VideoPostProcessorType, VideoPostProcessorInstanceGenerator> generators_;
111     std::unordered_map<VideoPostProcessorType, VideoPostProcessorSupportChecker> checkers_;
112 };
113 
114 template <typename T>
115 class AutoRegisterPostProcessor {
116 public:
AutoRegisterPostProcessor(const VideoPostProcessorType type)117     explicit AutoRegisterPostProcessor(const VideoPostProcessorType type)
118     {
119         VideoPostProcessorFactory::Instance().RegisterPostProcessor<T>(type);
120         VideoPostProcessorFactory::Instance().RegisterChecker(type);
121     }
122 
AutoRegisterPostProcessor(const VideoPostProcessorType type,const VideoPostProcessorInstanceGenerator & generator)123     AutoRegisterPostProcessor(const VideoPostProcessorType type, const VideoPostProcessorInstanceGenerator& generator)
124     {
125         VideoPostProcessorFactory::Instance().RegisterPostProcessor<T>(type, generator);
126         VideoPostProcessorFactory::Instance().RegisterChecker(type);
127     }
128 
AutoRegisterPostProcessor(const VideoPostProcessorType type,const VideoPostProcessorInstanceGenerator & generator,const VideoPostProcessorSupportChecker & checker)129     AutoRegisterPostProcessor(const VideoPostProcessorType type, const VideoPostProcessorInstanceGenerator& generator,
130                                     const VideoPostProcessorSupportChecker& checker)
131     {
132         VideoPostProcessorFactory::Instance().RegisterPostProcessor<T>(type, generator);
133         VideoPostProcessorFactory::Instance().RegisterChecker(type, checker);
134     }
135 
136     ~AutoRegisterPostProcessor() = default;
137 };
138 } // namespace Media
139 } // namespace OHOS
140 #endif // VIDEO_POST_PROCESSOR_FACTORY_H
141