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