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_ffmpeg_demo.h"
17 #include <dlfcn.h>
18 #include <iostream>
19 #include <fcntl.h>
20 #include <fstream>
21
22 namespace {
23 const char *FFMPEG_REGISTER_FUNC_NAME = "register_FFmpegMuxer";
24 const char *FFMPEG_UNREGISTER_FUNC_NAME = "unregister_FFmpegMuxer";
25 const char *FFMPEG_LIB_PATH = "/libav_codec_plugin_FFmpegMuxer.z.so";
26 }
27
28 namespace OHOS {
29 namespace MediaAVCodec {
30 namespace Plugin {
AddPlugin(const PluginDefBase & def)31 Status AVMuxerFFmpegDemo::FfmpegRegister::AddPlugin(const PluginDefBase& def)
32 {
33 auto& tempDef = (MuxerPluginDef&)def;
34 std::cout<<"find plugin apiVersion:"<<tempDef.apiVersion;
35 std::cout<<" |pluginType:"<<static_cast<int32_t>(tempDef.pluginType);
36 std::cout<<" |name:"<<tempDef.name;
37 std::cout<<" |description:"<<tempDef.description;
38 std::cout<<" |rank:"<<tempDef.rank;
39 std::cout<<" |sniffer:"<<tempDef.sniffer;
40 std::cout<<" |creator:"<<tempDef.creator;
41 std::cout<<std::endl;
42 plugins.push_back(tempDef);
43 return Status::NO_ERROR;
44 }
45
AVMuxerFFmpegDemo()46 AVMuxerFFmpegDemo::AVMuxerFFmpegDemo() : register_(std::make_shared<FfmpegRegister>())
47 {
48 }
49
DoAddTrack(int32_t & trackIndex,MediaDescription & trackDesc)50 int AVMuxerFFmpegDemo::DoAddTrack(int32_t &trackIndex, MediaDescription &trackDesc)
51 {
52 int32_t tempTrackId = 0;
53 ffmpegMuxer_->AddTrack(tempTrackId, trackDesc);
54 if (tempTrackId < 0) {
55 std::cout<<"AVMuxerFFmpegDemo::DoAddTrack failed! trackId:"<<tempTrackId<<std::endl;
56 return -1;
57 }
58 trackIndex = tempTrackId;
59 return 0;
60 }
61
DoRunMuxer()62 void AVMuxerFFmpegDemo::DoRunMuxer()
63 {
64 long long testTimeStart = GetTimestamp();
65 GetFfmpegRegister();
66 if (register_->plugins.size() == 0) {
67 std::cout<<"regist muxers failed!"<<std::endl;
68 return;
69 }
70
71 int32_t maxProb = 0;
72 MuxerPluginDef pluginDef {};
73 for (auto& plugin : register_->plugins) {
74 if (plugin.pluginType == PluginType::MUXER) {
75 auto prob = plugin.sniffer(plugin.name, outputFormat_);
76 if (prob > maxProb) {
77 maxProb = prob;
78 pluginDef = plugin;
79 }
80 }
81 }
82
83 if (pluginDef.creator == nullptr) {
84 std::cout<<"no plugins matching output format - "<< outputFormat_ <<std::endl;
85 return;
86 }
87
88 std::string outFileName = "ffmpeg_mux_" + audioType_ + "_" + videoType_ + "_" + coverType_ + "." + format_;
89 outFd_ = open(outFileName.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
90 if (outFd_ < 0) {
91 std::cout<<"create muxer output file failed! fd:"<<outFd_<<std::endl;
92 return;
93 }
94 std::cout<<"==== open success! =====\noutputFileName: "<<outFileName<<"\n============"<<std::endl;
95
96 ffmpegMuxer_ = pluginDef.creator(pluginDef.name, outFd_);
97 if (ffmpegMuxer_ == nullptr) {
98 std::cout<<"ffmpegMuxer create failed!"<<std::endl;
99 return;
100 }
101
102 ffmpegMuxer_->SetRotation(0);
103
104 AddAudioTrack(audioParams_);
105 AddVideoTrack(videoParams_);
106 AddCoverTrack(coverParams_);
107
108 ffmpegMuxer_->Start();
109 WriteCoverSample();
110 WriteTrackSample();
111 ffmpegMuxer_->Stop();
112 long long testTimeEnd = GetTimestamp();
113 std::cout << "muxer used time: " << testTimeEnd - testTimeStart << "us" << std::endl;
114 }
115
DoRunMultiThreadCase()116 void AVMuxerFFmpegDemo::DoRunMultiThreadCase()
117 {
118 std::cout<<"ffmpeg plugin demo is not support multi-thread write!"<<std::endl;
119 return;
120 }
121
DoWriteSample(uint32_t trackIndex,std::shared_ptr<AVSharedMemory> sample,AVCodecBufferInfo info,AVCodecBufferFlag flag)122 int AVMuxerFFmpegDemo::DoWriteSample(uint32_t trackIndex, std::shared_ptr<AVSharedMemory> sample,
123 AVCodecBufferInfo info, AVCodecBufferFlag flag)
124 {
125 if (ffmpegMuxer_ != nullptr &&
126 ffmpegMuxer_->WriteSample(trackIndex, sample->GetBase(), info, flag) == Status::NO_ERROR) {
127 return 0;
128 }
129 return -1;
130 }
131
GetFfmpegRegister()132 int AVMuxerFFmpegDemo::GetFfmpegRegister()
133 {
134 std::string libPath = AV_CODEC_PLUGIN_PATH;
135 libPath += FFMPEG_LIB_PATH;
136 dlHandle_ = ::dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL);
137 if (dlHandle_ == nullptr) {
138 std::cout<<"AVMuxerFFmpegDemo::GetFfmpegRegister dlHandle_ is nullptr!"<<std::endl;
139 return -1;
140 }
141
142 registerFunc_ = (RegisterFunc)(::dlsym(dlHandle_, FFMPEG_REGISTER_FUNC_NAME));
143 unregisterFunc_ = (UnregisterFunc)(::dlsym(dlHandle_, FFMPEG_UNREGISTER_FUNC_NAME));
144 if (registerFunc_ == nullptr || unregisterFunc_ == nullptr) {
145 std::cout<<"get dl function failed! registerFunc_:"<<(void*)registerFunc_;
146 return -1;
147 }
148
149 if (registerFunc_(register_) != Status::NO_ERROR) {
150 std::cout<<"ffmpeg register failed!"<<std::endl;
151 return -1;
152 }
153 return 0;
154 }
155 } // Plugin
156 } // namespace MediaAVCodec
157 } // namespace OHOS