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