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 <string>
17 #include <malloc.h>
18 #include <sys/stat.h>
19 #include <cinttypes>
20 #include <fcntl.h>
21 #include <list>
22 #include <cmath>
23 #include <sys/types.h>
24 #include <fstream>
25 #include <memory>
26 #include "gtest/gtest.h"
27 #include "avcodec_errors.h"
28 #include "pts_and_index_conversion_unit_test.h"
29
30 #define LOCAL true
31 namespace OHOS::Media {
32
33 using namespace std;
34 using namespace testing::ext;
35 using MediaSource = OHOS::Media::Plugins::MediaSource;
36
37 string g_flvPath = string("/data/test/media/h264.flv");
38 string g_ptsConversionPath = string("/data/test/media/camera_info_parser.mp4");
39 string g_ptsdoublevideoPath = string("/data/test/media/h264_double_video_audio.mp4");
40
SetUpTestCase(void)41 void PtsAndIndexConversionTest::SetUpTestCase(void)
42 {
43 }
44
TearDownTestCase(void)45 void PtsAndIndexConversionTest::TearDownTestCase(void)
46 {
47 }
48
SetUp()49 void PtsAndIndexConversionTest::SetUp()
50 {
51 }
52
TearDown()53 void PtsAndIndexConversionTest::TearDown()
54 {
55 if (fd_ > 0) {
56 close(fd_);
57 fd_ = -1;
58 }
59
60 if (index_ != 0) {
61 index_ = 0;
62 }
63
64 if (trackIndex_ != 0) {
65 trackIndex_ = 0;
66 }
67
68 if (relativePresentationTimeUs_ != 0) {
69 relativePresentationTimeUs_ = 0;
70 }
71
72 initStatus_ = false;
73 }
74
InitResource(const std::string & srtpath,Status code)75 void PtsAndIndexConversionTest::InitResource(const std::string &srtpath, Status code)
76 {
77 int64_t fileSize = 0;
78 if (!srtpath.empty()) {
79 struct stat fileStatus {};
80 if (stat(srtpath.c_str(), &fileStatus) == 0) {
81 fileSize = static_cast<int64_t>(fileStatus.st_size);
82 }
83 }
84 fd_ = open(srtpath.c_str(), O_RDONLY);
85 if (fd_ < 0) {
86 return;
87 }
88 std::string uri = "fd://" + std::to_string(fd_) + "?offset=0&size=" + std::to_string(fileSize);
89 TimeAndIndexConversions_ = std::make_shared<TimeAndIndexConversion>();
90 std::shared_ptr<MediaSource> mediaSource = std::make_shared<MediaSource>(uri);
91 ASSERT_EQ(TimeAndIndexConversions_->SetDataSource(mediaSource), code);
92 initStatus_ = true;
93 }
94
95 /**
96 * @tc.name: Demuxer_GetIndexByRelativePresentationTimeUs_1000
97 * @tc.desc: Get index by pts(audio track)
98 * @tc.type: FUNC
99 */
100 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetIndexByRelativePresentationTimeUs_1000, TestSize.Level1)
101 {
102 InitResource(g_ptsConversionPath, Status::OK);
103 ASSERT_TRUE(initStatus_);
104 relativePresentationTimeUs_ = 69659;
105 ASSERT_EQ(TimeAndIndexConversions_->GetIndexByRelativePresentationTimeUs(
106 trackIndex_, relativePresentationTimeUs_, index_), Status::OK);
107 ASSERT_EQ(index_, 3);
108 }
109
110 /**
111 * @tc.name: Demuxer_GetIndexByRelativePresentationTimeUs_1001
112 * @tc.desc: Get index by pts(video track)
113 * @tc.type: FUNC
114 */
115 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetIndexByRelativePresentationTimeUs_1001, TestSize.Level1)
116 {
117 InitResource(g_ptsConversionPath, Status::OK);
118 ASSERT_TRUE(initStatus_);
119 trackIndex_ = 1;
120 relativePresentationTimeUs_ = 66666;
121 ASSERT_EQ(TimeAndIndexConversions_->GetIndexByRelativePresentationTimeUs(
122 trackIndex_, relativePresentationTimeUs_, index_), Status::OK);
123 ASSERT_EQ(index_, 4);
124 }
125
126 /**
127 * @tc.name: Demuxer_GetIndexByRelativePresentationTimeUs_1002
128 * @tc.desc: Get index by pts(not MP4)
129 * @tc.type: FUNC
130 */
131 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetIndexByRelativePresentationTimeUs_1002, TestSize.Level1)
132 {
133 InitResource(g_flvPath, Status::ERROR_UNSUPPORTED_FORMAT);
134 ASSERT_TRUE(initStatus_);
135 relativePresentationTimeUs_ = 69659;
136 ASSERT_NE(TimeAndIndexConversions_->GetIndexByRelativePresentationTimeUs(
137 trackIndex_, relativePresentationTimeUs_, index_), Status::OK);
138 ASSERT_EQ(index_, 0);
139 }
140
141 /**
142 * @tc.name: Demuxer_GetIndexByRelativePresentationTimeUs_1003
143 * @tc.desc: Get index by pts(non-standard pts & different track)
144 * @tc.type: FUNC
145 */
146 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetIndexByRelativePresentationTimeUs_1003, TestSize.Level1)
147 {
148 InitResource(g_ptsConversionPath, Status::OK);
149 ASSERT_TRUE(initStatus_);
150 trackIndex_ = 1;
151 relativePresentationTimeUs_ = 166666;
152 ASSERT_EQ(TimeAndIndexConversions_->GetIndexByRelativePresentationTimeUs(
153 trackIndex_, relativePresentationTimeUs_, index_), Status::OK);
154 ASSERT_EQ(index_, 10);
155 }
156
157 /**
158 * @tc.name: Demuxer_GetRelativePresentationTimeUsByIndex_1000
159 * @tc.desc: get pts by frameIndex(audio track)
160 * @tc.type: FUNC
161 */
162 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetRelativePresentationTimeUsByIndex_1000, TestSize.Level1)
163 {
164 InitResource(g_ptsConversionPath, Status::OK);
165 ASSERT_TRUE(initStatus_);
166 index_ = 2;
167 ASSERT_EQ(TimeAndIndexConversions_->GetRelativePresentationTimeUsByIndex(
168 trackIndex_, index_, relativePresentationTimeUs_), Status::OK);
169 ASSERT_EQ(relativePresentationTimeUs_, 46439);
170 }
171
172 /**
173 * @tc.name: Demuxer_GetRelativePresentationTimeUsByIndex_1001
174 * @tc.desc: get pts by frameIndex(video track)
175 * @tc.type: FUNC
176 */
177 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetRelativePresentationTimeUsByIndex_1001, TestSize.Level1)
178 {
179 InitResource(g_ptsConversionPath, Status::OK);
180 ASSERT_TRUE(initStatus_);
181 index_ = 2;
182 trackIndex_ = 1;
183 ASSERT_EQ(TimeAndIndexConversions_->GetRelativePresentationTimeUsByIndex(
184 trackIndex_, index_, relativePresentationTimeUs_), Status::OK);
185 ASSERT_EQ(relativePresentationTimeUs_, 33333);
186 }
187
188 /**
189 * @tc.name: Demuxer_GetRelativePresentationTimeUsByIndex_1002
190 * @tc.desc: get pts by frameIndex(not MP4)
191 * @tc.type: FUNC
192 */
193 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetRelativePresentationTimeUsByIndex_1002, TestSize.Level1)
194 {
195 InitResource(g_flvPath, Status::ERROR_UNSUPPORTED_FORMAT);
196 ASSERT_TRUE(initStatus_);
197 index_ = 10;
198 ASSERT_NE(TimeAndIndexConversions_->GetRelativePresentationTimeUsByIndex(
199 trackIndex_, index_, relativePresentationTimeUs_), Status::OK);
200 ASSERT_EQ(relativePresentationTimeUs_, 0);
201 }
202
203 /**
204 * @tc.name: Demuxer_PtsAndFrameIndexConversion_1000
205 * @tc.desc: pts and frameIndex convertion test(pts -> frameIndex -> pts)
206 * @tc.type: FUNC
207 */
208 HWTEST_F(PtsAndIndexConversionTest, Demuxer_PtsAndFrameIndexConversion_1000, TestSize.Level1)
209 {
210 InitResource(g_ptsConversionPath, Status::OK);
211 ASSERT_TRUE(initStatus_);
212 relativePresentationTimeUs_ = 92879;
213 ASSERT_EQ(TimeAndIndexConversions_->GetIndexByRelativePresentationTimeUs(
214 trackIndex_, relativePresentationTimeUs_, index_), Status::OK);
215 ASSERT_EQ(index_, 4);
216
217 relativePresentationTimeUs_ = 0;
218 ASSERT_EQ(TimeAndIndexConversions_->GetRelativePresentationTimeUsByIndex(
219 trackIndex_, index_, relativePresentationTimeUs_), Status::OK);
220 ASSERT_EQ(relativePresentationTimeUs_, 92879);
221 }
222
223 /**
224 * @tc.name: Demuxer_PtsAndFrameIndexConversion_1001
225 * @tc.desc: pts and frameIndex convertion test(frameIndex -> pts -> frameIndex)
226 * @tc.type: FUNC
227 */
228 HWTEST_F(PtsAndIndexConversionTest, Demuxer_PtsAndFrameIndexConversion_1001, TestSize.Level1)
229 {
230 InitResource(g_ptsConversionPath, Status::OK);
231 ASSERT_TRUE(initStatus_);
232 index_ = 4;
233 ASSERT_EQ(TimeAndIndexConversions_->GetRelativePresentationTimeUsByIndex(
234 trackIndex_, index_, relativePresentationTimeUs_), Status::OK);
235 ASSERT_EQ(relativePresentationTimeUs_, 92879);
236
237 index_ = 0;
238 ASSERT_EQ(TimeAndIndexConversions_->GetIndexByRelativePresentationTimeUs(
239 trackIndex_, relativePresentationTimeUs_, index_), Status::OK);
240 ASSERT_EQ(index_, 4);
241 }
242
243 /**
244 * @tc.name: Demuxer_PTSOutOfRange_1000
245 * @tc.desc: pts out of range
246 * @tc.type: FUNC
247 */
248 HWTEST_F(PtsAndIndexConversionTest, Demuxer_PTSOutOfRange_1000, TestSize.Level1)
249 {
250 InitResource(g_ptsConversionPath, Status::OK);
251 ASSERT_TRUE(initStatus_);
252 relativePresentationTimeUs_ = 999999999;
253 ASSERT_NE(TimeAndIndexConversions_->GetIndexByRelativePresentationTimeUs(
254 trackIndex_, relativePresentationTimeUs_, index_), Status::OK);
255 ASSERT_EQ(index_, 0);
256 }
257
258 /**
259 * @tc.name: Demuxer_IndexOutOfRange_1000
260 * @tc.desc: Index out of range
261 * @tc.type: FUNC
262 */
263 HWTEST_F(PtsAndIndexConversionTest, Demuxer_IndexOutOfRange_1000, TestSize.Level1)
264 {
265 InitResource(g_ptsConversionPath, Status::OK);
266 ASSERT_TRUE(initStatus_);
267 index_ = 9999999;
268 ASSERT_NE(TimeAndIndexConversions_->GetRelativePresentationTimeUsByIndex(
269 trackIndex_, index_, relativePresentationTimeUs_), Status::OK);
270 ASSERT_EQ(relativePresentationTimeUs_, 0);
271 }
272
273 /**
274 * @tc.name: Demuxer_TrackOutOfRange_1000
275 * @tc.desc: Track out of range
276 * @tc.type: FUNC
277 */
278 HWTEST_F(PtsAndIndexConversionTest, Demuxer_TrackOutOfRange_1000, TestSize.Level1)
279 {
280 InitResource(g_ptsConversionPath, Status::OK);
281 ASSERT_TRUE(initStatus_);
282 trackIndex_ = 99;
283 ASSERT_NE(TimeAndIndexConversions_->GetRelativePresentationTimeUsByIndex(
284 trackIndex_, index_, relativePresentationTimeUs_), Status::OK);
285 ASSERT_EQ(relativePresentationTimeUs_, 0);
286 ASSERT_EQ(index_, 0);
287 }
288
289 /**
290 * @tc.name: Demuxer_GetVideoTrackIndex_1000
291 * @tc.desc: Get video track index
292 * @tc.type: FUNC
293 */
294 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetVideoTrackIndex_1000, TestSize.Level1)
295 {
296 InitResource(g_ptsConversionPath, Status::OK);
297 ASSERT_TRUE(initStatus_);
298 trackIndex_ = -1;
299 ASSERT_EQ(TimeAndIndexConversions_->GetFirstVideoTrackIndex(trackIndex_), Status::OK);
300 ASSERT_EQ(trackIndex_, 1);
301 }
302
303 /**
304 * @tc.name: Demuxer_GetVideoTrackIndex_1001
305 * @tc.desc: Get video track index(double video track index)
306 * @tc.type: FUNC
307 */
308 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetVideoTrackIndex_1001, TestSize.Level1)
309 {
310 InitResource(g_ptsdoublevideoPath, Status::OK);
311 ASSERT_TRUE(initStatus_);
312 trackIndex_ = -1;
313 ASSERT_EQ(TimeAndIndexConversions_->GetFirstVideoTrackIndex(trackIndex_), Status::OK);
314 ASSERT_EQ(trackIndex_, 0);
315 }
316
317 /**
318 * @tc.name: Demuxer_GetVideoTrackIndex_1002
319 * @tc.desc: Get video track index(not MP4)
320 * @tc.type: FUNC
321 */
322 HWTEST_F(PtsAndIndexConversionTest, Demuxer_GetVideoTrackIndex_1002, TestSize.Level1)
323 {
324 InitResource(g_flvPath, Status::ERROR_UNSUPPORTED_FORMAT);
325 ASSERT_TRUE(initStatus_);
326 trackIndex_ = -1;
327 ASSERT_NE(TimeAndIndexConversions_->GetFirstVideoTrackIndex(trackIndex_), Status::OK);
328 ASSERT_EQ(trackIndex_, -1);
329 }
330 }