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 <hdf_remote_service.h>
19 #include <securec.h>
20 #include <malloc.h>
21 #include <unistd.h>
22 #include <hitrace_meter.h>
23 #include "codec_log_wrapper.h"
24
25 namespace OHOS {
26 namespace HDI {
27 namespace Codec {
28 namespace V4_0 {
29
CodecComponentService(const std::shared_ptr<OHOS::Codec::Omx::ComponentNode> & node,const std::shared_ptr<OHOS::Codec::Omx::ComponentMgr> mgr,const std::string name)30 CodecComponentService::CodecComponentService(const std::shared_ptr<OHOS::Codec::Omx::ComponentNode> &node,
31 const std::shared_ptr<OHOS::Codec::Omx::ComponentMgr> mgr, const std::string name)
32 {
33 name_ = name;
34 node_ = node;
35 mgr_ = mgr;
36 isIPCMode_ = (HdfRemoteGetCallingPid() == getpid() ? false : true);
37 #ifdef SUPPORT_ROLE
38 SetComponentRole();
39 #endif
40 }
~CodecComponentService()41 CodecComponentService::~CodecComponentService()
42 {
43 std::lock_guard<std::mutex> lock(nodeMutex_);
44 if (node_ != nullptr) {
45 node_->ReleaseOMXResource();
46 int32_t ret = node_->CloseHandle();
47 if (ret != HDF_SUCCESS) {
48 CODEC_LOGE("CloseHandle failed, err[%{public}d]", ret);
49 }
50 node_ = nullptr;
51 ReleaseCache();
52 }
53 name_ = "";
54 mgr_ = nullptr;
55 }
56
ReleaseCache()57 void CodecComponentService::ReleaseCache()
58 {
59 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
60 mallopt(M_FLUSH_THREAD_CACHE, 0);
61 #endif
62 }
63
GetComponentVersion(CompVerInfo & verInfo)64 int32_t CodecComponentService::GetComponentVersion(CompVerInfo &verInfo)
65 {
66 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetComponentVersion");
67 std::lock_guard<std::mutex> lock(nodeMutex_);
68 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
69 return node_->GetComponentVersion(verInfo);
70 }
71
SendCommand(CodecCommandType cmd,uint32_t param,const std::vector<int8_t> & cmdData)72 int32_t CodecComponentService::SendCommand(CodecCommandType cmd, uint32_t param, const std::vector<int8_t> &cmdData)
73 {
74 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSendCommand");
75 CODEC_LOGI("commandType: [%{public}d], command [%{public}d]", cmd, param);
76 std::lock_guard<std::mutex> lock(nodeMutex_);
77 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
78 return node_->SendCommand(cmd, param, const_cast<int8_t *>(cmdData.data()));
79 }
80
GetParameter(uint32_t index,const std::vector<int8_t> & inParamStruct,std::vector<int8_t> & outParamStruct)81 int32_t CodecComponentService::GetParameter(uint32_t index, const std::vector<int8_t> &inParamStruct,
82 std::vector<int8_t> &outParamStruct)
83 {
84 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetParameter");
85 CODEC_LOGD("index [%{public}x]", index);
86 if (inParamStruct.empty() || (inParamStruct.size() < sizeof(uint32_t))) {
87 CODEC_LOGE("GetParamStruct is Invalid");
88 return HDF_ERR_INVALID_PARAM;
89 }
90 uint32_t currentSize = *(reinterpret_cast<const uint32_t*>(inParamStruct.data()));
91 if (inParamStruct.size() != currentSize) {
92 CODEC_LOGE("Invalid GetParams");
93 return HDF_ERR_INVALID_PARAM;
94 }
95 outParamStruct = inParamStruct;
96 std::lock_guard<std::mutex> lock(nodeMutex_);
97 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
98 return node_->GetParameter(static_cast<enum OMX_INDEXTYPE>(index), outParamStruct.data());
99 }
100
SetParameter(uint32_t index,const std::vector<int8_t> & paramStruct)101 int32_t CodecComponentService::SetParameter(uint32_t index, const std::vector<int8_t> ¶mStruct)
102 {
103 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSetParameter");
104 CODEC_LOGD("index [%{public}x]", index);
105 if (paramStruct.empty() || (paramStruct.size() < sizeof(uint32_t))) {
106 CODEC_LOGE("SetParamStruct is Invalid");
107 return HDF_ERR_INVALID_PARAM;
108 }
109 uint32_t currentSize = *(reinterpret_cast<const uint32_t*>(paramStruct.data()));
110 if (paramStruct.size() != currentSize) {
111 CODEC_LOGE("Invalid SetParams");
112 return HDF_ERR_INVALID_PARAM;
113 }
114 std::lock_guard<std::mutex> lock(nodeMutex_);
115 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
116 return node_->SetParameter(static_cast<enum OMX_INDEXTYPE>(index), paramStruct.data());
117 }
118
GetConfig(uint32_t index,const std::vector<int8_t> & inCfgStruct,std::vector<int8_t> & outCfgStruct)119 int32_t CodecComponentService::GetConfig(uint32_t index, const std::vector<int8_t> &inCfgStruct,
120 std::vector<int8_t> &outCfgStruct)
121 {
122 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetConfig");
123 CODEC_LOGD("index [%{public}x]", index);
124 if (inCfgStruct.empty() || (inCfgStruct.size() < sizeof(uint32_t))) {
125 CODEC_LOGE("GetCfgStruct is Invalid");
126 return HDF_ERR_INVALID_PARAM;
127 }
128 uint32_t currentSize = *(reinterpret_cast<const uint32_t*>(inCfgStruct.data()));
129 if (inCfgStruct.size() != currentSize) {
130 CODEC_LOGE("Invalid GetConfig");
131 return HDF_ERR_INVALID_PARAM;
132 }
133 outCfgStruct = inCfgStruct;
134 std::lock_guard<std::mutex> lock(nodeMutex_);
135 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
136 return node_->GetConfig(static_cast<enum OMX_INDEXTYPE>(index), outCfgStruct.data());
137 }
138
SetConfig(uint32_t index,const std::vector<int8_t> & cfgStruct)139 int32_t CodecComponentService::SetConfig(uint32_t index, const std::vector<int8_t> &cfgStruct)
140 {
141 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSetConfig");
142 CODEC_LOGD("index [%{public}x]", index);
143 if (cfgStruct.empty() || ((cfgStruct.size() < sizeof(uint32_t)))) {
144 CODEC_LOGE("SetCfgStruct is Invalid");
145 return HDF_ERR_INVALID_PARAM;
146 }
147 uint32_t currentSize = *(reinterpret_cast<const uint32_t*>(cfgStruct.data()));
148 if (cfgStruct.size() != currentSize) {
149 CODEC_LOGE("Invalid SetConfig");
150 return HDF_ERR_INVALID_PARAM;
151 }
152 std::lock_guard<std::mutex> lock(nodeMutex_);
153 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
154 return node_->SetConfig(static_cast<enum OMX_INDEXTYPE>(index), cfgStruct.data());
155 }
156
GetExtensionIndex(const std::string & paramName,uint32_t & indexType)157 int32_t CodecComponentService::GetExtensionIndex(const std::string ¶mName, uint32_t &indexType)
158 {
159 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetExtensionIndex");
160 CODEC_LOGI("paramName [%{public}s]", paramName.c_str());
161 std::lock_guard<std::mutex> lock(nodeMutex_);
162 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
163 return node_->GetExtensionIndex(paramName.c_str(), indexType);
164 }
165
GetState(CodecStateType & state)166 int32_t CodecComponentService::GetState(CodecStateType &state)
167 {
168 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetState");
169 std::lock_guard<std::mutex> lock(nodeMutex_);
170 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
171 return node_->GetState(state);
172 }
173
ComponentTunnelRequest(uint32_t port,int32_t tunneledComp,uint32_t tunneledPort,const CodecTunnelSetupType & inTunnelSetup,CodecTunnelSetupType & outTunnelSetup)174 int32_t CodecComponentService::ComponentTunnelRequest(uint32_t port, int32_t tunneledComp, uint32_t tunneledPort,
175 const CodecTunnelSetupType &inTunnelSetup,
176 CodecTunnelSetupType &outTunnelSetup)
177 {
178 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecComponentTunnelRequest");
179 CODEC_LOGI("port [%{public}d]", port);
180 outTunnelSetup = inTunnelSetup;
181 std::lock_guard<std::mutex> lock(nodeMutex_);
182 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
183 return node_->ComponentTunnelRequest(port, tunneledComp, tunneledPort, outTunnelSetup);
184 }
185
UseBuffer(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer)186 int32_t CodecComponentService::UseBuffer(uint32_t portIndex, const OmxCodecBuffer &inBuffer, OmxCodecBuffer &outBuffer)
187 {
188 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecUseBuffer");
189 CODEC_LOGD("portIndex: [%{public}d]", portIndex);
190 OHOS::Codec::Omx::OmxCodecBuffer internal = OHOS::Codec::Omx::Convert(inBuffer, isIPCMode_);
191 std::lock_guard<std::mutex> lock(nodeMutex_);
192 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
193 int32_t ret = node_->UseBuffer(portIndex, internal);
194 outBuffer = OHOS::Codec::Omx::Convert(internal, isIPCMode_);
195 return ret;
196 }
197
AllocateBuffer(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer)198 int32_t CodecComponentService::AllocateBuffer(uint32_t portIndex, const OmxCodecBuffer &inBuffer,
199 OmxCodecBuffer &outBuffer)
200 {
201 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecAllocateBuffer");
202 CODEC_LOGD("portIndex: [%{public}d]", portIndex);
203 OHOS::Codec::Omx::OmxCodecBuffer internal = OHOS::Codec::Omx::Convert(inBuffer, isIPCMode_);
204 std::lock_guard<std::mutex> lock(nodeMutex_);
205 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
206 int32_t ret = node_->AllocateBuffer(portIndex, internal);
207 outBuffer = OHOS::Codec::Omx::Convert(internal, isIPCMode_);
208 return ret;
209 }
210
FreeBuffer(uint32_t portIndex,const OmxCodecBuffer & buffer)211 int32_t CodecComponentService::FreeBuffer(uint32_t portIndex, const OmxCodecBuffer &buffer)
212 {
213 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecFreeBuffer");
214 CODEC_LOGD("portIndex: [%{public}d], bufferId: [%{public}d]", portIndex, buffer.bufferId);
215 OHOS::Codec::Omx::OmxCodecBuffer internal = OHOS::Codec::Omx::Convert(buffer, isIPCMode_);
216 std::lock_guard<std::mutex> lock(nodeMutex_);
217 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
218 int32_t ret = node_->FreeBuffer(portIndex, internal);
219 ReleaseCache();
220
221 return ret;
222 }
223
EmptyThisBuffer(const OmxCodecBuffer & buffer)224 int32_t CodecComponentService::EmptyThisBuffer(const OmxCodecBuffer &buffer)
225 {
226 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecEmptyThisBuffer");
227 OHOS::Codec::Omx::OmxCodecBuffer internal = OHOS::Codec::Omx::Convert(buffer, isIPCMode_);
228 std::lock_guard<std::mutex> lock(nodeMutex_);
229 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
230 return node_->EmptyThisBuffer(internal);
231 }
232
FillThisBuffer(const OmxCodecBuffer & buffer)233 int32_t CodecComponentService::FillThisBuffer(const OmxCodecBuffer &buffer)
234 {
235 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecFillThisBuffer");
236 OHOS::Codec::Omx::OmxCodecBuffer internal = OHOS::Codec::Omx::Convert(buffer, isIPCMode_);
237 std::lock_guard<std::mutex> lock(nodeMutex_);
238 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
239 return node_->FillThisBuffer(internal);
240 }
241
SetCallbacks(const sptr<ICodecCallback> & callbacks,int64_t appData)242 int32_t CodecComponentService::SetCallbacks(const sptr<ICodecCallback> &callbacks, int64_t appData)
243 {
244 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSetCallbacks");
245 CHECK_AND_RETURN_RET_LOG(callbacks != nullptr, HDF_ERR_INVALID_PARAM, "callbacks is null");
246 std::lock_guard<std::mutex> lock(nodeMutex_);
247 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
248 return node_->SetCallbacks(callbacks, appData);
249 }
250
ComponentDeInit()251 int32_t CodecComponentService::ComponentDeInit()
252 {
253 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecComponentDeInit");
254 CODEC_LOGI("ComponentDeInit");
255 std::lock_guard<std::mutex> lock(nodeMutex_);
256 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
257 return node_->ComponentDeInit();
258 }
259
UseEglImage(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer,const std::vector<int8_t> & eglImage)260 int32_t CodecComponentService::UseEglImage(uint32_t portIndex, const OmxCodecBuffer &inBuffer,
261 OmxCodecBuffer &outBuffer, const std::vector<int8_t> &eglImage)
262 {
263 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecUseEglImage");
264 CODEC_LOGI("portIndex [%{public}d]", portIndex);
265 OHOS::Codec::Omx::OmxCodecBuffer internal = OHOS::Codec::Omx::Convert(inBuffer, isIPCMode_);
266 std::lock_guard<std::mutex> lock(nodeMutex_);
267 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
268 int32_t ret = node_->UseEglImage(internal, portIndex, eglImage.data());
269 outBuffer = OHOS::Codec::Omx::Convert(internal, isIPCMode_);
270 return ret;
271 }
272
ComponentRoleEnum(std::vector<uint8_t> & role,uint32_t index)273 int32_t CodecComponentService::ComponentRoleEnum(std::vector<uint8_t> &role, uint32_t index)
274 {
275 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecComponentRoleEnum");
276 CODEC_LOGI("index [%{public}d]", index);
277 std::lock_guard<std::mutex> lock(nodeMutex_);
278 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
279 return node_->ComponentRoleEnum(role, index);
280 }
281
SetComponentRole()282 void CodecComponentService::SetComponentRole()
283 {
284 HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSetComponentRole");
285 if (name_ == "") {
286 CODEC_LOGE("compName is null");
287 return;
288 }
289 OHOS::Codec::Omx::CodecOMXCore *core;
290 auto err = mgr_->GetCoreOfComponent(core, name_);
291 if (err != HDF_SUCCESS) {
292 CODEC_LOGE("core is null");
293 return;
294 }
295
296 std::vector<std::string> roles;
297 int32_t ret = core->GetRolesOfComponent(name_, roles);
298 if (ret != HDF_SUCCESS) {
299 CODEC_LOGE("GetRoleOfComponent return err [%{public}d]", ret);
300 return;
301 }
302 if (roles.empty()) {
303 CODEC_LOGE("role of component is empty");
304 return;
305 }
306 uint32_t roleIndex = 0;
307 CODEC_LOGI("RoleName = [%{public}s]", roles[roleIndex].c_str());
308
309 OMX_PARAM_COMPONENTROLETYPE role;
310 errno_t res = strncpy_s(reinterpret_cast<char *>(role.cRole), OMX_MAX_STRINGNAME_SIZE,
311 roles[roleIndex].c_str(), roles[roleIndex].length());
312 if (res != EOK) {
313 CODEC_LOGE("strncpy_s return err [%{public}d]", err);
314 return;
315 }
316 role.nSize = sizeof(role);
317 ret = node_->SetParameter(OMX_IndexParamStandardComponentRole, reinterpret_cast<int8_t *>(&role));
318 if (ret != HDF_SUCCESS) {
319 CODEC_LOGE("OMX_IndexParamStandardComponentRole err [%{public}d]", ret);
320 }
321 }
322
SetParameterWithBuffer(uint32_t index,const std::vector<int8_t> & paramStruct,const OmxCodecBuffer & inBuffer)323 int32_t CodecComponentService::SetParameterWithBuffer(uint32_t index, const std::vector<int8_t>& paramStruct,
324 const OmxCodecBuffer& inBuffer)
325 {
326 OHOS::Codec::Omx::OmxCodecBuffer internal = OHOS::Codec::Omx::Convert(inBuffer, isIPCMode_);
327 std::lock_guard<std::mutex> lock(nodeMutex_);
328 CHECK_AND_RETURN_RET_LOG(node_ != nullptr, HDF_FAILURE, "componentNode is null");
329 return node_->SetParameterWithBuffer(index, paramStruct, internal);
330 }
331
GetComponentCompName() const332 const std::string &CodecComponentService::GetComponentCompName() const
333 {
334 return name_;
335 }
336
GetComponentNode(std::shared_ptr<OHOS::Codec::Omx::ComponentNode> & dumpNode_)337 void CodecComponentService::GetComponentNode(std::shared_ptr<OHOS::Codec::Omx::ComponentNode> &dumpNode_)
338 {
339 std::lock_guard<std::mutex> lock(nodeMutex_);
340 dumpNode_ = node_;
341 }
342
343 } // namespace V4_0
344 } // namespace Codec
345 } // namespace HDI
346 } // namespace OHOS
347