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 <atomic>
17 #include <fstream>
18 #include <iostream>
19 #include <mutex>
20 #include <queue>
21 #include <string>
22 #include <thread>
23 #include <unistd.h>
24 #include <vector>
25 #include <gtest/gtest.h>
26 #include "avcodec_codec_name.h"
27 #include "avcodec_common.h"
28 #include "avcodec_errors.h"
29 #include "iconsumer_surface.h"
30 #include "media_description.h"
31 #include "native_avcodec_base.h"
32 #include "native_avcodec_videodecoder.h"
33 #include "native_avformat.h"
34 #include "securec.h"
35 #include "window.h"
36 #include "unittest_log.h"
37
38 using namespace std;
39 using namespace testing::ext;
40 using namespace OHOS::MediaAVCodec;
41
42 namespace {
43 const uint32_t ES_H264[] = { // H264_FRAME_SIZE_240
44 2106, 11465, 321, 72, 472, 68, 76, 79, 509, 90, 677, 88, 956, 99, 347, 77, 452, 681, 81,
45 1263, 94, 106, 97, 998, 97, 797, 93, 1343, 150, 116, 117, 926, 1198, 128, 110, 78, 1582, 158,
46 135, 112, 1588, 165, 132, 128, 1697, 168, 149, 117, 1938, 170, 141, 142, 1830, 106, 161, 122, 1623,
47 160, 154, 156, 1998, 230, 177, 139, 1650, 186, 128, 134, 1214, 122, 1411, 120, 1184, 128, 1591, 195,
48 145, 105, 1587, 169, 140, 118, 1952, 177, 150, 161, 1437, 159, 123, 1758, 180, 165, 144, 1936, 214,
49 191, 175, 2122, 180, 179, 160, 1927, 161, 184, 119, 1973, 218, 210, 129, 1962, 196, 127, 154, 2308,
50 173, 127, 1572, 142, 122, 2065, 262, 159, 206, 2251, 269, 179, 170, 2056, 308, 168, 191, 2090, 303,
51 191, 110, 1932, 272, 162, 122, 1877, 245, 167, 141, 1908, 294, 162, 118, 1493, 132, 1782, 273, 184,
52 133, 1958, 274, 180, 149, 2070, 216, 169, 143, 1882, 224, 149, 139, 1749, 277, 184, 139, 2141, 197,
53 170, 140, 2002, 269, 162, 140, 1862, 202, 179, 131, 1868, 214, 164, 140, 1546, 226, 150, 130, 1707,
54 162, 146, 1824, 181, 147, 130, 1898, 209, 143, 131, 1805, 180, 148, 106, 1776, 147, 141, 1572, 177,
55 130, 105, 1776, 178, 144, 122, 1557, 142, 124, 114, 1436, 143, 126, 1326, 127, 1755, 169, 127, 105,
56 1807, 177, 131, 134, 1613, 187, 137, 136, 1314, 134, 118, 2005, 194, 129, 147, 1566, 185, 132, 131,
57 1236, 174, 137, 106, 11049, 574, 126, 1242, 188, 130, 119, 1450, 187, 137, 141, 1116, 124, 1848, 138,
58 122, 1605, 186, 127, 140, 1798, 170, 124, 121, 1666, 157, 128, 130, 1678, 135, 118, 1804, 169, 135,
59 125, 1837, 168, 124, 124, 2049, 180, 122, 128, 1334, 143, 128, 1379, 116, 1884, 149, 122, 150, 1962,
60 176, 122, 122, 1197, 139, 1853, 184, 151, 148, 1692, 209, 129, 126, 1736, 149, 135, 104, 1775, 165,
61 160, 121, 1653, 163, 123, 112, 1907, 181, 129, 107, 1808, 177, 125, 111, 2405, 166, 144, 114, 1833,
62 198, 136, 113, 1960, 206, 139, 116, 1791, 175, 130, 129, 1909, 194, 138, 119, 1807, 160, 156, 124,
63 1998, 184, 173, 114, 2069, 181, 127, 139, 2212, 182, 138, 146, 1993, 214, 135, 139, 2286, 194, 137,
64 120, 2090, 196, 159, 132, 2294, 194, 148, 137, 2312, 183, 163, 106, 2118, 201, 158, 127, 2291, 187,
65 144, 116, 2413, 139, 115, 2148, 178, 122, 103, 2370, 207, 161, 117, 2291, 213, 159, 129, 2244, 243,
66 157, 133, 2418, 255, 171, 127, 2316, 185, 160, 132, 2405, 220, 165, 155, 2539, 219, 172, 128, 2433,
67 199, 154, 119, 1681, 140, 1960, 143, 2682, 202, 153, 127, 2794, 239, 158, 155, 2643, 229, 172, 125,
68 2526, 201, 181, 159, 2554, 233, 167, 125, 2809, 205, 164, 117, 2707, 221, 156, 138, 2922, 240, 160,
69 146, 2952, 267, 177, 149, 3339, 271, 175, 136, 3006, 242, 168, 141, 3068, 232, 194, 149, 2760, 214,
70 208, 143, 2811, 218, 184, 149, 137, 15486, 2116, 235, 167, 157, 2894, 305, 184, 139, 3090, 345, 179,
71 155, 3226, 347, 160, 164, 3275, 321, 184, 174, 3240, 269, 166, 170, 3773, 265, 169, 155, 3023, 301,
72 188, 161, 3343, 275, 174, 155, 3526, 309, 177, 173, 3546, 307, 183, 149, 3648, 295, 213, 170, 3568,
73 305, 198, 166, 3641, 297, 172, 148, 3608, 301, 200, 159, 3693, 322, 209, 166, 3453, 318, 206, 162,
74 3696, 341, 200, 176, 3386, 320, 192, 176, 3903, 373, 207, 187, 3305, 361, 200, 202, 3110, 367, 220,
75 197, 2357, 332, 196, 201, 1827, 377, 187, 199, 860, 472, 173, 223, 238};
76 const uint32_t FC_H264[] = {139107, 1114, 474, 253, 282, 146, 197, 90, 108, 3214, 301, 77, 51, 43,
77 234, 210, 143, 108, 139107, 1114, 474, 253, 282, 146, 197, 90, 108};
78 constexpr uint32_t ES_LENGTH_H264 = sizeof(ES_H264) / sizeof(uint32_t);
79 constexpr uint32_t FC_LENGTH_H264 = sizeof(FC_H264) / sizeof(uint32_t);
80 constexpr uint32_t DEFAULT_WIDTH = 320;
81 constexpr uint32_t DEFAULT_HEIGHT = 240;
82 constexpr string_view inputFilePath = "/data/test/media/out_320_240_10s.h264";
83 constexpr string_view formatChangeInputFilePath = "/data/test/media/format_change_testseq.h264";
84 constexpr string_view outputFilePath = "/data/test/media/out_320_240_10s.yuv";
85 constexpr string_view outputSurfacePath = "/data/test/media/out_320_240_10s.rgba";
86 uint32_t g_writeFrameCount = 0;
87 } // namespace
88
89 namespace OHOS {
90 namespace MediaAVCodec {
91 namespace VCodecUT {
92 class VDecSignal {
93 public:
94 std::mutex inMutex_;
95 std::mutex outMutex_;
96 std::condition_variable inCond_;
97 std::condition_variable outCond_;
98 std::queue<uint32_t> inQueue_;
99 std::queue<uint32_t> outQueue_;
100 std::queue<OH_AVMemory *> inBufferQueue_;
101 std::queue<OH_AVMemory *> outBufferQueue_;
102 std::queue<OH_AVCodecBufferAttr> attrQueue_;
103 };
104
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)105 static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
106 {
107 (void)codec;
108 (void)errorCode;
109 (void)userData;
110 cout << "Error received, errorCode:" << errorCode << endl;
111 }
112
OnOutputFormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)113 static void OnOutputFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
114 {
115 (void)codec;
116 (void)format;
117 (void)userData;
118 cout << "OnOutputFormatChanged received" << endl;
119 }
120
OnInputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,void * userData)121 static void OnInputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)
122 {
123 (void)codec;
124 VDecSignal *signal = static_cast<VDecSignal *>(userData);
125 unique_lock<mutex> lock(signal->inMutex_);
126 signal->inQueue_.push(index);
127 signal->inBufferQueue_.push(data);
128 signal->inCond_.notify_all();
129 }
130
OnOutputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,OH_AVCodecBufferAttr * attr,void * userData)131 static void OnOutputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
132 void *userData)
133 {
134 (void)codec;
135 VDecSignal *signal = static_cast<VDecSignal *>(userData);
136 if (attr) {
137 unique_lock<mutex> lock(signal->outMutex_);
138 signal->outQueue_.push(index);
139 signal->outBufferQueue_.push(data);
140 signal->attrQueue_.push(*attr);
141 g_writeFrameCount += attr->size > 0 ? 1 : 0;
142 signal->outCond_.notify_all();
143 } else {
144 cout << "OnOutputBufferAvailable error, attr is nullptr!" << endl;
145 }
146 }
147
148 class TestConsumerListener : public IBufferConsumerListener {
149 public:
150 TestConsumerListener(sptr<Surface> cs, std::string_view name);
151 ~TestConsumerListener();
152 void OnBufferAvailable() override;
153
154 private:
155 int64_t timestamp_ = 0;
156 OHOS::Rect damage_ = {};
157 sptr<Surface> cs_ = nullptr;
158 std::unique_ptr<std::ofstream> outFile_;
159 };
160
TestConsumerListener(sptr<Surface> cs,std::string_view name)161 TestConsumerListener::TestConsumerListener(sptr<Surface> cs, std::string_view name) : cs_(cs)
162 {
163 outFile_ = std::make_unique<std::ofstream>();
164 outFile_->open(name.data(), std::ios::out | std::ios::binary);
165 }
166
~TestConsumerListener()167 TestConsumerListener::~TestConsumerListener()
168 {
169 if (outFile_ != nullptr) {
170 outFile_->close();
171 }
172 }
173
OnBufferAvailable()174 void TestConsumerListener::OnBufferAvailable()
175 {
176 sptr<SurfaceBuffer> buffer;
177 int32_t flushFence;
178
179 cs_->AcquireBuffer(buffer, flushFence, timestamp_, damage_);
180
181 (void)outFile_->write(reinterpret_cast<char *>(buffer->GetVirAddr()), buffer->GetSize());
182 cs_->ReleaseBuffer(buffer, -1);
183 }
184
GetSurface()185 static sptr<Surface> GetSurface()
186 {
187 sptr<Surface> cs = Surface::CreateSurfaceAsConsumer();
188 sptr<IBufferConsumerListener> listener = new TestConsumerListener(cs, outputSurfacePath);
189 cs->RegisterConsumerListener(listener);
190 auto p = cs->GetProducer();
191 sptr<Surface> ps = Surface::CreateSurfaceAsProducer(p);
192 return ps;
193 }
194
195 class VideoCodeCapiDecoderUnitTest : public testing::Test {
196 public:
197 static void SetUpTestCase(void);
198 static void TearDownTestCase(void);
199 void SetUp();
200 void TearDown();
201 int32_t ProceFunc();
202 void InputFunc();
203 void FormatChangeInputFunc();
204 void OutputFunc();
205
206 protected:
207 std::atomic<bool> isRunning_ = false;
208 std::unique_ptr<std::ifstream> testFile_;
209 std::unique_ptr<std::ofstream> outFile_;
210 std::unique_ptr<std::thread> inputLoop_;
211 std::unique_ptr<std::thread> outputLoop_;
212
213 struct OH_AVCodecAsyncCallback cb_;
214 std::shared_ptr<VDecSignal> signal_ = nullptr;
215 OH_AVCodec *videoDec_ = nullptr;
216 OH_AVFormat *format_ = nullptr;
217 bool isFirstFrame_ = true;
218 uint32_t frameCount_ = 0;
219 sptr<Surface> surface_ = nullptr;
220 };
221
SetUpTestCase(void)222 void VideoCodeCapiDecoderUnitTest::SetUpTestCase(void)
223 {
224 cout << "[SetUpTestCase]: " << endl;
225 }
226
TearDownTestCase(void)227 void VideoCodeCapiDecoderUnitTest::TearDownTestCase(void)
228 {
229 cout << "[TearDownTestCase]: " << endl;
230 }
231
SetUp(void)232 void VideoCodeCapiDecoderUnitTest::SetUp(void)
233 {
234 cout << "[SetUp]: SetUp!!!" << endl;
235 g_writeFrameCount = 0;
236 }
237
TearDown(void)238 void VideoCodeCapiDecoderUnitTest::TearDown(void)
239 {
240 cout << "[TearDown]: over!!!" << endl;
241 OH_VideoDecoder_Destroy(videoDec_);
242 }
243
InputFunc()244 void VideoCodeCapiDecoderUnitTest::InputFunc()
245 {
246 testFile_ = std::make_unique<std::ifstream>();
247 UNITTEST_CHECK_AND_RETURN_LOG(testFile_ != nullptr, "Fatal: No memory");
248 testFile_->open(inputFilePath, std::ios::in | std::ios::binary);
249 while (true) {
250 if (!isRunning_.load()) {
251 break;
252 }
253 unique_lock<mutex> lock(signal_->inMutex_);
254 signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
255 if (!isRunning_.load()) {
256 break;
257 }
258 uint32_t index = signal_->inQueue_.front();
259 auto buffer = signal_->inBufferQueue_.front();
260 OH_AVCodecBufferAttr info = {0, 0, 0, AVCODEC_BUFFER_FLAGS_EOS};
261 if (frameCount_ < ES_LENGTH_H264) {
262 info.size = ES_H264[frameCount_];
263 char *fileBuffer = static_cast<char *>(malloc(sizeof(char) * info.size + 1));
264 UNITTEST_CHECK_AND_RETURN_LOG(fileBuffer != nullptr, "Fatal: malloc fail.");
265 (void)testFile_->read(fileBuffer, info.size);
266 if (memcpy_s(OH_AVMemory_GetAddr(buffer), OH_AVMemory_GetSize(buffer), fileBuffer, info.size) != EOK) {
267 cout << "Fatal: memcpy fail" << endl;
268 free(fileBuffer);
269 break;
270 }
271 free(fileBuffer);
272 info.flags = AVCODEC_BUFFER_FLAGS_NONE;
273 if (isFirstFrame_) {
274 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
275 isFirstFrame_ = false;
276 }
277 int32_t ret = OH_VideoDecoder_PushInputData(videoDec_, index, info);
278 UNITTEST_CHECK_AND_RETURN_LOG(ret == AVCS_ERR_OK, "Fatal error, exit.");
279 frameCount_++;
280 } else {
281 OH_VideoDecoder_PushInputData(videoDec_, index, info);
282 std::cout << "input end buffer" << std::endl;
283 break;
284 }
285 signal_->inQueue_.pop();
286 signal_->inBufferQueue_.pop();
287 }
288 if (testFile_ != nullptr) {
289 testFile_->close();
290 }
291 }
292
FormatChangeInputFunc()293 void VideoCodeCapiDecoderUnitTest::FormatChangeInputFunc()
294 {
295 testFile_ = std::make_unique<std::ifstream>();
296 UNITTEST_CHECK_AND_RETURN_LOG(testFile_ != nullptr, "Fatal: No memory");
297 testFile_->open(formatChangeInputFilePath, std::ios::in | std::ios::binary);
298 while (true) {
299 if (!isRunning_.load()) {
300 break;
301 }
302 unique_lock<mutex> lock(signal_->inMutex_);
303 signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
304 if (!isRunning_.load()) {
305 break;
306 }
307 uint32_t index = signal_->inQueue_.front();
308 auto buffer = signal_->inBufferQueue_.front();
309 OH_AVCodecBufferAttr info = {0, 0, 0, AVCODEC_BUFFER_FLAGS_EOS};
310 if (frameCount_ < FC_LENGTH_H264) {
311 info.size = FC_H264[frameCount_];
312 char *fileBuffer = static_cast<char *>(malloc(sizeof(char) * info.size + 1));
313 UNITTEST_CHECK_AND_RETURN_LOG(fileBuffer != nullptr, "Fatal: malloc fail.");
314 (void)testFile_->read(fileBuffer, info.size);
315 if (memcpy_s(OH_AVMemory_GetAddr(buffer), OH_AVMemory_GetSize(buffer), fileBuffer, info.size) != EOK) {
316 cout << "Fatal: memcpy fail" << endl;
317 free(fileBuffer);
318 break;
319 }
320 free(fileBuffer);
321 info.flags = AVCODEC_BUFFER_FLAGS_NONE;
322 if (isFirstFrame_) {
323 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
324 isFirstFrame_ = false;
325 }
326 int32_t ret = OH_VideoDecoder_PushInputData(videoDec_, index, info);
327 UNITTEST_CHECK_AND_RETURN_LOG(ret == AVCS_ERR_OK, "Fatal error, exit.");
328 frameCount_++;
329 } else {
330 OH_VideoDecoder_PushInputData(videoDec_, index, info);
331 std::cout << "input end buffer" << std::endl;
332 break;
333 }
334 signal_->inQueue_.pop();
335 signal_->inBufferQueue_.pop();
336 }
337 if (testFile_ != nullptr) {
338 testFile_->close();
339 }
340 }
341
OutputFunc()342 void VideoCodeCapiDecoderUnitTest::OutputFunc()
343 {
344 if (!surface_) {
345 outFile_ = std::make_unique<std::ofstream>();
346 UNITTEST_CHECK_AND_RETURN_LOG(outFile_ != nullptr, "Fatal: No memory");
347 outFile_->open(outputFilePath.data(), std::ios::out | std::ios::binary);
348 }
349
350 while (true) {
351 if (!isRunning_.load()) {
352 cout << "stop, exit" << endl;
353 break;
354 }
355
356 unique_lock<mutex> lock(signal_->outMutex_);
357 signal_->outCond_.wait(lock, [this]() { return (signal_->outQueue_.size() > 0 || !isRunning_.load()); });
358
359 if (!isRunning_.load()) {
360 cout << "wait to stop, exit" << endl;
361 break;
362 }
363
364 uint32_t index = signal_->outQueue_.front();
365 OH_AVCodecBufferAttr attr = signal_->attrQueue_.front();
366 OH_AVMemory *data = signal_->outBufferQueue_.front();
367 if (outFile_ != nullptr && attr.size != 0 && data != nullptr && OH_AVMemory_GetAddr(data) != nullptr) {
368 outFile_->write(reinterpret_cast<char *>(OH_AVMemory_GetAddr(data)), attr.size);
369 }
370
371 if (attr.flags == AVCODEC_BUFFER_FLAGS_EOS) {
372 cout << "decode eos, write frame:" << g_writeFrameCount << endl;
373 isRunning_.store(false);
374 }
375 signal_->outBufferQueue_.pop();
376 signal_->attrQueue_.pop();
377 signal_->outQueue_.pop();
378 if (surface_) {
379 EXPECT_EQ(AV_ERR_OK, OH_VideoDecoder_RenderOutputData(videoDec_, index));
380 } else {
381 EXPECT_EQ(AV_ERR_OK, OH_VideoDecoder_FreeOutputData(videoDec_, index));
382 }
383 }
384 if (outFile_ != nullptr) {
385 outFile_->close();
386 }
387 }
388
ProceFunc(void)389 int32_t VideoCodeCapiDecoderUnitTest::ProceFunc(void)
390 {
391 videoDec_ = OH_VideoDecoder_CreateByName((AVCodecCodecName::VIDEO_DECODER_AVC_NAME).data());
392 EXPECT_NE(nullptr, videoDec_);
393
394 signal_ = make_shared<VDecSignal>();
395 cb_ = {&OnError, &OnOutputFormatChanged, &OnInputBufferAvailable, &OnOutputBufferAvailable};
396 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetCallback(videoDec_, cb_, signal_.get()));
397
398 format_ = OH_AVFormat_Create();
399 return AVCS_ERR_OK;
400 }
401
402 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Create_01, TestSize.Level1)
403 {
404 videoDec_ = OH_VideoDecoder_CreateByName("");
405 EXPECT_EQ(nullptr, videoDec_);
406 }
407
408 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Create_02, TestSize.Level1)
409 {
410 videoDec_ = OH_VideoDecoder_CreateByName("h266");
411 EXPECT_EQ(nullptr, videoDec_);
412 }
413
414 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Configure_01, TestSize.Level1)
415 {
416 ProceFunc();
417 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
418 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
419 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
420 }
421
422 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Configure_02, TestSize.Level1)
423 {
424 ProceFunc();
425 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
426 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
427 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
428 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
429 }
430
431 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Configure_03, TestSize.Level1)
432 {
433 ProceFunc();
434 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
435 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
436 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
437 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
438 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
439 }
440
441 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Configure_04, TestSize.Level1)
442 {
443 ProceFunc();
444 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
445 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
446 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
447 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
448 }
449
450 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Configure_05, TestSize.Level1)
451 {
452 ProceFunc();
453 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
454 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, 1000000);
455 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
456 }
457
458 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Configure_06, TestSize.Level1)
459 {
460 ProceFunc();
461 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
462 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
463 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_SCALING_MODE, DEFAULT_HEIGHT);
464 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
465 }
466
467 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Configure_07, TestSize.Level1)
468 {
469 ProceFunc();
470 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
471 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
472 OH_AVFormat_SetIntValue(format_, OH_MD_MAX_INPUT_BUFFER_COUNT, 4);
473 OH_AVFormat_SetIntValue(format_, OH_MD_MAX_OUTPUT_BUFFER_COUNT, 4);
474 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_BITRATE, 30000);
475 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_VIDEO_ENCODER_ENABLE_TEMPORAL_SCALABILITY, 4);
476 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
477 }
478
479 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Start_01, TestSize.Level1)
480 {
481 ProceFunc();
482 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
483 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
484 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
485 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
486 }
487
488 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Start_02, TestSize.Level1)
489 {
490 ProceFunc();
491 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
492 }
493
494 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Start_03, TestSize.Level1)
495 {
496 ProceFunc();
497 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
498 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
499 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
500 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
501 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
502 }
503
504 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Start_04, TestSize.Level1)
505 {
506 ProceFunc();
507 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
508 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
509 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
510 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
511 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
512 }
513
514 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_getOutputFormat_01, TestSize.Level1)
515 {
516 ProceFunc();
517 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
518 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
519 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
520 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
521 }
522
523 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_getOutputFormat_02, TestSize.Level1)
524 {
525 ProceFunc();
526 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
527 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
528 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
529 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
530 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
531 }
532
533 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_SetParameter_01, TestSize.Level1)
534 {
535 ProceFunc();
536 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
537 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
538
539 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
540 isRunning_.store(true);
541
542 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
543 EXPECT_NE(nullptr, inputLoop_);
544
545 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
546 EXPECT_NE(nullptr, outputLoop_);
547
548 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
549 while (isRunning_.load()) {
550 sleep(1); // sleep 1s
551 }
552
553 isRunning_.store(false);
554 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
555 unique_lock<mutex> lock(signal_->inMutex_);
556 signal_->inCond_.notify_all();
557 lock.unlock();
558 inputLoop_->join();
559 }
560 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
561 unique_lock<mutex> lock(signal_->outMutex_);
562 signal_->outCond_.notify_all();
563 lock.unlock();
564 outputLoop_->join();
565 }
566 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
567 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetParameter(videoDec_, format_));
568 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
569 }
570
571 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_SetParameter_02, TestSize.Level1)
572 {
573 ProceFunc();
574 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
575 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
576 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
577 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetParameter(videoDec_, format_));
578 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
579 }
580
581 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_SetParameter_03, TestSize.Level1)
582 {
583 ProceFunc();
584 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
585 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
586 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
587 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
588 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_MAX_INPUT_SIZE, 114514);
589 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetParameter(videoDec_, format_));
590 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
591 }
592
593 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_01, TestSize.Level1)
594 {
595 ProceFunc();
596 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
597 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
598
599 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
600 isRunning_.store(true);
601
602 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
603 EXPECT_NE(nullptr, inputLoop_);
604
605 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
606 EXPECT_NE(nullptr, outputLoop_);
607
608 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
609 while (isRunning_.load()) {
610 sleep(1); // sleep 1s
611 }
612
613 isRunning_.store(false);
614 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
615 unique_lock<mutex> lock(signal_->inMutex_);
616 signal_->inCond_.notify_all();
617 lock.unlock();
618 inputLoop_->join();
619 }
620 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
621 unique_lock<mutex> lock(signal_->outMutex_);
622 signal_->outCond_.notify_all();
623 lock.unlock();
624 outputLoop_->join();
625 }
626 }
627
628 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_02, TestSize.Level1)
629 {
630 ProceFunc();
631 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
632 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
633
634 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
635
636 surface_ = GetSurface();
637 EXPECT_NE(nullptr, surface_);
638 OHNativeWindow *nativeWindow = CreateNativeWindowFromSurface(&surface_);
639 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetSurface(videoDec_, nativeWindow));
640
641 isRunning_.store(true);
642
643 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
644 EXPECT_NE(nullptr, inputLoop_);
645
646 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
647 EXPECT_NE(nullptr, outputLoop_);
648
649 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
650 while (isRunning_.load()) {
651 if (g_writeFrameCount == 5) {
652 OH_AVFormat *format = OH_AVFormat_Create();
653 EXPECT_NE(nullptr, format);
654 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, 0);
655 OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, 0);
656 OH_AVFormat_SetIntValue(format, OH_MD_KEY_SCALING_MODE, 0);
657 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetParameter(videoDec_, format));
658 }
659 sleep(1); // sleep 1s
660 }
661
662 isRunning_.store(false);
663 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
664 unique_lock<mutex> lock(signal_->inMutex_);
665 signal_->inCond_.notify_all();
666 lock.unlock();
667 inputLoop_->join();
668 }
669 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
670 unique_lock<mutex> lock(signal_->outMutex_);
671 signal_->outCond_.notify_all();
672 lock.unlock();
673 outputLoop_->join();
674 }
675 }
676
677 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_03, TestSize.Level1)
678 {
679 ProceFunc();
680 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
681 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
682
683 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
684 isRunning_.store(true);
685 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
686 EXPECT_NE(nullptr, inputLoop_);
687 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
688 EXPECT_NE(nullptr, outputLoop_);
689
690 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
691 while (isRunning_.load()) {
692 sleep(1); // sleep 1s
693 }
694
695 isRunning_.store(false);
696 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
697 unique_lock<mutex> lock(signal_->inMutex_);
698 signal_->inCond_.notify_all();
699 lock.unlock();
700 inputLoop_->join();
701 }
702
703 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
704 unique_lock<mutex> lock(signal_->outMutex_);
705 signal_->outCond_.notify_all();
706 lock.unlock();
707 outputLoop_->join();
708 }
709 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
710 }
711
712 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_04, TestSize.Level1)
713 {
714 ProceFunc();
715 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
716 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
717
718 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
719 isRunning_.store(true);
720
721 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
722 EXPECT_NE(nullptr, inputLoop_);
723
724 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
725 EXPECT_NE(nullptr, outputLoop_);
726
727 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
728 while (isRunning_.load()) {
729 sleep(1); // sleep 1s
730 }
731
732 isRunning_.store(false);
733 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
734 unique_lock<mutex> lock(signal_->inMutex_);
735 signal_->inCond_.notify_all();
736 lock.unlock();
737 inputLoop_->join();
738 }
739
740 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
741 unique_lock<mutex> lock(signal_->outMutex_);
742 signal_->outCond_.notify_all();
743 lock.unlock();
744 outputLoop_->join();
745 }
746 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
747 }
748
749 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_05, TestSize.Level1)
750 {
751 ProceFunc();
752 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
753 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
754
755 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
756 isRunning_.store(true);
757
758 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
759 EXPECT_NE(nullptr, inputLoop_);
760
761 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
762 EXPECT_NE(nullptr, outputLoop_);
763
764 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
765 while (isRunning_.load()) {
766 sleep(1); // sleep 1s
767 }
768 isRunning_.store(false);
769 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
770 unique_lock<mutex> lock(signal_->inMutex_);
771 signal_->inCond_.notify_all();
772 lock.unlock();
773 inputLoop_->join();
774 }
775
776 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
777 unique_lock<mutex> lock(signal_->outMutex_);
778 signal_->outCond_.notify_all();
779 lock.unlock();
780 outputLoop_->join();
781 }
782 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
783 }
784
785 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_06, TestSize.Level1)
786 {
787 ProceFunc();
788 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
789 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
790
791 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
792 isRunning_.store(true);
793
794 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
795 EXPECT_NE(nullptr, inputLoop_);
796 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
797 EXPECT_NE(nullptr, outputLoop_);
798
799 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
800 while (isRunning_.load()) {
801 sleep(1); // sleep 1s
802 }
803
804 isRunning_.store(false);
805 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
806 unique_lock<mutex> lock(signal_->inMutex_);
807 signal_->inCond_.notify_all();
808 lock.unlock();
809 inputLoop_->join();
810 }
811
812 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
813 unique_lock<mutex> lock(signal_->outMutex_);
814 signal_->outCond_.notify_all();
815 lock.unlock();
816 outputLoop_->join();
817 }
818 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
819 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
820 }
821
822 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_formatchange, TestSize.Level1)
823 {
824 ProceFunc();
825 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, 1920);
826 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, 1080);
827
828 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
829
830 surface_ = GetSurface();
831 EXPECT_NE(nullptr, surface_);
832 OHNativeWindow *nativeWindow = CreateNativeWindowFromSurface(&surface_);
833 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetSurface(videoDec_, nativeWindow));
834
835 isRunning_.store(true);
836
837 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::FormatChangeInputFunc, this);
838 EXPECT_NE(nullptr, inputLoop_);
839
840 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
841 EXPECT_NE(nullptr, outputLoop_);
842
843 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
844 while (isRunning_.load()) {
845 sleep(1); // sleep 1s
846 }
847
848 isRunning_.store(false);
849 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
850 unique_lock<mutex> lock(signal_->inMutex_);
851 signal_->inCond_.notify_all();
852 lock.unlock();
853 inputLoop_->join();
854 }
855 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
856 unique_lock<mutex> lock(signal_->outMutex_);
857 signal_->outCond_.notify_all();
858 lock.unlock();
859 outputLoop_->join();
860 }
861 }
862
863 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_01, TestSize.Level1)
864 {
865 EXPECT_EQ(nullptr, videoDec_);
866 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
867 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
868 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
869 }
870
871 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_02, TestSize.Level1)
872 {
873 EXPECT_EQ(nullptr, videoDec_);
874 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
875 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
876 }
877
878 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_03, TestSize.Level1)
879 {
880 EXPECT_EQ(nullptr, videoDec_);
881 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
882 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
883 }
884
885 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_04, TestSize.Level1)
886 {
887 ProceFunc();
888 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
889 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
890 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
891 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
892 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
893 }
894
895 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_05, TestSize.Level1)
896 {
897 ProceFunc();
898 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
899 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
900 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
901 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
902 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
903 }
904
905 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_06, TestSize.Level1)
906 {
907 ProceFunc();
908 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
909 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
910 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
911 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
912 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
913 }
914
915 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_07, TestSize.Level1)
916 {
917 ProceFunc();
918 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
919 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
920 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
921 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
922 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
923 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
924 }
925
926 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_08, TestSize.Level1)
927 {
928 ProceFunc();
929 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
930 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
931 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
932 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
933 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
934 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
935 }
936
937 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_statuscase_01, TestSize.Level1)
938 {
939 ProceFunc();
940 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
941 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
942 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
943 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
944 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
945 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
946 }
947
948 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_statuscase_02, TestSize.Level1)
949 {
950 ProceFunc();
951 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
952 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
953 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
954 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
955 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
956 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
957 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
958 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
959 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
960 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
961 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
962 }
963
964 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_setcallback_01, TestSize.Level1)
965 {
966 videoDec_ = OH_VideoDecoder_CreateByName((AVCodecCodecName::VIDEO_DECODER_AVC_NAME).data());
967 EXPECT_NE(nullptr, videoDec_);
968 cb_ = {nullptr, nullptr, nullptr, nullptr};
969 signal_ = make_shared<VDecSignal>();
970 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetCallback(videoDec_, cb_, signal_.get()));
971 }
972
973 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_pushInputData_01, TestSize.Level1)
974 {
975 OH_AVCodecBufferAttr info = {0, 0, 0, AVCODEC_BUFFER_FLAGS_EOS};
976 int32_t bufferSize = 13571;
977 testFile_ = std::make_unique<std::ifstream>();
978 UNITTEST_CHECK_AND_RETURN_LOG(testFile_ != nullptr, "Fatal: No memory");
979
980 ProceFunc();
981 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
982 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
983 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
984 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
985
986 isRunning_.store(true);
987 testFile_->open(inputFilePath, std::ios::in | std::ios::binary);
988 unique_lock<mutex> lock(signal_->inMutex_);
__anon3d0e44fa0502() 989 signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
990 uint32_t index = signal_->inQueue_.front();
991 auto buffer = signal_->inBufferQueue_.front();
992 info.size = bufferSize;
993 char *fileBuffer = static_cast<char *>(malloc(sizeof(char) * info.size + 1));
994 (void)testFile_->read(fileBuffer, info.size);
995 memcpy_s(OH_AVMemory_GetAddr(buffer), OH_AVMemory_GetSize(buffer), fileBuffer, info.size);
996 free(fileBuffer);
997 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
998 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_PushInputData(videoDec_, index, info));
999 signal_->inQueue_.pop();
1000 signal_->inBufferQueue_.pop();
1001
1002 index = 0;
1003 buffer = signal_->inBufferQueue_.front();
1004 info.size = 0;
1005 info.flags = AVCODEC_BUFFER_FLAGS_EOS;
1006 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_PushInputData(videoDec_, index, info));
1007 testFile_->close();
1008 isRunning_.store(false);
1009 }
1010
1011 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_pushInputData_02, TestSize.Level1)
1012 {
1013 OH_AVCodecBufferAttr info = {0, 0, 0, AVCODEC_BUFFER_FLAGS_EOS};
1014 int32_t bufferSize = 13571;
1015 testFile_ = std::make_unique<std::ifstream>();
1016 UNITTEST_CHECK_AND_RETURN_LOG(testFile_ != nullptr, "Fatal: No memory");
1017
1018 ProceFunc();
1019 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
1020 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
1021 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
1022 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
1023
1024 isRunning_.store(true);
1025 testFile_->open(inputFilePath, std::ios::in | std::ios::binary);
1026 unique_lock<mutex> lock(signal_->inMutex_);
__anon3d0e44fa0602() 1027 signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
1028 uint32_t index = 1024;
1029 auto buffer = signal_->inBufferQueue_.front();
1030 info.size = bufferSize;
1031 char *fileBuffer = static_cast<char *>(malloc(sizeof(char) * info.size + 1));
1032 (void)testFile_->read(fileBuffer, info.size);
1033 memcpy_s(OH_AVMemory_GetAddr(buffer), OH_AVMemory_GetSize(buffer), fileBuffer, info.size);
1034 free(fileBuffer);
1035 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
1036 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_PushInputData(videoDec_, index, info));
1037 signal_->inQueue_.pop();
1038 signal_->inBufferQueue_.pop();
1039
1040 testFile_->close();
1041 isRunning_.store(false);
1042 }
1043
1044 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_getOutputBuffer_01, TestSize.Level1)
1045 {
1046 ProceFunc();
1047 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
1048 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
1049 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
1050 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
1051 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_FreeOutputData(videoDec_, 0));
1052 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_FreeOutputData(videoDec_, -1));
1053 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_FreeOutputData(videoDec_, 1024));
1054 surface_ = GetSurface();
1055 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_RenderOutputData(videoDec_, 0));
1056 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_RenderOutputData(videoDec_, -1));
1057 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_RenderOutputData(videoDec_, 1024));
1058 }
1059 } // namespace VCodecUT
1060 } // namespace MediaAVCodec
1061 } // namespace OHOS