1 /*
2 * Copyright 2022 Shenzhen Kaihong DID 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 "component_mgr.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #define HDF_LOG_TAG codec_hdi_server
20
21 namespace OHOS {
22 namespace Codec {
23 namespace Omx {
ComponentMgr()24 ComponentMgr::ComponentMgr()
25 {
26 AddVendorComponent();
27 AddSoftComponent();
28 }
29
~ComponentMgr()30 ComponentMgr::~ComponentMgr()
31 {
32 CleanComponent();
33 }
34
CreateComponentInstance(const char * componentName,const OMX_CALLBACKTYPE * callbacks,void * appData,OMX_COMPONENTTYPE ** component)35 int32_t ComponentMgr::CreateComponentInstance(const char *componentName, const OMX_CALLBACKTYPE *callbacks,
36 void *appData, OMX_COMPONENTTYPE **component)
37 {
38 int32_t err = HDF_ERR_INVALID_PARAM;
39 std::lock_guard<std::mutex> lk(mutex_);
40
41 auto iter = compoentsCore_.find(componentName);
42 if (iter == compoentsCore_.end() || iter->second == nullptr) {
43 HDF_LOGE("%{public}s: can not find component[%{public}s] in core", __func__, componentName);
44 return err;
45 }
46 auto core = iter->second;
47 OMX_HANDLETYPE handle = nullptr;
48 std::string name(componentName);
49 err = core->GetHandle(handle, name, appData, *callbacks);
50 if (err == OMX_ErrorNone && handle) {
51 OMXComponent comp;
52 comp.core = core;
53 *component = reinterpret_cast<OMX_COMPONENTTYPE *>(handle);
54 comp.handle = handle;
55 components_.push_back(comp);
56 }
57 return err;
58 }
59
DeleteComponentInstance(OMX_COMPONENTTYPE * component)60 int32_t ComponentMgr::DeleteComponentInstance(OMX_COMPONENTTYPE *component)
61 {
62 int32_t err = OMX_ErrorInvalidComponent;
63 for (size_t i = 0; i < components_.size(); i++) {
64 if (components_[i].handle == component) {
65 err = components_[i].core->FreeHandle(components_[i].handle);
66 components_.erase(components_.begin() + i);
67 break;
68 }
69 }
70 return err;
71 }
72
GetRolesForComponent(const char * componentName,std::vector<std::string> * roles)73 int32_t ComponentMgr::GetRolesForComponent(const char *componentName, std::vector<std::string> *roles)
74 {
75 (void)roles;
76 (void)componentName;
77 return OMX_ErrorNone;
78 }
79
AddVendorComponent()80 void ComponentMgr::AddVendorComponent()
81 {
82 std::string path = HDF_LIBRARY_FULL_PATH("libOMX_Core");
83 AddComponentByLibName(path.c_str());
84 }
85
AddSoftComponent()86 void ComponentMgr::AddSoftComponent()
87 {}
88
AddComponentByLibName(const char * libName)89 void ComponentMgr::AddComponentByLibName(const char *libName)
90 {
91 auto core = std::make_shared<CodecOMXCore>();
92 core->Init(libName);
93 std::lock_guard<std::mutex> lk(mutex_);
94 cores_.emplace_back(core);
95 std::string name("");
96 uint32_t index = 0;
97 while (HDF_SUCCESS == core->ComponentNameEnum(name, index)) {
98 ++index;
99 compoentsCore_.emplace(std::make_pair(name, core));
100 }
101 }
102
CleanComponent()103 void ComponentMgr::CleanComponent()
104 {
105 std::lock_guard<std::mutex> lk(mutex_);
106 for (size_t i = 0; i < components_.size(); i++) {
107 components_[i].core->FreeHandle(components_[i].handle);
108 }
109 components_.clear();
110
111 for (size_t i = 0; i < cores_.size(); i++) {
112 cores_[i]->DeInit();
113 }
114 cores_.clear();
115
116 compoentsCore_.clear();
117 }
118
GetCoreOfComponent(CodecOMXCore * & core,const char * componentName)119 int32_t ComponentMgr::GetCoreOfComponent(CodecOMXCore* &core, const char *componentName)
120 {
121 auto iter = compoentsCore_.find(componentName);
122 if (iter == compoentsCore_.end() || iter->second == nullptr) {
123 HDF_LOGE("%{public}s: can not find component[%{public}s] in core", __func__, componentName);
124 return HDF_FAILURE;
125 }
126 core = iter->second.get();
127 return HDF_SUCCESS;
128 }
129 } // namespace Omx
130 } // namespace Codec
131 } // namespace OHOS
132