• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "engine_factory_repo.h"
17 #include <limits>
18 #include <cinttypes>
19 #include <dlfcn.h>
20 #include "directory_ex.h"
21 #include "media_errors.h"
22 #include "media_log.h"
23 
24 namespace {
25     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "EngineFactoryRepo"};
26 #ifdef __aarch64__
27     static const std::string MEDIA_ENGINE_LIB_PATH = "/system/lib64/media";
28 #else
29     static const std::string MEDIA_ENGINE_LIB_PATH = "/system/lib/media";
30 #endif
31     static const std::string MEDIA_ENGINE_LIB_NAME_PREFIX = "libmedia_engine_";
32     static const std::string MEDIA_ENGINE_LIB_NAME_SUFFIX = ".z.so";
33     static const std::string MEDIA_ENGINE_ENTRY_SYMBOL = "CreateEngineFactory";
34 }
35 
36 namespace OHOS {
37 namespace Media {
38 using CreateFactoryFunc = IEngineFactory *(*)();
39 
IsAlphaNumUnderLine(const std::string str)40 static bool IsAlphaNumUnderLine(const std::string str)
41 {
42     for (auto &c : str) {
43         if (!(std::isalnum(c) || c == '_')) {
44             return false;
45         }
46     }
47     return true;
48 }
49 
GetMediaEngineLibs()50 static std::vector<std::string> GetMediaEngineLibs()
51 {
52     std::vector<std::string> allFiles;
53     std::vector<std::string> allLibs;
54     GetDirFiles(MEDIA_ENGINE_LIB_PATH, allFiles);
55     for (auto &file : allFiles) {
56         std::string::size_type namePos = file.find(MEDIA_ENGINE_LIB_NAME_PREFIX);
57         if (namePos == std::string::npos) {
58             continue;
59         }
60         namePos += MEDIA_ENGINE_LIB_NAME_PREFIX.size();
61         std::string::size_type nameEnd = file.rfind(MEDIA_ENGINE_LIB_NAME_SUFFIX);
62         if ((nameEnd == std::string::npos) || (nameEnd + MEDIA_ENGINE_LIB_NAME_SUFFIX.size() != file.size())) {
63             continue;
64         }
65         MEDIA_LOGI("lib: %{public}s", file.c_str());
66         if (!IsAlphaNumUnderLine(file.substr(namePos, nameEnd - namePos))) {
67             continue;
68         }
69         MEDIA_LOGI("find media engine library: %{public}s", file.c_str());
70         allLibs.push_back(file);
71     }
72 
73     return allLibs;
74 }
75 
Instance()76 EngineFactoryRepo &EngineFactoryRepo::Instance()
77 {
78     static EngineFactoryRepo inst;
79     return inst;
80 }
81 
~EngineFactoryRepo()82 EngineFactoryRepo::~EngineFactoryRepo()
83 {
84     for (auto &lib : factoryLibs_) {
85         if (lib != nullptr) {
86             (void)dlclose(lib);
87         }
88     }
89 }
90 
Init()91 int32_t EngineFactoryRepo::Init()
92 {
93     std::unique_lock<std::mutex> lock(mutex_);
94     if (inited_) {
95         return MSERR_OK;
96     }
97 
98     std::vector<std::string> allLibs = GetMediaEngineLibs();
99     for (auto &lib : allLibs) {
100         LoadLib(lib);
101     }
102     MEDIA_LOGI("load engine factory count: %{public}zu", factorys_.size());
103 
104     inited_ = true;
105     return MSERR_OK;
106 }
107 
LoadLib(const std::string & libPath)108 void EngineFactoryRepo::LoadLib(const std::string &libPath)
109 {
110     void *handle = dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL);
111     if (handle == nullptr) {
112         MEDIA_LOGE("failed to dlopen %{public}s, errno:%{public}d, errormsg:%{public}s",
113                    libPath.c_str(), errno, dlerror());
114         return;
115     }
116 
117     CreateFactoryFunc entry = reinterpret_cast<CreateFactoryFunc>(dlsym(handle, MEDIA_ENGINE_ENTRY_SYMBOL.c_str()));
118     if (entry == nullptr) {
119         MEDIA_LOGE("failed to dlsym %{public}s for lib %{public}s, errno:%{public}d, errormsg:%{public}s",
120             MEDIA_ENGINE_ENTRY_SYMBOL.c_str(), libPath.c_str(), errno, dlerror());
121         (void)dlclose(handle);
122         return;
123     }
124 
125     std::shared_ptr<IEngineFactory> factory = std::shared_ptr<IEngineFactory>(entry());
126     if (factory == nullptr) {
127         MEDIA_LOGE("failed to create engine factory for lib: %{public}s", libPath.c_str());
128         (void)dlclose(handle);
129         return;
130     }
131 
132     factoryLibs_.push_back(handle);
133     factorys_.push_back(factory);
134 }
135 
GetEngineFactory(IEngineFactory::Scene scene,const std::string & uri)136 std::shared_ptr<IEngineFactory> EngineFactoryRepo::GetEngineFactory(
137     IEngineFactory::Scene scene, const std::string &uri)
138 {
139     (void)Init();
140 
141     int32_t maxScore = std::numeric_limits<int32_t>::min();
142     std::shared_ptr<IEngineFactory> target = nullptr;
143     for (auto &factory : factorys_) {
144         int32_t score = factory->Score(scene, uri);
145         if (maxScore < score) {
146             maxScore = score;
147             target = factory;
148         }
149     }
150     if (target == nullptr && !factorys_.empty()) {
151         target = factorys_.front();
152     }
153 
154     MEDIA_LOGI("Selected factory: 0x%{public}06" PRIXPTR ", score: %{public}d", FAKE_POINTER(target.get()), maxScore);
155     return target;
156 }
157 } // namespace Media
158 } // namespace OHOS
159