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
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 DemuxerFormatNdkTest : 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
52 static int32_t g_trackCount;
53 static int32_t g_width = 3840;
54 static int32_t g_height = 2160;
55 constexpr uint64_t AVC_BITRATE = 2144994;
56 constexpr uint64_t ACTUAL_DURATION = 4120000;
57 constexpr uint32_t ACTUAL_AUDIOFORMAT = 9;
58 constexpr uint32_t ACTUAL_AUDIOCOUNT = 2;
59 constexpr uint64_t ACTUAL_LAYOUT = 3;
60 constexpr uint32_t ACTUAL_SAMPLERATE = 44100;
61 constexpr uint32_t ACTUAL_CODEDSAMPLE = 16;
62 constexpr uint32_t ACTUAL_CURRENTWIDTH = 1920;
63 constexpr uint32_t ACTUAL_CURRENTHEIGHT = 1080;
64 constexpr double ACTUAL_FRAMERATE = 25;
65 constexpr uint64_t HEVC_BITRATE = 4162669;
66 constexpr uint32_t ACTUAL_CHARACTERISTICS = 2;
67 constexpr uint32_t ACTUAL_COEFFICIENTS = 2;
68 constexpr uint32_t ACTUAL_PRIMARIES = 2;
69
SetUpTestCase()70 void DemuxerFormatNdkTest::SetUpTestCase() {}
TearDownTestCase()71 void DemuxerFormatNdkTest::TearDownTestCase() {}
SetUp()72 void DemuxerFormatNdkTest::SetUp()
73 {
74 memory = OH_AVMemory_Create(g_width * g_height);
75 g_trackCount = 0;
76 }
TearDown()77 void DemuxerFormatNdkTest::TearDown()
78 {
79 if (trackFormat != nullptr) {
80 OH_AVFormat_Destroy(trackFormat);
81 trackFormat = nullptr;
82 }
83
84 if (sourceFormat != nullptr) {
85 OH_AVFormat_Destroy(sourceFormat);
86 sourceFormat = nullptr;
87 }
88
89 if (memory != nullptr) {
90 OH_AVMemory_Destroy(memory);
91 memory = nullptr;
92 }
93 if (source != nullptr) {
94 OH_AVSource_Destroy(source);
95 source = nullptr;
96 }
97 if (demuxer != nullptr) {
98 OH_AVDemuxer_Destroy(demuxer);
99 demuxer = nullptr;
100 }
101 if (avBuffer != nullptr) {
102 OH_AVBuffer_Destroy(avBuffer);
103 avBuffer = nullptr;
104 }
105 if (format != nullptr) {
106 OH_AVFormat_Destroy(format);
107 format = nullptr;
108 }
109 }
110 } // namespace Media
111 } // namespace OHOS
112
113 using namespace std;
114 using namespace OHOS;
115 using namespace OHOS::Media;
116 using namespace testing::ext;
117
118 string g_vvc8bitPath = string("/data/test/media/vvc_8bit_3840_2160.mp4");
119 string g_vvc10bitPath = string("/data/test/media/vvc_aac_10bit_1920_1080.mp4");
120
GetFileSize(const char * fileName)121 static int64_t GetFileSize(const char *fileName)
122 {
123 int64_t fileSize = 0;
124 if (fileName != nullptr) {
125 struct stat fileStatus {};
126 if (stat(fileName, &fileStatus) == 0) {
127 fileSize = static_cast<int64_t>(fileStatus.st_size);
128 }
129 }
130 return fileSize;
131 }
132
SetAudioValue(OH_AVCodecBufferAttr attr,bool & audioIsEnd,int & audioFrame,int & aKeyCount)133 static void SetAudioValue(OH_AVCodecBufferAttr attr, bool &audioIsEnd, int &audioFrame, int &aKeyCount)
134 {
135 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
136 audioIsEnd = true;
137 cout << audioFrame << " audio is end !!!!!!!!!!!!!!!" << endl;
138 } else {
139 audioFrame++;
140 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
141 aKeyCount++;
142 }
143 }
144 }
145
SetVideoValue(OH_AVCodecBufferAttr attr,bool & videoIsEnd,int & videoFrame,int & vKeyCount)146 static void SetVideoValue(OH_AVCodecBufferAttr attr, bool &videoIsEnd, int &videoFrame, int &vKeyCount)
147 {
148 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
149 videoIsEnd = true;
150 cout << videoFrame << " video is end !!!!!!!!!!!!!!!" << endl;
151 } else {
152 videoFrame++;
153 cout << "video track !!!!!" << endl;
154 if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
155 vKeyCount++;
156 }
157 }
158 }
159
CheckVideoKey()160 static void CheckVideoKey()
161 {
162 uint8_t *codecConfig = nullptr;
163 size_t bufferSize;
164 int64_t bitrate = 0;
165 const char* mimeType = nullptr;
166 double frameRate;
167 int32_t currentWidth = 0;
168 int32_t currentHeight = 0;
169 const char* language = nullptr;
170 int32_t rotation;
171 ASSERT_TRUE(OH_AVFormat_GetLongValue(trackFormat, OH_MD_KEY_BITRATE, &bitrate));
172 int bitrateResult = 1660852;
173 ASSERT_EQ(bitrateResult, bitrate);
174 ASSERT_TRUE(OH_AVFormat_GetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, &codecConfig, &bufferSize));
175 size_t bufferSizeResult = 255;
176 ASSERT_EQ(bufferSizeResult, bufferSize);
177 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
178 int expectNum = 0;
179 ASSERT_EQ(expectNum, strcmp(mimeType, OH_AVCODEC_MIMETYPE_VIDEO_VVC));
180 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(trackFormat, OH_MD_KEY_FRAME_RATE, &frameRate));
181 int frameRateResult = 50.000000;
182 ASSERT_EQ(frameRateResult, frameRate);
183 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_HEIGHT, ¤tHeight));
184 int currentHeightResult = 1080;
185 ASSERT_EQ(currentHeightResult, currentHeight);
186 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_WIDTH, ¤tWidth));
187 int currentWidthResult = 1920;
188 ASSERT_EQ(currentWidthResult, currentWidth);
189 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_LANGUAGE, &language));
190 ASSERT_EQ(expectNum, strcmp(language, "und"));
191 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_ROTATION, &rotation));
192 int rotationResult = 0;
193 ASSERT_EQ(rotationResult, rotation);
194 }
195
CheckAudioKey()196 static void CheckAudioKey()
197 {
198 int32_t aacisAdts = 0;
199 int64_t channelLayout;
200 int32_t audioCount = 0;
201 int32_t sampleFormat;
202 int64_t bitrate = 0;
203 int32_t bitsPreCodedSample;
204 uint8_t *codecConfig = nullptr;
205 size_t bufferSize;
206 const char* mimeType = nullptr;
207 int32_t sampleRate = 0;
208 const char* language = nullptr;
209 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_AAC_IS_ADTS, &aacisAdts));
210 int aacisAdtsResult = 1;
211 ASSERT_EQ(aacisAdtsResult, aacisAdts);
212 ASSERT_TRUE(OH_AVFormat_GetLongValue(trackFormat, OH_MD_KEY_CHANNEL_LAYOUT, &channelLayout));
213 int channelLayoutResult = 3;
214 ASSERT_EQ(channelLayoutResult, channelLayout);
215 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, &audioCount));
216 int audioCountResult = 2;
217 ASSERT_EQ(audioCountResult, audioCount);
218 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &sampleFormat));
219 int sampleFormatResult = 0;
220 ASSERT_EQ(sampleFormatResult, sampleFormat);
221 ASSERT_TRUE(OH_AVFormat_GetLongValue(trackFormat, OH_MD_KEY_BITRATE, &bitrate));
222 int bitrateResult = 127881;
223 ASSERT_EQ(bitrateResult, bitrate);
224 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_BITS_PER_CODED_SAMPLE, &bitsPreCodedSample));
225 int bitsPreCodedSampleResult = 16;
226 ASSERT_EQ(bitsPreCodedSampleResult, bitsPreCodedSample);
227 ASSERT_TRUE(OH_AVFormat_GetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, &codecConfig, &bufferSize));
228 int bufferSizeResult = 5;
229 ASSERT_EQ(bufferSizeResult, bufferSize);
230 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
231 int expectNum = 0;
232 ASSERT_EQ(expectNum, strcmp(mimeType, OH_AVCODEC_MIMETYPE_AUDIO_AAC));
233 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_LANGUAGE, &language));
234 ASSERT_EQ(expectNum, strcmp(language, "eng"));
235 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, &sampleRate));
236 int sampleRateResult = 48000;
237 ASSERT_EQ(sampleRateResult, sampleRate);
238 }
239
CheckAudioKeyVvc()240 static void CheckAudioKeyVvc()
241 {
242 uint8_t *codecConfig = nullptr;
243 size_t bufferSize;
244 const char* language = nullptr;
245 int64_t bitrate = 0;
246 double frameRate;
247 int32_t currentWidth = 0;
248 int32_t currentHeight = 0;
249 int tarckType = 0;
250 const char* mimeType = nullptr;
251 int32_t rotation;
252 ASSERT_TRUE(OH_AVFormat_GetLongValue(trackFormat, OH_MD_KEY_BITRATE, &bitrate));
253 int bitrateResult = 10014008;
254 ASSERT_EQ(bitrateResult, bitrate);
255 ASSERT_TRUE(OH_AVFormat_GetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, &codecConfig, &bufferSize));
256 int bufferSizeResult = 247;
257 ASSERT_EQ(bufferSizeResult, bufferSize);
258 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
259 int expectNum = 0;
260 ASSERT_EQ(expectNum, strcmp(mimeType, OH_AVCODEC_MIMETYPE_VIDEO_VVC));
261 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(trackFormat, OH_MD_KEY_FRAME_RATE, &frameRate));
262 int frameRateResult = 60.000000;
263 ASSERT_EQ(frameRateResult, frameRate);
264 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_HEIGHT, ¤tHeight));
265 int currentHeightResult = 2160;
266 ASSERT_EQ(currentHeightResult, currentHeight);
267 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_WIDTH, ¤tWidth));
268 int currentWidthResult = 3840;
269 ASSERT_EQ(currentWidthResult, currentWidth);
270 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_LANGUAGE, &language));
271 ASSERT_EQ(expectNum, strcmp(language, "und"));
272 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_ROTATION, &rotation));
273 int rotationResult = 0;
274 ASSERT_EQ(rotationResult, rotation);
275 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
276 int tarckTypeResult = 1;
277 ASSERT_EQ(tarckTypeResult, tarckType);
278 }
279
SetAllParam(OH_AVFormat * paramFormat)280 static void SetAllParam(OH_AVFormat *paramFormat)
281 {
282 int64_t duration = 0;
283 int64_t startTime = 0;
284 const char* artist = nullptr;
285 const char* album = nullptr;
286 const char* albumArtist = nullptr;
287 const char* date = nullptr;
288 const char* comment = nullptr;
289 const char* genre = nullptr;
290 const char* copyright = nullptr;
291 const char* language = nullptr;
292 const char* description = nullptr;
293 const char* lyrics = nullptr;
294 const char* title = nullptr;
295 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_DURATION, &duration));
296 ASSERT_EQ(ACTUAL_DURATION, duration);
297 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_TITLE, &title));
298 ASSERT_EQ(0, strcmp(title, "title"));
299 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_ARTIST, &artist));
300 ASSERT_EQ(0, strcmp(artist, "artist"));
301 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_ALBUM, &album));
302 ASSERT_EQ(0, strcmp(album, "album"));
303 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_ALBUM_ARTIST, &albumArtist));
304 ASSERT_EQ(0, strcmp(albumArtist, "album artist"));
305 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_DATE, &date));
306 ASSERT_EQ(0, strcmp(date, "2023"));
307 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_COMMENT, &comment));
308 ASSERT_EQ(0, strcmp(comment, "comment"));
309 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_GENRE, &genre));
310 ASSERT_EQ(0, strcmp(genre, "genre"));
311 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_COPYRIGHT, ©right));
312 ASSERT_EQ(0, strcmp(copyright, "Copyright"));
313 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_LANGUAGE, &language));
314 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_DESCRIPTION, &description));
315 ASSERT_EQ(0, strcmp(description, "description"));
316 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_LYRICS, &lyrics));
317 ASSERT_EQ(0, strcmp(lyrics, "lyrics"));
318 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_START_TIME, &startTime));
319 ASSERT_EQ(0, startTime);
320 }
321
SetAudioParam(OH_AVFormat * paramFormat)322 static void SetAudioParam(OH_AVFormat *paramFormat)
323 {
324 int32_t codedSample = 0;
325 int32_t audioFormat = 0;
326 int32_t audioCount = 0;
327 int32_t sampleRate = 0;
328 int32_t aacisAdts = 0;
329 int64_t layout = 0;
330 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &audioFormat));
331 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, &audioCount));
332 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_AUD_SAMPLE_RATE, &sampleRate));
333 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_BITS_PER_CODED_SAMPLE, &codedSample));
334 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_AAC_IS_ADTS, &aacisAdts));
335 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_CHANNEL_LAYOUT, &layout));
336 ASSERT_EQ(ACTUAL_AUDIOFORMAT, audioFormat);
337 ASSERT_EQ(ACTUAL_AUDIOCOUNT, audioCount);
338 ASSERT_EQ(ACTUAL_SAMPLERATE, sampleRate);
339 ASSERT_EQ(ACTUAL_CODEDSAMPLE, codedSample);
340 ASSERT_EQ(1, aacisAdts);
341 ASSERT_EQ(ACTUAL_LAYOUT, layout);
342 }
343
AvcVideoParam(OH_AVFormat * paramFormat)344 static void AvcVideoParam(OH_AVFormat *paramFormat)
345 {
346 int32_t currentWidth = 0;
347 int32_t currentHeight = 0;
348 int32_t rotation = 0;
349 double frameRate = 0.0;
350 int32_t profile = 0;
351 int32_t flag = 0;
352 int32_t characteristics = 0;
353 int32_t coefficients = 0;
354 int32_t mode = 0;
355 int64_t bitrate = 0;
356 int32_t primaries = 0;
357 int32_t videoIsHdrvivid = 0;
358 const char* mimeType = nullptr;
359 double sar = 0.0;
360 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_WIDTH, ¤tWidth));
361 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_HEIGHT, ¤tHeight));
362 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_ROTATION, &rotation));
363 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(paramFormat, OH_MD_KEY_FRAME_RATE, &frameRate));
364 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_PROFILE, &profile));
365 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_BITRATE, &bitrate));
366 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, &mode));
367 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_RANGE_FLAG, &flag));
368 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_TRANSFER_CHARACTERISTICS, &characteristics));
369 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_MATRIX_COEFFICIENTS, &coefficients));
370 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_VIDEO_IS_HDR_VIVID, &videoIsHdrvivid));
371 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_COLOR_PRIMARIES, &primaries));
372 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(paramFormat, OH_MD_KEY_VIDEO_SAR, &sar));
373 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
374 ASSERT_EQ(0, strcmp(mimeType, OH_AVCODEC_MIMETYPE_VIDEO_AVC));
375 ASSERT_EQ(ACTUAL_CURRENTWIDTH, currentWidth);
376 ASSERT_EQ(ACTUAL_CURRENTHEIGHT, currentHeight);
377 ASSERT_EQ(ACTUAL_FRAMERATE, frameRate);
378 ASSERT_EQ(AVC_BITRATE, bitrate);
379 ASSERT_EQ(1, sar);
380 }
381
HevcVideoParam(OH_AVFormat * paramFormat)382 static void HevcVideoParam(OH_AVFormat *paramFormat)
383 {
384 int32_t currentWidth = 0;
385 int32_t currentHeight = 0;
386 int32_t rotation = 0;
387 double frameRate = 0.0;
388 int32_t profile = 0;
389 int32_t flag = 0;
390 int32_t characteristics = 0;
391 int32_t coefficients = 0;
392 int32_t mode = 0;
393 int64_t bitrate = 0;
394 int32_t primaries = 0;
395 int32_t videoIsHdrvivid = 0;
396 const char* mimeType = nullptr;
397 double sar = 0.0;
398 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_WIDTH, ¤tWidth));
399 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_HEIGHT, ¤tHeight));
400 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_ROTATION, &rotation));
401 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(paramFormat, OH_MD_KEY_FRAME_RATE, &frameRate));
402 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_BITRATE, &bitrate));
403 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, &mode));
404 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_RANGE_FLAG, &flag));
405 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_TRANSFER_CHARACTERISTICS, &characteristics));
406 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_MATRIX_COEFFICIENTS, &coefficients));
407 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_VIDEO_IS_HDR_VIVID, &videoIsHdrvivid));
408 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_COLOR_PRIMARIES, &primaries));
409 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(paramFormat, OH_MD_KEY_VIDEO_SAR, &sar));
410 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
411 if (!access("/system/lib64/media/", 0)) {
412 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_PROFILE, &profile));
413 ASSERT_EQ(0, profile);
414 } else {
415 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_PROFILE, &profile));
416 }
417 ASSERT_EQ(0, strcmp(mimeType, OH_AVCODEC_MIMETYPE_VIDEO_HEVC));
418 ASSERT_EQ(ACTUAL_CURRENTWIDTH, currentWidth);
419 ASSERT_EQ(ACTUAL_CURRENTHEIGHT, currentHeight);
420 ASSERT_EQ(ACTUAL_FRAMERATE, frameRate);
421 ASSERT_EQ(HEVC_BITRATE, bitrate);
422 ASSERT_EQ(0, flag);
423 ASSERT_EQ(ACTUAL_CHARACTERISTICS, characteristics);
424 ASSERT_EQ(ACTUAL_COEFFICIENTS, coefficients);
425 ASSERT_EQ(ACTUAL_PRIMARIES, primaries);
426 ASSERT_EQ(1, sar);
427 }
428
SetOtherAllParam(OH_AVFormat * paramFormat)429 static void SetOtherAllParam(OH_AVFormat *paramFormat)
430 {
431 int64_t duration = 0;
432 int64_t startTime = 0;
433 const char* artist = nullptr;
434 const char* album = nullptr;
435 const char* albumArtist = nullptr;
436 const char* date = nullptr;
437 const char* comment = nullptr;
438 const char* genre = nullptr;
439 const char* copyright = nullptr;
440 const char* language = nullptr;
441 const char* description = nullptr;
442 const char* lyrics = nullptr;
443 const char* title = nullptr;
444 int32_t dur = 10800000;
445 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_DURATION, &duration));
446 ASSERT_EQ(dur, duration);
447 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_TITLE, &title));
448 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_ARTIST, &artist));
449 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_ALBUM, &album));
450 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_ALBUM_ARTIST, &albumArtist));
451 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_DATE, &date));
452 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_COMMENT, &comment));
453 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_GENRE, &genre));
454 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_COPYRIGHT, ©right));
455 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_LANGUAGE, &language));
456 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_DESCRIPTION, &description));
457 ASSERT_FALSE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_LYRICS, &lyrics));
458 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_START_TIME, &startTime));
459 ASSERT_EQ(0, startTime);
460 }
461
SetOtherAudioParam(OH_AVFormat * paramFormat)462 static void SetOtherAudioParam(OH_AVFormat *paramFormat)
463 {
464 int32_t codedSample = 0;
465 int32_t audioFormat = 0;
466 int32_t audioCount = 0;
467 int32_t sampleRate = 0;
468 int32_t aacisAdts = 0;
469 int64_t layout = 0;
470 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &audioFormat));
471 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, &audioCount));
472 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_AUD_SAMPLE_RATE, &sampleRate));
473 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_BITS_PER_CODED_SAMPLE, &codedSample));
474 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_AAC_IS_ADTS, &aacisAdts));
475 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_CHANNEL_LAYOUT, &layout));
476 ASSERT_EQ(ACTUAL_AUDIOFORMAT, audioFormat);
477 ASSERT_EQ(ACTUAL_AUDIOCOUNT, audioCount);
478 ASSERT_EQ(ACTUAL_SAMPLERATE, sampleRate);
479 ASSERT_EQ(ACTUAL_CODEDSAMPLE, codedSample);
480 ASSERT_EQ(1, aacisAdts);
481 ASSERT_EQ(ACTUAL_LAYOUT, layout);
482 }
483
OtherVideoParam(OH_AVFormat * paramFormat)484 static void OtherVideoParam(OH_AVFormat *paramFormat)
485 {
486 int32_t currentWidth = 0;
487 int32_t currentHeight = 0;
488 int32_t rotation = 0;
489 double frameRate = 0.0;
490 int32_t profile = 0;
491 int32_t flag = 0;
492 int32_t characteristics = 0;
493 int32_t coefficients = 0;
494 int32_t mode = 0;
495 int64_t bitrate = 0;
496 int32_t primaries = 0;
497 int32_t videoIsHdrvivid = 0;
498 const char* mimeType = nullptr;
499 double sar = 0.0;
500 int32_t width = 3840;
501 int32_t height = 2160;
502 int32_t framerateActual = 30;
503 int32_t bitrateActual = 24863756;
504 int32_t rotationActual = 180;
505 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_WIDTH, ¤tWidth));
506 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_HEIGHT, ¤tHeight));
507 ASSERT_TRUE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_ROTATION, &rotation));
508 ASSERT_TRUE(OH_AVFormat_GetDoubleValue(paramFormat, OH_MD_KEY_FRAME_RATE, &frameRate));
509 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_PROFILE, &profile));
510 ASSERT_TRUE(OH_AVFormat_GetLongValue(paramFormat, OH_MD_KEY_BITRATE, &bitrate));
511 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, &mode));
512 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_RANGE_FLAG, &flag));
513 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_TRANSFER_CHARACTERISTICS, &characteristics));
514 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_MATRIX_COEFFICIENTS, &coefficients));
515 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_VIDEO_IS_HDR_VIVID, &videoIsHdrvivid));
516 ASSERT_FALSE(OH_AVFormat_GetIntValue(paramFormat, OH_MD_KEY_COLOR_PRIMARIES, &primaries));
517 ASSERT_FALSE(OH_AVFormat_GetDoubleValue(paramFormat, OH_MD_KEY_VIDEO_SAR, &sar));
518 ASSERT_TRUE(OH_AVFormat_GetStringValue(paramFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
519 ASSERT_EQ(0, strcmp(mimeType, OH_AVCODEC_MIMETYPE_VIDEO_AVC));
520 ASSERT_EQ(width, currentWidth);
521 ASSERT_EQ(height, currentHeight);
522 ASSERT_EQ(framerateActual, frameRate);
523 ASSERT_EQ(bitrateActual, bitrate);
524 ASSERT_EQ(rotationActual, rotation);
525 }
526
527 /**
528 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_4400
529 * @tc.name : demux hevc ts video
530 * @tc.desc : function test
531 */
532 HWTEST_F(DemuxerFormatNdkTest, SUB_MEDIA_DEMUXER_PROCESS_4400, TestSize.Level0)
533 {
534 int tarckType = 0;
535 const char *file = "/data/test/media/test_265_B_Gop25_4sec.mp4";
536 int fd = open(file, O_RDONLY);
537 int64_t size = GetFileSize(file);
538 cout << file << "----------------------" << fd << "---------" << size << endl;
539 source = OH_AVSource_CreateWithFD(fd, 0, size);
540 ASSERT_NE(source, nullptr);
541 demuxer = OH_AVDemuxer_CreateWithSource(source);
542 ASSERT_NE(demuxer, nullptr);
543 sourceFormat = OH_AVSource_GetSourceFormat(source);
544 SetAllParam(sourceFormat);
545 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
546 ASSERT_EQ(2, g_trackCount);
547 const char* mimeType = nullptr;
548 for (int32_t index = 0; index < g_trackCount; index++) {
549 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
550 }
551 OH_AVCodecBufferAttr attr;
552 int vKeyCount = 0;
553 int aKeyCount = 0;
554 bool audioIsEnd = false;
555 bool videoIsEnd = false;
556 int audioFrame = 0;
557 int videoFrame = 0;
558 while (!audioIsEnd || !videoIsEnd) {
559 for (int32_t index = 0; index < g_trackCount; index++) {
560 trackFormat = OH_AVSource_GetTrackFormat(source, index);
561 ASSERT_NE(trackFormat, nullptr);
562 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
563 if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD) && index == MEDIA_TYPE_AUD) ||
564 (videoIsEnd && (tarckType == MEDIA_TYPE_VID) && index == MEDIA_TYPE_VID)) {
565 continue;
566 }
567 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
568 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
569 if (tarckType == MEDIA_TYPE_AUD) {
570 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
571 SetAudioParam(trackFormat);
572 ASSERT_EQ(0, strcmp(mimeType, OH_AVCODEC_MIMETYPE_AUDIO_AAC));
573 } else if (tarckType == MEDIA_TYPE_VID) {
574 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
575 HevcVideoParam(trackFormat);
576 }
577 OH_AVFormat_Destroy(trackFormat);
578 trackFormat = nullptr;
579 }
580 }
581 close(fd);
582 }
583
584 /**
585 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_4500
586 * @tc.name : demux avc ts video
587 * @tc.desc : function test
588 */
589 HWTEST_F(DemuxerFormatNdkTest, SUB_MEDIA_DEMUXER_PROCESS_4500, TestSize.Level0)
590 {
591 int tarckType = 0;
592 const char *file = "/data/test/media/test_264_B_Gop25_4sec.mp4";
593 int fd = open(file, O_RDONLY);
594 int64_t size = GetFileSize(file);
595 cout << file << "----------------------" << fd << "---------" << size << endl;
596 source = OH_AVSource_CreateWithFD(fd, 0, size);
597 ASSERT_NE(source, nullptr);
598 demuxer = OH_AVDemuxer_CreateWithSource(source);
599 ASSERT_NE(demuxer, nullptr);
600 sourceFormat = OH_AVSource_GetSourceFormat(source);
601 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
602 ASSERT_EQ(2, g_trackCount);
603 SetAllParam(sourceFormat);
604 const char* mimeType = nullptr;
605 for (int32_t index = 0; index < g_trackCount; index++) {
606 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
607 }
608 OH_AVCodecBufferAttr attr;
609 int vKeyCount = 0;
610 int aKeyCount = 0;
611 bool audioIsEnd = false;
612 bool videoIsEnd = false;
613 int audioFrame = 0;
614 int videoFrame = 0;
615 while (!audioIsEnd || !videoIsEnd) {
616 for (int32_t index = 0; index < g_trackCount; index++) {
617 trackFormat = OH_AVSource_GetTrackFormat(source, index);
618 ASSERT_NE(trackFormat, nullptr);
619 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
620 if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD) && index == MEDIA_TYPE_AUD)
621 || (videoIsEnd && (tarckType == MEDIA_TYPE_VID) && index == MEDIA_TYPE_VID)) {
622 continue;
623 }
624 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
625 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
626 if (tarckType == MEDIA_TYPE_AUD) {
627 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
628 SetAudioParam(trackFormat);
629 ASSERT_EQ(0, strcmp(mimeType, OH_AVCODEC_MIMETYPE_AUDIO_AAC));
630 } else if (tarckType == MEDIA_TYPE_VID) {
631 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
632 AvcVideoParam(trackFormat);
633 }
634 OH_AVFormat_Destroy(trackFormat);
635 trackFormat = nullptr;
636 }
637 }
638 close(fd);
639 }
640 /**
641 * @tc.number : SUB_MEDIA_DEMUXER_PROCESS_4510
642 * @tc.name : demux avc ios video, check key
643 * @tc.desc : function test
644 */
645 HWTEST_F(DemuxerFormatNdkTest, SUB_MEDIA_DEMUXER_PROCESS_4510, TestSize.Level0)
646 {
647 int tarckType = 0;
648 const char *file = "/data/test/media/record_from_ios.mp4";
649 int fd = open(file, O_RDONLY);
650 int64_t size = GetFileSize(file);
651 cout << file << "----------------------" << fd << "---------" << size << endl;
652 source = OH_AVSource_CreateWithFD(fd, 0, size);
653 ASSERT_NE(source, nullptr);
654 demuxer = OH_AVDemuxer_CreateWithSource(source);
655 ASSERT_NE(demuxer, nullptr);
656 sourceFormat = OH_AVSource_GetSourceFormat(source);
657 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
658 ASSERT_EQ(5, g_trackCount);
659 SetOtherAllParam(sourceFormat);
660 const char* mimeType = nullptr;
661 for (int32_t index = 0; index < 2; index++) {
662 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
663 }
664 OH_AVCodecBufferAttr attr;
665 int vKeyCount = 0;
666 int aKeyCount = 0;
667 bool audioIsEnd = false;
668 bool videoIsEnd = false;
669 int audioFrame = 0;
670 int videoFrame = 0;
671 while (!audioIsEnd || !videoIsEnd) {
672 for (int32_t index = 0; index < 2; index++) {
673 trackFormat = OH_AVSource_GetTrackFormat(source, index);
674 ASSERT_NE(trackFormat, nullptr);
675 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
676 if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD) && index == MEDIA_TYPE_AUD)
677 || (videoIsEnd && (tarckType == MEDIA_TYPE_VID) && index == MEDIA_TYPE_VID)) {
678 continue;
679 }
680 ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
681 ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &mimeType));
682 if (tarckType == MEDIA_TYPE_AUD) {
683 SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
684 SetOtherAudioParam(trackFormat);
685 ASSERT_EQ(0, strcmp(mimeType, OH_AVCODEC_MIMETYPE_AUDIO_AAC));
686 } else if (tarckType == MEDIA_TYPE_VID) {
687 SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
688 OtherVideoParam(trackFormat);
689 }
690 OH_AVFormat_Destroy(trackFormat);
691 trackFormat = nullptr;
692 }
693 }
694 close(fd);
695 }
696 /**
697 * @tc.number : VIDEO_DEMUXER_VVC_0500
698 * @tc.name : demuxer 8bit H266 MP4 file, check key
699 * @tc.desc : function test
700 */
701 HWTEST_F(DemuxerFormatNdkTest, VIDEO_DEMUXER_VVC_0500, TestSize.Level0)
702 {
703 if (access(g_vvc8bitPath.c_str(), F_OK) != 0) {
704 return;
705 }
706 int64_t duration = 0;
707 int64_t startTime;
708 int fd = open(g_vvc8bitPath.c_str(), O_RDONLY);
709 int64_t size = GetFileSize(g_vvc8bitPath.c_str());
710 cout << g_vvc8bitPath.c_str() << "---------" << fd << "----------" << size <<endl;
711 source = OH_AVSource_CreateWithFD(fd, 0, size);
712 ASSERT_NE(source, nullptr);
713 sourceFormat = OH_AVSource_GetSourceFormat(source);
714 ASSERT_NE(sourceFormat, nullptr);
715 trackFormat = OH_AVSource_GetTrackFormat(source, 0);
716 ASSERT_NE(trackFormat, nullptr);
717 ASSERT_TRUE(OH_AVFormat_GetLongValue(sourceFormat, OH_MD_KEY_DURATION, &duration));
718 ASSERT_EQ(10000000, duration);
719 ASSERT_TRUE(OH_AVFormat_GetLongValue(sourceFormat, OH_MD_KEY_START_TIME, &startTime));
720 ASSERT_EQ(0, startTime);
721 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
722 ASSERT_EQ(1, g_trackCount);
723 CheckAudioKeyVvc();
724 close(fd);
725 }
726
727 /**
728 * @tc.number : VIDEO_DEMUXER_VVC_0600
729 * @tc.name : demuxer 10bit H266 MP4 file, check key
730 * @tc.desc : function test
731 */
732 HWTEST_F(DemuxerFormatNdkTest, VIDEO_DEMUXER_VVC_0600, TestSize.Level0)
733 {
734 if (access(g_vvc10bitPath.c_str(), F_OK) != 0) {
735 return;
736 }
737 int64_t duration = 0;
738 int64_t startTime;
739 int tarckType = 0;
740 int fd = open(g_vvc10bitPath.c_str(), O_RDONLY);
741 int64_t size = GetFileSize(g_vvc10bitPath.c_str());
742 cout << g_vvc10bitPath.c_str() << "---------" << fd << "----------" << size <<endl;
743 source = OH_AVSource_CreateWithFD(fd, 0, size);
744 ASSERT_NE(source, nullptr);
745 sourceFormat = OH_AVSource_GetSourceFormat(source);
746 ASSERT_NE(sourceFormat, nullptr);
747 ASSERT_TRUE(OH_AVFormat_GetLongValue(sourceFormat, OH_MD_KEY_DURATION, &duration));
748 ASSERT_EQ(60000000, duration);
749 ASSERT_TRUE(OH_AVFormat_GetLongValue(sourceFormat, OH_MD_KEY_START_TIME, &startTime));
750 ASSERT_EQ(0, startTime);
751 ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
752 ASSERT_EQ(2, g_trackCount);
753 for (int32_t index = 0; index < g_trackCount; index++) {
754 trackFormat = OH_AVSource_GetTrackFormat(source, 0);
755 ASSERT_NE(trackFormat, nullptr);
756 ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
757 OH_AVFormat_Destroy(trackFormat);
758 trackFormat = nullptr;
759 if (tarckType == MEDIA_TYPE_VID) {
760 CheckVideoKey();
761 } else if (tarckType == MEDIA_TYPE_AUD) {
762 CheckAudioKey();
763 }
764 }
765 close(fd);
766 }
767