• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 DEFAULT_BITRATE = 1000000;
34 constexpr uint32_t CODEC_NAME_SIZE = 128;
35 char g_codecName[CODEC_NAME_SIZE] = {};
36 OH_AVCapability *cap = nullptr;
37 OHOS::Media::VEncSignal *signal_ = nullptr;
38 OH_AVFormat *format;
onError(OH_AVCodec * codec,int32_t errorCode,void * userData)39 void onError(OH_AVCodec *codec, int32_t errorCode, void *userData)
40 {
41     cout << "Error errorCode=" << errorCode << endl;
42 };
43 
onStreamChanged(OH_AVCodec * codec,OH_AVFormat * fmt,void * userData)44 void onStreamChanged(OH_AVCodec *codec, OH_AVFormat *fmt, void *userData)
45 {
46     cout << "stream Changed" << endl;
47 };
48 
onNeedInputData(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,void * userData)49 void onNeedInputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)
50 {
51     VEncSignal *signal = static_cast<VEncSignal *>(userData);
52     if (signal == nullptr) {
53         return;
54     }
55     unique_lock<mutex> lock(signal->inMutex_);
56     signal->inIdxQueue_.push(index);
57     signal->inBufferQueue_.push(data);
58     signal->inCond_.notify_all();
59     cout << "need input data" << endl;
60 };
61 
onNewOutputData(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,OH_AVCodecBufferAttr * attr,void * userData)62 void onNewOutputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr, void *userData)
63 {
64     cout << "output data" << endl;
65     VEncSignal *signal = static_cast<VEncSignal *>(userData);
66     if (signal == nullptr) {
67         return;
68     }
69     unique_lock<mutex> lock(signal->outMutex_);
70     signal->outIdxQueue_.push(index);
71     signal->attrQueue_.push(*attr);
72     signal->outBufferQueue_.push(data);
73     signal->outCond_.notify_all();
74 };
75 } // namespace
76 
77 namespace OHOS {
78 namespace Media {
79 class AvcSwEncApiNdkTest : public testing::Test {
80 public:
81     // SetUpTestCase: Called before all test cases
82     static void SetUpTestCase(void);
83     // TearDownTestCase: Called after all test case
84     static void TearDownTestCase(void);
85     // SetUp: Called before each test cases
86     void SetUp(void);
87     // TearDown: Called after each test cases
88     void TearDown(void);
89 };
90 
SetUpTestCase()91 void AvcSwEncApiNdkTest::SetUpTestCase()
92 {
93     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, SOFTWARE);
94     const char *tmpCodecName = OH_AVCapability_GetName(cap);
95     if (memcpy_s(g_codecName, sizeof(g_codecName), tmpCodecName, strlen(tmpCodecName)) != 0) {
96         cout << "memcpy failed" << endl;
97     }
98     cout << "codecname: " << g_codecName << endl;
99 }
TearDownTestCase()100 void AvcSwEncApiNdkTest::TearDownTestCase() {}
SetUp()101 void AvcSwEncApiNdkTest::SetUp()
102 {
103     signal_ = new VEncSignal();
104 }
TearDown()105 void AvcSwEncApiNdkTest::TearDown()
106 {
107     if (format != nullptr) {
108         OH_AVFormat_Destroy(format);
109         format = nullptr;
110     }
111     if (venc_ != NULL) {
112         OH_VideoEncoder_Destroy(venc_);
113         venc_ = nullptr;
114     }
115     if (signal_) {
116         delete signal_;
117         signal_ = nullptr;
118     }
119 }
120 } // namespace Media
121 } // namespace OHOS
122 
123 namespace {
124 /**
125  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0100
126  * @tc.name      : OH_VideoEncoder_CreateByMime para1 error
127  * @tc.desc      : api test
128  */
129 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0100, TestSize.Level2)
130 {
131     if (strlen(g_codecName) == 0) {
132         return;
133     }
134     venc_ = OH_VideoEncoder_CreateByMime(nullptr);
135     ASSERT_EQ(nullptr, venc_);
136 }
137 
138 /**
139  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0200
140  * @tc.name      : OH_VideoEncoder_CreateByMime para2 error
141  * @tc.desc      : api test
142  */
143 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0200, TestSize.Level2)
144 {
145     if (strlen(g_codecName) == 0) {
146         return;
147     }
148     venc_ = OH_VideoEncoder_CreateByMime("");
149     ASSERT_EQ(nullptr, venc_);
150 }
151 
152 /**
153  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0300
154  * @tc.name      : OH_VideoEncoder_CreateByName para error
155  * @tc.desc      : api test
156  */
157 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0300, TestSize.Level2)
158 {
159     if (strlen(g_codecName) == 0) {
160         return;
161     }
162     venc_ = OH_VideoEncoder_CreateByName(nullptr);
163     ASSERT_EQ(nullptr, venc_);
164 }
165 
166 /**
167  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0400
168  * @tc.name      : OH_VideoEncoder_CreateByName para error
169  * @tc.desc      : api test
170  */
171 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0400, TestSize.Level2)
172 {
173     if (strlen(g_codecName) == 0) {
174         return;
175     }
176     venc_ = OH_VideoEncoder_CreateByName("");
177     ASSERT_EQ(nullptr, venc_);
178 }
179 
180 /**
181  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0500
182  * @tc.name      : OH_VideoEncoder_Destroy para error
183  * @tc.desc      : api test
184  */
185 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0500, TestSize.Level2)
186 {
187     if (strlen(g_codecName) == 0) {
188         return;
189     }
190     OH_AVErrCode ret = AV_ERR_OK;
191     ret = OH_VideoEncoder_Destroy(nullptr);
192     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
193 }
194 
195 /**
196  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0600
197  * @tc.name      : OH_VideoEncoder_SetCallback para error
198  * @tc.desc      : api test
199  */
200 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0600, TestSize.Level2)
201 {
202     if (strlen(g_codecName) == 0) {
203         return;
204     }
205     OH_AVCodecAsyncCallback cb_;
206     cb_.onError = onError;
207     cb_.onStreamChanged = onStreamChanged;
208     cb_.onNeedInputData = onNeedInputData;
209     cb_.onNeedOutputData = onNewOutputData;
210 
211     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_SetCallback(NULL, cb_, static_cast<void *>(signal_)));
212 }
213 
214 /**
215  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0700
216  * @tc.name      : OH_VideoEncoder_SetCallback para error
217  * @tc.desc      : api test
218  */
219 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0700, TestSize.Level2)
220 {
221     if (strlen(g_codecName) == 0) {
222         return;
223     }
224     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
225     ASSERT_NE(NULL, venc_);
226 
227     OH_AVCodecAsyncCallback cb2_;
228     cb2_.onError = NULL;
229     cb2_.onStreamChanged = NULL;
230     cb2_.onNeedInputData = NULL;
231     cb2_.onNeedOutputData = NULL;
232     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb2_, static_cast<void *>(signal_)));
233 }
234 
235 /**
236  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0800
237  * @tc.name      : OH_VideoEncoder_SetCallback para error
238  * @tc.desc      : api test
239  */
240 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0800, TestSize.Level2)
241 {
242     if (strlen(g_codecName) == 0) {
243         return;
244     }
245     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
246     OH_AVCodecAsyncCallback cb_;
247     cb_.onError = onError;
248     cb_.onStreamChanged = onStreamChanged;
249     cb_.onNeedInputData = onNeedInputData;
250     cb_.onNeedOutputData = onNewOutputData;
251     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
252 }
253 
254 /**
255  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_0900
256  * @tc.name      : OH_VideoEncoder_Configure para error
257  * @tc.desc      : api test
258  */
259 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0900, TestSize.Level2)
260 {
261     if (strlen(g_codecName) == 0) {
262         return;
263     }
264     OH_AVErrCode ret = AV_ERR_OK;
265     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
266     ASSERT_NE(nullptr, venc_);
267     ret = OH_VideoEncoder_Configure(venc_, nullptr);
268     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
269 }
270 
271 /**
272  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1000
273  * @tc.name      : OH_VideoEncoder_Configure para not enough
274  * @tc.desc      : api test
275  */
276 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1000, TestSize.Level2)
277 {
278     if (strlen(g_codecName) == 0) {
279         return;
280     }
281     OH_AVErrCode ret = AV_ERR_OK;
282     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
283     ASSERT_NE(nullptr, venc_);
284     format = OH_AVFormat_Create();
285     ASSERT_NE(nullptr, format);
286     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, 100000);
287     ret = OH_VideoEncoder_Configure(venc_, format);
288     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
289 }
290 
291 /**
292  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1100
293  * @tc.name      : OH_VideoEncoder_Start para error
294  * @tc.desc      : api test
295  */
296 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1100, TestSize.Level2)
297 {
298     if (strlen(g_codecName) == 0) {
299         return;
300     }
301     OH_AVErrCode ret = AV_ERR_OK;
302     ret = OH_VideoEncoder_Start(nullptr);
303     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
304 }
305 
306 /**
307  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1200
308  * @tc.name      : OH_VideoEncoder_Stop para error
309  * @tc.desc      : api test
310  */
311 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1200, TestSize.Level2)
312 {
313     if (strlen(g_codecName) == 0) {
314         return;
315     }
316     OH_AVErrCode ret = AV_ERR_OK;
317     ret = OH_VideoEncoder_Stop(nullptr);
318     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
319 }
320 
321 /**
322  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1300
323  * @tc.name      : OH_VideoEncoder_Flush para error
324  * @tc.desc      : api test
325  */
326 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1300, TestSize.Level2)
327 {
328     if (strlen(g_codecName) == 0) {
329         return;
330     }
331     OH_AVErrCode ret = AV_ERR_OK;
332     ret = OH_VideoEncoder_Flush(nullptr);
333     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
334 }
335 
336 /**
337  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1400
338  * @tc.name      : OH_VideoEncoder_Reset para error
339  * @tc.desc      : api test
340  */
341 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1400, TestSize.Level2)
342 {
343     if (strlen(g_codecName) == 0) {
344         return;
345     }
346     OH_AVErrCode ret = AV_ERR_OK;
347     ret = OH_VideoEncoder_Reset(nullptr);
348     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
349 }
350 
351 /**
352  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1500
353  * @tc.name      : OH_VideoEncoder_Reset para error
354  * @tc.desc      : api test
355  */
356 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1500, TestSize.Level2)
357 {
358     if (strlen(g_codecName) == 0) {
359         return;
360     }
361     format = OH_VideoEncoder_GetOutputDescription(nullptr);
362     ASSERT_EQ(format, nullptr);
363 }
364 
365 /**
366  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1600
367  * @tc.name      : OH_VideoEncoder_SetParameter para error
368  * @tc.desc      : api test
369  */
370 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1600, TestSize.Level2)
371 {
372     if (strlen(g_codecName) == 0) {
373         return;
374     }
375     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
376     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_SetParameter(venc_, nullptr));
377 }
378 
379 /**
380  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1700
381  * @tc.name      : OH_VideoEncoder_SetParameter para error
382  * @tc.desc      : api test
383  */
384 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1700, TestSize.Level2)
385 {
386     if (strlen(g_codecName) == 0) {
387         return;
388     }
389     format = OH_AVFormat_Create();
390     ASSERT_NE(NULL, format);
391     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
392     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_SetParameter(NULL, format));
393 }
394 
395 /**
396  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1800
397  * @tc.name      : OH_VideoEncoder_GetSurface para error
398  * @tc.desc      : api test
399  */
400 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1800, TestSize.Level2)
401 {
402     if (strlen(g_codecName) == 0) {
403         return;
404     }
405     OH_AVErrCode ret = AV_ERR_OK;
406     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
407     ASSERT_NE(nullptr, venc_);
408     ret = OH_VideoEncoder_GetSurface(venc_, nullptr);
409     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
410 }
411 
412 /**
413  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_1900
414  * @tc.name      : OH_VideoEncoder_FreeOutputData para error
415  * @tc.desc      : api test
416  */
417 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_1900, TestSize.Level2)
418 {
419     if (strlen(g_codecName) == 0) {
420         return;
421     }
422     OH_AVErrCode ret = AV_ERR_OK;
423     ret = OH_VideoEncoder_FreeOutputData(nullptr, 0);
424     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
425 }
426 
427 /**
428  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2000
429  * @tc.name      : OH_VideoEncoder_FreeOutputData para error
430  * @tc.desc      : api test
431  */
432 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2000, TestSize.Level2)
433 {
434     if (strlen(g_codecName) == 0) {
435         return;
436     }
437     OH_AVErrCode ret = AV_ERR_OK;
438     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
439     ASSERT_NE(nullptr, venc_);
440     format = OH_AVFormat_Create();
441     ASSERT_NE(nullptr, format);
442     OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
443     OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_HEIGHT);
444     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
445     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
446 
447     ret = OH_VideoEncoder_Configure(venc_, format);
448     ASSERT_EQ(ret, AV_ERR_OK);
449     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
450     usleep(1000000);
451     ret = OH_VideoEncoder_FreeOutputData(venc_, 9999999);
452     ASSERT_EQ(ret, AV_ERR_INVALID_STATE);
453 }
454 
455 /**
456  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2100
457  * @tc.name      : OH_VideoEncoder_NotifyEndOfStream para error
458  * @tc.desc      : api test
459  */
460 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2100, TestSize.Level2)
461 {
462     if (strlen(g_codecName) == 0) {
463         return;
464     }
465     OH_AVErrCode ret = AV_ERR_OK;
466     ret = OH_VideoEncoder_NotifyEndOfStream(nullptr);
467     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
468 }
469 /**
470  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2200
471  * @tc.name      : OH_VideoEncoder_NotifyEndOfStream para error
472  * @tc.desc      : api test
473  */
474 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2200, TestSize.Level2)
475 {
476     if (strlen(g_codecName) == 0) {
477         return;
478     }
479     OH_AVErrCode ret = AV_ERR_OK;
480     ret = OH_VideoEncoder_NotifyEndOfStream(nullptr);
481     ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
482 }
483 
484 /**
485  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2300
486  * @tc.name      : OH_VideoEncoder_PushInputData para error
487  * @tc.desc      : api test
488  */
489 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2300, TestSize.Level2)
490 {
491     if (strlen(g_codecName) == 0) {
492         return;
493     }
494     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
495     ASSERT_NE(nullptr, venc_);
496     OH_AVCodecBufferAttr attr;
497     attr.pts = -1;
498     attr.size = -1;
499     attr.offset = 0;
500     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
501 
502     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(venc_, 0, attr));
503 }
504 
505 /**
506  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2400
507  * @tc.name      : OH_VideoEncoder_PushInputData para error
508  * @tc.desc      : api test
509  */
510 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2400, TestSize.Level2)
511 {
512     if (strlen(g_codecName) == 0) {
513         return;
514     }
515     OH_AVCodecBufferAttr attr;
516     attr.pts = 0;
517     attr.size = 0;
518     attr.offset = 0;
519     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
520     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(NULL, 0, attr));
521 }
522 
523 /**
524  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2500
525  * @tc.name      : OH_VideoEncoder_PushInputData para error
526  * @tc.desc      : api test
527  */
528 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2500, TestSize.Level2)
529 {
530     if (strlen(g_codecName) == 0) {
531         return;
532     }
533     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
534     ASSERT_NE(nullptr, venc_);
535     OH_AVCodecBufferAttr attr;
536     attr.pts = 0;
537     attr.size = 0;
538     attr.offset = 0;
539     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
540     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_PushInputData(NULL, 99999, attr));
541 }
542 
543 /**
544  * @tc.number    : VIDEO_ENCODE_ILLEGAL_PARA_2600
545  * @tc.name      : OH_VideoEncoder_GetInputDescription para error
546  * * @tc.desc      : api test
547  */
548 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_2600, TestSize.Level2)
549 {
550     if (strlen(g_codecName) == 0) {
551         return;
552     }
553     ASSERT_EQ(nullptr, OH_VideoEncoder_GetInputDescription(nullptr));
554 }
555 
556 /**
557  * @tc.number    : VIDEO_ENCODE_API_0100
558  * @tc.name      : create create
559  * @tc.desc      : function test
560  */
561 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0100, TestSize.Level2)
562 {
563     if (strlen(g_codecName) == 0) {
564         return;
565     }
566     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
567     ASSERT_NE(venc_, NULL);
568     OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
569     ASSERT_NE(venc_2, NULL);
570     OH_VideoEncoder_Destroy(venc_2);
571     venc_2 = nullptr;
572 }
573 
574 /**
575  * @tc.number    : VIDEO_ENCODE_API_0200
576  * @tc.name      : create create
577  * @tc.desc      : function test
578  */
579 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0200, TestSize.Level2)
580 {
581     if (strlen(g_codecName) == 0) {
582         return;
583     }
584     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
585     ASSERT_NE(venc_, NULL);
586     OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByName(g_codecName);
587     ASSERT_NE(venc_2, NULL);
588     OH_VideoEncoder_Destroy(venc_2);
589     venc_2 = nullptr;
590 }
591 
592 /**
593  * @tc.number    : VIDEO_ENCODE_API_0300
594  * @tc.name      : create configure configure
595  * @tc.desc      : function test
596  */
597 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0300, TestSize.Level2)
598 {
599     if (strlen(g_codecName) == 0) {
600         return;
601     }
602     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
603     ASSERT_NE(NULL, venc_);
604 
605     format = OH_AVFormat_Create();
606     ASSERT_NE(NULL, format);
607 
608     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
609     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
610     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
611 
612     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
613     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_Configure(venc_, format));
614 }
615 
616 /**
617  * @tc.number    : VIDEO_ENCODE_API_0400
618  * @tc.name      : create configure start start
619  * @tc.desc      : function test
620  */
621 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0400, TestSize.Level2)
622 {
623     if (strlen(g_codecName) == 0) {
624         return;
625     }
626     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
627     ASSERT_NE(NULL, venc_);
628 
629     format = OH_AVFormat_Create();
630     ASSERT_NE(NULL, format);
631 
632     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
633     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
634     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
635     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
636     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
637     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
638     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_Start(venc_));
639 }
640 
641 /**
642  * @tc.number    : VIDEO_ENCODE_API_0500
643  * @tc.name      : create configure start stop stop
644  * @tc.desc      : function test
645  */
646 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0500, TestSize.Level2)
647 {
648     if (strlen(g_codecName) == 0) {
649         return;
650     }
651     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
652     ASSERT_NE(NULL, venc_);
653 
654     format = OH_AVFormat_Create();
655     ASSERT_NE(NULL, format);
656 
657     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
658     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
659     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
660     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
661 
662     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
663     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
664     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
665 }
666 
667 /**
668  * @tc.number    : VIDEO_ENCODE_API_0600
669  * @tc.name      : create configure start stop reset reset
670  * @tc.desc      : function test
671  */
672 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0600, TestSize.Level2)
673 {
674     if (strlen(g_codecName) == 0) {
675         return;
676     }
677     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
678     ASSERT_NE(NULL, venc_);
679 
680     format = OH_AVFormat_Create();
681     ASSERT_NE(NULL, format);
682     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
683     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
684     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
685     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
686 
687     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
688     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
689     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
690     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Reset(venc_));
691     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Reset(venc_));
692 }
693 
694 /**
695  * @tc.number    : VIDEO_ENCODE_API_0700
696  * @tc.name      : create configure start EOS EOS
697  * @tc.desc      : function test
698  */
699 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0700, TestSize.Level2)
700 {
701     if (strlen(g_codecName) == 0) {
702         return;
703     }
704     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
705     ASSERT_NE(NULL, venc_);
706     format = OH_AVFormat_Create();
707     ASSERT_NE(NULL, format);
708     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
709     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
710     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
711     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
712     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
713     OH_AVCodecAsyncCallback cb_;
714     cb_.onError = onError;
715     cb_.onStreamChanged = onStreamChanged;
716     cb_.onNeedInputData = onNeedInputData;
717     cb_.onNeedOutputData = onNewOutputData;
718     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, static_cast<void *>(signal_)));
719 
720     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
721     unique_lock<mutex> lock(signal_->inMutex_);
__anon935f02290302null722     signal_->inCond_.wait(lock, [] { return signal_->inIdxQueue_.size() > 1; });
723     uint32_t index = signal_->inIdxQueue_.front();
724     OH_AVCodecBufferAttr attr;
725     attr.pts = 0;
726     attr.size = 0;
727     attr.offset = 0;
728     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
729 
730     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_PushInputData(venc_, index, attr));
731     signal_->inIdxQueue_.pop();
732     index = signal_->inIdxQueue_.front();
733     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_PushInputData(venc_, index, attr));
734     signal_->inIdxQueue_.pop();
735 }
736 
737 /**
738  * @tc.number    : VIDEO_ENCODE_API_0800
739  * @tc.name      : create configure start flush flush
740  * @tc.desc      : function test
741  */
742 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0800, TestSize.Level2)
743 {
744     if (strlen(g_codecName) == 0) {
745         return;
746     }
747     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
748     ASSERT_NE(NULL, venc_);
749     format = OH_AVFormat_Create();
750     ASSERT_NE(NULL, format);
751     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
752     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
753     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
754     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
755     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
756     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
757     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Flush(venc_));
758 }
759 
760 /**
761  * @tc.number    : VIDEO_ENCODE_API_0900
762  * @tc.name      : create configure start stop release release
763  * @tc.desc      : function test
764  */
765 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_0900, TestSize.Level2)
766 {
767     if (strlen(g_codecName) == 0) {
768         return;
769     }
770     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
771     ASSERT_NE(NULL, venc_);
772 
773     format = OH_AVFormat_Create();
774     ASSERT_NE(NULL, format);
775 
776     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
777     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
778     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
779     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
780 
781     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Configure(venc_, format));
782     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Start(venc_));
783     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Stop(venc_));
784     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Destroy(venc_));
785     venc_ = nullptr;
786     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Destroy(venc_));
787 }
788 
789 /**
790  * @tc.number    : VIDEO_ENCODE_API_1000
791  * @tc.name      : create create
792  * @tc.desc      : function test
793  */
794 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_1000, TestSize.Level2)
795 {
796     if (strlen(g_codecName) == 0) {
797         return;
798     }
799     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
800     ASSERT_NE(venc_, NULL);
801     OH_AVCodec *venc_2 = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
802     ASSERT_NE(venc_2, NULL);
803     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_Destroy(venc_2));
804     venc_2 = nullptr;
805 }
806 
807 /**
808  * @tc.number    : VIDEO_ENCODE_API_1100
809  * @tc.name      : repeat OH_VideoEncoder_SetCallback
810  * @tc.desc      : function test
811  */
812 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_1100, TestSize.Level2)
813 {
814     if (strlen(g_codecName) == 0) {
815         return;
816     }
817     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
818     ASSERT_NE(venc_, NULL);
819     OH_AVCodecAsyncCallback cb_;
820     cb_.onError = onError;
821     cb_.onStreamChanged = onStreamChanged;
822     cb_.onNeedInputData = onNeedInputData;
823     cb_.onNeedOutputData = onNewOutputData;
824     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
825     ASSERT_EQ(AV_ERR_OK, OH_VideoEncoder_SetCallback(venc_, cb_, NULL));
826 }
827 
828 /**
829  * @tc.number    : VIDEO_ENCODE_API_1200
830  * @tc.name      : repeat OH_VideoEncoder_GetOutputDescription
831  * @tc.desc      : function test
832  */
833 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_1200, TestSize.Level2)
834 {
835     if (strlen(g_codecName) == 0) {
836         return;
837     }
838     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
839     ASSERT_NE(venc_, NULL);
840     format = OH_VideoEncoder_GetOutputDescription(venc_);
841     ASSERT_NE(NULL, format);
842     OH_AVFormat_Destroy(format);
843     format = OH_VideoEncoder_GetOutputDescription(venc_);
844     ASSERT_NE(NULL, format);
845 }
846 
847 /**
848  * @tc.number    : VIDEO_ENCODE_API_1300
849  * @tc.name      : repeat OH_VideoEncoder_SetParameter
850  * @tc.desc      : function test
851  */
852 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_1300, TestSize.Level2)
853 {
854     if (strlen(g_codecName) == 0) {
855         return;
856     }
857     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
858     ASSERT_NE(NULL, venc_);
859 
860     format = OH_AVFormat_Create();
861     ASSERT_NE(NULL, format);
862 
863     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
864     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
865     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
866 
867     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_SetParameter(venc_, format));
868     ASSERT_EQ(AV_ERR_INVALID_STATE, OH_VideoEncoder_SetParameter(venc_, format));
869 }
870 
871 /**
872  * @tc.number    : VIDEO_ENCODE_API_1400
873  * @tc.name      : repeat OH_VideoEncoder_GetInputDescription
874  * @tc.desc      : function test
875  */
876 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_1400, TestSize.Level2)
877 {
878     if (strlen(g_codecName) == 0) {
879         return;
880     }
881     venc_ = OH_VideoEncoder_CreateByName(g_codecName);
882     ASSERT_NE(NULL, venc_);
883     format = OH_VideoEncoder_GetInputDescription(venc_);
884     ASSERT_NE(NULL, format);
885     OH_AVFormat_Destroy(format);
886     format = OH_VideoEncoder_GetInputDescription(venc_);
887     ASSERT_NE(NULL, format);
888 }
889 
890 /**
891  * @tc.number    : VIDEO_ENCODE_API_1500
892  * @tc.name      : set quality with illegal value
893  * @tc.desc      : function test
894  */
895 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_1500, TestSize.Level2)
896 {
897     if (strlen(g_codecName) == 0) {
898         return;
899     }
900     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
901     ASSERT_NE(nullptr, venc_);
902     format = OH_AVFormat_Create();
903     ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, CQ));
904     ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, 101));
905     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
906     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
907     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
908     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Configure(venc_, format));
909 }
910 
911 /**
912  * @tc.number    : VIDEO_ENCODE_API_1600
913  * @tc.name      : set quality with illegal value
914  * @tc.desc      : function test
915  */
916 HWTEST_F(AvcSwEncApiNdkTest, VIDEO_ENCODE_API_1600, TestSize.Level2)
917 {
918     if (strlen(g_codecName) == 0) {
919         return;
920     }
921     venc_ = OH_VideoEncoder_CreateByMime(OH_AVCODEC_MIMETYPE_VIDEO_AVC);
922     ASSERT_NE(nullptr, venc_);
923     format = OH_AVFormat_Create();
924     ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, CQ));
925     ASSERT_EQ(true, OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, -1));
926     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
927     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
928     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_YUVI420);
929     ASSERT_EQ(AV_ERR_INVALID_VAL, OH_VideoEncoder_Configure(venc_, format));
930 }
931 } // namespace