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 "codec_omx_core.h"
17 #include <OMX_Core.h>
18 #include <dlfcn.h>
19 #include <hdf_base.h>
20 #include <hdf_log.h>
21 #include <securec.h>
22
23 #define HDF_LOG_TAG codec_hdi_server
24
25 namespace OHOS {
26 namespace Codec {
27 namespace Omx {
~CodecOMXCore()28 CodecOMXCore::~CodecOMXCore()
29 {
30 if (libHandle_ != nullptr) {
31 dlclose(libHandle_);
32 }
33 }
Init(const std::string & libName)34 int32_t CodecOMXCore::Init(const std::string &libName)
35 {
36 char pathBuf[PATH_MAX] = {'\0'};
37 if (realpath(libName.c_str(), pathBuf) == nullptr) {
38 HDF_LOGE("%{public}s: realpath failed!", __func__);
39 return HDF_ERR_INVALID_PARAM;
40 }
41
42 libHandle_ = dlopen(pathBuf, RTLD_LAZY);
43 if (libHandle_ == nullptr) {
44 HDF_LOGE("%{public}s:failed to dlopen %{public}s.", __func__, libName.c_str());
45 return HDF_ERR_INVALID_PARAM;
46 }
47
48 init_ = reinterpret_cast<InitFunc>(dlsym(libHandle_, "OMX_Init"));
49 deInit_ = reinterpret_cast<DeinitFunc>(dlsym(libHandle_, "OMX_Deinit"));
50 getHandle_ = reinterpret_cast<GetHandleFunc>(dlsym(libHandle_, "OMX_GetHandle"));
51 freeHandle_ = reinterpret_cast<FreeHandleFunc>(dlsym(libHandle_, "OMX_FreeHandle"));
52 getRoles_ = reinterpret_cast<GetRolesOfComponentFunc>(dlsym(libHandle_, "OMX_GetRolesOfComponent"));
53 componentNameEnum_ = reinterpret_cast<ComponentNameEnumFunc>(dlsym(libHandle_, "OMX_ComponentNameEnum"));
54
55 if (init_ != nullptr) {
56 (*(init_))();
57 }
58 return HDF_SUCCESS;
59 }
60
DeInit()61 void CodecOMXCore::DeInit()
62 {
63 if (deInit_) {
64 (*deInit_)();
65 }
66 }
67
GetHandle(OMX_HANDLETYPE & handle,std::string & compName,OMX_PTR appData,const OMX_CALLBACKTYPE & callbacks)68 int32_t CodecOMXCore::GetHandle(OMX_HANDLETYPE &handle, std::string &compName, OMX_PTR appData,
69 const OMX_CALLBACKTYPE &callbacks)
70 {
71 if (getHandle_ == nullptr) {
72 HDF_LOGE("%{public}s: getHandle is null.", __func__);
73 return HDF_ERR_INVALID_PARAM;
74 }
75 return (*getHandle_)(&handle, const_cast<char *>(compName.c_str()), appData, (OMX_CALLBACKTYPE *)&callbacks);
76 }
77
FreeHandle(OMX_HANDLETYPE handle)78 int32_t CodecOMXCore::FreeHandle(OMX_HANDLETYPE handle)
79 {
80 if (freeHandle_ == nullptr) {
81 HDF_LOGE("%{public}s: freeHandle_ is null.", __func__);
82 return HDF_ERR_INVALID_PARAM;
83 }
84 return (*freeHandle_)(handle);
85 }
86
ComponentNameEnum(std::string & name,uint32_t index)87 int32_t CodecOMXCore::ComponentNameEnum(std::string &name, uint32_t index)
88 {
89 if (componentNameEnum_ == nullptr) {
90 HDF_LOGE("%{public}s: componentNameEnum is null.", __func__);
91 return HDF_ERR_INVALID_PARAM;
92 }
93 char tmpComponentName[OMX_MAX_STRINGNAME_SIZE] = {0};
94 uint32_t err = (*(componentNameEnum_))(tmpComponentName, OMX_MAX_STRINGNAME_SIZE, index);
95 if (err == HDF_SUCCESS) {
96 name = tmpComponentName;
97 }
98 return err;
99 }
100
GetRolesOfComponent(std::string & name,std::vector<std::string> & roles)101 int32_t CodecOMXCore::GetRolesOfComponent(std::string &name, std::vector<std::string> &roles)
102 {
103 if (getRoles_ == nullptr) {
104 HDF_LOGE("%{public}s: getRoles is null.", __func__);
105 return HDF_ERR_INVALID_PARAM;
106 }
107 uint32_t roleCount = 0;
108 int32_t err = (*getRoles_)(const_cast<char *>(name.c_str()), &roleCount, nullptr);
109 if (err != HDF_SUCCESS) {
110 HDF_LOGE("%{public}s: getRoles_ nullptr return err [%{public}x].", __func__, err);
111 return err;
112 }
113 if (roleCount == 0) {
114 HDF_LOGE("%{public}s: roleCount = 0 ", __func__);
115 return err;
116 }
117
118 char *role[roleCount];
119 char array[roleCount][OMX_MAX_STRINGNAME_SIZE];
120 for (uint32_t i = 0; i < roleCount; i++) {
121 int32_t ret = memset_s(array[i], OMX_MAX_STRINGNAME_SIZE, 0, OMX_MAX_STRINGNAME_SIZE);
122 if (ret != EOK) {
123 HDF_LOGE("%{public}s: memset_s array err [%{public}x].", __func__, ret);
124 return ret;
125 }
126 role[i] = array[i];
127 }
128
129 uint32_t roleLen = roleCount;
130 err = (*getRoles_)(const_cast<char *>(name.c_str()), &roleCount, reinterpret_cast<OMX_U8 **>(role));
131 if (err != HDF_SUCCESS) {
132 HDF_LOGE("%{public}s: getRoles_ pRole return err [%{public}x].", __func__, err);
133 return err;
134 }
135 for (uint32_t i = 0; i < roleLen; i++) {
136 roles.push_back(role[i]);
137 }
138
139 return err;
140 }
141 } // namespace Omx
142 } // namespace Codec
143 } // namespace OHOS