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 
16 #include <iostream>
17 #include <cstdio>
18 
19 #include <atomic>
20 #include <fstream>
21 #include <thread>
22 #include <mutex>
23 #include <queue>
24 #include <string>
25 
26 #include "gtest/gtest.h"
27 #include "videodec_inner_sample.h"
28 #include "avcodec_video_decoder.h"
29 #include "meta/format.h"
30 #include "avcodec_errors.h"
31 #include "avcodec_common.h"
32 
33 using namespace std;
34 using namespace OHOS;
35 using namespace OHOS::MediaAVCodec;
36 using namespace testing::ext;
37 
38 namespace {
39 class SwdecInnerApiNdkTest : public testing::Test {
40 public:
41     // SetUpTestCase: Called before all test cases
42     static void SetUpTestCase(void);
43     // TearDownTestCase: Called after all test case
44     static void TearDownTestCase(void);
45     // SetUp: Called before each test cases
46     void SetUp(void) override;
47     // TearDown: Called after each test cases
48     void TearDown(void) override;
49 };
50 
51 std::shared_ptr<AVCodecVideoDecoder> vdec_ = nullptr;
52 std::shared_ptr<VDecInnerSignal> signal_ = nullptr;
53 std::string g_invalidCodecMime = "avdec_h264";
54 std::string g_codecMime = "video/avc";
55 std::string g_codecName = "OH.Media.Codec.Decoder.Video.AVC";
56 constexpr uint32_t DEFAULT_WIDTH = 1920;
57 constexpr uint32_t DEFAULT_HEIGHT = 1080;
58 constexpr uint32_t DEFAULT_FRAME_RATE = 30;
59 
SetUpTestCase()60 void SwdecInnerApiNdkTest::SetUpTestCase() {}
TearDownTestCase()61 void SwdecInnerApiNdkTest::TearDownTestCase() {}
SetUp()62 void SwdecInnerApiNdkTest::SetUp()
63 {
64     signal_ = make_shared<VDecInnerSignal>();
65 }
66 
TearDown()67 void SwdecInnerApiNdkTest::TearDown()
68 {
69     if (signal_) {
70         signal_ = nullptr;
71     }
72 
73     if (vdec_ != nullptr) {
74         vdec_->Release();
75         vdec_ = nullptr;
76     }
77 }
78 } // namespace
79 
80 namespace {
81 /**
82  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0100
83  * @tc.name      : CreateByMime para error
84  * @tc.desc      : param test
85  */
86 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0100, TestSize.Level2)
87 {
88     vdec_ = VideoDecoderFactory::CreateByMime("");
89     ASSERT_EQ(nullptr, vdec_);
90 }
91 
92 /**
93  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0200
94  * @tc.name      : CreateByName para error
95  * @tc.desc      : param test
96  */
97 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0200, TestSize.Level2)
98 {
99     vdec_ = VideoDecoderFactory::CreateByName("");
100     ASSERT_EQ(nullptr, vdec_);
101 }
102 
103 /**
104  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0300
105  * @tc.name      : SetCallback para error
106  * @tc.desc      : param test
107  */
108 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0300, TestSize.Level2)
109 {
110     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
111     ASSERT_NE(nullptr, vdec_);
112 
113     std::shared_ptr<VDecInnerCallback> cb_ = make_unique<VDecInnerCallback>(nullptr);
114     int32_t ret = vdec_->SetCallback(cb_);
115     ASSERT_EQ(AVCS_ERR_OK, ret);
116 }
117 
118 /**
119  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0400
120  * @tc.name      : SetCallback para error
121  * @tc.desc      : param test
122  */
123 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0400, TestSize.Level2)
124 {
125     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
126     ASSERT_NE(nullptr, vdec_);
127 
128     std::shared_ptr<VDecInnerCallback> cb_ = nullptr;
129     int32_t ret = vdec_->SetCallback(cb_);
130     ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
131 }
132 
133 /**
134  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0500
135  * @tc.name      : SetOutputSurface para error
136  * @tc.desc      : param test
137  */
138 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0500, TestSize.Level2)
139 {
140     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
141     ASSERT_NE(nullptr, vdec_);
142     ASSERT_EQ(AVCS_ERR_NO_MEMORY, vdec_->SetOutputSurface(nullptr));
143 }
144 
145 /**
146  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0600
147  * @tc.name      : CreateByName para error
148  * @tc.desc      : param test
149  */
150 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0600, TestSize.Level2)
151 {
152     vdec_ = VideoDecoderFactory::CreateByName(g_invalidCodecMime);
153     ASSERT_EQ(nullptr, vdec_);
154 }
155 
156 /**
157  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0700
158  * @tc.name      : CreateByMime para error
159  * @tc.desc      : param test
160  */
161 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0700, TestSize.Level2)
162 {
163     vdec_ = VideoDecoderFactory::CreateByMime(g_invalidCodecMime);
164     ASSERT_EQ(nullptr, vdec_);
165 }
166 
167 /**
168  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0800
169  * @tc.name      : ReleaseOutputBuffer para error
170  * @tc.desc      : param test
171  */
172 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0800, TestSize.Level2)
173 {
174     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
175     ASSERT_NE(nullptr, vdec_);
176     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->ReleaseOutputBuffer(0, true));
177 }
178 
179 /**
180  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_0900
181  * @tc.name      : ReleaseOutputBuffer para error
182  * @tc.desc      : param test
183  */
184 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_0900, TestSize.Level2)
185 {
186     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
187     ASSERT_NE(nullptr, vdec_);
188     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->ReleaseOutputBuffer(0, false));
189 }
190 
191 /**
192  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_1000
193  * @tc.name      : ReleaseOutputBuffer para error
194  * @tc.desc      : param test
195  */
196 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_1000, TestSize.Level2)
197 {
198     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
199     ASSERT_NE(nullptr, vdec_);
200     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->ReleaseOutputBuffer(-1, false));
201 }
202 
203 /**
204  * @tc.number    : VIDEO_SWDEC_ILLEGAL_PARA_1100
205  * @tc.name      : QueueInputBuffer para error
206  * @tc.desc      : param test
207  */
208 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_ILLEGAL_PARA_1100, TestSize.Level2)
209 {
210     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
211     ASSERT_NE(nullptr, vdec_);
212 
213     AVCodecBufferInfo info;
214     info.presentationTimeUs = -1;
215     info.size = -1;
216     info.offset = -1;
217     AVCodecBufferFlag flag = AVCODEC_BUFFER_FLAG_EOS;
218     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->QueueInputBuffer(0, info, flag));
219 }
220 
221 /**
222  * @tc.number    : VIDEO_SWDEC_API_0100
223  * @tc.name      : repeat CreateByName CreateByName
224  * @tc.desc      : api test
225  */
226 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0100, TestSize.Level2)
227 {
228     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
229     ASSERT_NE(nullptr, vdec_);
230 
231     std::shared_ptr<AVCodecVideoDecoder> vdec_2 = VideoDecoderFactory::CreateByName(g_codecName);
232     ASSERT_NE(nullptr, vdec_2);
233     ASSERT_EQ(AVCS_ERR_OK, vdec_2->Release());
234     vdec_2 = nullptr;
235 }
236 
237 /**
238  * @tc.number    : VIDEO_SWDEC_API_0200
239  * @tc.name      : create configure configure
240  * @tc.desc      : api test
241  */
242 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0200, TestSize.Level2)
243 {
244     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
245     ASSERT_NE(nullptr, vdec_);
246 
247     Format format;
248     format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
249     format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
250     format.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
251 
252     ASSERT_EQ(AVCS_ERR_OK, vdec_->Configure(format));
253     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->Configure(format));
254 }
255 
256 /**
257  * @tc.number    : VIDEO_SWDEC_API_0300
258  * @tc.name      : create configure start start
259  * @tc.desc      : api test
260  */
261 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0300, TestSize.Level2)
262 {
263     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
264     ASSERT_NE(nullptr, vdec_);
265 
266     Format format;
267     format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
268     format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
269     format.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
270 
271     ASSERT_EQ(AVCS_ERR_OK, vdec_->Configure(format));
272     ASSERT_EQ(AVCS_ERR_OK, vdec_->Start());
273     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->Start());
274 }
275 
276 /**
277  * @tc.number    : VIDEO_SWDEC_API_0400
278  * @tc.name      : create configure start stop stop
279  * @tc.desc      : api test
280  */
281 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0400, TestSize.Level2)
282 {
283     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
284     ASSERT_NE(nullptr, vdec_);
285 
286     Format format;
287     format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
288     format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
289     format.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
290 
291     ASSERT_EQ(AVCS_ERR_OK, vdec_->Configure(format));
292     ASSERT_EQ(AVCS_ERR_OK, vdec_->Start());
293     ASSERT_EQ(AVCS_ERR_OK, vdec_->Stop());
294     ASSERT_EQ(AVCS_ERR_OK, vdec_->Stop());
295 }
296 
297 /**
298  * @tc.number    : VIDEO_SWDEC_API_0500
299  * @tc.name      : create configure start stop reset reset
300  * @tc.desc      : api test
301  */
302 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0500, TestSize.Level2)
303 {
304     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
305     ASSERT_NE(nullptr, vdec_);
306 
307     Format format;
308     format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
309     format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
310     format.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
311 
312     ASSERT_EQ(AVCS_ERR_OK, vdec_->Configure(format));
313     ASSERT_EQ(AVCS_ERR_OK, vdec_->Start());
314     ASSERT_EQ(AVCS_ERR_OK, vdec_->Stop());
315     ASSERT_EQ(AVCS_ERR_OK, vdec_->Reset());
316     ASSERT_EQ(AVCS_ERR_OK, vdec_->Reset());
317 }
318 
319 /**
320  * @tc.number    : VIDEO_SWDEC_API_0600
321  * @tc.name      : create configure start EOS EOS
322  * @tc.desc      : api test
323  */
324 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0600, TestSize.Level2)
325 {
326     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
327     ASSERT_NE(nullptr, vdec_);
328 
329     Format format;
330     format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
331     format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
332     format.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
333 
334     ASSERT_EQ(AVCS_ERR_OK, vdec_->Configure(format));
335     ASSERT_EQ(AVCS_ERR_OK, vdec_->Start());
336 
337     AVCodecBufferInfo info;
338     info.presentationTimeUs = 0;
339     info.size = 0;
340     info.offset = 0;
341     AVCodecBufferFlag flag = AVCODEC_BUFFER_FLAG_EOS;
342     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->QueueInputBuffer(0, info, flag));
343     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->QueueInputBuffer(0, info, flag));
344 }
345 
346 /**
347  * @tc.number    : VIDEO_SWDEC_API_0700
348  * @tc.name      : create configure start flush flush
349  * @tc.desc      : api test
350  */
351 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0700, TestSize.Level2)
352 {
353     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
354     ASSERT_NE(nullptr, vdec_);
355 
356     Format format;
357     format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
358     format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
359     format.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
360 
361     ASSERT_EQ(AVCS_ERR_OK, vdec_->Configure(format));
362     ASSERT_EQ(AVCS_ERR_OK, vdec_->Start());
363     ASSERT_EQ(AVCS_ERR_OK, vdec_->Flush());
364     ASSERT_EQ(AVCS_ERR_OK, vdec_->Flush());
365 }
366 
367 /**
368  * @tc.number    : VIDEO_SWDEC_API_0800
369  * @tc.name      : create configure start stop release
370  * @tc.desc      : api test
371  */
372 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0800, TestSize.Level2)
373 {
374     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
375     ASSERT_NE(nullptr, vdec_);
376 
377     Format format;
378     format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
379     format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
380     format.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
381 
382     ASSERT_EQ(AVCS_ERR_OK, vdec_->Configure(format));
383     ASSERT_EQ(AVCS_ERR_OK, vdec_->Start());
384     ASSERT_EQ(AVCS_ERR_OK, vdec_->Stop());
385     ASSERT_EQ(AVCS_ERR_OK, vdec_->Release());
386     vdec_ = nullptr;
387 }
388 
389 /**
390  * @tc.number    : VIDEO_SWDEC_API_0900
391  * @tc.name      : create create
392  * @tc.desc      : api test
393  */
394 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_0900, TestSize.Level2)
395 {
396     vdec_ = VideoDecoderFactory::CreateByMime(g_codecMime);
397     ASSERT_NE(nullptr, vdec_);
398 
399     std::shared_ptr<AVCodecVideoDecoder> vdec_2 = VideoDecoderFactory::CreateByMime(g_codecMime);
400     ASSERT_NE(nullptr, vdec_2);
401     ASSERT_EQ(AVCS_ERR_OK, vdec_2->Release());
402     vdec_2 = nullptr;
403 }
404 
405 /**
406  * @tc.number    : VIDEO_SWDEC_API_1000
407  * @tc.name      : repeat SetCallback
408  * @tc.desc      : api test
409  */
410 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_1000, TestSize.Level2)
411 {
412     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
413     ASSERT_NE(nullptr, vdec_);
414 
415     std::shared_ptr<VDecInnerCallback> cb_ = make_unique<VDecInnerCallback>(nullptr);
416     ASSERT_EQ(AVCS_ERR_OK, vdec_->SetCallback(cb_));
417     ASSERT_EQ(AVCS_ERR_OK, vdec_->SetCallback(cb_));
418 }
419 
420 /**
421  * @tc.number    : VIDEO_SWDEC_API_1100
422  * @tc.name      : repeat GetOutputFormat
423  * @tc.desc      : api test
424  */
425 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_1100, TestSize.Level2)
426 {
427     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
428     ASSERT_NE(nullptr, vdec_);
429 
430     Format format;
431     ASSERT_EQ(AVCS_ERR_OK, vdec_->GetOutputFormat(format));
432     ASSERT_EQ(AVCS_ERR_OK, vdec_->GetOutputFormat(format));
433 }
434 
435 /**
436  * @tc.number    : VIDEO_SWDEC_API_1200
437  * @tc.name      : repeat SetParameter
438  * @tc.desc      : api test
439  */
440 HWTEST_F(SwdecInnerApiNdkTest, VIDEO_SWDEC_API_1200, TestSize.Level2)
441 {
442     vdec_ = VideoDecoderFactory::CreateByName(g_codecName);
443     ASSERT_NE(nullptr, vdec_);
444 
445     Format format;
446     format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
447     format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
448     format.PutDoubleValue(MediaDescriptionKey::MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
449 
450     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->SetParameter(format));
451     ASSERT_EQ(AVCS_ERR_INVALID_STATE, vdec_->SetParameter(format));
452 }
453 } // namespace