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