• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
203     if (cap) {
204         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
205         ASSERT_NE(NULL, venc_);
206 
207         OH_AVCodecAsyncCallback cb2_;
208         cb2_.onError = NULL;
209         cb2_.onStreamChanged = NULL;
210         cb2_.onNeedInputData = NULL;
211         cb2_.onNeedOutputData = NULL;
212         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb2_, static_cast<void *>(signal_)));
213     } else {
214         return;
215     }
216 }
217 
218 /**
219  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0800
220  * @tc.name      : OH_VideoEncoder_SetCallback para error
221  * @tc.desc      : api test
222  */
223 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0800, TestSize.Level2)
224 {
225     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
226     if (cap) {
227         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
228         OH_AVCodecAsyncCallback cb_;
229         cb_.onError = onError;
230         cb_.onStreamChanged = onStreamChanged;
231         cb_.onNeedInputData = onNeedInputData;
232         cb_.onNeedOutputData = onNewOutputData;
233         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
234     } else {
235         return;
236     }
237 }
238 
239 /**
240  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0900
241  * @tc.name      : OH_VideoEncoder_Configure para error
242  * @tc.desc      : api test
243  */
244 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0900, TestSize.Level2)
245 {
246     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
247     if (cap) {
248         OH_AVErrCode ret = AV_ERR_OK;
249         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
250         ASSERT_NE(nullptr, venc_);
251         ret = OH_VideoEncoder_Configure(venc_, nullptr);
252         ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
253     } else {
254         return;
255     }
256 }
257 
258 /**
259  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1100
260  * @tc.name      : OH_VideoEncoder_Configure para not enough
261  * @tc.desc      : api test
262  */
263 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1100, TestSize.Level2)
264 {
265     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
266     if (cap) {
267         OH_AVErrCode ret = AV_ERR_OK;
268         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
269         ASSERT_NE(nullptr, venc_);
270         format = OH_AVFormat_Create();
271         ASSERT_NE(nullptr, format);
272         OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, 100000);
273         ret = OH_VideoEncoder_Configure(venc_, format);
274         ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
275     } else {
276         return;
277     }
278 }
279 
280 /**
281  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1400
282  * @tc.name      : OH_VideoEncoder_Start para error
283  * @tc.desc      : api test
284  */
285 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1400, TestSize.Level2)
286 {
287     OH_AVErrCode ret = AV_ERR_OK;
288     ret = OH_VideoEncoder_Start(nullptr);
289     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
290 }
291 
292 /**
293  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1500
294  * @tc.name      : OH_VideoEncoder_Stop para error
295  * @tc.desc      : api test
296  */
297 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1500, TestSize.Level2)
298 {
299     OH_AVErrCode ret = AV_ERR_OK;
300     ret = OH_VideoEncoder_Stop(nullptr);
301     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
302 }
303 
304 /**
305  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1600
306  * @tc.name      : OH_VideoEncoder_Flush para error
307  * @tc.desc      : api test
308  */
309 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1600, TestSize.Level2)
310 {
311     OH_AVErrCode ret = AV_ERR_OK;
312     ret = OH_VideoEncoder_Flush(nullptr);
313     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
314 }
315 
316 /**
317  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1700
318  * @tc.name      : OH_VideoEncoder_Reset para error
319  * @tc.desc      : api test
320  */
321 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1700, TestSize.Level2)
322 {
323     OH_AVErrCode ret = AV_ERR_OK;
324     ret = OH_VideoEncoder_Reset(nullptr);
325     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
326 }
327 
328 /**
329  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1800
330  * @tc.name      : OH_VideoEncoder_Reset para error
331  * @tc.desc      : api test
332  */
333 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1800, TestSize.Level2)
334 {
335     format = OH_VideoEncoder_GetOutputDescription(nullptr);
336     ASSERT_EQ(format, nullptr);
337 }
338 
339 /**
340  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1900
341  * @tc.name      : OH_VideoEncoder_SetParameter para error
342  * @tc.desc      : api test
343  */
344 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1900, TestSize.Level2)
345 {
346     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
347     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_SetParameter(venc_, nullptr));
348 }
349 
350 /**
351  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2000
352  * @tc.name      : OH_VideoEncoder_SetParameter para error
353  * @tc.desc      : api test
354  */
355 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2000, TestSize.Level2)
356 {
357     format = OH_AVFormat_Create();
358     ASSERT_NE(NULL, format);
359     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
360     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_SetParameter(NULL, format));
361 }
362 
363 /**
364  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2100
365  * @tc.name      : OH_VideoEncoder_GetSurface para error
366  * @tc.desc      : api test
367  */
368 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2100, TestSize.Level2)
369 {
370     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
371     if (cap) {
372         OH_AVErrCode ret = AV_ERR_OK;
373         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
374         ASSERT_NE(nullptr, venc_);
375         ret = OH_VideoEncoder_GetSurface(venc_, nullptr);
376         ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
377     } else {
378         return;
379     }
380 }
381 
382 /**
383  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2200
384  * @tc.name      : OH_VideoEncoder_FreeOutputData para error
385  * @tc.desc      : api test
386  */
387 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2200, TestSize.Level2)
388 {
389     OH_AVErrCode ret = AV_ERR_OK;
390     ret = OH_VideoEncoder_FreeOutputData(nullptr, 0);
391     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
392 }
393 
394 /**
395  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2300
396  * @tc.name      : OH_VideoEncoder_FreeOutputData para error
397  * @tc.desc      : api test
398  */
399 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2300, TestSize.Level2)
400 {
401     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
402     if (cap) {
403         OH_AVErrCode ret = AV_ERR_OK;
404         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
405         ASSERT_NE(nullptr, venc_);
406         format = OH_AVFormat_Create();
407         ASSERT_NE(nullptr, format);
408         OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
409         OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_HEIGHT);
410         OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
411 
412         ret = OH_VideoEncoder_Configure(venc_, format);
413         ASSERT_EQ(ret, AV_ERR_OK);
414         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
415         usleep(1000000);
416         ret = OH_VideoEncoder_FreeOutputData(venc_, 9999999);
417         ASSERT_EQ(ret, AV_ERR_INVALID_STATE);
418     } else {
419         return;
420     }
421 }
422 
423 /**
424  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2400
425  * @tc.name      : OH_VideoEncoder_NotifyEndOfStream para error
426  * @tc.desc      : api test
427  */
428 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2400, TestSize.Level2)
429 {
430     OH_AVErrCode ret = AV_ERR_OK;
431     ret = OH_VideoEncoder_NotifyEndOfStream(nullptr);
432     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
433 }
434 /**
435  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2500
436  * @tc.name      : OH_VideoEncoder_NotifyEndOfStream para error
437  * @tc.desc      : api test
438  */
439 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2500, TestSize.Level2)
440 {
441     OH_AVErrCode ret = AV_ERR_OK;
442     ret = OH_VideoEncoder_NotifyEndOfStream(nullptr);
443     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
444 }
445 
446 /**
447  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2600
448  * @tc.name      : OH_VideoEncoder_PushInputData para error
449  * @tc.desc      : api test
450  */
451 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2600, TestSize.Level2)
452 {
453     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
454     if (cap) {
455         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
456         ASSERT_NE(nullptr, venc_);
457         OH_AVCodecBufferAttr attr;
458         attr.pts = -1;
459         attr.size = -1;
460         attr.offset = 0;
461         attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
462 
463         ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(venc_, 0, attr));
464     } else {
465         return;
466     }
467 }
468 
469 /**
470  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2700
471  * @tc.name      : OH_VideoEncoder_PushInputData para error
472  * @tc.desc      : api test
473  */
474 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2700, TestSize.Level2)
475 {
476     OH_AVCodecBufferAttr attr;
477     attr.pts = 0;
478     attr.size = 0;
479     attr.offset = 0;
480     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
481     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(NULL, 0, attr));
482 }
483 
484 /**
485  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2800
486  * @tc.name      : OH_VideoEncoder_PushInputData para error
487  * @tc.desc      : api test
488  */
489 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2800, TestSize.Level2)
490 {
491     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
492     if (cap) {
493         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
494         ASSERT_NE(nullptr, venc_);
495         OH_AVCodecBufferAttr attr;
496         attr.pts = 0;
497         attr.size = 0;
498         attr.offset = 0;
499         attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
500         ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(NULL, 99999, attr));
501     } else {
502         return;
503     }
504 }
505 
506 /**
507  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2900
508  * @tc.name      : OH_VideoEncoder_GetInputDescription para error
509  * * @tc.desc      : api test
510  */
511 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2900, TestSize.Level2)
512 {
513     ASSERT_EQ(nullptr, OH_VideoEncoder_GetInputDescription(nullptr));
514 }
515 
516 /**
517  * @tc.number    : VIDEO_ENCODE_API_0100
518  * @tc.name      : create create
519  * @tc.desc      : function test
520  */
521 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0100, TestSize.Level2)
522 {
523     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
524     if (cap) {
525         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
526         ASSERT_NE(venc_, NULL);
527         OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
528         ASSERT_NE(venc_2, NULL);
529         OH_VideoEncoder_Destroy(venc_2);
530         venc_2 = nullptr;
531     } else {
532         return;
533     }
534 }
535 
536 /**
537  * @tc.number    : VIDEO_ENCODE_API_3100
538  * @tc.name      : create create
539  * @tc.desc      : function test
540  */
541 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_3100, TestSize.Level2)
542 {
543     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
544     if (cap) {
545         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
546         ASSERT_NE(venc_, NULL);
547         OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByName(g_codecName);
548         ASSERT_NE(venc_2, NULL);
549         OH_VideoEncoder_Destroy(venc_2);
550         venc_2 = nullptr;
551     } else {
552         return;
553     }
554 }
555 
556 /**
557  * @tc.number    : VIDEO_ENCODE_API_0200
558  * @tc.name      : create configure configure
559  * @tc.desc      : function test
560  */
561 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0200, TestSize.Level2)
562 {
563     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
564     if (cap) {
565         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
566         ASSERT_NE(NULL, venc_);
567 
568         format = OH_AVFormat_Create();
569         ASSERT_NE(NULL, format);
570 
571         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
572         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
573         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
574 
575         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
576         ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_Configure(venc_, format));
577     } else {
578         return;
579     }
580 }
581 
582 /**
583  * @tc.number    : VIDEO_ENCODE_API_0300
584  * @tc.name      : create configure start start
585  * @tc.desc      : function test
586  */
587 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0300, TestSize.Level2)
588 {
589     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
590     if (cap) {
591         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
592         ASSERT_NE(NULL, venc_);
593 
594         format = OH_AVFormat_Create();
595         ASSERT_NE(NULL, format);
596 
597         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
598         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
599         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
600         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
601         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
602         ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_Start(venc_));
603     } else {
604         return;
605     }
606 }
607 
608 /**
609  * @tc.number    : VIDEO_ENCODE_API_0400
610  * @tc.name      : create configure start stop stop
611  * @tc.desc      : function test
612  */
613 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0400, TestSize.Level2)
614 {
615     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
616     if (cap) {
617         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
618         ASSERT_NE(NULL, venc_);
619 
620         format = OH_AVFormat_Create();
621         ASSERT_NE(NULL, format);
622 
623         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
624         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
625         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
626 
627         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
628         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
629         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
630         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
631     } else {
632         return;
633     }
634 }
635 
636 /**
637  * @tc.number    : VIDEO_ENCODE_API_0500
638  * @tc.name      : create configure start stop reset reset
639  * @tc.desc      : function test
640  */
641 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0500, TestSize.Level2)
642 {
643     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
644     if (cap) {
645         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
646         ASSERT_NE(NULL, venc_);
647 
648         format = OH_AVFormat_Create();
649         ASSERT_NE(NULL, format);
650         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
651         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
652         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
653 
654         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
655         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
656         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
657         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Reset(venc_));
658         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Reset(venc_));
659     } else {
660         return;
661     }
662 }
663 
664 /**
665  * @tc.number    : VIDEO_ENCODE_API_0600
666  * @tc.name      : create configure start EOS EOS
667  * @tc.desc      : function test
668  */
669 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0600, TestSize.Level2)
670 {
671     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
672     if (cap) {
673         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
674         ASSERT_NE(NULL, venc_);
675         format = OH_AVFormat_Create();
676         ASSERT_NE(NULL, format);
677         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
678         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
679         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
680         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
681         OH_AVCodecAsyncCallback cb_;
682         cb_.onError = onError;
683         cb_.onStreamChanged = onStreamChanged;
684         cb_.onNeedInputData = onNeedInputData;
685         cb_.onNeedOutputData = onNewOutputData;
686         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, static_cast<void *>(signal_)));
687 
688         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
689         unique_lock<mutex> lock(signal_->inMutex_);
__anon8d66a4f00302null690         signal_->inCond_.wait(lock, [] { return signal_->inIdxQueue_.size() > 1; });
691         uint32_t index = signal_->inIdxQueue_.front();
692         OH_AVCodecBufferAttr attr;
693         attr.pts = 0;
694         attr.size = 0;
695         attr.offset = 0;
696         attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
697 
698         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_PushInputData(venc_, index, attr));
699         signal_->inIdxQueue_.pop();
700         index = signal_->inIdxQueue_.front();
701         ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_PushInputData(venc_, index, attr));
702         signal_->inIdxQueue_.pop();
703     } else {
704         return;
705     }
706 }
707 
708 /**
709  * @tc.number    : VIDEO_ENCODE_API_0700
710  * @tc.name      : create configure start flush flush
711  * @tc.desc      : function test
712  */
713 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0700, TestSize.Level2)
714 {
715     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
716     if (cap) {
717         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
718         ASSERT_NE(NULL, venc_);
719         format = OH_AVFormat_Create();
720         ASSERT_NE(NULL, format);
721         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
722         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
723         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
724         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
725         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
726         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Flush(venc_));
727         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Flush(venc_));
728     } else {
729         return;
730     }
731 }
732 
733 /**
734  * @tc.number    : VIDEO_ENCODE_API_0800
735  * @tc.name      : create configure start stop release release
736  * @tc.desc      : function test
737  */
738 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0800, TestSize.Level2)
739 {
740     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
741     if (cap) {
742         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
743         ASSERT_NE(NULL, venc_);
744 
745         format = OH_AVFormat_Create();
746         ASSERT_NE(NULL, format);
747 
748         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
749         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
750         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
751 
752         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
753         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
754         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
755         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Destroy(venc_));
756         venc_ = nullptr;
757         ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Destroy(venc_));
758     } else {
759         return;
760     }
761 }
762 
763 /**
764  * @tc.number    : VIDEO_ENCODE_API_0900
765  * @tc.name      : create create
766  * @tc.desc      : function test
767  */
768 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_0900, TestSize.Level2)
769 {
770     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
771     if (cap) {
772         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
773         ASSERT_NE(venc_, NULL);
774         OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
775         ASSERT_NE(venc_2, NULL);
776         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Destroy(venc_2));
777         venc_2 = nullptr;
778     } else {
779         return;
780     }
781 }
782 
783 /**
784  * @tc.number    : VIDEO_ENCODE_API_1000
785  * @tc.name      : repeat OH_VideoEncoder_SetCallback
786  * @tc.desc      : function test
787  */
788 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1000, TestSize.Level2)
789 {
790     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
791     if (cap) {
792         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
793         ASSERT_NE(venc_, NULL);
794         OH_AVCodecAsyncCallback cb_;
795         cb_.onError = onError;
796         cb_.onStreamChanged = onStreamChanged;
797         cb_.onNeedInputData = onNeedInputData;
798         cb_.onNeedOutputData = onNewOutputData;
799         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
800         ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
801     } else {
802         return;
803     }
804 }
805 
806 /**
807  * @tc.number    : VIDEO_ENCODE_API_1100
808  * @tc.name      : repeat OH_VideoEncoder_GetOutputDescription
809  * @tc.desc      : function test
810  */
811 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1100, TestSize.Level2)
812 {
813     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
814     if (cap) {
815         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
816         ASSERT_NE(venc_, NULL);
817         format = OH_VideoEncoder_GetOutputDescription(venc_);
818         ASSERT_NE(NULL, format);
819         OH_AVFormat_Destroy(format);
820         format = OH_VideoEncoder_GetOutputDescription(venc_);
821         ASSERT_NE(NULL, format);
822     } else {
823         return;
824     }
825 }
826 
827 /**
828  * @tc.number    : VIDEO_ENCODE_API_1200
829  * @tc.name      : repeat OH_VideoEncoder_SetParameter
830  * @tc.desc      : function test
831  */
832 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1200, TestSize.Level2)
833 {
834     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
835     if (cap) {
836         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
837         ASSERT_NE(NULL, venc_);
838 
839         format = OH_AVFormat_Create();
840         ASSERT_NE(NULL, format);
841 
842         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
843         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
844         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
845 
846         ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_SetParameter(venc_, format));
847         ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_SetParameter(venc_, format));
848     } else {
849         return;
850     }
851 }
852 
853 /**
854  * @tc.number    : VIDEO_ENCODE_API_1200
855  * @tc.name      : repeat OH_VideoEncoder_GetInputDescription
856  * @tc.desc      : function test
857  */
858 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1300, TestSize.Level2)
859 {
860     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
861     if (cap) {
862         venc_ = OH_VideoEncoder_CreateByName(g_codecName);
863         ASSERT_NE(NULL, venc_);
864         format = OH_VideoEncoder_GetInputDescription(venc_);
865         ASSERT_NE(NULL, format);
866         OH_AVFormat_Destroy(format);
867         format = OH_VideoEncoder_GetInputDescription(venc_);
868         ASSERT_NE(NULL, format);
869     } else {
870         return;
871     }
872 }
873 
874 /**
875  * @tc.number    : VIDEO_ENCODE_API_1400
876  * @tc.name      : set quality with illegal value
877  * @tc.desc      : function test
878  */
879 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1400, TestSize.Level2)
880 {
881     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
882     if (cap) {
883         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
884         ASSERT_NE(nullptr, venc_);
885         format = OH_AVFormat_Create();
886         ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, CQ));
887         ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, 101));
888         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
889         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
890         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
891         ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Configure(venc_, format));
892     } else {
893         return;
894     }
895 }
896 
897 /**
898  * @tc.number    : VIDEO_ENCODE_API_1410
899  * @tc.name      : set quality with illegal value
900  * @tc.desc      : function test
901  */
902 HWTEST_F(HwEncApiNdkTest, VIDEO_ENCODE_API_1410, TestSize.Level2)
903 {
904     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
905     if (cap) {
906         venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
907         ASSERT_NE(nullptr, venc_);
908         format = OH_AVFormat_Create();
909         ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, CQ));
910         ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, -1));
911         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
912         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
913         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
914         ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Configure(venc_, format));
915     } else {
916         return;
917     }
918 }
919 } // namespace
920