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 "avmuxer_unit_test.h"
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <vector>
20 #include <fcntl.h>
21 #include "avmuxer.h"
22 #include "native_avbuffer.h"
23 #include "native_audio_channel_layout.h"
24 #ifdef AVMUXER_UNITTEST_CAPI
25 #include "native_avmuxer.h"
26 #include "native_avformat.h"
27 #endif
28
29 using namespace testing::ext;
30 using namespace OHOS::MediaAVCodec;
31 using namespace OHOS::Media;
32 namespace {
33 constexpr int32_t TEST_CHANNEL_COUNT = 2;
34 constexpr int32_t TEST_SAMPLE_RATE = 2;
35 constexpr int32_t TEST_WIDTH = 720;
36 constexpr int32_t TEST_HEIGHT = 480;
37 constexpr int32_t TEST_ROTATION = 90;
38 constexpr int32_t INVALID_FORMAT = -99;
39 const std::string TEST_FILE_PATH = "/data/test/media/";
40 const std::string INPUT_FILE_PATH = "/data/test/media/h264_720_480.dat";
41 const std::string LOGINFO_INPUT_FILE_PATH = "/data/test/media/h265_logInfo.dat";
42 const std::string HEVC_LIB_PATH = std::string(AV_CODEC_PATH) + "/libav_codec_hevc_parser.z.so";
43 constexpr uint32_t AVCODEC_BUFFER_FLAGS_DISPOSABLE_EXT_TEST = 1 << 6;
44 const std::string TIMED_METADATA_TRACK_MIMETYPE = "meta/timed-metadata";
45 const std::string TIMED_METADATA_KEY = "com.openharmony.timed_metadata.test";
46 const std::string TRACK_REF_TYPE_DEPTH = "vdep";
47 const std::string TRACK_REF_TYPE_PREY = "auxl";
48 const std::string TRACK_REF_TYPE_AUDIO = "auxl";
49 const std::string AUXILIARY_DEPTH_TRACK_KEY = "com.openharmony.moviemode.depth";
50 const std::string AUXILIARY_PREY_TRACK_KEY = "com.openharmony.moviemode.prey";
51 const std::string AUXILIARY_AUDIO_TRACK_KEY = "com.openharmony.audiomode.auxiliary";
52 } // namespace
53
SetUpTestCase()54 void AVMuxerUnitTest::SetUpTestCase() {}
55
TearDownTestCase()56 void AVMuxerUnitTest::TearDownTestCase() {}
57
SetUp()58 void AVMuxerUnitTest::SetUp()
59 {
60 avmuxer_ = std::make_shared<AVMuxerSample>();
61 }
62
TearDown()63 void AVMuxerUnitTest::TearDown()
64 {
65 if (fd_ >= 0) {
66 close(fd_);
67 fd_ = -1;
68 }
69
70 if (inputFile_ != nullptr) {
71 if (inputFile_->is_open()) {
72 inputFile_->close();
73 }
74 }
75
76 if (avmuxer_ != nullptr) {
77 avmuxer_->Destroy();
78 avmuxer_ = nullptr;
79 }
80 }
81
WriteSample(int32_t trackId,std::shared_ptr<std::ifstream> file,bool & eosFlag,uint32_t flag)82 int32_t AVMuxerUnitTest::WriteSample(int32_t trackId, std::shared_ptr<std::ifstream> file,
83 bool &eosFlag, uint32_t flag)
84 {
85 OH_AVCodecBufferAttr info;
86
87 if (file->eof()) {
88 eosFlag = true;
89 return 0;
90 }
91 file->read(reinterpret_cast<char*>(&info.pts), sizeof(info.pts));
92
93 if (file->eof()) {
94 eosFlag = true;
95 return 0;
96 }
97 file->read(reinterpret_cast<char*>(&info.flags), sizeof(info.flags));
98
99 if (file->eof()) {
100 eosFlag = true;
101 return 0;
102 }
103 file->read(reinterpret_cast<char*>(&info.size), sizeof(info.size));
104
105 if (file->eof()) {
106 eosFlag = true;
107 return 0;
108 }
109
110 if (info.flags & AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
111 info.flags |= flag;
112 }
113
114 OH_AVBuffer *buffer = OH_AVBuffer_Create(info.size);
115 file->read(reinterpret_cast<char*>(OH_AVBuffer_GetAddr(buffer)), info.size);
116 OH_AVBuffer_SetBufferAttr(buffer, &info);
117 int32_t ret = avmuxer_->WriteSampleBuffer(trackId, buffer);
118 OH_AVBuffer_Destroy(buffer);
119 return ret;
120 }
121
WriteSample(sptr<AVBufferQueueProducer> bqProducer,std::shared_ptr<std::ifstream> file,bool & eosFlag)122 int32_t AVMuxerUnitTest::WriteSample(sptr<AVBufferQueueProducer> bqProducer,
123 std::shared_ptr<std::ifstream> file, bool &eosFlag)
124 {
125 if (file->eof()) {
126 eosFlag = true;
127 return 0;
128 }
129 int64_t pts = 0;
130 file->read(reinterpret_cast<char*>(&pts), sizeof(pts));
131 if (file->eof()) {
132 eosFlag = true;
133 return 0;
134 }
135 uint32_t flags = 0;
136 file->read(reinterpret_cast<char*>(&flags), sizeof(flags));
137 if (file->eof()) {
138 eosFlag = true;
139 return 0;
140 }
141 int32_t size = 0;
142 file->read(reinterpret_cast<char*>(&size), sizeof(size));
143 if (file->eof()) {
144 eosFlag = true;
145 return 0;
146 }
147 std::shared_ptr<AVBuffer> buffer = nullptr;
148 AVBufferConfig avBufferConfig;
149 avBufferConfig.size = size;
150 avBufferConfig.memoryType = MemoryType::VIRTUAL_MEMORY;
151 bqProducer->RequestBuffer(buffer, avBufferConfig, -1);
152 buffer->pts_ = pts;
153 buffer->flag_ = flags;
154 file->read(reinterpret_cast<char*>(buffer->memory_->GetAddr()), size);
155 buffer->memory_->SetSize(size);
156 Status ret = bqProducer->PushBuffer(buffer, true);
157 if (ret == Status::OK) {
158 return 0;
159 }
160 return -1;
161 }
162
AuxiliaryWriteSample(std::string inputFilePath,int32_t trackId)163 void AVMuxerUnitTest::AuxiliaryWriteSample(std::string inputFilePath, int32_t trackId)
164 {
165 inputFile_ = std::make_shared<std::ifstream>(inputFilePath, std::ios::binary);
166
167 int32_t extSize = 0;
168 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
169 if (extSize > 0) {
170 std::vector<uint8_t> buffer(extSize);
171 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
172 }
173
174 bool eosFlag = false;
175 uint32_t flag = AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
176 int32_t ret = WriteSample(trackId, inputFile_, eosFlag, flag);
177 while (!eosFlag && (ret == 0)) {
178 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
179 }
180 ASSERT_EQ(ret, 0);
181 }
182
183 namespace {
184 /**
185 * @tc.name: Muxer_Create_001
186 * @tc.desc: Muxer Create
187 * @tc.type: FUNC
188 */
189 HWTEST_F(AVMuxerUnitTest, Muxer_Create_001, TestSize.Level0)
190 {
191 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Create.mp4");
192 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
193 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
194 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
195 ASSERT_TRUE(isCreated);
196 }
197
198 /**
199 * @tc.name: Muxer_Create_002
200 * @tc.desc: Muxer Create write only
201 * @tc.type: FUNC
202 */
203 HWTEST_F(AVMuxerUnitTest, Muxer_Create_002, TestSize.Level0)
204 {
205 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Create.mp4");
206 fd_ = open(outputFile.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
207 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
208 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
209 ASSERT_TRUE(isCreated);
210 }
211
212 /**
213 * @tc.name: Muxer_Create_003
214 * @tc.desc: Muxer Create read only
215 * @tc.type: FUNC
216 */
217 HWTEST_F(AVMuxerUnitTest, Muxer_Create_003, TestSize.Level0)
218 {
219 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Create.mp4");
220 fd_ = open(outputFile.c_str(), O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
221 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
222 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
223 ASSERT_FALSE(isCreated);
224 }
225
226 /**
227 * @tc.name: Muxer_Create_004
228 * @tc.desc: Muxer Create rand fd
229 * @tc.type: FUNC
230 */
231 HWTEST_F(AVMuxerUnitTest, Muxer_Create_004, TestSize.Level0)
232 {
233 constexpr int32_t invalidFd = 999999;
234 constexpr int32_t negativeFd = -999;
235 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
236 bool isCreated = avmuxer_->CreateMuxer(invalidFd, outputFormat);
237 ASSERT_FALSE(isCreated);
238
239 avmuxer_->Destroy();
240 isCreated = avmuxer_->CreateMuxer(0xfffffff, outputFormat);
241 ASSERT_FALSE(isCreated);
242
243 avmuxer_->Destroy();
244 isCreated = avmuxer_->CreateMuxer(-1, outputFormat);
245 ASSERT_FALSE(isCreated);
246
247 avmuxer_->Destroy();
248 isCreated = avmuxer_->CreateMuxer(negativeFd, outputFormat);
249 ASSERT_FALSE(isCreated);
250 }
251
252 /**
253 * @tc.name: Muxer_Create_005
254 * @tc.desc: Muxer Create different outputFormat
255 * @tc.type: FUNC
256 */
257 HWTEST_F(AVMuxerUnitTest, Muxer_Create_005, TestSize.Level0)
258 {
259 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Create.mp4");
260 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
261
262 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
263 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
264 ASSERT_TRUE(isCreated);
265
266 avmuxer_->Destroy();
267 outputFormat = AV_OUTPUT_FORMAT_M4A;
268 isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
269 ASSERT_TRUE(isCreated);
270
271 avmuxer_->Destroy();
272 outputFormat = AV_OUTPUT_FORMAT_AMR;
273 isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
274 ASSERT_TRUE(isCreated);
275
276 avmuxer_->Destroy();
277 outputFormat = AV_OUTPUT_FORMAT_DEFAULT;
278 isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
279 ASSERT_TRUE(isCreated);
280
281 avmuxer_->Destroy();
282 outputFormat = AV_OUTPUT_FORMAT_MP3;
283 isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
284 ASSERT_TRUE(isCreated);
285
286 avmuxer_->Destroy();
287 outputFormat = AV_OUTPUT_FORMAT_WAV;
288 isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
289 ASSERT_TRUE(isCreated);
290
291 avmuxer_->Destroy();
292 outputFormat = AV_OUTPUT_FORMAT_AAC;
293 isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
294 ASSERT_TRUE(isCreated);
295
296 avmuxer_->Destroy();
297 isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(INVALID_FORMAT));
298 ASSERT_FALSE(isCreated);
299 }
300
301 /**
302 * @tc.name: Muxer_AddTrack_001
303 * @tc.desc: Muxer AddTrack add audio track
304 * @tc.type: FUNC
305 */
306 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_001, TestSize.Level0)
307 {
308 int32_t audioTrackId = -1;
309 int32_t ret = 0;
310 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack.mp4");
311 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
312 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
313 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
314 ASSERT_TRUE(isCreated);
315
316 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
317 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
318 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, TEST_SAMPLE_RATE);
319 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, TEST_CHANNEL_COUNT);
320 ret = avmuxer_->AddTrack(audioTrackId, audioParams);
321 EXPECT_EQ(ret, AV_ERR_OK);
322 EXPECT_GE(audioTrackId, 0);
323
324 audioParams = FormatMockFactory::CreateFormat();
325 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, TEST_SAMPLE_RATE);
326 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, TEST_CHANNEL_COUNT);
327 ret = avmuxer_->AddTrack(audioTrackId, audioParams);
328 EXPECT_NE(ret, AV_ERR_OK);
329
330 audioParams = FormatMockFactory::CreateFormat();
331 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
332 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, TEST_CHANNEL_COUNT);
333 ret = avmuxer_->AddTrack(audioTrackId, audioParams);
334 EXPECT_NE(ret, AV_ERR_OK);
335
336 audioParams = FormatMockFactory::CreateFormat();
337 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
338 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, TEST_SAMPLE_RATE);
339 ret = avmuxer_->AddTrack(audioTrackId, audioParams);
340 EXPECT_NE(ret, AV_ERR_OK);
341 }
342
343 /**
344 * @tc.name: Muxer_AddTrack_002
345 * @tc.desc: Muxer AddTrack add video track
346 * @tc.type: FUNC
347 */
348 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_002, TestSize.Level0)
349 {
350 int32_t videoTrackId = -1;
351 int32_t ret = AV_ERR_INVALID_VAL;
352 std::string outputFile = TEST_FILE_PATH + std::string("avmuxer_AddTrack_002.mp4");
353 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
354 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
355 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
356 ASSERT_TRUE(isCreated);
357
358 std::shared_ptr<FormatMock> videoParams = FormatMockFactory::CreateFormat();
359 videoParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
360 videoParams->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
361 videoParams->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
362 ret = avmuxer_->AddTrack(videoTrackId, videoParams);
363 EXPECT_EQ(ret, AV_ERR_OK);
364 EXPECT_GE(videoTrackId, 0);
365
366 videoParams = FormatMockFactory::CreateFormat();
367 videoParams->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
368 videoParams->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
369 ret = avmuxer_->AddTrack(videoTrackId, videoParams);
370 EXPECT_NE(ret, AV_ERR_OK);
371
372 videoParams = FormatMockFactory::CreateFormat();
373 videoParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
374 videoParams->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
375 ret = avmuxer_->AddTrack(videoTrackId, videoParams);
376 EXPECT_NE(ret, AV_ERR_OK);
377
378 videoParams = FormatMockFactory::CreateFormat();
379 videoParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
380 videoParams->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
381 ret = avmuxer_->AddTrack(videoTrackId, videoParams);
382 EXPECT_NE(ret, AV_ERR_OK);
383 }
384
385 /**
386 * @tc.name: Muxer_AddTrack_003
387 * @tc.desc: Muxer AddTrack after Start()
388 * @tc.type: FUNC
389 */
390 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_003, TestSize.Level0)
391 {
392 int32_t audioTrackId = -1;
393 int32_t videoTrackId = -1;
394 int32_t ret = AV_ERR_INVALID_VAL;
395 std::string outputFile = TEST_FILE_PATH + std::string("avmuxer_AddTrack_003.mp4");
396 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
397 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
398 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
399 ASSERT_TRUE(isCreated);
400
401 std::shared_ptr<FormatMock> videoParams =
402 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
403 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
404 ret = avmuxer_->AddTrack(videoTrackId, videoParams);
405 EXPECT_EQ(ret, AV_ERR_OK);
406 EXPECT_GE(videoTrackId, 0);
407
408 ASSERT_EQ(avmuxer_->Start(), 0);
409 std::shared_ptr<FormatMock> audioParams =
410 FormatMockFactory::CreateAudioFormat(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, TEST_SAMPLE_RATE, TEST_CHANNEL_COUNT);
411 ret = avmuxer_->AddTrack(audioTrackId, audioParams);
412 EXPECT_NE(ret, AV_ERR_OK);
413 }
414
415 /**
416 * @tc.name: Muxer_AddTrack_004
417 * @tc.desc: Muxer AddTrack mimeType test
418 * @tc.type: FUNC
419 */
420 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_004, TestSize.Level0)
421 {
422 int32_t trackId = -1;
423 int32_t ret = 0;
424 const std::vector<std::string_view> testMp4MimeTypeList =
425 {
426 OH_AVCODEC_MIMETYPE_AUDIO_MPEG,
427 OH_AVCODEC_MIMETYPE_AUDIO_AAC,
428 OH_AVCODEC_MIMETYPE_VIDEO_AVC,
429 OH_AVCODEC_MIMETYPE_IMAGE_JPG,
430 OH_AVCODEC_MIMETYPE_IMAGE_PNG,
431 OH_AVCODEC_MIMETYPE_IMAGE_BMP,
432 };
433
434 const std::vector<std::string_view> testM4aMimeTypeList =
435 {
436 OH_AVCODEC_MIMETYPE_AUDIO_AAC,
437 OH_AVCODEC_MIMETYPE_IMAGE_JPG,
438 OH_AVCODEC_MIMETYPE_IMAGE_PNG,
439 OH_AVCODEC_MIMETYPE_IMAGE_BMP,
440 };
441
442 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack.mp4");
443 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
444 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
445
446 std::shared_ptr<FormatMock> avParam = FormatMockFactory::CreateFormat();
447 avParam->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, TEST_SAMPLE_RATE);
448 avParam->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, TEST_CHANNEL_COUNT);
449 avParam->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
450 avParam->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
451
452 for (uint32_t i = 0; i < testMp4MimeTypeList.size(); ++i) {
453 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
454 ASSERT_TRUE(isCreated);
455 avParam->PutStringValue(OH_MD_KEY_CODEC_MIME, testMp4MimeTypeList[i]);
456 ret = avmuxer_->AddTrack(trackId, avParam);
457 EXPECT_EQ(ret, AV_ERR_OK) << "AddTrack failed: i:" << i << " mimeType:" << testMp4MimeTypeList[i];
458 EXPECT_EQ(trackId, 0) << "i:" << i << " TrackId:" << trackId << " mimeType:" << testMp4MimeTypeList[i];
459 }
460
461 // need to change libohosffmpeg.z.so, muxer build config add ipod
462 avmuxer_->Destroy();
463 outputFormat = AV_OUTPUT_FORMAT_M4A;
464 avParam->PutIntValue(OH_MD_KEY_PROFILE, AAC_PROFILE_LC);
465 avParam->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
466 for (uint32_t i = 0; i < testM4aMimeTypeList.size(); ++i) {
467 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
468 ASSERT_TRUE(isCreated);
469 avParam->PutStringValue(OH_MD_KEY_CODEC_MIME, testM4aMimeTypeList[i]);
470 ret = avmuxer_->AddTrack(trackId, avParam);
471 EXPECT_EQ(ret, AV_ERR_OK) << "AddTrack failed: i:" << i << " mimeType:" << testM4aMimeTypeList[i];
472 EXPECT_EQ(trackId, 0) << "i:" << i << " TrackId:" << trackId << " mimeType:" << testM4aMimeTypeList[i];
473 }
474 }
475
476 /**
477 * @tc.name: Muxer_AddTrack_0091
478 * @tc.desc: Muxer AddTrack while create by unexpected outputFormat
479 * @tc.type: FUNC
480 */
481 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_0091, TestSize.Level0)
482 {
483 int32_t audioTrackId = -1; // -1 track
484 int32_t ret = 0;
485 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack.mp3");
486 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
487 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MP3;
488 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
489 ASSERT_TRUE(isCreated);
490
491 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
492 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
493 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, TEST_SAMPLE_RATE);
494 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, TEST_CHANNEL_COUNT);
495 ret = avmuxer_->AddTrack(audioTrackId, audioParams);
496 EXPECT_EQ(ret, AV_ERR_OK);
497 EXPECT_GE(audioTrackId, 0);
498 }
499
500 /**
501 * @tc.name: Muxer_AddTrack_005
502 * @tc.desc: Muxer AddTrack while create by unexpected outputFormat
503 * @tc.type: FUNC
504 */
505 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_005, TestSize.Level0)
506 {
507 int32_t trackId = -2;
508 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack.mp4");
509 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
510 bool isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(INVALID_FORMAT));
511 ASSERT_FALSE(isCreated);
512
513 std::shared_ptr<FormatMock> avParam = FormatMockFactory::CreateFormat();
514 avParam->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
515 avParam->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, TEST_SAMPLE_RATE);
516 avParam->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, TEST_CHANNEL_COUNT);
517 avParam->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
518 avParam->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
519
520 int32_t ret = avmuxer_->AddTrack(trackId, avParam);
521 ASSERT_NE(ret, 0);
522
523 avParam->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
524 ret = avmuxer_->AddTrack(trackId, avParam);
525 ASSERT_NE(ret, 0);
526 }
527
528 /**
529 * @tc.name: Muxer_AddTrack_006
530 * @tc.desc: Muxer AddTrack while create by unexpected value
531 * @tc.type: FUNC
532 */
533 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_006, TestSize.Level0)
534 {
535 int32_t trackId = -2;
536 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack.mp4");
537 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
538 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
539 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
540 ASSERT_TRUE(isCreated);
541
542 std::shared_ptr<FormatMock> audioParam = FormatMockFactory::CreateFormat();
543 audioParam->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
544 audioParam->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, TEST_SAMPLE_RATE);
545 audioParam->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, -1);
546 int32_t ret = avmuxer_->AddTrack(trackId, audioParam);
547 ASSERT_NE(ret, 0);
548
549 audioParam->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, -1);
550 audioParam->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, TEST_CHANNEL_COUNT);
551 ret = avmuxer_->AddTrack(trackId, audioParam);
552 ASSERT_NE(ret, 0);
553
554 // test add video track
555 std::shared_ptr<FormatMock> videoParam = FormatMockFactory::CreateFormat();
556 videoParam->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
557 videoParam->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
558 videoParam->PutIntValue(OH_MD_KEY_HEIGHT, -1);
559 ret = avmuxer_->AddTrack(trackId, videoParam);
560 ASSERT_NE(ret, 0);
561
562 videoParam->PutIntValue(OH_MD_KEY_WIDTH, -1);
563 videoParam->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
564 ret = avmuxer_->AddTrack(trackId, videoParam);
565 ASSERT_NE(ret, 0);
566
567 videoParam->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
568 videoParam->PutIntValue(OH_MD_KEY_HEIGHT, 0xFFFF + 1);
569 ret = avmuxer_->AddTrack(trackId, videoParam);
570 ASSERT_NE(ret, 0);
571
572 videoParam->PutIntValue(OH_MD_KEY_WIDTH, 0xFFFF + 1);
573 videoParam->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
574 ret = avmuxer_->AddTrack(trackId, videoParam);
575 ASSERT_NE(ret, 0);
576
577 videoParam->PutIntValue(OH_MD_KEY_WIDTH, 0xFFFF);
578 videoParam->PutIntValue(OH_MD_KEY_HEIGHT, 0xFFFF);
579 ret = avmuxer_->AddTrack(trackId, videoParam);
580 ASSERT_EQ(ret, 0);
581 }
582
583 /**
584 * @tc.name: Muxer_AddTrack_007
585 * @tc.desc: Create amr-nb Muxer AddTrack
586 * @tc.type: FUNC
587 */
588 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_007, TestSize.Level0)
589 {
590 int32_t trackId = -2; // -2: Initialize to an invalid ID
591 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack.amr");
592 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
593 bool isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(AV_OUTPUT_FORMAT_AMR));
594 ASSERT_TRUE(isCreated);
595
596 std::shared_ptr<FormatMock> avParam = FormatMockFactory::CreateFormat();
597 avParam->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AMR_NB);
598 avParam->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 8000); // 8000: 8khz sample rate
599 avParam->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 1); // 1: 1 audio channel,mono
600
601 int32_t ret = avmuxer_->AddTrack(trackId, avParam);
602 ASSERT_EQ(ret, 0);
603 }
604
605 /**
606 * @tc.name: Muxer_AddTrack_008
607 * @tc.desc: Create amr-wb Muxer AddTrack
608 * @tc.type: FUNC
609 */
610 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_008, TestSize.Level0)
611 {
612 int32_t trackId = -2; // -2: Initialize to an invalid ID
613 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack.amr");
614 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
615 bool isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(AV_OUTPUT_FORMAT_AMR));
616 ASSERT_TRUE(isCreated);
617
618 std::shared_ptr<FormatMock> avParam = FormatMockFactory::CreateFormat();
619 avParam->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AMR_WB);
620 avParam->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 16000); // 16000: 16khz sample rate
621 avParam->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 1); // 1: 1 audio channel, mono
622
623 int32_t ret = avmuxer_->AddTrack(trackId, avParam);
624 ASSERT_EQ(ret, 0);
625 }
626
627 /**
628 * @tc.name: Muxer_AddTrack_009
629 * @tc.desc: Create AAC Muxer AddTrack
630 * @tc.type: FUNC
631 */
632 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_009, TestSize.Level0)
633 {
634 int32_t trackId = -2; // -2: Initialize to an invalid ID
635 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack.aac");
636 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
637 bool isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(AV_OUTPUT_FORMAT_AAC));
638 ASSERT_TRUE(isCreated);
639
640 std::shared_ptr<FormatMock> avParam = FormatMockFactory::CreateFormat();
641 avParam->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
642 avParam->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 16000); // 16000: 16khz sample rate
643 avParam->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 1); // 1: 1 audio channel, mono
644 avParam->PutIntValue(OH_MD_KEY_PROFILE, AAC_PROFILE_LC);
645 avParam->PutIntValue(OH_MD_KEY_AAC_IS_ADTS, 0);
646
647 int32_t ret = avmuxer_->AddTrack(trackId, avParam);
648 ASSERT_EQ(ret, 0);
649 }
650
651 /**
652 * @tc.name: Muxer_Start_001
653 * @tc.desc: Muxer Start normal
654 * @tc.type: FUNC
655 */
656 HWTEST_F(AVMuxerUnitTest, Muxer_Start_001, TestSize.Level0)
657 {
658 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Start.mp4");
659 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
660 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
661 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
662 ASSERT_TRUE(isCreated);
663
664 std::shared_ptr<FormatMock> videoParams =
665 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
666 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
667
668 int32_t videoTrackId = -1;
669 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
670 ASSERT_EQ(ret, AV_ERR_OK);
671 ASSERT_GE(videoTrackId, 0);
672 EXPECT_EQ(avmuxer_->Start(), 0);
673 }
674
675 /**
676 * @tc.name: Muxer_Start_002
677 * @tc.desc: Muxer Start twice
678 * @tc.type: FUNC
679 */
680 HWTEST_F(AVMuxerUnitTest, Muxer_Start_002, TestSize.Level0)
681 {
682 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Start.mp4");
683 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
684 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
685 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
686 ASSERT_TRUE(isCreated);
687
688 std::shared_ptr<FormatMock> videoParams =
689 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
690 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
691
692 int32_t videoTrackId = -1;
693 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
694 ASSERT_EQ(ret, AV_ERR_OK);
695 ASSERT_GE(videoTrackId, 0);
696 ASSERT_EQ(avmuxer_->Start(), 0);
697 EXPECT_NE(avmuxer_->Start(), 0);
698 }
699
700 /**
701 * @tc.name: Muxer_Start_003
702 * @tc.desc: Muxer Start after Create()
703 * @tc.type: FUNC
704 */
705 HWTEST_F(AVMuxerUnitTest, Muxer_Start_003, TestSize.Level0)
706 {
707 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Start.mp4");
708 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
709 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
710 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
711 ASSERT_TRUE(isCreated);
712 EXPECT_NE(avmuxer_->Start(), 0);
713 }
714
715 /**
716 * @tc.name: Muxer_Start_004
717 * @tc.desc: Muxer Start after Stop()
718 * @tc.type: FUNC
719 */
720 HWTEST_F(AVMuxerUnitTest, Muxer_Start_004, TestSize.Level0)
721 {
722 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Start.mp4");
723 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
724 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
725 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
726 ASSERT_TRUE(isCreated);
727
728 std::shared_ptr<FormatMock> videoParams =
729 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
730 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
731
732 int32_t videoTrackId = -1;
733 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
734 ASSERT_EQ(ret, AV_ERR_OK);
735 ASSERT_GE(videoTrackId, 0);
736 ASSERT_EQ(avmuxer_->Start(), 0);
737 ASSERT_EQ(avmuxer_->Stop(), 0);
738
739 EXPECT_NE(avmuxer_->Start(), 0);
740 }
741
742 /**
743 * @tc.name: Muxer_Start_005
744 * @tc.desc: Muxer Start while create by unexpected outputFormat
745 * @tc.type: FUNC
746 */
747 HWTEST_F(AVMuxerUnitTest, Muxer_Start_005, TestSize.Level0)
748 {
749 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Start.mp4");
750 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
751 bool isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(INVALID_FORMAT));
752 ASSERT_FALSE(isCreated);
753
754 std::shared_ptr<FormatMock> videoParams =
755 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
756 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
757
758 int32_t videoTrackId = -1;
759 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
760 ASSERT_NE(ret, AV_ERR_OK);
761 ASSERT_LT(videoTrackId, 0);
762 ASSERT_NE(avmuxer_->Start(), 0);
763 }
764
765 /**
766 * @tc.name: Muxer_Stop_001
767 * @tc.desc: Muxer Stop() normal
768 * @tc.type: FUNC
769 */
770 HWTEST_F(AVMuxerUnitTest, Muxer_Stop_001, TestSize.Level0)
771 {
772 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Stop.mp4");
773 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
774 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
775 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
776 ASSERT_TRUE(isCreated);
777
778 std::shared_ptr<FormatMock> videoParams =
779 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
780 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
781 int32_t videoTrackId = -1;
782 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
783 ASSERT_EQ(ret, AV_ERR_OK);
784 ASSERT_GE(videoTrackId, 0);
785 ASSERT_EQ(avmuxer_->Start(), 0);
786 EXPECT_EQ(avmuxer_->Stop(), 0);
787 }
788
789 /**
790 * @tc.name: Muxer_Stop_002
791 * @tc.desc: Muxer Stop() after Create()
792 * @tc.type: FUNC
793 */
794 HWTEST_F(AVMuxerUnitTest, Muxer_Stop_002, TestSize.Level0)
795 {
796 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Stop.mp4");
797 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
798 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
799 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
800 ASSERT_TRUE(isCreated);
801 EXPECT_NE(avmuxer_->Stop(), 0);
802 }
803
804 /**
805 * @tc.name: Muxer_Stop_003
806 * @tc.desc: Muxer Stop() after AddTrack()
807 * @tc.type: FUNC
808 */
809 HWTEST_F(AVMuxerUnitTest, Muxer_Stop_003, TestSize.Level0)
810 {
811 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Stop.mp4");
812 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
813 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
814 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
815 ASSERT_TRUE(isCreated);
816
817 std::shared_ptr<FormatMock> videoParams =
818 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
819 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
820 int32_t videoTrackId = -1;
821 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
822 ASSERT_EQ(ret, AV_ERR_OK);
823 ASSERT_GE(videoTrackId, 0);
824 EXPECT_NE(avmuxer_->Stop(), 0);
825 }
826
827 /**
828 * @tc.name: Muxer_Stop_004
829 * @tc.desc: Muxer Stop() multiple times
830 * @tc.type: FUNC
831 */
832 HWTEST_F(AVMuxerUnitTest, Muxer_Stop_004, TestSize.Level0)
833 {
834 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Stop.mp4");
835 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
836 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
837 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
838 ASSERT_TRUE(isCreated);
839
840 std::shared_ptr<FormatMock> videoParams =
841 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
842 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
843 int32_t videoTrackId = -1;
844 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
845 ASSERT_EQ(ret, AV_ERR_OK);
846 ASSERT_GE(videoTrackId, 0);
847 ASSERT_EQ(avmuxer_->Start(), 0);
848 ASSERT_EQ(avmuxer_->Stop(), 0);
849
850 ASSERT_NE(avmuxer_->Stop(), 0);
851 ASSERT_NE(avmuxer_->Stop(), 0);
852 ASSERT_NE(avmuxer_->Stop(), 0);
853 }
854
855 /**
856 * @tc.name: Muxer_Stop_005
857 * @tc.desc: Muxer Stop() before Start
858 * @tc.type: FUNC
859 */
860 HWTEST_F(AVMuxerUnitTest, Muxer_Stop_005, TestSize.Level0)
861 {
862 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Stop.mp4");
863 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
864 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
865 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
866 ASSERT_TRUE(isCreated);
867
868 std::shared_ptr<FormatMock> videoParams =
869 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
870 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
871 int32_t videoTrackId = -1;
872 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
873 ASSERT_EQ(ret, AV_ERR_OK);
874 EXPECT_GE(videoTrackId, 0);
875 EXPECT_NE(avmuxer_->Stop(), 0);
876 EXPECT_EQ(avmuxer_->Start(), 0);
877 EXPECT_EQ(avmuxer_->Stop(), 0);
878 }
879
880 /**
881 * @tc.name: Muxer_Stop_006
882 * @tc.desc: Muxer Stop() while create by unexpected outputFormat
883 * @tc.type: FUNC
884 */
885 HWTEST_F(AVMuxerUnitTest, Muxer_Stop_006, TestSize.Level0)
886 {
887 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Stop.mp4");
888 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
889 bool isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(INVALID_FORMAT));
890 ASSERT_FALSE(isCreated);
891
892 std::shared_ptr<FormatMock> videoParams =
893 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
894 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
895 int32_t videoTrackId = -1;
896 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
897 ASSERT_NE(ret, AV_ERR_OK);
898 ASSERT_LT(videoTrackId, 0);
899 ASSERT_NE(avmuxer_->Start(), 0);
900 ASSERT_NE(avmuxer_->Stop(), 0);
901 }
902
903 /**
904 * @tc.name: Muxer_writeSample_001
905 * @tc.desc: Muxer Write Sample normal
906 * @tc.type: FUNC
907 */
908 HWTEST_F(AVMuxerUnitTest, Muxer_writeSample_001, TestSize.Level0)
909 {
910 int32_t trackId = -1;
911 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_WriteSample.mp4");
912 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
913
914 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
915 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
916 ASSERT_TRUE(isCreated);
917
918 std::shared_ptr<FormatMock> vParam =
919 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
920
921 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
922 ASSERT_EQ(ret, 0);
923 ASSERT_GE(trackId, 0);
924 ASSERT_EQ(avmuxer_->Start(), 0);
925
926 OH_AVCodecBufferAttr info;
927 info.pts = 0;
928 info.offset = 0;
929 info.size = sizeof(buffer_);
930 ret = avmuxer_->WriteSample(trackId, buffer_, info);
931 ASSERT_EQ(ret, 0);
932 }
933
934 /**
935 * @tc.name: Muxer_writeSample_002
936 * @tc.desc: Muxer Write Sample while create by unexpected outputFormat
937 * @tc.type: FUNC
938 */
939 HWTEST_F(AVMuxerUnitTest, Muxer_writeSample_002, TestSize.Level0)
940 {
941 int32_t trackId = -1;
942 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_writeSample.mp4");
943 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
944 bool isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(INVALID_FORMAT));
945 ASSERT_FALSE(isCreated);
946
947 std::shared_ptr<FormatMock> vParam =
948 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
949
950 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
951 ASSERT_NE(ret, 0);
952 ASSERT_LT(trackId, 0);
953 ASSERT_NE(avmuxer_->Start(), 0);
954
955 OH_AVCodecBufferAttr info;
956 info.pts = 0;
957 info.size = sizeof(buffer_);
958 ret = avmuxer_->WriteSample(trackId, buffer_, info);
959 ASSERT_NE(ret, 0);
960 }
961
962 /**
963 * @tc.name: Muxer_writeSample_003
964 * @tc.desc: Muxer Write Sample without Start()
965 * @tc.type: FUNC
966 */
967 HWTEST_F(AVMuxerUnitTest, Muxer_writeSample_003, TestSize.Level0)
968 {
969 int32_t trackId = -1;
970 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_WriteSample.mp4");
971 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
972
973 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
974 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
975 ASSERT_TRUE(isCreated);
976
977 std::shared_ptr<FormatMock> vParam =
978 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
979
980 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
981 ASSERT_EQ(ret, 0);
982 ASSERT_GE(trackId, 0);
983
984 OH_AVCodecBufferAttr info;
985 info.pts = 0;
986 info.size = sizeof(buffer_);
987 ret = avmuxer_->WriteSample(trackId, buffer_, info);
988 ASSERT_NE(ret, 0);
989 }
990
991 /**
992 * @tc.name: Muxer_writeSample_004
993 * @tc.desc: Muxer Write Sample unexisting track
994 * @tc.type: FUNC
995 */
996 HWTEST_F(AVMuxerUnitTest, Muxer_writeSample_004, TestSize.Level0)
997 {
998 constexpr int32_t invalidTrackId = 99999;
999 int32_t trackId = -1;
1000 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_WriteSample.mp4");
1001 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1002
1003 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1004 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1005 ASSERT_TRUE(isCreated);
1006
1007 std::shared_ptr<FormatMock> vParam =
1008 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1009
1010 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
1011 ASSERT_EQ(ret, 0);
1012 ASSERT_GE(trackId, 0);
1013 ASSERT_EQ(avmuxer_->Start(), 0);
1014
1015 OH_AVCodecBufferAttr info;
1016 info.pts = 0;
1017 info.size = sizeof(buffer_);
1018 ret = avmuxer_->WriteSample(trackId + 1, buffer_, info);
1019 ASSERT_NE(ret, 0);
1020
1021 ret = avmuxer_->WriteSample(-1, buffer_, info);
1022 ASSERT_NE(ret, 0);
1023
1024 ret = avmuxer_->WriteSample(invalidTrackId, buffer_, info);
1025 ASSERT_NE(ret, 0);
1026 }
1027
1028 /**
1029 * @tc.name: Muxer_writeSample_005
1030 * @tc.desc: Muxer Write Sample after Stop()
1031 * @tc.type: FUNC
1032 */
1033 HWTEST_F(AVMuxerUnitTest, Muxer_writeSample_005, TestSize.Level0)
1034 {
1035 int32_t trackId = -1;
1036 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_WriteSample.mp4");
1037 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1038
1039 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1040 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1041 ASSERT_TRUE(isCreated);
1042
1043 std::shared_ptr<FormatMock> vParam =
1044 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1045
1046 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
1047 ASSERT_EQ(ret, 0);
1048 ASSERT_GE(trackId, 0);
1049 ASSERT_EQ(avmuxer_->Start(), 0);
1050 ASSERT_EQ(avmuxer_->Stop(), 0);
1051
1052 OH_AVCodecBufferAttr info;
1053 info.pts = 0;
1054 info.size = sizeof(buffer_);
1055 ret = avmuxer_->WriteSample(trackId, buffer_, info);
1056 ASSERT_NE(ret, 0);
1057 }
1058
1059 /**
1060 * @tc.name: Muxer_SetRotation_001
1061 * @tc.desc: Muxer SetRotation after Create
1062 * @tc.type: FUNC
1063 */
1064 HWTEST_F(AVMuxerUnitTest, Muxer_SetRotation_001, TestSize.Level0)
1065 {
1066 int32_t trackId = -1;
1067 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetRotation.mp4");
1068 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1069
1070 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1071 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1072 ASSERT_TRUE(isCreated);
1073
1074 std::shared_ptr<FormatMock> vParam =
1075 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1076 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
1077 ASSERT_EQ(ret, 0);
1078 ASSERT_GE(trackId, 0);
1079
1080 ret = avmuxer_->SetRotation(TEST_ROTATION);
1081 EXPECT_EQ(ret, 0);
1082 EXPECT_EQ(avmuxer_->Start(), 0);
1083 EXPECT_EQ(avmuxer_->Stop(), 0);
1084 }
1085
1086
1087 /**
1088 * @tc.name: Muxer_SetRotation_002
1089 * @tc.desc: Muxer SetRotation after Create
1090 * @tc.type: FUNC
1091 */
1092 HWTEST_F(AVMuxerUnitTest, Muxer_SetRotation_002, TestSize.Level0)
1093 {
1094 int32_t trackId = -1;
1095 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetRotation.mp4");
1096 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1097
1098 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1099 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1100 ASSERT_TRUE(isCreated);
1101
1102 int32_t ret = avmuxer_->SetRotation(180); // 180 rotation
1103 EXPECT_EQ(ret, 0);
1104
1105 std::shared_ptr<FormatMock> vParam =
1106 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1107 ret = avmuxer_->AddTrack(trackId, vParam);
1108 ASSERT_EQ(ret, 0);
1109 ASSERT_GE(trackId, 0);
1110
1111 EXPECT_EQ(avmuxer_->Start(), 0);
1112 EXPECT_EQ(avmuxer_->Stop(), 0);
1113 }
1114
1115 /**
1116 * @tc.name: Muxer_SetRotation_003
1117 * @tc.desc: Muxer SetRotation after Start
1118 * @tc.type: FUNC
1119 */
1120 HWTEST_F(AVMuxerUnitTest, Muxer_SetRotation_003, TestSize.Level0)
1121 {
1122 int32_t trackId = -1;
1123 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetRotation.mp4");
1124 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1125
1126 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1127 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1128 ASSERT_TRUE(isCreated);
1129
1130 std::shared_ptr<FormatMock> vParam =
1131 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1132 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
1133 ASSERT_EQ(ret, 0);
1134 ASSERT_GE(trackId, 0);
1135 ASSERT_EQ(avmuxer_->Start(), 0);
1136
1137 ret = avmuxer_->SetRotation(TEST_ROTATION);
1138 EXPECT_NE(ret, 0);
1139 }
1140
1141 /**
1142 * @tc.name: Muxer_SetRotation_004
1143 * @tc.desc: Muxer SetRotation after Stop
1144 * @tc.type: FUNC
1145 */
1146 HWTEST_F(AVMuxerUnitTest, Muxer_SetRotation_004, TestSize.Level0)
1147 {
1148 int32_t trackId = -1;
1149 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetRotation.mp4");
1150 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1151
1152 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1153 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1154 ASSERT_TRUE(isCreated);
1155
1156 std::shared_ptr<FormatMock> vParam =
1157 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1158 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
1159 ASSERT_EQ(ret, 0);
1160 ASSERT_GE(trackId, 0);
1161 ASSERT_EQ(avmuxer_->Start(), 0);
1162 ASSERT_EQ(avmuxer_->Stop(), 0);
1163
1164 ret = avmuxer_->SetRotation(TEST_ROTATION);
1165 EXPECT_NE(ret, 0);
1166 }
1167
1168 /**
1169 * @tc.name: Muxer_SetRotation_005
1170 * @tc.desc: Muxer SetRotation after WriteSample
1171 * @tc.type: FUNC
1172 */
1173 HWTEST_F(AVMuxerUnitTest, Muxer_SetRotation_005, TestSize.Level0)
1174 {
1175 int32_t trackId = -1;
1176 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetRotation.mp4");
1177 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1178
1179 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1180 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1181 ASSERT_TRUE(isCreated);
1182
1183 std::shared_ptr<FormatMock> vParam =
1184 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1185
1186 int32_t ret = avmuxer_->AddTrack(trackId, vParam);
1187 ASSERT_EQ(ret, 0);
1188 ASSERT_GE(trackId, 0);
1189 ASSERT_EQ(avmuxer_->Start(), 0);
1190
1191 OH_AVCodecBufferAttr info;
1192 info.pts = 0;
1193 info.size = sizeof(buffer_);
1194 ret = avmuxer_->WriteSample(trackId, buffer_, info);
1195 EXPECT_EQ(ret, 0);
1196
1197 ret = avmuxer_->SetRotation(TEST_ROTATION);
1198 EXPECT_NE(ret, 0);
1199 }
1200
1201 /**
1202 * @tc.name: Muxer_SetRotation_006
1203 * @tc.desc: Muxer SetRotation while Create failed!
1204 * @tc.type: FUNC
1205 */
1206 HWTEST_F(AVMuxerUnitTest, Muxer_SetRotation_006, TestSize.Level0)
1207 {
1208 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetRotation.mp4");
1209 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1210 bool isCreated = avmuxer_->CreateMuxer(fd_, static_cast<OH_AVOutputFormat>(INVALID_FORMAT));
1211 ASSERT_FALSE(isCreated);
1212
1213 int32_t ret = avmuxer_->SetRotation(TEST_ROTATION);
1214 ASSERT_NE(ret, 0);
1215 }
1216
1217 /**
1218 * @tc.name: Muxer_SetRotation_007
1219 * @tc.desc: Muxer SetRotation expected value
1220 * @tc.type: FUNC
1221 */
1222 HWTEST_F(AVMuxerUnitTest, Muxer_SetRotation_007, TestSize.Level0)
1223 {
1224 int32_t trackId = -1;
1225 constexpr int32_t testRotation180 = 180;
1226 constexpr int32_t testRotation270 = 270;
1227 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetRotation.mp4");
1228 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1229
1230 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1231 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1232 ASSERT_TRUE(isCreated);
1233
1234 int32_t ret = avmuxer_->SetRotation(0);
1235 EXPECT_EQ(ret, 0);
1236
1237 ret = avmuxer_->SetRotation(TEST_ROTATION);
1238 EXPECT_EQ(ret, 0);
1239
1240 ret = avmuxer_->SetRotation(testRotation180);
1241 EXPECT_EQ(ret, 0);
1242
1243 ret = avmuxer_->SetRotation(testRotation270);
1244 EXPECT_EQ(ret, 0);
1245
1246 std::shared_ptr<FormatMock> vParam =
1247 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1248 ret = avmuxer_->AddTrack(trackId, vParam);
1249 ASSERT_EQ(ret, 0);
1250 ASSERT_GE(trackId, 0);
1251
1252 EXPECT_EQ(avmuxer_->Start(), 0);
1253 EXPECT_EQ(avmuxer_->Stop(), 0);
1254 }
1255
1256 /**
1257 * @tc.name: Muxer_SetRotation_008
1258 * @tc.desc: Muxer SetRotation unexpected value
1259 * @tc.type: FUNC
1260 */
1261 HWTEST_F(AVMuxerUnitTest, Muxer_SetRotation_008, TestSize.Level0)
1262 {
1263 constexpr int32_t testRotation360 = 360;
1264 constexpr int32_t testRotationNeg90 = -90;
1265 constexpr int32_t testRotationNeg180 = -180;
1266 constexpr int32_t testRotationNeg270 = -270;
1267 constexpr int32_t testRotationNeg360 = -360;
1268 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetRotation.mp4");
1269 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1270
1271 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1272 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1273 ASSERT_TRUE(isCreated);
1274
1275 int32_t ret = avmuxer_->SetRotation(1);
1276 EXPECT_NE(ret, 0);
1277
1278 ret = avmuxer_->SetRotation(TEST_ROTATION + 1);
1279 EXPECT_NE(ret, 0);
1280
1281 ret = avmuxer_->SetRotation(testRotation360);
1282 EXPECT_NE(ret, 0);
1283
1284 ret = avmuxer_->SetRotation(-1);
1285 EXPECT_NE(ret, 0);
1286
1287 ret = avmuxer_->SetRotation(testRotationNeg90);
1288 EXPECT_NE(ret, 0);
1289
1290 ret = avmuxer_->SetRotation(testRotationNeg180);
1291 EXPECT_NE(ret, 0);
1292
1293 ret = avmuxer_->SetRotation(testRotationNeg270);
1294 EXPECT_NE(ret, 0);
1295
1296 ret = avmuxer_->SetRotation(testRotationNeg360);
1297 EXPECT_NE(ret, 0);
1298 }
1299
1300 /**
1301 * @tc.name: Muxer_Hevc_AddTrack_001
1302 * @tc.desc: Muxer Hevc AddTrack while create by unexpected value
1303 * @tc.type: FUNC
1304 */
1305 HWTEST_F(AVMuxerUnitTest, Muxer_Hevc_AddTrack_001, TestSize.Level0)
1306 {
1307 if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
1308 std::cout << "the hevc of mimetype is not supported" << std::endl;
1309 return;
1310 }
1311
1312 constexpr int32_t validVideoDelay = 1;
1313 constexpr int32_t invalidVideoDelay = -1;
1314 constexpr int32_t validFrameRate = 30;
1315 constexpr int32_t invalidFrameRate = -1;
1316
1317 int32_t trackId = -1;
1318 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_H265.mp4");
1319 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1320 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1321 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1322 ASSERT_TRUE(isCreated);
1323
1324 std::shared_ptr<FormatMock> videoParams =
1325 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, TEST_WIDTH, TEST_HEIGHT);
1326
1327 videoParams->PutIntValue("video_delay", validVideoDelay);
1328 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1329 ASSERT_NE(ret, 0);
1330
1331 videoParams->PutIntValue("video_delay", invalidVideoDelay);
1332 videoParams->PutDoubleValue(OH_MD_KEY_FRAME_RATE, validFrameRate);
1333 ret = avmuxer_->AddTrack(trackId, videoParams);
1334 ASSERT_NE(ret, 0);
1335
1336 videoParams->PutIntValue("video_delay", validVideoDelay);
1337 videoParams->PutDoubleValue(OH_MD_KEY_FRAME_RATE, invalidFrameRate);
1338 ret = avmuxer_->AddTrack(trackId, videoParams);
1339 ASSERT_NE(ret, 0);
1340
1341 videoParams->PutIntValue("video_delay", 0xFF);
1342 videoParams->PutDoubleValue(OH_MD_KEY_FRAME_RATE, validFrameRate);
1343 ret = avmuxer_->AddTrack(trackId, videoParams);
1344 ASSERT_NE(ret, 0);
1345
1346 videoParams->PutIntValue("video_delay", validVideoDelay);
1347 videoParams->PutDoubleValue(OH_MD_KEY_FRAME_RATE, validFrameRate);
1348 ret = avmuxer_->AddTrack(trackId, videoParams);
1349 ASSERT_EQ(ret, 0);
1350 ASSERT_GE(trackId, 0);
1351 }
1352
1353 /**
1354 * @tc.name: Muxer_Hevc_WriteSample_001
1355 * @tc.desc: Muxer Hevc Write Sample normal
1356 * @tc.type: FUNC
1357 */
1358 HWTEST_F(AVMuxerUnitTest, Muxer_Hevc_WriteSample_001, TestSize.Level0)
1359 {
1360 if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
1361 return;
1362 }
1363
1364 constexpr int32_t invalidTrackId = 99999;
1365 int32_t trackId = -1;
1366 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_H265.mp4");
1367 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1368
1369 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1370 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1371 ASSERT_TRUE(isCreated);
1372
1373 std::shared_ptr<FormatMock> videoParams =
1374 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, TEST_WIDTH, TEST_HEIGHT);
1375
1376 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1377 ASSERT_EQ(ret, 0);
1378 ASSERT_GE(trackId, 0);
1379 ASSERT_EQ(avmuxer_->Start(), 0);
1380
1381 OH_AVCodecBufferAttr info;
1382 info.pts = 0;
1383 info.size = sizeof(buffer_);
1384 OH_AVBuffer *buffer = OH_AVBuffer_Create(info.size);
1385 ASSERT_EQ(memcpy_s(OH_AVBuffer_GetAddr(buffer), info.size, buffer_, sizeof(buffer_)), 0);
1386 OH_AVBuffer_SetBufferAttr(buffer, &info);
1387
1388 ret = avmuxer_->WriteSampleBuffer(trackId + 1, buffer);
1389 ASSERT_NE(ret, 0);
1390
1391 ret = avmuxer_->WriteSampleBuffer(-1, buffer);
1392 ASSERT_NE(ret, 0);
1393
1394 ret = avmuxer_->WriteSampleBuffer(invalidTrackId, buffer);
1395 ASSERT_NE(ret, 0);
1396
1397 ret = avmuxer_->WriteSampleBuffer(trackId, buffer);
1398 ASSERT_EQ(ret, 0);
1399
1400 OH_AVBuffer_Destroy(buffer);
1401 }
1402
1403 /**
1404 * @tc.name: Muxer_Hevc_WriteSample_002
1405 * @tc.desc: Muxer Hevc Write Sample flags AVCODEC_BUFFER_FLAGS_CODEC_DATA
1406 * @tc.type: FUNC
1407 */
1408 HWTEST_F(AVMuxerUnitTest, Muxer_Hevc_WriteSample_002, TestSize.Level0)
1409 {
1410 if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
1411 return;
1412 }
1413
1414 int32_t trackId = -1;
1415 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_H265.mp4");
1416 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1417
1418 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1419 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1420 ASSERT_TRUE(isCreated);
1421
1422 std::shared_ptr<FormatMock> videoParams =
1423 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, TEST_WIDTH, TEST_HEIGHT);
1424
1425 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1426 ASSERT_EQ(ret, 0);
1427 ASSERT_GE(trackId, 0);
1428 ASSERT_EQ(avmuxer_->Start(), 0);
1429
1430 OH_AVCodecBufferAttr info;
1431 info.pts = 0;
1432 info.size = sizeof(annexBuffer_);
1433 OH_AVBuffer *buffer = OH_AVBuffer_Create(info.size);
1434 ASSERT_EQ(memcpy_s(OH_AVBuffer_GetAddr(buffer), info.size, annexBuffer_, sizeof(annexBuffer_)), 0);
1435
1436 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
1437 OH_AVBuffer_SetBufferAttr(buffer, &info);
1438 ret = avmuxer_->WriteSampleBuffer(trackId, buffer);
1439 ASSERT_EQ(ret, 0);
1440
1441 info.flags = AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
1442 OH_AVBuffer_SetBufferAttr(buffer, &info);
1443 ret = avmuxer_->WriteSampleBuffer(trackId, buffer);
1444 ASSERT_EQ(ret, 0);
1445
1446 OH_AVBuffer_Destroy(buffer);
1447 }
1448
1449 /**
1450 * @tc.name: Muxer_Hevc_WriteSample_003
1451 * @tc.desc: Muxer Hevc Write Sample flags AVCODEC_BUFFER_FLAGS_CODEC_DATA | AVCODEC_BUFFER_FLAGS_SYNC_FRAME
1452 * @tc.type: FUNC
1453 */
1454 HWTEST_F(AVMuxerUnitTest, Muxer_Hevc_WriteSample_003, TestSize.Level0)
1455 {
1456 if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
1457 return;
1458 }
1459
1460 int32_t trackId = -1;
1461 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_H265.mp4");
1462 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1463
1464 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1465 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1466 ASSERT_TRUE(isCreated);
1467
1468 std::shared_ptr<FormatMock> videoParams =
1469 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, TEST_WIDTH, TEST_HEIGHT);
1470
1471 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1472 ASSERT_EQ(ret, 0);
1473 ASSERT_GE(trackId, 0);
1474 ASSERT_EQ(avmuxer_->Start(), 0);
1475
1476 OH_AVCodecBufferAttr info;
1477 info.pts = 0;
1478 info.size = sizeof(annexBuffer_);
1479 OH_AVBuffer *buffer = OH_AVBuffer_Create(info.size);
1480 ASSERT_EQ(memcpy_s(OH_AVBuffer_GetAddr(buffer), info.size, annexBuffer_, sizeof(annexBuffer_)), 0);
1481
1482 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA | AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
1483 OH_AVBuffer_SetBufferAttr(buffer, &info);
1484 ret = avmuxer_->WriteSampleBuffer(trackId, buffer);
1485 ASSERT_EQ(ret, 0);
1486
1487 info.flags = AVCODEC_BUFFER_FLAGS_NONE;
1488 OH_AVBuffer_SetBufferAttr(buffer, &info);
1489 ret = avmuxer_->WriteSampleBuffer(trackId, buffer);
1490 ASSERT_EQ(ret, 0);
1491
1492 OH_AVBuffer_Destroy(buffer);
1493 }
1494
1495 /**
1496 * @tc.name: Muxer_Hevc_WriteSample_004
1497 * @tc.desc: Muxer Hevc Write Sample flags AVCODEC_BUFFER_FLAGS_SYNC_FRAME
1498 * @tc.type: FUNC
1499 */
1500 HWTEST_F(AVMuxerUnitTest, Muxer_Hevc_WriteSample_004, TestSize.Level0)
1501 {
1502 if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
1503 return;
1504 }
1505
1506 int32_t trackId = -1;
1507 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_H265.mp4");
1508 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1509
1510 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1511 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1512 ASSERT_TRUE(isCreated);
1513
1514 std::shared_ptr<FormatMock> videoParams =
1515 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, TEST_WIDTH, TEST_HEIGHT);
1516
1517 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1518 ASSERT_EQ(ret, 0);
1519 ASSERT_GE(trackId, 0);
1520 ASSERT_EQ(avmuxer_->Start(), 0);
1521
1522 OH_AVCodecBufferAttr info;
1523 info.pts = 0;
1524 info.size = sizeof(annexBuffer_);
1525 OH_AVBuffer *buffer = OH_AVBuffer_Create(info.size);
1526 ASSERT_EQ(memcpy_s(OH_AVBuffer_GetAddr(buffer), info.size, annexBuffer_, sizeof(annexBuffer_)), 0);
1527
1528 info.flags = AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
1529 OH_AVBuffer_SetBufferAttr(buffer, &info);
1530 ret = avmuxer_->WriteSampleBuffer(trackId, buffer);
1531 ASSERT_EQ(ret, 0);
1532
1533 info.flags = AVCODEC_BUFFER_FLAGS_NONE;
1534 OH_AVBuffer_SetBufferAttr(buffer, &info);
1535 ret = avmuxer_->WriteSampleBuffer(trackId, buffer);
1536 ASSERT_EQ(ret, 0);
1537
1538 OH_AVBuffer_Destroy(buffer);
1539 }
1540
1541 /**
1542 * @tc.name: Muxer_Hevc_WriteSample_005
1543 * @tc.desc: Muxer Hevc Write Sample include sei logInfo
1544 * @tc.type: FUNC
1545 */
1546 HWTEST_F(AVMuxerUnitTest, Muxer_Hevc_WriteSample_005, TestSize.Level0)
1547 {
1548 if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
1549 return;
1550 }
1551
1552 int32_t trackId = -1;
1553 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_H265_logInfo.mp4");
1554 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1555
1556 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1557 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1558 ASSERT_TRUE(isCreated);
1559
1560 std::shared_ptr<FormatMock> videoParams =
1561 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, TEST_WIDTH, TEST_HEIGHT);
1562
1563 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1564 ASSERT_EQ(ret, 0);
1565 ASSERT_GE(trackId, 0);
1566 ASSERT_EQ(avmuxer_->Start(), 0);
1567
1568 inputFile_ = std::make_shared<std::ifstream>(LOGINFO_INPUT_FILE_PATH, std::ios::binary);
1569
1570 int32_t extSize = 0;
1571 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
1572 if (extSize > 0) {
1573 std::vector<uint8_t> buffer(extSize);
1574 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
1575 }
1576
1577 bool eosFlag = false;
1578 uint32_t flag = AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
1579 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1580 while (!eosFlag && (ret == 0)) {
1581 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1582 }
1583 ASSERT_EQ(ret, 0);
1584 }
1585
1586 /**
1587 * @tc.name: Muxer_SetFlag_001
1588 * @tc.desc: Muxer Write Sample flags AVCODEC_BUFFER_FLAGS_DISPOSABLE
1589 * @tc.type: FUNC
1590 */
1591 HWTEST_F(AVMuxerUnitTest, Muxer_SetFlag_001, TestSize.Level0)
1592 {
1593 int32_t trackId = -1;
1594 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Disposable_flag.mp4");
1595 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1596
1597 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1598 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1599 ASSERT_TRUE(isCreated);
1600
1601 std::shared_ptr<FormatMock> videoParams =
1602 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1603
1604 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1605 ASSERT_EQ(ret, 0);
1606 ASSERT_GE(trackId, 0);
1607 ASSERT_EQ(avmuxer_->Start(), 0);
1608
1609 inputFile_ = std::make_shared<std::ifstream>(INPUT_FILE_PATH, std::ios::binary);
1610
1611 int32_t extSize = 0;
1612 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
1613 if (extSize > 0) {
1614 std::vector<uint8_t> buffer(extSize);
1615 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
1616 }
1617
1618 bool eosFlag = false;
1619 uint32_t flag = AVCODEC_BUFFER_FLAGS_DISPOSABLE;
1620 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1621 while (!eosFlag && (ret == 0)) {
1622 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1623 }
1624 ASSERT_EQ(ret, 0);
1625 }
1626
1627 /**
1628 * @tc.name: Muxer_SetFlag_001
1629 * @tc.desc: Muxer Write Sample flags AVCODEC_BUFFER_FLAGS_DISPOSABLE_EXT
1630 * @tc.type: FUNC
1631 */
1632 HWTEST_F(AVMuxerUnitTest, Muxer_SetFlag_002, TestSize.Level0)
1633 {
1634 int32_t trackId = -1;
1635 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_DisposableExt_flag.mp4");
1636 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1637
1638 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1639 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1640 ASSERT_TRUE(isCreated);
1641
1642 std::shared_ptr<FormatMock> videoParams =
1643 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1644
1645 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1646 ASSERT_EQ(ret, 0);
1647 ASSERT_GE(trackId, 0);
1648 ASSERT_EQ(avmuxer_->Start(), 0);
1649
1650 inputFile_ = std::make_shared<std::ifstream>(INPUT_FILE_PATH, std::ios::binary);
1651
1652 int32_t extSize = 0;
1653 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
1654 if (extSize > 0) {
1655 std::vector<uint8_t> buffer(extSize);
1656 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
1657 }
1658
1659 bool eosFlag = false;
1660 uint32_t flag = AVCODEC_BUFFER_FLAGS_DISPOSABLE_EXT_TEST;
1661 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1662 while (!eosFlag && (ret == 0)) {
1663 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1664 }
1665 ASSERT_EQ(ret, 0);
1666 }
1667
1668 /**
1669 * @tc.name: Muxer_SetFlag_001
1670 * @tc.desc: Muxer Write Sample flags AVCODEC_BUFFER_FLAGS_DISPOSABLE & AVCODEC_BUFFER_FLAGS_DISPOSABLE_EXT
1671 * @tc.type: FUNC
1672 */
1673 HWTEST_F(AVMuxerUnitTest, Muxer_SetFlag_003, TestSize.Level0)
1674 {
1675 int32_t trackId = -1;
1676 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Disposable_DisposableExt_flag.mp4");
1677 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
1678
1679 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1680 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1681 ASSERT_TRUE(isCreated);
1682
1683 ASSERT_EQ(avmuxer_->SetRotation(180), 0); // 180 rotation
1684
1685 std::shared_ptr<FormatMock> videoParams =
1686 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
1687
1688 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
1689 ASSERT_EQ(ret, 0);
1690 ASSERT_GE(trackId, 0);
1691 ASSERT_EQ(avmuxer_->Start(), 0);
1692
1693 inputFile_ = std::make_shared<std::ifstream>(INPUT_FILE_PATH, std::ios::binary);
1694
1695 int32_t extSize = 0;
1696 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
1697 if (extSize > 0) {
1698 std::vector<uint8_t> buffer(extSize);
1699 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
1700 }
1701
1702 bool eosFlag = false;
1703 uint32_t flag = AVCODEC_BUFFER_FLAGS_DISPOSABLE;
1704 flag |= AVCODEC_BUFFER_FLAGS_DISPOSABLE_EXT_TEST;
1705 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1706 while (!eosFlag && (ret == 0)) {
1707 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1708 }
1709 ASSERT_EQ(ret, 0);
1710 ASSERT_EQ(avmuxer_->Stop(), 0);
1711 }
1712
1713 /**
1714 * @tc.name: Muxer_WAV_001
1715 * @tc.desc: Muxer mux the wav by g711mu
1716 * @tc.type: FUNC
1717 */
1718 HWTEST_F(AVMuxerUnitTest, Muxer_WAV_001, TestSize.Level0)
1719 {
1720 int32_t trackId = -1;
1721 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_G711MU_44100_2.wav");
1722 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_WAV;
1723
1724 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1725 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1726 ASSERT_TRUE(isCreated);
1727
1728 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
1729 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
1730 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
1731 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
1732 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_U8);
1733 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 705600); // 705600 bit rate
1734 audioParams->PutIntValue("audio_samples_per_frame", 2048); // 2048 frame size
1735 int32_t ret = avmuxer_->AddTrack(trackId, audioParams);
1736 ASSERT_EQ(ret, 0);
1737 ASSERT_GE(trackId, 0);
1738 ASSERT_EQ(avmuxer_->Start(), 0);
1739
1740 inputFile_ = std::make_shared<std::ifstream>("/data/test/media/g711mu_44100_2.dat", std::ios::binary);
1741
1742 int32_t extSize = 0;
1743 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
1744 if (extSize > 0) {
1745 std::vector<uint8_t> buffer(extSize);
1746 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
1747 }
1748
1749 bool eosFlag = false;
1750 uint32_t flag = AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
1751 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1752 while (!eosFlag && (ret == 0)) {
1753 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1754 }
1755 ASSERT_EQ(ret, 0);
1756 ASSERT_EQ(avmuxer_->Stop(), 0);
1757 }
1758
1759 /**
1760 * @tc.name: Muxer_WAV_002
1761 * @tc.desc: Muxer mux the wav by pcm
1762 * @tc.type: FUNC
1763 */
1764 HWTEST_F(AVMuxerUnitTest, Muxer_WAV_002, TestSize.Level0)
1765 {
1766 int32_t trackId = -1;
1767 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_PCM_44100_2_S16LE.wav");
1768 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_WAV;
1769
1770 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1771 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1772 ASSERT_TRUE(isCreated);
1773
1774 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
1775 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, "audio/raw");
1776 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
1777 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
1778 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
1779 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 1411200); // 1411200 bit rate
1780 audioParams->PutIntValue("audio_samples_per_frame", 1024); // 1024 frame size
1781 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_STEREO);
1782 int32_t ret = avmuxer_->AddTrack(trackId, audioParams);
1783 ASSERT_EQ(ret, 0);
1784 ASSERT_GE(trackId, 0);
1785 ASSERT_EQ(avmuxer_->Start(), 0);
1786
1787 inputFile_ = std::make_shared<std::ifstream>("/data/test/media/pcm_44100_2_s16le.dat", std::ios::binary);
1788
1789 int32_t extSize = 0;
1790 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
1791 if (extSize > 0) {
1792 std::vector<uint8_t> buffer(extSize);
1793 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
1794 }
1795
1796 bool eosFlag = false;
1797 uint32_t flag = AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
1798 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1799 while (!eosFlag && (ret == 0)) {
1800 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1801 }
1802 ASSERT_EQ(ret, 0);
1803 ASSERT_EQ(avmuxer_->Stop(), 0);
1804 }
1805
1806 /**
1807 * @tc.name: Muxer_WAV_003
1808 * @tc.desc: Muxer mux the wav by pcm, test OH_MD_KEY_AUDIO_SAMPLE_FORMAT
1809 * @tc.type: FUNC
1810 */
1811 HWTEST_F(AVMuxerUnitTest, Muxer_WAV_003, TestSize.Level0)
1812 {
1813 int32_t trackId = -1;
1814 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_PCM_44100_2_XXX.wav");
1815 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_WAV;
1816
1817 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1818 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1819 ASSERT_TRUE(isCreated);
1820
1821 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
1822 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, "audio/raw");
1823 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
1824 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
1825 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_U8P);
1826 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 705600); // 705600 bit rate
1827 audioParams->PutIntValue("audio_samples_per_frame", 1024); // 1024 frame size
1828 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_STEREO);
1829 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1830 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16P);
1831 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1832 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S24P);
1833 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1834 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S32P);
1835 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1836 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_F32P);
1837 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1838 }
1839
1840 /**
1841 * @tc.name: Muxer_WAV_004
1842 * @tc.desc: Muxer mux the wav by pcm, test OH_MD_KEY_CHANNEL_LAYOUT
1843 * @tc.type: FUNC
1844 */
1845 HWTEST_F(AVMuxerUnitTest, Muxer_WAV_004, TestSize.Level0)
1846 {
1847 int32_t trackId = -1;
1848 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_PCM_44100_2_XXX.wav");
1849 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_WAV;
1850
1851 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1852 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1853 ASSERT_TRUE(isCreated);
1854
1855 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
1856 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, "audio/raw");
1857 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
1858 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
1859 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
1860 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 1411200); // 1411200 bit rate
1861 audioParams->PutIntValue("audio_samples_per_frame", 1024); // 1024 frame size
1862 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_2POINT0POINT2);
1863 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1864 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_AMB_ORDER1_ACN_N3D);
1865 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1866 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_AMB_ORDER1_FUMA);
1867 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1868 }
1869
1870 /**
1871 * @tc.name: Muxer_AAC_001
1872 * @tc.desc: Muxer mux the aac with adts header
1873 * @tc.type: FUNC
1874 */
1875 HWTEST_F(AVMuxerUnitTest, Muxer_AAC_001, TestSize.Level0)
1876 {
1877 int32_t trackId = -1;
1878 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AAC_44100_2.aac");
1879 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_AAC;
1880
1881 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1882 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1883 ASSERT_TRUE(isCreated);
1884
1885 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
1886 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
1887 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
1888 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
1889 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_U8);
1890 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 705600); // 705600 bit rate
1891 audioParams->PutIntValue("audio_samples_per_frame", 2048); // 2048 frame size
1892 audioParams->PutIntValue(OH_MD_KEY_PROFILE, AAC_PROFILE_LC);
1893 audioParams->PutIntValue(OH_MD_KEY_AAC_IS_ADTS, 0);
1894 int32_t ret = avmuxer_->AddTrack(trackId, audioParams);
1895 ASSERT_EQ(ret, 0);
1896 ASSERT_GE(trackId, 0);
1897 ASSERT_EQ(avmuxer_->Start(), 0);
1898
1899 inputFile_ = std::make_shared<std::ifstream>("/data/test/media/aac_2c_44100hz_199k.dat", std::ios::binary);
1900
1901 int32_t extSize = 0;
1902 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
1903 if (extSize > 0) {
1904 std::vector<uint8_t> buffer(extSize);
1905 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
1906 }
1907
1908 bool eosFlag = false;
1909 uint32_t flag = AVCODEC_BUFFER_FLAGS_SYNC_FRAME;
1910 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1911 while (!eosFlag && (ret == 0)) {
1912 ret = WriteSample(trackId, inputFile_, eosFlag, flag);
1913 }
1914 ASSERT_EQ(ret, 0);
1915 ASSERT_EQ(avmuxer_->Stop(), 0);
1916 }
1917
1918 /**
1919 * @tc.name: Muxer_AAC_002
1920 * @tc.desc: Muxer mux aac, test OH_MD_KEY_AUDIO_SAMPLE_FORMAT
1921 * @tc.type: FUNC
1922 */
1923 HWTEST_F(AVMuxerUnitTest, Muxer_AAC_002, TestSize.Level0)
1924 {
1925 int32_t trackId = -1;
1926 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AAC_44100_2_XXX.aac");
1927 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_AAC;
1928
1929 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1930 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1931 ASSERT_TRUE(isCreated);
1932
1933 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
1934 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
1935 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
1936 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
1937 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_U8P);
1938 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 705600); // 705600 bit rate
1939 audioParams->PutIntValue("audio_samples_per_frame", 1024); // 1024 frame size
1940 audioParams->PutIntValue(OH_MD_KEY_PROFILE, AAC_PROFILE_LC);
1941 audioParams->PutIntValue(OH_MD_KEY_AAC_IS_ADTS, 0);
1942 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_STEREO);
1943 ASSERT_EQ(avmuxer_->AddTrack(trackId, audioParams), 0);
1944 }
1945
1946 /**
1947 * @tc.name: Muxer_AAC_003
1948 * @tc.desc: Muxer mux aac, test OH_MD_KEY_CHANNEL_LAYOUT
1949 * @tc.type: FUNC
1950 */
1951 HWTEST_F(AVMuxerUnitTest, Muxer_AAC_003, TestSize.Level0)
1952 {
1953 int32_t trackId = -1;
1954 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AAC_44100_2_XXX.aac");
1955 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_AAC;
1956
1957 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1958 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1959 ASSERT_TRUE(isCreated);
1960
1961 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
1962 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
1963 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
1964 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
1965 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
1966 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 1411200); // 1411200 bit rate
1967 audioParams->PutIntValue("audio_samples_per_frame", 1024); // 1024 frame size
1968 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_2POINT0POINT2);
1969 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1970 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_AMB_ORDER1_ACN_N3D);
1971 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1972 audioParams->PutLongValue(OH_MD_KEY_CHANNEL_LAYOUT, CH_LAYOUT_AMB_ORDER1_FUMA);
1973 ASSERT_NE(avmuxer_->AddTrack(trackId, audioParams), 0);
1974 }
1975
1976 /**
1977 * @tc.name: Muxer_AAC_004
1978 * @tc.desc: Muxer check unsupported adts value
1979 * @tc.type: FUNC
1980 */
1981 HWTEST_F(AVMuxerUnitTest, Muxer_AAC_004, TestSize.Level0)
1982 {
1983 int32_t trackId = -1;
1984 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AAC_44100_2.aac");
1985 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_AAC;
1986
1987 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1988 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
1989 ASSERT_TRUE(isCreated);
1990
1991 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
1992 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
1993 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
1994 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
1995 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_U8);
1996 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 705600); // 705600 bit rate
1997 audioParams->PutIntValue("audio_samples_per_frame", 2048); // 2048 frame size
1998 audioParams->PutIntValue(OH_MD_KEY_PROFILE, AAC_PROFILE_LC);
1999 audioParams->PutIntValue(OH_MD_KEY_AAC_IS_ADTS, 3); // unsupported aac_is_adts value
2000 int32_t ret = avmuxer_->AddTrack(trackId, audioParams);
2001 ret = (ret == AV_ERR_OK) ? AV_ERR_OK : AV_ERR_INVALID_VAL;
2002 ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
2003 }
2004
2005 /**
2006 * @tc.name: Muxer_AAC_005
2007 * @tc.desc: Muxer check unsupported profile value
2008 * @tc.type: FUNC
2009 */
2010 HWTEST_F(AVMuxerUnitTest, Muxer_AAC_005, TestSize.Level0)
2011 {
2012 int32_t trackId = -1;
2013 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AAC_44100_2.aac");
2014 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_AAC;
2015
2016 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2017 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2018 ASSERT_TRUE(isCreated);
2019
2020 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2021 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
2022 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
2023 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
2024 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_U8);
2025 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 705600); // 705600 bit rate
2026 audioParams->PutIntValue("audio_samples_per_frame", 2048); // 2048 frame size
2027 audioParams->PutIntValue(OH_MD_KEY_PROFILE, 2); // unsupported profile value
2028 audioParams->PutIntValue(OH_MD_KEY_AAC_IS_ADTS, 0);
2029 int32_t ret = avmuxer_->AddTrack(trackId, audioParams);
2030 ret = (ret == AV_ERR_OK) ? AV_ERR_OK : AV_ERR_INVALID_VAL;
2031 ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
2032 }
2033
2034 /**
2035 * @tc.name: Muxer_AddTrack_Auxiliary_001
2036 * @tc.desc: Muxer AddTrack video Auxiliary track.
2037 * @tc.type: FUNC
2038 */
2039 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_Auxiliary_001, TestSize.Level0) {
2040 int32_t trackId = -1;
2041 int32_t trackIdDepth = -1;
2042 std::vector<int32_t> vDepth = {0};
2043 int32_t *trackIdsDepth = vDepth.data();
2044 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack_Auxiliary.mp4");
2045 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2046
2047 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2048 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2049 ASSERT_TRUE(isCreated);
2050
2051 std::shared_ptr<FormatMock> videoParams =
2052 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
2053
2054 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
2055 ASSERT_EQ(ret, 0);
2056 ASSERT_GE(trackId, 0);
2057
2058 std::shared_ptr<FormatMock> metadataParamsDepth = FormatMockFactory::CreateFormat();
2059 metadataParamsDepth->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
2060 metadataParamsDepth->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
2061 metadataParamsDepth->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
2062 metadataParamsDepth->PutIntValue(OH_MD_KEY_TRACK_TYPE, static_cast<int32_t>(OH_MediaType::MEDIA_TYPE_AUXILIARY));
2063 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_REFERENCE_TYPE, TRACK_REF_TYPE_DEPTH);
2064 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_DESCRIPTION, AUXILIARY_DEPTH_TRACK_KEY);
2065 metadataParamsDepth->PutIntBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, trackIdsDepth, vDepth.size());
2066
2067 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2068 ASSERT_EQ(ret, AV_ERR_OK);
2069 ASSERT_GE(trackIdDepth, 1);
2070 }
2071
2072 /**
2073 * @tc.name: Muxer_AddTrack_Auxiliary_002
2074 * @tc.desc: Muxer AddTrack video Auxiliary track(no parameter).
2075 * @tc.type: FUNC
2076 */
2077 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_Auxiliary_002, TestSize.Level0) {
2078 int32_t trackId = -1;
2079 int32_t trackIdDepth = -1;
2080 std::vector<int32_t> vDepth = {0};
2081 int32_t *trackIdsDepth = vDepth.data();
2082 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack_Auxiliary.mp4");
2083 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2084
2085 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2086 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2087 ASSERT_TRUE(isCreated);
2088
2089 std::shared_ptr<FormatMock> videoParams =
2090 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
2091
2092 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
2093 ASSERT_EQ(ret, 0);
2094 ASSERT_GE(trackId, 0);
2095
2096 std::shared_ptr<FormatMock> metadataParamsDepth = FormatMockFactory::CreateFormat();
2097 metadataParamsDepth->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
2098 metadataParamsDepth->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
2099 metadataParamsDepth->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
2100
2101 metadataParamsDepth->PutIntValue(OH_MD_KEY_TRACK_TYPE, static_cast<int32_t>(OH_MediaType::MEDIA_TYPE_AUXILIARY));
2102 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2103 ASSERT_NE(ret, AV_ERR_OK);
2104
2105 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_REFERENCE_TYPE, TRACK_REF_TYPE_DEPTH);
2106 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2107 ASSERT_NE(ret, AV_ERR_OK);
2108
2109 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_DESCRIPTION, AUXILIARY_DEPTH_TRACK_KEY);
2110 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2111 ASSERT_NE(ret, AV_ERR_OK);
2112
2113 metadataParamsDepth->PutIntBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, trackIdsDepth, vDepth.size());
2114 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2115 ASSERT_EQ(ret, AV_ERR_OK);
2116 }
2117
2118 /**
2119 * @tc.name: Muxer_AddTrack_Auxiliary_003
2120 * @tc.desc: Muxer AddTrack video Auxiliary track(H265 video).
2121 * @tc.type: FUNC
2122 */
2123 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_Auxiliary_003, TestSize.Level0) {
2124 if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
2125 return;
2126 }
2127 int32_t trackId = -1;
2128 int32_t trackIdDepth = -1;
2129 std::vector<int32_t> vDepth = {0};
2130 int32_t *trackIdsDepth = vDepth.data();
2131 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack_Auxiliary.mp4");
2132 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2133
2134 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2135 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2136 ASSERT_TRUE(isCreated);
2137
2138 std::shared_ptr<FormatMock> videoParams =
2139 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, TEST_WIDTH, TEST_HEIGHT);
2140
2141 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
2142 ASSERT_EQ(ret, 0);
2143 ASSERT_GE(trackId, 0);
2144
2145 std::shared_ptr<FormatMock> metadataParamsDepth = FormatMockFactory::CreateFormat();
2146 metadataParamsDepth->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_HEVC);
2147 metadataParamsDepth->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
2148 metadataParamsDepth->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
2149 metadataParamsDepth->PutIntValue(OH_MD_KEY_TRACK_TYPE, static_cast<int32_t>(OH_MediaType::MEDIA_TYPE_AUXILIARY));
2150 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_REFERENCE_TYPE, TRACK_REF_TYPE_DEPTH);
2151 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_DESCRIPTION, AUXILIARY_DEPTH_TRACK_KEY);
2152 metadataParamsDepth->PutIntBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, trackIdsDepth, vDepth.size());
2153
2154 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2155 ASSERT_EQ(ret, AV_ERR_OK);
2156 ASSERT_GE(trackIdDepth, 1);
2157 }
2158
2159 /**
2160 * @tc.name: Muxer_AddTrack_Auxiliary_004
2161 * @tc.desc: Muxer AddTrack video Auxiliary track(invalid reference track id).
2162 * @tc.type: FUNC
2163 */
2164 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_Auxiliary_004, TestSize.Level0) {
2165 int32_t trackId = -1;
2166 int32_t trackIdDepth = -1;
2167 std::vector<int32_t> vDepth = {10}; // invalid reference track id
2168 int32_t *trackIdsDepth = vDepth.data();
2169 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack_Auxiliary.mp4");
2170 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2171
2172 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2173 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2174 ASSERT_TRUE(isCreated);
2175
2176 std::shared_ptr<FormatMock> videoParams =
2177 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
2178
2179 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
2180 ASSERT_EQ(ret, 0);
2181 ASSERT_GE(trackId, 0);
2182
2183 std::shared_ptr<FormatMock> metadataParamsDepth = FormatMockFactory::CreateFormat();
2184 metadataParamsDepth->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
2185 metadataParamsDepth->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
2186 metadataParamsDepth->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
2187 metadataParamsDepth->PutIntValue(OH_MD_KEY_TRACK_TYPE, static_cast<int32_t>(OH_MediaType::MEDIA_TYPE_AUXILIARY));
2188 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_REFERENCE_TYPE, TRACK_REF_TYPE_DEPTH);
2189 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_DESCRIPTION, AUXILIARY_DEPTH_TRACK_KEY);
2190 metadataParamsDepth->PutIntBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, trackIdsDepth, vDepth.size());
2191
2192 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2193 ASSERT_NE(ret, AV_ERR_OK);
2194 }
2195
2196 /**
2197 * @tc.name: Muxer_AddTrack_Auxiliary_005
2198 * @tc.desc: Muxer AddTrack video Auxiliary track(use PutBuffer).
2199 * @tc.type: FUNC
2200 */
2201 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_Auxiliary_005, TestSize.Level0) {
2202 int32_t trackId = -1;
2203 int32_t trackIdDepth = -1;
2204 std::vector<int32_t> vDepth = {0};
2205 int32_t *trackIdsDepth = vDepth.data();
2206 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack_Auxiliary.mp4");
2207 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2208
2209 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2210 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2211 ASSERT_TRUE(isCreated);
2212
2213 std::shared_ptr<FormatMock> videoParams =
2214 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
2215
2216 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
2217 ASSERT_EQ(ret, 0);
2218 ASSERT_GE(trackId, 0);
2219
2220 std::shared_ptr<FormatMock> metadataParamsDepth = FormatMockFactory::CreateFormat();
2221 metadataParamsDepth->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
2222 metadataParamsDepth->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
2223 metadataParamsDepth->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
2224 metadataParamsDepth->PutIntValue(OH_MD_KEY_TRACK_TYPE, static_cast<int32_t>(OH_MediaType::MEDIA_TYPE_AUXILIARY));
2225 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_REFERENCE_TYPE, TRACK_REF_TYPE_DEPTH);
2226 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_DESCRIPTION, AUXILIARY_DEPTH_TRACK_KEY);
2227 std::vector<int32_t> vPutBuffer = {0};
2228 int32_t *trackIdsPutBuffer = vPutBuffer.data();
2229 ret = metadataParamsDepth->PutBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, reinterpret_cast<uint8_t*>(trackIdsPutBuffer),
2230 sizeof(int32_t) * vPutBuffer.size());
2231 ASSERT_NE(ret, true);
2232 ret = metadataParamsDepth->PutIntBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, trackIdsDepth, vDepth.size());
2233 ASSERT_EQ(ret, true);
2234
2235 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2236 ASSERT_EQ(ret, AV_ERR_OK);
2237 }
2238
2239 /**
2240 * @tc.name: Muxer_Add_Video_Auxiliary
2241 * @tc.desc: Muxer add video Auxiliary.
2242 * @tc.type: FUNC
2243 */
2244 HWTEST_F(AVMuxerUnitTest, Muxer_Add_Video_Auxiliary, TestSize.Level0) {
2245 int32_t trackId = -1;
2246 int32_t trackIdDepth = -1;
2247 int32_t trackIdPrey = -1;
2248 std::vector<int32_t> vDepth = {0};
2249 int32_t *trackIdsDepth = vDepth.data();
2250 std::vector<int32_t> vPrey = {0, 1};
2251 int32_t *trackIdsPrey = vPrey.data();
2252 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Add_Video_Auxiliary.mp4");
2253 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2254
2255 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2256 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2257 ASSERT_TRUE(isCreated);
2258
2259 std::shared_ptr<FormatMock> videoParams =
2260 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
2261
2262 int32_t ret = avmuxer_->AddTrack(trackId, videoParams);
2263 ASSERT_EQ(ret, 0);
2264 ASSERT_GE(trackId, 0);
2265
2266 // create auxiliary format depth track
2267 std::shared_ptr<FormatMock> metadataParamsDepth = FormatMockFactory::CreateFormat();
2268 metadataParamsDepth->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
2269 metadataParamsDepth->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
2270 metadataParamsDepth->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
2271 metadataParamsDepth->PutIntValue(OH_MD_KEY_TRACK_TYPE, static_cast<int32_t>(OH_MediaType::MEDIA_TYPE_AUXILIARY));
2272 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_REFERENCE_TYPE, TRACK_REF_TYPE_DEPTH);
2273 metadataParamsDepth->PutStringValue(OH_MD_KEY_TRACK_DESCRIPTION, AUXILIARY_DEPTH_TRACK_KEY);
2274 metadataParamsDepth->PutIntBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, trackIdsDepth, vDepth.size());
2275
2276 ret = avmuxer_->AddTrack(trackIdDepth, metadataParamsDepth);
2277 ASSERT_EQ(ret, AV_ERR_OK);
2278 ASSERT_GE(trackIdDepth, 1);
2279
2280 // create auxiliary format prey track
2281 std::shared_ptr<FormatMock> metadataParamsPrey = FormatMockFactory::CreateFormat();
2282 metadataParamsPrey->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC);
2283 metadataParamsPrey->PutIntValue(OH_MD_KEY_WIDTH, TEST_WIDTH);
2284 metadataParamsPrey->PutIntValue(OH_MD_KEY_HEIGHT, TEST_HEIGHT);
2285 metadataParamsPrey->PutIntValue(OH_MD_KEY_TRACK_TYPE, static_cast<int32_t>(OH_MediaType::MEDIA_TYPE_AUXILIARY));
2286 metadataParamsPrey->PutStringValue(OH_MD_KEY_TRACK_REFERENCE_TYPE, TRACK_REF_TYPE_PREY);
2287 metadataParamsPrey->PutStringValue(OH_MD_KEY_TRACK_DESCRIPTION, AUXILIARY_PREY_TRACK_KEY);
2288 metadataParamsPrey->PutIntBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, trackIdsPrey, vPrey.size());
2289
2290 ret = avmuxer_->AddTrack(trackIdPrey, metadataParamsPrey);
2291 ASSERT_EQ(ret, AV_ERR_OK);
2292 ASSERT_GE(trackIdPrey, 2);
2293
2294 ASSERT_EQ(avmuxer_->Start(), 0);
2295
2296 AuxiliaryWriteSample(INPUT_FILE_PATH, trackId);
2297 AuxiliaryWriteSample(INPUT_FILE_PATH, trackIdDepth);
2298 AuxiliaryWriteSample(INPUT_FILE_PATH, trackIdPrey);
2299
2300 ASSERT_EQ(avmuxer_->Stop(), 0);
2301 }
2302
2303 /**
2304 * @tc.name: Muxer_Add_Audio_Auxiliary
2305 * @tc.desc: Muxer add audio Auxiliary.
2306 * @tc.type: FUNC
2307 */
2308 HWTEST_F(AVMuxerUnitTest, Muxer_Add_Audio_Auxiliary, TestSize.Level0) {
2309 int32_t trackId = -1;
2310 int32_t trackIdAudio = -1;
2311 std::vector<int32_t> vAudio = {0};
2312 int32_t *trackIdsAudio = vAudio.data();
2313 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Add_Audio_Auxiliary.mp4");
2314 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2315
2316 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2317 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2318 ASSERT_TRUE(isCreated);
2319
2320 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2321 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
2322 audioParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
2323 audioParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
2324 audioParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
2325 audioParams->PutLongValue(OH_MD_KEY_BITRATE, 199000); // 199000 bit rate
2326 audioParams->PutIntValue("audio_samples_per_frame", 1024); // 1024 frame size
2327 audioParams->PutIntValue(OH_MD_KEY_PROFILE, AAC_PROFILE_LC);
2328 audioParams->PutIntValue(OH_MD_KEY_AAC_IS_ADTS, 0);
2329
2330 int32_t ret = avmuxer_->AddTrack(trackId, audioParams);
2331 ASSERT_EQ(ret, 0);
2332 ASSERT_GE(trackId, 0);
2333
2334 std::shared_ptr<FormatMock> audioAuxiliaryParams = FormatMockFactory::CreateFormat();
2335 audioAuxiliaryParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
2336 audioAuxiliaryParams->PutIntValue(OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // 44100 sample rate
2337 audioAuxiliaryParams->PutIntValue(OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // 2 channels
2338 audioAuxiliaryParams->PutIntValue(OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
2339 audioAuxiliaryParams->PutLongValue(OH_MD_KEY_BITRATE, 199000); // 199000 bit rate
2340 audioAuxiliaryParams->PutIntValue("audio_samples_per_frame", 1024); // 1024 frame size
2341 audioAuxiliaryParams->PutIntValue(OH_MD_KEY_PROFILE, AAC_PROFILE_LC);
2342 audioAuxiliaryParams->PutIntValue(OH_MD_KEY_AAC_IS_ADTS, 0);
2343 audioAuxiliaryParams->PutIntValue(OH_MD_KEY_TRACK_TYPE, static_cast<int32_t>(OH_MediaType::MEDIA_TYPE_AUXILIARY));
2344 audioAuxiliaryParams->PutStringValue(OH_MD_KEY_TRACK_REFERENCE_TYPE, TRACK_REF_TYPE_AUDIO);
2345 audioAuxiliaryParams->PutStringValue(OH_MD_KEY_TRACK_DESCRIPTION, AUXILIARY_AUDIO_TRACK_KEY);
2346 audioAuxiliaryParams->PutIntBuffer(OH_MD_KEY_REFERENCE_TRACK_IDS, trackIdsAudio, vAudio.size());
2347
2348 ret = avmuxer_->AddTrack(trackIdAudio, audioAuxiliaryParams);
2349 ASSERT_EQ(ret, AV_ERR_OK);
2350 ASSERT_GE(trackIdAudio, 1);
2351
2352 ASSERT_EQ(avmuxer_->Start(), 0);
2353
2354 std::string inputFilePath = "/data/test/media/aac_2c_44100hz_199k.dat";
2355 AuxiliaryWriteSample(inputFilePath, trackId);
2356 AuxiliaryWriteSample(inputFilePath, trackIdAudio);
2357
2358 ASSERT_EQ(avmuxer_->Stop(), 0);
2359 }
2360 #ifdef AVMUXER_UNITTEST_CAPI
2361 /**
2362 * @tc.name: Muxer_Destroy_001
2363 * @tc.desc: Muxer Destroy normal
2364 * @tc.type: FUNC
2365 */
2366 HWTEST_F(AVMuxerUnitTest, Muxer_Destroy_001, TestSize.Level0)
2367 {
2368 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Destroy.mp4");
2369 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2370 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2371 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2372 ASSERT_TRUE(isCreated);
2373
2374 std::shared_ptr<FormatMock> videoParams =
2375 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
2376 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
2377 int32_t videoTrackId = -1;
2378 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
2379 ASSERT_EQ(ret, AV_ERR_OK);
2380 ASSERT_GE(videoTrackId, 0);
2381 ASSERT_EQ(avmuxer_->Start(), 0);
2382 ASSERT_EQ(avmuxer_->Stop(), 0);
2383 ASSERT_EQ(avmuxer_->Destroy(), 0);
2384 }
2385
2386 /**
2387 * @tc.name: Muxer_Destroy_002
2388 * @tc.desc: Muxer Destroy normal
2389 * @tc.type: FUNC
2390 */
2391 HWTEST_F(AVMuxerUnitTest, Muxer_Destroy_002, TestSize.Level0)
2392 {
2393 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Destroy.mp4");
2394 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2395 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2396 OH_AVMuxer *muxer = OH_AVMuxer_Create(fd_, outputFormat);
2397 int32_t ret = OH_AVMuxer_Destroy(muxer);
2398 ASSERT_EQ(ret, 0);
2399 muxer = nullptr;
2400 }
2401
2402 /**
2403 * @tc.name: Muxer_Destroy_003
2404 * @tc.desc: Muxer Destroy nullptr
2405 * @tc.type: FUNC
2406 */
2407 HWTEST_F(AVMuxerUnitTest, Muxer_Destroy_003, TestSize.Level0)
2408 {
2409 int32_t ret = OH_AVMuxer_Destroy(nullptr);
2410 ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
2411 }
2412
2413 /**
2414 * @tc.name: Muxer_Destroy_004
2415 * @tc.desc: Muxer Destroy other class
2416 * @tc.type: FUNC
2417 */
2418 HWTEST_F(AVMuxerUnitTest, Muxer_Destroy_004, TestSize.Level0)
2419 {
2420 OH_AVFormat *format = OH_AVFormat_Create();
2421 int32_t ret = OH_AVMuxer_Destroy(reinterpret_cast<OH_AVMuxer*>(format));
2422 ASSERT_EQ(ret, AV_ERR_INVALID_VAL);
2423 OH_AVFormat_Destroy(format);
2424 }
2425
2426 /**
2427 * @tc.name: Muxer_SetFormat_CreationTime_001
2428 * @tc.desc: Muxer set format with valid creation time
2429 * @tc.type: FUNC
2430 */
2431 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_CreationTime_001, TestSize.Level0)
2432 {
2433 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2434 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2435 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2436
2437 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2438 ASSERT_TRUE(isCreated);
2439
2440 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2441 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
2442 audioParams->PutStringValue(OH_MD_KEY_CREATION_TIME, "2023-12-19T03:16:00.000000Z");
2443 int32_t ret = avmuxer_->SetFormat(audioParams);
2444 ASSERT_EQ(ret, 0);
2445 }
2446
2447 /**
2448 * @tc.name: Muxer_SetFormat_CreationTime_002
2449 * @tc.desc: Muxer set format with invalid length
2450 * @tc.type: FUNC
2451 */
2452 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_CreationTime_002, TestSize.Level0)
2453 {
2454 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2455 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2456 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2457
2458 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2459 ASSERT_TRUE(isCreated);
2460
2461 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2462 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
2463 audioParams->PutStringValue(OH_MD_KEY_CREATION_TIME, "2023-12-19T03:16:00.00000000Z");
2464 int32_t ret = avmuxer_->SetFormat(audioParams);
2465 ASSERT_EQ(ret, 3);
2466 }
2467
2468 /**
2469 * @tc.name: Muxer_SetFormat_CreationTime_003
2470 * @tc.desc: Muxer set format and fill several bits with char which should have been int
2471 * @tc.type: FUNC
2472 */
2473 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_CreationTime_003, TestSize.Level0)
2474 {
2475 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2476 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2477 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2478
2479 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2480 ASSERT_TRUE(isCreated);
2481
2482 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2483 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
2484 audioParams->PutStringValue(OH_MD_KEY_CREATION_TIME, "202a-12-19T03:16:0b.0000c0Z");
2485 int32_t ret = avmuxer_->SetFormat(audioParams);
2486 ASSERT_EQ(ret, 3);
2487 }
2488
2489 /**
2490 * @tc.name: Muxer_SetFormat_CreationTime_004
2491 * @tc.desc: Muxer set format without valid key in Meta
2492 * @tc.type: FUNC
2493 */
2494 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_CreationTime_004, TestSize.Level0)
2495 {
2496 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2497 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2498 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2499
2500 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2501 ASSERT_TRUE(isCreated);
2502
2503 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2504 audioParams->PutStringValue(OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_MPEG);
2505 int32_t ret = avmuxer_->SetFormat(audioParams);
2506 ASSERT_EQ(ret, 0);
2507 }
2508
2509 /**
2510 * @tc.name: Muxer_SetFormat_DefinedKey_001
2511 * @tc.desc: Muxer set format with defined key(include MEDIA_CREATION_TIME)
2512 * @tc.type: FUNC
2513 */
2514 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_DefinedKey_001, TestSize.Level0)
2515 {
2516 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2517 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2518 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2519
2520 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2521 ASSERT_TRUE(isCreated);
2522
2523 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2524 audioParams->PutIntValue(Tag::AUDIO_CHANNEL_COUNT, 2);
2525 audioParams->PutIntValue(Tag::AUDIO_SAMPLE_RATE, 48000);
2526 audioParams->PutStringValue(Tag::MEDIA_CREATION_TIME, "2023-12-19T03:16:00.000000Z");
2527 audioParams->PutLongValue(Tag::MEDIA_BITRATE, 128000);
2528 audioParams->PutFloatValue(Tag::MEDIA_LATITUDE, 22.67f);
2529 int32_t ret = avmuxer_->SetFormat(audioParams);
2530 ASSERT_EQ(ret, 0);
2531 }
2532
2533 /**
2534 * @tc.name: Muxer_SetFormat_DefinedKey_002
2535 * @tc.desc: Muxer set format with defined key(no include MEDIA_CREATION_TIME)
2536 * @tc.type: FUNC
2537 */
2538 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_DefinedKey_002, TestSize.Level0)
2539 {
2540 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2541 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2542 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2543
2544 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2545 ASSERT_TRUE(isCreated);
2546
2547 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2548 audioParams->PutIntValue(Tag::AUDIO_CHANNEL_COUNT, 2);
2549 audioParams->PutIntValue(Tag::AUDIO_SAMPLE_RATE, 48000);
2550 audioParams->PutLongValue(Tag::MEDIA_BITRATE, 128000);
2551 audioParams->PutFloatValue(Tag::MEDIA_LATITUDE, 22.67f);
2552 int32_t ret = avmuxer_->SetFormat(audioParams);
2553 ASSERT_EQ(ret, 0);
2554 }
2555
2556 /**
2557 * @tc.name: Muxer_SetFormat_UserKey_001
2558 * @tc.desc: Muxer set format with user key(true keys)
2559 * @tc.type: FUNC
2560 */
2561 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_UserKey_001, TestSize.Level0)
2562 {
2563 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2564 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2565 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2566
2567 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2568 ASSERT_TRUE(isCreated);
2569
2570 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2571 audioParams->PutIntValue("com.openharmony.version", 5);
2572 audioParams->PutStringValue("com.openharmony.model", "LNA-AL00");
2573 audioParams->PutFloatValue("com.openharmony.capture.fps", 30.00f);
2574 int32_t ret = avmuxer_->SetFormat(audioParams);
2575 ASSERT_EQ(ret, 0);
2576 }
2577
2578 /**
2579 * @tc.name: Muxer_SetFormat_UserKey_002
2580 * @tc.desc: Muxer set format with user key(include wrong keys)
2581 * @tc.type: FUNC
2582 */
2583 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_UserKey_002, TestSize.Level0)
2584 {
2585 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2586 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2587 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2588
2589 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2590 ASSERT_TRUE(isCreated);
2591
2592 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2593 audioParams->PutIntValue("com.openharmony.version", 5);
2594 audioParams->PutStringValue("com.openharmony.model", "LNA-AL00");
2595 audioParams->PutIntValue("com.openopen.version", 1);
2596 audioParams->PutFloatValue("com.openharmony.capture.fps", 30.00f);
2597 audioParams->PutFloatValue("capture.fps", 30.00f);
2598 int32_t ret = avmuxer_->SetFormat(audioParams);
2599 ASSERT_EQ(ret, 0);
2600 }
2601
2602 /**
2603 * @tc.name: Muxer_SetFormat_UserKey_003
2604 * @tc.desc: Muxer set format with user key(include wrong keys and 2 times)
2605 * @tc.type: FUNC
2606 */
2607 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_UserKey_003, TestSize.Level0)
2608 {
2609 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2610 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2611 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2612
2613 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2614 ASSERT_TRUE(isCreated);
2615
2616 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2617 audioParams->PutIntValue("com.openharmony.version", 5);
2618 audioParams->PutStringValue("com.openharmony.model", "LNA-AL00");
2619 audioParams->PutIntValue("com.openopen.version", 1);
2620 audioParams->PutFloatValue("com.openharmony.capture.fps", 30.00f);
2621 audioParams->PutFloatValue("capture.fps", 30.00f);
2622 int32_t ret = avmuxer_->SetFormat(audioParams);
2623 ASSERT_EQ(ret, 0);
2624
2625 audioParams->PutIntValue("com.openharmony.version", 5);
2626 audioParams->PutStringValue("com.openharmony.model", "LNA-AL00");
2627 audioParams->PutFloatValue("com.openharmony.capture.fps", 30.00f);
2628 ret = avmuxer_->SetFormat(audioParams);
2629 ASSERT_EQ(ret, 0);
2630 }
2631
2632 /**
2633 * @tc.name: Muxer_SetFormat_UserKey_004
2634 * @tc.desc: Muxer set format with user key(string keys length more than 256 characters)
2635 * @tc.type: FUNC
2636 */
2637 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_UserKey_004, TestSize.Level0)
2638 {
2639 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2640 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2641 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2642
2643 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2644 ASSERT_TRUE(isCreated);
2645
2646 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2647 std::string str = "";
2648 for (uint32_t i = 0; i < 260; ++i) {
2649 char ch = '0' + i % 10;
2650 str += ch;
2651 }
2652 audioParams->PutStringValue("com.openharmony.model", str);
2653 int32_t ret = avmuxer_->SetFormat(audioParams);
2654 ASSERT_EQ(ret, 3);
2655 }
2656
2657 /**
2658 * @tc.name: Muxer_SetFormat_UserKey_005
2659 * @tc.desc: Muxer set format with user key(string keys length less than 256 characters)
2660 * @tc.type: FUNC
2661 */
2662 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_UserKey_005, TestSize.Level0)
2663 {
2664 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2665 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2666 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2667
2668 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2669 ASSERT_TRUE(isCreated);
2670
2671 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2672 std::string str = "";
2673 for (uint32_t i = 0; i < 255; ++i) {
2674 char ch = '0' + i % 10;
2675 str += ch;
2676 }
2677 audioParams->PutStringValue("com.openharmony.model", str);
2678 int32_t ret = avmuxer_->SetFormat(audioParams);
2679 ASSERT_EQ(ret, 0);
2680 }
2681
2682 /**
2683 * @tc.name: Muxer_SetFormat_UserKey_006
2684 * @tc.desc: Muxer set format with user key(include buffer)
2685 * @tc.type: FUNC
2686 */
2687 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_UserKey_006, TestSize.Level0)
2688 {
2689 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2690 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2691 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2692
2693 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2694 ASSERT_TRUE(isCreated);
2695
2696 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2697 uint8_t testData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
2698 ASSERT_EQ(sizeof(testData), 10);
2699 audioParams->PutBuffer("com.openharmony.test", testData, sizeof(testData));
2700
2701 int32_t ret = avmuxer_->SetFormat(audioParams);
2702 ASSERT_EQ(ret, 0);
2703 }
2704
2705 /**
2706 * @tc.name: Muxer_SetFormat_UserKey_007
2707 * @tc.desc: Muxer set format with user key(more than max size)
2708 * @tc.type: FUNC
2709 */
2710 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_UserKey_007, TestSize.Level0)
2711 {
2712 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2713 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2714 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2715
2716 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2717 ASSERT_TRUE(isCreated);
2718
2719 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2720
2721 // more than max size
2722 const size_t maxSize = 1024 * 1024;
2723 uint8_t* testData = new uint8_t[maxSize + 10];
2724 bool retBool = audioParams->PutBuffer("com.openharmony.test", testData, maxSize + 10);
2725 ASSERT_EQ(retBool, false);
2726 delete[] testData;
2727
2728 // max size
2729 const size_t maxSize2 = 1024 * 1024;
2730 uint8_t* testData2 = new uint8_t[maxSize2];
2731 bool retBool2 = audioParams->PutBuffer("com.openharmony.test", testData2, maxSize2);
2732 ASSERT_EQ(retBool2, true);
2733 delete[] testData2;
2734
2735 int32_t ret = avmuxer_->SetFormat(audioParams);
2736 ASSERT_EQ(ret, 0);
2737 }
2738
2739 /**
2740 * @tc.name: Muxer_SetFormat_UserKey_008
2741 * @tc.desc: Muxer set format with user key(complete)
2742 * @tc.type: FUNC
2743 */
2744 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_UserKey_008, TestSize.Level0)
2745 {
2746 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2747 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2748 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2749
2750 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2751 ASSERT_TRUE(isCreated);
2752
2753 std::shared_ptr<FormatMock> videoParams =
2754 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
2755 videoParams->PutBuffer(OH_MD_KEY_CODEC_CONFIG, buffer_, sizeof(buffer_));
2756 int32_t videoTrackId = -1;
2757 int32_t ret = avmuxer_->AddTrack(videoTrackId, videoParams);
2758 ASSERT_EQ(ret, AV_ERR_OK);
2759
2760 ASSERT_EQ(avmuxer_->Start(), 0);
2761
2762 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2763 uint8_t testData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
2764 ASSERT_EQ(sizeof(testData), 10);
2765 audioParams->PutBuffer("com.openharmony.test", testData, sizeof(testData));
2766
2767 ret = avmuxer_->SetFormat(audioParams);
2768 ASSERT_EQ(ret, 0);
2769
2770 ASSERT_EQ(avmuxer_->Stop(), 0);
2771 ASSERT_EQ(avmuxer_->Destroy(), 0);
2772 }
2773
2774 /**
2775 * @tc.name: Muxer_SetFormat_DefinedKey_And_UserKey_001
2776 * @tc.desc: Muxer set format with user key(true keys)
2777 * @tc.type: FUNC
2778 */
2779 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_DefinedKey_And_UserKey_001, TestSize.Level0)
2780 {
2781 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2782 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2783 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2784
2785 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2786 ASSERT_TRUE(isCreated);
2787
2788 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2789 audioParams->PutIntValue("com.openharmony.version", 5);
2790 audioParams->PutLongValue(Tag::MEDIA_BITRATE, 128000);
2791 audioParams->PutStringValue("com.openharmony.model", "LNA-AL00");
2792 audioParams->PutStringValue(Tag::MEDIA_CREATION_TIME, "2023-12-19T03:16:00.000000Z");
2793 audioParams->PutFloatValue("com.openharmony.capture.fps", 30.00f);
2794 int32_t ret = avmuxer_->SetFormat(audioParams);
2795 ASSERT_EQ(ret, 0);
2796 }
2797
2798 /**
2799 * @tc.name: Muxer_SetFormat_DefinedKey_And_UserKey_002
2800 * @tc.desc: Muxer set format with user key(include wrong value)
2801 * @tc.type: FUNC
2802 */
2803 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_DefinedKey_And_UserKey_002, TestSize.Level0)
2804 {
2805 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2806 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2807 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2808
2809 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2810 ASSERT_TRUE(isCreated);
2811
2812 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2813 audioParams->PutIntValue("com.openharmony.version", 5);
2814 audioParams->PutLongValue(Tag::MEDIA_BITRATE, 128000);
2815 audioParams->PutStringValue("com.openharmony.model", "LNA-AL00");
2816 audioParams->PutStringValue(Tag::MEDIA_CREATION_TIME, "202a-12-19T03:16:0b.0000c0Z");
2817 audioParams->PutFloatValue("com.openharmony.capture.fps", 30.00f);
2818 int32_t ret = avmuxer_->SetFormat(audioParams);
2819 ASSERT_NE(ret, 0);
2820 }
2821
2822 /**
2823 * @tc.name: Muxer_SetFormat_DefinedKey_And_UserKey_003
2824 * @tc.desc: Muxer set format with user key(include wrong keys)
2825 * @tc.type: FUNC
2826 */
2827 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_DefinedKey_And_UserKey_003, TestSize.Level0)
2828 {
2829 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2830 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2831 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2832
2833 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2834 ASSERT_TRUE(isCreated);
2835
2836 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2837 audioParams->PutIntValue("com.openharny.version", 5);
2838 audioParams->PutLongValue(Tag::MEDIA_BITRATE, 128000);
2839 audioParams->PutStringValue("c.openharmony.model", "LNA-AL00");
2840 audioParams->PutStringValue(Tag::MEDIA_CREATION_TIME, "2023-12-19T03:16:00.000000Z");
2841 audioParams->PutFloatValue("com.openhar.capture.fps", 30.00f);
2842 int32_t ret = avmuxer_->SetFormat(audioParams);
2843 ASSERT_EQ(ret, 0);
2844 }
2845
2846 /**
2847 * @tc.name: Muxer_SetFormat_Comment_001
2848 * @tc.desc: Muxer set format with defined key(include MEDIA_COMMENT)
2849 * @tc.type: FUNC
2850 */
2851 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_Comment_001, TestSize.Level0)
2852 {
2853 int32_t trackId = -1;
2854 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2855 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2856 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2857
2858 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2859 ASSERT_TRUE(isCreated);
2860
2861 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2862 audioParams->PutIntValue(Tag::AUDIO_CHANNEL_COUNT, 2);
2863 audioParams->PutIntValue(Tag::AUDIO_SAMPLE_RATE, 48000);
2864 audioParams->PutLongValue(Tag::MEDIA_BITRATE, 128000);
2865 audioParams->PutStringValue(Tag::MEDIA_COMMENT, "comment_test_str");
2866 int32_t ret = avmuxer_->SetFormat(audioParams);
2867 ASSERT_EQ(ret, 0);
2868 audioParams->InitAudioTrackFormat(Plugins::MimeType::AUDIO_MPEG, 48000, 2);
2869 ret = avmuxer_->AddTrack(trackId, audioParams);
2870 ASSERT_EQ(ret, 0);
2871 ASSERT_GE(trackId, 0);
2872 ASSERT_EQ(avmuxer_->Start(), 0);
2873
2874 OH_AVCodecBufferAttr info;
2875 info.pts = 0;
2876 info.offset = 0;
2877 info.size = sizeof(buffer_);
2878 ret = avmuxer_->WriteSample(trackId, buffer_, info);
2879 ASSERT_EQ(ret, 0);
2880 }
2881
2882 /**
2883 * @tc.name: Muxer_SetFormat_Comment_002
2884 * @tc.desc: Muxer set invalid comment with length 0 (less than 1)
2885 * @tc.type: FUNC
2886 */
2887 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_Comment_002, TestSize.Level0)
2888 {
2889 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2890 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2891 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2892
2893 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2894 ASSERT_TRUE(isCreated);
2895
2896 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2897 audioParams->PutIntValue(Tag::AUDIO_CHANNEL_COUNT, 2);
2898 audioParams->PutIntValue(Tag::AUDIO_SAMPLE_RATE, 48000);
2899 audioParams->PutLongValue(Tag::MEDIA_BITRATE, 128000);
2900 audioParams->PutStringValue(Tag::MEDIA_COMMENT, "");
2901 int32_t ret = avmuxer_->SetFormat(audioParams);
2902 ASSERT_NE(ret, 0);
2903 }
2904
2905 /**
2906 * @tc.name: Muxer_SetFormat_Comment_003
2907 * @tc.desc: Muxer set invalid comment with length 0 (longer than 256)
2908 * @tc.type: FUNC
2909 */
2910 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_Comment_003, TestSize.Level0)
2911 {
2912 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat.mp4");
2913 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2914 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2915
2916 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2917 ASSERT_TRUE(isCreated);
2918
2919 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2920 audioParams->PutIntValue(Tag::AUDIO_CHANNEL_COUNT, 2);
2921 audioParams->PutIntValue(Tag::AUDIO_SAMPLE_RATE, 48000);
2922 audioParams->PutLongValue(Tag::MEDIA_BITRATE, 128000);
2923 audioParams->PutStringValue(Tag::MEDIA_COMMENT, "thisisaninvalidcommentstringwhichistoolongthisisaninvalidcomment"
2924 "stringwhichistoolongthisisaninvalidcommentstringwhichistoolongthisisaninvalidcommentstringwhichistoolongthis"
2925 "isaninvalidcommentstringwhichistoolongthisisaninvalidcommentstringwhichistoolongthisisan");
2926 int32_t ret = avmuxer_->SetFormat(audioParams);
2927 ASSERT_NE(ret, 0);
2928 }
2929
2930 /**
2931 * @tc.name: Muxer_SetFormat_IsMoovFront_001
2932 * @tc.desc: do not set moov in front
2933 * @tc.type: FUNC
2934 */
2935 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_IsMoovFront_001, TestSize.Level0)
2936 {
2937 int32_t trackId = -1;
2938 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat_IsMoovFront_001.mp4");
2939 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2940 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2941
2942 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2943 ASSERT_TRUE(isCreated);
2944
2945 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2946 audioParams->PutIntValue(Tag::MEDIA_ENABLE_MOOV_FRONT, 0);
2947 int32_t ret = avmuxer_->SetFormat(audioParams);
2948 ASSERT_EQ(ret, 0);
2949 audioParams->InitAudioTrackFormat(Plugins::MimeType::AUDIO_MPEG, 48000, 2);
2950 ret = avmuxer_->AddTrack(trackId, audioParams);
2951 ASSERT_EQ(ret, 0);
2952 ASSERT_GE(trackId, 0);
2953 ASSERT_EQ(avmuxer_->Start(), 0);
2954
2955 OH_AVCodecBufferAttr info;
2956 info.pts = 0;
2957 info.offset = 0;
2958 info.size = sizeof(buffer_);
2959 ret = avmuxer_->WriteSample(trackId, buffer_, info);
2960 ASSERT_EQ(ret, 0);
2961 }
2962
2963 /**
2964 * @tc.name: Muxer_SetFormat_IsMoovFront_002
2965 * @tc.desc: set moov in front
2966 * @tc.type: FUNC
2967 */
2968 HWTEST_F(AVMuxerUnitTest, Muxer_SetFormat_IsMoovFront_002, TestSize.Level0)
2969 {
2970 int32_t trackId = -1;
2971 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetFormat_IsMoovFront_001.mp4");
2972 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
2973 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
2974
2975 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
2976 ASSERT_TRUE(isCreated);
2977
2978 std::shared_ptr<FormatMock> audioParams = FormatMockFactory::CreateFormat();
2979 audioParams->PutIntValue(Tag::MEDIA_ENABLE_MOOV_FRONT, 1);
2980 int32_t ret = avmuxer_->SetFormat(audioParams);
2981 ASSERT_EQ(ret, 0);
2982 audioParams->InitAudioTrackFormat(Plugins::MimeType::AUDIO_MPEG, 48000, 2);
2983 ret = avmuxer_->AddTrack(trackId, audioParams);
2984 ASSERT_EQ(ret, 0);
2985 ASSERT_GE(trackId, 0);
2986 ASSERT_EQ(avmuxer_->Start(), 0);
2987
2988 OH_AVCodecBufferAttr info;
2989 info.pts = 0;
2990 info.offset = 0;
2991 info.size = sizeof(buffer_);
2992 ret = avmuxer_->WriteSample(trackId, buffer_, info);
2993 ASSERT_EQ(ret, 0);
2994 }
2995 #endif // AVMUXER_UNITTEST_CAPI
2996
2997 #ifdef AVMUXER_UNITTEST_INNER_API
2998 /**
2999 * @tc.name: Muxer_SetParameter_001
3000 * @tc.desc: Muxer SetParameter after Create
3001 * @tc.type: FUNC
3002 */
3003 HWTEST_F(AVMuxerUnitTest, Muxer_SetParameter_001, TestSize.Level0)
3004 {
3005 int32_t trackId = -1;
3006 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetParameter.mp4");
3007 Plugins::OutputFormat outputFormat = Plugins::OutputFormat::MPEG_4;
3008
3009 int32_t fd = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
3010 std::shared_ptr<AVMuxer> avmuxer = AVMuxerFactory::CreateAVMuxer(fd, outputFormat);
3011 ASSERT_NE(avmuxer, nullptr);
3012
3013 std::shared_ptr<Meta> videoParams = std::make_shared<Meta>();
3014 videoParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::VIDEO_AVC);
3015 videoParams->Set<Tag::VIDEO_WIDTH>(TEST_WIDTH);
3016 videoParams->Set<Tag::VIDEO_HEIGHT>(TEST_HEIGHT);
3017
3018 int32_t ret = avmuxer->AddTrack(trackId, videoParams);
3019 ASSERT_EQ(ret, 0);
3020 ASSERT_GE(trackId, 0);
3021
3022 std::shared_ptr<Meta> param = std::make_shared<Meta>();
3023 param->Set<Tag::VIDEO_ROTATION>(Plugins::VideoRotation::VIDEO_ROTATION_0);
3024 param->Set<Tag::MEDIA_CREATION_TIME>("2023-12-19T03:16:00.000Z");
3025 param->Set<Tag::MEDIA_LATITUDE>(22.67f); // 22.67f test latitude
3026 param->Set<Tag::MEDIA_LONGITUDE>(114.06f); // 114.06f test longitude
3027 param->Set<Tag::MEDIA_TITLE>("ohos muxer");
3028 param->Set<Tag::MEDIA_ARTIST>("ohos muxer");
3029 param->Set<Tag::MEDIA_COMPOSER>("ohos muxer");
3030 param->Set<Tag::MEDIA_DATE>("2023-12-19");
3031 param->Set<Tag::MEDIA_ALBUM>("ohos muxer");
3032 param->Set<Tag::MEDIA_ALBUM_ARTIST>("ohos muxer");
3033 param->Set<Tag::MEDIA_COPYRIGHT>("ohos muxer");
3034 param->Set<Tag::MEDIA_GENRE>("{marketing-name:\"HW P60\"}");
3035 ret = avmuxer->SetParameter(param);
3036 EXPECT_EQ(ret, 0);
3037
3038 close(fd);
3039 }
3040
3041 /**
3042 * @tc.name: Muxer_SetParameter_002
3043 * @tc.desc: Muxer SetParameter after Create
3044 * @tc.type: FUNC
3045 */
3046 HWTEST_F(AVMuxerUnitTest, Muxer_SetParameter_002, TestSize.Level0)
3047 {
3048 int32_t trackId = -1;
3049 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetParameter.mp4");
3050 Plugins::OutputFormat outputFormat = Plugins::OutputFormat::MPEG_4;
3051
3052 int32_t fd = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
3053 std::shared_ptr<AVMuxer> avmuxer = AVMuxerFactory::CreateAVMuxer(fd, outputFormat);
3054 ASSERT_NE(avmuxer, nullptr);
3055
3056 std::shared_ptr<Meta> videoParams = std::make_shared<Meta>();
3057 videoParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::VIDEO_AVC);
3058 videoParams->Set<Tag::VIDEO_WIDTH>(TEST_WIDTH);
3059 videoParams->Set<Tag::VIDEO_HEIGHT>(TEST_HEIGHT);
3060
3061 int32_t ret = avmuxer->AddTrack(trackId, videoParams);
3062 ASSERT_EQ(ret, 0);
3063 ASSERT_GE(trackId, 0);
3064
3065 std::shared_ptr<Meta> param = std::make_shared<Meta>();
3066 param->Set<Tag::MEDIA_LONGITUDE>(114.06f); // 114.06f test longitude
3067 ret = avmuxer->SetParameter(param);
3068 EXPECT_NE(ret, 0);
3069
3070 param->Set<Tag::MEDIA_LATITUDE>(-90.0f); // -90.0f test latitude
3071 ret = avmuxer->SetParameter(param);
3072 EXPECT_EQ(ret, 0);
3073
3074 param->Set<Tag::MEDIA_LATITUDE>(90.0f); // 90.0f test latitude
3075 ret = avmuxer->SetParameter(param);
3076 EXPECT_EQ(ret, 0);
3077
3078 param->Set<Tag::MEDIA_LATITUDE>(-90.1f); // -90.1f test latitude
3079 ret = avmuxer->SetParameter(param);
3080 EXPECT_NE(ret, 0);
3081
3082 param->Set<Tag::MEDIA_LATITUDE>(90.1f); // 90.1f test latitude
3083 ret = avmuxer->SetParameter(param);
3084 EXPECT_NE(ret, 0);
3085
3086 close(fd);
3087 }
3088
3089 /**
3090 * @tc.name: Muxer_SetParameter_003
3091 * @tc.desc: Muxer SetParameter after Create
3092 * @tc.type: FUNC
3093 */
3094 HWTEST_F(AVMuxerUnitTest, Muxer_SetParameter_003, TestSize.Level0)
3095 {
3096 int32_t trackId = -1;
3097 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetParameter.mp4");
3098 Plugins::OutputFormat outputFormat = Plugins::OutputFormat::MPEG_4;
3099
3100 int32_t fd = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
3101 std::shared_ptr<AVMuxer> avmuxer = AVMuxerFactory::CreateAVMuxer(fd, outputFormat);
3102 ASSERT_NE(avmuxer, nullptr);
3103
3104 std::shared_ptr<Meta> videoParams = std::make_shared<Meta>();
3105 videoParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::VIDEO_AVC);
3106 videoParams->Set<Tag::VIDEO_WIDTH>(TEST_WIDTH);
3107 videoParams->Set<Tag::VIDEO_HEIGHT>(TEST_HEIGHT);
3108
3109 int32_t ret = avmuxer->AddTrack(trackId, videoParams);
3110 ASSERT_EQ(ret, 0);
3111 ASSERT_GE(trackId, 0);
3112
3113 std::shared_ptr<Meta> param = std::make_shared<Meta>();
3114 param->Set<Tag::MEDIA_LATITUDE>(22.67f); // 22.67f test latitude
3115 ret = avmuxer->SetParameter(param);
3116 EXPECT_NE(ret, 0);
3117
3118 param->Set<Tag::MEDIA_LONGITUDE>(-180.0f); // -180.0f test longitude
3119 ret = avmuxer->SetParameter(param);
3120 EXPECT_EQ(ret, 0);
3121
3122 param->Set<Tag::MEDIA_LONGITUDE>(180.0f); // 180.0f test longitude
3123 ret = avmuxer->SetParameter(param);
3124 EXPECT_EQ(ret, 0);
3125
3126 param->Set<Tag::MEDIA_LONGITUDE>(-180.1f); // -180.1f test longitude
3127 ret = avmuxer->SetParameter(param);
3128 EXPECT_NE(ret, 0);
3129
3130 param->Set<Tag::MEDIA_LONGITUDE>(180.1f); // 180.1f test longitude
3131 ret = avmuxer->SetParameter(param);
3132 EXPECT_NE(ret, 0);
3133
3134 close(fd);
3135 }
3136
3137 /**
3138 * @tc.name: Muxer_SetUserMeta_001
3139 * @tc.desc: Muxer SetUserMeta after Create
3140 * @tc.type: FUNC
3141 */
3142 HWTEST_F(AVMuxerUnitTest, Muxer_SetUserMeta_001, TestSize.Level0)
3143 {
3144 int32_t trackId = -1;
3145 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_SetUserMeta.mp4");
3146 Plugins::OutputFormat outputFormat = Plugins::OutputFormat::MPEG_4;
3147
3148 int32_t fd = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
3149 std::shared_ptr<AVMuxer> avmuxer = AVMuxerFactory::CreateAVMuxer(fd, outputFormat);
3150 ASSERT_NE(avmuxer, nullptr);
3151
3152 std::shared_ptr<Meta> videoParams = std::make_shared<Meta>();
3153 videoParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::VIDEO_AVC);
3154 videoParams->Set<Tag::VIDEO_WIDTH>(TEST_WIDTH);
3155 videoParams->Set<Tag::VIDEO_HEIGHT>(TEST_HEIGHT);
3156
3157 int32_t ret = avmuxer->AddTrack(trackId, videoParams);
3158 ASSERT_EQ(ret, 0);
3159 ASSERT_GE(trackId, 0);
3160
3161 std::shared_ptr<Meta> param = std::make_shared<Meta>();
3162 param->Set<Tag::VIDEO_ROTATION>(Plugins::VideoRotation::VIDEO_ROTATION_0);
3163 param->Set<Tag::MEDIA_CREATION_TIME>("2023-12-19T03:16:00.000Z");
3164 param->Set<Tag::MEDIA_LATITUDE>(22.67f); // 22.67f test latitude
3165 param->Set<Tag::MEDIA_LONGITUDE>(114.06f); // 114.06f test longitude
3166 param->Set<Tag::MEDIA_TITLE>("ohos muxer");
3167 param->Set<Tag::MEDIA_ARTIST>("ohos muxer");
3168 param->Set<Tag::MEDIA_COMPOSER>("ohos muxer");
3169 param->Set<Tag::MEDIA_DATE>("2023-12-19");
3170 param->Set<Tag::MEDIA_ALBUM>("ohos muxer");
3171 param->Set<Tag::MEDIA_ALBUM_ARTIST>("ohos muxer");
3172 param->Set<Tag::MEDIA_COPYRIGHT>("ohos muxer");
3173 param->Set<Tag::MEDIA_GENRE>("{marketing-name:\"HW P60\"}");
3174 param->SetData("fast_start", static_cast<int32_t>(1)); // 1 moov 前置
3175 ret = avmuxer->SetParameter(param);
3176 EXPECT_EQ(ret, 0);
3177
3178 std::shared_ptr<Meta> userMeta = std::make_shared<Meta>();
3179 userMeta->SetData("com.openharmony.version", 5); // 5 test version
3180 userMeta->SetData("com.openharmony.model", "LNA-AL00");
3181 userMeta->SetData("com.openharmony.manufacturer", "HW");
3182 userMeta->SetData("com.openharmony.marketing_name", "HW P60");
3183 userMeta->SetData("com.openharmony.capture.fps", 30.00f); // 30.00f test capture fps
3184 userMeta->SetData("model", "LNA-AL00");
3185 userMeta->SetData("com.openharmony.flag", true);
3186 ret = avmuxer->SetUserMeta(userMeta);
3187 EXPECT_EQ(ret, 0);
3188
3189 close(fd);
3190 }
3191
3192 /**
3193 * @tc.name: Muxer_AddTrack_TimeMeta
3194 * @tc.desc: Muxer AddTrack for timed metadata track
3195 * @tc.type: FUNC
3196 */
3197 HWTEST_F(AVMuxerUnitTest, Muxer_AddTrack_TimeMeta, TestSize.Level0)
3198 {
3199 int32_t vidTrackId = -1;
3200 int32_t metaTrackId = -1;
3201 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AddTrack_TimeMeta.mp4");
3202 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
3203
3204 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
3205 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
3206 ASSERT_TRUE(isCreated);
3207
3208 std::shared_ptr<FormatMock> videoParams =
3209 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
3210
3211 ASSERT_EQ(avmuxer_->AddTrack(vidTrackId, videoParams), 0);
3212 ASSERT_GE(vidTrackId, 0);
3213
3214 std::shared_ptr<FormatMock> metadataParams = FormatMockFactory::CreateTimedMetadataFormat(
3215 TIMED_METADATA_TRACK_MIMETYPE.c_str(), TIMED_METADATA_KEY, 1);
3216 ASSERT_NE(metadataParams, nullptr);
3217
3218 ASSERT_NE(avmuxer_->AddTrack(metaTrackId, metadataParams), 0);
3219
3220 ASSERT_NE(avmuxer_->AddTrack(metaTrackId, metadataParams), 0);
3221
3222 std::shared_ptr<FormatMock> metadataParams2 = FormatMockFactory::CreateTimedMetadataFormat(
3223 TIMED_METADATA_TRACK_MIMETYPE.c_str(), TIMED_METADATA_KEY, -1);
3224 ASSERT_NE(metadataParams, nullptr);
3225
3226 ASSERT_NE(avmuxer_->AddTrack(metaTrackId, metadataParams2), 0);
3227 }
3228
3229 /**
3230 * @tc.name: Muxer_SetFlag_004
3231 * @tc.desc: Muxer Write Sample for timed metadata track
3232 * @tc.type: FUNC
3233 */
3234 HWTEST_F(AVMuxerUnitTest, Muxer_SetFlag_004, TestSize.Level0)
3235 {
3236 int32_t vidTrackId = -1;
3237 int32_t metaTrackId = -1;
3238 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_Timedmetadata_track.mp4");
3239 OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
3240
3241 fd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
3242 bool isCreated = avmuxer_->CreateMuxer(fd_, outputFormat);
3243 ASSERT_TRUE(isCreated);
3244
3245 std::shared_ptr<FormatMock> videoParams =
3246 FormatMockFactory::CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, TEST_WIDTH, TEST_HEIGHT);
3247
3248 ASSERT_EQ(avmuxer_->AddTrack(vidTrackId, videoParams), 0);
3249 ASSERT_GE(vidTrackId, 0);
3250
3251 std::shared_ptr<FormatMock> metadataParams = FormatMockFactory::CreateTimedMetadataFormat(
3252 TIMED_METADATA_TRACK_MIMETYPE.c_str(), TIMED_METADATA_KEY, vidTrackId);
3253 ASSERT_NE(metadataParams, nullptr);
3254
3255 ASSERT_EQ(avmuxer_->AddTrack(metaTrackId, metadataParams), 0);
3256 ASSERT_GE(metaTrackId, 1);
3257
3258 int32_t ret = avmuxer_->SetTimedMetadata();
3259 EXPECT_EQ(ret, 0);
3260
3261 ASSERT_EQ(avmuxer_->Start(), 0);
3262
3263 inputFile_ = std::make_shared<std::ifstream>(INPUT_FILE_PATH, std::ios::binary);
3264
3265 int32_t extSize = 0;
3266 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
3267 if (extSize > 0) {
3268 std::vector<uint8_t> buffer(extSize);
3269 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
3270 }
3271
3272 bool eosFlag = false;
3273 uint32_t flag = AVCODEC_BUFFER_FLAGS_DISPOSABLE_EXT_TEST;
3274 ret = WriteSample(vidTrackId, inputFile_, eosFlag, flag);
3275 while (!eosFlag && (ret == 0)) {
3276 ret = WriteSample(vidTrackId, inputFile_, eosFlag, flag);
3277 }
3278 ASSERT_EQ(ret, 0);
3279
3280 auto inputFileMeta = std::make_shared<std::ifstream>(INPUT_FILE_PATH, std::ios::binary);
3281 extSize = 0;
3282 inputFileMeta->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
3283 if (extSize > 0) {
3284 std::vector<uint8_t> buffer(extSize);
3285 inputFileMeta->read(reinterpret_cast<char*>(buffer.data()), extSize);
3286 }
3287 eosFlag = false;
3288 flag = AVCODEC_BUFFER_FLAGS_DISPOSABLE_EXT_TEST;
3289 ret = WriteSample(metaTrackId, inputFileMeta, eosFlag, flag);
3290 while (!eosFlag && (ret == 0)) {
3291 ret = WriteSample(metaTrackId, inputFileMeta, eosFlag, flag);
3292 }
3293 ASSERT_EQ(ret, 0);
3294 ASSERT_EQ(avmuxer_->Stop(), 0);
3295 }
3296
3297 /**
3298 * @tc.name: Muxer_MP4_001
3299 * @tc.desc: Muxer mux mp4 by h264
3300 * @tc.type: FUNC
3301 */
3302 HWTEST_F(AVMuxerUnitTest, Muxer_MP4_001, TestSize.Level0)
3303 {
3304 int32_t trackId = -1;
3305 std::string outputFile = TEST_FILE_PATH + std::string("Muxer_AVC.mp4");
3306 Plugins::OutputFormat outputFormat = Plugins::OutputFormat::MPEG_4;
3307 int32_t fd = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
3308 std::shared_ptr<AVMuxer> avmuxer = AVMuxerFactory::CreateAVMuxer(fd, outputFormat);
3309 ASSERT_NE(avmuxer, nullptr);
3310
3311 std::shared_ptr<Meta> videoParams = std::make_shared<Meta>();
3312 videoParams->Set<Tag::MIME_TYPE>(Plugins::MimeType::VIDEO_AVC);
3313 videoParams->Set<Tag::VIDEO_WIDTH>(TEST_WIDTH);
3314 videoParams->Set<Tag::VIDEO_HEIGHT>(TEST_HEIGHT);
3315 videoParams->Set<Tag::VIDEO_FRAME_RATE>(60.0); // 60.0 fps
3316 videoParams->Set<Tag::VIDEO_DELAY>(0);
3317 ASSERT_EQ(avmuxer->AddTrack(trackId, videoParams), 0);
3318 ASSERT_GE(trackId, 0);
3319
3320 std::shared_ptr<Meta> param = std::make_shared<Meta>();
3321 param->Set<Tag::VIDEO_ROTATION>(Plugins::VideoRotation::VIDEO_ROTATION_90);
3322 param->Set<Tag::MEDIA_CREATION_TIME>("2023-12-19T03:16:00.000Z");
3323 param->Set<Tag::MEDIA_LATITUDE>(22.67f); // 22.67f test latitude
3324 param->Set<Tag::MEDIA_LONGITUDE>(114.06f); // 114.06f test longitude
3325 param->SetData("fast_start", static_cast<int32_t>(1)); // 1 moov 前置
3326 EXPECT_EQ(avmuxer->SetParameter(param), 0);
3327 OHOS::sptr<AVBufferQueueProducer> bqProducer= avmuxer->GetInputBufferQueue(trackId);
3328 ASSERT_NE(bqProducer, nullptr);
3329
3330 ASSERT_EQ(avmuxer->Start(), 0);
3331 std::shared_ptr<Meta> userMeta = std::make_shared<Meta>();
3332 userMeta->SetData("com.openharmony.version", 5); // 5 test version
3333 userMeta->SetData("com.openharmony.model", "LNA-AL00");
3334 userMeta->SetData("com.openharmony.capture.fps", 30.00f); // 30.00f test capture fps
3335 EXPECT_EQ(avmuxer->SetUserMeta(userMeta), 0);
3336
3337 inputFile_ = std::make_shared<std::ifstream>(INPUT_FILE_PATH, std::ios::binary);
3338 int32_t extSize = 0;
3339 inputFile_->read(reinterpret_cast<char*>(&extSize), sizeof(extSize));
3340 if (extSize > 0) {
3341 std::vector<uint8_t> buffer(extSize);
3342 inputFile_->read(reinterpret_cast<char*>(buffer.data()), extSize);
3343 }
3344 bool eosFlag = false;
3345 int32_t ret = 0;
3346 do {
3347 ret = WriteSample(bqProducer, inputFile_, eosFlag);
3348 } while (!eosFlag && (ret == 0));
3349 ASSERT_EQ(ret, 0);
3350 ASSERT_EQ(avmuxer->Stop(), 0);
3351 close(fd);
3352 }
3353 #endif
3354 } // namespace