• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #ifndef AVMUXER_DEMO_COMMON_H
17 #define AVMUXER_DEMO_COMMON_H
18 
19 #include <iostream>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include "avmuxer.h"
23 #include "nocopyable.h"
24 #include "native_avmuxer.h"
25 #include "plugin_definition.h"
26 #include "ffmpeg_muxer_plugin.h"
27 #include "avsharedmemory.h"
28 #include "avsharedmemorybase.h"
29 #include "avcodec_errors.h"
30 #include "libavformat/avformat.h"
31 #include "libavutil/opt.h"
32 
33 namespace OHOS {
34     namespace MediaAVCodec {
35         // only for demo
36         typedef struct AudioTrackParam {
37             const char* mimeType;
38             long long bitRate;
39             int sampleFormat;
40             int sampleRate;
41             int channels;
42             long long channelMask;
43             int samplePerFrame;
44         } AudioTrackParam;
45 
46         typedef struct VideoTrackParam {
47             const char* mimeType;
48             long long bitRate;
49             int pixelFormat;
50             int width;
51             int height;
52         } VideoTrackParam;
53 
54         class AVMuxerDemo : public NoCopyable {
55         public:
56             AVMuxerDemo() = default;
57             ~AVMuxerDemo() = default;
58 
59             int32_t getFdByMode(OH_AVOutputFormat format);
60             int32_t getErrorFd();
61             int32_t getFdByName(OH_AVOutputFormat format, std::string fileName);
62             int32_t FFmpeggetFdByMode(OutputFormat format);
63             int32_t FFmpeggetFdByName(OutputFormat format, std::string fileName);
64             int32_t InnergetFdByMode(OutputFormat format);
65             int32_t InnergetFdByName(OutputFormat format, std::string fileName);
66             // native api
67             OH_AVMuxer* NativeCreate(int32_t fd, OH_AVOutputFormat format);
68             OH_AVErrCode NativeSetRotation(OH_AVMuxer* muxer, int32_t rotation);
69             OH_AVErrCode NativeAddTrack(OH_AVMuxer* muxer, int32_t* trackIndex, OH_AVFormat* trackFormat);
70             OH_AVErrCode NativeStart(OH_AVMuxer* muxer);
71             OH_AVErrCode NativeWriteSampleBuffer(OH_AVMuxer* muxer,
72             uint32_t trackIndex, OH_AVMemory* sample, OH_AVCodecBufferAttr info);
73             OH_AVErrCode NativeStop(OH_AVMuxer* muxer);
74             OH_AVErrCode NativeDestroy(OH_AVMuxer* muxer);
75 
76             // FFmpeg plugin api
77             void FFmpegCreate(int32_t fd);
78             Plugin::Status FFmpegSetRotation(int32_t rotation);
79             Plugin::Status FFmpegAddTrack(int32_t& trackIndex, const MediaDescription& trackDesc);
80             Plugin::Status FFmpegStart();
81             Plugin::Status FFmpegWriteSample(uint32_t trackIndex, const uint8_t *sample,
82             AVCodecBufferInfo info, AVCodecBufferFlag flag);
83             Plugin::Status FFmpegStop();
84             Plugin::Status FFmpegDestroy();
85 
86             // Inner api
87             int32_t InnerCreate(int32_t fd, OutputFormat format);
88             int32_t InnerSetRotation(int32_t rotation);
89             int32_t InnerAddTrack(int32_t& trackIndex, const MediaDescription& trackDesc);
90             int32_t InnerStart();
91             int32_t InnerWriteSample(uint32_t trackIndex,
92             std::shared_ptr<AVSharedMemory> sample, AVCodecBufferInfo info, AVCodecBufferFlag flag);
93             int32_t InnerStop();
94             int32_t InnerDestroy();
95         private:
96             struct FfmpegRegister : Plugin::PackageRegister {
97                 Plugin::Status AddPlugin(const Plugin::PluginDefBase& def) override;
AddPackageFfmpegRegister98                 Plugin::Status AddPackage(const Plugin::PackageDef& def) override
99                 {
100                     (void)def;
101                     return Plugin::Status::OK;
102                 };
103                 Plugin::MuxerPluginDef pluginDef {};
104             };
105 
106             std::string filename = "";
107             std::map<std::string, std::shared_ptr<AVOutputFormat>> pluginOutputFmt_;
108             std::shared_ptr<Plugin::MuxerPlugin> ffmpegMuxer_ {nullptr};
109             std::shared_ptr<FfmpegRegister> register_ {nullptr};
110 
111             std::shared_ptr<AVMuxer> avmuxer_;
112         };
113     }
114 }
115 #endif // AVMUXER_DEMO_COMMON_H