• 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_demo.h"
17 #include <iostream>
18 #include <fstream>
19 #include <cstdio>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <thread>
23 #include <vector>
24 #include "avcodec_errors.h"
25 
26 namespace OHOS {
27 namespace MediaAVCodec {
DoWriteSample(uint32_t trackIndex,std::shared_ptr<AVSharedMemory> sample,AVCodecBufferInfo info,AVCodecBufferFlag flag)28 int AVMuxerDemo::DoWriteSample(uint32_t trackIndex, std::shared_ptr<AVSharedMemory> sample,
29     AVCodecBufferInfo info, AVCodecBufferFlag flag)
30 {
31     if (avmuxer_ != nullptr &&
32         avmuxer_->WriteSample(trackIndex, sample, info, flag) == AVCS_ERR_OK) {
33             return 0;
34     }
35     return -1;
36 }
37 
DoAddTrack(int32_t & trackIndex,MediaDescription & trackDesc)38 int AVMuxerDemo::DoAddTrack(int32_t &trackIndex, MediaDescription &trackDesc)
39 {
40     int ret;
41     if ((ret = avmuxer_->AddTrack(trackIndex, trackDesc)) != AVCS_ERR_OK) {
42         std::cout<<"AVMuxerDemo::DoAddTrack failed! ret:"<<ret<<std::endl;
43         return -1;
44     }
45     return 0;
46 }
47 
DoRunMuxer(const std::string & runMode)48 void AVMuxerDemo::DoRunMuxer(const std::string &runMode)
49 {
50     std::string outFileName = "inner_mux_" + runMode + "_" + audioType_ +
51         "_" + videoType_ + "_" + coverType_ + "." + format_;
52     outFd_ = open(outFileName.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
53     if (outFd_ < 0) {
54         std::cout << "Open file failed! filePath is: " << outFileName << std::endl;
55         return;
56     }
57     std::cout<<"==== open success! =====\noutputFileName: "<<outFileName<<"\n============"<<std::endl;
58     long long testTimeStart = GetTimestamp();
59     avmuxer_ = AVMuxerFactory::CreateAVMuxer(outFd_, outputFormat_);
60     if (avmuxer_ == nullptr) {
61         std::cout << "avmuxer_ is null" << std::endl;
62         return;
63     }
64     std::cout << "create muxer success " << avmuxer_ << std::endl;
65 
66     if (avmuxer_->SetRotation(0) != AVCS_ERR_OK) {
67         std::cout<<"set failed!"<<std::endl;
68         return;
69     }
70 
71     AddAudioTrack(audioParams_);
72     AddVideoTrack(videoParams_);
73     AddCoverTrack(coverParams_);
74 
75     std::cout << "add track success" << std::endl;
76 
77     if (avmuxer_->Start() != AVCS_ERR_OK) {
78         return;
79     }
80 
81     std::cout << "start muxer success" << std::endl;
82 
83     WriteCoverSample();
84 
85     std::cout<<"AVMuxerDemo::DoRunMuxer runMode is : "<<runMode<<std::endl;
86     if (runMode.compare(RUN_NORMAL) == 0) {
87         WriteTrackSample();
88     } else if (runMode.compare(RUN_MUL_THREAD) == 0) {
89         std::vector<std::thread> vecThread;
90         vecThread.emplace_back(MulThdWriteTrackSample, this, audioTrackId_, audioFile_);
91         vecThread.emplace_back(MulThdWriteTrackSample, this, videoTrackId_, videoFile_);
92         for (uint32_t i = 0; i < vecThread.size(); ++i) {
93             vecThread[i].join();
94         }
95     }
96 
97     std::cout << "write muxer success" << std::endl;
98 
99     if (avmuxer_->Stop() != AVCS_ERR_OK) {
100         return;
101     }
102     std::cout << "stop muxer success" << std::endl;
103     long long testTimeEnd = GetTimestamp();
104     std::cout << "muxer used time: " << testTimeEnd - testTimeStart << "us" << std::endl;
105 }
106 
DoRunMuxer()107 void AVMuxerDemo::DoRunMuxer()
108 {
109     DoRunMuxer(std::string(RUN_NORMAL));
110 }
111 
DoRunMultiThreadCase()112 void AVMuxerDemo::DoRunMultiThreadCase()
113 {
114     DoRunMuxer(std::string(RUN_MUL_THREAD));
115 }
116 }  // namespace MediaAVCodec
117 }  // namespace OHOS