1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <cstdio>
17094332d3Sopenharmony_ci#include <unistd.h>
18094332d3Sopenharmony_ci#include "codec_function_utils.h"
19094332d3Sopenharmony_ci#include <gtest/gtest.h>
20094332d3Sopenharmony_ci#include <securec.h>
21094332d3Sopenharmony_ci#include <servmgr_hdi.h>
22094332d3Sopenharmony_ci
23094332d3Sopenharmony_ci#define HDF_LOG_TAG codec_hdi_test
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_ciusing namespace std;
26094332d3Sopenharmony_ciusing namespace OHOS::HDI::Codec::V3_0;
27094332d3Sopenharmony_ciusing namespace OHOS::HDI::Display::Buffer::V1_0;
28094332d3Sopenharmony_ciusing namespace OHOS::HDI::Display::Composer::V1_0;
29094332d3Sopenharmony_ciIDisplayBuffer *FunctionUtil::buffer_ = nullptr;
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_ciFunctionUtil::FunctionUtil(CodecVersionType version)
32094332d3Sopenharmony_ci{
33094332d3Sopenharmony_ci    buffer_ = IDisplayBuffer::Get();
34094332d3Sopenharmony_ci    version_ = version;
35094332d3Sopenharmony_ci}
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ciFunctionUtil::~FunctionUtil()
38094332d3Sopenharmony_ci{
39094332d3Sopenharmony_ci    buffer_ = nullptr;
40094332d3Sopenharmony_ci}
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ciuint32_t FunctionUtil::AlignUp(uint32_t width)
43094332d3Sopenharmony_ci{
44094332d3Sopenharmony_ci    return (((width) + ALIGNMENT - 1) & (~(ALIGNMENT - 1)));
45094332d3Sopenharmony_ci}
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_civoid FunctionUtil::InitOmxCodecBuffer(OmxCodecBuffer &buffer, CodecBufferType type)
48094332d3Sopenharmony_ci{
49094332d3Sopenharmony_ci    buffer.bufferType = type;
50094332d3Sopenharmony_ci    buffer.fenceFd = ERROE_FENCEFD;
51094332d3Sopenharmony_ci    buffer.version = version_;
52094332d3Sopenharmony_ci    buffer.allocLen = BUFFER_SIZE;
53094332d3Sopenharmony_ci    buffer.fd = FD_DEFAULT;
54094332d3Sopenharmony_ci    buffer.bufferhandle = nullptr;
55094332d3Sopenharmony_ci    buffer.pts = 0;
56094332d3Sopenharmony_ci    buffer.flag = 0;
57094332d3Sopenharmony_ci    buffer.size = sizeof(OmxCodecBuffer);
58094332d3Sopenharmony_ci    buffer.type = READ_ONLY_TYPE;
59094332d3Sopenharmony_ci}
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_civoid FunctionUtil::InitCodecBufferWithAshMem(enum PortIndex port, int bufferSize, shared_ptr<OmxCodecBuffer> omxBuffer,
62094332d3Sopenharmony_ci    shared_ptr<OHOS::Ashmem> sharedMem)
63094332d3Sopenharmony_ci{
64094332d3Sopenharmony_ci    InitOmxCodecBuffer(*omxBuffer.get(), CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
65094332d3Sopenharmony_ci    omxBuffer->fd = sharedMem->GetAshmemFd();
66094332d3Sopenharmony_ci    omxBuffer->allocLen = bufferSize;
67094332d3Sopenharmony_ci    if (port == PortIndex::INDEX_INPUT) {
68094332d3Sopenharmony_ci        omxBuffer->type = READ_ONLY_TYPE;
69094332d3Sopenharmony_ci        sharedMem->MapReadAndWriteAshmem();
70094332d3Sopenharmony_ci    } else {
71094332d3Sopenharmony_ci        omxBuffer->type = READ_WRITE_TYPE;
72094332d3Sopenharmony_ci        sharedMem->MapReadOnlyAshmem();
73094332d3Sopenharmony_ci    }
74094332d3Sopenharmony_ci}
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_cibool FunctionUtil::InitBufferHandleParameter(sptr<ICodecComponent> component, OMX_PARAM_PORTDEFINITIONTYPE &param,
77094332d3Sopenharmony_ci    uint32_t port, CodecBufferType bufferType)
78094332d3Sopenharmony_ci{
79094332d3Sopenharmony_ci    InitParam(param);
80094332d3Sopenharmony_ci    param.nPortIndex = port;
81094332d3Sopenharmony_ci    std::vector<int8_t> inParam, outParam;
82094332d3Sopenharmony_ci    ObjectToVector(param, inParam);
83094332d3Sopenharmony_ci    auto ret = component->GetParameter(OMX_IndexParamPortDefinition, inParam, outParam);
84094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
85094332d3Sopenharmony_ci        HDF_LOGE("GetParameter OMX_IndexParamPortDefinition error");
86094332d3Sopenharmony_ci        return false;
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_ci    VectorToObject(outParam, param);
90094332d3Sopenharmony_ci    param.format.video.nFrameWidth = WIDTH;
91094332d3Sopenharmony_ci    param.format.video.nFrameHeight = HEIGHT;
92094332d3Sopenharmony_ci    param.format.video.nStride = AlignUp(WIDTH);
93094332d3Sopenharmony_ci    param.format.video.nSliceHeight = HEIGHT;
94094332d3Sopenharmony_ci    param.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
95094332d3Sopenharmony_ci    std::vector<int8_t> enc;
96094332d3Sopenharmony_ci    ObjectToVector(param, enc);
97094332d3Sopenharmony_ci    ret = component->SetParameter(OMX_IndexParamPortDefinition, enc);
98094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
99094332d3Sopenharmony_ci        HDF_LOGE("SetParameter OMX_IndexParamPortDefinition error");
100094332d3Sopenharmony_ci        return false;
101094332d3Sopenharmony_ci    }
102094332d3Sopenharmony_ci
103094332d3Sopenharmony_ci    std::vector<int8_t> data;
104094332d3Sopenharmony_ci    UseBufferType type;
105094332d3Sopenharmony_ci    type.size = sizeof(UseBufferType);
106094332d3Sopenharmony_ci    type.version.s.nVersionMajor = 1;
107094332d3Sopenharmony_ci    type.portIndex = port;
108094332d3Sopenharmony_ci    type.bufferType = bufferType;
109094332d3Sopenharmony_ci    ObjectToVector(type, data);
110094332d3Sopenharmony_ci    ret = component->SetParameter(OMX_IndexParamUseBufferType, data);
111094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
112094332d3Sopenharmony_ci        HDF_LOGE("SetParameter OMX_IndexParamUseBufferType error");
113094332d3Sopenharmony_ci        return false;
114094332d3Sopenharmony_ci    }
115094332d3Sopenharmony_ci    return true;
116094332d3Sopenharmony_ci}
117094332d3Sopenharmony_ci
118094332d3Sopenharmony_cibool FunctionUtil::FillCodecBufferWithBufferHandle(shared_ptr<OmxCodecBuffer> omxBuffer)
119094332d3Sopenharmony_ci{
120094332d3Sopenharmony_ci    AllocInfo alloc = {.width = WIDTH,
121094332d3Sopenharmony_ci                       .height = HEIGHT,
122094332d3Sopenharmony_ci                       .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
123094332d3Sopenharmony_ci                       .format = PIXEL_FMT_YCBCR_420_SP};
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_ci    BufferHandle *bufferHandle = nullptr;
126094332d3Sopenharmony_ci    if (buffer_ == nullptr) {
127094332d3Sopenharmony_ci        HDF_LOGE("buffer_ is nullptr");
128094332d3Sopenharmony_ci        return false;
129094332d3Sopenharmony_ci    }
130094332d3Sopenharmony_ci    auto ret = buffer_->AllocMem(alloc, bufferHandle);
131094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
132094332d3Sopenharmony_ci        HDF_LOGE("AllocMem error");
133094332d3Sopenharmony_ci        return false;
134094332d3Sopenharmony_ci    }
135094332d3Sopenharmony_ci    omxBuffer->bufferhandle = new NativeBuffer(bufferHandle);
136094332d3Sopenharmony_ci    return true;
137094332d3Sopenharmony_ci}
138094332d3Sopenharmony_ci
139094332d3Sopenharmony_cibool FunctionUtil::UseDynaBuffer(sptr<ICodecComponent> component, enum PortIndex port, int bufferCount,
140094332d3Sopenharmony_ci    int bufferSize)
141094332d3Sopenharmony_ci{
142094332d3Sopenharmony_ci    if (bufferCount <= 0 || bufferSize <= 0) {
143094332d3Sopenharmony_ci        HDF_LOGE("bufferCount <= 0 or bufferSize <= 0");
144094332d3Sopenharmony_ci        return false;
145094332d3Sopenharmony_ci    }
146094332d3Sopenharmony_ci
147094332d3Sopenharmony_ci    for (int i = 0; i < bufferCount; i++) {
148094332d3Sopenharmony_ci        auto omxBuffer = std::make_shared<OmxCodecBuffer>();
149094332d3Sopenharmony_ci        InitOmxCodecBuffer(*omxBuffer.get(), CODEC_BUFFER_TYPE_DYNAMIC_HANDLE);
150094332d3Sopenharmony_ci        FillCodecBufferWithBufferHandle(omxBuffer);
151094332d3Sopenharmony_ci        omxBuffer->allocLen = WIDTH * HEIGHT * NUMERATOR / DENOMINATOR;
152094332d3Sopenharmony_ci
153094332d3Sopenharmony_ci        OmxCodecBuffer outBuffer;
154094332d3Sopenharmony_ci        auto ret = component->UseBuffer(static_cast<uint32_t>(port), *omxBuffer.get(), outBuffer);
155094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
156094332d3Sopenharmony_ci            HDF_LOGE("UseBuffer error");
157094332d3Sopenharmony_ci            return false;
158094332d3Sopenharmony_ci        }
159094332d3Sopenharmony_ci
160094332d3Sopenharmony_ci        omxBuffer->bufferId = outBuffer.bufferId;
161094332d3Sopenharmony_ci        auto bufferInfo = std::make_shared<BufferInfo>();
162094332d3Sopenharmony_ci        bufferInfo->omxBuffer = omxBuffer;
163094332d3Sopenharmony_ci        inputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
164094332d3Sopenharmony_ci    }
165094332d3Sopenharmony_ci    return true;
166094332d3Sopenharmony_ci}
167094332d3Sopenharmony_ci
168094332d3Sopenharmony_cibool FunctionUtil::UseHandleBuffer(sptr<ICodecComponent> component, enum PortIndex port,
169094332d3Sopenharmony_ci    int bufferCount, int bufferSize)
170094332d3Sopenharmony_ci{
171094332d3Sopenharmony_ci    if (bufferCount <= 0 || bufferSize <= 0) {
172094332d3Sopenharmony_ci        HDF_LOGE("bufferCount <= 0 or bufferSize <= 0");
173094332d3Sopenharmony_ci        return false;
174094332d3Sopenharmony_ci    }
175094332d3Sopenharmony_ci
176094332d3Sopenharmony_ci    for (int i = 0; i < bufferCount; i++) {
177094332d3Sopenharmony_ci        auto omxBuffer = std::make_shared<OmxCodecBuffer>();
178094332d3Sopenharmony_ci        InitOmxCodecBuffer(*omxBuffer.get(), CODEC_BUFFER_TYPE_HANDLE);
179094332d3Sopenharmony_ci        FillCodecBufferWithBufferHandle(omxBuffer);
180094332d3Sopenharmony_ci        omxBuffer->allocLen = WIDTH * HEIGHT * NUMERATOR / DENOMINATOR;
181094332d3Sopenharmony_ci
182094332d3Sopenharmony_ci        OmxCodecBuffer outBuffer;
183094332d3Sopenharmony_ci        int32_t ret = component->UseBuffer(static_cast<uint32_t>(port), *omxBuffer.get(), outBuffer);
184094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
185094332d3Sopenharmony_ci            HDF_LOGE("UseBuffer error");
186094332d3Sopenharmony_ci            return false;
187094332d3Sopenharmony_ci        }
188094332d3Sopenharmony_ci
189094332d3Sopenharmony_ci        omxBuffer->bufferId = outBuffer.bufferId;
190094332d3Sopenharmony_ci        auto bufferInfo = std::make_shared<BufferInfo>();
191094332d3Sopenharmony_ci        bufferInfo->omxBuffer = omxBuffer;
192094332d3Sopenharmony_ci        outputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
193094332d3Sopenharmony_ci    }
194094332d3Sopenharmony_ci    return true;
195094332d3Sopenharmony_ci}
196094332d3Sopenharmony_ci
197094332d3Sopenharmony_cibool FunctionUtil::UseBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port,
198094332d3Sopenharmony_ci    int32_t bufferCount, int32_t bufferSize)
199094332d3Sopenharmony_ci{
200094332d3Sopenharmony_ci    for (int i = 0; i < bufferCount; i++) {
201094332d3Sopenharmony_ci        std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
202094332d3Sopenharmony_ci        int fd = OHOS::AshmemCreate(0, bufferSize);
203094332d3Sopenharmony_ci        shared_ptr<OHOS::Ashmem> sharedMem = make_shared<OHOS::Ashmem>(fd, bufferSize);
204094332d3Sopenharmony_ci        InitCodecBufferWithAshMem(port, bufferSize, omxBuffer, sharedMem);
205094332d3Sopenharmony_ci        OmxCodecBuffer outBuffer;
206094332d3Sopenharmony_ci        int32_t err = component->UseBuffer(static_cast<uint32_t>(port), *omxBuffer.get(), outBuffer);
207094332d3Sopenharmony_ci        if (err != HDF_SUCCESS) {
208094332d3Sopenharmony_ci            HDF_LOGE("UseBuffer error");
209094332d3Sopenharmony_ci            sharedMem->UnmapAshmem();
210094332d3Sopenharmony_ci            sharedMem->CloseAshmem();
211094332d3Sopenharmony_ci            return false;
212094332d3Sopenharmony_ci        }
213094332d3Sopenharmony_ci
214094332d3Sopenharmony_ci        omxBuffer->bufferId = outBuffer.bufferId;
215094332d3Sopenharmony_ci        omxBuffer->fd = FD_DEFAULT;
216094332d3Sopenharmony_ci        std::shared_ptr<BufferInfo> bufferInfo = std::make_shared<BufferInfo>();
217094332d3Sopenharmony_ci        bufferInfo->omxBuffer = omxBuffer;
218094332d3Sopenharmony_ci        bufferInfo->sharedMem = sharedMem;
219094332d3Sopenharmony_ci        if (port == PortIndex::INDEX_INPUT) {
220094332d3Sopenharmony_ci            inputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
221094332d3Sopenharmony_ci        } else {
222094332d3Sopenharmony_ci            outputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
223094332d3Sopenharmony_ci        }
224094332d3Sopenharmony_ci    }
225094332d3Sopenharmony_ci    return true;
226094332d3Sopenharmony_ci}
227094332d3Sopenharmony_ci
228094332d3Sopenharmony_cibool FunctionUtil::AllocateBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port,
229094332d3Sopenharmony_ci    int32_t bufferCount, int32_t bufferSize)
230094332d3Sopenharmony_ci{
231094332d3Sopenharmony_ci    for (int i = 0; i < bufferCount; i++) {
232094332d3Sopenharmony_ci        std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
233094332d3Sopenharmony_ci        InitOmxCodecBuffer(*omxBuffer.get(), CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
234094332d3Sopenharmony_ci        omxBuffer->allocLen = bufferSize;
235094332d3Sopenharmony_ci        if (port == PortIndex::INDEX_INPUT) {
236094332d3Sopenharmony_ci            omxBuffer->type = READ_ONLY_TYPE;
237094332d3Sopenharmony_ci        } else {
238094332d3Sopenharmony_ci            omxBuffer->type = READ_WRITE_TYPE;
239094332d3Sopenharmony_ci        }
240094332d3Sopenharmony_ci
241094332d3Sopenharmony_ci        OmxCodecBuffer outBuffer;
242094332d3Sopenharmony_ci        auto err = component->AllocateBuffer(static_cast<uint32_t>(port), *omxBuffer.get(), outBuffer);
243094332d3Sopenharmony_ci        if (err != HDF_SUCCESS) {
244094332d3Sopenharmony_ci            HDF_LOGE("AllocateBuffer error");
245094332d3Sopenharmony_ci            return false;
246094332d3Sopenharmony_ci        }
247094332d3Sopenharmony_ci        omxBuffer->type = outBuffer.type;
248094332d3Sopenharmony_ci        omxBuffer->bufferId = outBuffer.bufferId;
249094332d3Sopenharmony_ci
250094332d3Sopenharmony_ci        int fd = outBuffer.fd;
251094332d3Sopenharmony_ci        shared_ptr<OHOS::Ashmem> sharedMem = make_shared<OHOS::Ashmem>(fd, bufferSize);
252094332d3Sopenharmony_ci
253094332d3Sopenharmony_ci        std::shared_ptr<BufferInfo> bufferInfo = std::make_shared<BufferInfo>();
254094332d3Sopenharmony_ci        bufferInfo->omxBuffer = omxBuffer;
255094332d3Sopenharmony_ci        bufferInfo->sharedMem = sharedMem;
256094332d3Sopenharmony_ci        if (port == PortIndex::INDEX_INPUT) {
257094332d3Sopenharmony_ci            sharedMem->MapReadAndWriteAshmem();
258094332d3Sopenharmony_ci            inputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
259094332d3Sopenharmony_ci        } else {
260094332d3Sopenharmony_ci            sharedMem->MapReadOnlyAshmem();
261094332d3Sopenharmony_ci            outputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
262094332d3Sopenharmony_ci        }
263094332d3Sopenharmony_ci    }
264094332d3Sopenharmony_ci    return true;
265094332d3Sopenharmony_ci}
266094332d3Sopenharmony_ci
267094332d3Sopenharmony_cibool FunctionUtil::FreeBufferOnPort(sptr<ICodecComponent> component, enum PortIndex port)
268094332d3Sopenharmony_ci{
269094332d3Sopenharmony_ci    int32_t ret;
270094332d3Sopenharmony_ci    std::map<int32_t, std::shared_ptr<BufferInfo>> &buffer = inputBuffers_;
271094332d3Sopenharmony_ci    if (port == PortIndex::INDEX_OUTPUT) {
272094332d3Sopenharmony_ci        buffer = outputBuffers_;
273094332d3Sopenharmony_ci    }
274094332d3Sopenharmony_ci    for (auto [bufferId, bufferInfo] : buffer) {
275094332d3Sopenharmony_ci        ret = component->FreeBuffer(static_cast<uint32_t>(port), *bufferInfo->omxBuffer.get());
276094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
277094332d3Sopenharmony_ci            HDF_LOGE("FreeBuffer error");
278094332d3Sopenharmony_ci            return false;
279094332d3Sopenharmony_ci        }
280094332d3Sopenharmony_ci    }
281094332d3Sopenharmony_ci    buffer.clear();
282094332d3Sopenharmony_ci    return true;
283094332d3Sopenharmony_ci}
284094332d3Sopenharmony_ci
285094332d3Sopenharmony_ciint32_t FunctionUtil::GetPortParameter(sptr<ICodecComponent> component, PortIndex index,
286094332d3Sopenharmony_ci    OMX_PARAM_PORTDEFINITIONTYPE &param)
287094332d3Sopenharmony_ci{
288094332d3Sopenharmony_ci    InitParam(param);
289094332d3Sopenharmony_ci    param.nPortIndex = static_cast<OMX_U32>(index);
290094332d3Sopenharmony_ci    std::vector<int8_t> inParam;
291094332d3Sopenharmony_ci    ObjectToVector(param, inParam);
292094332d3Sopenharmony_ci
293094332d3Sopenharmony_ci    std::vector<int8_t> outParam;
294094332d3Sopenharmony_ci    auto ret = component->GetParameter(OMX_IndexParamPortDefinition, inParam, outParam);
295094332d3Sopenharmony_ci    VectorToObject(outParam, param);
296094332d3Sopenharmony_ci    return ret;
297094332d3Sopenharmony_ci}
298094332d3Sopenharmony_ci
299094332d3Sopenharmony_cibool FunctionUtil::PushAlongParam(OmxCodecBuffer &omxBuffer)
300094332d3Sopenharmony_ci{
301094332d3Sopenharmony_ci    const std::string processName = "cast_engine_service";
302094332d3Sopenharmony_ci    ProcessNameParam nameParam;
303094332d3Sopenharmony_ci    this->InitExtParam(nameParam);
304094332d3Sopenharmony_ci    int32_t ret = strcpy_s(nameParam.processName, sizeof(nameParam.processName), processName.c_str());
305094332d3Sopenharmony_ci    if (ret != EOK) {
306094332d3Sopenharmony_ci        return false;
307094332d3Sopenharmony_ci    }
308094332d3Sopenharmony_ci
309094332d3Sopenharmony_ci    uint32_t size = sizeof(nameParam);
310094332d3Sopenharmony_ci    uint8_t *ptr = reinterpret_cast<uint8_t*>(&nameParam);
311094332d3Sopenharmony_ci    for (uint32_t i = 0; i < size; i++) {
312094332d3Sopenharmony_ci        omxBuffer.alongParam.push_back(*(ptr + i));
313094332d3Sopenharmony_ci    }
314094332d3Sopenharmony_ci
315094332d3Sopenharmony_ci    return true;
316094332d3Sopenharmony_ci}
317094332d3Sopenharmony_ci
318094332d3Sopenharmony_cibool FunctionUtil::FillAndEmptyAllBuffer(sptr<ICodecComponent> component, CodecBufferType type)
319094332d3Sopenharmony_ci{
320094332d3Sopenharmony_ci    int32_t ret;
321094332d3Sopenharmony_ci    auto iter = outputBuffers_.begin();
322094332d3Sopenharmony_ci    for (; iter != outputBuffers_.end(); iter++) {
323094332d3Sopenharmony_ci        auto bufferInfo = iter->second;
324094332d3Sopenharmony_ci        if (type != bufferInfo->omxBuffer->bufferType) {
325094332d3Sopenharmony_ci            continue;
326094332d3Sopenharmony_ci        }
327094332d3Sopenharmony_ci        ret = component->FillThisBuffer(*bufferInfo->omxBuffer.get());
328094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
329094332d3Sopenharmony_ci            HDF_LOGE("FillThisBuffer error");
330094332d3Sopenharmony_ci            return false;
331094332d3Sopenharmony_ci        }
332094332d3Sopenharmony_ci    }
333094332d3Sopenharmony_ci    iter = inputBuffers_.begin();
334094332d3Sopenharmony_ci    for (; iter != inputBuffers_.end(); iter++) {
335094332d3Sopenharmony_ci        auto bufferInfo = iter->second;
336094332d3Sopenharmony_ci        if (type != bufferInfo->omxBuffer->bufferType) {
337094332d3Sopenharmony_ci            continue;
338094332d3Sopenharmony_ci        }
339094332d3Sopenharmony_ci        if (type == CODEC_BUFFER_TYPE_DYNAMIC_HANDLE && (!PushAlongParam(*bufferInfo->omxBuffer.get()))) {
340094332d3Sopenharmony_ci            HDF_LOGE("PushAlongParam error");
341094332d3Sopenharmony_ci            return false;
342094332d3Sopenharmony_ci        }
343094332d3Sopenharmony_ci        ret = component->EmptyThisBuffer(*bufferInfo->omxBuffer.get());
344094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
345094332d3Sopenharmony_ci            HDF_LOGE("EmptyThisBuffer error");
346094332d3Sopenharmony_ci            return false;
347094332d3Sopenharmony_ci        }
348094332d3Sopenharmony_ci    }
349094332d3Sopenharmony_ci    return true;
350094332d3Sopenharmony_ci}
351094332d3Sopenharmony_ci
352094332d3Sopenharmony_cibool FunctionUtil::WaitState(sptr<ICodecComponent> component, CodecStateType objState)
353094332d3Sopenharmony_ci{
354094332d3Sopenharmony_ci    CodecStateType state = CODEC_STATE_INVALID;
355094332d3Sopenharmony_ci    uint32_t count = 0;
356094332d3Sopenharmony_ci    do {
357094332d3Sopenharmony_ci        usleep(WAIT_TIME);
358094332d3Sopenharmony_ci        auto ret = component->GetState(state);
359094332d3Sopenharmony_ci        if (ret != HDF_SUCCESS) {
360094332d3Sopenharmony_ci            HDF_LOGE("EmptyThisBuffer error");
361094332d3Sopenharmony_ci            return false;
362094332d3Sopenharmony_ci        }
363094332d3Sopenharmony_ci        count++;
364094332d3Sopenharmony_ci    } while (state != objState && count <= MAX_WAIT);
365094332d3Sopenharmony_ci    return true;
366094332d3Sopenharmony_ci}
367094332d3Sopenharmony_ci
368