1 /* 2 * Copyright (c) 2021-2021 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 HISTREAMER_FFMPEG_MUXER_PLUGIN_H 17 #define HISTREAMER_FFMPEG_MUXER_PLUGIN_H 18 19 #include "plugin/interface/muxer_plugin.h" 20 #include "foundation/osal/thread/mutex.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 #include "libavformat/avformat.h" 26 #include "libavutil/opt.h" 27 #ifdef __cplusplus 28 } 29 #endif 30 31 namespace OHOS { 32 namespace Media { 33 namespace Plugin { 34 namespace Ffmpeg { 35 class FFmpegMuxerPlugin : public MuxerPlugin { 36 public: 37 explicit FFmpegMuxerPlugin(std::string name); 38 39 ~FFmpegMuxerPlugin() override; 40 41 Status Init() override; 42 43 Status Deinit() override; 44 45 std::shared_ptr<Allocator> GetAllocator() override; 46 47 Status SetCallback(Callback* cb) override; 48 49 Status GetParameter(Tag tag, ValueType& value) override; 50 51 Status SetParameter(Tag tag, const ValueType& value) override; 52 53 Status Prepare() override; 54 55 Status Reset() override; 56 57 Status AddTrack(uint32_t& trackId) override; 58 59 Status SetTrackParameter(uint32_t trackId, Tag tag, const ValueType& value) override; 60 61 Status GetTrackParameter(uint32_t trackId, Tag tag, ValueType& value) override; 62 63 Status SetDataSink(const std::shared_ptr<DataSink>& dataSink) override; 64 65 Status WriteHeader() override; 66 67 Status WriteFrame(const std::shared_ptr<Buffer>& buffer) override; 68 69 Status WriteTrailer() override; 70 71 private: 72 struct IOContext { 73 std::shared_ptr<DataSink> dataSink_ {}; 74 int64_t pos_ {0}; 75 int64_t end_ {0}; 76 }; 77 78 static int32_t IoRead(void* opaque, uint8_t* buf, int bufSize); 79 80 static int32_t IoWrite(void* opaque, uint8_t* buf, int bufSize); 81 82 static int64_t IoSeek(void* opaque, int64_t offset, int whence); 83 84 AVIOContext* InitAvIoCtx(); 85 86 static void DeInitAvIoCtx(AVIOContext* ptr); 87 88 static void ResetIoCtx(IOContext& ioContext); 89 90 Status InitFormatCtxLocked(); 91 92 Status Release(); 93 94 std::shared_ptr<AVOutputFormat> outputFormat_{}; 95 96 std::map<uint32_t, TagMap> trackParameters_{}; 97 98 TagMap generalParameters_{}; 99 100 OSAL::Mutex fmtMutex_{}; 101 std::shared_ptr<AVFormatContext> formatContext_{}; 102 103 std::shared_ptr<AVPacket> cachePacket_ {}; 104 105 IOContext ioContext_; 106 }; 107 } // Ffmpeg 108 } // Plugin 109 } // Media 110 } // OHOS 111 #endif // HISTREAMER_FFMPEG_MUXER_PLUGIN_H 112