1 /*
2 * Copyright (C) 2024 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 #include "meta/meta_key.h"
24 #include "meta/meta.h"
25 #include "av_common.h"
26
27 #include <iostream>
28 #include <cstdio>
29 #include <string>
30 #include <fcntl.h>
31 #include <cmath>
32 #include <thread>
33
34 using namespace std;
35 using namespace OHOS;
36 using namespace OHOS::Media;
37 using namespace testing::ext;
38
39 namespace OHOS {
40 namespace Media {
41 class DemuxerFunc2NdkTest : public testing::Test {
42 public:
43 // SetUpTestCase: Called before all test cases
44 static void SetUpTestCase(void);
45 // TearDownTestCase: Called after all test case
46 static void TearDownTestCase(void);
47 // SetUp: Called before each test cases
48 void SetUp(void);
49 // TearDown: Called after each test cases
50 void TearDown(void);
51 };
52
53 static OH_AVMemory *memory = nullptr;
54 static OH_AVSource *source = nullptr;
55 static OH_AVDemuxer *demuxer = nullptr;
56 static OH_AVFormat *sourceFormat = nullptr;
57 static OH_AVFormat *trackFormat = nullptr;
58 static OH_AVBuffer *avBuffer = nullptr;
59 static OH_AVFormat *format = nullptr;
60 static int32_t g_trackCount;
61 static int32_t g_width = 3840;
62 static int32_t g_height = 2160;
SetUpTestCase()63 void DemuxerFunc2NdkTest::SetUpTestCase() {}
TearDownTestCase()64 void DemuxerFunc2NdkTest::TearDownTestCase() {}
SetUp()65 void DemuxerFunc2NdkTest::SetUp()
66 {
67 memory = OH_AVMemory_Create(g_width * g_height);
68 g_trackCount = 0;
69 }
TearDown()70 void DemuxerFunc2NdkTest::TearDown()
71 {
72 if (trackFormat != nullptr) {
73 OH_AVFormat_Destroy(trackFormat);
74 trackFormat = nullptr;
75 }
76
77 if (sourceFormat != nullptr) {
78 OH_AVFormat_Destroy(sourceFormat);
79 sourceFormat = nullptr;
80 }
81
82 if (memory != nullptr) {
83 OH_AVMemory_Destroy(memory);
84 memory = nullptr;
85 }
86 if (source != nullptr) {
87 OH_AVSource_Destroy(source);
88 source = nullptr;
89 }
90 if (demuxer != nullptr) {
91 OH_AVDemuxer_Destroy(demuxer);
92 demuxer = nullptr;
93 }
94 if (avBuffer != nullptr) {
95 OH_AVBuffer_Destroy(avBuffer);
96 avBuffer = nullptr;
97 }
98 if (format != nullptr) {
99 OH_AVFormat_Destroy(format);
100 format = nullptr;
101 }
102 }
103 } // namespace Media
104 } // namespace OHOS
GetFileSize(const char * fileName)105 static int64_t GetFileSize(const char *fileName)
106 {
107 int64_t fileSize = 0;
108 if (fileName != nullptr) {
109 struct stat fileStatus {};
110 if (stat(fileName, &fileStatus) == 0) {
111 fileSize = static_cast<int64_t>(fileStatus.st_size);
112 }
113 }
114 return fileSize;
115 }
116
117 /**
118 * @tc.number : SUB_MEDIA_DEMUXER_VTT_6100
119 * @tc.name : create vtt demuxer with error file -- alternating Up and Down Times
120 * @tc.desc : function test
121 */
122 HWTEST_F(DemuxerFunc2NdkTest, SUB_MEDIA_DEMUXER_VTT_6100, TestSize.Level2)
123 {
124 OH_AVCodecBufferAttr attr;
125 const char* mimeType = nullptr;
126 const char *file = "/data/test/media/vtt_6100.vtt";
127 int fd = open(file, O_RDONLY);
128 int64_t size = GetFileSize(file);
129 cout << file << "----------------------" << fd << "---------" << size << endl;
130 source = OH_AVSource_CreateWithFD(fd, 0, size);
131 ASSERT_NE(source, nullptr);
132 demuxer = OH_AVDemuxer_CreateWithSource(source);
133 ASSERT_NE(demuxer, nullptr);
134 sourceFormat = OH_AVSource_GetSourceFormat(source);
135 trackFormat = OH_AVSource_GetTrackFormat(source, 0);
136 ASSERT_NE(trackFormat, nullptr);
137 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
138 ASSERT_EQ(0, strcmp(mimeType, OH_AVCODEC_MIMETYPE_SUBTITLE_WEBVTT));
139 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
140 ASSERT_EQ(1, g_trackCount);
141 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
142 int tarckType = 0;
143 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
144 ASSERT_EQ(tarckType, MEDIA_TYPE_SUBTITLE);
145 while (true) {
146 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
147 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
148 cout << " vtt is end !!!!!!!!!!!!!!!" << endl;
149 break;
150 }
151 uint8_t *data = OH_AVMemory_GetAddr(memory);
152 cout << "subtitle"<< "----------------" << data << "-----------------" << endl;
153 }
154 close(fd);
155 }
156
157 /**
158 * @tc.number : DEMUXER_META_0090
159 * @tc.name : demuxer meta info, souce is null
160 * @tc.desc : function test
161 */
162 HWTEST_F(DemuxerFunc2NdkTest, DEMUXER_META_0090, TestSize.Level0)
163 {
164 OH_AVFormat *metaFormat= OH_AVSource_GetCustomMetadataFormat(nullptr);
165 ASSERT_EQ(metaFormat, nullptr);
166 }