• 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 #include "video_processing_algorithm_factory.h"
17 
18 #include <dlfcn.h>
19 
20 #include "algorithm_errors.h"
21 #include "video_processing_algorithm_factory_common.h"
22 #include "vpe_log.h"
23 
24 // NOTE: Add header file of static algorithm which would be called by VPE SA below:
25 // algorithm begin
26 // algorithm end
27 
28 using namespace OHOS;
29 using namespace OHOS::Media::VideoProcessingEngine;
30 
31 namespace {
32 const std::string DYNAMIC_ALGORITHM_LIBRARY_PATH = "libvideoprocessingengineservice_ext.z.so";
33 VpeAlgorithmCreatorMap g_creators = {
34     // NOTE: Add static algorithm which would be called by VPE SA below:
35     // algorithm begin
36     // algorithm end
37 };
38 }
39 
VideoProcessingAlgorithmFactory()40 VideoProcessingAlgorithmFactory::VideoProcessingAlgorithmFactory()
41 {
42     VPE_LOGD("step in");
43     if (!LoadDynamicAlgorithm(DYNAMIC_ALGORITHM_LIBRARY_PATH)) {
44         UnloadDynamicAlgorithm();
45     }
46     GenerateFeatureIDs();
47 }
48 
~VideoProcessingAlgorithmFactory()49 VideoProcessingAlgorithmFactory::~VideoProcessingAlgorithmFactory()
50 {
51     UnloadDynamicAlgorithm();
52 }
53 
Create(const std::string & feature) const54 std::shared_ptr<IVideoProcessingAlgorithm> VideoProcessingAlgorithmFactory::Create(const std::string& feature) const
55 {
56     auto it = g_creators.find(feature);
57     if (it == g_creators.end()) {
58         return nullptr;
59     }
60     return it->second.creator(feature, it->second.id);
61 }
62 
LoadDynamicAlgorithm(const std::string & path)63 bool VideoProcessingAlgorithmFactory::LoadDynamicAlgorithm(const std::string& path)
64 {
65     handle_ = dlopen(path.c_str(), RTLD_NOW);
66     if (handle_ == nullptr) {
67         VPE_LOGD("Can't open library '%{public}s' - %{public}s", path.c_str(), dlerror());
68         return false;
69     }
70 
71     using GetCreator = VpeAlgorithmCreatorMap* (*)();
72     auto getCreator = reinterpret_cast<GetCreator>(dlsym(handle_, "GetDynamicAlgorithmCreator"));
73     if (getCreator == nullptr) {
74         VPE_LOGD("Failed to locate GetDynamicAlgorithmCreator in '%{public}s' - %{public}s", path.c_str(), dlerror());
75         return false;
76     }
77 
78     auto dynamicAlgorithms = getCreator();
79     if (dynamicAlgorithms == nullptr) {
80         VPE_LOGD("Failed to GetDynamicAlgorithmCreator() from '%{public}s'", path.c_str());
81         return false;
82     }
83 
84     auto staticSize = g_creators.size();
85     auto dynamicSize = dynamicAlgorithms->size();
86     g_creators.merge(*dynamicAlgorithms);
87     VPE_LOGI("Algorithms: { static:%{public}zu + dynamic:%{public}zu -> total:%{public}zu }",
88         staticSize, dynamicSize, g_creators.size());
89     return true;
90 }
91 
UnloadDynamicAlgorithm()92 void VideoProcessingAlgorithmFactory::UnloadDynamicAlgorithm()
93 {
94     if (handle_ != nullptr) {
95         dlclose(handle_);
96         handle_ = nullptr;
97     }
98 }
99 
GenerateFeatureIDs()100 void VideoProcessingAlgorithmFactory::GenerateFeatureIDs()
101 {
102     // When GenerateFeatureIDs be called, the g_creators size is fixed.
103     // And we fill algorithm feature ID here from 1 to N.
104     uint32_t id = 0;
105     for (auto& creatorInfo : g_creators) {
106         creatorInfo.second.id = ++id;
107     }
108 }
109