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_DEMUXER_PLUGIN_H 17 #define HISTREAMER_FFMPEG_DEMUXER_PLUGIN_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 #include "foundation/osal/thread/mutex.h" 23 #include "plugin/interface/demuxer_plugin.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 #include "libavformat/avformat.h" 29 #include "libavcodec/avcodec.h" 30 #ifdef __cplusplus 31 } 32 #endif 33 34 namespace OHOS { 35 namespace Media { 36 namespace Plugin { 37 namespace Ffmpeg { 38 class FFmpegDemuxerPlugin : public DemuxerPlugin { 39 public: 40 explicit FFmpegDemuxerPlugin(std::string name); 41 ~FFmpegDemuxerPlugin() override; 42 43 Status Init() override; 44 Status Deinit() override; 45 Status Prepare() override; 46 Status Reset() override; 47 Status GetParameter(Tag tag, ValueType& value) override; 48 Status SetParameter(Tag tag, const ValueType& value) override; 49 std::shared_ptr<Allocator> GetAllocator() override; 50 Status SetCallback(Callback* cb) override; 51 52 Status SetDataSource(const std::shared_ptr<DataSource>& source) override; 53 Status GetMediaInfo(MediaInfo& mediaInfo) override; 54 size_t GetTrackCount() override; 55 Status SelectTrack(int32_t trackId) override; 56 Status UnselectTrack(int32_t trackId) override; 57 Status GetSelectedTracks(std::vector<int32_t>& trackIds) override; 58 Status ReadFrame(Buffer& info, int32_t timeOutMs) override; 59 Status SeekTo(int32_t trackId, int64_t seekTime, SeekMode mode, int64_t& realSeekTime) override; 60 61 private: 62 class DemuxerPluginAllocator : public Allocator { 63 public: 64 DemuxerPluginAllocator() = default; 65 ~DemuxerPluginAllocator() override = default; 66 void* Alloc(size_t size) override; 67 void Free(void* ptr) override; // NOLINT: void* 68 }; 69 70 struct IOContext { 71 std::shared_ptr<DataSource> dataSource {nullptr}; 72 int64_t offset {0}; 73 bool eos {false}; 74 }; 75 76 void InitAVFormatContext(); 77 78 static std::shared_ptr<AVCodecContext> InitCodecContext(const AVStream& avStream); 79 80 AVIOContext* AllocAVIOContext(int flags); 81 82 bool IsSelectedTrack(int32_t trackId); 83 84 void SaveFileInfoToMetaInfo(Meta &meta); 85 86 bool ParseMediaData(); 87 88 bool ConvertAVPacketToFrameInfo(const AVStream& avStream, AVPacket& pkt, Buffer& frameInfo); 89 90 static int AVReadPacket(void* opaque, uint8_t* buf, int bufSize); // NOLINT: void* 91 92 static int AVWritePacket(void* opaque, uint8_t* buf, int bufSize); // NOLINT: void* 93 94 static int64_t AVSeek(void* opaque, int64_t offset, int whence); // NOLINT: void* 95 96 void InitConvertContext(const AVStream& avStream); 97 98 void ConvertAvcOrHevcToAnnexb(AVPacket& pkt); 99 100 VideoBitStreamFormat vdBitStreamFormat_ {VideoBitStreamFormat::UNKNOWN}; 101 std::shared_ptr<AVBSFContext> avbsfContext_ {nullptr}; 102 double playbackSpeed_ {1.0}; 103 Seekable seekable_; 104 IOContext ioContext_; 105 Callback* callback_ {}; 106 std::shared_ptr<AVInputFormat> pluginImpl_; 107 std::shared_ptr<AVFormatContext> formatContext_; 108 std::shared_ptr<Allocator> allocator_; 109 std::unique_ptr<MediaInfo> mediaInfo_; 110 std::vector<int32_t> selectedTrackIds_; 111 OSAL::Mutex mutex_ {}; 112 }; 113 } // namespace Ffmpeg 114 } // namespace Plugin 115 } // namespace Media 116 } // namespace OHOS 117 118 #endif // HISTREAMER_FFMPEG_DEMUXER_PLUGIN_H 119