• 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_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 namespace OHOS {
25 namespace HDI {
26 namespace Codec {
27 namespace V2_0 {
CodecComponentService(const std::shared_ptr<OHOS::Codec::Omx::ComponentNode> & node,const std::shared_ptr<OHOS::Codec::Omx::ComponentMgr> mgr,const std::string name)28 CodecComponentService::CodecComponentService(const std::shared_ptr<OHOS::Codec::Omx::ComponentNode> &node,
29     const std::shared_ptr<OHOS::Codec::Omx::ComponentMgr> mgr, const std::string name)
30 {
31     name_ = name;
32     node_ = node;
33     mgr_  = mgr;
34     isIPCMode_ = (HdfRemoteGetCallingPid() == getpid() ? false : true);
35 #ifdef SUPPORT_ROLE
36     SetComponentRole();
37 #endif
38 }
~CodecComponentService()39 CodecComponentService::~CodecComponentService()
40 {
41     if (node_ != nullptr) {
42         node_->ReleaseOMXResource();
43         int32_t ret = node_->CloseHandle();
44         if (ret != HDF_SUCCESS) {
45             CODEC_LOGE("CloseHandle failed, err[%{public}d]", ret);
46         }
47         node_ = nullptr;
48         ReleaseCache();
49     }
50     name_ = "";
51     mgr_ = nullptr;
52 }
53 
ReleaseCache()54 void CodecComponentService::ReleaseCache()
55 {
56 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
57     auto err = mallopt(M_FLUSH_THREAD_CACHE, 0);
58     if (err != HDF_SUCCESS) {
59         CODEC_LOGE("release cache error, m_purge = %{public}d", err);
60     }
61 #endif
62 }
63 
GetComponentVersion(CompVerInfo & verInfo)64 int32_t CodecComponentService::GetComponentVersion(CompVerInfo &verInfo)
65 {
66     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetComponentVersion");
67     return node_->GetComponentVersion(verInfo);
68 }
69 
SendCommand(CodecCommandType cmd,uint32_t param,const std::vector<int8_t> & cmdData)70 int32_t CodecComponentService::SendCommand(CodecCommandType cmd, uint32_t param, const std::vector<int8_t> &cmdData)
71 {
72     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSendCommand");
73     CODEC_LOGI("commandType: [%{public}d], command [%{public}d]", cmd, param);
74     return node_->SendCommand(cmd, param, const_cast<int8_t *>(cmdData.data()));
75 }
76 
GetParameter(uint32_t index,const std::vector<int8_t> & inParamStruct,std::vector<int8_t> & outParamStruct)77 int32_t CodecComponentService::GetParameter(uint32_t index, const std::vector<int8_t> &inParamStruct,
78                                             std::vector<int8_t> &outParamStruct)
79 {
80     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetParameter");
81     CODEC_LOGI("index [%{public}x]", index);
82     outParamStruct = inParamStruct;
83     return node_->GetParameter(static_cast<enum OMX_INDEXTYPE>(index), outParamStruct.data());
84 }
85 
SetParameter(uint32_t index,const std::vector<int8_t> & paramStruct)86 int32_t CodecComponentService::SetParameter(uint32_t index, const std::vector<int8_t> &paramStruct)
87 {
88     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSetParameter");
89     CODEC_LOGI("index [%{public}x]", index);
90     return node_->SetParameter(static_cast<enum OMX_INDEXTYPE>(index), paramStruct.data());
91 }
92 
GetConfig(uint32_t index,const std::vector<int8_t> & inCfgStruct,std::vector<int8_t> & outCfgStruct)93 int32_t CodecComponentService::GetConfig(uint32_t index, const std::vector<int8_t> &inCfgStruct,
94                                          std::vector<int8_t> &outCfgStruct)
95 {
96     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetConfig");
97     CODEC_LOGI("index [%{public}x]", index);
98     outCfgStruct = inCfgStruct;
99     return node_->GetConfig(static_cast<enum OMX_INDEXTYPE>(index), outCfgStruct.data());
100 }
101 
SetConfig(uint32_t index,const std::vector<int8_t> & cfgStruct)102 int32_t CodecComponentService::SetConfig(uint32_t index, const std::vector<int8_t> &cfgStruct)
103 {
104     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSetConfig");
105     CODEC_LOGI("index [%{public}x]", index);
106     return node_->SetConfig(static_cast<enum OMX_INDEXTYPE>(index), cfgStruct.data());
107 }
108 
GetExtensionIndex(const std::string & paramName,uint32_t & indexType)109 int32_t CodecComponentService::GetExtensionIndex(const std::string &paramName, uint32_t &indexType)
110 {
111     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetExtensionIndex");
112     CODEC_LOGI("paramName [%{public}s]", paramName.c_str());
113     return node_->GetExtensionIndex(paramName.c_str(), indexType);
114 }
115 
GetState(CodecStateType & state)116 int32_t CodecComponentService::GetState(CodecStateType &state)
117 {
118     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecGetState");
119     return node_->GetState(state);
120 }
121 
ComponentTunnelRequest(uint32_t port,int32_t tunneledComp,uint32_t tunneledPort,const CodecTunnelSetupType & inTunnelSetup,CodecTunnelSetupType & outTunnelSetup)122 int32_t CodecComponentService::ComponentTunnelRequest(uint32_t port, int32_t tunneledComp, uint32_t tunneledPort,
123                                                       const CodecTunnelSetupType &inTunnelSetup,
124                                                       CodecTunnelSetupType &outTunnelSetup)
125 {
126     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecComponentTunnelRequest");
127     CODEC_LOGI("port [%{public}d]", port);
128     outTunnelSetup = inTunnelSetup;
129     return node_->ComponentTunnelRequest(port, tunneledComp, tunneledPort, outTunnelSetup);
130 }
131 
UseBuffer(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer)132 int32_t CodecComponentService::UseBuffer(uint32_t portIndex, const OmxCodecBuffer &inBuffer, OmxCodecBuffer &outBuffer)
133 {
134     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecUseBuffer");
135     CODEC_LOGI("portIndex: [%{public}d]", portIndex);
136     outBuffer = const_cast<OmxCodecBuffer &>(inBuffer);
137     if (outBuffer.fd >= 0 && !isIPCMode_ && outBuffer.bufferType == CODEC_BUFFER_TYPE_AVSHARE_MEM_FD) {
138         outBuffer.fd = dup(inBuffer.fd);
139     }
140 
141     if (outBuffer.fd >= 0 && isIPCMode_ && outBuffer.bufferType != CODEC_BUFFER_TYPE_AVSHARE_MEM_FD &&
142         outBuffer.bufferType != CODEC_BUFFER_TYPE_DMA_MEM_FD) {
143         close(outBuffer.fd);
144         outBuffer.fd = -1;
145     }
146 
147     return node_->UseBuffer(portIndex, outBuffer);
148 }
149 
AllocateBuffer(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer)150 int32_t CodecComponentService::AllocateBuffer(uint32_t portIndex, const OmxCodecBuffer &inBuffer,
151                                               OmxCodecBuffer &outBuffer)
152 {
153     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecAllocateBuffer");
154     CODEC_LOGI("portIndex: [%{public}d]", portIndex);
155     outBuffer = inBuffer;
156     return node_->AllocateBuffer(portIndex, outBuffer);
157 }
158 
FreeBuffer(uint32_t portIndex,const OmxCodecBuffer & buffer)159 int32_t CodecComponentService::FreeBuffer(uint32_t portIndex, const OmxCodecBuffer &buffer)
160 {
161     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecFreeBuffer");
162     OmxCodecBuffer &bufferTemp = const_cast<OmxCodecBuffer &>(buffer);
163     CODEC_LOGI("portIndex: [%{public}d], bufferId: [%{public}d]", portIndex, buffer.bufferId);
164     int32_t ret = node_->FreeBuffer(portIndex, buffer);
165     ReleaseCache();
166     if (isIPCMode_ && bufferTemp.fd >= 0) {
167         close(bufferTemp.fd);
168         bufferTemp.fd = -1;
169     }
170 
171     return ret;
172 }
173 
EmptyThisBuffer(const OmxCodecBuffer & buffer)174 int32_t CodecComponentService::EmptyThisBuffer(const OmxCodecBuffer &buffer)
175 {
176     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecEmptyThisBuffer");
177     OmxCodecBuffer &bufferTemp = const_cast<OmxCodecBuffer &>(buffer);
178     int32_t ret = node_->EmptyThisBuffer(bufferTemp);
179     if (isIPCMode_ && bufferTemp.fd >= 0) {
180         close(bufferTemp.fd);
181         bufferTemp.fd = -1;
182     }
183 
184     return ret;
185 }
186 
FillThisBuffer(const OmxCodecBuffer & buffer)187 int32_t CodecComponentService::FillThisBuffer(const OmxCodecBuffer &buffer)
188 {
189     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecFillThisBuffer");
190     OmxCodecBuffer &bufferTemp = const_cast<OmxCodecBuffer &>(buffer);
191     int32_t ret = node_->FillThisBuffer(bufferTemp);
192     if (isIPCMode_ && bufferTemp.fd >= 0) {
193         close(bufferTemp.fd);
194         bufferTemp.fd = -1;
195     }
196 
197     return ret;
198 }
199 
SetCallbacks(const sptr<ICodecCallback> & callbacks,int64_t appData)200 int32_t CodecComponentService::SetCallbacks(const sptr<ICodecCallback> &callbacks, int64_t appData)
201 {
202     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSetCallbacks");
203     CODEC_LOGI("service impl!");
204     CHECK_AND_RETURN_RET_LOG(callbacks != nullptr, HDF_ERR_INVALID_PARAM, "callbacks is null");
205     return node_->SetCallbacks(callbacks, appData);
206 }
207 
ComponentDeInit()208 int32_t CodecComponentService::ComponentDeInit()
209 {
210     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecComponentDeInit");
211     CODEC_LOGI("service impl!");
212     return node_->ComponentDeInit();
213 }
214 
UseEglImage(uint32_t portIndex,const OmxCodecBuffer & inBuffer,OmxCodecBuffer & outBuffer,const std::vector<int8_t> & eglImage)215 int32_t CodecComponentService::UseEglImage(uint32_t portIndex, const OmxCodecBuffer &inBuffer,
216                                            OmxCodecBuffer &outBuffer, const std::vector<int8_t> &eglImage)
217 {
218     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecUseEglImage");
219     CODEC_LOGI("portIndex [%{public}d]", portIndex);
220     outBuffer = inBuffer;
221     return node_->UseEglImage(outBuffer, portIndex, eglImage.data());
222 }
223 
ComponentRoleEnum(std::vector<uint8_t> & role,uint32_t index)224 int32_t CodecComponentService::ComponentRoleEnum(std::vector<uint8_t> &role, uint32_t index)
225 {
226     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecComponentRoleEnum");
227     CODEC_LOGI("index [%{public}d]", index);
228     return node_->ComponentRoleEnum(role, index);
229 }
230 
SetComponentRole()231 void CodecComponentService::SetComponentRole()
232 {
233     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HDFCodecSetComponentRole");
234     if (name_ == "") {
235         CODEC_LOGE("compName is null");
236         return;
237     }
238     OHOS::Codec::Omx::CodecOMXCore *core;
239     auto err = mgr_->GetCoreOfComponent(core, name_);
240     if (err != HDF_SUCCESS) {
241         CODEC_LOGE("core is null");
242         return;
243     }
244 
245     std::vector<std::string> roles;
246     int32_t ret = core->GetRolesOfComponent(name_, roles);
247     if (ret != HDF_SUCCESS) {
248         CODEC_LOGE("GetRoleOfComponent return err [%{public}d]", ret);
249         return;
250     }
251     uint32_t roleIndex = 0;
252     CODEC_LOGI("RoleName = [%{public}s]", roles[roleIndex].c_str());
253 
254     OMX_PARAM_COMPONENTROLETYPE role;
255     errno_t res = strncpy_s(reinterpret_cast<char *>(role.cRole), OMX_MAX_STRINGNAME_SIZE,
256                             roles[roleIndex].c_str(), roles[roleIndex].length());
257     if (res != EOK) {
258         CODEC_LOGE("strncpy_s return err [%{public}d]", err);
259         return;
260     }
261     role.nSize = sizeof(role);
262     ret = node_->SetParameter(OMX_IndexParamStandardComponentRole, reinterpret_cast<int8_t *>(&role));
263     if (ret != HDF_SUCCESS) {
264         CODEC_LOGE("OMX_IndexParamStandardComponentRole err [%{public}d]", ret);
265     }
266 }
267 
GetComponentCompName() const268 const std::string &CodecComponentService::GetComponentCompName() const
269 {
270     return name_;
271 }
272 
GetComponentNode(std::shared_ptr<OHOS::Codec::Omx::ComponentNode> & dumpNode_)273 void CodecComponentService::GetComponentNode(std::shared_ptr<OHOS::Codec::Omx::ComponentNode> &dumpNode_)
274 {
275     dumpNode_ = node_;
276 }
277 
278 }  // namespace V2_0
279 }  // namespace Codec
280 }  // namespace HDI
281 }  // namespace OHOS
282