1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <iostream>
16 #include <cstdio>
17 #include <string>
18
19 #include "gtest/gtest.h"
20 #include "avcodec_common.h"
21 #include "meta/format.h"
22 #include "avcodec_video_encoder.h"
23 #include "videoenc_ndk_inner_sample.h"
24 #include "native_avcapability.h"
25
26 using namespace std;
27 using namespace OHOS;
28 using namespace OHOS::MediaAVCodec;
29 using namespace testing::ext;
30
31 namespace {
32 class HwEncInnerApiNdkTest : public testing::Test {
33 public:
34 // SetUpTestCase: Called before all test cases
35 static void SetUpTestCase(void);
36 // TearDownTestCase: Called after all test case
37 static void TearDownTestCase(void);
38 // SetUp: Called before each test cases
39 void SetUp() override;
40 // TearDown: Called after each test cases
41 void TearDown() override;
42 };
43
44 constexpr uint32_t DEFAULT_WIDTH = 1920;
45 constexpr uint32_t DEFAULT_HEIGHT = 1080;
46 std::string g_codecMime = "video/avc";
47 std::string g_codecName = "";
48 std::shared_ptr<AVCodecVideoEncoder> venc_ = nullptr;
49 std::shared_ptr<VEncInnerSignal> signal_ = nullptr;
50
SetUpTestCase()51 void HwEncInnerApiNdkTest::SetUpTestCase()
52 {
53 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory(g_codecMime.c_str(), true, HARDWARE);
54 const char *tmpCodecName = OH_AVCapability_GetName(cap);
55 g_codecName = tmpCodecName;
56 cout << "g_codecName: " << g_codecName << endl;
57 }
58
TearDownTestCase()59 void HwEncInnerApiNdkTest::TearDownTestCase() {}
60
SetUp()61 void HwEncInnerApiNdkTest::SetUp()
62 {
63 signal_ = make_shared<VEncInnerSignal>();
64 }
65
TearDown()66 void HwEncInnerApiNdkTest::TearDown()
67 {
68 if (venc_ != nullptr) {
69 venc_->Release();
70 venc_ = nullptr;
71 }
72
73 if (signal_) {
74 signal_ = nullptr;
75 }
76 }
77 } // namespace
78
79 namespace {
80 /**
81 * @tc.number : VIDEO_ENCODE_ILLEGAL_PARA_0100
82 * @tc.name : CreateByMime para1 error
83 * @tc.desc : param test
84 */
85 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0100, TestSize.Level2)
86 {
87 venc_ = VideoEncoderFactory::CreateByMime("");
88 ASSERT_EQ(nullptr, venc_);
89 }
90
91 /**
92 * @tc.number : VIDEO_ENCODE_ILLEGAL_PARA_0200
93 * @tc.name : CreateByMime para2 error
94 * @tc.desc : param test
95 */
96 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0200, TestSize.Level2)
97 {
98 venc_ = VideoEncoderFactory::CreateByMime("");
99 ASSERT_EQ(nullptr, venc_);
100 }
101
102 /**
103 * @tc.number : VIDEO_ENCODE_ILLEGAL_PARA_0300
104 * @tc.name : CreateByName para1 error
105 * @tc.desc : param test
106 */
107 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0300, TestSize.Level2)
108 {
109 venc_ = VideoEncoderFactory::CreateByName("");
110 ASSERT_EQ(nullptr, venc_);
111 }
112
113 /**
114 * @tc.number : VIDEO_ENCODE_ILLEGAL_PARA_0400
115 * @tc.name : CreateByName para2 error
116 * @tc.desc : param test
117 */
118 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0400, TestSize.Level2)
119 {
120 venc_ = VideoEncoderFactory::CreateByName("");
121 ASSERT_EQ(nullptr, venc_);
122 }
123
124 /**
125 * @tc.number : VIDEO_ENCODE_ILLEGAL_PARA_0500
126 * @tc.name : SetCallback para error
127 * @tc.desc : param test
128 */
129 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0500, TestSize.Level2)
130 {
131 venc_ = VideoEncoderFactory::CreateByMime(g_codecMime);
132 ASSERT_NE(nullptr, venc_);
133
134 std::shared_ptr<VEncInnerCallback> cb_ = make_shared<VEncInnerCallback>(nullptr);
135 int32_t ret = venc_->SetCallback(cb_);
136 ASSERT_EQ(AVCS_ERR_OK, ret);
137 }
138
139 /**
140 * @tc.number : VIDEO_ENCODE_ILLEGAL_PARA_0600
141 * @tc.name : Configure para not enough
142 * @tc.desc : param test
143 */
144 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0600, TestSize.Level2)
145 {
146 venc_ = VideoEncoderFactory::CreateByMime(g_codecMime);
147 ASSERT_NE(nullptr, venc_);
148
149 Format format;
150 format.PutIntValue(MediaDescriptionKey::MD_KEY_BITRATE, 100000);
151 int32_t ret = venc_->Configure(format);
152 ASSERT_EQ(ret, AVCS_ERR_INVALID_VAL);
153 }
154
155 /**
156 * @tc.number : VIDEO_ENCODE_ILLEGAL_PARA_0700
157 * @tc.name : ReleaseOutputBuffer para error
158 * @tc.desc : param test
159 */
160 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0700, TestSize.Level2)
161 {
162 venc_ = VideoEncoderFactory::CreateByMime(g_codecMime);
163 ASSERT_NE(nullptr, venc_);
164
165 Format format;
166 format.PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, 1080);
167 format.PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, 1080);
168 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::YUVI420));
169
170 ASSERT_EQ(AVCS_ERR_OK, venc_->Configure(format));
171 ASSERT_EQ(AVCS_ERR_OK, venc_->Start());
172 usleep(1000000);
173 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->ReleaseOutputBuffer(9999999));
174 }
175
176 /**
177 * @tc.number : VIDEO_ENCODE_ILLEGAL_PARA_0800
178 * @tc.name : QueueInputBuffer para error
179 * @tc.desc : param test
180 */
181 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_ILLEGAL_PARA_0800, TestSize.Level2)
182 {
183 venc_ = VideoEncoderFactory::CreateByMime(g_codecMime);
184 ASSERT_NE(nullptr, venc_);
185
186 AVCodecBufferInfo info;
187 info.presentationTimeUs = -1;
188 info.size = -1;
189 info.offset = 0;
190 AVCodecBufferFlag flag = AVCODEC_BUFFER_FLAG_EOS;
191 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->QueueInputBuffer(0, info, flag));
192 }
193
194 /**
195 * @tc.number : VIDEO_ENCODE_API_0100
196 * @tc.name : CreateByName CreateByName
197 * @tc.desc : api test
198 */
199 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0100, TestSize.Level2)
200 {
201 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
202 ASSERT_NE(nullptr, venc_);
203
204 std::shared_ptr<AVCodecVideoEncoder> venc2_ = VideoEncoderFactory::CreateByName(g_codecName);
205 ASSERT_NE(nullptr, venc_);
206 ASSERT_EQ(AVCS_ERR_OK, venc2_->Release());
207 venc2_ = nullptr;
208 }
209
210 /**
211 * @tc.number : VIDEO_ENCODE_API_0200
212 * @tc.name : CreateByName configure configure
213 * @tc.desc : api test
214 */
215 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0200, TestSize.Level2)
216 {
217 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
218 ASSERT_NE(nullptr, venc_);
219
220 Format format;
221 string widthStr = "width";
222 string heightStr = "height";
223 string frameRateStr = "frame_rate";
224 format.PutIntValue(widthStr.c_str(), DEFAULT_WIDTH);
225 format.PutIntValue(heightStr.c_str(), DEFAULT_HEIGHT);
226 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
227
228 ASSERT_EQ(AVCS_ERR_OK, venc_->Configure(format));
229 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->Configure(format));
230 }
231
232 /**
233 * @tc.number : VIDEO_ENCODE_API_0300
234 * @tc.name : CreateByName configure start start
235 * @tc.desc : api test
236 */
237 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0300, TestSize.Level2)
238 {
239 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
240 ASSERT_NE(nullptr, venc_);
241
242 Format format;
243 string widthStr = "width";
244 string heightStr = "height";
245 string frameRateStr = "frame_rate";
246 format.PutIntValue(widthStr.c_str(), DEFAULT_WIDTH);
247 format.PutIntValue(heightStr.c_str(), DEFAULT_HEIGHT);
248 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
249
250 ASSERT_EQ(AVCS_ERR_OK, venc_->Configure(format));
251 ASSERT_EQ(AVCS_ERR_OK, venc_->Start());
252 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->Start());
253 }
254
255 /**
256 * @tc.number : VIDEO_ENCODE_API_0400
257 * @tc.name : CreateByName configure start stop stop
258 * @tc.desc : api test
259 */
260 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0400, TestSize.Level2)
261 {
262 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
263 ASSERT_NE(nullptr, venc_);
264
265 Format format;
266 string widthStr = "width";
267 string heightStr = "height";
268 string frameRateStr = "frame_rate";
269 format.PutIntValue(widthStr.c_str(), DEFAULT_WIDTH);
270 format.PutIntValue(heightStr.c_str(), DEFAULT_HEIGHT);
271 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
272
273 ASSERT_EQ(AVCS_ERR_OK, venc_->Configure(format));
274 ASSERT_EQ(AVCS_ERR_OK, venc_->Start());
275 ASSERT_EQ(AVCS_ERR_OK, venc_->Stop());
276 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->Stop());
277 }
278
279 /**
280 * @tc.number : VIDEO_ENCODE_API_0500
281 * @tc.name : CreateByName configure start stop reset reset
282 * @tc.desc : api test
283 */
284 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0500, TestSize.Level2)
285 {
286 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
287 ASSERT_NE(nullptr, venc_);
288
289 Format format;
290 string widthStr = "width";
291 string heightStr = "height";
292 string frameRateStr = "frame_rate";
293 format.PutIntValue(widthStr.c_str(), DEFAULT_WIDTH);
294 format.PutIntValue(heightStr.c_str(), DEFAULT_HEIGHT);
295 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
296
297 ASSERT_EQ(AVCS_ERR_OK, venc_->Configure(format));
298 ASSERT_EQ(AVCS_ERR_OK, venc_->Start());
299 ASSERT_EQ(AVCS_ERR_OK, venc_->Stop());
300 ASSERT_EQ(AVCS_ERR_OK, venc_->Reset());
301 ASSERT_EQ(AVCS_ERR_OK, venc_->Reset());
302 }
303
304 /**
305 * @tc.number : VIDEO_ENCODE_API_0600
306 * @tc.name : CreateByName configure start EOS EOS
307 * @tc.desc : api test
308 */
309 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0600, TestSize.Level2)
310 {
311 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
312 ASSERT_NE(nullptr, venc_);
313
314 Format format;
315 string widthStr = "width";
316 string heightStr = "height";
317 string frameRateStr = "frame_rate";
318 format.PutIntValue(widthStr.c_str(), DEFAULT_WIDTH);
319 format.PutIntValue(heightStr.c_str(), DEFAULT_HEIGHT);
320 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
321 ASSERT_EQ(AVCS_ERR_OK, venc_->Configure(format));
322
323 std::shared_ptr<VEncInnerCallback> cb_ = make_shared<VEncInnerCallback>(signal_);
324 ASSERT_EQ(AVCS_ERR_OK, venc_->SetCallback(cb_));
325 ASSERT_EQ(AVCS_ERR_OK, venc_->Start());
326
327 unique_lock<mutex> lock(signal_->inMutex_);
__anonf093d0c90302null328 signal_->inCond_.wait(lock, [] { return signal_->inIdxQueue_.size() > 1; });
329 uint32_t index = signal_->inIdxQueue_.front();
330 AVCodecBufferInfo info;
331 info.presentationTimeUs = 0;
332 info.size = 0;
333 info.offset = 0;
334 AVCodecBufferFlag flag = AVCODEC_BUFFER_FLAG_EOS;
335
336 ASSERT_EQ(AVCS_ERR_OK, venc_->QueueInputBuffer(index, info, flag));
337 signal_->inIdxQueue_.pop();
338 index = signal_->inIdxQueue_.front();
339 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->QueueInputBuffer(index, info, flag));
340 signal_->inIdxQueue_.pop();
341 }
342
343 /**
344 * @tc.number : VIDEO_ENCODE_API_0700
345 * @tc.name : CreateByName configure start flush flush
346 * @tc.desc : api test
347 */
348 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0700, TestSize.Level2)
349 {
350 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
351 ASSERT_NE(nullptr, venc_);
352
353 Format format;
354 string widthStr = "width";
355 string heightStr = "height";
356 string frameRateStr = "frame_rate";
357 format.PutIntValue(widthStr.c_str(), DEFAULT_WIDTH);
358 format.PutIntValue(heightStr.c_str(), DEFAULT_HEIGHT);
359 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
360
361 ASSERT_EQ(AVCS_ERR_OK, venc_->Configure(format));
362 ASSERT_EQ(AVCS_ERR_OK, venc_->Start());
363 ASSERT_EQ(AVCS_ERR_OK, venc_->Flush());
364 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->Flush());
365 }
366
367 /**
368 * @tc.number : VIDEO_ENCODE_API_0800
369 * @tc.name : CreateByName configure start stop release
370 * @tc.desc : api test
371 */
372 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0800, TestSize.Level2)
373 {
374 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
375 ASSERT_NE(nullptr, venc_);
376
377 Format format;
378 string widthStr = "width";
379 string heightStr = "height";
380 string frameRateStr = "frame_rate";
381 format.PutIntValue(widthStr.c_str(), DEFAULT_WIDTH);
382 format.PutIntValue(heightStr.c_str(), DEFAULT_HEIGHT);
383 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
384
385 ASSERT_EQ(AVCS_ERR_OK, venc_->Configure(format));
386 ASSERT_EQ(AVCS_ERR_OK, venc_->Start());
387 ASSERT_EQ(AVCS_ERR_OK, venc_->Stop());
388 ASSERT_EQ(AVCS_ERR_OK, venc_->Release());
389 venc_ = nullptr;
390 }
391
392 /**
393 * @tc.number : VIDEO_ENCODE_API_0900
394 * @tc.name : CreateByMime CreateByMime
395 * @tc.desc : api test
396 */
397 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_0900, TestSize.Level2)
398 {
399 venc_ = VideoEncoderFactory::CreateByMime(g_codecMime);
400 ASSERT_NE(nullptr, venc_);
401
402 std::shared_ptr<AVCodecVideoEncoder> venc2_ = VideoEncoderFactory::CreateByMime(g_codecMime);
403 ASSERT_NE(nullptr, venc_);
404
405 ASSERT_EQ(AVCS_ERR_OK, venc2_->Release());
406 venc2_ = nullptr;
407 }
408
409 /**
410 * @tc.number : VIDEO_ENCODE_API_1000
411 * @tc.name : repeat SetCallback
412 * @tc.desc : api test
413 */
414 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_1000, TestSize.Level2)
415 {
416 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
417 ASSERT_NE(nullptr, venc_);
418
419 std::shared_ptr<VEncInnerCallback> cb_ = make_shared<VEncInnerCallback>(nullptr);
420 ASSERT_EQ(AVCS_ERR_OK, venc_->SetCallback(cb_));
421 ASSERT_EQ(AVCS_ERR_OK, venc_->SetCallback(cb_));
422 }
423
424 /**
425 * @tc.number : VIDEO_ENCODE_API_1100
426 * @tc.name : repeat GetOutputFormat
427 * @tc.desc : api test
428 */
429 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_1100, TestSize.Level2)
430 {
431 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
432 ASSERT_NE(nullptr, venc_);
433
434 Format format;
435 ASSERT_EQ(AVCS_ERR_OK, venc_->GetOutputFormat(format));
436 ASSERT_EQ(AVCS_ERR_OK, venc_->GetOutputFormat(format));
437 }
438
439 /**
440 * @tc.number : VIDEO_ENCODE_API_1200
441 * @tc.name : repeat SetParameter
442 * @tc.desc : api test
443 */
444 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_1200, TestSize.Level2)
445 {
446 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
447 ASSERT_NE(nullptr, venc_);
448
449 Format format;
450 string widthStr = "width";
451 string heightStr = "height";
452 string frameRateStr = "frame_rate";
453 format.PutIntValue(widthStr.c_str(), DEFAULT_WIDTH);
454 format.PutIntValue(heightStr.c_str(), DEFAULT_HEIGHT);
455 format.PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
456
457 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->SetParameter(format));
458 ASSERT_EQ(AVCS_ERR_INVALID_STATE, venc_->SetParameter(format));
459 }
460
461 /**
462 * @tc.number : VIDEO_ENCODE_API_1300
463 * @tc.name : repeat GetInputFormat
464 * @tc.desc : api test
465 */
466 HWTEST_F(HwEncInnerApiNdkTest, VIDEO_ENCODE_API_1300, TestSize.Level2)
467 {
468 venc_ = VideoEncoderFactory::CreateByName(g_codecName);
469 ASSERT_NE(nullptr, venc_);
470
471 Format format;
472 int32_t ret = venc_->GetInputFormat(format);
473 ASSERT_EQ(AVCS_ERR_OK, ret);
474
475 ret = venc_->GetInputFormat(format);
476 ASSERT_EQ(AVCS_ERR_OK, ret);
477 }
478 } // namespace