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_component_service.h"
17 #include <hdf_base.h>
18 #include <securec.h>
19 #include <malloc.h>
20 #include "codec_log_wrapper.h"
21 namespace OHOS {
22 namespace HDI {
23 namespace Codec {
24 namespace V1_0 {
CodecComponentService(const std::shared_ptr<OHOS::Codec::Omx::ComponentNode> & node,const std::shared_ptr<OHOS::Codec::Omx::ComponentMgr> mgr,const std::string name)25 CodecComponentService::CodecComponentService(const std::shared_ptr<OHOS::Codec::Omx::ComponentNode> &node,
26 const std::shared_ptr<OHOS::Codec::Omx::ComponentMgr> mgr, const std::string name)
27 {
28 name_ = name;
29 node_ = node;
30 mgr_ = mgr;
31 #ifdef SUPPORT_ROLE
32 SetComponentRole();
33 #endif
34 }
~CodecComponentService()35 CodecComponentService::~CodecComponentService()
36 {
37 if (node_ != nullptr) {
38 int32_t ret = node_->CloseHandle();
39 if (ret != HDF_SUCCESS) {
40 CODEC_LOGE("CloseHandle failed, err[%{public}d]", ret);
41 }
42 node_ = nullptr;
43 ReleaseCache();
44 }
45 name_ = "";
46 mgr_ = nullptr;
47 }
48
ReleaseCache()49 void CodecComponentService::ReleaseCache()
50 {
51 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
52 auto err = mallopt(M_FLUSH_THREAD_CACHE, 0);
53 if (err != HDF_SUCCESS) {
54 CODEC_LOGE("release cache error, m_purge = %{public}d", err);
55 }
56 #endif
57 }
58
GetComponentVersion(CompVerInfo & verInfo)59 int32_t CodecComponentService::GetComponentVersion(CompVerInfo &verInfo)
60 {
61 return node_->GetComponentVersion(verInfo);
62 }
63
SendCommand(CodecCommandType cmd,uint32_t param,const std::vector<int8_t> & cmdData)64 int32_t CodecComponentService::SendCommand(CodecCommandType cmd, uint32_t param, const std::vector<int8_t> &cmdData)
65 {
66 CODEC_LOGI("commandType: [%{public}d], command [%{public}d]", cmd, param);
67 return node_->SendCommand(cmd, param, const_cast<int8_t *>(cmdData.data()));
68 }
69
GetParameter(uint32_t index,const std::vector<int8_t> & inParamStruct,std::vector<int8_t> & outParamStruct)70 int32_t CodecComponentService::GetParameter(uint32_t index, const std::vector<int8_t> &inParamStruct,
71 std::vector<int8_t> &outParamStruct)
72 {
73 CODEC_LOGI("index [%{public}x]", index);
74 outParamStruct = inParamStruct;
75 return node_->GetParameter(static_cast<enum OMX_INDEXTYPE>(index), outParamStruct.data());
76 }
77
SetParameter(uint32_t index,const std::vector<int8_t> & paramStruct)78 int32_t CodecComponentService::SetParameter(uint32_t index, const std::vector<int8_t> ¶mStruct)
79 {
80 CODEC_LOGI("index [%{public}x]", index);
81 return node_->SetParameter(static_cast<enum OMX_INDEXTYPE>(index), paramStruct.data());
82 }
83
GetConfig(uint32_t index,const std::vector<int8_t> & inCfgStruct,std::vector<int8_t> & outCfgStruct)84 int32_t CodecComponentService::GetConfig(uint32_t index, const std::vector<int8_t> &inCfgStruct,
85 std::vector<int8_t> &outCfgStruct)
86 {
87 CODEC_LOGI("index [%{public}x]", index);
88 outCfgStruct = inCfgStruct;
89 return node_->GetConfig(static_cast<enum OMX_INDEXTYPE>(index), outCfgStruct.data());
90 }
91
SetConfig(uint32_t index,const std::vector<int8_t> & cfgStruct)92 int32_t CodecComponentService::SetConfig(uint32_t index, const std::vector<int8_t> &cfgStruct)
93 {
94 CODEC_LOGI("index [%{public}x]", index);
95 return node_->SetConfig(static_cast<enum OMX_INDEXTYPE>(index), cfgStruct.data());
96 }
97
GetExtensionIndex(const std::string & paramName,uint32_t & indexType)98 int32_t CodecComponentService::GetExtensionIndex(const std::string ¶mName, uint32_t &indexType)
99 {
100 CODEC_LOGI("paramName [%{public}s]", paramName.c_str());
101 return node_->GetExtensionIndex(paramName.c_str(), indexType);
102 }
103
GetState(CodecStateType & state)104 int32_t CodecComponentService::GetState(CodecStateType &state)
105 {
106 return node_->GetState(state);
107 }
108
ComponentTunnelRequest(uint32_t port,int32_t tunneledComp,uint32_t tunneledPort,const CodecTunnelSetupType & inTunnelSetup,CodecTunnelSetupType & outTunnelSetup)109 int32_t CodecComponentService::ComponentTunnelRequest(uint32_t port, int32_t tunneledComp, uint32_t tunneledPort,
110 const CodecTunnelSetupType &inTunnelSetup,
111 CodecTunnelSetupType &outTunnelSetup)
112 {
113 CODEC_LOGI("port [%{public}d]", port);
114 outTunnelSetup = inTunnelSetup;
115 return node_->ComponentTunnelRequest(port, tunneledComp, tunneledPort, outTunnelSetup);
116 }
117
UseBuffer(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer)118 int32_t CodecComponentService::UseBuffer(uint32_t portIndex, const OmxCodecBuffer &inBuffer, OmxCodecBuffer &outBuffer)
119 {
120 CODEC_LOGI("portIndex: [%{public}d]", portIndex);
121 outBuffer = inBuffer;
122 return node_->UseBuffer(portIndex, outBuffer);
123 }
124
AllocateBuffer(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer)125 int32_t CodecComponentService::AllocateBuffer(uint32_t portIndex, const OmxCodecBuffer &inBuffer,
126 OmxCodecBuffer &outBuffer)
127 {
128 CODEC_LOGI("portIndex: [%{public}d]", portIndex);
129 outBuffer = inBuffer;
130 return node_->AllocateBuffer(portIndex, outBuffer);
131 }
132
FreeBuffer(uint32_t portIndex,const OmxCodecBuffer & buffer)133 int32_t CodecComponentService::FreeBuffer(uint32_t portIndex, const OmxCodecBuffer &buffer)
134 {
135 CODEC_LOGI("portIndex: [%{public}d], bufferId: [%{public}d]", portIndex, buffer.bufferId);
136 int32_t ret = node_->FreeBuffer(portIndex, buffer);
137 ReleaseCache();
138 return ret;
139 }
140
EmptyThisBuffer(const OmxCodecBuffer & buffer)141 int32_t CodecComponentService::EmptyThisBuffer(const OmxCodecBuffer &buffer)
142 {
143 return node_->EmptyThisBuffer(const_cast<OmxCodecBuffer &>(buffer));
144 }
145
FillThisBuffer(const OmxCodecBuffer & buffer)146 int32_t CodecComponentService::FillThisBuffer(const OmxCodecBuffer &buffer)
147 {
148 return node_->FillThisBuffer(const_cast<OmxCodecBuffer &>(buffer));
149 }
150
SetCallbacks(const sptr<ICodecCallback> & callbacks,int64_t appData)151 int32_t CodecComponentService::SetCallbacks(const sptr<ICodecCallback> &callbacks, int64_t appData)
152 {
153 CODEC_LOGI("service impl!");
154 CHECK_AND_RETURN_RET_LOG(callbacks != nullptr, HDF_ERR_INVALID_PARAM, "callbacks is null");
155 return node_->SetCallbacks(callbacks, appData);
156 }
157
ComponentDeInit()158 int32_t CodecComponentService::ComponentDeInit()
159 {
160 CODEC_LOGI("service impl!");
161 return node_->ComponentDeInit();
162 }
163
UseEglImage(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer,const std::vector<int8_t> & eglImage)164 int32_t CodecComponentService::UseEglImage(uint32_t portIndex, const OmxCodecBuffer &inBuffer,
165 OmxCodecBuffer &outBuffer, const std::vector<int8_t> &eglImage)
166 {
167 CODEC_LOGI("portIndex [%{public}d]", portIndex);
168 outBuffer = inBuffer;
169 return node_->UseEglImage(outBuffer, portIndex, eglImage.data());
170 }
171
ComponentRoleEnum(std::vector<uint8_t> & role,uint32_t index)172 int32_t CodecComponentService::ComponentRoleEnum(std::vector<uint8_t> &role, uint32_t index)
173 {
174 CODEC_LOGI("index [%{public}d]", index);
175 return node_->ComponentRoleEnum(role, index);
176 }
177
SetComponentRole()178 void CodecComponentService::SetComponentRole()
179 {
180 if (name_ == "") {
181 CODEC_LOGE("compName is null");
182 return;
183 }
184 OHOS::Codec::Omx::CodecOMXCore *core;
185 auto err = mgr_->GetCoreOfComponent(core, name_);
186 if (err != HDF_SUCCESS) {
187 CODEC_LOGE("core is null");
188 return;
189 }
190
191 std::vector<std::string> roles;
192 int32_t ret = core->GetRolesOfComponent(name_, roles);
193 if (ret != HDF_SUCCESS) {
194 CODEC_LOGE("GetRoleOfComponent return err [%{public}d]", ret);
195 return;
196 }
197 uint32_t roleIndex = 0;
198 CODEC_LOGI("RoleName = [%{public}s]", roles[roleIndex].c_str());
199
200 OMX_PARAM_COMPONENTROLETYPE role;
201 errno_t res = strncpy_s(reinterpret_cast<char *>(role.cRole), OMX_MAX_STRINGNAME_SIZE,
202 roles[roleIndex].c_str(), roles[roleIndex].length());
203 if (res != EOK) {
204 CODEC_LOGE("strncpy_s return err [%{public}d]", err);
205 return;
206 }
207 role.nSize = sizeof(role);
208 ret = node_->SetParameter(OMX_IndexParamStandardComponentRole, reinterpret_cast<int8_t *>(&role));
209 if (ret != HDF_SUCCESS) {
210 CODEC_LOGE("OMX_IndexParamStandardComponentRole err [%{public}d]", ret);
211 }
212 }
213
GetComponentCompName()214 std::string &CodecComponentService::GetComponentCompName()
215 {
216 return name_;
217 }
218
GetComponentNode(std::shared_ptr<OHOS::Codec::Omx::ComponentNode> & dumpNode_)219 void CodecComponentService::GetComponentNode(std::shared_ptr<OHOS::Codec::Omx::ComponentNode> &dumpNode_)
220 {
221 dumpNode_ = node_;
222 }
223
224 } // namespace V1_0
225 } // namespace Codec
226 } // namespace HDI
227 } // namespace OHOS
228