• 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 #include "media_utils.h"
24 
25 namespace {
26     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "EngineFactoryRepo"};
27 #if (defined(__aarch64__) || defined(__x86_64__))
28     static const std::string MEDIA_ENGINE_LIB_PATH = "/system/lib64/media";
29 #else
30     static const std::string MEDIA_ENGINE_LIB_PATH = "/system/lib/media";
31 #endif
32     static const std::string MEDIA_ENGINE_LIB_NAME_HISTREAMER = "libmedia_engine_histreamer.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 
Instance()40 EngineFactoryRepo &EngineFactoryRepo::Instance()
41 {
42     static EngineFactoryRepo* inst = nullptr;
43     static std::once_flag once;
44     std::call_once(once, [&] { inst = new EngineFactoryRepo(); });
45     return *inst;
46 }
47 
~EngineFactoryRepo()48 EngineFactoryRepo::~EngineFactoryRepo()
49 {
50     UnloadLib();
51 }
52 
UnloadLib()53 void __attribute__((no_sanitize("cfi"))) EngineFactoryRepo::UnloadLib()
54 {
55     factorys_.clear();
56     for (auto &lib : factoryLibs_) {
57         if (lib != nullptr) {
58             (void)dlclose(lib);
59             lib = nullptr;
60         }
61     }
62 }
63 
LoadLib(const std::string & libPath)64 int32_t __attribute__((no_sanitize("cfi"))) EngineFactoryRepo::LoadLib(const std::string &libPath)
65 {
66     void *handle = dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL);
67     if (handle == nullptr) {
68         MEDIA_LOGE("failed to dlopen %{public}s, errno:%{public}d, errormsg:%{public}s",
69                    libPath.c_str(), errno, dlerror());
70         return MSERR_OPEN_FILE_FAILED;
71     }
72 
73     CreateFactoryFunc entry = reinterpret_cast<CreateFactoryFunc>(dlsym(handle, MEDIA_ENGINE_ENTRY_SYMBOL.c_str()));
74     if (entry == nullptr) {
75         MEDIA_LOGE("failed to dlsym %{public}s for lib %{public}s, errno:%{public}d, errormsg:%{public}s",
76             MEDIA_ENGINE_ENTRY_SYMBOL.c_str(), libPath.c_str(), errno, dlerror());
77         (void)dlclose(handle);
78         return MSERR_OPEN_FILE_FAILED;
79     }
80 
81     std::shared_ptr<IEngineFactory> factory = std::shared_ptr<IEngineFactory>(entry());
82     if (factory == nullptr) {
83         MEDIA_LOGE("failed to create engine factory for lib: %{public}s", libPath.c_str());
84         (void)dlclose(handle);
85         return MSERR_OPEN_FILE_FAILED;
86     }
87 
88     factoryLibs_.push_back(handle);
89     factorys_.push_back(factory);
90     return MSERR_OK;
91 }
92 
LoadHistreamerEngine(const int32_t & appUid)93 int32_t EngineFactoryRepo::LoadHistreamerEngine(const int32_t& appUid)
94 {
95     std::unique_lock<std::mutex> lock(mutex_);
96     if (histreamerLoad_) {
97         MEDIA_LOGD("Histreamer is enabled");
98         return MSERR_OK;
99     }
100 
101     MEDIA_LOGI("LoadHistreamerEngine succeed!");
102     std::vector<std::string> allFiles;
103     GetDirFiles(MEDIA_ENGINE_LIB_PATH, allFiles);
104     for (auto &file : allFiles) {
105         std::string::size_type namePos = file.find(MEDIA_ENGINE_LIB_NAME_HISTREAMER);
106         if (namePos == std::string::npos) {
107             continue;
108         } else {
109             CHECK_AND_RETURN_RET_LOG(LoadLib(file) == MSERR_OK, MSERR_OPEN_FILE_FAILED, "LoadLib failed");
110             histreamerLoad_ = true;
111             break;
112         }
113     }
114 
115     return MSERR_OK;
116 }
117 
GetEngineFactory(IEngineFactory::Scene scene,const int32_t & appUid,const std::string & uri)118 std::shared_ptr<IEngineFactory> EngineFactoryRepo::GetEngineFactory(
119     IEngineFactory::Scene scene, const int32_t& appUid, const std::string &uri)
120 {
121     MEDIA_LOGD("GetEngineFactory entered.");
122     (void)LoadHistreamerEngine(appUid);
123 
124     if (factorys_.empty()) {
125         histreamerLoad_ = false;
126         MEDIA_LOGE("Failed to load media engine library");
127         return nullptr;
128     }
129 
130     int32_t maxScore = std::numeric_limits<int32_t>::min();
131     std::shared_ptr<IEngineFactory> target = nullptr;
132     for (auto &factory : factorys_) {
133         int32_t score = factory->Score(scene, appUid, uri);
134         if (maxScore < score) {
135             maxScore = score;
136             target = factory;
137         }
138     }
139     if (target == nullptr && !factorys_.empty()) {
140         target = factorys_.front();
141     }
142 
143     MEDIA_LOGD("Selected factory: 0x%{public}06" PRIXPTR ", score: %{public}d,"
144         "appUid: %{public}d", FAKE_POINTER(target.get()), maxScore, appUid);
145     return target;
146 }
147 } // namespace Media
148 } // namespace OHOS
149