1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <iostream>
17 #include <cstdio>
18 #include <atomic>
19 #include <fstream>
20 #include <thread>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24
25 #include "gtest/gtest.h"
26 #include "videoenc_api11_sample.h"
27 #include "native_avcodec_videoencoder.h"
28 #include "native_avcodec_base.h"
29 #include "native_avcapability.h"
30
31 using namespace std;
32 using namespace OHOS;
33 using namespace OHOS::Media;
34 using namespace testing::ext;
35
36 namespace OHOS {
37 namespace Media {
38 class AvcSwEncReliNdkTest : public testing::Test {
39 public:
40 static void SetUpTestCase(); // 第一个测试用例执行前
41 static void TearDownTestCase(); // 最后一个测试用例执行后
42 void SetUp() override; // 每个测试用例执行前
43 void TearDown() override; // 每个测试用例执行后
44 void InputFunc();
45 void OutputFunc();
46 void Release();
47 int32_t Stop();
48
49 protected:
50 const char *inpDir720 = "/data/test/media/1280_720_nv.yuv";
51 const char *inpDir720Array[16] = {"/data/test/media/1280_720_nv.yuv", "/data/test/media/1280_720_nv12.yuv"};
52 bool createCodecSuccess_ = false;
53 };
54 } // namespace Media
55 } // namespace OHOS
56 namespace {
57 OH_AVCapability *cap = nullptr;
58 constexpr uint32_t CODEC_NAME_SIZE = 128;
59 char g_codecName[CODEC_NAME_SIZE] = {};
60 } // namespace
SetUpTestCase()61 void AvcSwEncReliNdkTest::SetUpTestCase()
62 {
63 cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, SOFTWARE);
64 const char *tmpCodecName = OH_AVCapability_GetName(cap);
65 if (memcpy_s(g_codecName, sizeof(g_codecName), tmpCodecName, strlen(tmpCodecName)) != 0)
66 cout << "memcpy failed" << endl;
67 cout << "codecname: " << g_codecName << endl;
68 }
69
TearDownTestCase()70 void AvcSwEncReliNdkTest::TearDownTestCase() {}
71
SetUp()72 void AvcSwEncReliNdkTest::SetUp() {}
73
TearDown()74 void AvcSwEncReliNdkTest::TearDown() {}
75
76 namespace {
77 /**
78 * @tc.number : VIDEO_HWENC_RELI_WHILE_0100
79 * @tc.name : 720P
80 * @tc.desc : reliable test
81 */
82 HWTEST_F(AvcSwEncReliNdkTest, VIDEO_HWENC_RELI_WHILE_0100, TestSize.Level3)
83 {
84 if (strlen(g_codecName) == 0) {
85 return;
86 }
87 shared_ptr<VEncAPI11Sample> vEncSample = make_shared<VEncAPI11Sample>();
88 vEncSample->INP_DIR = inpDir720;
89 vEncSample->DEFAULT_WIDTH = 1280;
90 vEncSample->DEFAULT_HEIGHT = 720;
91 vEncSample->DEFAULT_FRAME_RATE = 30;
92 vEncSample->OUT_DIR = "/data/test/media/1280_720_buffer.h264";
93 ASSERT_EQ(AV_ERR_OK, vEncSample->CreateVideoEncoder(g_codecName));
94 ASSERT_EQ(AV_ERR_OK, vEncSample->SetVideoEncoderCallback());
95 ASSERT_EQ(AV_ERR_OK, vEncSample->ConfigureVideoEncoder());
96 ASSERT_EQ(AV_ERR_OK, vEncSample->StartVideoEncoder());
97 vEncSample->WaitForEOS();
98 ASSERT_EQ(AV_ERR_OK, vEncSample->errCount);
99 }
100
101
102 /**
103 * @tc.number : VIDEO_HWENC_RELI_WHILE_0200
104 * @tc.name : 2 instances
105 * @tc.desc : reliable test
106 */
107 HWTEST_F(AvcSwEncReliNdkTest, VIDEO_HWENC_RELI_WHILE_0200, TestSize.Level3)
108 {
109 if (strlen(g_codecName) == 0) {
110 return;
111 }
112 vector<shared_ptr<VEncAPI11Sample>> encVec;
113 for (int i = 0; i < 2; i++) {
114 auto vEncSample = make_shared<VEncAPI11Sample>();
115 encVec.push_back(vEncSample);
116 vEncSample->INP_DIR = inpDir720Array[i];
117 vEncSample->DEFAULT_WIDTH = 1280;
118 vEncSample->DEFAULT_HEIGHT = 720;
119 vEncSample->DEFAULT_FRAME_RATE = 30;
120 vEncSample->OUT_DIR = "/data/test/media/1280_720_buffer.h264";
121 ASSERT_EQ(AV_ERR_OK, vEncSample->CreateVideoEncoder(g_codecName));
122 ASSERT_EQ(AV_ERR_OK, vEncSample->SetVideoEncoderCallback());
123 ASSERT_EQ(AV_ERR_OK, vEncSample->ConfigureVideoEncoder());
124 ASSERT_EQ(AV_ERR_OK, vEncSample->StartVideoEncoder());
125 }
126 for (int i = 0; i < 2; i++) {
127 encVec[i]->WaitForEOS();
128 }
129 encVec.clear();
130 }
131
132
133 /**
134 * @tc.number : RESET_BITRATE_RELI_0100
135 * @tc.name : reset bitrate
136 * @tc.desc : function test
137 */
138 HWTEST_F(AvcSwEncReliNdkTest, RESET_BITRATE_RELI_0100, TestSize.Level3)
139 {
140 if (strlen(g_codecName) == 0) {
141 return;
142 }
143 vector<shared_ptr<VEncAPI11Sample>> encVec;
144 for (int i = 0; i < 2; i++) {
145 auto vEncSample = make_shared<VEncAPI11Sample>();
146 encVec.push_back(vEncSample);
147 vEncSample->INP_DIR = inpDir720Array[i];
148 vEncSample->DEFAULT_WIDTH = 1280;
149 vEncSample->DEFAULT_HEIGHT = 720;
150 vEncSample->DEFAULT_FRAME_RATE = 30;
151 vEncSample->enableAutoSwitchParam = true;
152 vEncSample->needResetBitrate = true;
153 vEncSample->OUT_DIR = "/data/test/media/1280_720_buffer.h264";
154 ASSERT_EQ(AV_ERR_OK, vEncSample->CreateVideoEncoder(g_codecName));
155 ASSERT_EQ(AV_ERR_OK, vEncSample->SetVideoEncoderCallback());
156 ASSERT_EQ(AV_ERR_OK, vEncSample->ConfigureVideoEncoder());
157 ASSERT_EQ(AV_ERR_OK, vEncSample->StartVideoEncoder());
158 }
159 for (int i = 0; i < 2; i++) {
160 encVec[i]->WaitForEOS();
161 }
162 encVec.clear();
163 }
164 } // namespace