• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device 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 <hdf_log.h>
18 #include <securec.h>
19 #include <osal_mem.h>
20 #include <unistd.h>
21 #include "codec_interface.h"
22 #include "component_common.h"
23 #include "codec_omx_ext.h"
24 
25 #define HDF_LOG_TAG codec_hdi_passthrough
26 
27 using namespace OHOS::Codec::Common;
28 
29 namespace OHOS {
30 namespace Codec {
31 namespace CodecAdapter {
OnEvent(UINTPTR userData,EventType event,uint32_t length,int32_t eventData[])32 int32_t ComponentNode::OnEvent(UINTPTR userData, EventType event, uint32_t length, int32_t eventData[])
33 {
34     ComponentNode *node = reinterpret_cast<ComponentNode *>(userData);
35     if (node != nullptr) {
36         node->OnEvent(event, length, eventData);
37     }
38 
39     return HDF_SUCCESS;
40 }
41 
InputBufferAvailable(UINTPTR userData,CodecBuffer * inBuf,int32_t * acquireFd)42 int32_t ComponentNode::InputBufferAvailable(UINTPTR userData, CodecBuffer *inBuf, int32_t *acquireFd)
43 {
44     ComponentNode *node = reinterpret_cast<ComponentNode *>(userData);
45     if (node != nullptr) {
46         node->OnEmptyBufferDone(inBuf, acquireFd);
47     }
48 
49     return HDF_SUCCESS;
50 }
51 
OutputBufferAvailable(UINTPTR userData,CodecBuffer * outBuf,int32_t * acquireFd)52 int32_t ComponentNode::OutputBufferAvailable(UINTPTR userData, CodecBuffer *outBuf, int32_t *acquireFd)
53 {
54     ComponentNode *node = reinterpret_cast<ComponentNode *>(userData);
55     if (node != nullptr) {
56         node->OnFillBufferDone(outBuf, acquireFd);
57     }
58 
59     return HDF_SUCCESS;
60 }
61 
62 CodecCallback ComponentNode::callbacks_ = {
63     &ComponentNode::OnEvent, &ComponentNode::InputBufferAvailable, &ComponentNode::OutputBufferAvailable};
64 
ComponentNode(CODEC_HANDLETYPE handle,CodecExInfo info)65 ComponentNode::ComponentNode(CODEC_HANDLETYPE handle, CodecExInfo info)
66     : comp_(handle),
67       omxCallback_(nullptr),
68       exInfo_(info),
69       appData_(0),
70       bufferId_(0),
71       setCallbackComplete_(false),
72       state_(OMX_StateMax),
73       codecType_(info.type),
74       inputMode_(ALLOCATE_INPUT_BUFFER_USER_PRESET),
75       outputMode_(ALLOCATE_OUTPUT_BUFFER_USER_PRESET)
76 {
77 }
78 
GetComponentVersion(CompVerInfo & verInfo)79 int32_t ComponentNode::GetComponentVersion(CompVerInfo &verInfo)
80 {
81     if (comp_ == nullptr) {
82         HDF_LOGE("%{public}s error, comp_ is null !", __func__);
83         return HDF_ERR_INVALID_PARAM;
84     }
85     HDF_LOGW("%{public}s is not support!", __func__);
86     return HDF_ERR_NOT_SUPPORT;
87 }
88 
SendCommand(OMX_COMMANDTYPE cmd,uint32_t param,int8_t * cmdData,uint32_t cmdDataLen)89 int32_t ComponentNode::SendCommand(OMX_COMMANDTYPE cmd, uint32_t param, int8_t *cmdData, uint32_t cmdDataLen)
90 {
91     if (comp_ == nullptr) {
92         HDF_LOGE("%{public}s error, comp_ is null", __func__);
93         return HDF_ERR_INVALID_PARAM;
94     }
95     int32_t ret;
96     switch (cmd) {
97         case OMX_CommandStateSet:
98             ret = ChangeComponentState(param);
99             break;
100         case OMX_CommandFlush:
101             ret = FlushComponent(param);
102             break;
103 
104         default: {
105             ret = HDF_ERR_NOT_SUPPORT;
106             HDF_LOGE("%{public}s error, CMD[%{public}d] is not support!", __func__, cmd);
107             break;
108         }
109     }
110     return ret;
111 }
112 
GetParameter(OMX_INDEXTYPE paramIndex,int8_t * param,uint32_t paramLen)113 int32_t ComponentNode::GetParameter(OMX_INDEXTYPE paramIndex, int8_t *param, uint32_t paramLen)
114 {
115     if (comp_ == nullptr) {
116         HDF_LOGE("%{public}s error, comp_ is null", __func__);
117         return HDF_ERR_INVALID_PARAM;
118     }
119 
120     int32_t paramCnt = 0;
121     Param paramOut[PARAM_COUNT_MAX] = {};
122     int32_t ret = Common::SplitParam(paramIndex, param, paramOut, paramCnt, codecType_);
123     if (ret != HDF_SUCCESS) {
124         HDF_LOGE("%{public}s error, paramIndex is not support", __func__);
125         return ret;
126     }
127     ret = HDF_FAILURE;
128     for (int32_t i = 0; i < paramCnt; i++) {
129         int32_t err = CodecGetParameter(comp_, &paramOut[i], 1);
130         if (err == HDF_SUCCESS) {
131             HDF_LOGI("%{public}s CodecGetParameter %{public}d Success", __func__, paramOut[i].key);
132             ret = HDF_SUCCESS;
133         }
134     }
135 
136     if (ret == HDF_SUCCESS) {
137         ret = Common::ParseParam(paramIndex, paramOut, paramCnt, param, exInfo_);
138         if (ret != HDF_SUCCESS) {
139             HDF_LOGE("%{public}s ParseParam failed", __func__);
140         }
141     }
142     return ret;
143 }
144 
SetParameter(OMX_INDEXTYPE paramIndex,const int8_t * param,uint32_t paramLen)145 int32_t ComponentNode::SetParameter(OMX_INDEXTYPE paramIndex, const int8_t *param, uint32_t paramLen)
146 {
147     if (comp_ == nullptr || param == nullptr) {
148         HDF_LOGE("%{public}s error, comp_ is null or param is null", __func__);
149         return HDF_ERR_INVALID_PARAM;
150     }
151 
152     int32_t paramCnt = 1;
153     Param paramOut[PARAM_COUNT_MAX] = {};
154     int32_t ret = Common::SplitParam(paramIndex, const_cast<int8_t *>(param), paramOut, paramCnt, codecType_);
155     if (ret != HDF_SUCCESS) {
156         HDF_LOGE("%{public}s error, paramIndex is not support", __func__);
157         return ret;
158     }
159     ret = HDF_FAILURE;
160     for (int32_t i = 0; i < paramCnt; i++) {
161         int32_t err = CodecSetParameter(comp_, &paramOut[i], 1);
162         HDF_LOGI("%{public}s CodecSetParameter %{public}d ret[%{public}d]", __func__, paramOut[i].key, ret);
163         if (err == HDF_SUCCESS) {
164             HDF_LOGI("%{public}s CodecSetParameter %{public}d Success", __func__, paramOut[i].key);
165             ret = HDF_SUCCESS;
166         }
167     }
168     return ret;
169 }
170 
GetConfig(OMX_INDEXTYPE index,int8_t * config,uint32_t configLen)171 int32_t ComponentNode::GetConfig(OMX_INDEXTYPE index, int8_t *config, uint32_t configLen)
172 {
173     if (comp_ == nullptr) {
174         HDF_LOGE("%{public}s error, comp_ is null", __func__);
175         return HDF_ERR_INVALID_PARAM;
176     }
177     HDF_LOGW("%{public}s is not support!", __func__);
178     return HDF_ERR_NOT_SUPPORT;
179 }
180 
SetConfig(OMX_INDEXTYPE index,const int8_t * config,uint32_t configLen)181 int32_t ComponentNode::SetConfig(OMX_INDEXTYPE index, const int8_t *config, uint32_t configLen)
182 {
183     if (comp_ == nullptr) {
184         HDF_LOGE("%{public}s error, comp_ is null", __func__);
185         return HDF_ERR_INVALID_PARAM;
186     }
187     HDF_LOGW("%{public}s is not support!", __func__);
188     return HDF_ERR_NOT_SUPPORT;
189 }
190 
GetExtensionIndex(const char * parameterName,OMX_INDEXTYPE * indexType)191 int32_t ComponentNode::GetExtensionIndex(const char *parameterName, OMX_INDEXTYPE *indexType)
192 {
193     if (comp_ == nullptr) {
194         HDF_LOGE("%{public}s error, comp_ is null", __func__);
195         return HDF_ERR_INVALID_PARAM;
196     }
197     HDF_LOGW("%{public}s is not support!", __func__);
198     return HDF_ERR_NOT_SUPPORT;
199 }
200 
SetState(OMX_STATETYPE state)201 int32_t ComponentNode::SetState(OMX_STATETYPE state)
202 {
203     int32_t ret = HDF_FAILURE;
204     bool stateFlag = false;
205     switch (state) {
206         case OMX_StateInvalid:
207             stateFlag = true;
208             break;
209         case OMX_StateLoaded: {
210             stateFlag = (state_ == OMX_StateIdle || state_ == OMX_StateWaitForResources || state_ == OMX_StateMax);
211             break;
212         }
213         case OMX_StateIdle: {
214             stateFlag = (state_ == OMX_StateWaitForResources || state_ == OMX_StateLoaded || state_ == OMX_StatePause ||
215                 state_ == OMX_StateExecuting);
216             break;
217         }
218         case OMX_StateExecuting: {
219             stateFlag = (state_ == OMX_StateIdle || state_ == OMX_StatePause);
220             break;
221         }
222         case OMX_StatePause: {
223             stateFlag = (state_ == OMX_StateIdle || state_ == OMX_StateExecuting);
224             break;
225         }
226         case OMX_StateWaitForResources: {
227             stateFlag = (state_ == OMX_StateLoaded);
228             break;
229         }
230 
231         default:
232             HDF_LOGW("%{public}s warn, unsupport state[%{public}d]", __func__, state);
233             break;
234     }
235     if (stateFlag) {
236         ret = HDF_SUCCESS;
237         state_ = state;
238     }
239     HDF_LOGI("%{public}s set state[%{public}d], current state is [%{public}d]", __func__, state, state_);
240 
241     return ret;
242 }
243 
GetState(OMX_STATETYPE * state)244 int32_t ComponentNode::GetState(OMX_STATETYPE *state)
245 {
246     if (comp_ == nullptr) {
247         HDF_LOGE("%{public}s error, comp_ is null", __func__);
248         return HDF_ERR_INVALID_PARAM;
249     }
250     if (state == nullptr) {
251         HDF_LOGE("%{public}s error, state is null", __func__);
252         return HDF_ERR_INVALID_PARAM;
253     }
254     *state = state_;
255     return HDF_SUCCESS;
256 }
257 
ComponentTunnelRequest(uint32_t port,int32_t omxHandleTypeTunneledComp,uint32_t tunneledPort,OMX_TUNNELSETUPTYPE * tunnelSetup)258 int32_t ComponentNode::ComponentTunnelRequest(
259     uint32_t port, int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort, OMX_TUNNELSETUPTYPE *tunnelSetup)
260 {
261     if (comp_ == nullptr) {
262         HDF_LOGE("%{public}s error, comp_ is null", __func__);
263         return HDF_ERR_INVALID_PARAM;
264     }
265     HDF_LOGW("%{public}s is not support!", __func__);
266     return HDF_ERR_NOT_SUPPORT;
267 }
268 
SetCallbacks(const CodecCallbackType * omxCallback,int64_t appData)269 int32_t ComponentNode::SetCallbacks(const CodecCallbackType *omxCallback, int64_t appData)
270 {
271     int32_t ret = HDF_SUCCESS;
272     if (!setCallbackComplete_) {
273         if (comp_ == nullptr) {
274             HDF_LOGE("%{public}s error, comp_ is null", __func__);
275             return HDF_ERR_INVALID_PARAM;
276         }
277         ret = CodecSetCallback(comp_, &callbacks_, reinterpret_cast<UINTPTR>(this));
278         if (ret != HDF_SUCCESS) {
279             HDF_LOGE("%{public}s CodecSetCallback error[0x%{public}x]", __func__, ret);
280             return ret;
281         }
282         setCallbackComplete_ = true;
283     }
284     this->omxCallback_ = const_cast<CodecCallbackType *>(omxCallback);
285     this->appData_ = appData;
286 
287     return ret;
288 }
289 
UseEglImage(OmxCodecBuffer & buffer,uint32_t portIndex,int8_t * eglImage,uint32_t eglImageLen)290 int32_t ComponentNode::UseEglImage(OmxCodecBuffer &buffer, uint32_t portIndex, int8_t *eglImage, uint32_t eglImageLen)
291 {
292     if (comp_ == nullptr) {
293         HDF_LOGE("%{public}s error, comp_ is null", __func__);
294         return HDF_ERR_INVALID_PARAM;
295     }
296     HDF_LOGW("%{public}s is not support!", __func__);
297     return HDF_ERR_NOT_SUPPORT;
298 }
299 
ComponentRoleEnum(uint8_t * role,uint32_t roleLen,uint32_t index)300 int32_t ComponentNode::ComponentRoleEnum(uint8_t *role, uint32_t roleLen, uint32_t index)
301 {
302     if (comp_ == nullptr) {
303         HDF_LOGE("%{public}s error, comp_ is null", __func__);
304         return HDF_ERR_INVALID_PARAM;
305     }
306     HDF_LOGW("%{public}s is not support!", __func__);
307     return HDF_ERR_NOT_SUPPORT;
308 }
309 
ComponentDeInit()310 int32_t ComponentNode::ComponentDeInit()
311 {
312     if (comp_ == nullptr) {
313         HDF_LOGE("%{public}s error, comp_ is null", __func__);
314         return HDF_ERR_INVALID_PARAM;
315     }
316     HDF_LOGW("%{public}s is not support!", __func__);
317     return HDF_ERR_NOT_SUPPORT;
318 }
319 
OnEvent(EventType event,uint32_t length,int32_t eventData[])320 int32_t ComponentNode::OnEvent(EventType event, uint32_t length, int32_t eventData[])
321 {
322     if (omxCallback_ == nullptr || omxCallback_->EventHandler == nullptr) {
323         HDF_LOGE("%{public}s omxCallback_ or EventHandler is null", __func__);
324         return HDF_FAILURE;
325     }
326 
327     OMX_EVENTTYPE omxEvent;
328     EventInfo info = {0};
329     info.appData = appData_;
330     info.data1 = 0;
331     info.data2 = 0;
332     info.eventData = nullptr;
333     info.eventDataLen = 0;
334     switch (event) {
335         case EVENT_ERROR: {
336             omxEvent = OMX_EventError;
337             if (length > 0) {
338                 info.data1 = static_cast<uint32_t>(eventData[0]);
339             }
340             break;
341         }
342         case EVENT_FLUSH_COMPLETE: {
343             omxEvent = OMX_EventCmdComplete;
344             info.data1 = OMX_CommandFlush;
345             break;
346         }
347         case EVENT_EOS_COMPLETE: {
348             omxEvent = OMX_EventBufferFlag;
349             break;
350         }
351 
352         default: {
353             HDF_LOGW("%{public}s unsupport event [%{public}d]", __func__, event);
354             omxEvent = OMX_EventMax;
355             break;
356         }
357     }
358     HDF_LOGD("%{public}s EventHandler ON", __func__);
359     omxCallback_->EventHandler(omxCallback_, omxEvent, &info);
360 
361     return HDF_SUCCESS;
362 }
363 
OnEmptyBufferDone(CodecBuffer * inBuf,int32_t * acquireFd)364 int32_t ComponentNode::OnEmptyBufferDone(CodecBuffer *inBuf, int32_t *acquireFd)
365 {
366     if (omxCallback_ == nullptr || inBuf == nullptr || omxCallback_->EmptyBufferDone == nullptr) {
367         HDF_LOGE("%{public}s error, omxCallback_ or buffer or EmptyBufferDone is null", __func__);
368         return HDF_FAILURE;
369     }
370 
371     OmxCodecBuffer omxCodecBuffer = {0};
372     Common::ConvertCodecBufferToOmxCodecBuffer(omxCodecBuffer, *inBuf);
373     omxCodecBuffer.size = sizeof(OmxCodecBuffer);
374     omxCodecBuffer.fenceFd = *acquireFd;
375     omxCodecBuffer.type = READ_ONLY_TYPE;
376     HDF_LOGD("%{public}s EmptyBufferDone ON", __func__);
377     omxCallback_->EmptyBufferDone(omxCallback_, appData_, &omxCodecBuffer);
378 
379     return HDF_SUCCESS;
380 }
381 
OnFillBufferDone(CodecBuffer * outBuf,int32_t * acquireFd)382 int32_t ComponentNode::OnFillBufferDone(CodecBuffer *outBuf, int32_t *acquireFd)
383 {
384     if (omxCallback_ == nullptr || outBuf == nullptr || omxCallback_->FillBufferDone == nullptr) {
385         HDF_LOGE("%{public}s error, omxCallback_ or buffer or FillBufferDone is null", __func__);
386         return HDF_FAILURE;
387     }
388 
389     OmxCodecBuffer omxCodecBuffer = {0};
390     Common::ConvertCodecBufferToOmxCodecBuffer(omxCodecBuffer, *outBuf);
391     omxCodecBuffer.size = sizeof(OmxCodecBuffer);
392     omxCodecBuffer.fenceFd = *acquireFd;
393     omxCodecBuffer.type = READ_WRITE_TYPE;
394     HDF_LOGD("%{public}s FillBufferDone ON", __func__);
395     omxCallback_->FillBufferDone(omxCallback_, appData_, &omxCodecBuffer);
396 
397     return HDF_SUCCESS;
398 }
399 
UseBuffer(uint32_t portIndex,OmxCodecBuffer & buffer)400 int32_t ComponentNode::UseBuffer(uint32_t portIndex, OmxCodecBuffer &buffer)
401 {
402     if (comp_ == nullptr || portIndex > OUTPUT_PORTINDEX) {
403         HDF_LOGE("%{public}s error, comp_ is null or portIndex[%{public}d] > OUTPUT_PORTINDEX", __func__, portIndex);
404         return HDF_ERR_INVALID_PARAM;
405     }
406     if (buffer.buffer == nullptr) {
407         HDF_LOGE("%{public}s error, buffer is null", __func__);
408         return HDF_ERR_INVALID_PARAM;
409     }
410 
411     AllocateBufferMode mode = ALLOCATE_INPUT_BUFFER_USER_PRESET;
412     if (portIndex == OUTPUT_PORTINDEX) {
413         mode = ALLOCATE_OUTPUT_BUFFER_USER_PRESET;
414     }
415     int32_t ret = SetPortMode(portIndex, buffer, mode);
416     if (ret != HDF_SUCCESS) {
417         HDF_LOGE("%{public}s error, SetPortMode failed", __func__);
418         return ret;
419     }
420 
421     CodecBuffer *codecBuffer = reinterpret_cast<CodecBuffer *>
422         (OsalMemCalloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo)));
423     if (codecBuffer == nullptr) {
424         HDF_LOGE("%{public}s error, codecBuffer is nullptr", __func__);
425         return HDF_FAILURE;
426     }
427     buffer.bufferId = bufferId_++;
428     Common::ConvertOmxCodecBufferToCodecBuffer(buffer, *codecBuffer);
429     if (portIndex == INPUT_PORTINDEX) {
430         ret = CodecQueueInput(comp_, codecBuffer, 0, buffer.fenceFd);
431         if (ret != HDF_SUCCESS) {
432             HDF_LOGE("%{public}s error, CodecQueueInput failed", __func__);
433             OsalMemFree(codecBuffer);
434             return ret;
435         }
436     } else if (portIndex == OUTPUT_PORTINDEX) {
437         ret = CodecQueueOutput(comp_, codecBuffer, 0, buffer.fenceFd);
438         if (ret != HDF_SUCCESS) {
439             HDF_LOGE("%{public}s error, CodecQueueOutput failed", __func__);
440             OsalMemFree(codecBuffer);
441             return ret;
442         }
443     }
444     Common::ConvertCodecBufferToOmxCodecBuffer(buffer, *codecBuffer);
445     OsalMemFree(codecBuffer);
446     return ret;
447 }
448 
AllocateBuffer(uint32_t portIndex,OmxCodecBuffer & buffer)449 int32_t ComponentNode::AllocateBuffer(uint32_t portIndex, OmxCodecBuffer &buffer)
450 {
451     if (comp_ == nullptr || portIndex > OUTPUT_PORTINDEX) {
452         HDF_LOGE("%{public}s error, comp_ is null or portIndex[%{public}d] > OUTPUT_PORTINDEX", __func__, portIndex);
453         return HDF_ERR_INVALID_PARAM;
454     }
455     if (buffer.buffer == nullptr) {
456         HDF_LOGE("%{public}s error, buffer is null", __func__);
457         return HDF_ERR_INVALID_PARAM;
458     }
459 
460     AllocateBufferMode mode = ALLOCATE_INPUT_BUFFER_CODEC_PRESET;
461     if (portIndex == OUTPUT_PORTINDEX) {
462         mode = ALLOCATE_OUTPUT_BUFFER_CODEC_PRESET;
463     }
464     int32_t ret = SetPortMode(portIndex, buffer, mode);
465     if (ret != HDF_SUCCESS) {
466         HDF_LOGE("%{public}s error, SetPortMode failed", __func__);
467         return ret;
468     }
469 
470     CodecBuffer *codecBuffer = reinterpret_cast<CodecBuffer *>
471         (OsalMemCalloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo)));
472     if (codecBuffer == nullptr) {
473         HDF_LOGE("%{public}s error, codecBuffer is nullptr", __func__);
474         return HDF_FAILURE;
475     }
476     buffer.bufferId = bufferId_++;
477     ConvertOmxCodecBufferToCodecBuffer(buffer, *codecBuffer);
478     if (portIndex == INPUT_PORTINDEX) {
479         ret = CodecQueueInput(comp_, codecBuffer, 0, buffer.fenceFd);
480         if (ret != HDF_SUCCESS) {
481             HDF_LOGE("%{public}s error, CodecQueueInput failed", __func__);
482             OsalMemFree(codecBuffer);
483             return ret;
484         }
485     } else if (portIndex == OUTPUT_PORTINDEX) {
486         ret = CodecQueueOutput(comp_, codecBuffer, 0, buffer.fenceFd);
487         if (ret != HDF_SUCCESS) {
488             HDF_LOGE("%{public}s error, CodecQueueOutput failed", __func__);
489             OsalMemFree(codecBuffer);
490             return ret;
491         }
492     }
493     Common::ConvertCodecBufferToOmxCodecBuffer(buffer, *codecBuffer);
494     OsalMemFree(codecBuffer);
495     return HDF_SUCCESS;
496 }
497 
FreeBuffer(uint32_t portIndex,const OmxCodecBuffer & buffer)498 int32_t ComponentNode::FreeBuffer(uint32_t portIndex, const OmxCodecBuffer &buffer)
499 {
500     if (comp_ == nullptr) {
501         HDF_LOGE("%{public}s comp_ is nullptr", __func__);
502         return HDF_ERR_INVALID_PARAM;
503     }
504     HDF_LOGW("%{public}s is not support!", __func__);
505     return HDF_ERR_NOT_SUPPORT;
506 }
507 
EmptyThisBuffer(const OmxCodecBuffer & buffer)508 int32_t ComponentNode::EmptyThisBuffer(const OmxCodecBuffer &buffer)
509 {
510     if (comp_ == nullptr) {
511         HDF_LOGE("%{public}s comp_ is nullptr", __func__);
512         return HDF_ERR_INVALID_PARAM;
513     }
514     if (buffer.bufferId >= bufferId_) {
515         HDF_LOGE("%{public}s error, bufferId = %{public}d is invalid.", __func__, buffer.bufferId);
516         return HDF_ERR_INVALID_PARAM;
517     }
518 
519     CodecBuffer *codecBuffer = reinterpret_cast<CodecBuffer *>
520         (OsalMemCalloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo)));
521     if (codecBuffer == nullptr) {
522         HDF_LOGE("%{public}s error, codecBuffer is nullptr", __func__);
523         return HDF_FAILURE;
524     }
525     Common::ConvertOmxCodecBufferToCodecBuffer(buffer, *codecBuffer);
526     int32_t ret = CodecQueueInput(comp_, codecBuffer, 0, buffer.fenceFd);
527     if (ret != HDF_SUCCESS) {
528         HDF_LOGE("%{public}s error, CodecQueueInput failed", __func__);
529     }
530     OsalMemFree(codecBuffer);
531 
532     return ret;
533 }
534 
FillThisBuffer(const OmxCodecBuffer & buffer)535 int32_t ComponentNode::FillThisBuffer(const OmxCodecBuffer &buffer)
536 {
537     if (comp_ == nullptr) {
538         HDF_LOGE("%{public}s comp_ is nullptr", __func__);
539         return HDF_ERR_INVALID_PARAM;
540     }
541     if (buffer.bufferId >= bufferId_) {
542         HDF_LOGE("%{public}s error, bufferId = %{public}d is invalid.", __func__, buffer.bufferId);
543         return HDF_ERR_INVALID_PARAM;
544     }
545 
546     CodecBuffer *codecBuffer = reinterpret_cast<CodecBuffer *>
547         (OsalMemCalloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo)));
548     if (codecBuffer == nullptr) {
549         HDF_LOGE("%{public}s error, codecBuffer is nullptr", __func__);
550         return HDF_FAILURE;
551     }
552     Common::ConvertOmxCodecBufferToCodecBuffer(buffer, *codecBuffer);
553     int32_t ret = CodecQueueOutput(comp_, codecBuffer, 0, buffer.fenceFd);
554     if (ret != HDF_SUCCESS) {
555         HDF_LOGE("%{public}s error, CodecQueueOutput failed", __func__);
556     }
557     OsalMemFree(codecBuffer);
558 
559     return ret;
560 }
561 
SetPortMode(uint32_t portIndex,OmxCodecBuffer & buffer,AllocateBufferMode mode)562 int32_t ComponentNode::SetPortMode(uint32_t portIndex, OmxCodecBuffer &buffer, AllocateBufferMode mode)
563 {
564     DirectionType direct = INPUT_TYPE;
565     if (portIndex == INPUT_PORTINDEX) {
566         inputMode_ = mode;
567     } else if (portIndex == OUTPUT_PORTINDEX) {
568         direct = OUTPUT_TYPE;
569         outputMode_ = mode;
570     }
571     BufferType type;
572     int32_t ret = Common::ConvertOmxBufferTypeToBufferType(buffer.bufferType, type);
573     if (ret != HDF_SUCCESS) {
574         HDF_LOGE("%{public}s error, ConvertOmxBufferTypeToBufferType failed", __func__);
575         return ret;
576     }
577 
578     ret = CodecSetPortMode(comp_, direct, mode, type);
579     if (ret != HDF_SUCCESS) {
580         HDF_LOGE("%{public}s error, CodecSetPortMode failed", __func__);
581     }
582     return HDF_SUCCESS;
583 }
584 
ChangeComponentState(uint32_t param)585 int32_t ComponentNode::ChangeComponentState(uint32_t param)
586 {
587     int32_t ret;
588     switch (param) {
589         case OMX_StateInvalid:
590         case OMX_StateLoaded:
591         case OMX_StateIdle:
592         case OMX_StateWaitForResources: {
593             ret = SetState((OMX_STATETYPE)param);
594             break;
595         }
596         case OMX_StateExecuting: {
597             ret = CodecStart(comp_);
598             if (ret == HDF_SUCCESS) {
599                 ret = SetState((OMX_STATETYPE)param);
600             }
601             break;
602         }
603         case OMX_StatePause: {
604             ret = CodecStop(comp_);
605             if (ret == HDF_SUCCESS) {
606                 ret = SetState((OMX_STATETYPE)param);
607             }
608             break;
609         }
610 
611         default: {
612             HDF_LOGW("%{public}s warn, unsupport state[%{public}d]", __func__, param);
613             ret = HDF_ERR_NOT_SUPPORT;
614             break;
615         }
616     }
617 
618     if (ret == HDF_SUCCESS) {
619         if (omxCallback_->EventHandler != nullptr) {
620             OMX_EVENTTYPE omxEvent;
621             EventInfo info = {0};
622             info.appData = appData_;
623             info.eventData = nullptr;
624             info.eventDataLen = 0;
625             omxEvent = OMX_EventCmdComplete;
626             info.data1 = OMX_CommandStateSet;
627             info.data2 = param;
628             omxCallback_->EventHandler(omxCallback_, omxEvent, &info);
629         }
630     } else {
631         HDF_LOGE("%{public}s error, state = %{public}d", __func__, param);
632     }
633     return ret;
634 }
635 
FlushComponent(uint32_t param)636 int32_t ComponentNode::FlushComponent(uint32_t param)
637 {
638     DirectionType directType = (DirectionType)param;
639     return CodecFlush(comp_, directType);
640 }
641 }  // namespace CodecAdapter
642 }  // namespace Codec
643 }  // namespace OHOS
644