• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory.h>
21 #include <securec.h>
22 #include "codec_log_wrapper.h"
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_ != NULL) {
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 nullptr.");
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 nullptr.");
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 nullptr.");
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 }
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         CODEC_LOGE("getRoles_ is null.");
105         return HDF_ERR_INVALID_PARAM;
106     }
107     if (name.empty()) {
108         CODEC_LOGE("empty name");
109         return HDF_ERR_INVALID_PARAM;
110     }
111     uint32_t roleCount = 0;
112     uint32_t err = (*getRoles_)(const_cast<char *>(name.c_str()), &roleCount, nullptr);
113     if (err != HDF_SUCCESS) {
114         CODEC_LOGE("get roleCount return err [%{public}x].", err);
115         return err;
116     }
117     if (roleCount == 0) {
118         return err;
119     }
120 
121     char *role[roleCount];
122     char array[roleCount][OMX_MAX_STRINGNAME_SIZE];
123     for (uint32_t i = 0; i < roleCount; i++) {
124         int32_t ret = memset_s(array[i], OMX_MAX_STRINGNAME_SIZE, 0, OMX_MAX_STRINGNAME_SIZE);
125         if (ret != EOK) {
126             CODEC_LOGE("memset_s array err [%{public}d].", ret);
127             return ret;
128         }
129         role[i] = array[i];
130     }
131 
132     uint32_t roleLen = roleCount;
133     err = (*getRoles_)(const_cast<char *>(name.c_str()), &roleCount, reinterpret_cast<OMX_U8 **>(role));
134     if (err != HDF_SUCCESS) {
135         CODEC_LOGE("get roles return err [%{public}x].", err);
136         return err;
137     }
138     for (uint32_t i = 0; i < roleLen; i++) {
139         roles.push_back(role[i]);
140     }
141 
142     return err;
143 }
144 }  // namespace Omx
145 }  // namespace Codec
146 }  // namespace OHOS
147