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 "surface/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 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 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 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:" << 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_Start_01, 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, DEFAULT_HEIGHT);
455 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
456 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
457 }
458
459 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Start_02, TestSize.Level1)
460 {
461 ProceFunc();
462 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
463 }
464
465 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Start_03, TestSize.Level1)
466 {
467 ProceFunc();
468 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
469 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
470 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
471 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
472 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
473 }
474
475 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_Start_04, TestSize.Level1)
476 {
477 ProceFunc();
478 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
479 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
480 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
481 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
482 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
483 }
484
485 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_getOutputFormat_01, TestSize.Level1)
486 {
487 ProceFunc();
488 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
489 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
490 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
491 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
492 }
493
494 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_getOutputFormat_02, 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(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
502 }
503
504 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_SetParameter_01, 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
510 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
511 isRunning_.store(true);
512
513 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
514 EXPECT_NE(nullptr, inputLoop_);
515
516 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
517 EXPECT_NE(nullptr, outputLoop_);
518
519 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
520 while (isRunning_.load()) {
521 sleep(1); // sleep 1s
522 }
523
524 isRunning_.store(false);
525 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
526 unique_lock<mutex> lock(signal_->inMutex_);
527 signal_->inCond_.notify_all();
528 lock.unlock();
529 inputLoop_->join();
530 }
531 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
532 unique_lock<mutex> lock(signal_->outMutex_);
533 signal_->outCond_.notify_all();
534 lock.unlock();
535 outputLoop_->join();
536 }
537 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
538 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetParameter(videoDec_, format_));
539 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
540 }
541
542 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_SetParameter_02, TestSize.Level1)
543 {
544 ProceFunc();
545 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
546 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
547 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
548 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetParameter(videoDec_, format_));
549 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
550 }
551
552 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_SetParameter_03, TestSize.Level1)
553 {
554 ProceFunc();
555 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
556 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
557 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
558 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
559 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_MAX_INPUT_SIZE, 114514);
560 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetParameter(videoDec_, format_));
561 EXPECT_NE(nullptr, OH_VideoDecoder_GetOutputDescription(videoDec_));
562 }
563
564 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_01, TestSize.Level1)
565 {
566 ProceFunc();
567 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
568 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
569
570 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
571 isRunning_.store(true);
572
573 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
574 EXPECT_NE(nullptr, inputLoop_);
575
576 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
577 EXPECT_NE(nullptr, outputLoop_);
578
579 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
580 while (isRunning_.load()) {
581 sleep(1); // sleep 1s
582 }
583
584 isRunning_.store(false);
585 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
586 unique_lock<mutex> lock(signal_->inMutex_);
587 signal_->inCond_.notify_all();
588 lock.unlock();
589 inputLoop_->join();
590 }
591 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
592 unique_lock<mutex> lock(signal_->outMutex_);
593 signal_->outCond_.notify_all();
594 lock.unlock();
595 outputLoop_->join();
596 }
597 }
598
599 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_02, TestSize.Level1)
600 {
601 ProceFunc();
602 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
603 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
604
605 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
606
607 surface_ = GetSurface();
608 EXPECT_NE(nullptr, surface_);
609 OHNativeWindow *nativeWindow = CreateNativeWindowFromSurface(&surface_);
610 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetSurface(videoDec_, nativeWindow));
611
612 isRunning_.store(true);
613
614 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
615 EXPECT_NE(nullptr, inputLoop_);
616
617 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
618 EXPECT_NE(nullptr, outputLoop_);
619
620 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
621 while (isRunning_.load()) {
622 sleep(1); // sleep 1s
623 }
624
625 isRunning_.store(false);
626 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
627 unique_lock<mutex> lock(signal_->inMutex_);
628 signal_->inCond_.notify_all();
629 lock.unlock();
630 inputLoop_->join();
631 }
632 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
633 unique_lock<mutex> lock(signal_->outMutex_);
634 signal_->outCond_.notify_all();
635 lock.unlock();
636 outputLoop_->join();
637 }
638 }
639
640 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_03, TestSize.Level1)
641 {
642 ProceFunc();
643 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
644 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
645
646 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
647 isRunning_.store(true);
648 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
649 EXPECT_NE(nullptr, inputLoop_);
650 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
651 EXPECT_NE(nullptr, outputLoop_);
652
653 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
654 while (isRunning_.load()) {
655 sleep(1); // sleep 1s
656 }
657
658 isRunning_.store(false);
659 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
660 unique_lock<mutex> lock(signal_->inMutex_);
661 signal_->inCond_.notify_all();
662 lock.unlock();
663 inputLoop_->join();
664 }
665
666 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
667 unique_lock<mutex> lock(signal_->outMutex_);
668 signal_->outCond_.notify_all();
669 lock.unlock();
670 outputLoop_->join();
671 }
672 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
673 }
674
675 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_04, TestSize.Level1)
676 {
677 ProceFunc();
678 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
679 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
680
681 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
682 isRunning_.store(true);
683
684 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
685 EXPECT_NE(nullptr, inputLoop_);
686
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_Flush(videoDec_));
710 }
711
712 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_05, 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 isRunning_.store(false);
732 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
733 unique_lock<mutex> lock(signal_->inMutex_);
734 signal_->inCond_.notify_all();
735 lock.unlock();
736 inputLoop_->join();
737 }
738
739 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
740 unique_lock<mutex> lock(signal_->outMutex_);
741 signal_->outCond_.notify_all();
742 lock.unlock();
743 outputLoop_->join();
744 }
745 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
746 }
747
748 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_06, TestSize.Level1)
749 {
750 ProceFunc();
751 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
752 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
753
754 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
755 isRunning_.store(true);
756
757 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::InputFunc, this);
758 EXPECT_NE(nullptr, inputLoop_);
759 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
760 EXPECT_NE(nullptr, outputLoop_);
761
762 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
763 while (isRunning_.load()) {
764 sleep(1); // sleep 1s
765 }
766
767 isRunning_.store(false);
768 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
769 unique_lock<mutex> lock(signal_->inMutex_);
770 signal_->inCond_.notify_all();
771 lock.unlock();
772 inputLoop_->join();
773 }
774
775 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
776 unique_lock<mutex> lock(signal_->outMutex_);
777 signal_->outCond_.notify_all();
778 lock.unlock();
779 outputLoop_->join();
780 }
781 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
782 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
783 }
784
785 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_normalcase_formatchange, TestSize.Level1)
786 {
787 ProceFunc();
788 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, 1920);
789 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, 1080);
790
791 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
792
793 surface_ = GetSurface();
794 EXPECT_NE(nullptr, surface_);
795 OHNativeWindow *nativeWindow = CreateNativeWindowFromSurface(&surface_);
796 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetSurface(videoDec_, nativeWindow));
797
798 isRunning_.store(true);
799
800 inputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::FormatChangeInputFunc, this);
801 EXPECT_NE(nullptr, inputLoop_);
802
803 outputLoop_ = make_unique<thread>(&VideoCodeCapiDecoderUnitTest::OutputFunc, this);
804 EXPECT_NE(nullptr, outputLoop_);
805
806 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
807 while (isRunning_.load()) {
808 sleep(1); // sleep 1s
809 }
810
811 isRunning_.store(false);
812 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
813 unique_lock<mutex> lock(signal_->inMutex_);
814 signal_->inCond_.notify_all();
815 lock.unlock();
816 inputLoop_->join();
817 }
818 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
819 unique_lock<mutex> lock(signal_->outMutex_);
820 signal_->outCond_.notify_all();
821 lock.unlock();
822 outputLoop_->join();
823 }
824 }
825
826 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_01, TestSize.Level1)
827 {
828 EXPECT_EQ(nullptr, videoDec_);
829 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
830 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
831 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
832 }
833
834 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_02, TestSize.Level1)
835 {
836 EXPECT_EQ(nullptr, videoDec_);
837 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
838 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
839 }
840
841 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_03, TestSize.Level1)
842 {
843 EXPECT_EQ(nullptr, videoDec_);
844 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
845 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
846 }
847
848 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_04, TestSize.Level1)
849 {
850 ProceFunc();
851 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
852 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
853 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
854 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
855 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
856 }
857
858 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_05, TestSize.Level1)
859 {
860 ProceFunc();
861 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
862 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
863 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
864 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
865 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
866 }
867
868 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_06, TestSize.Level1)
869 {
870 ProceFunc();
871 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
872 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
873 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
874 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
875 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
876 }
877
878 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_07, TestSize.Level1)
879 {
880 ProceFunc();
881 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
882 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
883 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
884 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
885 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
886 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
887 }
888
889 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_abnormalcase_08, TestSize.Level1)
890 {
891 ProceFunc();
892 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
893 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
894 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
895 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
896 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
897 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
898 }
899
900 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_statuscase_01, TestSize.Level1)
901 {
902 ProceFunc();
903 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
904 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
905 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
906 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
907 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
908 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
909 }
910
911 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_statuscase_02, TestSize.Level1)
912 {
913 ProceFunc();
914 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
915 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
916 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
917 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
918 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Stop(videoDec_));
919 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
920 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Reset(videoDec_));
921 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
922 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
923 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Flush(videoDec_));
924 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
925 }
926
927 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_setcallback_01, TestSize.Level1)
928 {
929 videoDec_ = OH_VideoDecoder_CreateByName((AVCodecCodecName::VIDEO_DECODER_AVC_NAME).data());
930 EXPECT_NE(nullptr, videoDec_);
931 cb_ = {nullptr, nullptr, nullptr, nullptr};
932 signal_ = make_shared<VDecSignal>();
933 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_SetCallback(videoDec_, cb_, signal_.get()));
934 }
935
936 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_pushInputData_01, TestSize.Level1)
937 {
938 OH_AVCodecBufferAttr info = {0, 0, 0, AVCODEC_BUFFER_FLAGS_EOS};
939 int32_t bufferSize = 13571;
940 testFile_ = std::make_unique<std::ifstream>();
941 UNITTEST_CHECK_AND_RETURN_LOG(testFile_ != nullptr, "Fatal: No memory");
942
943 ProceFunc();
944 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
945 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
946 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
947 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
948
949 isRunning_.store(true);
950 testFile_->open(inputFilePath, std::ios::in | std::ios::binary);
951 unique_lock<mutex> lock(signal_->inMutex_);
__anon2bb4bed80502() 952 signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
953 uint32_t index = signal_->inQueue_.front();
954 auto buffer = signal_->inBufferQueue_.front();
955 info.size = bufferSize;
956 char *fileBuffer = static_cast<char *>(malloc(sizeof(char) * info.size + 1));
957 (void)testFile_->read(fileBuffer, info.size);
958 memcpy_s(OH_AVMemory_GetAddr(buffer), OH_AVMemory_GetSize(buffer), fileBuffer, info.size);
959 free(fileBuffer);
960 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
961 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_PushInputData(videoDec_, index, info));
962 signal_->inQueue_.pop();
963 signal_->inBufferQueue_.pop();
964
965 index = 0;
966 buffer = signal_->inBufferQueue_.front();
967 info.size = 0;
968 info.flags = AVCODEC_BUFFER_FLAGS_EOS;
969 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_PushInputData(videoDec_, index, info));
970 testFile_->close();
971 isRunning_.store(false);
972 }
973
974 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_pushInputData_02, TestSize.Level1)
975 {
976 OH_AVCodecBufferAttr info = {0, 0, 0, AVCODEC_BUFFER_FLAGS_EOS};
977 int32_t bufferSize = 13571;
978 testFile_ = std::make_unique<std::ifstream>();
979 UNITTEST_CHECK_AND_RETURN_LOG(testFile_ != nullptr, "Fatal: No memory");
980
981 ProceFunc();
982 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
983 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
984 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
985 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
986
987 isRunning_.store(true);
988 testFile_->open(inputFilePath, std::ios::in | std::ios::binary);
989 unique_lock<mutex> lock(signal_->inMutex_);
__anon2bb4bed80602() 990 signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
991 uint32_t index = 1024;
992 auto buffer = signal_->inBufferQueue_.front();
993 info.size = bufferSize;
994 char *fileBuffer = static_cast<char *>(malloc(sizeof(char) * info.size + 1));
995 (void)testFile_->read(fileBuffer, info.size);
996 memcpy_s(OH_AVMemory_GetAddr(buffer), OH_AVMemory_GetSize(buffer), fileBuffer, info.size);
997 free(fileBuffer);
998 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
999 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_PushInputData(videoDec_, index, info));
1000 signal_->inQueue_.pop();
1001 signal_->inBufferQueue_.pop();
1002
1003 testFile_->close();
1004 isRunning_.store(false);
1005 }
1006
1007 HWTEST_F(VideoCodeCapiDecoderUnitTest, videoDecoder_getOutputBuffer_01, TestSize.Level1)
1008 {
1009 ProceFunc();
1010 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
1011 OH_AVFormat_SetIntValue(format_, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
1012 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Configure(videoDec_, format_));
1013 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_VideoDecoder_Start(videoDec_));
1014 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_FreeOutputData(videoDec_, 0));
1015 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_FreeOutputData(videoDec_, -1));
1016 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_FreeOutputData(videoDec_, 1024));
1017 surface_ = GetSurface();
1018 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_RenderOutputData(videoDec_, 0));
1019 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_RenderOutputData(videoDec_, -1));
1020 EXPECT_NE(AV_ERR_OK, OH_VideoDecoder_RenderOutputData(videoDec_, 1024));
1021 }
1022 } // namespace VCodecUT
1023 } // namespace MediaAVCodec
1024 } // namespace OHOS