• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <OMX_Component.h>
17 #include <OMX_Core.h>
18 #include <OMX_Video.h>
19 #include <OMX_VideoExt.h>
20 #include <ashmem.h>
21 #include <buffer_handle.h>
22 #include <gtest/gtest.h>
23 #include <hdf_log.h>
24 #include <osal_mem.h>
25 #include <securec.h>
26 #include <servmgr_hdi.h>
27 #include "codec_callback_if.h"
28 #include "codec_component_manager.h"
29 #include "codec_component_type.h"
30 #include "codec_omx_ext.h"
31 #include "hdf_io_service_if.h"
32 #include "v1_0/display_composer_type.h"
33 #include "v1_0/display_buffer_type.h"
34 #include "v1_0/include/idisplay_buffer.h"
35 #define HDF_LOG_TAG codec_hdi_test
36 
37 using namespace std;
38 using namespace testing::ext;
39 using namespace OHOS::HDI::Display::Buffer::V1_0;
40 using namespace OHOS::HDI::Display::Composer::V1_0;
41 namespace {
42 constexpr int32_t WIDTH = 640;
43 #ifdef SUPPORT_OMX
44 constexpr uint32_t MAX_ROLE_INDEX = 1000;
45 constexpr int32_t ROLE_LEN = 240;
46 #endif
47 constexpr int32_t HEIGHT = 480;
48 constexpr int32_t BUFFER_SIZE = WIDTH * HEIGHT * 3;
49 constexpr int32_t FRAMERATE = 30 << 16;
50 constexpr uint32_t BUFFER_ID_ERROR = 65000;
51 constexpr uint32_t WAIT_TIME = 1000;
52 constexpr uint32_t MAX_WAIT = 50;
53 constexpr uint32_t ERROR_FENCEFD = 1;
54 constexpr uint32_t BUFFER_LEN = 1024;
55 static IDisplayBuffer *gralloc_ = nullptr;
56 
InitCodecBuffer(OmxCodecBuffer & buffer,CodecBufferType type,OMX_VERSIONTYPE & version)57 static void InitCodecBuffer(OmxCodecBuffer& buffer, CodecBufferType type, OMX_VERSIONTYPE& version)
58 {
59     buffer.bufferType = type;
60     buffer.fenceFd = -1;
61     buffer.version = version;
62     buffer.allocLen = BUFFER_SIZE;
63     buffer.buffer = 0;
64     buffer.bufferLen = 0;
65     buffer.pts = 0;
66     buffer.flag = 0;
67     buffer.type = READ_WRITE_TYPE;
68 }
69 
70 class CodecHdiOmxTest : public testing::Test {
71 public:
72     enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 };
73     struct BufferInfo {
74         std::shared_ptr<OmxCodecBuffer> omxBuffer;
75         std::shared_ptr<OHOS::Ashmem> sharedMem;
76         BufferHandle *bufferHandle;
BufferInfo__anon214d62c90111::CodecHdiOmxTest::BufferInfo77         BufferInfo()
78         {
79             omxBuffer = nullptr;
80             sharedMem = nullptr;
81             bufferHandle = nullptr;
82         }
~BufferInfo__anon214d62c90111::CodecHdiOmxTest::BufferInfo83         ~BufferInfo()
84         {
85             omxBuffer = nullptr;
86             if (sharedMem != nullptr) {
87                 sharedMem->UnmapAshmem();
88                 sharedMem->CloseAshmem();
89                 sharedMem = nullptr;
90             }
91             if (bufferHandle != nullptr && gralloc_ != nullptr) {
92                 gralloc_->FreeMem(*bufferHandle);
93                 bufferHandle = nullptr;
94             }
95         }
96     };
97     template <typename T>
InitParam(T & param)98     void InitParam(T &param)
99     {
100         int32_t ret = memset_s(&param, sizeof(param), 0x0, sizeof(param));
101         ASSERT_EQ(ret, EOK);
102         param.nSize = sizeof(param);
103         param.nVersion = version_;
104     }
105 
106     template <typename T>
InitExtParam(T & param)107     void InitExtParam(T &param)
108     {
109         int32_t ret = memset_s(&param, sizeof(param), 0x0, sizeof(param));
110         ASSERT_EQ(ret, EOK);
111         param.size = sizeof(param);
112         param.version = version_;
113     }
114 
InitCodecBufferWithAshMem(enum PortIndex portIndex,int bufferSize,shared_ptr<OmxCodecBuffer> omxBuffer,shared_ptr<OHOS::Ashmem> sharedMem)115     void InitCodecBufferWithAshMem(enum PortIndex portIndex, int bufferSize, shared_ptr<OmxCodecBuffer> omxBuffer,
116                                    shared_ptr<OHOS::Ashmem> sharedMem)
117     {
118         omxBuffer->size = sizeof(OmxCodecBuffer);
119         omxBuffer->version = version_;
120         omxBuffer->bufferType = CODEC_BUFFER_TYPE_AVSHARE_MEM_FD;
121         omxBuffer->bufferLen = sizeof(int);
122         omxBuffer->buffer = (uint8_t *)(uintptr_t)sharedMem->GetAshmemFd();
123         omxBuffer->allocLen = bufferSize;
124         omxBuffer->fenceFd = -1;
125         omxBuffer->pts = 0;
126         omxBuffer->flag = 0;
127         if (portIndex == PortIndex::PORT_INDEX_INPUT) {
128             omxBuffer->type = READ_ONLY_TYPE;
129             sharedMem->MapReadAndWriteAshmem();
130         } else {
131             omxBuffer->type = READ_WRITE_TYPE;
132             sharedMem->MapReadOnlyAshmem();
133         }
134     }
135 
UseBufferOnPort(enum PortIndex portIndex,int32_t bufferCount,int32_t bufferSize)136     bool UseBufferOnPort(enum PortIndex portIndex, int32_t bufferCount, int32_t bufferSize)
137     {
138         for (int i = 0; i < bufferCount; i++) {
139             std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
140             if (omxBuffer == nullptr) {
141                 return false;
142             }
143 
144             int fd = OHOS::AshmemCreate(0, bufferSize);
145             shared_ptr<OHOS::Ashmem> sharedMem = make_shared<OHOS::Ashmem>(fd, bufferSize);
146             if (sharedMem == nullptr) {
147                 if (fd >= 0) {
148                     close(fd);
149                     fd = -1;
150                 }
151                 return false;
152             }
153             InitCodecBufferWithAshMem(portIndex, bufferSize, omxBuffer, sharedMem);
154             auto err = component_->UseBuffer(component_, static_cast<uint32_t>(portIndex), omxBuffer.get());
155             if (err != HDF_SUCCESS) {
156                 sharedMem->UnmapAshmem();
157                 sharedMem->CloseAshmem();
158                 sharedMem = nullptr;
159                 omxBuffer = nullptr;
160                 return false;
161             }
162             omxBuffer->bufferLen = 0;
163             std::shared_ptr<BufferInfo> bufferInfo = std::make_shared<BufferInfo>();
164             bufferInfo->omxBuffer = omxBuffer;
165             bufferInfo->sharedMem = sharedMem;
166             if (portIndex == PortIndex::PORT_INDEX_INPUT) {
167                 inputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
168             } else {
169                 outputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
170             }
171         }
172         return true;
173     }
174 
FreeBufferOnPort(enum PortIndex portIndex)175     bool FreeBufferOnPort(enum PortIndex portIndex)
176     {
177         std::map<int32_t, std::shared_ptr<BufferInfo>> &buffer = inputBuffers_;
178         if (portIndex == PortIndex::PORT_INDEX_OUTPUT) {
179             buffer = outputBuffers_;
180         }
181         for (auto [bufferId, bufferInfo] : buffer) {
182             auto ret = component_->FreeBuffer(component_, static_cast<uint32_t>(portIndex),
183                 bufferInfo->omxBuffer.get());
184             if (ret != HDF_SUCCESS) {
185                 return false;
186             }
187         }
188         buffer.clear();
189         return true;
190     }
191 
waitState(OMX_STATETYPE objState)192     void waitState(OMX_STATETYPE objState)
193     {
194         OMX_STATETYPE state = OMX_StateInvalid;
195         uint32_t count = 0;
196         do {
197             usleep(WAIT_TIME);
198             auto ret = component_->GetState(component_, &state);
199             ASSERT_EQ(ret, HDF_SUCCESS);
200             count++;
201         } while (state != objState && count <= MAX_WAIT);
202     }
203 
InitBufferHandle(std::shared_ptr<OmxCodecBuffer> & omxBuffer,BufferHandle ** bufferHandle)204     void InitBufferHandle(std::shared_ptr<OmxCodecBuffer> &omxBuffer, BufferHandle **bufferHandle)
205     {
206         ASSERT_TRUE(gralloc_ != nullptr);
207         AllocInfo alloc = {.width = WIDTH,
208             .height = HEIGHT,
209             .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
210             .format = PIXEL_FMT_YCBCR_420_SP};
211 
212         auto err = gralloc_->AllocMem(alloc, *bufferHandle);
213         ASSERT_EQ(err, DISPLAY_SUCCESS);
214 
215         omxBuffer->size = static_cast<uint32_t>(sizeof(OmxCodecBuffer));
216         omxBuffer->version = version_;
217         omxBuffer->bufferLen = static_cast<uint32_t>(sizeof(BufferHandle));
218         omxBuffer->buffer = reinterpret_cast<uint8_t *>(*bufferHandle);
219         omxBuffer->allocLen = static_cast<uint32_t>(sizeof(BufferHandle));
220         omxBuffer->fenceFd = -1;
221         omxBuffer->pts = 0;
222         omxBuffer->flag = 0;
223     }
224 
SetUpTestCase()225     static void SetUpTestCase()
226     {
227         manager_ = GetCodecComponentManager();
228         gralloc_ = IDisplayBuffer::Get();
229         if (manager_ == nullptr) {
230             std::cout<<"GetCodecComponentManager ret nullptr"<<std::endl;
231             return;
232         }
233         auto count = manager_->GetComponentNum();
234         if (count > 0) {
235             CodecCompCapability *capList = (CodecCompCapability *)OsalMemAlloc(sizeof(CodecCompCapability) * count);
236             ASSERT_TRUE(capList != nullptr);
237             auto err = manager_->GetComponentCapabilityList(capList, count);
238             ASSERT_TRUE(err == HDF_SUCCESS);
239             compName_ = capList[0].compName;
240             OsalMemFree(capList);
241             capList = nullptr;
242         }
243     }
TearDownTestCase()244     static void TearDownTestCase()
245     {
246         CodecComponentManagerRelease();
247         manager_ = nullptr;
248     }
SetUp()249     void SetUp()
250     {
251         if (manager_ == nullptr) {
252             return;
253         }
254         callback_ = CodecCallbackTypeGet(nullptr);
255         if (callback_ == nullptr) {
256             return;
257         }
258         if (compName_.empty()) {
259             return;
260         }
261 
262         auto ret = manager_->CreateComponent(&component_, &componentId_, compName_.data(),
263                                              reinterpret_cast<int64_t>(this), callback_);
264         if (ret != HDF_SUCCESS) {
265             return;
266         }
267         struct CompVerInfo verInfo;
268         ret = component_->GetComponentVersion(component_, &verInfo);
269         if (ret != HDF_SUCCESS) {
270             return;
271         }
272         version_ = verInfo.compVersion;
273     }
TearDown()274     void TearDown()
275     {
276         if (manager_ != nullptr && component_ != nullptr) {
277             manager_->DestroyComponent(componentId_);
278         }
279         if (callback_ != nullptr) {
280             CodecCallbackTypeRelease(callback_);
281             callback_ = nullptr;
282         }
283     }
284 
285 public:
286     struct CodecComponentType *component_ = nullptr;
287     uint32_t componentId_ = 0;
288     struct CodecCallbackType *callback_ = nullptr;
289 
290     static inline struct CodecComponentManager *manager_ = nullptr;
291     static inline std::string compName_ = "";
292     union OMX_VERSIONTYPE version_;
293     std::map<int32_t, std::shared_ptr<BufferInfo>> inputBuffers_;
294     std::map<int32_t, std::shared_ptr<BufferInfo>> outputBuffers_;
295 };
296 
297 // Test GetComponentVersion
298 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetVersionTest_001, TestSize.Level1)
299 {
300     ASSERT_TRUE(component_ != nullptr);
301     struct CompVerInfo verInfo;
302     auto ret = component_->GetComponentVersion(component_, &verInfo);
303     ASSERT_EQ(ret, HDF_SUCCESS);
304 }
305 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetVersionTest_002, TestSize.Level1)
306 {
307     ASSERT_TRUE(component_ != nullptr);
308     auto ret = component_->GetComponentVersion(component_, nullptr);
309     ASSERT_NE(ret, HDF_SUCCESS);
310 }
311 
312 #ifdef SUPPORT_OMX
313 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetParameterTest_001, TestSize.Level1)
314 {
315     ASSERT_TRUE(component_ != nullptr);
316     CodecVideoPortFormatParam pixFormat;
317     InitExtParam(pixFormat);
318     pixFormat.portIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT);
319     pixFormat.codecColorIndex = 0;
320     auto ret = component_->GetParameter(component_, OMX_IndexCodecVideoPortFormat,
321                                         reinterpret_cast<int8_t *>(&pixFormat), sizeof(pixFormat));
322     ASSERT_EQ(ret, HDF_SUCCESS);
323 }
324 
325 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetParameterTest_002, TestSize.Level1)
326 {
327     ASSERT_TRUE(component_ != nullptr);
328     CodecVideoPortFormatParam pixFormat;
329     InitExtParam(pixFormat);
330     pixFormat.portIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
331     pixFormat.codecColorIndex = 0;
332     auto ret = component_->GetParameter(component_, OMX_IndexCodecVideoPortFormat,
333                                         reinterpret_cast<int8_t *>(&pixFormat), sizeof(pixFormat));
334     ASSERT_EQ(ret, HDF_SUCCESS);
335 }
336 #endif
337 
338 // Test GetParameter
339 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetParameterTest_003, TestSize.Level1)
340 {
341     ASSERT_TRUE(component_ != nullptr);
342     auto ret = component_->GetParameter(component_, OMX_IndexParamVideoPortFormat, nullptr, 0);
343     ASSERT_NE(ret, HDF_SUCCESS);
344 }
345 
346 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetParameterTest_004, TestSize.Level1)
347 {
348     ASSERT_TRUE(component_ != nullptr);
349     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
350     InitParam(param);
351     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
352     param.eCompressionFormat = OMX_VIDEO_CodingAVC;
353     auto ret = component_->GetParameter(component_, OMX_IndexParamVideoPortFormat, reinterpret_cast<int8_t *>(&param),
354                                         sizeof(param));
355     ASSERT_EQ(ret, HDF_SUCCESS);
356 }
357 
358 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetParameterTest_005, TestSize.Level1)
359 {
360     ASSERT_TRUE(component_ != nullptr);
361     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
362     int32_t ret = memset_s(&param, sizeof(param), 0, sizeof(param));
363     ASSERT_EQ(ret, EOK);
364     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
365     param.eCompressionFormat = OMX_VIDEO_CodingAVC;
366     ret = component_->GetParameter(component_, OMX_IndexParamVideoPortFormat, reinterpret_cast<int8_t *>(&param),
367                                         sizeof(param));
368     ASSERT_NE(ret, HDF_SUCCESS);
369 }
370 
371 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetParameterTest_006, TestSize.Level1)
372 {
373     ASSERT_TRUE(component_ != nullptr);
374     OMX_VIDEO_CONFIG_BITRATETYPE param;
375     InitParam(param);
376     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
377     auto ret = component_->GetParameter(component_, OMX_IndexParamVideoPortFormat, reinterpret_cast<int8_t *>(&param),
378                                         sizeof(param));
379     ASSERT_NE(ret, HDF_SUCCESS);
380 }
381 
382 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetParameterTest_007, TestSize.Level1)
383 {
384     ASSERT_TRUE(component_ != nullptr);
385     OMX_VIDEO_CONFIG_BITRATETYPE param;
386     InitParam(param);
387     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
388     auto ret = component_->GetParameter(component_, OMX_IndexVideoStartUnused, reinterpret_cast<int8_t *>(&param),
389                                         sizeof(param));
390     ASSERT_NE(ret, HDF_SUCCESS);
391 }
392 
393 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetParameterTest_001, TestSize.Level1)
394 {
395     ASSERT_TRUE(component_ != nullptr);
396     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
397     InitParam(param);
398     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
399     auto ret = component_->SetParameter(component_, OMX_IndexParamVideoPortFormat, reinterpret_cast<int8_t *>(&param),
400                                         sizeof(param));
401     ASSERT_EQ(ret, HDF_SUCCESS);
402 }
403 
404 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetParameterTest_002, TestSize.Level1)
405 {
406     ASSERT_TRUE(component_ != nullptr);
407     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
408     int32_t ret = memset_s(&param, sizeof(param), 0, sizeof(param));
409     ASSERT_EQ(ret, EOK);
410     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
411     ret = component_->SetParameter(component_, OMX_IndexParamVideoPortFormat, reinterpret_cast<int8_t *>(&param),
412                                         sizeof(param));
413     ASSERT_NE(ret, HDF_SUCCESS);
414 }
415 
416 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetParameterTest_003, TestSize.Level1)
417 {
418     ASSERT_TRUE(component_ != nullptr);
419     auto ret = component_->SetParameter(component_, OMX_IndexParamVideoPortFormat, nullptr, 0);
420     ASSERT_NE(ret, HDF_SUCCESS);
421 }
422 
423 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetParameterTest_004, TestSize.Level1)
424 {
425     ASSERT_TRUE(component_ != nullptr);
426     OMX_VIDEO_CONFIG_BITRATETYPE param;
427     InitParam(param);
428     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
429     auto ret = component_->SetParameter(component_, OMX_IndexParamVideoPortFormat, reinterpret_cast<int8_t *>(&param),
430                                         sizeof(param));
431     ASSERT_NE(ret, HDF_SUCCESS);
432 }
433 
434 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetParameterTest_005, TestSize.Level1)
435 {
436     ASSERT_TRUE(component_ != nullptr);
437     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
438     InitParam(param);
439     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
440     auto ret = component_->SetParameter(component_, OMX_IndexVideoStartUnused, reinterpret_cast<int8_t *>(&param),
441                                         sizeof(param));
442     ASSERT_NE(ret, HDF_SUCCESS);
443 }
444 
445 #ifdef SUPPORT_OMX
446 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetParameterTest_006, TestSize.Level1)
447 {
448     ASSERT_TRUE(component_ != nullptr);
449     CodecVideoPortFormatParam pixFormat;
450     InitExtParam(pixFormat);
451     pixFormat.portIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
452     pixFormat.codecColorIndex = 0;
453     auto ret = component_->GetParameter(component_, OMX_IndexCodecVideoPortFormat,
454                                         reinterpret_cast<int8_t *>(&pixFormat), sizeof(pixFormat));
455     ASSERT_EQ(ret, HDF_SUCCESS);
456     pixFormat.codecColorFormat = PIXEL_FMT_RGB_555;
457     ret = component_->SetParameter(component_, OMX_IndexCodecVideoPortFormat, reinterpret_cast<int8_t *>(&pixFormat),
458                                    sizeof(pixFormat));
459     ASSERT_EQ(ret, HDF_SUCCESS);
460 }
461 #endif
462 
463 // Test GetConfig
464 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetConfigTest_001, TestSize.Level1)
465 {
466     ASSERT_TRUE(component_ != nullptr);
467     OMX_VIDEO_CONFIG_BITRATETYPE param;
468     InitParam(param);
469     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT);
470     auto ret = component_->GetConfig(component_, OMX_IndexConfigVideoBitrate, reinterpret_cast<int8_t *>(&param),
471                                      sizeof(param));
472     ASSERT_EQ(ret, HDF_SUCCESS);
473 }
474 
475 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetConfigTest_002, TestSize.Level1)
476 {
477     ASSERT_TRUE(component_ != nullptr);
478     OMX_VIDEO_CONFIG_BITRATETYPE param;
479     InitParam(param);
480     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
481     auto ret = component_->GetConfig(component_, OMX_IndexConfigVideoBitrate, reinterpret_cast<int8_t *>(&param),
482                                      sizeof(param));
483     ASSERT_NE(ret, HDF_SUCCESS);
484 }
485 
486 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetConfigTest_003, TestSize.Level1)
487 {
488     ASSERT_TRUE(component_ != nullptr);
489     auto ret = component_->GetConfig(component_, OMX_IndexConfigVideoBitrate, nullptr, 0);
490     ASSERT_NE(ret, HDF_SUCCESS);
491 }
492 
493 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetConfigTest_004, TestSize.Level1)
494 {
495     ASSERT_TRUE(component_ != nullptr);
496     OMX_VIDEO_CONFIG_BITRATETYPE param;
497     InitParam(param);
498     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT);
499     auto ret = component_->GetConfig(component_, OMX_IndexVideoStartUnused,
500         reinterpret_cast<int8_t *>(&param), sizeof(param));
501     ASSERT_NE(ret, HDF_SUCCESS);
502 }
503 
504 // Test SetConfig
505 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetConfigTest_001, TestSize.Level1)
506 {
507     ASSERT_TRUE(component_ != nullptr);
508     OMX_VIDEO_CONFIG_BITRATETYPE param;
509     InitParam(param);
510     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT);
511     param.nEncodeBitrate = FRAMERATE;
512     auto ret = component_->SetConfig(component_, OMX_IndexConfigVideoBitrate, reinterpret_cast<int8_t *>(&param),
513                                      sizeof(param));
514     ASSERT_EQ(ret, HDF_SUCCESS);
515 }
516 
517 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetConfigTest_002, TestSize.Level1)
518 {
519     ASSERT_TRUE(component_ != nullptr);
520     OMX_VIDEO_CONFIG_BITRATETYPE param;
521     InitParam(param);
522     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
523     param.nEncodeBitrate = FRAMERATE;
524     auto ret = component_->SetConfig(component_, OMX_IndexConfigVideoBitrate, reinterpret_cast<int8_t *>(&param),
525                                      sizeof(param));
526     ASSERT_NE(ret, HDF_SUCCESS);
527 }
528 
529 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetConfigTest_003, TestSize.Level1)
530 {
531     ASSERT_TRUE(component_ != nullptr);
532     auto ret = component_->SetConfig(component_, OMX_IndexConfigVideoBitrate, nullptr, 0);
533     ASSERT_NE(ret, HDF_SUCCESS);
534 }
535 
536 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetConfigTest_004, TestSize.Level1)
537 {
538     ASSERT_TRUE(component_ != nullptr);
539     OMX_VIDEO_CONFIG_BITRATETYPE param;
540     InitParam(param);
541     param.nPortIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT);
542     auto ret = component_->SetConfig(component_, OMX_IndexVideoStartUnused,
543         reinterpret_cast<int8_t *>(&param), sizeof(param));
544     ASSERT_NE(ret, HDF_SUCCESS);
545 }
546 
547 #ifdef SUPPORT_OMX
548 // Test GetExtensionIndex
549 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetExtensionIndexTest_001, TestSize.Level1)
550 {
551     ASSERT_TRUE(component_ != nullptr);
552     OMX_INDEXTYPE indexType;
553     auto ret = component_->GetExtensionIndex(component_, "OMX.Topaz.index.param.extended_video",
554         reinterpret_cast<uint32_t *>(&indexType));
555     ASSERT_EQ(ret, HDF_SUCCESS);
556 }
557 #endif
558 
559 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetExtensionIndexTest_002, TestSize.Level1)
560 {
561     ASSERT_TRUE(component_ != nullptr);
562     OMX_INDEXTYPE indexType;
563     auto ret = component_->GetExtensionIndex(component_, nullptr, reinterpret_cast<uint32_t *>(&indexType));
564     ASSERT_NE(ret, HDF_SUCCESS);
565 }
566 
567 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetExtensionIndexTest_003, TestSize.Level1)
568 {
569     ASSERT_TRUE(component_ != nullptr);
570     OMX_INDEXTYPE indexType;
571     auto ret = component_->GetExtensionIndex(component_, "OMX.Topaz.index.param.extended_test",
572         reinterpret_cast<uint32_t *>(&indexType));
573     ASSERT_NE(ret, HDF_SUCCESS);
574 }
575 
576 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetExtensionIndexTest_004, TestSize.Level1)
577 {
578     ASSERT_TRUE(component_ != nullptr);
579     auto ret = component_->GetExtensionIndex(component_, "OMX.Topaz.index.param.extended_video", nullptr);
580     ASSERT_NE(ret, HDF_SUCCESS);
581 }
582 
583 // Test GetState
584 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetStateTest_001, TestSize.Level1)
585 {
586     ASSERT_TRUE(component_ != nullptr);
587     OMX_STATETYPE state;
588     auto ret = component_->GetState(component_, &state);
589     ASSERT_EQ(state, OMX_StateLoaded);
590     ASSERT_EQ(ret, HDF_SUCCESS);
591 }
592 
593 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiGetStateTest_002, TestSize.Level1)
594 {
595     ASSERT_TRUE(component_ != nullptr);
596     auto ret = component_->GetState(component_, nullptr);
597     ASSERT_NE(ret, HDF_SUCCESS);
598 }
599 
600 #ifdef SUPPORT_OMX
601 // Test ComponentTunnelRequest
602 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiTunnelRequestTest_001, TestSize.Level1)
603 {
604     ASSERT_TRUE(component_ != nullptr);
605     const int32_t tunneledComp = 1002;
606     const uint32_t tunneledPort = 101;
607     OMX_TUNNELSETUPTYPE tunnelSetup;
608     tunnelSetup.eSupplier = OMX_BufferSupplyInput;
609 
610     auto ret = component_->ComponentTunnelRequest(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT),
611         tunneledComp, tunneledPort, &tunnelSetup);
612     ASSERT_NE(ret, HDF_SUCCESS);
613 }
614 #endif
615 
616 // Test SendCommand
617 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiLoadedToExecutingTest_001, TestSize.Level1)
618 {
619     ASSERT_TRUE(component_ != nullptr);
620     auto ret = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateExecuting, nullptr, 0);
621     ASSERT_EQ(ret, HDF_SUCCESS);
622 }
623 
624 struct OmxCodecBuffer allocBuffer;
625 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiAllocateBufferTest_001, TestSize.Level1)
626 {
627     ASSERT_TRUE(component_ != nullptr);
628     InitCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_INVALID, version_);
629     auto ret = component_->AllocateBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT),
630         &allocBuffer);
631     ASSERT_NE(ret, HDF_SUCCESS);
632 }
633 
634 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiAllocateBufferTest_002, TestSize.Level1)
635 {
636     ASSERT_TRUE(component_ != nullptr);
637     InitCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_VIRTUAL_ADDR, version_);
638     auto ret = component_->AllocateBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT),
639         &allocBuffer);
640     ASSERT_NE(ret, HDF_SUCCESS);
641 }
642 
643 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiAllocateBufferTest_003, TestSize.Level1)
644 {
645     ASSERT_TRUE(component_ != nullptr);
646     InitCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_INVALID, version_);
647     auto ret = component_->AllocateBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT),
648         &allocBuffer);
649     ASSERT_NE(ret, HDF_SUCCESS);
650 }
651 
652 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiAllocateBufferTest_004, TestSize.Level1)
653 {
654     ASSERT_TRUE(component_ != nullptr);
655     InitCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_VIRTUAL_ADDR, version_);
656     auto ret = component_->AllocateBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT),
657         &allocBuffer);
658     ASSERT_NE(ret, HDF_SUCCESS);
659 }
660 
661 // Test UseBuffer
662 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_001, TestSize.Level1)
663 {
664     ASSERT_TRUE(component_ != nullptr);
665     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
666     ASSERT_TRUE(omxBuffer != nullptr);
667     omxBuffer->size = sizeof(OmxCodecBuffer);
668     omxBuffer->version = version_;
669     omxBuffer->bufferType = CODEC_BUFFER_TYPE_INVALID;
670     omxBuffer->bufferLen = 0;
671     omxBuffer->buffer = nullptr;
672     omxBuffer->allocLen = 0;
673     omxBuffer->fenceFd = -1;
674     omxBuffer->pts = 0;
675     omxBuffer->flag = 0;
676 
677     auto err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
678     ASSERT_NE(err, HDF_SUCCESS);
679 }
680 
681 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_002, TestSize.Level1)
682 {
683     ASSERT_TRUE(component_ != nullptr);
684     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
685     ASSERT_TRUE(omxBuffer != nullptr);
686     omxBuffer->size = sizeof(OmxCodecBuffer);
687     omxBuffer->version = version_;
688     omxBuffer->bufferType = CODEC_BUFFER_TYPE_INVALID;
689     omxBuffer->bufferLen = 0;
690     omxBuffer->buffer = nullptr;
691     omxBuffer->allocLen = 0;
692     omxBuffer->fenceFd = -1;
693     omxBuffer->pts = 0;
694     omxBuffer->flag = 0;
695 
696     auto err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT), omxBuffer.get());
697     ASSERT_NE(err, HDF_SUCCESS);
698 }
699 
700 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_003, TestSize.Level1)
701 {
702     ASSERT_TRUE(component_ != nullptr);
703     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
704     ASSERT_TRUE(omxBuffer != nullptr);
705     omxBuffer->size = sizeof(OmxCodecBuffer);
706     omxBuffer->version = version_;
707     omxBuffer->bufferType = CODEC_BUFFER_TYPE_VIRTUAL_ADDR;
708     omxBuffer->bufferLen = 0;
709     omxBuffer->buffer = nullptr;
710     omxBuffer->allocLen = 0;
711     omxBuffer->fenceFd = -1;
712     omxBuffer->pts = 0;
713     omxBuffer->flag = 0;
714 
715     auto err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
716     ASSERT_NE(err, HDF_SUCCESS);
717 }
718 
719 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_004, TestSize.Level1)
720 {
721     ASSERT_TRUE(component_ != nullptr);
722     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
723     ASSERT_TRUE(omxBuffer != nullptr);
724     omxBuffer->size = sizeof(OmxCodecBuffer);
725     omxBuffer->version = version_;
726     omxBuffer->bufferType = CODEC_BUFFER_TYPE_VIRTUAL_ADDR;
727     omxBuffer->bufferLen = 0;
728     omxBuffer->buffer = nullptr;
729     omxBuffer->allocLen = 0;
730     omxBuffer->fenceFd = -1;
731     omxBuffer->pts = 0;
732     omxBuffer->flag = 0;
733 
734     auto err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT), omxBuffer.get());
735     ASSERT_NE(err, HDF_SUCCESS);
736 }
737 
738 #ifdef SUPPORT_OMX
739 // Use buffer on input index
740 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_005, TestSize.Level1)
741 {
742     ASSERT_TRUE(component_ != nullptr);
743     OMX_PARAM_PORTDEFINITIONTYPE param;
744     InitParam(param);
745     param.nPortIndex = (OMX_U32)PortIndex::PORT_INDEX_INPUT;
746     auto err = component_->GetParameter(component_, OMX_IndexParamPortDefinition, reinterpret_cast<int8_t *>(&param),
747         sizeof(param));
748     ASSERT_EQ(err, HDF_SUCCESS);
749 
750     int32_t bufferSize = param.nBufferSize;
751     int32_t bufferCount = param.nBufferCountActual;
752     auto ret = UseBufferOnPort(PortIndex::PORT_INDEX_INPUT, bufferCount, bufferSize);
753     ASSERT_TRUE(ret);
754     FreeBufferOnPort(PortIndex::PORT_INDEX_INPUT);
755 }
756 
757 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_006, TestSize.Level1)
758 {
759     ASSERT_TRUE(component_ != nullptr);
760     auto err = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateIdle, NULL, 0);
761     ASSERT_EQ(err, HDF_SUCCESS);
762 
763     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
764     ASSERT_TRUE(omxBuffer != nullptr);
765     BufferHandle *bufferHandle = nullptr;
766     InitBufferHandle(omxBuffer, &bufferHandle);
767     omxBuffer->bufferType = CODEC_BUFFER_TYPE_HANDLE;
768 
769     err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
770     if (err != HDF_SUCCESS) {
771         HDF_LOGE("%{public}s failed to UseBuffer with  input port", __func__);
772         omxBuffer = nullptr;
773     }
774     ASSERT_EQ(err, HDF_SUCCESS);
775     omxBuffer->bufferLen = 0;
776     std::shared_ptr<BufferInfo> bufferInfo = std::make_shared<BufferInfo>();
777     ASSERT_TRUE(bufferInfo != nullptr);
778     bufferInfo->omxBuffer = omxBuffer;
779     bufferInfo->bufferHandle = bufferHandle;
780     inputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
781     FreeBufferOnPort(PortIndex::PORT_INDEX_INPUT);
782 }
783 
784 // Use Buffer on output index
785 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_007, TestSize.Level1)
786 {
787     ASSERT_TRUE(component_ != nullptr);
788     OMX_PARAM_PORTDEFINITIONTYPE param;
789     InitParam(param);
790     param.nPortIndex = (OMX_U32)PortIndex::PORT_INDEX_OUTPUT;
791     auto err = component_->GetParameter(component_, OMX_IndexParamPortDefinition, reinterpret_cast<int8_t *>(&param),
792         sizeof(param));
793     ASSERT_EQ(err, HDF_SUCCESS);
794 
795     int32_t bufferSize = param.nBufferSize;
796     int32_t bufferCount = param.nBufferCountActual;
797     bool ret = UseBufferOnPort(PortIndex::PORT_INDEX_OUTPUT, bufferCount, bufferSize);
798     ASSERT_TRUE(ret);
799     ret = FreeBufferOnPort(PortIndex::PORT_INDEX_OUTPUT);
800     ASSERT_TRUE(ret);
801 }
802 
803 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_008, TestSize.Level1)
804 {
805     ASSERT_TRUE(component_ != nullptr);
806     auto err = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateIdle, NULL, 0);
807     ASSERT_EQ(err, HDF_SUCCESS);
808     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
809     ASSERT_TRUE(omxBuffer != nullptr);
810     omxBuffer->size = sizeof(OmxCodecBuffer);
811     omxBuffer->version = version_;
812     omxBuffer->bufferType = CODEC_BUFFER_TYPE_DYNAMIC_HANDLE;
813     omxBuffer->bufferLen = 0;
814     omxBuffer->buffer = nullptr;
815     omxBuffer->allocLen = 0;
816     omxBuffer->fenceFd = -1;
817     omxBuffer->pts = 0;
818     omxBuffer->flag = 0;
819 
820     err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT), omxBuffer.get());
821     if (err != HDF_SUCCESS) {
822         HDF_LOGE("%{public}s failed to UseBuffer with  input port", __func__);
823         omxBuffer = nullptr;
824     }
825     ASSERT_EQ(err, HDF_SUCCESS);
826     omxBuffer->bufferLen = 0;
827     std::shared_ptr<BufferInfo> bufferInfo = std::make_shared<BufferInfo>();
828     ASSERT_TRUE(bufferInfo != nullptr);
829     bufferInfo->omxBuffer = omxBuffer;
830     outputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
831     FreeBufferOnPort(PortIndex::PORT_INDEX_OUTPUT);
832 }
833 
834 // Use buffer on input index error when OMX_ErrorInsufficientResources
835 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_009, TestSize.Level1)
836 {
837     ASSERT_TRUE(component_ != nullptr);
838     OMX_PARAM_PORTDEFINITIONTYPE param;
839     InitParam(param);
840     param.nPortIndex = (OMX_U32)PortIndex::PORT_INDEX_INPUT;
841     auto err = component_->GetParameter(component_, OMX_IndexParamPortDefinition, reinterpret_cast<int8_t *>(&param),
842         sizeof(param));
843     ASSERT_EQ(err, HDF_SUCCESS);
844 
845     bool ret = UseBufferOnPort(PortIndex::PORT_INDEX_INPUT, param.nBufferCountActual, param.nBufferSize);
846     ASSERT_TRUE(ret);
847     ret = UseBufferOnPort(PortIndex::PORT_INDEX_INPUT, 1, param.nBufferSize);
848     FreeBufferOnPort(PortIndex::PORT_INDEX_INPUT);
849     ASSERT_FALSE(ret);
850 }
851 // Use buffer on output index error when OMX_ErrorInsufficientResources
852 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_010, TestSize.Level1)
853 {
854     ASSERT_TRUE(component_ != nullptr);
855     OMX_PARAM_PORTDEFINITIONTYPE param;
856     InitParam(param);
857     param.nPortIndex = (OMX_U32)PortIndex::PORT_INDEX_OUTPUT;
858     auto err = component_->GetParameter(component_, OMX_IndexParamPortDefinition, reinterpret_cast<int8_t *>(&param),
859         sizeof(param));
860     ASSERT_EQ(err, HDF_SUCCESS);
861     bool ret = UseBufferOnPort(PortIndex::PORT_INDEX_OUTPUT, param.nBufferCountActual, param.nBufferSize);
862     ASSERT_TRUE(ret);
863     ret = UseBufferOnPort(PortIndex::PORT_INDEX_OUTPUT, 1, param.nBufferSize);
864     FreeBufferOnPort(PortIndex::PORT_INDEX_OUTPUT);
865     ASSERT_FALSE(ret);
866 }
867 
868 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiAllocateBufferTest_005, TestSize.Level1)
869 {
870     ASSERT_TRUE(component_ != nullptr);
871     auto err = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateIdle, NULL, 0);
872     ASSERT_EQ(err, HDF_SUCCESS);
873     InitCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD, version_);
874     err = component_->AllocateBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), &allocBuffer);
875     ASSERT_EQ(err, HDF_SUCCESS);
876     err = component_->FreeBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), &allocBuffer);
877     ASSERT_EQ(err, HDF_SUCCESS);
878 }
879 
880 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiAllocateBufferTest_006, TestSize.Level1)
881 {
882     ASSERT_TRUE(component_ != nullptr);
883     auto err = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateIdle, NULL, 0);
884     ASSERT_EQ(err, HDF_SUCCESS);
885     InitCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD, version_);
886     allocBuffer.type = READ_WRITE_TYPE;
887     err = component_->AllocateBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT), &allocBuffer);
888     ASSERT_EQ(err, HDF_SUCCESS);
889     err = component_->FreeBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT), &allocBuffer);
890     ASSERT_EQ(err, HDF_SUCCESS);
891 }
892 #endif
893 
894 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_011, TestSize.Level1)
895 {
896     ASSERT_TRUE(component_ != nullptr);
897     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
898     ASSERT_TRUE(omxBuffer != nullptr);
899     BufferHandle *bufferHandle = nullptr;
900     InitBufferHandle(omxBuffer, &bufferHandle);
901     omxBuffer->bufferType = CODEC_BUFFER_TYPE_INVALID;
902     auto err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
903     ASSERT_NE(err, HDF_SUCCESS);
904 }
905 
906 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_012, TestSize.Level1)
907 {
908     ASSERT_TRUE(component_ != nullptr);
909     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
910     ASSERT_TRUE(omxBuffer != nullptr);
911     BufferHandle *bufferHandle = nullptr;
912     InitBufferHandle(omxBuffer, &bufferHandle);
913     omxBuffer->bufferType = CODEC_BUFFER_TYPE_DYNAMIC_HANDLE;
914     omxBuffer->fenceFd = ERROR_FENCEFD;
915     auto err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
916     ASSERT_NE(err, HDF_SUCCESS);
917 }
918 
919 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseBufferTest_013, TestSize.Level1)
920 {
921     ASSERT_TRUE(component_ != nullptr);
922     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
923     ASSERT_TRUE(omxBuffer != nullptr);
924     auto err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
925     ASSERT_NE(err, HDF_SUCCESS);
926 
927     omxBuffer->bufferType = CODEC_BUFFER_TYPE_DYNAMIC_HANDLE;
928     err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
929     ASSERT_NE(err, HDF_SUCCESS);
930 
931     omxBuffer->bufferLen = BUFFER_LEN;
932     err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
933     ASSERT_NE(err, HDF_SUCCESS);
934 
935     omxBuffer->fenceFd = ERROR_FENCEFD;
936     err = component_->UseBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), omxBuffer.get());
937     ASSERT_NE(err, HDF_SUCCESS);
938 }
939 
940 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiUseEglImageTest_001, TestSize.Level1)
941 {
942     ASSERT_TRUE(component_ != nullptr);
943     struct OmxCodecBuffer buffer;
944     buffer.fenceFd = -1;
945     buffer.version = version_;
946     buffer.bufferType = CODEC_BUFFER_TYPE_AVSHARE_MEM_FD;
947     buffer.allocLen = BUFFER_SIZE;
948     buffer.buffer = 0;
949     buffer.bufferLen = 0;
950     buffer.pts = 0;
951     buffer.flag = 0;
952     buffer.type = READ_ONLY_TYPE;
953     auto eglImage = std::make_unique<int8_t[]>(BUFFER_SIZE);
954     ASSERT_TRUE(eglImage != nullptr);
955     auto ret = component_->UseEglImage(component_, &buffer, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT),
956         eglImage.get(), BUFFER_SIZE);
957     ASSERT_NE(ret, HDF_SUCCESS);
958     eglImage = nullptr;
959 }
960 
961 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiBufferFillAndEmptyTest_001, TestSize.Level1)
962 {
963     ASSERT_TRUE(component_ != nullptr);
964     auto err = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateIdle, NULL, 0);
965     ASSERT_EQ(err, HDF_SUCCESS);
966     OMX_PARAM_PORTDEFINITIONTYPE param;
967     InitParam(param);
968     param.nPortIndex = (OMX_U32)PortIndex::PORT_INDEX_INPUT;
969     err = component_->GetParameter(component_, OMX_IndexParamPortDefinition, reinterpret_cast<int8_t *>(&param),
970         sizeof(param));
971     ASSERT_EQ(err, HDF_SUCCESS);
972 
973     bool ret = UseBufferOnPort(PortIndex::PORT_INDEX_INPUT, param.nBufferCountActual, param.nBufferSize);
974     ASSERT_TRUE(ret);
975     InitParam(param);
976     param.nPortIndex = (OMX_U32)PortIndex::PORT_INDEX_OUTPUT;
977     err = component_->GetParameter(component_, OMX_IndexParamPortDefinition, reinterpret_cast<int8_t *>(&param),
978         sizeof(param));
979     ASSERT_EQ(err, HDF_SUCCESS);
980     ret = UseBufferOnPort(PortIndex::PORT_INDEX_OUTPUT, param.nBufferCountActual, param.nBufferSize);
981     ASSERT_TRUE(ret);
982     err = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateExecuting, NULL, 0);
983 
984     waitState(OMX_StateExecuting);
985     auto iter = outputBuffers_.begin();
986     if (iter != outputBuffers_.end()) {
987         auto ret = component_->FillThisBuffer(component_, iter->second->omxBuffer.get());
988         ASSERT_EQ(ret, HDF_SUCCESS);
989     }
990     iter = inputBuffers_.begin();
991     if (iter != inputBuffers_.end()) {
992         auto ret = component_->EmptyThisBuffer(component_, iter->second->omxBuffer.get());
993         ASSERT_EQ(ret, HDF_SUCCESS);
994     }
995 
996     err = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateIdle, nullptr, 0);
997     waitState(OMX_StateIdle);
998 
999     err = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateLoaded, nullptr, 0);
1000     FreeBufferOnPort(PortIndex::PORT_INDEX_INPUT);
1001     FreeBufferOnPort(PortIndex::PORT_INDEX_OUTPUT);
1002 
1003     waitState(OMX_StateLoaded);
1004 }
1005 
1006 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiFillThisBufferTest_002, TestSize.Level1)
1007 {
1008     ASSERT_TRUE(component_ != nullptr);
1009     allocBuffer.bufferType = CODEC_BUFFER_TYPE_AVSHARE_MEM_FD;
1010     allocBuffer.fenceFd = -1;
1011     allocBuffer.version = version_;
1012     allocBuffer.allocLen = BUFFER_SIZE;
1013     allocBuffer.buffer = 0;
1014     allocBuffer.bufferLen = 0;
1015     allocBuffer.pts = 0;
1016     allocBuffer.flag = 0;
1017     allocBuffer.type = READ_ONLY_TYPE;
1018     allocBuffer.bufferId = BUFFER_ID_ERROR;
1019     auto ret = component_->FillThisBuffer(component_, &allocBuffer);
1020     ASSERT_NE(ret, HDF_SUCCESS);
1021 }
1022 
1023 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiEmptyThisBufferTest_002, TestSize.Level1)
1024 {
1025     ASSERT_TRUE(component_ != nullptr);
1026     allocBuffer.bufferType = CODEC_BUFFER_TYPE_AVSHARE_MEM_FD;
1027     allocBuffer.fenceFd = -1;
1028     allocBuffer.version = version_;
1029     allocBuffer.allocLen = BUFFER_SIZE;
1030     allocBuffer.buffer = 0;
1031     allocBuffer.bufferLen = 0;
1032     allocBuffer.pts = 0;
1033     allocBuffer.flag = 0;
1034     allocBuffer.type = READ_ONLY_TYPE;
1035     allocBuffer.bufferId = BUFFER_ID_ERROR;
1036     auto ret = component_->EmptyThisBuffer(component_, &allocBuffer);
1037     ASSERT_NE(ret, HDF_SUCCESS);
1038 }
1039 
1040 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiSetCallbackTest_001, TestSize.Level1)
1041 {
1042     ASSERT_TRUE(component_ != nullptr);
1043     if (callback_ != nullptr) {
1044         CodecCallbackTypeRelease(callback_);
1045     }
1046     callback_ = CodecCallbackTypeGet(nullptr);
1047     ASSERT_TRUE(callback_ != nullptr);
1048     auto ret = component_->SetCallbacks(component_, callback_, (int64_t)this);
1049     ASSERT_EQ(ret, HDF_SUCCESS);
1050 }
1051 
1052 #ifdef SUPPORT_OMX
1053 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiRoleEnumTest_001, TestSize.Level1)
1054 {
1055     ASSERT_TRUE(component_ != nullptr);
1056     uint8_t role[ROLE_LEN] = {0};
1057     auto ret = component_->ComponentRoleEnum(component_, role, ROLE_LEN, 0);
1058     ASSERT_EQ(ret, HDF_SUCCESS);
1059 }
1060 #endif
1061 
1062 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiRoleEnumTest_002, TestSize.Level1)
1063 {
1064     ASSERT_TRUE(component_ != nullptr);
1065     auto ret = component_->ComponentRoleEnum(component_, nullptr, 0, 0);
1066     ASSERT_NE(ret, HDF_SUCCESS);
1067 }
1068 
1069 #ifdef SUPPORT_OMX
1070 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiRoleEnumTest_003, TestSize.Level1)
1071 {
1072     ASSERT_TRUE(component_ != nullptr);
1073     uint8_t role[ROLE_LEN] = {0};
1074     auto ret = component_->ComponentRoleEnum(component_, role, ROLE_LEN, MAX_ROLE_INDEX);
1075     ASSERT_NE(ret, HDF_SUCCESS);
1076 }
1077 
1078 // Executing to Idle
1079 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiExecutingToIdleTest_001, TestSize.Level1)
1080 {
1081     ASSERT_TRUE(component_ != nullptr);
1082     auto ret = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateIdle, nullptr, 0);
1083     ASSERT_EQ(ret, HDF_SUCCESS);
1084 }
1085 #endif
1086 
1087 // Release input buffer
1088 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiFreeBufferTest_001, TestSize.Level1)
1089 {
1090     ASSERT_TRUE(component_ != nullptr);
1091     allocBuffer.bufferType = CODEC_BUFFER_TYPE_AVSHARE_MEM_FD;
1092     allocBuffer.fenceFd = -1;
1093     allocBuffer.version = version_;
1094     allocBuffer.allocLen = BUFFER_SIZE;
1095     allocBuffer.buffer = 0;
1096     allocBuffer.bufferLen = 0;
1097     allocBuffer.pts = 0;
1098     allocBuffer.flag = 0;
1099     allocBuffer.type = READ_ONLY_TYPE;
1100     allocBuffer.bufferId = BUFFER_ID_ERROR;
1101     auto ret = component_->FreeBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT), &allocBuffer);
1102     ASSERT_NE(ret, HDF_SUCCESS);
1103 }
1104 
1105 #ifdef SUPPORT_OMX
1106 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiFreeBufferTest_002, TestSize.Level1)
1107 {
1108     ASSERT_TRUE(component_ != nullptr);
1109     OMX_PARAM_PORTDEFINITIONTYPE param;
1110     InitParam(param);
1111     param.nPortIndex = (OMX_U32)PortIndex::PORT_INDEX_OUTPUT;
1112     auto err = component_->GetParameter(component_, OMX_IndexParamPortDefinition, reinterpret_cast<int8_t *>(&param),
1113         sizeof(param));
1114     ASSERT_EQ(err, HDF_SUCCESS);
1115 
1116     int32_t bufferSize = param.nBufferSize;
1117     int32_t bufferCount = param.nBufferCountActual;
1118     auto ret = UseBufferOnPort(PortIndex::PORT_INDEX_OUTPUT, bufferCount, bufferSize);
1119     ASSERT_TRUE(ret);
1120     ret = FreeBufferOnPort(PortIndex::PORT_INDEX_OUTPUT);
1121     ASSERT_TRUE(ret);
1122 }
1123 #endif
1124 
1125 // Release input buffer
1126 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiFreeBufferTest_003, TestSize.Level1)
1127 {
1128     ASSERT_TRUE(component_ != nullptr);
1129     allocBuffer.bufferType = CODEC_BUFFER_TYPE_AVSHARE_MEM_FD;
1130     allocBuffer.fenceFd = -1;
1131     allocBuffer.version = version_;
1132     allocBuffer.allocLen = BUFFER_SIZE;
1133     allocBuffer.buffer = 0;
1134     allocBuffer.bufferLen = 0;
1135     allocBuffer.pts = 0;
1136     allocBuffer.flag = 0;
1137     allocBuffer.type = READ_ONLY_TYPE;
1138     allocBuffer.bufferId = BUFFER_ID_ERROR;
1139     auto ret = component_->FreeBuffer(component_, static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT), &allocBuffer);
1140     ASSERT_NE(ret, HDF_SUCCESS);
1141 }
1142 
1143 #ifdef SUPPORT_OMX
1144 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiFreeBufferTest_004, TestSize.Level1)
1145 {
1146     ASSERT_TRUE(component_ != nullptr);
1147     OMX_PARAM_PORTDEFINITIONTYPE param;
1148     InitParam(param);
1149     param.nPortIndex = (OMX_U32)PortIndex::PORT_INDEX_INPUT;
1150     auto err = component_->GetParameter(component_, OMX_IndexParamPortDefinition, reinterpret_cast<int8_t *>(&param),
1151         sizeof(param));
1152     ASSERT_EQ(err, HDF_SUCCESS);
1153 
1154     int32_t bufferSize = param.nBufferSize;
1155     int32_t bufferCount = param.nBufferCountActual;
1156     auto ret = UseBufferOnPort(PortIndex::PORT_INDEX_INPUT, bufferCount, bufferSize);
1157     ASSERT_TRUE(ret);
1158     ret = FreeBufferOnPort(PortIndex::PORT_INDEX_INPUT);
1159     ASSERT_TRUE(ret);
1160 }
1161 
1162 // When ComponentDeInit, must change to Loaded State
1163 HWTEST_F(CodecHdiOmxTest, HdfCodecHdiDeInitTest_001, TestSize.Level1)
1164 {
1165     ASSERT_TRUE(component_ != nullptr);
1166     auto ret = component_->SendCommand(component_, OMX_CommandStateSet, OMX_StateLoaded, nullptr, 0);
1167     ASSERT_EQ(ret, HDF_SUCCESS);
1168     // State changed OMX_StateIdle when release all this buffer
1169     OMX_STATETYPE state = OMX_StateInvalid;
1170     do {
1171         usleep(100);
1172         ret = component_->GetState(component_, &state);
1173         ASSERT_EQ(ret, HDF_SUCCESS);
1174     } while (state != OMX_StateLoaded);
1175     ret = component_->ComponentDeInit(component_);
1176     ASSERT_EQ(ret, HDF_SUCCESS);
1177 }
1178 #endif
1179 
1180 }  // namespace
1181