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