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