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 "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 {
31 constexpr uint32_t CODEC_NAME_SIZE = 128;
32 char g_codecNameHEVC[CODEC_NAME_SIZE] = {};
33 }
34 namespace OHOS {
35 namespace Media {
36 class DemuxerProcNdkTest : public testing::Test {
37 public:
38 // SetUpTestCase: Called before all test cases
39 static void SetUpTestCase(void);
40 // TearDownTestCase: Called after all test case
41 static void TearDownTestCase(void);
42 // SetUp: Called before each test cases
43 void SetUp(void);
44 // TearDown: Called after each test cases
45 void TearDown(void);
46 };
47
48 static OH_AVMemory *memory = nullptr;
49 static OH_AVSource *source = nullptr;
50 static OH_AVDemuxer *demuxer = nullptr;
51 static OH_AVFormat *sourceFormat = nullptr;
52 static OH_AVFormat *trackFormat = nullptr;
53 static OH_AVBuffer *avBuffer = nullptr;
54 static OH_AVFormat *format = nullptr;
55
56 static int32_t g_trackCount;
57 static int32_t g_width = 3840;
58 static int32_t g_height = 2160;
59 constexpr int64_t START_TIME_NUM = 5011;
60
61
SetUpTestCase()62 void DemuxerProcNdkTest::SetUpTestCase() {}
TearDownTestCase()63 void DemuxerProcNdkTest::TearDownTestCase() {}
SetUp()64 void DemuxerProcNdkTest::SetUp()
65 {
66 memory = OH_AVMemory_Create(g_width * g_height);
67 g_trackCount = 0;
68 }
TearDown()69 void DemuxerProcNdkTest::TearDown()
70 {
71 if (trackFormat != nullptr) {
72 OH_AVFormat_Destroy(trackFormat);
73 trackFormat = nullptr;
74 }
75
76 if (sourceFormat != nullptr) {
77 OH_AVFormat_Destroy(sourceFormat);
78 sourceFormat = nullptr;
79 }
80
81 if (memory != nullptr) {
82 OH_AVMemory_Destroy(memory);
83 memory = nullptr;
84 }
85 if (source != nullptr) {
86 OH_AVSource_Destroy(source);
87 source = nullptr;
88 }
89 if (demuxer != nullptr) {
90 OH_AVDemuxer_Destroy(demuxer);
91 demuxer = nullptr;
92 }
93 if (avBuffer != nullptr) {
94 OH_AVBuffer_Destroy(avBuffer);
95 avBuffer = nullptr;
96 }
97 if (format != nullptr) {
98 OH_AVFormat_Destroy(format);
99 format = nullptr;
100 }
101 }
102 } // namespace Media
103 } // namespace OHOS
104
105 using namespace std;
106 using namespace OHOS;
107 using namespace OHOS::Media;
108 using namespace testing::ext;
109
GetFileSize(const char * fileName)110 static int64_t GetFileSize(const char *fileName)
111 {
112 int64_t fileSize = 0;
113 if (fileName != nullptr) {
114 struct stat fileStatus {};
115 if (stat(fileName, &fileStatus) == 0) {
116 fileSize = static_cast<int64_t>(fileStatus.st_size);
117 }
118 }
119 return fileSize;
120 }
121
SetAudioValue(OH_AVCodecBufferAttr attr,bool & audioIsEnd,int & audioFrame,int & aKeyCount)122 static void SetAudioValue(OH_AVCodecBufferAttr attr, bool &audioIsEnd, int &audioFrame, int &aKeyCount)
123 {
124 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
125 audioIsEnd = true;
126 cout << audioFrame << " audio is end !!!!!!!!!!!!!!!" << endl;
127 } else {
128 audioFrame++;
129 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
130 aKeyCount++;
131 }
132 }
133 }
134
SetVideoValue(OH_AVCodecBufferAttr attr,bool & videoIsEnd,int & videoFrame,int & vKeyCount)135 static void SetVideoValue(OH_AVCodecBufferAttr attr, bool &videoIsEnd, int &videoFrame, int &vKeyCount)
136 {
137 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
138 videoIsEnd = true;
139 cout << videoFrame << " video is end !!!!!!!!!!!!!!!" << endl;
140 } else {
141 videoFrame++;
142 cout << "video track !!!!!" << endl;
143 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
144 vKeyCount++;
145 }
146 }
147 }
148
149 /**
150 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_1400
151 * @tc.name : demuxer video and 2 audio file
152 * @tc.desc : function test
153 */
154 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_1400, TestSize.Level0)
155 {
156 int tarckType = 0;
157 int auidoTrackCount = 2;
158 OH_AVCodecBufferAttr attr;
159 bool videoIsEnd = false;
160 int videoFrame = 0;
161 const char *file = "/data/test/media/video_2audio.mp4";
162 const char* mimeType = nullptr;
163 int fd = open(file, O_RDONLY);
164 int64_t size = GetFileSize(file);
165 source = OH_AVSource_CreateWithFD(fd, 0, size);
166 ASSERT_NE(source, nullptr);
167 demuxer = OH_AVDemuxer_CreateWithSource(source);
168 ASSERT_NE(demuxer, nullptr);
169 sourceFormat = OH_AVSource_GetSourceFormat(source);
170 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
171 ASSERT_EQ(auidoTrackCount + 1, g_trackCount);
172 for (int32_t index = 0; index < g_trackCount; index++) {
173 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
174 }
175 int vKeyCount = 0;
176 int aKeyCount[2] = {};
177 int audioFrame[2] = {};
178 bool audioIsEnd = false;
179 while (!audioIsEnd || !videoIsEnd) {
180 for (int32_t index = 0; index < g_trackCount; index++) {
181 trackFormat = OH_AVSource_GetTrackFormat(source, index);
182 ASSERT_NE(trackFormat, nullptr);
183 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
184 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
185 continue;
186 }
187 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
188 if (tarckType == 1) {
189 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
190 } else if (tarckType == 0) {
191 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
192 ASSERT_NE(0, strcmp(mimeType, OH_AVCODEC_MIMETYPE_AUDIO_RAW));
193 SetAudioValue(attr, audioIsEnd, audioFrame[index-1], aKeyCount[index-1]);
194 }
195 }
196 }
197 for (int index = 0; index < auidoTrackCount; index++) {
198 ASSERT_EQ(audioFrame[index], 433);
199 ASSERT_EQ(aKeyCount[index], 433);
200 }
201 ASSERT_EQ(videoFrame, 602);
202 ASSERT_EQ(vKeyCount, 3);
203 close(fd);
204 }
205
206 /**
207 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_1500
208 * @tc.name : demuxer video and 9 audio file
209 * @tc.desc : function test
210 */
211 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_1500, TestSize.Level0)
212 {
213 int tarckType = 0;
214 int auidoTrackCount = 9;
215 OH_AVCodecBufferAttr attr;
216 bool videoIsEnd = false;
217 int videoFrame = 0;
218 const char *file = "/data/test/media/video_9audio.mp4";
219 int fd = open(file, O_RDONLY);
220 int64_t size = GetFileSize(file);
221 cout << file << "----------------------" << fd << "---------" << size << endl;
222 source = OH_AVSource_CreateWithFD(fd, 0, size);
223 ASSERT_NE(source, nullptr);
224 demuxer = OH_AVDemuxer_CreateWithSource(source);
225 ASSERT_NE(demuxer, nullptr);
226 sourceFormat = OH_AVSource_GetSourceFormat(source);
227 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
228 ASSERT_EQ(auidoTrackCount + 1, g_trackCount);
229 for (int32_t index = 0; index < g_trackCount; index++) {
230 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
231 }
232 int vKeyCount = 0;
233 int aKeyCount[9] = {};
234 int audioFrame[9] = {};
235 bool audioIsEnd = false;
236 while (!audioIsEnd || !videoIsEnd) {
237 for (int32_t index = 0; index < g_trackCount; index++) {
238 trackFormat = OH_AVSource_GetTrackFormat(source, index);
239 ASSERT_NE(trackFormat, nullptr);
240 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
241 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
242 continue;
243 }
244 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
245 if (tarckType == 1) {
246 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
247 } else if (tarckType == 0) {
248 SetAudioValue(attr, audioIsEnd, audioFrame[index-1], aKeyCount[index-1]);
249 }
250 }
251 }
252 for (int index = 0; index < auidoTrackCount; index++) {
253 ASSERT_EQ(audioFrame[index], 433);
254 ASSERT_EQ(aKeyCount[index], 433);
255 }
256 ASSERT_EQ(videoFrame, 602);
257 ASSERT_EQ(vKeyCount, 3);
258 close(fd);
259 }
260
261 /**
262 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_1600
263 * @tc.name : demuxer avc+MP3 flv video file
264 * @tc.desc : function test
265 */
266 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_1600, TestSize.Level0)
267 {
268 int tarckType = 0;
269 OH_AVCodecBufferAttr attr;
270 bool videoIsEnd = false;
271 int videoFrame = 0;
272 const char *file = "/data/test/media/avc_mp3.flv";
273 int fd = open(file, O_RDONLY);
274 int64_t size = GetFileSize(file);
275 cout << file << "----------------------" << fd << "---------" << size << endl;
276 source = OH_AVSource_CreateWithFD(fd, 0, size);
277 ASSERT_NE(source, nullptr);
278 demuxer = OH_AVDemuxer_CreateWithSource(source);
279 ASSERT_NE(demuxer, nullptr);
280 sourceFormat = OH_AVSource_GetSourceFormat(source);
281 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
282 ASSERT_EQ(2, g_trackCount);
283 for (int32_t index = 0; index < g_trackCount; index++) {
284 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
285 }
286 int vKeyCount = 0;
287 int aKeyCount = 0;
288 int audioFrame = 0;
289 bool audioIsEnd = false;
290 while (!audioIsEnd || !videoIsEnd) {
291 for (int32_t index = 0; index < g_trackCount; index++) {
292 trackFormat = OH_AVSource_GetTrackFormat(source, index);
293 ASSERT_NE(trackFormat, nullptr);
294 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
295 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
296 continue;
297 }
298 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
299 if (tarckType == 1) {
300 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
301 } else if (tarckType == 0) {
302 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
303 }
304 }
305 }
306 ASSERT_EQ(audioFrame, 385);
307 ASSERT_EQ(aKeyCount, 385);
308 ASSERT_EQ(videoFrame, 602);
309 ASSERT_EQ(vKeyCount, 3);
310 close(fd);
311 }
312
313 /**
314 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_1700
315 * @tc.name : demuxer hevc+pcm flv video file
316 * @tc.desc : function test
317 */
318 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_1700, TestSize.Level0)
319 {
320 int tarckType = 0;
321 OH_AVCodecBufferAttr attr;
322 bool videoIsEnd = false;
323 int videoFrame = 0;
324 const char *file = "/data/test/media/hevc_pcm_a.flv";
325 int fd = open(file, O_RDONLY);
326 int64_t size = GetFileSize(file);
327 cout << file << "----------------------" << fd << "---------" << size << endl;
328 source = OH_AVSource_CreateWithFD(fd, 0, size);
329 ASSERT_NE(source, nullptr);
330 demuxer = OH_AVDemuxer_CreateWithSource(source);
331 ASSERT_NE(demuxer, nullptr);
332 sourceFormat = OH_AVSource_GetSourceFormat(source);
333 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
334 ASSERT_EQ(2, g_trackCount);
335 for (int32_t index = 0; index < g_trackCount; index++) {
336 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
337 }
338 int vKeyCount = 0;
339 int aKeyCount = 0;
340 int audioFrame = 0;
341 bool audioIsEnd = false;
342 while (!audioIsEnd || !videoIsEnd) {
343 for (int32_t index = 0; index < g_trackCount; index++) {
344 trackFormat = OH_AVSource_GetTrackFormat(source, index);
345 ASSERT_NE(trackFormat, nullptr);
346 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
347 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
348 continue;
349 }
350 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
351 if (tarckType == 1) {
352 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
353 } else if (tarckType == 0) {
354 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
355 }
356 }
357 }
358 ASSERT_EQ(audioFrame, 385);
359 ASSERT_EQ(aKeyCount, 385);
360 ASSERT_EQ(videoFrame, 602);
361 ASSERT_EQ(vKeyCount, 3);
362 close(fd);
363 }
364
365 /**
366 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_1800
367 * @tc.name : demuxer damaged flv video file
368 * @tc.desc : function test
369 */
370 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_1800, TestSize.Level2)
371 {
372 int tarckType = 0;
373 OH_AVCodecBufferAttr attr;
374 bool videoIsEnd = false;
375 int videoFrame = 0;
376 const char *file = "/data/test/media/avc_mp3_error.flv";
377 int fd = open(file, O_RDONLY);
378 int64_t size = GetFileSize(file);
379 cout << file << "----------------------" << fd << "---------" << size << endl;
380 source = OH_AVSource_CreateWithFD(fd, 0, size);
381 ASSERT_NE(source, nullptr);
382 demuxer = OH_AVDemuxer_CreateWithSource(source);
383 ASSERT_NE(demuxer, nullptr);
384 sourceFormat = OH_AVSource_GetSourceFormat(source);
385 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
386 ASSERT_EQ(2, g_trackCount);
387 for (int32_t index = 0; index < g_trackCount; index++) {
388 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
389 }
390 int vKeyCount = 0;
391 int aKeyCount = 0;
392 int audioFrame = 0;
393 bool audioIsEnd = false;
394 while (!audioIsEnd || !videoIsEnd) {
395 for (int32_t index = 0; index < g_trackCount; index++) {
396 trackFormat = OH_AVSource_GetTrackFormat(source, index);
397 ASSERT_NE(trackFormat, nullptr);
398 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
399 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
400 continue;
401 }
402 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
403 if (tarckType == 1) {
404 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
405 } else if (tarckType == 0) {
406 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
407 }
408 }
409 }
410 close(fd);
411 }
412
413 /**
414 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_1900
415 * @tc.name : demuxer damaged ape audio file
416 * @tc.desc : function test
417 */
418 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_1900, TestSize.Level2)
419 {
420 OH_AVCodecBufferAttr attr;
421 const char* mimeType = nullptr;
422 bool audioIsEnd = false;
423 int audioFrame = 0;
424 const char *file = "/data/test/media/audio/ape.ape";
425 int fd = open(file, O_RDONLY);
426 int64_t size = GetFileSize(file);
427 cout << file << "----------------------" << fd << "---------" << size << endl;
428 source = OH_AVSource_CreateWithFD(fd, 0, size);
429 ASSERT_NE(source, nullptr);
430 demuxer = OH_AVDemuxer_CreateWithSource(source);
431 ASSERT_NE(demuxer, nullptr);
432 sourceFormat = OH_AVSource_GetSourceFormat(source);
433 trackFormat = OH_AVSource_GetTrackFormat(source, 0);
434 ASSERT_NE(trackFormat, nullptr);
435 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
436 string mimeTypeString = mimeType;
437 string apeString = OH_AVCODEC_MIMETYPE_AUDIO_APE;
438 cout << "------mimeType-------" << mimeTypeString << endl;
439 ASSERT_EQ(mimeTypeString, apeString);
440 ASSERT_NE(mimeTypeString, OH_AVCODEC_MIMETYPE_VIDEO_VVC);
441 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
442 ASSERT_EQ(1, g_trackCount);
443 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
444 int aKeyCount = 0;
445 while (!audioIsEnd) {
446 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
447 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
448 }
449 ASSERT_EQ(audioFrame, 8);
450 ASSERT_EQ(aKeyCount, 8);
451 close(fd);
452 }
453
454 /**
455 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2000
456 * @tc.name : demuxer h264+mp3 fmp4 file
457 * @tc.desc : function test
458 */
459 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2000, TestSize.Level0)
460 {
461 int tarckType = 0;
462 OH_AVCodecBufferAttr attr;
463 bool videoIsEnd = false;
464 int videoFrame = 0;
465 const char *file = "/data/test/media/h264_mp3_3mevx_fmp4.mp4";
466 int fd = open(file, O_RDONLY);
467 int64_t size = GetFileSize(file);
468 cout << file << "----------------------" << fd << "---------" << size << endl;
469 source = OH_AVSource_CreateWithFD(fd, 0, size);
470 ASSERT_NE(source, nullptr);
471 demuxer = OH_AVDemuxer_CreateWithSource(source);
472 ASSERT_NE(demuxer, nullptr);
473 sourceFormat = OH_AVSource_GetSourceFormat(source);
474 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
475 ASSERT_EQ(2, g_trackCount);
476 for (int32_t index = 0; index < g_trackCount; index++) {
477 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
478 }
479 int vKeyCount = 0;
480 int aKeyCount = 0;
481 int audioFrame = 0;
482 bool audioIsEnd = false;
483 while (!audioIsEnd || !videoIsEnd) {
484 for (int32_t index = 0; index < g_trackCount; index++) {
485 trackFormat = OH_AVSource_GetTrackFormat(source, index);
486 ASSERT_NE(trackFormat, nullptr);
487 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
488 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
489 continue;
490 }
491 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
492 if (tarckType == 1) {
493 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
494 } else if (tarckType == 0) {
495 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
496 }
497 }
498 }
499 ASSERT_EQ(audioFrame, 465);
500 ASSERT_EQ(aKeyCount, 465);
501 ASSERT_EQ(videoFrame, 369);
502 ASSERT_EQ(vKeyCount, 3);
503 close(fd);
504 }
505
506 /**
507 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2100
508 * @tc.name : demuxer h265+aac fmp4 file
509 * @tc.desc : function test
510 */
511 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2100, TestSize.Level0)
512 {
513 int tarckType = 0;
514 OH_AVCodecBufferAttr attr;
515 bool videoIsEnd = false;
516 int videoFrame = 0;
517 const char *file = "/data/test/media/h265_aac_1mvex_fmp4.mp4";
518 int fd = open(file, O_RDONLY);
519 int64_t size = GetFileSize(file);
520 cout << file << "----------------------" << fd << "---------" << size << endl;
521 source = OH_AVSource_CreateWithFD(fd, 0, size);
522 ASSERT_NE(source, nullptr);
523 demuxer = OH_AVDemuxer_CreateWithSource(source);
524 ASSERT_NE(demuxer, nullptr);
525 sourceFormat = OH_AVSource_GetSourceFormat(source);
526 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
527 ASSERT_EQ(2, g_trackCount);
528 for (int32_t index = 0; index < g_trackCount; index++) {
529 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
530 }
531 int vKeyCount = 0;
532 int aKeyCount = 0;
533 int audioFrame = 0;
534 bool audioIsEnd = false;
535 while (!audioIsEnd || !videoIsEnd) {
536 for (int32_t index = 0; index < g_trackCount; index++) {
537 trackFormat = OH_AVSource_GetTrackFormat(source, index);
538 ASSERT_NE(trackFormat, nullptr);
539 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
540 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
541 continue;
542 }
543 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
544 if (tarckType == 1) {
545 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
546 } else if (tarckType == 0) {
547 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
548 }
549 }
550 }
551 ASSERT_EQ(audioFrame, 173);
552 ASSERT_EQ(aKeyCount, 173);
553 ASSERT_EQ(videoFrame, 242);
554 ASSERT_EQ(vKeyCount, 1);
555 close(fd);
556 }
557
558 /**
559 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2200
560 * @tc.name : demuxer HDRVivid+AudioVivid fmp4 file
561 * @tc.desc : function test
562 */
563 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2200, TestSize.Level0)
564 {
565 int tarckType = 0;
566 OH_AVCodecBufferAttr attr;
567 bool videoIsEnd = false;
568 int videoFrame = 0;
569 const char *file = "/data/test/media/audiovivid_hdrvivid_1s_fmp4.mp4";
570 int fd = open(file, O_RDONLY);
571 int64_t size = GetFileSize(file);
572 cout << file << "----------------------" << fd << "---------" << size << endl;
573 source = OH_AVSource_CreateWithFD(fd, 0, size);
574 ASSERT_NE(source, nullptr);
575 demuxer = OH_AVDemuxer_CreateWithSource(source);
576 ASSERT_NE(demuxer, nullptr);
577 sourceFormat = OH_AVSource_GetSourceFormat(source);
578 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
579 ASSERT_EQ(2, g_trackCount);
580 for (int32_t index = 0; index < g_trackCount; index++) {
581 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
582 }
583 int vKeyCount = 0;
584 int aKeyCount = 0;
585 int audioFrame = 0;
586 bool audioIsEnd = false;
587 while (!audioIsEnd || !videoIsEnd) {
588 for (int32_t index = 0; index < g_trackCount; index++) {
589 trackFormat = OH_AVSource_GetTrackFormat(source, index);
590 ASSERT_NE(trackFormat, nullptr);
591 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
592 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
593 continue;
594 }
595 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
596 if (tarckType == 1) {
597 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
598 } else if (tarckType == 0) {
599 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
600 }
601 }
602 }
603 ASSERT_EQ(videoFrame, 26);
604 ASSERT_EQ(vKeyCount, 1);
605 close(fd);
606 }
607
608 /**
609 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2300
610 * @tc.name : demuxer M4A fmp4 file
611 * @tc.desc : function test
612 */
613 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2300, TestSize.Level0)
614 {
615 OH_AVCodecBufferAttr attr;
616 bool audioIsEnd = false;
617 int audioFrame = 0;
618 const char *file = "/data/test/media/m4a_fmp4.mp4";
619 int fd = open(file, O_RDONLY);
620 int64_t size = GetFileSize(file);
621 cout << file << "----------------------" << fd << "---------" << size << endl;
622 source = OH_AVSource_CreateWithFD(fd, 0, size);
623 ASSERT_NE(source, nullptr);
624 demuxer = OH_AVDemuxer_CreateWithSource(source);
625 ASSERT_NE(demuxer, nullptr);
626 sourceFormat = OH_AVSource_GetSourceFormat(source);
627 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
628 ASSERT_EQ(1, g_trackCount);
629 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
630 int aKeyCount = 0;
631 while (!audioIsEnd) {
632 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
633 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
634 }
635 ASSERT_EQ(audioFrame, 352);
636 ASSERT_EQ(aKeyCount, 352);
637 close(fd);
638 }
639
640 /**
641 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2400
642 * @tc.name : demuxer M4V fmp4 file
643 * @tc.desc : function test
644 */
645 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2400, TestSize.Level0)
646 {
647 int tarckType = 0;
648 OH_AVCodecBufferAttr attr;
649 bool videoIsEnd = false;
650 int videoFrame = 0;
651 const char *file = "/data/test/media/m4v_fmp4.mp4";
652 int fd = open(file, O_RDONLY);
653 int64_t size = GetFileSize(file);
654 cout << file << "----------------------" << fd << "---------" << size << endl;
655 source = OH_AVSource_CreateWithFD(fd, 0, size);
656 ASSERT_NE(source, nullptr);
657 demuxer = OH_AVDemuxer_CreateWithSource(source);
658 ASSERT_NE(demuxer, nullptr);
659 sourceFormat = OH_AVSource_GetSourceFormat(source);
660 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
661 ASSERT_EQ(2, g_trackCount);
662 for (int32_t index = 0; index < g_trackCount; index++) {
663 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
664 }
665 int vKeyCount = 0;
666 int aKeyCount = 0;
667 int audioFrame = 0;
668 bool audioIsEnd = false;
669 while (!audioIsEnd || !videoIsEnd) {
670 for (int32_t index = 0; index < g_trackCount; index++) {
671 trackFormat = OH_AVSource_GetTrackFormat(source, index);
672 ASSERT_NE(trackFormat, nullptr);
673 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
674 if ((audioIsEnd && (tarckType == 0)) || (videoIsEnd && (tarckType == 1))) {
675 continue;
676 }
677 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
678 if (tarckType == 1) {
679 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
680 } else if (tarckType == 0) {
681 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
682 }
683 }
684 }
685 ASSERT_EQ(audioFrame, 176);
686 ASSERT_EQ(aKeyCount, 176);
687 ASSERT_EQ(videoFrame, 123);
688 ASSERT_EQ(vKeyCount, 1);
689 close(fd);
690 }
691
692 /**
693 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2500
694 * @tc.name : create hls demuxer with error uri
695 * @tc.desc : function test
696 */
697 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2500, TestSize.Level0)
698 {
699 const char *uri = "http://192.168.3.11:8080/share/index.m3u8";
700 source = OH_AVSource_CreateWithURI(const_cast<char *>(uri));
701 ASSERT_EQ(nullptr, source);
702 }
703
704 /**
705 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2600
706 * @tc.name : create str demuxer with file and read
707 * @tc.desc : function test
708 */
709 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2600, TestSize.Level0)
710 {
711 OH_AVCodecBufferAttr attr;
712 const char* mimeType = nullptr;
713 int srtIndex = 1;
714 int srtSubtitle = 0;
715 const char *file = "/data/test/media/srt_test.srt";
716 int fd = open(file, O_RDONLY);
717 int64_t size = GetFileSize(file);
718 cout << file << "----------------------" << fd << "---------" << size << endl;
719 source = OH_AVSource_CreateWithFD(fd, 0, size);
720 ASSERT_NE(source, nullptr);
721 demuxer = OH_AVDemuxer_CreateWithSource(source);
722 ASSERT_NE(demuxer, nullptr);
723 sourceFormat = OH_AVSource_GetSourceFormat(source);
724 trackFormat = OH_AVSource_GetTrackFormat(source, 0);
725 ASSERT_NE(trackFormat, nullptr);
726 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
727 string mimeTypeString = mimeType;
728 string srtString = OH_AVCODEC_MIMETYPE_SUBTITLE_SRT;
729 cout << "------mimeType-------" << mimeTypeString << endl;
730 ASSERT_EQ(mimeTypeString, srtString);
731 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
732 ASSERT_EQ(1, g_trackCount);
733 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
734 while (true) {
735 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
736 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
737 cout << " srt is end !!!!!!!!!!!!!!!" << endl;
738 break;
739 }
740 uint8_t *data = OH_AVMemory_GetAddr(memory);
741 srtSubtitle = atoi(reinterpret_cast<const char*>(data));
742 cout << "subtitle" << "----------------" << srtSubtitle << "-----------------" << endl;
743 ASSERT_EQ(srtSubtitle, srtIndex);
744 srtIndex++;
745 }
746 close(fd);
747 }
748
749 /**
750 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2700
751 * @tc.name : create str demuxer with file and seek+read
752 * @tc.desc : function test
753 */
754 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2700, TestSize.Level0)
755 {
756 OH_AVCodecBufferAttr attr;
757 int srtIndex = 1;
758 int srtSubtitle = 0;
759 uint8_t *data = nullptr;
760 const char *file = "/data/test/media/srt_test.srt";
761 int fd = open(file, O_RDONLY);
762 int64_t size = GetFileSize(file);
763 cout << file << "----------------------" << fd << "---------" << size << endl;
764 source = OH_AVSource_CreateWithFD(fd, 0, size);
765 ASSERT_NE(source, nullptr);
766 demuxer = OH_AVDemuxer_CreateWithSource(source);
767 ASSERT_NE(demuxer, nullptr);
768 sourceFormat = OH_AVSource_GetSourceFormat(source);
769 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
770 ASSERT_EQ(1, g_trackCount);
771 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
772
773 for (int index = 0; index < 5; index++) {
774 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
775 data = OH_AVMemory_GetAddr(memory);
776 srtSubtitle = atoi(reinterpret_cast<const char*>(data));
777 cout << "subtitle" << "----------------" << srtSubtitle << "-----------------" << endl;
778 ASSERT_EQ(srtSubtitle, srtIndex);
779 srtIndex++;
780 }
781 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, 5400, SEEK_MODE_CLOSEST_SYNC));
782 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
783 data = OH_AVMemory_GetAddr(memory);
784 srtSubtitle = atoi(reinterpret_cast<const char*>(data));
785 cout << "subtitle"<< "----------------" << srtSubtitle << "-----------------" << endl;
786 srtIndex = 2;
787 ASSERT_EQ(srtSubtitle, srtIndex);
788
789 while (true) {
790 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
791 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
792 cout << " srt is end !!!!!!!!!!!!!!!" << endl;
793 break;
794 }
795 data = OH_AVMemory_GetAddr(memory);
796 srtSubtitle = atoi(reinterpret_cast<const char*>(data));
797 cout << "subtitle" << "----------------" << srtSubtitle << "-----------------" << endl;
798 srtIndex++;
799 ASSERT_EQ(srtSubtitle, srtIndex);
800 }
801
802 close(fd);
803 }
804
805 /**
806 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2800
807 * @tc.name : create str demuxer with error file -- no empty paragraphs
808 * @tc.desc : function test
809 */
810 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2800, TestSize.Level2)
811 {
812 OH_AVCodecBufferAttr attr;
813 const char *file = "/data/test/media/srt_2800.srt";
814 int fd = open(file, O_RDONLY);
815 int64_t size = GetFileSize(file);
816 cout << file << "----------------------" << fd << "---------" << size << endl;
817 source = OH_AVSource_CreateWithFD(fd, 0, size);
818 ASSERT_NE(source, nullptr);
819 demuxer = OH_AVDemuxer_CreateWithSource(source);
820 ASSERT_NE(demuxer, nullptr);
821 sourceFormat = OH_AVSource_GetSourceFormat(source);
822 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
823 ASSERT_EQ(1, g_trackCount);
824 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
825 while (true) {
826 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
827 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
828 cout << " srt is end !!!!!!!!!!!!!!!" << endl;
829 break;
830 }
831 uint8_t *data = OH_AVMemory_GetAddr(memory);
832 cout << "subtitle"<< "----------------" << data << "-----------------" << endl;
833 }
834
835 close(fd);
836 }
837
838 /**
839 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_2900
840 * @tc.name : create str demuxer with error file -- subtitle sequence error
841 * @tc.desc : function test
842 */
843 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_2900, TestSize.Level2)
844 {
845 OH_AVCodecBufferAttr attr;
846 const char *file = "/data/test/media/srt_2900.srt";
847 int fd = open(file, O_RDONLY);
848 int64_t size = GetFileSize(file);
849 cout << file << "----------------------" << fd << "---------" << size << endl;
850 source = OH_AVSource_CreateWithFD(fd, 0, size);
851 ASSERT_NE(source, nullptr);
852 demuxer = OH_AVDemuxer_CreateWithSource(source);
853 ASSERT_NE(demuxer, nullptr);
854 sourceFormat = OH_AVSource_GetSourceFormat(source);
855 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
856 ASSERT_EQ(1, g_trackCount);
857 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
858 while (true) {
859 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
860 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
861 cout << " srt is end !!!!!!!!!!!!!!!" << endl;
862 break;
863 }
864 uint8_t *data = OH_AVMemory_GetAddr(memory);
865 cout << "subtitle" << "----------------" << data << "-----------------" << endl;
866 }
867
868 close(fd);
869 }
870
871 /**
872 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3000
873 * @tc.name : create str demuxer with error file -- timeline format error
874 * @tc.desc : function test
875 */
876 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3000, TestSize.Level2)
877 {
878 OH_AVCodecBufferAttr attr;
879 const char *file = "/data/test/media/srt_3000.srt";
880 int fd = open(file, O_RDONLY);
881 int64_t size = GetFileSize(file);
882 cout << file << "----------------------" << fd << "---------" << size << endl;
883 source = OH_AVSource_CreateWithFD(fd, 0, size);
884 demuxer = OH_AVDemuxer_CreateWithSource(source);
885 sourceFormat = OH_AVSource_GetSourceFormat(source);
886 OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount);
887 cout << "g_trackCount"<< "----------------" << g_trackCount << "-----------------" << endl;
888 OH_AVDemuxer_SelectTrackByID(demuxer, 0);
889 OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr);
890 uint8_t *data = OH_AVMemory_GetAddr(memory);
891 cout << "subtitle"<< "----------------" << data << "-----------------" << endl;
892 close(fd);
893 }
894
895 /**
896 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3100
897 * @tc.name : create str demuxer with error file -- subtitle is empty
898 * @tc.desc : function test
899 */
900 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3100, TestSize.Level2)
901 {
902 OH_AVCodecBufferAttr attr;
903 const char *file = "/data/test/media/srt_3100.srt";
904 int fd = open(file, O_RDONLY);
905 int64_t size = GetFileSize(file);
906 cout << file << "----------------------" << fd << "---------" << size << endl;
907 source = OH_AVSource_CreateWithFD(fd, 0, size);
908 ASSERT_NE(source, nullptr);
909 demuxer = OH_AVDemuxer_CreateWithSource(source);
910 ASSERT_NE(demuxer, nullptr);
911 sourceFormat = OH_AVSource_GetSourceFormat(source);
912 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
913 ASSERT_EQ(1, g_trackCount);
914 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
915 while (true) {
916 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
917 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
918 cout << " srt is end !!!!!!!!!!!!!!!" << endl;
919 break;
920 }
921 uint8_t *data = OH_AVMemory_GetAddr(memory);
922 cout << "subtitle"<< "----------------" << data << "-----------------" << endl;
923 }
924
925 close(fd);
926 }
927
928 /**
929 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3200
930 * @tc.name : create str demuxer with error file -- SRT file is empty
931 * @tc.desc : function test
932 * fail
933 */
934 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3200, TestSize.Level2)
935 {
936 OH_AVCodecBufferAttr attr;
937 const char *file = "/data/test/media/srt_3200.srt";
938 int fd = open(file, O_RDONLY);
939 int64_t size = GetFileSize(file);
940 cout << file << "----------------------" << fd << "---------" << size << endl;
941 source = OH_AVSource_CreateWithFD(fd, 0, size);
942 demuxer = OH_AVDemuxer_CreateWithSource(source);
943 sourceFormat = OH_AVSource_GetSourceFormat(source);
944 OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount);
945 cout << "g_trackCount"<< "----------------" << g_trackCount << "-----------------" << endl;
946 OH_AVDemuxer_SelectTrackByID(demuxer, 0);
947 OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr);
948 uint8_t *data = OH_AVMemory_GetAddr(memory);
949 cout << "subtitle"<< "----------------" << data << "-----------------" << endl;
950 close(fd);
951 }
952
953 /**
954 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3300
955 * @tc.name : create str demuxer with error file -- alternating Up and Down Times
956 * @tc.desc : function test
957 */
958 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3300, TestSize.Level2)
959 {
960 OH_AVCodecBufferAttr attr;
961 const char *file = "/data/test/media/srt_3300.srt";
962 int fd = open(file, O_RDONLY);
963 int64_t size = GetFileSize(file);
964 cout << file << "----------------------" << fd << "---------" << size << endl;
965 source = OH_AVSource_CreateWithFD(fd, 0, size);
966 ASSERT_NE(source, nullptr);
967 demuxer = OH_AVDemuxer_CreateWithSource(source);
968 ASSERT_NE(demuxer, nullptr);
969 sourceFormat = OH_AVSource_GetSourceFormat(source);
970 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
971 ASSERT_EQ(1, g_trackCount);
972 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
973 while (true) {
974 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
975 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
976 cout << " srt is end !!!!!!!!!!!!!!!" << endl;
977 break;
978 }
979 uint8_t *data = OH_AVMemory_GetAddr(memory);
980 cout << "subtitle"<< "----------------" << data << "-----------------" << endl;
981 }
982
983 close(fd);
984 }
985
986 /**
987 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3400
988 * @tc.name : demuxer MP4 ,OH_MD_KEY_DURATION,OH_MD_KEY_CODEC_CONFIG
989 * @tc.desc : function test
990 */
991 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3400, TestSize.Level0)
992 {
993 if (!strcmp(g_codecNameHEVC, "OMX.hisi.video.encoder.hevc")) {
994 int64_t duration;
995 static OH_AVFormat *trackFormatFirst = nullptr;
996 static OH_AVFormat *trackFormatSecond = nullptr;
997 uint8_t *codecConfig = nullptr;
998 double frameRate;
999 int32_t rotation;
1000 int64_t channelLayout;
1001 int32_t audioSampleFormat;
1002 int32_t bitsPreCodedSample;
1003 int32_t profile;
1004 int32_t colorPrimaries;
1005 int32_t videoIsHdrvivid;
1006 size_t bufferSize;
1007 const char *file = "/data/test/media/01_video_audio.mp4";
1008 int fd = open(file, O_RDONLY);
1009 int64_t size = GetFileSize(file);
1010 source = OH_AVSource_CreateWithFD(fd, 0, size);
1011 ASSERT_NE(source, nullptr);
1012 sourceFormat = OH_AVSource_GetSourceFormat(source);
1013 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1014 trackFormatFirst = OH_AVSource_GetTrackFormat(source, 0);
1015 ASSERT_NE(trackFormatFirst, nullptr);
1016 trackFormatSecond = OH_AVSource_GetTrackFormat(source, 1);
1017 ASSERT_NE(trackFormatSecond, nullptr);
1018 ASSERT_TRUE(OH_AVFormat_GetLongValue(sourceFormat, OH_MD_KEY_DURATION, &duration));
1019 ASSERT_EQ(duration, 10032000);
1020 ASSERT_TRUE(OH_AVFormat_GetBuffer(trackFormatSecond, OH_MD_KEY_CODEC_CONFIG, &codecConfig, &bufferSize));
1021 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(trackFormatSecond, OH_MD_KEY_FRAME_RATE, &frameRate));
1022 ASSERT_EQ(frameRate, 25.1);
1023 ASSERT_FALSE(OH_AVFormat_GetIntValue(trackFormatSecond, OH_MD_KEY_ROTATION, &rotation));
1024 ASSERT_TRUE(OH_AVFormat_GetLongValue(trackFormatFirst, OH_MD_KEY_CHANNEL_LAYOUT, &channelLayout));
1025 ASSERT_EQ(channelLayout, 3);
1026 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormatFirst, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &audioSampleFormat));
1027 ASSERT_EQ(audioSampleFormat, 9);
1028 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormatFirst, OH_MD_KEY_BITS_PER_CODED_SAMPLE, &bitsPreCodedSample));
1029 ASSERT_EQ(bitsPreCodedSample, 16);
1030 ASSERT_FALSE(OH_AVFormat_GetIntValue(trackFormatFirst, OH_MD_KEY_PROFILE, &profile));
1031 ASSERT_FALSE(OH_AVFormat_GetIntValue(trackFormatFirst, OH_MD_KEY_COLOR_PRIMARIES, &colorPrimaries));
1032 ASSERT_FALSE(OH_AVFormat_GetIntValue(trackFormatFirst, OH_MD_KEY_VIDEO_IS_HDR_VIVID, &videoIsHdrvivid));
1033 close(fd);
1034 }
1035 }
1036
1037 /**
1038 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3800
1039 * @tc.name : demuxer MP4 ,AVCODEC_BUFFER_FLAGS_DISCARD
1040 * @tc.desc : function test
1041 */
1042 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3800, TestSize.Level0)
1043 {
1044 if (!strcmp(g_codecNameHEVC, "OMX.hisi.video.encoder.hevc")) {
1045 OH_AVCodecBufferAttr attr;
1046 int tarckType = 0;
1047 const char *file = "/data/test/media/test_265_B_Gop25_4sec.mp4";
1048 int fd = open(file, O_RDONLY);
1049 int64_t size = GetFileSize(file);
1050 cout << file << "----------------------" << fd << "---------" << size << endl;
1051 source = OH_AVSource_CreateWithFD(fd, 0, size);
1052 ASSERT_NE(source, nullptr);
1053 sourceFormat = OH_AVSource_GetSourceFormat(source);
1054 ASSERT_NE(sourceFormat, nullptr);
1055 demuxer = OH_AVDemuxer_CreateWithSource(source);
1056 ASSERT_NE(demuxer, nullptr);
1057 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1058 ASSERT_EQ(2, g_trackCount);
1059 for (int32_t index = 0; index < g_trackCount; index++) {
1060 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1061 }
1062 int audioFrame = 0;
1063 bool audioIsEnd = false;
1064 while (!audioIsEnd) {
1065 for (int32_t index = 0; index < g_trackCount; index++) {
1066 trackFormat = OH_AVSource_GetTrackFormat(source, index);
1067 ASSERT_NE(trackFormat, nullptr);
1068 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
1069 if ((audioIsEnd && (tarckType == 0))) {
1070 continue;
1071 }
1072 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1073 if (tarckType == 0 && (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_DISCARD)) {
1074 audioIsEnd = true;
1075 cout << audioFrame << " audio is end !!!!!!!!!!!!!!!" << endl;
1076 }
1077 }
1078 }
1079 close(fd);
1080 }
1081 }
1082
1083 /**
1084 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3700
1085 * @tc.name : demuxer MP4,duration,dts
1086 * @tc.desc : function test
1087 */
1088 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3700, TestSize.Level0)
1089 {
1090 if (!strcmp(g_codecNameHEVC, "OMX.hisi.video.encoder.hevc")) {
1091 int tarckType = 0;
1092 int64_t duration;
1093 int64_t dts;
1094 const char *file = "/data/test/media/test_265_B_Gop25_4sec.mp4";
1095 int fd = open(file, O_RDONLY);
1096 int64_t size = GetFileSize(file);
1097 cout << file << "----------------------" << fd << "---------" << size << endl;
1098 source = OH_AVSource_CreateWithFD(fd, 0, size);
1099 ASSERT_NE(source, nullptr);
1100 demuxer = OH_AVDemuxer_CreateWithSource(source);
1101 ASSERT_NE(demuxer, nullptr);
1102 avBuffer = OH_AVBuffer_Create(size);
1103 ASSERT_NE(avBuffer, nullptr);
1104 sourceFormat = OH_AVSource_GetSourceFormat(source);
1105 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1106 ASSERT_EQ(2, g_trackCount);
1107 for (int32_t index = 0; index < g_trackCount; index++) {
1108 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1109 }
1110 for (int32_t index = 0; index < g_trackCount; index++) {
1111 trackFormat = OH_AVSource_GetTrackFormat(source, index);
1112 ASSERT_NE(trackFormat, nullptr);
1113 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
1114 if (tarckType == MEDIA_TYPE_VID) {
1115 OH_AVDemuxer_ReadSampleBuffer(demuxer, index, avBuffer);
1116 ASSERT_NE(avBuffer, nullptr);
1117 format = OH_AVBuffer_GetParameter(avBuffer);
1118 ASSERT_NE(format, nullptr);
1119 ASSERT_TRUE(OH_AVFormat_GetLongValue(format, OH_MD_KEY_BUFFER_DURATION, &duration));
1120 ASSERT_TRUE(OH_AVFormat_GetLongValue(format, OH_MD_KEY_DECODING_TIMESTAMP, &dts));
1121 ASSERT_EQ(40000, duration);
1122 ASSERT_EQ(-80000, dts);
1123 }
1124 }
1125 }
1126 }
1127
1128 /**
1129 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3600
1130 * @tc.name : demuxer MP4 ,SAR,bitsPreCodedSample,sampleFormat
1131 * @tc.desc : function test
1132 */
1133 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3600, TestSize.Level0)
1134 {
1135 if (!strcmp(g_codecNameHEVC, "OMX.hisi.video.encoder.hevc")) {
1136 int tarckType = 0;
1137 double sar;
1138 int32_t bitsPreCodedSample;
1139 int32_t sampleFormat;
1140 const char *file = "/data/test/media/test_265_B_Gop25_4sec.mp4";
1141 int fd = open(file, O_RDONLY);
1142 int64_t size = GetFileSize(file);
1143 cout << file << "----------------------" << fd << "---------" << size << endl;
1144 source = OH_AVSource_CreateWithFD(fd, 0, size);
1145 ASSERT_NE(source, nullptr);
1146 sourceFormat = OH_AVSource_GetSourceFormat(source);
1147 ASSERT_NE(sourceFormat, nullptr);
1148 demuxer = OH_AVDemuxer_CreateWithSource(source);
1149 ASSERT_NE(demuxer, nullptr);
1150 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1151 ASSERT_EQ(2, g_trackCount);
1152 for (int32_t index = 0; index < g_trackCount; index++) {
1153 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1154 }
1155 for (int32_t index = 0; index < g_trackCount; index++) {
1156 trackFormat = OH_AVSource_GetTrackFormat(source, index);
1157 ASSERT_NE(trackFormat, nullptr);
1158 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
1159 if (tarckType == MEDIA_TYPE_VID) {
1160 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(trackFormat, OH_MD_KEY_VIDEO_SAR, &sar));
1161 }else if (tarckType == MEDIA_TYPE_AUD) {
1162 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_BITS_PER_CODED_SAMPLE, &bitsPreCodedSample));
1163 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &sampleFormat));
1164 }
1165 }
1166 ASSERT_EQ(1, sar);
1167 ASSERT_EQ(16, bitsPreCodedSample);
1168 ASSERT_EQ(9, sampleFormat);
1169 close(fd);
1170 }
1171 }
1172
1173 /**
1174 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3510
1175 * @tc.name : demuxer MP4 ,startTime Non-zero
1176 * @tc.desc : function test
1177 */
1178 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3510, TestSize.Level0)
1179 {
1180 int64_t startTime;
1181 const char *file = "/data/test/media/test_starttime.mp4";
1182 int fd = open(file, O_RDONLY);
1183 int64_t size = GetFileSize(file);
1184 source = OH_AVSource_CreateWithFD(fd, 0, size);
1185 ASSERT_NE(source, nullptr);
1186 sourceFormat = OH_AVSource_GetSourceFormat(source);
1187 ASSERT_NE(sourceFormat, nullptr);
1188 ASSERT_TRUE(OH_AVFormat_GetLongValue(sourceFormat, OH_MD_KEY_START_TIME, &startTime));
1189 cout << "---startTime---" << startTime << endl;
1190 ASSERT_EQ(START_TIME_NUM, startTime);
1191 close(fd);
1192 }
1193
1194 /**
1195 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_3500
1196 * @tc.name : demuxer MP4 ,startTime
1197 * @tc.desc : function test
1198 */
1199 HWTEST_F(DemuxerProcNdkTest, SUB_MEDIA_DEMUXER_PROCESS_3500, TestSize.Level0)
1200 {
1201 if (!strcmp(g_codecNameHEVC, "OMX.hisi.video.encoder.hevc")) {
1202 int64_t startTime;
1203 const char *file = "/data/test/media/test_265_B_Gop25_4sec.mp4";
1204 int fd = open(file, O_RDONLY);
1205 int64_t size = GetFileSize(file);
1206 source = OH_AVSource_CreateWithFD(fd, 0, size);
1207 ASSERT_NE(source, nullptr);
1208 sourceFormat = OH_AVSource_GetSourceFormat(source);
1209 ASSERT_NE(sourceFormat, nullptr);
1210 ASSERT_TRUE(OH_AVFormat_GetLongValue(sourceFormat, OH_MD_KEY_START_TIME, &startTime));
1211 ASSERT_EQ(0, startTime);
1212 close(fd);
1213 }
1214 }
1215
1216