1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17 #include <osal_mem.h>
18 #include <securec.h>
19 #include <unistd.h>
20 #include "codec_callback_stub.h"
21 #include "hdf_log.h"
22 #include "hdi_mpp_ext_param_keys.h"
23 #include "icodec.h"
24 #include "share_mem.h"
25
26 #define HDF_LOG_TAG codec_hdi_uinttest
27
28 using namespace std;
29 using namespace testing::ext;
30
31 namespace {
32 constexpr const char *TEST_SERVICE_NAME = "codec_hdi_service";
33 constexpr const int TEST_PACKET_BUFFER_SIZE = 4096;
34 constexpr const int TEST_FRAME_BUFFER_SIZE = 640 * 480 * 3 / 2;
35 constexpr const uint32_t QUEUE_TIME_OUT = 10;
36 constexpr const int CAPABILITY_COUNT = 9;
37 constexpr int32_t INT_TO_STR_LEN = 32;
38 constexpr int32_t ARRAY_TO_STR_LEN = 1000;
39 struct ICodec *g_codecObj = nullptr;
40 ShareMemory g_inputBuffer;
41 ShareMemory g_outputBuffer;
42 CodecBuffer *g_inputInfoData = nullptr;
43 CodecBuffer *g_outputInfoData = nullptr;
44 CODEC_HANDLETYPE g_handle = NULL;
45
46 class CodecProxyTest : public testing::Test {
47 public:
SetUpTestCase()48 static void SetUpTestCase() {}
TearDownTestCase()49 static void TearDownTestCase() {}
SetUp()50 void SetUp() {}
TearDown()51 void TearDown() {}
52 };
53
54 static char arrayStr[ARRAY_TO_STR_LEN];
GetArrayStr(int32_t * array,int32_t arrayLen,int32_t endValue)55 static char *GetArrayStr(int32_t *array, int32_t arrayLen, int32_t endValue)
56 {
57 int32_t len = 0;
58 int32_t totalLen = 0;
59 int32_t ret;
60 char value[INT_TO_STR_LEN];
61 ret = memset_s(arrayStr, sizeof(arrayStr), 0, sizeof(arrayStr));
62 if (ret != EOK) {
63 HDF_LOGE("%{public}s: memset_s arrayStr failed, error code: %{public}d", __func__, ret);
64 return arrayStr;
65 }
66 for (int32_t i = 0; i < arrayLen; i++) {
67 if (array[i] == endValue) {
68 break;
69 }
70 ret = memset_s(value, sizeof(value), 0, sizeof(value));
71 if (ret != EOK) {
72 HDF_LOGE("%{public}s: memset_s value failed, error code: %{public}d", __func__, ret);
73 return arrayStr;
74 }
75 ret = sprintf_s(value, sizeof(value) - 1, "0x0%X, ", array[i]);
76 if (ret < 0) {
77 HDF_LOGE("%{public}s: sprintf_s value failed, error code: %{public}d", __func__, ret);
78 return arrayStr;
79 }
80 len = strlen(value);
81 ret = memcpy_s(arrayStr + totalLen, len, value, len);
82 if (ret != EOK) {
83 HDF_LOGE("%{public}s: memcpy_s arrayStr failed, error code: %{public}d", __func__, ret);
84 return arrayStr;
85 }
86 totalLen += len;
87 }
88 return arrayStr;
89 }
90
PrintCapability(CodecCapability * cap,int index)91 static void PrintCapability(CodecCapability *cap, int index)
92 {
93 int32_t mime = 0;
94 if (cap == NULL) {
95 HDF_LOGE("null capability!");
96 return;
97 }
98 mime = (int32_t)cap->mime;
99 if (mime < 0) {
100 HDF_LOGE("print invalid capability!");
101 return;
102 }
103 HDF_LOGI("-------------------------- capability %{public}d ---------------------------", index + 1);
104 HDF_LOGI("mime:%{public}d", cap->mime);
105 HDF_LOGI("type:%{public}d", cap->type);
106 HDF_LOGI("name:%{public}s", cap->name);
107 HDF_LOGI("supportProfiles:%{public}s", GetArrayStr(cap->supportProfiles, PROFILE_NUM, INVALID_PROFILE));
108 HDF_LOGI("isSoftwareCodec:%{public}d", cap->isSoftwareCodec);
109 HDF_LOGI("processModeMask:0x0%{public}x", cap->processModeMask);
110 HDF_LOGI("capsMask:0x0%{public}x", cap->capsMask);
111 HDF_LOGI("allocateMask:0x0%{public}x", cap->allocateMask);
112 HDF_LOGI("inputBufferNum.min:%{public}d", cap->inputBufferNum.min);
113 HDF_LOGI("inputBufferNum.max:%{public}d", cap->inputBufferNum.max);
114 HDF_LOGI("outputBufferNum.min:%{public}d", cap->outputBufferNum.min);
115 HDF_LOGI("outputBufferNum.max:%{public}d", cap->outputBufferNum.max);
116 HDF_LOGI("bitRate.min:%{public}d", cap->bitRate.min);
117 HDF_LOGI("bitRate.max:%{public}d", cap->bitRate.max);
118 HDF_LOGI("inputBufferSize:%{public}d", cap->inputBufferSize);
119 HDF_LOGI("outputBufferSize:%{public}d", cap->outputBufferSize);
120 if (cap->mime < MEDIA_MIMETYPE_AUDIO_FIRST) {
121 HDF_LOGI("minSize.width:%{public}d", cap->port.video.minSize.width);
122 HDF_LOGI("minSize.height:%{public}d", cap->port.video.minSize.height);
123 HDF_LOGI("maxSize.width:%{public}d", cap->port.video.maxSize.width);
124 HDF_LOGI("maxSize.height:%{public}d", cap->port.video.maxSize.height);
125 HDF_LOGI("widthAlignment:%{public}d", cap->port.video.whAlignment.widthAlignment);
126 HDF_LOGI("heightAlignment:%{public}d", cap->port.video.whAlignment.heightAlignment);
127 HDF_LOGI("supportPixFmts:%{public}s", GetArrayStr(cap->port.video.supportPixFmts, PIX_FMT_NUM, 0));
128 } else {
129 HDF_LOGI(":%{public}s", GetArrayStr(cap->port.audio.sampleFormats, SAMPLE_FORMAT_NUM, 0));
130 HDF_LOGI(":%{public}s", GetArrayStr(cap->port.audio.sampleRate, SAMPLE_RATE_NUM, 0));
131 HDF_LOGI(":%{public}s", GetArrayStr(cap->port.audio.channelLayouts, CHANNEL_NUM, -1));
132 }
133 HDF_LOGI("-------------------------------------------------------------------");
134 }
135
136 HWTEST_F(CodecProxyTest, HdfCodecHdiV1GetCodecObjTest_001, TestSize.Level1)
137 {
138 g_codecObj = HdiCodecGet(TEST_SERVICE_NAME);
139 ASSERT_TRUE(g_codecObj != nullptr);
140 }
141
142 HWTEST_F(CodecProxyTest, HdfCodecHdiV1EnumerateCapabilityTest_001, TestSize.Level1)
143 {
144 int32_t ret = HDF_SUCCESS;
145 for (int index = 0; index < CAPABILITY_COUNT; index++) {
146 CodecCapability cap;
147 ret = g_codecObj->CodecEnumerateCapability(g_codecObj, index, &cap);
148 ASSERT_EQ(ret, HDF_SUCCESS);
149 PrintCapability(&cap, index);
150 }
151 }
152
153 HWTEST_F(CodecProxyTest, HdfCodecHdiV1GetCapabilityTest_001, TestSize.Level1)
154 {
155 CodecCapability cap;
156 int32_t ret = g_codecObj->CodecGetCapability(g_codecObj, MEDIA_MIMETYPE_IMAGE_JPEG, VIDEO_DECODER, 0, &cap);
157 ASSERT_EQ(ret, HDF_SUCCESS);
158 PrintCapability(&cap, 0);
159 }
160
161 HWTEST_F(CodecProxyTest, HdfCodecHdiV1InitCodecTest_001, TestSize.Level1)
162 {
163 ASSERT_TRUE(g_codecObj != nullptr);
164 int32_t errorCode = g_codecObj->CodecInit(g_codecObj);
165 ASSERT_EQ(errorCode, HDF_SUCCESS);
166 }
167
168 HWTEST_F(CodecProxyTest, HdfCodecHdiV1CreateCodecTest_001, TestSize.Level1)
169 {
170 const char* name = "codec.avc.hardware.decoder";
171 int32_t errorCode = g_codecObj->CodecCreate(g_codecObj, name, &g_handle);
172 ASSERT_EQ(errorCode, HDF_SUCCESS);
173 ASSERT_TRUE(g_handle != nullptr);
174 }
175
176 HWTEST_F(CodecProxyTest, HdfCodecHdiV1CreateCodecByTypeTest_001, TestSize.Level1)
177 {
178 CodecType type = VIDEO_DECODER;
179 AvCodecMime mime = MEDIA_MIMETYPE_VIDEO_AVC;
180 int32_t errorCode = g_codecObj->CodecCreateByType(g_codecObj, type, mime, &g_handle);
181 ASSERT_EQ(errorCode, HDF_ERR_NOT_SUPPORT);
182 }
183
184 HWTEST_F(CodecProxyTest, HdfCodecHdiV1SetPortModeTest_001, TestSize.Level1)
185 {
186 DirectionType direct = OUTPUT_TYPE;
187 AllocateBufferMode mode = ALLOCATE_INPUT_BUFFER_CODEC_PRESET;
188 BufferType type = BUFFER_TYPE_FD;
189 int32_t errorCode = g_codecObj->CodecSetPortMode(g_codecObj, g_handle, direct, mode, type);
190 ASSERT_EQ(errorCode, HDF_ERR_NOT_SUPPORT);
191 }
192
193 HWTEST_F(CodecProxyTest, HdfCodecHdiV1GetPortModeTest_001, TestSize.Level1)
194 {
195 DirectionType direct = OUTPUT_TYPE;
196 AllocateBufferMode mode;
197 BufferType type;
198 int32_t errorCode = g_codecObj->CodecGetPortMode(g_codecObj, g_handle, direct, &mode, &type);
199 ASSERT_EQ(errorCode, HDF_ERR_NOT_SUPPORT);
200 }
201
202 HWTEST_F(CodecProxyTest, HdfCodecHdiV1SetCodecTypeTest_001, TestSize.Level1)
203 {
204 Param *params;
205 int paramCnt = 1;
206 params = (Param *)OsalMemAlloc(sizeof(Param)*paramCnt);
207 ASSERT_TRUE(params != nullptr);
208 params->key = KEY_CODEC_TYPE;
209 CodecType ct = VIDEO_DECODER;
210 params->val = (void *)&ct;
211 params->size = sizeof(ct);
212
213 int32_t errorCode = g_codecObj->CodecSetParameter(g_codecObj, g_handle, params, paramCnt);
214 OsalMemFree(params);
215 ASSERT_EQ(errorCode, HDF_SUCCESS);
216 }
217
218 HWTEST_F(CodecProxyTest, HdfCodecHdiV1GetDefaultCfgTest_001, TestSize.Level1)
219 {
220 Param *params;
221 int paramCnt = 1;
222 params = (Param *)OsalMemAlloc(sizeof(Param)*paramCnt);
223 ASSERT_TRUE(params != nullptr);
224 params->key = (ParamKey)ParamExtKey::KEY_EXT_DEFAULT_CFG_RK;
225 params->val = nullptr;
226 params->size = 0;
227
228 int32_t errorCode = g_codecObj->CodecGetParameter(g_codecObj, g_handle, params, paramCnt);
229 OsalMemFree(params);
230 ASSERT_EQ(errorCode, HDF_SUCCESS);
231 }
232
233 HWTEST_F(CodecProxyTest, HdfCodecHdiV1SetSplitModeTest_001, TestSize.Level1)
234 {
235 Param *params;
236 int paramCnt = 1;
237 params = (Param *)OsalMemAlloc(sizeof(Param)*paramCnt);
238 ASSERT_TRUE(params != nullptr);
239 params->key = (ParamKey)ParamExtKey::KEY_EXT_SPLIT_PARSE_RK;
240 int32_t needSplit = 1;
241 params->val = (void *)&needSplit;
242 params->size = sizeof(needSplit);
243
244 int32_t errorCode = g_codecObj->CodecSetParameter(g_codecObj, g_handle, params, paramCnt);
245 OsalMemFree(params);
246 ASSERT_EQ(errorCode, HDF_SUCCESS);
247 }
248
249 HWTEST_F(CodecProxyTest, HdfCodecHdiV1QueueInputTest_001, TestSize.Level1)
250 {
251 g_inputBuffer.id = 0;
252 g_inputBuffer.size = TEST_PACKET_BUFFER_SIZE;
253 int32_t ret = CreateFdShareMemory(&g_inputBuffer);
254 ASSERT_EQ(ret, HDF_SUCCESS);
255
256 g_inputInfoData = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * 1);
257 g_inputInfoData->bufferId = 0;
258 g_inputInfoData->bufferCnt = 1;
259 g_inputInfoData->flag = 1;
260 g_inputInfoData->timeStamp = 1;
261 g_inputInfoData->buffer[0].type = BUFFER_TYPE_FD;
262 g_inputInfoData->buffer[0].offset = 1;
263 g_inputInfoData->buffer[0].length = 1;
264 g_inputInfoData->buffer[0].capacity = TEST_PACKET_BUFFER_SIZE;
265 g_inputInfoData->buffer[0].buf = (intptr_t)g_inputBuffer.fd;
266
267 int32_t errorCode = g_codecObj->CodecQueueInput(g_codecObj,
268 g_handle, g_inputInfoData, (uint32_t)QUEUE_TIME_OUT, -1);
269 ASSERT_EQ(errorCode, HDF_SUCCESS);
270 }
271
272 HWTEST_F(CodecProxyTest, HdfCodecHdiV1DequeInputTest_001, TestSize.Level1)
273 {
274 int32_t acquireFd;
275 CodecBuffer *inputInfo = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * 1);
276 inputInfo->bufferCnt = 1;
277
278 int32_t errorCode = g_codecObj->CodecDequeueInput(g_codecObj, g_handle, QUEUE_TIME_OUT, &acquireFd, inputInfo);
279 ASSERT_EQ(errorCode, HDF_SUCCESS);
280 ASSERT_EQ(inputInfo->bufferId, g_inputInfoData->bufferId);
281 ASSERT_EQ(inputInfo->bufferCnt, g_inputInfoData->bufferCnt);
282 ASSERT_EQ(inputInfo->buffer[0].type, g_inputInfoData->buffer[0].type);
283 ASSERT_EQ(inputInfo->buffer[0].offset, g_inputInfoData->buffer[0].offset);
284 ASSERT_EQ(inputInfo->buffer[0].length, g_inputInfoData->buffer[0].length);
285 ASSERT_EQ(inputInfo->buffer[0].capacity, g_inputInfoData->buffer[0].capacity);
286 OsalMemFree(inputInfo);
287 OsalMemFree(g_inputInfoData);
288 }
289
290 HWTEST_F(CodecProxyTest, HdfCodecHdiV1StartCodecTest_001, TestSize.Level1)
291 {
292 int32_t errorCode = g_codecObj->CodecStart(g_codecObj, g_handle);
293 ASSERT_EQ(errorCode, HDF_SUCCESS);
294 }
295
296 HWTEST_F(CodecProxyTest, HdfCodecHdiV1StopCodecTest_001, TestSize.Level1)
297 {
298 int32_t errorCode = g_codecObj->CodecStop(g_codecObj, g_handle);
299 ASSERT_EQ(errorCode, HDF_SUCCESS);
300 }
301
302 HWTEST_F(CodecProxyTest, HdfCodecHdiV1ResetCodecTest_001, TestSize.Level1)
303 {
304 int32_t errorCode = g_codecObj->CodecReset(g_codecObj, g_handle);
305 ASSERT_EQ(errorCode, HDF_ERR_NOT_SUPPORT);
306 }
307
308 HWTEST_F(CodecProxyTest, HdfCodecHdiV1QueueOutputTest_001, TestSize.Level1)
309 {
310 g_outputBuffer.id = 1;
311 g_outputBuffer.size = TEST_FRAME_BUFFER_SIZE;
312 int32_t ret = CreateFdShareMemory(&g_outputBuffer);
313 ASSERT_EQ(ret, HDF_SUCCESS);
314
315 g_outputInfoData = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * 1);
316 g_outputInfoData->bufferId = 1;
317 g_outputInfoData->bufferCnt = 1;
318 g_outputInfoData->flag = 1;
319 g_outputInfoData->timeStamp = 1;
320 g_outputInfoData->buffer[0].type = BUFFER_TYPE_FD;
321 g_outputInfoData->buffer[0].offset = 1;
322 g_outputInfoData->buffer[0].length = 1;
323 g_outputInfoData->buffer[0].capacity = TEST_FRAME_BUFFER_SIZE;
324 g_outputInfoData->buffer[0].buf = (intptr_t)g_outputBuffer.fd;
325
326 int32_t errorCode = g_codecObj->CodecQueueOutput(g_codecObj, g_handle, g_outputInfoData, (uint32_t)0, -1);
327 ASSERT_EQ(errorCode, HDF_SUCCESS);
328 }
329
330 HWTEST_F(CodecProxyTest, HdfCodecHdiV1DequeueOutputTest_001, TestSize.Level1)
331 {
332 int32_t errorCode = 0;
333 int32_t acquireFd;
334 CodecBuffer *outInfo = (CodecBuffer *)OsalMemAlloc(sizeof(CodecBuffer) + sizeof(CodecBufferInfo) * 1);
335 outInfo->bufferCnt = 1;
336
337 errorCode = g_codecObj->CodecDequeueOutput(g_codecObj, g_handle, QUEUE_TIME_OUT, &acquireFd, outInfo);
338 ASSERT_EQ(errorCode, HDF_SUCCESS);
339 ASSERT_EQ(outInfo->bufferId, g_outputInfoData->bufferId);
340 ASSERT_EQ(outInfo->bufferCnt, g_outputInfoData->bufferCnt);
341 ASSERT_EQ(outInfo->buffer[0].type, g_outputInfoData->buffer[0].type);
342 ASSERT_EQ(outInfo->buffer[0].offset, g_outputInfoData->buffer[0].offset);
343 ASSERT_EQ(outInfo->buffer[0].length, g_outputInfoData->buffer[0].length);
344 ASSERT_EQ(outInfo->buffer[0].capacity, g_outputInfoData->buffer[0].capacity);
345 OsalMemFree(outInfo);
346 OsalMemFree(g_outputInfoData);
347 }
348
349 HWTEST_F(CodecProxyTest, HdfCodecHdiV1FlushTest_001, TestSize.Level1)
350 {
351 DirectionType directType = OUTPUT_TYPE;
352
353 int32_t errorCode = g_codecObj->CodecFlush(g_codecObj, g_handle, directType);
354 ASSERT_EQ(errorCode, HDF_SUCCESS);
355 ASSERT_EQ(directType, OUTPUT_TYPE);
356 }
357
358 HWTEST_F(CodecProxyTest, HdfCodecHdiV1SetCallbackTest_001, TestSize.Level1)
359 {
360 UINTPTR instance = 0;
361 ICodecCallback *callback = CodecCallbackStubObtain();
362 ASSERT_TRUE(callback != nullptr);
363
364 int32_t errorCode = g_codecObj->CodecSetCallback(g_codecObj, g_handle, callback, instance);
365 ASSERT_EQ(errorCode, HDF_SUCCESS);
366 }
367
368 HWTEST_F(CodecProxyTest, HdfCodecHdiV1DestroyCodecTest_001, TestSize.Level1)
369 {
370 int32_t errorCode = g_codecObj->CodecDestroy(g_codecObj, g_handle);
371 ASSERT_EQ(errorCode, HDF_SUCCESS);
372 }
373
374 HWTEST_F(CodecProxyTest, HdfCodecHdiV1DeinitTest_001, TestSize.Level1)
375 {
376 int32_t errorCode = g_codecObj->CodecDeinit(g_codecObj);
377 ASSERT_EQ(errorCode, HDF_SUCCESS);
378 HdiCodecRelease(g_codecObj);
379 }
380 }
381