1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by 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 <gtest/gtest.h>
17 #include <securec.h>
18 #include <servmgr_hdi.h>
19 #include <vector>
20 #include "codec_function_utils.h"
21 #include "v3_0/codec_callback_service.h"
22 
23 #define ERR_COUNT (-1)
24 
25 using namespace std;
26 using namespace testing::ext;
27 using OHOS::sptr;
28 using OHOS::HDI::Base::NativeBuffer;
29 using namespace OHOS::HDI::Codec::V3_0;
30 using namespace OHOS::HDI::Display::Buffer::V1_0;
31 using namespace OHOS::HDI::Display::Composer::V1_0;
32 
33 namespace {
34 constexpr CodecType TYPE = CodecType::VIDEO_ENCODER;
35 constexpr AvCodecRole ROLE = MEDIA_ROLETYPE_VIDEO_AVC;
36 static sptr<ICodecComponent> g_component = nullptr;
37 static sptr<ICodecCallback> g_callback = nullptr;
38 static sptr<ICodecComponentManager> g_manager = nullptr;
39 static OHOS::HDI::Codec::V3_0::CodecVersionType g_version;
40 static std::string g_compName = "";
41 
42 class CodecHdiOmxEncTest : public testing::Test {
43 public:
SetUpTestCase()44     static void SetUpTestCase()
45     {
46         g_manager = ICodecComponentManager::Get();
47         int32_t count = 0;
48         auto ret = g_manager->GetComponentNum(count);
49         ASSERT_EQ(ret, HDF_SUCCESS);
50         if (count <= 0) {
51             return;
52         }
53 
54         std::vector<CodecCompCapability> capList;
55         auto err = g_manager->GetComponentCapabilityList(capList, count);
56         ASSERT_TRUE(err == HDF_SUCCESS);
57         for (auto cap : capList) {
58             if (cap.type == TYPE && cap.role == ROLE) {
59                 g_compName = cap.compName;
60                 break;
61             }
62         }
63     }
64 
TearDownTestCase()65     static void TearDownTestCase()
66     {
67         g_manager = nullptr;
68     }
69 
SetUp()70     void SetUp()
71     {
72         ASSERT_TRUE(g_manager != nullptr && !g_compName.empty());
73         g_callback = new CodecCallbackService();
74         ASSERT_TRUE(g_callback != nullptr);
75         auto ret = g_manager->CreateComponent(g_component, componentId_, g_compName.data(), APP_DATA, g_callback);
76         ASSERT_EQ(ret, HDF_SUCCESS);
77         ret = g_manager->CreateComponent(g_component, componentId_, "", APP_DATA, g_callback);
78         ASSERT_TRUE(ret != HDF_SUCCESS);
79         struct CompVerInfo verInfo;
80         ret = g_component->GetComponentVersion(verInfo);
81         ASSERT_EQ(ret, HDF_SUCCESS);
82         g_version = verInfo.compVersion;
83 
84         func_ = new FunctionUtil(g_version);
85         ASSERT_TRUE(func_ != nullptr);
86     }
87 
TearDown()88     void TearDown()
89     {
90         std::vector<int8_t> cmdData;
91         if (g_component != nullptr) {
92             g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_LOADED, cmdData);
93         }
94         if (g_manager != nullptr && g_component != nullptr) {
95             g_manager->DestroyComponent(componentId_);
96         }
97         g_component = nullptr;
98         g_callback = nullptr;
99         func_ = nullptr;
100     }
101 
102 public:
103     uint32_t componentId_ = 0;
104     sptr<FunctionUtil> func_ = nullptr;
105     const static uint32_t inputIndex = static_cast<uint32_t>(PortIndex::INDEX_INPUT);
106     const static uint32_t outputIndex = static_cast<uint32_t>(PortIndex::INDEX_OUTPUT);
107 };
108 
109 // Test GetComponentVersion
110 /**
111 * @tc.number : SUB_Driver_Codec_idlomx_0900
112 * @tc.name   : HdfCodecHdiGetVersionTest001
113 * @tc.desc   : Verify the GetComponentVersion function when the input parameter is valid.
114   @tc.type: FUNC
115 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetVersionTest001, TestSize.Level1)116 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetVersionTest001, TestSize.Level1)
117 {
118     ASSERT_TRUE(g_component != nullptr);
119     struct CompVerInfo verInfo;
120     auto ret = g_component->GetComponentVersion(verInfo);
121     ASSERT_EQ(ret, HDF_SUCCESS);
122 }
123 
124 /**
125 * @tc.number : SUB_Driver_Codec_idlomx_1000
126 * @tc.name   : HdfCodecHdiGetParameterTest001
127 * @tc.desc   : Verify the GetParameter function when the input parameter pixFormat.portIndex is outputIndex.
128   @tc.type: FUNC
129 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest001, TestSize.Level1)130 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest001, TestSize.Level1)
131 {
132     ASSERT_TRUE(g_component != nullptr);
133     CodecVideoPortFormatParam pixFormat;
134     func_->InitExtParam(pixFormat);
135     pixFormat.portIndex = outputIndex;
136     pixFormat.codecColorIndex = 0;
137 
138     std::vector<int8_t> inParam;
139     func_->ObjectToVector(pixFormat, inParam);
140 
141     std::vector<int8_t> outParam;
142     auto ret = g_component->GetParameter(OMX_IndexCodecVideoPortFormat, inParam, outParam);
143     ASSERT_EQ(ret, HDF_SUCCESS);
144 }
145 
146 /**
147 * @tc.number : SUB_Driver_Codec_idlomx_1100
148 * @tc.name   : HdfCodecHdiGetParameterTest002
149 * @tc.desc   : Verify the GetParameter function when the input parameter pixFormat.portIndex is inputIndex.
150   @tc.type: FUNC
151 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest002, TestSize.Level1)152 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest002, TestSize.Level1)
153 {
154     ASSERT_TRUE(g_component != nullptr);
155     CodecVideoPortFormatParam pixFormat;
156     func_->InitExtParam(pixFormat);
157     pixFormat.portIndex = inputIndex;
158     pixFormat.codecColorIndex = 0;
159 
160     std::vector<int8_t> inParam;
161     func_->ObjectToVector(pixFormat, inParam);
162 
163     std::vector<int8_t> outParam;
164     auto ret = g_component->GetParameter(OMX_IndexCodecVideoPortFormat, inParam, outParam);
165     ASSERT_EQ(ret, HDF_SUCCESS);
166 }
167 
168 // Test GetParameter
169 /**
170 * @tc.number : SUB_Driver_Codec_idlomx_1200
171 * @tc.name   : HdfCodecHdiGetParameterTest003
172 * @tc.desc   : Verify the GetParameter function when the input parameter is valid.
173   @tc.type: FUNC
174 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest003, TestSize.Level1)175 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest003, TestSize.Level1)
176 {
177     ASSERT_TRUE(g_component != nullptr);
178     std::vector<int8_t> inParam;
179     std::vector <int8_t> outParam;
180     auto ret = g_component->GetParameter(OMX_IndexParamVideoPortFormat, inParam, outParam);
181     ASSERT_NE(ret, HDF_SUCCESS);
182 }
183 
184 /**
185 * @tc.number : SUB_Driver_Codec_idlomx_1300
186 * @tc.name   : HdfCodecHdiGetParameterTest004
187 * @tc.desc   : Verify the GetParameter function when the input parameter is valid.
188   @tc.type: FUNC
189 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest004, TestSize.Level1)190 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest004, TestSize.Level1)
191 {
192     ASSERT_TRUE(g_component != nullptr);
193     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
194     func_->InitParam(param);
195     param.nPortIndex = inputIndex;
196     param.eCompressionFormat = OMX_VIDEO_CodingAVC;
197     std::vector<int8_t> inParam;
198     func_->ObjectToVector(param, inParam);
199 
200     std::vector<int8_t> outParam;
201     auto ret = g_component->GetParameter(OMX_IndexCodecVideoPortFormat, inParam, outParam);
202     ASSERT_EQ(ret, HDF_SUCCESS);
203 }
204 
205 /**
206 * @tc.number : SUB_Driver_Codec_idlomx_1400
207 * @tc.name   : HdfCodecHdiGetParameterTest005
208 * @tc.desc   : Verify the GetParameter function when the input parameter is invalid.
209   @tc.type: FUNC
210 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest005, TestSize.Level1)211 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest005, TestSize.Level1)
212 {
213     ASSERT_TRUE(g_component != nullptr);
214     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
215     int32_t ret = memset_s(&param, sizeof(param), 0, sizeof(param));
216     ASSERT_EQ(ret, EOK);
217     param.nPortIndex = inputIndex;
218     param.eCompressionFormat = OMX_VIDEO_CodingAVC;
219     std::vector<int8_t> inParam;
220     func_->ObjectToVector(param, inParam);
221 
222     std::vector<int8_t> outParam;
223     ret = g_component->GetParameter(OMX_IndexCodecVideoPortFormat, inParam, outParam);
224     ASSERT_NE(ret, HDF_SUCCESS);
225 }
226 
227 /**
228 * @tc.number : SUB_Driver_Codec_idlomx_1500
229 * @tc.name   : HdfCodecHdiGetParameterTest006
230 * @tc.desc   : Verify the GetParameter function when the input parameter is invalid.
231   @tc.type: FUNC
232 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest006, TestSize.Level1)233 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest006, TestSize.Level1)
234 {
235     ASSERT_TRUE(g_component != nullptr);
236     OMX_VIDEO_CONFIG_BITRATETYPE param;
237     func_->InitParam(param);
238     param.nPortIndex = inputIndex;
239     std::vector<int8_t> inParam;
240     func_->ObjectToVector(param, inParam);
241 
242     std::vector<int8_t> outParam;
243     auto ret = g_component->GetParameter(OMX_IndexCodecVideoPortFormat, inParam, outParam);
244     ASSERT_NE(ret, HDF_SUCCESS);
245 }
246 
247 /**
248 * @tc.number : SUB_Driver_Codec_idlomx_1600
249 * @tc.name   : HdfCodecHdiGetParameterTest007
250 * @tc.desc   : Verify the GetParameter function when the input parameter is invalid.
251   @tc.type: FUNC
252 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest007, TestSize.Level1)253 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetParameterTest007, TestSize.Level1)
254 {
255     ASSERT_TRUE(g_component != nullptr);
256     OMX_VIDEO_CONFIG_BITRATETYPE param;
257     func_->InitParam(param);
258     param.nPortIndex = inputIndex;
259     std::vector<int8_t> inParam;
260     func_->ObjectToVector(param, inParam);
261 
262     std::vector<int8_t> outParam;
263     auto ret = g_component->GetParameter(OMX_IndexVideoStartUnused, inParam, outParam);
264     ASSERT_NE(ret, HDF_SUCCESS);
265 }
266 
267 /**
268 * @tc.number : SUB_Driver_Codec_idlomx_1700
269 * @tc.name   : HdfCodecHdiSetParameterTest001
270 * @tc.desc   : Verify the SetParameter function when the input parameter is valid.
271   @tc.type: FUNC
272 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest001, TestSize.Level1)273 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest001, TestSize.Level1)
274 {
275     ASSERT_TRUE(g_component != nullptr);
276     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
277     func_->InitParam(param);
278     param.nPortIndex = inputIndex;
279     std::vector<int8_t> paramVec;
280     func_->ObjectToVector(param, paramVec);
281     auto ret = g_component->SetParameter(OMX_IndexParamVideoPortFormat, paramVec);
282     ASSERT_EQ(ret, HDF_SUCCESS);
283 }
284 
285 /**
286 * @tc.number : SUB_Driver_Codec_idlomx_1800
287 * @tc.name   : HdfCodecHdiSetParameterTest002
288 * @tc.desc   : Verify the SetParameter function when the input parameter is isvalid.
289   @tc.type: FUNC
290 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest002, TestSize.Level1)291 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest002, TestSize.Level1)
292 {
293     ASSERT_TRUE(g_component != nullptr);
294     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
295     int32_t ret = memset_s(&param, sizeof(param), 0, sizeof(param));
296     ASSERT_EQ(ret, EOK);
297     param.nPortIndex = inputIndex;
298     std::vector<int8_t> paramVec;
299     func_->ObjectToVector(param, paramVec);
300     ret = g_component->SetParameter(OMX_IndexParamVideoPortFormat, paramVec);
301     ASSERT_NE(ret, HDF_SUCCESS);
302 }
303 
304 /**
305 * @tc.number : SUB_Driver_Codec_idlomx_1900
306 * @tc.name   : HdfCodecHdiSetParameterTest003
307 * @tc.desc   : Verify the SetParameter function when the input parameter is isvalid.
308   @tc.type: FUNC
309 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest003, TestSize.Level1)310 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest003, TestSize.Level1)
311 {
312     ASSERT_TRUE(g_component != nullptr);
313     std::vector<int8_t> paramVec;
314     auto ret = g_component->SetParameter(OMX_IndexParamVideoPortFormat, paramVec);
315     ASSERT_NE(ret, HDF_SUCCESS);
316 }
317 
318 /**
319 * @tc.number : SUB_Driver_Codec_idlomx_2000
320 * @tc.name   : HdfCodecHdiSetParameterTest004
321 * @tc.desc   : Verify the SetParameter function when the input parameter param.nPortIndex = inputIndex.
322   @tc.type: FUNC
323 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest004, TestSize.Level1)324 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest004, TestSize.Level1)
325 {
326     ASSERT_TRUE(g_component != nullptr);
327     OMX_VIDEO_CONFIG_BITRATETYPE param;
328     func_->InitParam(param);
329     param.nPortIndex = inputIndex;
330     std::vector<int8_t> paramVec;
331     func_->ObjectToVector(param, paramVec);
332     auto ret = g_component->SetParameter(OMX_IndexParamVideoPortFormat, paramVec);
333     ASSERT_NE(ret, HDF_SUCCESS);
334 }
335 
336 /**
337 * @tc.number : SUB_Driver_Codec_idlomx_2100
338 * @tc.name   : HdfCodecHdiSetParameterTest005
339 * @tc.desc   : Verify the SetParameter function when the input parameter OMX_IndexVideoStartUnused.
340   @tc.type: FUNC
341 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest005, TestSize.Level1)342 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest005, TestSize.Level1)
343 {
344     ASSERT_TRUE(g_component != nullptr);
345     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
346     func_->InitParam(param);
347     param.nPortIndex = inputIndex;
348     std::vector<int8_t> paramVec;
349     func_->ObjectToVector(param, paramVec);
350     auto ret = g_component->SetParameter(OMX_IndexVideoStartUnused, paramVec);
351     ASSERT_NE(ret, HDF_SUCCESS);
352 }
353 
354 /**
355 * @tc.number : SUB_Driver_Codec_idlomx_2200
356 * @tc.name   : HdfCodecHdiSetParameterTest006
357 * @tc.desc   : Verify the SetParameter function when the input parameter is valid.
358   @tc.type: FUNC
359 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest006, TestSize.Level1)360 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetParameterTest006, TestSize.Level1)
361 {
362     ASSERT_TRUE(g_component != nullptr);
363     CodecVideoPortFormatParam pixFormat;
364     func_->InitExtParam(pixFormat);
365     pixFormat.portIndex = inputIndex;
366     pixFormat.codecColorIndex = 0;
367     std::vector<int8_t> inParam;
368     func_->ObjectToVector(pixFormat, inParam);
369 
370     std::vector<int8_t> outParam;
371     auto ret = g_component->GetParameter(OMX_IndexCodecVideoPortFormat, inParam, outParam);
372     ASSERT_EQ(ret, HDF_SUCCESS);
373 
374     pixFormat.codecColorFormat = PIXEL_FMT_YCBCR_420_SP;
375     std::vector<int8_t> paramVec;
376     func_->ObjectToVector(pixFormat, paramVec);
377     ret = g_component->SetParameter(OMX_IndexCodecVideoPortFormat, paramVec);
378     ASSERT_EQ(ret, HDF_SUCCESS);
379 }
380 
381 #ifdef SUPPORT_DMA_BUFFER
382 /**
383 * @tc.number : SUB_Driver_Codec_idlomx_2300
384 * @tc.name   : HdfCodecHdiDMABufferTest001
385 * @tc.desc   : Verify the codec support DMA buffer process.
386   @tc.type: FUNC
387 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiDMABufferTest001, TestSize.Level1)388 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiDMABufferTest001, TestSize.Level1)
389 {
390     ASSERT_TRUE(g_component != nullptr);
391     SupportBufferType bufferType;
392     func_->InitExtParam(bufferType);
393     bufferType.portIndex = outputIndex;
394     std::vector<int8_t> inParam, outParam;
395     func_->ObjectToVector(bufferType, inParam);
396     auto ret = g_component->GetParameter(OMX_IndexParamSupportBufferType, inParam, outParam);
397     ASSERT_EQ(ret, HDF_SUCCESS);
398     func_->VectorToObject(outParam, bufferType);
399     ASSERT_TRUE(bufferType.bufferTypes & CODEC_BUFFER_TYPE_DMA_MEM_FD) ;
400 }
401 #endif
402 
403 #ifdef SUPPORT_OMX_EXTEND
404 /**
405 * @tc.number : SUB_Driver_Codec_idlomx_2400
406 * @tc.name   : HdfCodecHdiUseBufferAndFreeBufferTest001
407 * @tc.desc   : Verify Use buffer on input index error when OMX_ErrorInsufficientResources.
408   @tc.type: FUNC
409 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferAndFreeBufferTest001, TestSize.Level1)410 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferAndFreeBufferTest001, TestSize.Level1)
411 {
412     ASSERT_TRUE(g_component != nullptr);
413     std::vector<int8_t> cmdData;
414     auto ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
415     ASSERT_EQ(ret, HDF_SUCCESS);
416 
417     OMX_PARAM_PORTDEFINITIONTYPE param;
418     func_->GetPortParameter(g_component, PortIndex::INDEX_INPUT, param);
419 
420     auto err = func_->UseBufferOnPort(g_component, PortIndex::INDEX_INPUT, param.nBufferCountActual,
421         param.nBufferSize);
422     ASSERT_TRUE(err);
423     err = func_->UseBufferOnPort(g_component, PortIndex::INDEX_INPUT, 1, param.nBufferSize);
424     ASSERT_FALSE(err);
425     err = func_->FreeBufferOnPort(g_component, PortIndex::INDEX_INPUT);
426     ASSERT_TRUE(err);
427 }
428 
429 /**
430 * @tc.number : SUB_Driver_Codec_idlomx_2500
431 * @tc.name   : HdfCodecHdiUseBufferAndFreeBufferTest002
432 * @tc.desc   : Verify Use buffer on output index error when OMX_ErrorInsufficientResources.
433   @tc.type: FUNC
434 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferAndFreeBufferTest002, TestSize.Level1)435 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferAndFreeBufferTest002, TestSize.Level1)
436 {
437     ASSERT_TRUE(g_component != nullptr);
438     std::vector<int8_t> cmdData;
439     auto ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
440     ASSERT_EQ(ret, HDF_SUCCESS);
441 
442     OMX_PARAM_PORTDEFINITIONTYPE param;
443     func_->GetPortParameter(g_component, PortIndex::INDEX_OUTPUT, param);
444 
445     auto err = func_->UseBufferOnPort(g_component, PortIndex::INDEX_OUTPUT, param.nBufferCountActual,
446         param.nBufferSize);
447     ASSERT_TRUE(err);
448     err = func_->UseBufferOnPort(g_component, PortIndex::INDEX_OUTPUT, 1, param.nBufferSize);
449     ASSERT_FALSE(err);
450     err = func_->FreeBufferOnPort(g_component, PortIndex::INDEX_OUTPUT);
451     ASSERT_TRUE(err);
452 }
453 #endif
454 
455 /**
456 * @tc.number : SUB_Driver_Codec_idlomx_2600
457 * @tc.name   : HdfCodecHdiEmptyAndFillBufferTest001
458 * @tc.desc   : Verify the encode EmptyAndFillBuffer process.
459   @tc.type: FUNC
460 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiEmptyAndFillBufferTest001, TestSize.Level1)461 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiEmptyAndFillBufferTest001, TestSize.Level1)
462 {
463     ASSERT_TRUE(g_component != nullptr);
464     std::vector<int8_t> cmdData;
465     auto ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
466     ASSERT_EQ(ret, HDF_SUCCESS);
467 
468     OMX_PARAM_PORTDEFINITIONTYPE param;
469     auto err = func_->InitBufferHandleParameter(g_component, param, inputIndex, CODEC_BUFFER_TYPE_DYNAMIC_HANDLE);
470     ASSERT_TRUE(err);
471     ret = func_->GetPortParameter(g_component, PortIndex::INDEX_INPUT, param);
472     ASSERT_EQ(ret, HDF_SUCCESS);
473     err = func_->UseDynaBuffer(g_component, PortIndex::INDEX_INPUT, param.nBufferCountActual, param.nBufferSize);
474     ASSERT_TRUE(err);
475 
476     ret = func_->GetPortParameter(g_component, PortIndex::INDEX_OUTPUT, param);
477     ASSERT_EQ(ret, HDF_SUCCESS);
478     err = func_->UseBufferOnPort(g_component, PortIndex::INDEX_OUTPUT, param.nBufferCountActual, param.nBufferSize);
479     ASSERT_TRUE(err);
480 
481     ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_EXECUTING, cmdData);
482     ASSERT_EQ(ret, HDF_SUCCESS);
483     err = func_->WaitState(g_component, CODEC_STATE_EXECUTING);
484     ASSERT_TRUE(err);
485     err = func_->FillAndEmptyAllBuffer(g_component, CODEC_BUFFER_TYPE_DYNAMIC_HANDLE);
486     ASSERT_TRUE(err);
487 
488     ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
489     ASSERT_EQ(ret, HDF_SUCCESS);
490     err = func_->WaitState(g_component, CODEC_STATE_IDLE);
491     ASSERT_TRUE(err);
492     ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_LOADED, cmdData);
493     ASSERT_EQ(ret, HDF_SUCCESS);
494     err = func_->WaitState(g_component, CODEC_STATE_IDLE);
495     ASSERT_TRUE(err);
496 
497     err = func_->FreeBufferOnPort(g_component, PortIndex::INDEX_INPUT);
498     ASSERT_TRUE(err);
499     err = func_->FreeBufferOnPort(g_component, PortIndex::INDEX_OUTPUT);
500     ASSERT_TRUE(err);
501     err = func_->WaitState(g_component, CODEC_STATE_LOADED);
502     ASSERT_TRUE(err);
503 }
504 /**
505 * @tc.number : SUB_Driver_Codec_idlomx_2700
506 * @tc.name   : HdfCodecHdiFreeBufferTest001
507 * @tc.desc   : Verify the encode Release output buffer.
508   @tc.type: FUNC
509 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiFreeBufferTest001, TestSize.Level1)510 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiFreeBufferTest001, TestSize.Level1)
511 {
512     ASSERT_TRUE(g_component != nullptr);
513     struct OmxCodecBuffer omxBuffer;
514     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
515     omxBuffer.bufferId = BUFFER_ID_ERROR;
516     auto ret = g_component->FreeBuffer(outputIndex, omxBuffer);
517     ASSERT_NE(ret, HDF_SUCCESS);
518 }
519 
520 /**
521 * @tc.number : SUB_Driver_Codec_idlomx_2800
522 * @tc.name   : HdfCodecHdiFreeBufferTest002
523 * @tc.desc   : Verify the encode Release input buffer.
524   @tc.type: FUNC
525 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiFreeBufferTest002, TestSize.Level1)526 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiFreeBufferTest002, TestSize.Level1)
527 {
528     ASSERT_TRUE(g_component != nullptr);
529     struct OmxCodecBuffer omxBuffer;
530     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
531     omxBuffer.bufferId = BUFFER_ID_ERROR;
532     auto ret = g_component->FreeBuffer(inputIndex, omxBuffer);
533     ASSERT_NE(ret, HDF_SUCCESS);
534 }
535 
536 #ifdef SUPPORT_OMX_EXTEND
537 /**
538 * @tc.number : SUB_Driver_Codec_idlomx_2900
539 * @tc.name   : HdfCodecHdiDeInitTest001
540 * @tc.desc   : Verify When ComponentDeInit, must change to Loaded State.
541   @tc.type: FUNC
542 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiDeInitTest001, TestSize.Level1)543 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiDeInitTest001, TestSize.Level1)
544 {
545     ASSERT_TRUE(g_component != nullptr);
546     auto ret = g_component->ComponentDeInit();
547     ASSERT_EQ(ret, HDF_SUCCESS);
548 }
549 #endif
550 
551 #ifdef SUPPORT_HIGH_WORK_FREQUENCY
552 /**
553 * @tc.number : SUB_Driver_Codec_idlomx_3000
554 * @tc.name   : HdfCodecHdiHighWorkingFrequencyTest001
555 * @tc.desc   : Verify the encode support HIGH_WORK_FREQUENCY process.
556   @tc.type: FUNC
557 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiHighWorkingFrequencyTest001, TestSize.Level1)558 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiHighWorkingFrequencyTest001, TestSize.Level1)
559 {
560     const std::string processName = "cast_engine_service";
561     std::vector<int8_t> paramVec;
562 
563     ASSERT_TRUE(g_component != nullptr);
564 
565     ProcessNameParam nameParam;
566     func_->InitExtParam(nameParam);
567     int32_t ret = strcpy_s(nameParam.processName, sizeof(nameParam.processName), processName.c_str());
568     ASSERT_TRUE(ret == EOK);
569     func_->ObjectToVector(nameParam, paramVec);
570     ret = g_component->SetParameter(OMX_IndexParamProcessName, paramVec);
571     ASSERT_TRUE(ret == HDF_SUCCESS);
572 
573     WorkingFrequencyParam freqParam;
574     std::vector<int8_t> inParam;
575     std::vector<int8_t> outParam;
576 
577     func_->InitExtParam(freqParam);
578     func_->ObjectToVector(freqParam, inParam);
579     ret = g_component->GetParameter(OMX_IndexParamWorkingFrequency, inParam, outParam);
580     ASSERT_TRUE(ret == HDF_SUCCESS);
581     func_->VectorToObject(outParam, freqParam);
582 
583     // 设置为最高档
584     freqParam.level = freqParam.level - 1;
585     func_->ObjectToVector(freqParam, inParam);
586     ret = g_component->SetParameter(OMX_IndexParamWorkingFrequency, inParam);
587     ASSERT_TRUE(ret == HDF_SUCCESS);
588 }
589 #endif
590 
591 /**
592 * @tc.number : SUB_Driver_Codec_idlomx_3100
593 * @tc.name   : HdfCodecHdiGetConfigTest001
594 * @tc.desc   : Verify Set parameters is outputindex  under the structure of OMX_VIDEO_CONFIG_BITRATETYPE.
595   @tc.type: FUNC
596 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetConfigTest001, TestSize.Level1)597 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetConfigTest001, TestSize.Level1)
598 {
599     ASSERT_TRUE(g_component != nullptr);
600     OMX_VIDEO_CONFIG_BITRATETYPE param;
601     func_->InitParam(param);
602     param.nPortIndex = outputIndex;
603 
604     std::vector<int8_t> inParam;
605     func_->ObjectToVector(param, inParam);
606     std::vector<int8_t> outParam;
607     auto ret = g_component->GetConfig(OMX_IndexConfigVideoBitrate, inParam, outParam);
608     ASSERT_EQ(ret, HDF_SUCCESS);
609 }
610 
611 /**
612 * @tc.number : SUB_Driver_Codec_idlomx_3200
613 * @tc.name   : HdfCodecHdiGetConfigTest002
614 * @tc.desc   : Verify Set parameters is inputIndex under the structure of OMX_VIDEO_CONFIG_BITRATETYPE.
615   @tc.type: FUNC
616 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetConfigTest002, TestSize.Level1)617 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetConfigTest002, TestSize.Level1)
618 {
619     ASSERT_TRUE(g_component != nullptr);
620     OMX_VIDEO_CONFIG_BITRATETYPE param;
621     func_->InitParam(param);
622     param.nPortIndex = inputIndex;
623 
624     std::vector<int8_t> inParam;
625     func_->ObjectToVector(param, inParam);
626     std::vector<int8_t> outParam;
627     auto ret = g_component->GetConfig(OMX_IndexConfigVideoBitrate, inParam, outParam);
628     ASSERT_NE(ret, HDF_SUCCESS);
629 }
630 
631 /**
632 * @tc.number : SUB_Driver_Codec_idlomx_3300
633 * @tc.name   : HdfCodecHdiGetConfigTest003
634 * @tc.desc   : Verify param not initialized and not set structure.
635   @tc.type: FUNC
636 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetConfigTest003, TestSize.Level1)637 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetConfigTest003, TestSize.Level1)
638 {
639     ASSERT_TRUE(g_component != nullptr);
640     std::vector<int8_t> inParam;
641     std::vector<int8_t> outParam;
642     auto ret = g_component->GetConfig(OMX_IndexConfigVideoBitrate, inParam, outParam);
643     ASSERT_NE(ret, HDF_SUCCESS);
644 }
645 
646 /**
647 * @tc.number : SUB_Driver_Codec_idlomx_3400
648 * @tc.name   : HdfCodecHdiGetConfigTest004
649 * @tc.desc   : Verify that the structure does not match the index.
650   @tc.type: FUNC
651 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetConfigTest004, TestSize.Level1)652 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiGetConfigTest004, TestSize.Level1)
653 {
654     ASSERT_TRUE(g_component != nullptr);
655     OMX_VIDEO_CONFIG_BITRATETYPE param;
656     func_->InitParam(param);
657     param.nPortIndex = outputIndex;
658 
659     std::vector<int8_t> inParam;
660     func_->ObjectToVector(param, inParam);
661     std::vector<int8_t> outParam;
662     auto ret = g_component->GetConfig(OMX_IndexVideoStartUnused, inParam, outParam);
663     ASSERT_NE(ret, HDF_SUCCESS);
664 }
665 
666 /**
667 * @tc.number : SUB_Driver_Codec_idlomx_3500
668 * @tc.name   : HdfCodecHdiSetConfigTest001
669 * @tc.desc   : Verify param is nPortIndex is outputIndex and nEncodeBitrate is FRAMERATE.
670   @tc.type: FUNC
671 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetConfigTest001, TestSize.Level1)672 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetConfigTest001, TestSize.Level1)
673 {
674     ASSERT_TRUE(g_component != nullptr);
675     OMX_VIDEO_CONFIG_BITRATETYPE param;
676     func_->InitParam(param);
677     param.nPortIndex = outputIndex;
678     param.nEncodeBitrate = FRAMERATE;
679 
680     std::vector<int8_t> inParam;
681     func_->ObjectToVector(param, inParam);
682     auto ret = g_component->SetConfig(OMX_IndexConfigVideoBitrate, inParam);
683     ASSERT_EQ(ret, HDF_SUCCESS);
684 }
685 
686 /**
687 * @tc.number : SUB_Driver_Codec_idlomx_3600
688 * @tc.name   : HdfCodecHdiSetConfigTest002
689 * @tc.desc   : Verify param is nPortIndex is inputIndex and nEncodeBitrate is FRAMERATE.
690   @tc.type: FUNC
691 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetConfigTest002, TestSize.Level1)692 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetConfigTest002, TestSize.Level1)
693 {
694     ASSERT_TRUE(g_component != nullptr);
695     OMX_VIDEO_CONFIG_BITRATETYPE param;
696     func_->InitParam(param);
697     param.nPortIndex = inputIndex;
698     param.nEncodeBitrate = FRAMERATE;
699 
700     std::vector<int8_t> inParam;
701     func_->ObjectToVector(param, inParam);
702     auto ret = g_component->SetConfig(OMX_IndexConfigVideoBitrate, inParam);
703     ASSERT_NE(ret, HDF_SUCCESS);
704 }
705 
706 /**
707 * @tc.number : SUB_Driver_Codec_idlomx_3700
708 * @tc.name   : HdfCodecHdiSetConfigTest003
709 * @tc.desc   : Verify param not initialized and not set structure.
710   @tc.type: FUNC
711 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetConfigTest003, TestSize.Level1)712 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetConfigTest003, TestSize.Level1)
713 {
714     ASSERT_TRUE(g_component != nullptr);
715     std::vector<int8_t> inParam;
716     auto ret = g_component->SetConfig(OMX_IndexConfigVideoBitrate, inParam);
717     ASSERT_NE(ret, HDF_SUCCESS);
718 }
719 
720 /**
721 * @tc.number : SUB_Driver_Codec_idlomx_3800
722 * @tc.name   : HdfCodecHdiSetConfigTest004
723 * @tc.desc   : Verify that the structure does not match the index.
724   @tc.type: FUNC
725 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetConfigTest004, TestSize.Level1)726 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetConfigTest004, TestSize.Level1)
727 {
728     ASSERT_TRUE(g_component != nullptr);
729     OMX_VIDEO_CONFIG_BITRATETYPE param;
730     func_->InitParam(param);
731     param.nPortIndex = outputIndex;
732 
733     std::vector<int8_t> inParam;
734     func_->ObjectToVector(param, inParam);
735     auto ret = g_component->SetConfig(OMX_IndexVideoStartUnused, inParam);
736     ASSERT_NE(ret, HDF_SUCCESS);
737 }
738 
739 /**
740 * @tc.number : SUB_Driver_Codec_idlomx_3900
741 * @tc.name   : HdfCodecHdiUseEglImageTest001
742 * @tc.desc   : Verify the function is UseEglImage whether or not supported.
743   @tc.type: FUNC
744 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseEglImageTest001, TestSize.Level1)745 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseEglImageTest001, TestSize.Level1)
746 {
747     ASSERT_TRUE(g_component != nullptr);
748     struct OmxCodecBuffer omxBuffer;
749     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
750     auto eglImage = std::make_unique<int8_t[]>(BUFFER_SIZE);
751     ASSERT_TRUE(eglImage != nullptr);
752     std::vector<int8_t> eglImageVec;
753     eglImageVec.assign(eglImage.get(), eglImage.get() + BUFFER_SIZE);
754     struct OmxCodecBuffer outbuffer;
755     int32_t ret = g_component->UseEglImage(inputIndex, omxBuffer, outbuffer, eglImageVec);
756     ASSERT_NE(ret, HDF_SUCCESS);
757     eglImage = nullptr;
758 }
759 
760 /**
761 * @tc.number : SUB_Driver_Codec_idlomx_4000
762 * @tc.name   : HdfCodecHdiFillThisBufferTest001
763 * @tc.desc   : Verify the function is FillThisBuffer when bufferId is BUFFER_ID_ERROR.
764   @tc.type: FUNC
765 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiFillThisBufferTest001, TestSize.Level1)766 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiFillThisBufferTest001, TestSize.Level1)
767 {
768     ASSERT_TRUE(g_component != nullptr);
769     struct OmxCodecBuffer omxBuffer;
770     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
771     omxBuffer.bufferId = BUFFER_ID_ERROR;
772     auto ret = g_component->FillThisBuffer(omxBuffer);
773     ASSERT_NE(ret, HDF_SUCCESS);
774 }
775 
776 /**
777 * @tc.number : SUB_Driver_Codec_idlomx_4100
778 * @tc.name   : HdfCodecHdiEmptyThisBufferTest001
779 * @tc.desc   : Verify the function is EmptyThisBuffer when bufferId is BUFFER_ID_ERROR.
780   @tc.type: FUNC
781 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiEmptyThisBufferTest001, TestSize.Level1)782 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiEmptyThisBufferTest001, TestSize.Level1)
783 {
784     ASSERT_TRUE(g_component != nullptr);
785     struct OmxCodecBuffer omxBuffer;
786     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
787     omxBuffer.bufferId = BUFFER_ID_ERROR;
788     auto ret = g_component->EmptyThisBuffer(omxBuffer);
789     ASSERT_NE(ret, HDF_SUCCESS);
790 }
791 
792 /**
793 * @tc.number : SUB_Driver_Codec_idlomx_4200
794 * @tc.name   : HdfCodecHdiSetCallbackTest001
795 * @tc.desc   : Verify the function is SetCallbacks when params is valid.
796   @tc.type: FUNC
797 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetCallbackTest001, TestSize.Level1)798 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetCallbackTest001, TestSize.Level1)
799 {
800     ASSERT_TRUE(g_component != nullptr);
801     g_callback = new CodecCallbackService();
802     ASSERT_TRUE(g_callback != nullptr);
803     auto ret = g_component->SetCallbacks(g_callback, APP_DATA);
804     ASSERT_EQ(ret, HDF_SUCCESS);
805 }
806 
807 /**
808 * @tc.number : SUB_Driver_Codec_idlomx_4300
809 * @tc.name   : HdfCodecHdiSetCallbackTest002
810 * @tc.desc   : Verify the function is SetCallbacks when params is invalid.
811   @tc.type: FUNC
812 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetCallbackTest002, TestSize.Level1)813 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiSetCallbackTest002, TestSize.Level1)
814 {
815     ASSERT_TRUE(g_component != nullptr);
816     auto ret = g_component->SetCallbacks(nullptr, APP_DATA);
817     ASSERT_NE(ret, HDF_SUCCESS);
818 }
819 
820 /**
821 * @tc.number : SUB_Driver_Codec_idlomx_4400
822 * @tc.name   : HdfCodecHdiUseBufferTest001
823 * @tc.desc   : Verify the function is UseBuffer when portindex is inputIndex.
824   @tc.type: FUNC
825 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferTest001, TestSize.Level1)826 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferTest001, TestSize.Level1)
827 {
828     ASSERT_TRUE(g_component != nullptr);
829     struct OmxCodecBuffer omxBuffer;
830     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_INVALID);
831     struct OmxCodecBuffer outBuffer;
832     auto ret = g_component->UseBuffer(inputIndex, omxBuffer, outBuffer);
833     ASSERT_NE(ret, HDF_SUCCESS);
834 }
835 
836 /**
837 * @tc.number : SUB_Driver_Codec_idlomx_4500
838 * @tc.name   : HdfCodecHdiUseBufferTest002
839 * @tc.desc   : Verify the function is UseBuffer when portindex is outputIndex.
840   @tc.type: FUNC
841 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferTest002, TestSize.Level1)842 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferTest002, TestSize.Level1)
843 {
844     ASSERT_TRUE(g_component != nullptr);
845     struct OmxCodecBuffer omxBuffer;
846     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_INVALID);
847     struct OmxCodecBuffer outBuffer;
848     auto ret = g_component->UseBuffer(outputIndex, omxBuffer, outBuffer);
849     ASSERT_NE(ret, HDF_SUCCESS);
850 }
851 
852 /**
853 * @tc.number : SUB_Driver_Codec_idlomx_4600
854 * @tc.name   : HdfCodecHdiUseBufferTest003
855 * @tc.desc   : Verify the function is UseBuffer when portindex is inputIndex
856                and omxBuffer is CODEC_BUFFER_TYPE_VIRTUAL_ADDR.
857   @tc.type: FUNC
858 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferTest003, TestSize.Level1)859 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferTest003, TestSize.Level1)
860 {
861     ASSERT_TRUE(g_component != nullptr);
862     struct OmxCodecBuffer omxBuffer;
863     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_VIRTUAL_ADDR);
864     struct OmxCodecBuffer outBuffer;
865     auto ret = g_component->UseBuffer(inputIndex, omxBuffer, outBuffer);
866     ASSERT_NE(ret, HDF_SUCCESS);
867 }
868 
869 /**
870 * @tc.number : SUB_Driver_Codec_idlomx_4700
871 * @tc.name   : HdfCodecHdiUseBufferTest003
872 * @tc.desc   : Verify the function is UseBuffer when portindex is outputIndex
873                and omxBuffer is CODEC_BUFFER_TYPE_VIRTUAL_ADDR.
874   @tc.type: FUNC
875 */
HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferTest004, TestSize.Level1)876 HWTEST_F(CodecHdiOmxEncTest, HdfCodecHdiUseBufferTest004, TestSize.Level1)
877 {
878     ASSERT_TRUE(g_component != nullptr);
879     struct OmxCodecBuffer omxBuffer;
880     func_->InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_VIRTUAL_ADDR);
881     struct OmxCodecBuffer outBuffer;
882     auto ret = g_component->UseBuffer(outputIndex, omxBuffer, outBuffer);
883     ASSERT_NE(ret, HDF_SUCCESS);
884 }
885 }  // namespace
886