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 std::string bundleName = GetClientBundleName(appUid);
102 (void) bundleName;
103
104 MEDIA_LOGI("LoadHistreamerEngine succeed!");
105 std::vector<std::string> allFiles;
106 GetDirFiles(MEDIA_ENGINE_LIB_PATH, allFiles);
107 for (auto &file : allFiles) {
108 std::string::size_type namePos = file.find(MEDIA_ENGINE_LIB_NAME_HISTREAMER);
109 if (namePos == std::string::npos) {
110 continue;
111 } else {
112 CHECK_AND_RETURN_RET_LOG(LoadLib(file) == MSERR_OK, MSERR_OPEN_FILE_FAILED, "LoadLib failed");
113 histreamerLoad_ = true;
114 break;
115 }
116 }
117
118 return MSERR_OK;
119 }
120
GetEngineFactory(IEngineFactory::Scene scene,const int32_t & appUid,const std::string & uri)121 std::shared_ptr<IEngineFactory> EngineFactoryRepo::GetEngineFactory(
122 IEngineFactory::Scene scene, const int32_t& appUid, const std::string &uri)
123 {
124 std::string bundleName = GetClientBundleName(appUid);
125 (void)LoadHistreamerEngine(appUid);
126
127 if (factorys_.empty()) {
128 histreamerLoad_ = false;
129 MEDIA_LOGE("Failed to load media engine library");
130 return nullptr;
131 }
132
133 int32_t maxScore = std::numeric_limits<int32_t>::min();
134 std::shared_ptr<IEngineFactory> target = nullptr;
135 for (auto &factory : factorys_) {
136 int32_t score = factory->Score(scene, appUid, uri);
137 if (maxScore < score) {
138 maxScore = score;
139 target = factory;
140 }
141 }
142 if (target == nullptr && !factorys_.empty()) {
143 target = factorys_.front();
144 }
145
146 MEDIA_LOGD("Selected factory: 0x%{public}06" PRIXPTR ", score: %{public}d,"
147 "appUid: %{public}d", FAKE_POINTER(target.get()), maxScore, appUid);
148 return target;
149 }
150 } // namespace Media
151 } // namespace OHOS
152