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 "gtest/gtest.h"
17
18 #include "native_avcodec_base.h"
19 #include "native_avdemuxer.h"
20 #include "native_avformat.h"
21 #include "native_avsource.h"
22 #include "native_avmemory.h"
23
24 #include <iostream>
25 #include <cstdio>
26 #include <string>
27 #include <fcntl.h>
28 #include <cmath>
29 #include <thread>
30 namespace OHOS {
31 namespace Media {
32 class DemuxerFunc3NdkTest : 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(void);
40 // TearDown: Called after each test cases
41 void TearDown(void);
42 };
43
44 static OH_AVMemory *memory = nullptr;
45 static OH_AVSource *source = nullptr;
46 static OH_AVDemuxer *demuxer = nullptr;
47 static OH_AVFormat *sourceFormat = nullptr;
48 static OH_AVFormat *trackFormat = nullptr;
49 static OH_AVBuffer *avBuffer = nullptr;
50 static OH_AVFormat *format = nullptr;
51 static OH_AVFormat *metaFormat = nullptr;
52 static int32_t g_trackCount;
53 static int32_t g_width = 3840;
54 static int32_t g_height = 2160;
55 constexpr int FLV_AUDIONUM_AAC = 5148;
56 constexpr int FLV_AUDIONUM_HEVC_AAC = 210;
57 constexpr int FLV_VIDEONUM_HEVC_AAC_ALL = 602;
58 constexpr int FLV_AUDIONUM_AVC_AAC = 318;
59 constexpr int32_t GOPNUM = 60;
SetUpTestCase()60 void DemuxerFunc3NdkTest::SetUpTestCase() {}
TearDownTestCase()61 void DemuxerFunc3NdkTest::TearDownTestCase() {}
SetUp()62 void DemuxerFunc3NdkTest::SetUp()
63 {
64 memory = OH_AVMemory_Create(g_width * g_height);
65 g_trackCount = 0;
66 }
TearDown()67 void DemuxerFunc3NdkTest::TearDown()
68 {
69 if (trackFormat != nullptr) {
70 OH_AVFormat_Destroy(trackFormat);
71 trackFormat = nullptr;
72 }
73
74 if (sourceFormat != nullptr) {
75 OH_AVFormat_Destroy(sourceFormat);
76 sourceFormat = nullptr;
77 }
78 if (format != nullptr) {
79 OH_AVFormat_Destroy(format);
80 format = nullptr;
81 }
82
83 if (memory != nullptr) {
84 OH_AVMemory_Destroy(memory);
85 memory = nullptr;
86 }
87 if (source != nullptr) {
88 OH_AVSource_Destroy(source);
89 source = nullptr;
90 }
91 if (demuxer != nullptr) {
92 OH_AVDemuxer_Destroy(demuxer);
93 demuxer = nullptr;
94 }
95 if (avBuffer != nullptr) {
96 OH_AVBuffer_Destroy(avBuffer);
97 avBuffer = nullptr;
98 }
99 if (metaFormat != nullptr) {
100 OH_AVFormat_Destroy(metaFormat);
101 metaFormat = nullptr;
102 }
103 }
104 } // namespace Media
105 } // namespace OHOS
106
107 using namespace std;
108 using namespace OHOS;
109 using namespace OHOS::Media;
110 using namespace testing::ext;
111
GetFileSize(const char * fileName)112 static int64_t GetFileSize(const char *fileName)
113 {
114 int64_t fileSize = 0;
115 if (fileName != nullptr) {
116 struct stat fileStatus {};
117 if (stat(fileName, &fileStatus) == 0) {
118 fileSize = static_cast<int64_t>(fileStatus.st_size);
119 }
120 }
121 return fileSize;
122 }
123
SetAudioValue(OH_AVCodecBufferAttr attr,bool & audioIsEnd,int & audioFrame,int & aKeyCount)124 static void SetAudioValue(OH_AVCodecBufferAttr attr, bool &audioIsEnd, int &audioFrame, int &aKeyCount)
125 {
126 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
127 audioIsEnd = true;
128 cout << audioFrame << " audio is end !!!!!!!!!!!!!!!" << endl;
129 } else {
130 audioFrame++;
131 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
132 aKeyCount++;
133 }
134 }
135 }
136
SetVideoValue(OH_AVCodecBufferAttr attr,bool & videoIsEnd,int & videoFrame,int & vKeyCount)137 static void SetVideoValue(OH_AVCodecBufferAttr attr, bool &videoIsEnd, int &videoFrame, int &vKeyCount)
138 {
139 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
140 videoIsEnd = true;
141 cout << videoFrame << " video is end !!!!!!!!!!!!!!!" << endl;
142 } else {
143 videoFrame++;
144 cout << "video track !!!!!" << endl;
145 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
146 vKeyCount++;
147 }
148 }
149 }
150
InitFile(const char * file,int32_t trackNum,int & fd,bool & initResult)151 static void InitFile(const char *file, int32_t trackNum, int &fd, bool &initResult)
152 {
153 fd = open(file, O_RDONLY);
154 int64_t size = GetFileSize(file);
155 cout << file << "----------------------" << fd << "---------" << size << endl;
156 source = OH_AVSource_CreateWithFD(fd, 0, size);
157 ASSERT_NE(source, nullptr);
158
159 demuxer = OH_AVDemuxer_CreateWithSource(source);
160 ASSERT_NE(demuxer, nullptr);
161
162 sourceFormat = OH_AVSource_GetSourceFormat(source);
163 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
164 ASSERT_EQ(trackNum, g_trackCount);
165 for (int32_t index = 0; index < g_trackCount; index++) {
166 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
167 }
168 avBuffer = OH_AVBuffer_Create(size);
169 ASSERT_NE(avBuffer, nullptr);
170 initResult = true;
171 }
172
CheckVideoSyncFrame(int & vKeyCount,int32_t & gopCount,int32_t & count)173 static bool CheckVideoSyncFrame(int &vKeyCount, int32_t &gopCount, int32_t &count)
174 {
175 if (gopCount % GOPNUM == 0 && vKeyCount == count + 1) {
176 return true;
177 } else if (gopCount % GOPNUM != 0 && vKeyCount == count) {
178 return true;
179 } else {
180 return false;
181 }
182 }
183
CheckVideoFirstSyncFrame(int32_t & gopCount,int & vKeyCount)184 static bool CheckVideoFirstSyncFrame(int32_t &gopCount, int &vKeyCount)
185 {
186 if (gopCount == 0 && vKeyCount == 1) {
187 return true;
188 } else if (gopCount != 0 && vKeyCount == 1) {
189 return true;
190 } else {
191 return false;
192 }
193 }
194 /**
195 * @tc.number : DEMUXER_FUNCTION_FLV_0010
196 * @tc.name : create source with fd, avc+aac gop60 flv
197 * @tc.desc : function test
198 */
199 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_FUNCTION_FLV_0010, TestSize.Level0)
200 {
201 int tarckType = 0;
202 bool audioIsEnd = false;
203 bool videoIsEnd = false;
204 int audioFrame = 0;
205 int videoFrame = 0;
206 OH_AVCodecBufferAttr bufferAttr;
207 const char *file = "/data/test/media/avc_aac_60.flv";
208 int fd = 0;
209 bool initResult = false;
210 InitFile(file, 2, fd, initResult);
211 ASSERT_TRUE(initResult);
212 int aKeyCount = 0;
213 int vKeyCount = 0;
214 int32_t gopCount = 0;
215 int32_t count = 0;
216 while (!audioIsEnd || !videoIsEnd) {
217 for (int32_t index = 0; index < g_trackCount; index++) {
218 trackFormat = OH_AVSource_GetTrackFormat(source, index);
219 ASSERT_NE(trackFormat, nullptr);
220 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
221 OH_AVFormat_Destroy(trackFormat);
222 trackFormat = nullptr;
223 if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
224 continue;
225 }
226 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSampleBuffer(demuxer, index, avBuffer));
227 ASSERT_NE(avBuffer, nullptr);
228 ASSERT_EQ(AV_ERR_OK, OH_AVBuffer_GetBufferAttr(avBuffer, &bufferAttr));
229 if (tarckType == MEDIA_TYPE_AUD) {
230 SetAudioValue(bufferAttr, audioIsEnd, audioFrame, aKeyCount);
231 } else if (tarckType == MEDIA_TYPE_VID) {
232 count = vKeyCount;
233 SetVideoValue(bufferAttr, videoIsEnd, videoFrame, vKeyCount);
234 ASSERT_TRUE(CheckVideoSyncFrame(vKeyCount, gopCount, count));
235 gopCount++;
236 }
237 }
238 }
239 ASSERT_EQ(aKeyCount, FLV_AUDIONUM_AVC_AAC);
240 close(fd);
241 }
242
243 /**
244 * @tc.number : DEMUXER_FUNCTION_FLV_0020
245 * @tc.name : create source with fd, hevc+aac gop1 flv
246 * @tc.desc : function test
247 */
248 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_FUNCTION_FLV_0020, TestSize.Level0)
249 {
250 int tarckType = 0;
251 bool audioIsEnd = false;
252 bool videoIsEnd = false;
253 int audioFrame = 0;
254 int videoFrame = 0;
255 OH_AVCodecBufferAttr bufferAttr;
256 const char *file = "/data/test/media/hevc_aac_1.flv";
257 int fd = 0;
258 bool initResult = false;
259 InitFile(file, 2, fd, initResult);
260 ASSERT_TRUE(initResult);
261 int aKeyCount = 0;
262 int vKeyCount = 0;
263 while (!audioIsEnd || !videoIsEnd) {
264 for (int32_t index = 0; index < g_trackCount; index++) {
265 trackFormat = OH_AVSource_GetTrackFormat(source, index);
266 ASSERT_NE(trackFormat, nullptr);
267 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
268 OH_AVFormat_Destroy(trackFormat);
269 trackFormat = nullptr;
270 if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
271 continue;
272 }
273 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSampleBuffer(demuxer, index, avBuffer));
274 ASSERT_NE(avBuffer, nullptr);
275 ASSERT_EQ(AV_ERR_OK, OH_AVBuffer_GetBufferAttr(avBuffer, &bufferAttr));
276 if (tarckType == MEDIA_TYPE_AUD) {
277 SetAudioValue(bufferAttr, audioIsEnd, audioFrame, aKeyCount);
278 } else if (tarckType == MEDIA_TYPE_VID) {
279 SetVideoValue(bufferAttr, videoIsEnd, videoFrame, vKeyCount);
280 }
281 }
282 }
283 ASSERT_EQ(aKeyCount, FLV_AUDIONUM_HEVC_AAC);
284 ASSERT_EQ(vKeyCount, FLV_VIDEONUM_HEVC_AAC_ALL);
285 close(fd);
286 }
287
288 /**
289 * @tc.number : DEMUXER_FUNCTION_FLV_0030
290 * @tc.name : create source with fd, hevc+aac gop-1 flv
291 * @tc.desc : function test
292 */
293 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_FUNCTION_FLV_0030, TestSize.Level0)
294 {
295 int tarckType = 0;
296 bool audioIsEnd = false;
297 bool videoIsEnd = false;
298 int audioFrame = 0;
299 int videoFrame = 0;
300 OH_AVCodecBufferAttr bufferAttr;
301 const char *file = "/data/test/media/hevc_aac_first.flv";
302 int fd = 0;
303 bool initResult = false;
304 InitFile(file, 2, fd, initResult);
305 ASSERT_TRUE(initResult);
306 int aKeyCount = 0;
307 int vKeyCount = 0;
308 int32_t gopCount = 0;
309 while (!audioIsEnd || !videoIsEnd) {
310 for (int32_t index = 0; index < g_trackCount; index++) {
311 trackFormat = OH_AVSource_GetTrackFormat(source, index);
312 ASSERT_NE(trackFormat, nullptr);
313 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
314 OH_AVFormat_Destroy(trackFormat);
315 trackFormat = nullptr;
316 if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
317 continue;
318 }
319 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSampleBuffer(demuxer, index, avBuffer));
320 ASSERT_NE(avBuffer, nullptr);
321 ASSERT_EQ(AV_ERR_OK, OH_AVBuffer_GetBufferAttr(avBuffer, &bufferAttr));
322 if (tarckType == MEDIA_TYPE_AUD) {
323 SetAudioValue(bufferAttr, audioIsEnd, audioFrame, aKeyCount);
324 } else if (tarckType == MEDIA_TYPE_VID) {
325 SetVideoValue(bufferAttr, videoIsEnd, videoFrame, vKeyCount);
326 ASSERT_TRUE(CheckVideoFirstSyncFrame(gopCount, vKeyCount));
327 gopCount++;
328 }
329 }
330 }
331 ASSERT_EQ(aKeyCount, FLV_AUDIONUM_HEVC_AAC);
332 ASSERT_EQ(vKeyCount, 1);
333 close(fd);
334 }
335
336 /**
337 * @tc.number : DEMUXER_FUNCTION_FLV_0040
338 * @tc.name : create source with fd, avc gop60 flv
339 * @tc.desc : function test
340 */
341 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_FUNCTION_FLV_0040, TestSize.Level2)
342 {
343 int tarckType = 0;
344 OH_AVCodecBufferAttr bufferAttr;
345 bool videoIsEnd = false;
346 int videoFrame = 0;
347 const char *file = "/data/test/media/avc_60.flv";
348 int fd = 0;
349 bool initResult = false;
350 InitFile(file, 1, fd, initResult);
351 ASSERT_TRUE(initResult);
352 int vKeyCount = 0;
353 int32_t gopCount = 0;
354 int32_t count = 0;
355 while (!videoIsEnd) {
356 for (int32_t index = 0; index < g_trackCount; index++) {
357 trackFormat = OH_AVSource_GetTrackFormat(source, index);
358 ASSERT_NE(trackFormat, nullptr);
359 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
360 OH_AVFormat_Destroy(trackFormat);
361 trackFormat = nullptr;
362 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSampleBuffer(demuxer, index, avBuffer));
363 ASSERT_NE(avBuffer, nullptr);
364 ASSERT_EQ(AV_ERR_OK, OH_AVBuffer_GetBufferAttr(avBuffer, &bufferAttr));
365 if (tarckType == MEDIA_TYPE_VID) {
366 count = vKeyCount;
367 SetVideoValue(bufferAttr, videoIsEnd, videoFrame, vKeyCount);
368 ASSERT_TRUE(CheckVideoSyncFrame(vKeyCount, gopCount, count));
369 gopCount++;
370 }
371 }
372 }
373 close(fd);
374 }
375
376 /**
377 * @tc.number : DEMUXER_FUNCTION_FLV_0050
378 * @tc.name : create source with fd, aac flv
379 * @tc.desc : function test
380 */
381 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_FUNCTION_FLV_0050, TestSize.Level2)
382 {
383 int tarckType = 0;
384 bool audioIsEnd = false;
385 int audioFrame = 0;
386 OH_AVCodecBufferAttr bufferAttr;
387 const char *file = "/data/test/media/only_aac.flv";
388 int fd = 0;
389 bool initResult = false;
390 InitFile(file, 1, fd, initResult);
391 ASSERT_TRUE(initResult);
392 int aKeyCount = 0;
393 while (!audioIsEnd) {
394 for (int32_t index = 0; index < g_trackCount; index++) {
395 trackFormat = OH_AVSource_GetTrackFormat(source, index);
396 ASSERT_NE(trackFormat, nullptr);
397 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
398 OH_AVFormat_Destroy(trackFormat);
399 trackFormat = nullptr;
400 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSampleBuffer(demuxer, index, avBuffer));
401 ASSERT_NE(avBuffer, nullptr);
402 ASSERT_EQ(AV_ERR_OK, OH_AVBuffer_GetBufferAttr(avBuffer, &bufferAttr));
403 if (tarckType == MEDIA_TYPE_AUD) {
404 SetAudioValue(bufferAttr, audioIsEnd, audioFrame, aKeyCount);
405 }
406 }
407 }
408 ASSERT_EQ(aKeyCount, FLV_AUDIONUM_AAC);
409 close(fd);
410 }
411
412 /**
413 * @tc.number : DEMUXER_META_0010
414 * @tc.name : demuxer meta info, get value with right key
415 * @tc.desc : function test
416 */
417 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0010, TestSize.Level1)
418 {
419 int32_t metaIntValue = 0;
420 const char *file = "/data/test/media/metaIntval.mp4";
421 int fd = open(file, O_RDONLY);
422 int64_t size = GetFileSize(file);
423 cout << file << "----------------------" << fd << "---------" << size << endl;
424 source = OH_AVSource_CreateWithFD(fd, 0, size);
425 ASSERT_NE(source, nullptr);
426 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
427 ASSERT_NE(metaFormat, nullptr);
428 int32_t metaNum = 101010101;
429 ASSERT_TRUE(OH_AVFormat_GetIntValue(metaFormat, "com.openharmony.intval.intvalintval", &metaIntValue));
430 ASSERT_EQ(metaIntValue, metaNum);
431 close(fd);
432 }
433
434 /**
435 * @tc.number : DEMUXER_META_0020
436 * @tc.name : demuxer meta info, get value with right key
437 * @tc.desc : function test
438 */
439 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0020, TestSize.Level1)
440 {
441 float metaFloatValue = 0.0;
442 const char *file = "/data/test/media/metaFloatval.mp4";
443 int fd = open(file, O_RDONLY);
444 int64_t size = GetFileSize(file);
445 cout << file << "----------------------" << fd << "---------" << size << endl;
446 source = OH_AVSource_CreateWithFD(fd, 0, size);
447 ASSERT_NE(source, nullptr);
448 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
449 ASSERT_NE(metaFormat, nullptr);
450 float metaFloatVal = 2.3;
451 ASSERT_TRUE(OH_AVFormat_GetFloatValue(metaFormat, "com.openharmony.floatval.aaa", &metaFloatValue));
452 ASSERT_EQ(metaFloatValue, metaFloatVal);
453 metaFloatVal = 25.3;
454 ASSERT_TRUE(OH_AVFormat_GetFloatValue(metaFormat, "com.openharmony.floatval.bbb", &metaFloatValue));
455 ASSERT_EQ(metaFloatValue, metaFloatVal);
456 metaFloatVal = 252.3;
457 ASSERT_TRUE(OH_AVFormat_GetFloatValue(metaFormat, "com.openharmony.floatval.ccc", &metaFloatValue));
458 ASSERT_EQ(metaFloatValue, metaFloatVal);
459 metaFloatVal = 2525.3;
460 ASSERT_TRUE(OH_AVFormat_GetFloatValue(metaFormat, "com.openharmony.floatval.ddd", &metaFloatValue));
461 ASSERT_EQ(metaFloatValue, metaFloatVal);
462 metaFloatVal = 25252.3;
463 ASSERT_TRUE(OH_AVFormat_GetFloatValue(metaFormat, "com.openharmony.floatval.eee", &metaFloatValue));
464 ASSERT_EQ(metaFloatValue, metaFloatVal);
465 close(fd);
466 }
467 /**
468 * @tc.number : DEMUXER_META_0030
469 * @tc.name : demuxer meta info, get value with right key
470 * @tc.desc : function test
471 */
472 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0030, TestSize.Level1)
473 {
474 const char *file = "/data/test/media/metaStringval.mp4";
475 int fd = open(file, O_RDONLY);
476 int64_t size = GetFileSize(file);
477 cout << file << "----------------------" << fd << "---------" << size << endl;
478 source = OH_AVSource_CreateWithFD(fd, 0, size);
479 ASSERT_NE(source, nullptr);
480 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
481 ASSERT_NE(metaFormat, nullptr);
482 const char* metaStringValue = nullptr;
483 string metaKeyAdd = "aba";
484 string metaKey = "com.openharmony.stringval.";
485 string metaVal = "aaaaa";
486 string metaValAdd = "b";
487 for (int i = 0; i < 100; i++) {
488 metaKey.append(metaKeyAdd);
489 metaVal.append(metaValAdd);
490 ASSERT_TRUE(OH_AVFormat_GetStringValue(metaFormat, metaKey.c_str(), &metaStringValue));
491 ASSERT_EQ(metaStringValue, metaVal);
492 }
493 close(fd);
494 }
495 /**
496 * @tc.number : DEMUXER_META_0040
497 * @tc.name : demuxer meta info, get value with right key
498 * @tc.desc : function test
499 */
500 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0040, TestSize.Level2)
501 {
502 const char* metaStringValue = nullptr;
503 int32_t metaIntValue = 0;
504 float metaFloatValue = 0.0;
505 const char *file = "/data/test/media/metaAlltype.mp4";
506 int fd = open(file, O_RDONLY);
507 int64_t size = GetFileSize(file);
508 cout << file << "----------------------" << fd << "---------" << size << endl;
509 source = OH_AVSource_CreateWithFD(fd, 0, size);
510 ASSERT_NE(source, nullptr);
511 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
512 ASSERT_NE(metaFormat, nullptr);
513 string metaKeyAdd = "a";
514 string metaKey = "com.openharmony.stringval.";
515 string metaVal = "aaaaa";
516 string metaValAdd = "b";
517 for (int i = 0; i < 50; i++) {
518 metaKey.append(metaKeyAdd);
519 metaVal.append(metaValAdd);
520 ASSERT_TRUE(OH_AVFormat_GetStringValue(metaFormat, metaKey.c_str(), &metaStringValue));
521 ASSERT_EQ(metaStringValue, metaVal);
522 }
523
524 metaKeyAdd = "a";
525 metaKey = "com.openharmony.floatval.";
526 float metaFloatVal = 123.5;
527 for (int i = 0; i < 50; i++) {
528 metaKey.append(metaKeyAdd);
529 metaFloatVal = metaFloatVal + 2;
530 ASSERT_TRUE(OH_AVFormat_GetFloatValue(metaFormat, metaKey.c_str(), &metaFloatValue));
531 ASSERT_EQ(metaFloatValue, metaFloatVal);
532 }
533 metaKeyAdd = "a";
534 metaKey = "com.openharmony.Intval.";
535 int32_t metaIntVal = 123;
536 for (int i = 0; i < 50; i++) {
537 metaKey.append(metaKeyAdd);
538 metaIntVal = metaIntVal + 2;
539 ASSERT_TRUE(OH_AVFormat_GetIntValue(metaFormat, metaKey.c_str(), &metaIntValue));
540 ASSERT_EQ(metaIntValue, metaIntVal);
541 }
542 close(fd);
543 }
544 /**
545 * @tc.number : DEMUXER_META_0050
546 * @tc.name : demuxer meta info, get value with error key
547 * @tc.desc : function test
548 */
549 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0050, TestSize.Level2)
550 {
551 const char* metaStringValue = nullptr;
552 int32_t metaIntValue = 0;
553 float metaFloatValue = 0.0;
554 const char *file = "/data/test/media/metaAlltype.mp4";
555 int fd = open(file, O_RDONLY);
556 int64_t size = GetFileSize(file);
557 cout << file << "----------------------" << fd << "---------" << size << endl;
558 source = OH_AVSource_CreateWithFD(fd, 0, size);
559 ASSERT_NE(source, nullptr);
560 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
561 ASSERT_NE(metaFormat, nullptr);
562 ASSERT_FALSE(OH_AVFormat_GetStringValue(metaFormat, "com.openharmony.stringval.abb", &metaStringValue));
563 ASSERT_FALSE(OH_AVFormat_GetIntValue(metaFormat, "com.openharmony.intnum.abb", &metaIntValue));
564 ASSERT_FALSE(OH_AVFormat_GetFloatValue(metaFormat, "com.openharmony.floatval.abb", &metaFloatValue));
565 close(fd);
566 }
567
568 /**
569 * @tc.number : DEMUXER_META_0060
570 * @tc.name : demuxer meta info, get value with error key type
571 * @tc.desc : function test
572 */
573 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0060, TestSize.Level2)
574 {
575 const char* metaStringValue = nullptr;
576 int32_t metaIntValue = 0;
577 float metaFloatValue = 0.0;
578 const char *file = "/data/test/media/metaAlltype.mp4";
579 int fd = open(file, O_RDONLY);
580 int64_t size = GetFileSize(file);
581 cout << file << "----------------------" << fd << "---------" << size << endl;
582 source = OH_AVSource_CreateWithFD(fd, 0, size);
583 ASSERT_NE(source, nullptr);
584 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
585 ASSERT_NE(metaFormat, nullptr);
586 ASSERT_FALSE(OH_AVFormat_GetStringValue(metaFormat, "com.openharmony.floatval.a", &metaStringValue));
587 ASSERT_FALSE(OH_AVFormat_GetIntValue(metaFormat, "com.openharmony.stringval.a", &metaIntValue));
588 ASSERT_FALSE(OH_AVFormat_GetFloatValue(metaFormat, "com.openharmony.intnum.a", &metaFloatValue));
589 close(fd);
590 }
591
592 /**
593 * @tc.number : DEMUXER_META_0070
594 * @tc.name : demuxer meta info, file with no meta
595 * @tc.desc : function test
596 */
597 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0070, TestSize.Level2)
598 {
599 const char *file = "/data/test/media/m4v_fmp4.mp4";
600 int fd = open(file, O_RDONLY);
601 int64_t size = GetFileSize(file);
602 cout << file << "----------------------" << fd << "---------" << size << endl;
603 source = OH_AVSource_CreateWithFD(fd, 0, size);
604 ASSERT_NE(source, nullptr);
605 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
606 ASSERT_NE(metaFormat, nullptr);
607 const char* language = OH_AVFormat_DumpInfo(metaFormat);
608 ASSERT_EQ(language, nullptr);
609 close(fd);
610 }
611
612 /**
613 * @tc.number : DEMUXER_META_0080
614 * @tc.name : demuxer meta info, get value with right key
615 * @tc.desc : function test
616 */
617 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0080, TestSize.Level3)
618 {
619 const char *file = "/data/test/media/double_hevc.mp4";
620 int fd = open(file, O_RDONLY);
621 int64_t size = GetFileSize(file);
622 cout << file << "----------------------" << fd << "---------" << size << endl;
623 source = OH_AVSource_CreateWithFD(fd, 0, size);
624 ASSERT_NE(source, nullptr);
625 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
626 ASSERT_NE(metaFormat, nullptr);
627 string metamanufacturer = "ABCDEF";
628 string metamarketingName = "AABBAABBAABBAABBAA";
629 string metamodel = "ABABABAB";
630 string metaversion = "12";
631 const char* manufacturer;
632 ASSERT_TRUE(OH_AVFormat_GetStringValue(metaFormat, "com.abababa.manufacturer", &manufacturer));
633 ASSERT_EQ(manufacturer, metamanufacturer);
634 const char* marketingName;
635 ASSERT_TRUE(OH_AVFormat_GetStringValue(metaFormat, "com.abababa.marketing_name", &marketingName));
636 ASSERT_EQ(marketingName, metamarketingName);
637 const char* model;
638 ASSERT_TRUE(OH_AVFormat_GetStringValue(metaFormat, "com.abababa.model", &model));
639 ASSERT_EQ(model, metamodel);
640 const char* version;
641 ASSERT_TRUE(OH_AVFormat_GetStringValue(metaFormat, "com.abababa.version", &version));
642 ASSERT_EQ(version, metaversion);
643 close(fd);
644 }
645
646 /**
647 * @tc.number : DEMUXER_META_0090
648 * @tc.name : demuxer meta info, souce is null
649 * @tc.desc : api test
650 */
651 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0090, TestSize.Level0)
652 {
653 metaFormat= OH_AVSource_GetCustomMetadataFormat(nullptr);
654 ASSERT_EQ(metaFormat, nullptr);
655 }
656
657 /**
658 * @tc.number : DEMUXER_META_0100
659 * @tc.name : demuxer meta info, get max value
660 * @tc.desc : function test
661 */
662 HWTEST_F(DemuxerFunc3NdkTest, DEMUXER_META_0100, TestSize.Level2)
663 {
664 const char* metaStringValue = nullptr;
665 int32_t metaIntValue = 0;
666 float metaFloatValue = 0.0;
667 const char *file = "/data/test/media/metaMaxval.mp4";
668 int fd = open(file, O_RDONLY);
669 int64_t size = GetFileSize(file);
670 cout << file << "----------------------" << fd << "---------" << size << endl;
671 source = OH_AVSource_CreateWithFD(fd, 0, size);
672 ASSERT_NE(source, nullptr);
673 metaFormat= OH_AVSource_GetCustomMetadataFormat(source);
674 ASSERT_NE(metaFormat, nullptr);
675 ASSERT_FALSE(OH_AVFormat_GetStringValue(metaFormat, "com.openharmony.stringval", &metaStringValue));
676 float floatValue = 3.4028235E38;
677 ASSERT_TRUE(OH_AVFormat_GetFloatValue(metaFormat, "com.openharmony.floatval.eee", &metaFloatValue));
678 ASSERT_EQ(floatValue, metaFloatValue);
679 int32_t intValue = 2147483647;
680 ASSERT_TRUE(OH_AVFormat_GetIntValue(metaFormat, "com.openharmony.intval.aaaa", &metaIntValue));
681 ASSERT_EQ(metaIntValue, intValue);
682 close(fd);
683 }