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