1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <iostream>
16 #include <cstdio>
17 #include <string>
18 
19 #include "gtest/gtest.h"
20 #include "native_avcodec_base.h"
21 #include "native_avformat.h"
22 #include "native_avcodec_videoencoder.h"
23 #include "videoenc_sample.h"
24 #include "native_avcapability.h"
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::Media;
28 using namespace testing::ext;
29 namespace {
30 OH_AVCodec *venc_ = NULL;
31 constexpr uint32_t DEFAULT_WIDTH = 1920;
32 constexpr uint32_t DEFAULT_HEIGHT = 1080;
33 constexpr uint32_t CODEC_NAME_SIZE = 128;
34 char g_codecName[CODEC_NAME_SIZE] = {};
35 OH_AVCapability *cap = nullptr;
36 OHOS::Media::VEncSignal *signal_ = nullptr;
37 OH_AVFormat *format;
onError(OH_AVCodec * codec,int32_t errorCode,void * userData)38 void onError(OH_AVCodec *codec, int32_t errorCode, void *userData)
39 {
40     cout << "Error errorCode=" << errorCode << endl;
41 };
42 
onStreamChanged(OH_AVCodec * codec,OH_AVFormat * fmt,void * userData)43 void onStreamChanged(OH_AVCodec *codec, OH_AVFormat *fmt, void *userData)
44 {
45     cout << "stream Changed" << endl;
46 };
47 
onNeedInputData(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,void * userData)48 void onNeedInputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)
49 {
50     VEncSignal *signal = static_cast<VEncSignal *>(userData);
51     if (signal == nullptr) {
52         return;
53     }
54     unique_lock<mutex> lock(signal->inMutex_);
55     signal->inIdxQueue_.push(index);
56     signal->inBufferQueue_.push(data);
57     signal->inCond_.notify_all();
58     cout << "need input data" << endl;
59 };
60 
onNewOutputData(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,OH_AVCodecBufferAttr * attr,void * userData)61 void onNewOutputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr, void *userData)
62 {
63     cout << "output data" << endl;
64     VEncSignal *signal = static_cast<VEncSignal *>(userData);
65     if (signal == nullptr) {
66         return;
67     }
68     unique_lock<mutex> lock(signal->outMutex_);
69     signal->outIdxQueue_.push(index);
70     signal->attrQueue_.push(*attr);
71     signal->outBufferQueue_.push(data);
72     signal->outCond_.notify_all();
73 };
74 } // namespace
75 
76 namespace OHOS {
77 namespace Media {
78 class HwEncApiNdkTest : public testing::Test {
79 public:
80     // SetUpTestCase: Called before all test cases
81     static void SetUpTestCase(void);
82     // TearDownTestCase: Called after all test case
83     static void TearDownTestCase(void);
84     // SetUp: Called before each test cases
85     void SetUp(void);
86     // TearDown: Called after each test cases
87     void TearDown(void);
88 };
89 
SetUpTestCase()90 void HwEncApiNdkTest::SetUpTestCase()
91 {
92     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
93     const char *TMP_CODEC_NAME = OH_AVCapability_GetName(cap);
94     if (memcpy_s(g_codecName, sizeof(g_codecName), TMP_CODEC_NAME, strlen(TMP_CODEC_NAME)) != 0) {
95         cout << "memcpy failed" << endl;
96     }
97     cout << "codecname: " << g_codecName << endl;
98 }
TearDownTestCase()99 void HwEncApiNdkTest::TearDownTestCase() {}
SetUp()100 void HwEncApiNdkTest::SetUp()
101 {
102     signal_ = new VEncSignal();
103 }
TearDown()104 void HwEncApiNdkTest::TearDown()
105 {
106     if (format != nullptr) {
107         OH_AVFormat_Destroy(format);
108         format = nullptr;
109     }
110     if (venc_ != NULL) {
111         OH_VideoEncoder_Destroy(venc_);
112         venc_ = nullptr;
113     }
114     if (signal_) {
115         delete signal_;
116         signal_ = nullptr;
117     }
118 }
119 } // namespace Media
120 } // namespace OHOS
121 
122 namespace {
123 /**
124  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0100
125  * @tc.name      : OH_VideoEncoder_CreateByMime para1 error
126  * @tc.desc      : api test
127  */
128 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0100, TestSize.Level2)
129 {
130     venc_ = OH_VideoEncoder_CreateByMime(nullptr);
131     ASSERT_EQ(nullptr, venc_);
132 }
133 
134 /**
135  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0200
136  * @tc.name      : OH_VideoEncoder_CreateByMime para2 error
137  * @tc.desc      : api test
138  */
139 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0200, TestSize.Level2)
140 {
141     venc_ = OH_VideoEncoder_CreateByMime("");
142     ASSERT_EQ(nullptr, venc_);
143 }
144 
145 /**
146  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0300
147  * @tc.name      : OH_VideoEncoder_CreateByMime para error
148  * @tc.desc      : api test
149  */
150 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0300, TestSize.Level2)
151 {
152     venc_ = OH_VideoEncoder_CreateByName(nullptr);
153     ASSERT_EQ(nullptr, venc_);
154 }
155 
156 /**
157  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0400
158  * @tc.name      : OH_VideoEncoder_CreateByMime para error
159  * @tc.desc      : api test
160  */
161 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0400, TestSize.Level2)
162 {
163     venc_ = OH_VideoEncoder_CreateByName("");
164     ASSERT_EQ(nullptr, venc_);
165 }
166 
167 /**
168  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0500
169  * @tc.name      : OH_VideoEncoder_CreateByMime para error
170  * @tc.desc      : api test
171  */
172 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0500, TestSize.Level2)
173 {
174     OH_AVErrCode ret = AV_ERR_OK;
175     ret = OH_VideoEncoder_Destroy(nullptr);
176     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
177 }
178 
179 /**
180  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0600
181  * @tc.name      : OH_VideoEncoder_SetCallback para error
182  * @tc.desc      : api test
183  */
184 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0600, TestSize.Level2)
185 {
186     OH_AVCodecAsyncCallback cb_;
187     cb_.onError = onError;
188     cb_.onStreamChanged = onStreamChanged;
189     cb_.onNeedInputData = onNeedInputData;
190     cb_.onNeedOutputData = onNewOutputData;
191 
192     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_SetCallback(NULL, cb_, static_cast<void *>(signal_)));
193 }
194 
195 /**
196  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0700
197  * @tc.name      : OH_VideoEncoder_SetCallback para error
198  * @tc.desc      : api test
199  */
200 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0700, TestSize.Level2)
201 {
202     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
203     ASSERT_NE(NULL, venc_);
204 
205     OH_AVCodecAsyncCallback cb2_;
206     cb2_.onError = NULL;
207     cb2_.onStreamChanged = NULL;
208     cb2_.onNeedInputData = NULL;
209     cb2_.onNeedOutputData = NULL;
210     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb2_, static_cast<void *>(signal_)));
211 }
212 
213 /**
214  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0800
215  * @tc.name      : OH_VideoEncoder_SetCallback para error
216  * @tc.desc      : api test
217  */
218 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0800, TestSize.Level2)
219 {
220     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
221     OH_AVCodecAsyncCallback cb_;
222     cb_.onError = onError;
223     cb_.onStreamChanged = onStreamChanged;
224     cb_.onNeedInputData = onNeedInputData;
225     cb_.onNeedOutputData = onNewOutputData;
226     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
227 }
228 
229 /**
230  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0900
231  * @tc.name      : OH_VideoEncoder_Configure para error
232  * @tc.desc      : api test
233  */
234 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0900, TestSize.Level2)
235 {
236     OH_AVErrCode ret = AV_ERR_OK;
237     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
238     ASSERT_NE(nullptr, venc_);
239     ret = OH_VideoEncoder_Configure(venc_, nullptr);
240     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
241 }
242 
243 /**
244  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1100
245  * @tc.name      : OH_VideoEncoder_Configure para not enough
246  * @tc.desc      : api test
247  */
248 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1100, TestSize.Level2)
249 {
250     OH_AVErrCode ret = AV_ERR_OK;
251     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
252     ASSERT_NE(nullptr, venc_);
253     format = OH_AVFormat_Create();
254     ASSERT_NE(nullptr, format);
255     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, 100000);
256     ret = OH_VideoEncoder_Configure(venc_, format);
257     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
258 }
259 
260 /**
261  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1400
262  * @tc.name      : OH_VideoEncoder_Start para error
263  * @tc.desc      : api test
264  */
265 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1400, TestSize.Level2)
266 {
267     OH_AVErrCode ret = AV_ERR_OK;
268     ret = OH_VideoEncoder_Start(nullptr);
269     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
270 }
271 
272 /**
273  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1500
274  * @tc.name      : OH_VideoEncoder_Stop para error
275  * @tc.desc      : api test
276  */
277 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1500, TestSize.Level2)
278 {
279     OH_AVErrCode ret = AV_ERR_OK;
280     ret = OH_VideoEncoder_Stop(nullptr);
281     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
282 }
283 
284 /**
285  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1600
286  * @tc.name      : OH_VideoEncoder_Flush para error
287  * @tc.desc      : api test
288  */
289 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1600, TestSize.Level2)
290 {
291     OH_AVErrCode ret = AV_ERR_OK;
292     ret = OH_VideoEncoder_Flush(nullptr);
293     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
294 }
295 
296 /**
297  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1700
298  * @tc.name      : OH_VideoEncoder_Reset para error
299  * @tc.desc      : api test
300  */
301 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1700, TestSize.Level2)
302 {
303     OH_AVErrCode ret = AV_ERR_OK;
304     ret = OH_VideoEncoder_Reset(nullptr);
305     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
306 }
307 
308 /**
309  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1800
310  * @tc.name      : OH_VideoEncoder_Reset para error
311  * @tc.desc      : api test
312  */
313 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1800, TestSize.Level2)
314 {
315     format = OH_VideoEncoder_GetOutputDescription(nullptr);
316     ASSERT_EQ(format, nullptr);
317 }
318 
319 /**
320  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1900
321  * @tc.name      : OH_VideoEncoder_SetParameter para error
322  * @tc.desc      : api test
323  */
324 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1900, TestSize.Level2)
325 {
326     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
327     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_SetParameter(venc_, nullptr));
328 }
329 
330 /**
331  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2000
332  * @tc.name      : OH_VideoEncoder_SetParameter para error
333  * @tc.desc      : api test
334  */
335 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2000, TestSize.Level2)
336 {
337     format = OH_AVFormat_Create();
338     ASSERT_NE(NULL, format);
339 
340     string widthStr = "width";
341     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
342     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_SetParameter(NULL, format));
343 }
344 
345 /**
346  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2100
347  * @tc.name      : OH_VideoEncoder_GetSurface para error
348  * @tc.desc      : api test
349  */
350 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2100, TestSize.Level2)
351 {
352     OH_AVErrCode ret = AV_ERR_OK;
353     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
354     ASSERT_NE(nullptr, venc_);
355     ret = OH_VideoEncoder_GetSurface(venc_, nullptr);
356     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
357 }
358 
359 /**
360  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2200
361  * @tc.name      : OH_VideoEncoder_FreeOutputData para error
362  * @tc.desc      : api test
363  */
364 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2200, TestSize.Level2)
365 {
366     OH_AVErrCode ret = AV_ERR_OK;
367     ret = OH_VideoEncoder_FreeOutputData(nullptr, 0);
368     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
369 }
370 
371 /**
372  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2300
373  * @tc.name      : OH_VideoEncoder_FreeOutputData para error
374  * @tc.desc      : api test
375  */
376 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2300, TestSize.Level2)
377 {
378     OH_AVErrCode ret = AV_ERR_OK;
379     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
380     ASSERT_NE(nullptr, venc_);
381     format = OH_AVFormat_Create();
382     ASSERT_NE(nullptr, format);
383     OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
384     OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_HEIGHT);
385     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
386 
387     ret = OH_VideoEncoder_Configure(venc_, format);
388     ASSERT_EQ(ret, AV_ERR_OK);
389     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
390     usleep(1000000);
391     ret = OH_VideoEncoder_FreeOutputData(venc_, 9999999);
392     ASSERT_EQ(ret, AV_ERR_INVALID_STATE);
393 }
394 
395 /**
396  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2400
397  * @tc.name      : OH_VideoEncoder_NotifyEndOfStream para error
398  * @tc.desc      : api test
399  */
400 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2400, TestSize.Level2)
401 {
402     OH_AVErrCode ret = AV_ERR_OK;
403     ret = OH_VideoEncoder_NotifyEndOfStream(nullptr);
404     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
405 }
406 /**
407  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2500
408  * @tc.name      : OH_VideoEncoder_NotifyEndOfStream para error
409  * @tc.desc      : api test
410  */
411 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2500, TestSize.Level2)
412 {
413     OH_AVErrCode ret = AV_ERR_OK;
414     ret = OH_VideoEncoder_NotifyEndOfStream(nullptr);
415     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
416 }
417 
418 /**
419  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2600
420  * @tc.name      : OH_VideoEncoder_PushInputData para error
421  * @tc.desc      : api test
422  */
423 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2600, TestSize.Level2)
424 {
425     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
426     ASSERT_NE(nullptr, venc_);
427     OH_AVCodecBufferAttr attr;
428     attr.pts = -1;
429     attr.size = -1;
430     attr.offset = 0;
431     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
432 
433     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(venc_, 0, attr));
434 }
435 
436 /**
437  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2700
438  * @tc.name      : OH_VideoEncoder_PushInputData para error
439  * @tc.desc      : api test
440  */
441 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2700, TestSize.Level2)
442 {
443     OH_AVCodecBufferAttr attr;
444     attr.pts = 0;
445     attr.size = 0;
446     attr.offset = 0;
447     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
448     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(NULL, 0, attr));
449 }
450 
451 /**
452  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2800
453  * @tc.name      : OH_VideoEncoder_PushInputData para error
454  * @tc.desc      : api test
455  */
456 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2800, TestSize.Level2)
457 {
458     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
459     ASSERT_NE(nullptr, venc_);
460     OH_AVCodecBufferAttr attr;
461     attr.pts = 0;
462     attr.size = 0;
463     attr.offset = 0;
464     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
465     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(NULL, 99999, attr));
466 }
467 
468 /**
469  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2900
470  * @tc.name      : OH_VideoEncoder_GetInputDescription para error
471  * * @tc.desc      : api test
472  */
473 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2900, TestSize.Level2)
474 {
475     ASSERT_EQ(nullptr, OH_VideoEncoder_GetInputDescription(nullptr));
476 }
477 
478 /**
479  * @tc.number    : VIDEO_ENCODE_API_0100
480  * @tc.name      : create create
481  * @tc.desc      : function test
482  */
483 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0100, TestSize.Level2)
484 {
485     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
486     ASSERT_NE(venc_, NULL);
487     OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
488     ASSERT_NE(venc_2, NULL);
489     OH_VideoEncoder_Destroy(venc_2);
490     venc_2 = nullptr;
491 }
492 
493 /**
494  * @tc.number    : VIDEO_ENCODE_API_3100
495  * @tc.name      : create create
496  * @tc.desc      : function test
497  */
498 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_3100, TestSize.Level2)
499 {
500     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
501     ASSERT_NE(venc_, NULL);
502     OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByName(g_codecName);
503     ASSERT_NE(venc_2, NULL);
504     OH_VideoEncoder_Destroy(venc_2);
505     venc_2 = nullptr;
506 }
507 
508 /**
509  * @tc.number    : VIDEO_ENCODE_API_0200
510  * @tc.name      : create configure configure
511  * @tc.desc      : function test
512  */
513 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0200, TestSize.Level2)
514 {
515     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
516     ASSERT_NE(NULL, venc_);
517 
518     format = OH_AVFormat_Create();
519     ASSERT_NE(NULL, format);
520 
521     string widthStr = "width";
522     string heightStr = "height";
523     string frameRateStr = "frame_rate";
524     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
525     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), DEFAULT_HEIGHT);
526     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
527 
528     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
529     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_Configure(venc_, format));
530 }
531 
532 /**
533  * @tc.number    : VIDEO_ENCODE_API_0300
534  * @tc.name      : create configure start start
535  * @tc.desc      : function test
536  */
537 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0300, TestSize.Level2)
538 {
539     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
540     ASSERT_NE(NULL, venc_);
541 
542     format = OH_AVFormat_Create();
543     ASSERT_NE(NULL, format);
544 
545     string widthStr = "width";
546     string heightStr = "height";
547     string frameRateStr = "frame_rate";
548     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
549     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), DEFAULT_HEIGHT);
550     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
551     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
552     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
553     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_Start(venc_));
554 }
555 
556 /**
557  * @tc.number    : VIDEO_ENCODE_API_0400
558  * @tc.name      : create configure start stop stop
559  * @tc.desc      : function test
560  */
561 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0400, TestSize.Level2)
562 {
563     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
564     ASSERT_NE(NULL, venc_);
565 
566     format = OH_AVFormat_Create();
567     ASSERT_NE(NULL, format);
568 
569     string widthStr = "width";
570     string heightStr = "height";
571     string frameRateStr = "frame_rate";
572     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
573     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), DEFAULT_HEIGHT);
574     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
575 
576     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
577     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
578     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
579     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
580 }
581 
582 /**
583  * @tc.number    : VIDEO_ENCODE_API_0500
584  * @tc.name      : create configure start stop reset reset
585  * @tc.desc      : function test
586  */
587 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0500, TestSize.Level2)
588 {
589     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
590     ASSERT_NE(NULL, venc_);
591 
592     format = OH_AVFormat_Create();
593     ASSERT_NE(NULL, format);
594 
595     string widthStr = "width";
596     string heightStr = "height";
597     string frameRateStr = "frame_rate";
598     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
599     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), DEFAULT_HEIGHT);
600     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
601 
602     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
603     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
604     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
605     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Reset(venc_));
606     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Reset(venc_));
607 }
608 
609 /**
610  * @tc.number    : VIDEO_ENCODE_API_0600
611  * @tc.name      : create configure start EOS EOS
612  * @tc.desc      : function test
613  */
614 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0600, TestSize.Level2)
615 {
616     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
617     ASSERT_NE(NULL, venc_);
618     format = OH_AVFormat_Create();
619     ASSERT_NE(NULL, format);
620     string widthStr = "width";
621     string heightStr = "height";
622     string frameRateStr = "frame_rate";
623     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
624     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), DEFAULT_HEIGHT);
625     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
626     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
627     OH_AVCodecAsyncCallback cb_;
628     cb_.onError = onError;
629     cb_.onStreamChanged = onStreamChanged;
630     cb_.onNeedInputData = onNeedInputData;
631     cb_.onNeedOutputData = onNewOutputData;
632     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, static_cast<void *>(signal_)));
633 
634     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
635     unique_lock<mutex> lock(signal_->inMutex_);
__anon3782789b0302null636     signal_->inCond_.wait(lock, [] { return signal_->inIdxQueue_.size() > 1; });
637     uint32_t index = signal_->inIdxQueue_.front();
638     OH_AVCodecBufferAttr attr;
639     attr.pts = 0;
640     attr.size = 0;
641     attr.offset = 0;
642     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
643 
644     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_PushInputData(venc_, index, attr));
645     signal_->inIdxQueue_.pop();
646     index = signal_->inIdxQueue_.front();
647     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_PushInputData(venc_, index, attr));
648     signal_->inIdxQueue_.pop();
649 }
650 
651 /**
652  * @tc.number    : VIDEO_ENCODE_API_0700
653  * @tc.name      : create configure start flush flush
654  * @tc.desc      : function test
655  */
656 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0700, TestSize.Level2)
657 {
658     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
659     ASSERT_NE(NULL, venc_);
660     format = OH_AVFormat_Create();
661     ASSERT_NE(NULL, format);
662     string widthStr = "width";
663     string heightStr = "height";
664     string frameRateStr = "frame_rate";
665     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
666     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), DEFAULT_HEIGHT);
667     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
668     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
669     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
670     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Flush(venc_));
671     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Flush(venc_));
672 }
673 
674 /**
675  * @tc.number    : VIDEO_ENCODE_API_0800
676  * @tc.name      : create configure start stop release release
677  * @tc.desc      : function test
678  */
679 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0800, TestSize.Level2)
680 {
681     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
682     ASSERT_NE(NULL, venc_);
683 
684     format = OH_AVFormat_Create();
685     ASSERT_NE(NULL, format);
686 
687     string widthStr = "width";
688     string heightStr = "height";
689     string frameRateStr = "frame_rate";
690     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
691     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), DEFAULT_HEIGHT);
692     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
693 
694     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
695     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
696     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
697     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Destroy(venc_));
698     venc_ = nullptr;
699     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Destroy(venc_));
700 }
701 
702 /**
703  * @tc.number    : VIDEO_ENCODE_API_0900
704  * @tc.name      : create create
705  * @tc.desc      : function test
706  */
707 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0900, TestSize.Level2)
708 {
709     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
710     ASSERT_NE(venc_, NULL);
711     OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
712     ASSERT_NE(venc_2, NULL);
713     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Destroy(venc_2));
714     venc_2 = nullptr;
715 }
716 
717 /**
718  * @tc.number    : VIDEO_ENCODE_API_1000
719  * @tc.name      : repeat OH_VideoEncoder_SetCallback
720  * @tc.desc      : function test
721  */
722 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1000, TestSize.Level2)
723 {
724     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
725     ASSERT_NE(venc_, NULL);
726     OH_AVCodecAsyncCallback cb_;
727     cb_.onError = onError;
728     cb_.onStreamChanged = onStreamChanged;
729     cb_.onNeedInputData = onNeedInputData;
730     cb_.onNeedOutputData = onNewOutputData;
731     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
732     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
733 }
734 
735 /**
736  * @tc.number    : VIDEO_ENCODE_API_1100
737  * @tc.name      : repeat OH_VideoEncoder_GetOutputDescription
738  * @tc.desc      : function test
739  */
740 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1100, TestSize.Level2)
741 {
742     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
743     ASSERT_NE(venc_, NULL);
744     format = OH_VideoEncoder_GetOutputDescription(venc_);
745     ASSERT_NE(NULL, format);
746     OH_AVFormat_Destroy(format);
747     format = OH_VideoEncoder_GetOutputDescription(venc_);
748     ASSERT_NE(NULL, format);
749 }
750 
751 /**
752  * @tc.number    : VIDEO_ENCODE_API_1200
753  * @tc.name      : repeat OH_VideoEncoder_SetParameter
754  * @tc.desc      : function test
755  */
756 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1200, TestSize.Level2)
757 {
758     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
759     ASSERT_NE(NULL, venc_);
760 
761     format = OH_AVFormat_Create();
762     ASSERT_NE(NULL, format);
763 
764     string widthStr = "width";
765     string heightStr = "height";
766     string frameRateStr = "frame_rate";
767     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), DEFAULT_WIDTH);
768     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), DEFAULT_HEIGHT);
769     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
770 
771     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_SetParameter(venc_, format));
772     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_SetParameter(venc_, format));
773 }
774 
775 /**
776  * @tc.number    : VIDEO_ENCODE_API_1200
777  * @tc.name      : repeat OH_VideoEncoder_GetInputDescription
778  * @tc.desc      : function test
779  */
780 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1300, TestSize.Level2)
781 {
782     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
783     ASSERT_NE(NULL, venc_);
784     format = OH_VideoEncoder_GetInputDescription(venc_);
785     ASSERT_NE(NULL, format);
786     OH_AVFormat_Destroy(format);
787     format = OH_VideoEncoder_GetInputDescription(venc_);
788     ASSERT_NE(NULL, format);
789 }
790 
791 /**
792  * @tc.number    : VIDEO_ENCODE_API_1400
793  * @tc.name      : set quality with illegal value
794  * @tc.desc      : function test
795  */
796 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1400, TestSize.Level2)
797 {
798     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
799     ASSERT_NE(nullptr, venc_);
800     format = OH_AVFormat_Create();
801     ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, CQ));
802     ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, 101));
803     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
804     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
805     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
806     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Configure(venc_, format));
807 }
808 
809 /**
810  * @tc.number    : VIDEO_ENCODE_API_1410
811  * @tc.name      : set quality with illegal value
812  * @tc.desc      : function test
813  */
814 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1410, TestSize.Level2)
815 {
816     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
817     ASSERT_NE(nullptr, venc_);
818     format = OH_AVFormat_Create();
819     ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, CQ));
820     ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, -1));
821     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
822     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
823     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
824     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Configure(venc_, format));
825 }
826 } // namespace