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 "component_node.h"
17 #include <ashmem.h>
18 #include <securec.h>
19 #include <unistd.h>
20 #include "codec_log_wrapper.h"
21 #include "component_mgr.h"
22 #include "icodec_buffer.h"
23
24 using OHOS::HDI::Codec::V1_0::EventInfo;
25 using OHOS::HDI::Codec::V1_0::CodecEventType;
26 using OHOS::HDI::Codec::V1_0::CodecStateType;
27 using OHOS::HDI::Codec::V1_0::CodecCommandType;
28 #define FD_SIZE sizeof(int)
29 namespace {
30 constexpr int NAME_LENGTH = 32;
31 constexpr int ROLE_MAX_LEN = 256;
32 }
33
34 namespace OHOS {
35 namespace Codec {
36 namespace Omx {
OnEvent(OMX_HANDLETYPE component,void * appData,OMX_EVENTTYPE event,uint32_t data1,uint32_t data2,void * eventData)37 OMX_ERRORTYPE ComponentNode::OnEvent(OMX_HANDLETYPE component, void *appData, OMX_EVENTTYPE event, uint32_t data1,
38 uint32_t data2, void *eventData)
39 {
40 ComponentNode *node = static_cast<ComponentNode *>(appData);
41 (void)component;
42 if (node != nullptr) {
43 node->OnEvent(static_cast<CodecEventType>(event), data1, data2, eventData);
44 }
45 return OMX_ErrorNone;
46 }
47
OnEmptyBufferDone(OMX_HANDLETYPE component,void * appData,OMX_BUFFERHEADERTYPE * buffer)48 OMX_ERRORTYPE ComponentNode::OnEmptyBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer)
49 {
50 ComponentNode *node = static_cast<ComponentNode *>(appData);
51 (void)component;
52 if (node != nullptr) {
53 node->OnEmptyBufferDone(buffer);
54 }
55 return OMX_ErrorNone;
56 }
57
OnFillBufferDone(OMX_HANDLETYPE component,void * appData,OMX_BUFFERHEADERTYPE * buffer)58 OMX_ERRORTYPE ComponentNode::OnFillBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer)
59 {
60 ComponentNode *node = static_cast<ComponentNode *>(appData);
61 (void)component;
62 if (node != nullptr) {
63 node->OnFillBufferDone(buffer);
64 }
65 return OMX_ErrorNone;
66 }
67
68 OMX_CALLBACKTYPE ComponentNode::callbacks_ = {&ComponentNode::OnEvent, &ComponentNode::OnEmptyBufferDone,
69 &ComponentNode::OnFillBufferDone};
70
ComponentNode(const sptr<ICodecCallback> & callbacks,int64_t appData,std::shared_ptr<ComponentMgr> & mgr)71 ComponentNode::ComponentNode(const sptr<ICodecCallback> &callbacks, int64_t appData, std::shared_ptr<ComponentMgr> &mgr)
72 {
73 omxCallback_ = callbacks;
74 appData_ = appData;
75 comp_ = nullptr;
76 codecBufferMap_.clear();
77 bufferHeaderMap_.clear();
78 portIndexMap_.clear();
79 bufferIdCount_ = 0;
80 mgr_ = mgr;
81 }
82
~ComponentNode()83 ComponentNode::~ComponentNode()
84 {
85 omxCallback_ = nullptr;
86
87 codecBufferMap_.clear();
88 bufferHeaderMap_.clear();
89 portIndexMap_.clear();
90 bufferIdCount_ = 0;
91 comp_ = nullptr;
92 mgr_ = nullptr;
93 }
94
OpenHandle(const std::string & name)95 int32_t ComponentNode::OpenHandle(const std::string &name)
96 {
97 if (comp_ != nullptr) {
98 return HDF_SUCCESS;
99 }
100
101 OMX_COMPONENTTYPE *comp = nullptr;
102 auto err = mgr_->CreateComponentInstance(name.c_str(), &callbacks_, this, &comp);
103 if (err != OMX_ErrorNone) {
104 CODEC_LOGE("CreateComponentInstance err = %{public}x ", err);
105 return err;
106 }
107 this->comp_ = (OMX_HANDLETYPE)comp;
108 return HDF_SUCCESS;
109 }
110
CloseHandle()111 int32_t ComponentNode::CloseHandle()
112 {
113 if (comp_ == nullptr) {
114 CODEC_LOGE("comp_ is null");
115 return HDF_FAILURE;
116 }
117
118 auto err = mgr_->DeleteComponentInstance(reinterpret_cast<OMX_COMPONENTTYPE *>(comp_));
119 if (err != OMX_ErrorNone) {
120 CODEC_LOGE("DeleteComponentInstance err = %{public}x ", err);
121 return err;
122 }
123 return HDF_SUCCESS;
124 }
125
GetComponentVersion(CompVerInfo & verInfo)126 int32_t ComponentNode::GetComponentVersion(CompVerInfo &verInfo)
127 {
128 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
129 char name[NAME_LENGTH] = {0};
130 OMX_UUIDTYPE uuid = {0};
131 OMX_VERSIONTYPE compVersion = {.nVersion = 0};
132 OMX_VERSIONTYPE sepcVersion = {.nVersion = 0};
133 int32_t err = OMX_GetComponentVersion(comp_, name, &compVersion, &sepcVersion, &uuid);
134 if (err != OMX_ErrorNone) {
135 CODEC_LOGE("OMX_GetComponentVersion err = %{public}x ", err);
136 return err;
137 }
138
139 verInfo.compName = name;
140 verInfo.compUUID.insert(verInfo.compUUID.end(), uuid, uuid + sizeof(OMX_UUIDTYPE));
141 err = memcpy_s(&verInfo.compVersion, sizeof(verInfo.compVersion), &compVersion, sizeof(sepcVersion));
142 if (err != HDF_SUCCESS) {
143 CODEC_LOGE("memset_s return err [%{public}d].", err);
144 return err;
145 }
146
147 err = memcpy_s(&verInfo.specVersion, sizeof(verInfo.specVersion), &sepcVersion, sizeof(sepcVersion));
148 if (err != HDF_SUCCESS) {
149 CODEC_LOGE("memset_s return err [%{public}d].", err);
150 return err;
151 }
152 return err;
153 }
154
SendCommand(CodecCommandType cmd,uint32_t param,int8_t * cmdData)155 int32_t ComponentNode::SendCommand(CodecCommandType cmd, uint32_t param, int8_t *cmdData)
156 {
157 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
158 OMX_COMMANDTYPE omxCmd = static_cast<OMX_COMMANDTYPE>(cmd);
159 auto err = OMX_SendCommand(comp_, omxCmd, param, cmdData);
160 if (err != OMX_ErrorNone) {
161 CODEC_LOGE("OMX_SendCommand err = %{public}x ", err);
162 }
163 return err;
164 }
165
GetParameter(OMX_INDEXTYPE paramIndex,int8_t * param)166 int32_t ComponentNode::GetParameter(OMX_INDEXTYPE paramIndex, int8_t *param)
167 {
168 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
169 auto err = OMX_GetParameter(comp_, paramIndex, param);
170 if (err != OMX_ErrorNone) {
171 CODEC_LOGE("OMX_GetParameter err = %{public}x ", err);
172 }
173 return err;
174 }
175
SetParameter(OMX_INDEXTYPE paramIndex,const int8_t * param)176 int32_t ComponentNode::SetParameter(OMX_INDEXTYPE paramIndex, const int8_t *param)
177 {
178 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
179 auto err = OMX_SetParameter(comp_, paramIndex, const_cast<int8_t *>(param));
180 if (err != OMX_ErrorNone) {
181 CODEC_LOGE("OMX_SetParameter err = %{public}x ", err);
182 }
183 return err;
184 }
185
GetConfig(OMX_INDEXTYPE index,int8_t * config)186 int32_t ComponentNode::GetConfig(OMX_INDEXTYPE index, int8_t *config)
187 {
188 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
189 auto err = OMX_GetConfig(comp_, index, config);
190 if (err != OMX_ErrorNone) {
191 CODEC_LOGE("OMX_GetConfig err = %{public}x ", err);
192 }
193 return err;
194 }
195
SetConfig(OMX_INDEXTYPE index,const int8_t * config)196 int32_t ComponentNode::SetConfig(OMX_INDEXTYPE index, const int8_t *config)
197 {
198 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
199 auto err = OMX_SetConfig(comp_, index, const_cast<int8_t *>(config));
200 if (err != OMX_ErrorNone) {
201 CODEC_LOGE("OMX_SetConfig err = %{public}x ", err);
202 }
203 return err;
204 }
205
GetExtensionIndex(const char * parameterName,uint32_t & index)206 int32_t ComponentNode::GetExtensionIndex(const char *parameterName, uint32_t &index)
207 {
208 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
209 OMX_INDEXTYPE indexType = OMX_IndexComponentStartUnused;
210 auto err = OMX_GetExtensionIndex(comp_, const_cast<char *>(parameterName), &indexType);
211 if (err != OMX_ErrorNone) {
212 CODEC_LOGE("OMX_GetExtensionIndex ret value[%{public}x]", err);
213 return err;
214 }
215 index = indexType;
216 return err;
217 }
218
GetState(CodecStateType & state)219 int32_t ComponentNode::GetState(CodecStateType &state)
220 {
221 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
222 OMX_STATETYPE status = OMX_StateInvalid;
223 auto err = OMX_GetState(comp_, &status);
224 if (err != OMX_ErrorNone) {
225 CODEC_LOGE("OMX_GetState ret value[%{public}x]", err);
226 return err;
227 }
228 state = static_cast<CodecStateType>(status);
229 return err;
230 }
231
ComponentTunnelRequest(uint32_t port,int32_t omxHandleTypeTunneledComp,uint32_t tunneledPort,OHOS::HDI::Codec::V1_0::CodecTunnelSetupType & tunnelSetup)232 int32_t ComponentNode::ComponentTunnelRequest(uint32_t port, int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort,
233 OHOS::HDI::Codec::V1_0::CodecTunnelSetupType &tunnelSetup)
234 {
235 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
236 OMX_COMPONENTTYPE *comType = static_cast<OMX_COMPONENTTYPE *>(comp_);
237 unsigned long tunneledComp = (unsigned long)omxHandleTypeTunneledComp;
238 auto err = comType->ComponentTunnelRequest(comp_, port, (OMX_HANDLETYPE)tunneledComp, tunneledPort,
239 reinterpret_cast<OMX_TUNNELSETUPTYPE *>(&tunnelSetup));
240 if (err != OMX_ErrorNone) {
241 CODEC_LOGE("ComponentTunnelRequest err = %{public}x ", err);
242 }
243 return err;
244 }
245
SetCallbacks(const sptr<ICodecCallback> & callbacks,int64_t appData)246 int32_t ComponentNode::SetCallbacks(const sptr<ICodecCallback> &callbacks, int64_t appData)
247 {
248 this->omxCallback_ = callbacks;
249 appData_ = appData;
250 return OMX_ErrorNone;
251 }
252
UseEglImage(struct OmxCodecBuffer & buffer,uint32_t portIndex,const int8_t * eglImage)253 int32_t ComponentNode::UseEglImage(struct OmxCodecBuffer &buffer, uint32_t portIndex, const int8_t *eglImage)
254 {
255 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
256 OMX_BUFFERHEADERTYPE *pBufferHdrType = nullptr;
257 auto err = OMX_UseEGLImage(comp_, &pBufferHdrType, portIndex, 0, const_cast<int8_t *>(eglImage));
258 if (err != OMX_ErrorNone) {
259 CODEC_LOGE("OMX_UseEGLImage error[0x%{public}x]", err);
260 return err;
261 }
262 (void)buffer;
263 return OMX_ErrorNotImplemented;
264 }
265
ComponentRoleEnum(std::vector<uint8_t> & role,uint32_t index)266 int32_t ComponentNode::ComponentRoleEnum(std::vector<uint8_t> &role, uint32_t index)
267 {
268 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
269 CHECK_AND_RETURN_RET_LOG(index < ROLE_MAX_LEN, HDF_ERR_INVALID_PARAM, "index is too large");
270 uint8_t omxRole[ROLE_MAX_LEN] = {0};
271 OMX_COMPONENTTYPE *comType = static_cast<OMX_COMPONENTTYPE *>(comp_);
272 int32_t err = comType->ComponentRoleEnum(comp_, omxRole, index);
273 if (err != OMX_ErrorNone) {
274 CODEC_LOGE("ComponentRoleEnum ret err [0x%{public}x] ", err);
275 return err;
276 }
277 role.insert(role.end(), omxRole, omxRole + strlen((const char *)omxRole));
278 return OMX_ErrorNone;
279 }
280
ComponentDeInit()281 int32_t ComponentNode::ComponentDeInit()
282 {
283 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
284 OMX_COMPONENTTYPE *comType = static_cast<OMX_COMPONENTTYPE *>(comp_);
285 auto err = comType->ComponentDeInit(comp_);
286 if (err != OMX_ErrorNone) {
287 CODEC_LOGE("ComponentDeInit err = %{public}x ", err);
288 }
289 return err;
290 }
291
OnEvent(CodecEventType event,uint32_t data1,uint32_t data2,void * eventData)292 int32_t ComponentNode::OnEvent(CodecEventType event, uint32_t data1, uint32_t data2, void *eventData)
293 {
294 CODEC_LOGI("eventType: [%{public}d], data1: [%{public}x], data2: [%{public}x]", event, data1, data2);
295 if (omxCallback_ == nullptr) {
296 CODEC_LOGE("omxCallback_ is null");
297 return OMX_ErrorNone;
298 }
299 (void)eventData;
300 EventInfo info = {.appData = appData_, .data1 = data1, .data2 = data2};
301 (void)omxCallback_->EventHandler(event, info);
302
303 return OMX_ErrorNone;
304 }
305
OnEmptyBufferDone(OMX_BUFFERHEADERTYPE * buffer)306 int32_t ComponentNode::OnEmptyBufferDone(OMX_BUFFERHEADERTYPE *buffer)
307 {
308 if ((omxCallback_ == nullptr) || (buffer == nullptr)) {
309 CODEC_LOGE("omxCallback_ or buffer is null");
310 return OMX_ErrorNone;
311 }
312 sptr<ICodecBuffer> codecBuffer = GetBufferInfoByHeader(buffer);
313 if (codecBuffer == nullptr || codecBuffer->EmptyOmxBufferDone(*buffer) != HDF_SUCCESS) {
314 CODEC_LOGE("codecBuffer is null or EmptyOmxBufferDone error");
315 return OMX_ErrorNone;
316 }
317 OmxCodecBuffer &codecOmxBuffer = codecBuffer->GetCodecBuffer();
318 (void)omxCallback_->EmptyBufferDone(appData_, codecOmxBuffer);
319 return OMX_ErrorNone;
320 }
321
OnFillBufferDone(OMX_BUFFERHEADERTYPE * buffer)322 int32_t ComponentNode::OnFillBufferDone(OMX_BUFFERHEADERTYPE *buffer)
323 {
324 if ((omxCallback_ == nullptr) || (buffer == nullptr)) {
325 CODEC_LOGE("omxCallback_ or buffer is null");
326 return OMX_ErrorNone;
327 }
328
329 sptr<ICodecBuffer> codecBuffer = GetBufferInfoByHeader(buffer);
330 if (codecBuffer == nullptr || codecBuffer->FillOmxBufferDone(*buffer) != HDF_SUCCESS) {
331 CODEC_LOGE("codecBuffer is null or EmptyOmxBufferDone error");
332 return OMX_ErrorNone;
333 }
334
335 struct OmxCodecBuffer &codecOmxBuffer = codecBuffer->GetCodecBuffer();
336 (void)omxCallback_->FillBufferDone(appData_, codecOmxBuffer);
337 return OMX_ErrorNone;
338 }
339
UseBuffer(uint32_t portIndex,OmxCodecBuffer & buffer)340 int32_t ComponentNode::UseBuffer(uint32_t portIndex, OmxCodecBuffer &buffer)
341 {
342 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
343 if (buffer.fenceFd >= 0) {
344 close(buffer.fenceFd);
345 buffer.fenceFd = -1;
346 }
347
348 int32_t err = OMX_ErrorBadParameter;
349 sptr<ICodecBuffer> codecBuffer = ICodecBuffer::CreateCodeBuffer(buffer);
350 if (codecBuffer == nullptr) {
351 CODEC_LOGE("codecBuffer is null");
352 return OMX_ErrorInvalidComponent;
353 }
354 OMX_BUFFERHEADERTYPE *bufferHdrType = nullptr;
355 if (buffer.bufferType == CODEC_BUFFER_TYPE_AVSHARE_MEM_FD) {
356 err = OMX_AllocateBuffer((OMX_HANDLETYPE)comp_, &bufferHdrType, portIndex, 0, buffer.allocLen);
357 } else {
358 err = OMX_UseBuffer((OMX_HANDLETYPE)comp_, &bufferHdrType, portIndex, 0, buffer.allocLen,
359 codecBuffer->GetBuffer());
360 }
361
362 if (err != OMX_ErrorNone) {
363 CODEC_LOGE("type [%{public}d] OMX_AllocateBuffer or OMX_UseBuffer ret err[%{public}x]", buffer.bufferType, err);
364 codecBuffer = nullptr;
365 return err;
366 }
367
368 uint32_t bufferId = GenerateBufferId();
369 buffer.bufferId = bufferId;
370 codecBuffer->SetBufferId(bufferId);
371 {
372 std::unique_lock<std::shared_mutex> lk(mapMutex_);
373 codecBufferMap_.emplace(std::make_pair(bufferId, codecBuffer));
374 bufferHeaderMap_.emplace(std::make_pair(bufferHdrType, bufferId));
375 }
376 portIndexMap_.emplace(std::make_pair(bufferHdrType, portIndex));
377 return err;
378 }
379
AllocateBuffer(uint32_t portIndex,OmxCodecBuffer & buffer)380 int32_t ComponentNode::AllocateBuffer(uint32_t portIndex, OmxCodecBuffer &buffer)
381 {
382 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
383 OMX_BUFFERHEADERTYPE *bufferHdrType = 0;
384 int32_t err = OMX_AllocateBuffer((OMX_HANDLETYPE)comp_, &bufferHdrType, portIndex, 0, buffer.allocLen);
385 if (err != OMX_ErrorNone) {
386 CODEC_LOGE("OMX_AllocateBuffer error, err = %{public}x", err);
387 return err;
388 }
389
390 buffer.allocLen = bufferHdrType->nAllocLen;
391 sptr<ICodecBuffer> codecBuffer = ICodecBuffer::AllocateCodecBuffer(buffer);
392 if (codecBuffer == nullptr) {
393 CODEC_LOGE("codecBuffer is null");
394 (void)OMX_FreeBuffer((OMX_HANDLETYPE)comp_, portIndex, bufferHdrType);
395 return OMX_ErrorInvalidComponent;
396 }
397
398 uint32_t bufferId = GenerateBufferId();
399 buffer.bufferId = bufferId;
400 {
401 std::unique_lock<std::shared_mutex> lk(mapMutex_);
402 codecBufferMap_.emplace(std::make_pair(bufferId, codecBuffer));
403 bufferHeaderMap_.emplace(std::make_pair(bufferHdrType, bufferId));
404 }
405 portIndexMap_.emplace(std::make_pair(bufferHdrType, portIndex));
406 return OMX_ErrorNone;
407 }
408
FreeBuffer(uint32_t portIndex,const OmxCodecBuffer & buffer)409 int32_t ComponentNode::FreeBuffer(uint32_t portIndex, const OmxCodecBuffer &buffer)
410 {
411 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
412 int32_t err = OMX_ErrorBadParameter;
413 sptr<ICodecBuffer> codecBufer = nullptr;
414 OMX_BUFFERHEADERTYPE *bufferHdrType = nullptr;
415 if (!GetBufferById(buffer.bufferId, codecBufer, bufferHdrType)) {
416 CODEC_LOGE(" GetBufferById return false");
417 return err;
418 }
419
420 err = OMX_FreeBuffer((OMX_HANDLETYPE)comp_, portIndex, bufferHdrType);
421 if (err != OMX_ErrorNone) {
422 CODEC_LOGE("OMX_FreeBuffer err [%{public}x]", err);
423 return err;
424 }
425
426 {
427 std::unique_lock<std::shared_mutex> lk(mapMutex_);
428 auto iterOmxBuffer = bufferHeaderMap_.begin();
429 while (iterOmxBuffer != bufferHeaderMap_.end()) {
430 if (iterOmxBuffer->first == bufferHdrType) {
431 bufferHeaderMap_.erase(iterOmxBuffer);
432 break;
433 }
434 iterOmxBuffer++;
435 }
436
437 auto iter = codecBufferMap_.find(buffer.bufferId);
438 if (iter != codecBufferMap_.end()) {
439 codecBufferMap_.erase(iter);
440 }
441 }
442
443 (void)codecBufer->FreeBuffer(const_cast<OmxCodecBuffer &>(buffer));
444
445 return err;
446 }
447
EmptyThisBuffer(OmxCodecBuffer & buffer)448 int32_t ComponentNode::EmptyThisBuffer(OmxCodecBuffer &buffer)
449 {
450 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
451 int32_t err = OMX_ErrorBadParameter;
452 OMX_BUFFERHEADERTYPE *bufferHdrType = nullptr;
453 sptr<ICodecBuffer> codecBuffer = nullptr;
454 if (!GetBufferById(buffer.bufferId, codecBuffer, bufferHdrType)) {
455 CODEC_LOGE(" GetBufferById return false");
456 return err;
457 }
458 err = codecBuffer->EmptyOmxBuffer(buffer, *bufferHdrType);
459 if (err != HDF_SUCCESS) {
460 CODEC_LOGE("EmptyOmxBuffer err [%{public}d]", err);
461 return err;
462 }
463
464 err = OMX_EmptyThisBuffer((OMX_HANDLETYPE)comp_, bufferHdrType);
465 return err;
466 }
467
FillThisBuffer(OmxCodecBuffer & buffer)468 int32_t ComponentNode::FillThisBuffer(OmxCodecBuffer &buffer)
469 {
470 CHECK_AND_RETURN_RET_LOG(comp_ != nullptr, OMX_ErrorInvalidComponent, "comp_ is null");
471 int32_t err = OMX_ErrorBadParameter;
472 OMX_BUFFERHEADERTYPE *bufferHdrType = nullptr;
473 sptr<ICodecBuffer> codecBuffer = nullptr;
474 if (!GetBufferById(buffer.bufferId, codecBuffer, bufferHdrType)) {
475 CODEC_LOGE("GetBufferById return false");
476 return err;
477 }
478
479 err = codecBuffer->FillOmxBuffer(buffer, *bufferHdrType);
480 if (err != HDF_SUCCESS) {
481 CODEC_LOGE("FillOmxBuffer err [%{public}d]", err);
482 return err;
483 }
484
485 err = OMX_FillThisBuffer((OMX_HANDLETYPE)comp_, bufferHdrType);
486 return err;
487 }
488
GenerateBufferId()489 uint32_t ComponentNode::GenerateBufferId()
490 {
491 uint32_t bufferId = 0;
492 do {
493 if (++bufferIdCount_ == 0) {
494 ++bufferIdCount_;
495 }
496 bufferId = bufferIdCount_;
497 } while (codecBufferMap_.find(bufferId) != codecBufferMap_.end());
498 return bufferId;
499 }
500
GetBufferInfoByHeader(OMX_BUFFERHEADERTYPE * buffer)501 sptr<ICodecBuffer> ComponentNode::GetBufferInfoByHeader(OMX_BUFFERHEADERTYPE *buffer)
502 {
503 if (buffer == nullptr) {
504 CODEC_LOGE("Buffer is null");
505 return nullptr;
506 }
507 std::shared_lock<std::shared_mutex> lk(mapMutex_);
508 auto iterHead = bufferHeaderMap_.find(buffer);
509 if (iterHead == bufferHeaderMap_.end()) {
510 CODEC_LOGE("Can not find bufferID");
511 return nullptr;
512 }
513
514 uint32_t bufferId = iterHead->second;
515 auto iter = codecBufferMap_.find(bufferId);
516 if (iter == codecBufferMap_.end()) {
517 CODEC_LOGE("Can not find bufferInfo by bufferId = %{public}d", bufferId);
518 return nullptr;
519 }
520 return iter->second;
521 }
522
GetBufferById(uint32_t bufferId,sptr<ICodecBuffer> & codecBuffer,OMX_BUFFERHEADERTYPE * & bufferHdrType)523 bool ComponentNode::GetBufferById(uint32_t bufferId, sptr<ICodecBuffer> &codecBuffer,
524 OMX_BUFFERHEADERTYPE *&bufferHdrType)
525 {
526 std::shared_lock<std::shared_mutex> lk(mapMutex_);
527 auto iter = codecBufferMap_.find(bufferId);
528 if ((iter == codecBufferMap_.end()) || (iter->second == nullptr)) {
529 CODEC_LOGE("Can not find bufferIndo by bufferID [%{public}d]", bufferId);
530 return false;
531 }
532
533 auto iterHead = bufferHeaderMap_.begin();
534 for (; iterHead != bufferHeaderMap_.end(); iterHead++) {
535 if (iterHead->second == bufferId) {
536 break;
537 }
538 }
539 if ((iterHead == bufferHeaderMap_.end()) || (iterHead->first == nullptr)) {
540 CODEC_LOGE("Can not find bufferHeaderType by bufferID [%{public}d] or iterHead->first is null", bufferId);
541 return false;
542 }
543 bufferHdrType = iterHead->first;
544 codecBuffer = iter->second;
545 return true;
546 }
547
GetBufferMapCount()548 std::map<OMX_BUFFERHEADERTYPE *, uint32_t> &ComponentNode::GetBufferMapCount()
549 {
550 return portIndexMap_;
551 }
552 } // namespace Omx
553 } // namespace Codec
554 } // namespace OHOS
555