1da853ecaSopenharmony_ci/*
2da853ecaSopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3da853ecaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4da853ecaSopenharmony_ci * you may not use this file except in compliance with the License.
5da853ecaSopenharmony_ci * You may obtain a copy of the License at
6da853ecaSopenharmony_ci *
7da853ecaSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8da853ecaSopenharmony_ci *
9da853ecaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10da853ecaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11da853ecaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12da853ecaSopenharmony_ci * See the License for the specific language governing permissions and
13da853ecaSopenharmony_ci * limitations under the License.
14da853ecaSopenharmony_ci */
15da853ecaSopenharmony_ci
16da853ecaSopenharmony_ci#ifndef AVCODEC_DEMUXER_PLUGIN_H
17da853ecaSopenharmony_ci#define AVCODEC_DEMUXER_PLUGIN_H
18da853ecaSopenharmony_ci
19da853ecaSopenharmony_ci#include <memory>
20da853ecaSopenharmony_ci#include <vector>
21da853ecaSopenharmony_ci#include "meta/meta.h"
22da853ecaSopenharmony_ci#include "plugin/plugin_base.h"
23da853ecaSopenharmony_ci#include "plugin/plugin_caps.h"
24da853ecaSopenharmony_ci#include "plugin/plugin_definition.h"
25da853ecaSopenharmony_ci#include "plugin/plugin_info.h"
26da853ecaSopenharmony_ci#include "common/media_core.h"
27da853ecaSopenharmony_ci
28da853ecaSopenharmony_cinamespace OHOS {
29da853ecaSopenharmony_cinamespace Media {
30da853ecaSopenharmony_cinamespace Plugins {
31da853ecaSopenharmony_ci/**
32da853ecaSopenharmony_ci * @brief Demuxer Plugin Interface.
33da853ecaSopenharmony_ci *
34da853ecaSopenharmony_ci * Used for audio and video media file parse.
35da853ecaSopenharmony_ci *
36da853ecaSopenharmony_ci * @since 1.0
37da853ecaSopenharmony_ci * @version 1.0
38da853ecaSopenharmony_ci */
39da853ecaSopenharmony_cistruct DemuxerPlugin : public PluginBase {
40da853ecaSopenharmony_ci    /// constructor
41da853ecaSopenharmony_ci    explicit DemuxerPlugin(std::string name): PluginBase(std::move(name)) {}
42da853ecaSopenharmony_ci    /**
43da853ecaSopenharmony_ci     * @brief Set the data source to demuxer component.
44da853ecaSopenharmony_ci     *
45da853ecaSopenharmony_ci     * The function is valid only in the CREATED state.
46da853ecaSopenharmony_ci     *
47da853ecaSopenharmony_ci     * @param source Data source where data read from.
48da853ecaSopenharmony_ci     * @return  Execution Status return
49da853ecaSopenharmony_ci     *  @retval OK: Plugin SetDataSource succeeded.
50da853ecaSopenharmony_ci     */
51da853ecaSopenharmony_ci    virtual Status SetDataSource(const std::shared_ptr<DataSource>& source) = 0;
52da853ecaSopenharmony_ci
53da853ecaSopenharmony_ci    /**
54da853ecaSopenharmony_ci     * @brief Get the attributes of a media file.
55da853ecaSopenharmony_ci     *
56da853ecaSopenharmony_ci     * The attributes contain file and stream attributes.
57da853ecaSopenharmony_ci     * The function is valid only after INITIALIZED state.
58da853ecaSopenharmony_ci     *
59da853ecaSopenharmony_ci     * @param mediaInfo Indicates the pointer to the source attributes
60da853ecaSopenharmony_ci     * @return  Execution status return
61da853ecaSopenharmony_ci     *  @retval OK: Plugin GetMediaInfo succeeded.
62da853ecaSopenharmony_ci     */
63da853ecaSopenharmony_ci    virtual Status GetMediaInfo(MediaInfo& mediaInfo) = 0;
64da853ecaSopenharmony_ci
65da853ecaSopenharmony_ci    /**
66da853ecaSopenharmony_ci     * @brief Get the user meta of a media file.
67da853ecaSopenharmony_ci     *
68da853ecaSopenharmony_ci     * The attributes contain file and stream attributes.
69da853ecaSopenharmony_ci     * The function is valid only after INITIALIZED state.
70da853ecaSopenharmony_ci     *
71da853ecaSopenharmony_ci     * @param mediaInfo Indicates the pointer to the user meta attributes
72da853ecaSopenharmony_ci     * @return  Execution status return
73da853ecaSopenharmony_ci     *  @retval OK: Plugin GetUserMeta succeeded.
74da853ecaSopenharmony_ci     */
75da853ecaSopenharmony_ci    virtual Status GetUserMeta(std::shared_ptr<Meta> meta) = 0;
76da853ecaSopenharmony_ci
77da853ecaSopenharmony_ci    /**
78da853ecaSopenharmony_ci     * @brief Select a specified media track.
79da853ecaSopenharmony_ci     *
80da853ecaSopenharmony_ci     * The function is valid only after RUNNING state.
81da853ecaSopenharmony_ci     *
82da853ecaSopenharmony_ci     * @param trackId Identifies the media track. If an invalid value is passed, the default media track specified.
83da853ecaSopenharmony_ci     * @return  Execution Status return
84da853ecaSopenharmony_ci     *  @retval OK: Plugin SelectTrack succeeded.
85da853ecaSopenharmony_ci     */
86da853ecaSopenharmony_ci    virtual Status SelectTrack(uint32_t trackId) = 0;
87da853ecaSopenharmony_ci
88da853ecaSopenharmony_ci    /**
89da853ecaSopenharmony_ci     * @brief Unselect a specified media track from which the demuxer reads data frames.
90da853ecaSopenharmony_ci     *
91da853ecaSopenharmony_ci     * The function is valid only after RUNNING state.
92da853ecaSopenharmony_ci     *
93da853ecaSopenharmony_ci     * @param trackId Identifies the media track. ignore the invalid value is passed.
94da853ecaSopenharmony_ci     * @return  Execution Status return
95da853ecaSopenharmony_ci     *  @retval OK: Plugin UnselectTrack succeeded.
96da853ecaSopenharmony_ci     */
97da853ecaSopenharmony_ci    virtual Status UnselectTrack(uint32_t trackId) = 0;
98da853ecaSopenharmony_ci
99da853ecaSopenharmony_ci    /**
100da853ecaSopenharmony_ci     * @brief Reads data frames.
101da853ecaSopenharmony_ci     *
102da853ecaSopenharmony_ci     * The function is valid only after RUNNING state.
103da853ecaSopenharmony_ci     *
104da853ecaSopenharmony_ci     * @param trackId Identifies the media track. ignore the invalid value is passed.
105da853ecaSopenharmony_ci     * @param sample Buffer where store data frames.
106da853ecaSopenharmony_ci     * @return  Execution Status return
107da853ecaSopenharmony_ci     *  @retval OK: Plugin ReadFrame succeeded.
108da853ecaSopenharmony_ci     *  @retval ERROR_TIMED_OUT: Operation timeout.
109da853ecaSopenharmony_ci     */
110da853ecaSopenharmony_ci    virtual Status ReadSample(uint32_t trackId, std::shared_ptr<AVBuffer> sample) = 0;
111da853ecaSopenharmony_ci
112da853ecaSopenharmony_ci    /**
113da853ecaSopenharmony_ci     * @brief Get next sample size.
114da853ecaSopenharmony_ci     *
115da853ecaSopenharmony_ci     * The function is valid only after RUNNING state.
116da853ecaSopenharmony_ci     *
117da853ecaSopenharmony_ci     * @param trackId Identifies the media track. ignore the invalid value is passed.
118da853ecaSopenharmony_ci     * @return Execution Status
119da853ecaSopenharmony_ci     */
120da853ecaSopenharmony_ci    virtual Status GetNextSampleSize(uint32_t trackId, int32_t& size) = 0;
121da853ecaSopenharmony_ci
122da853ecaSopenharmony_ci    /**
123da853ecaSopenharmony_ci     * @brief Seeks for a specified position for the demuxer.
124da853ecaSopenharmony_ci     *
125da853ecaSopenharmony_ci     * After being started, the demuxer seeks for a specified position to read data frames.
126da853ecaSopenharmony_ci     *
127da853ecaSopenharmony_ci     * The function is valid only after RUNNING state.
128da853ecaSopenharmony_ci     *
129da853ecaSopenharmony_ci     * @param trackId Identifies the stream in the media file.
130da853ecaSopenharmony_ci     * @param seekTime Indicates the target position, based on {@link HST_TIME_BASE} .
131da853ecaSopenharmony_ci     * @param mode Indicates the seek mode.
132da853ecaSopenharmony_ci     * @param realSeekTime Indicates the accurate target position, based on {@link HST_TIME_BASE} .
133da853ecaSopenharmony_ci     * @return  Execution Status return
134da853ecaSopenharmony_ci     *  @retval OK: Plugin SeekTo succeeded.
135da853ecaSopenharmony_ci     *  @retval ERROR_INVALID_DATA: The input data is invalid.
136da853ecaSopenharmony_ci     */
137da853ecaSopenharmony_ci    virtual Status SeekTo(int32_t trackId, int64_t seekTime, SeekMode mode, int64_t& realSeekTime) = 0;
138da853ecaSopenharmony_ci
139da853ecaSopenharmony_ci    virtual Status Reset() = 0;
140da853ecaSopenharmony_ci
141da853ecaSopenharmony_ci    virtual Status Start() = 0;
142da853ecaSopenharmony_ci
143da853ecaSopenharmony_ci    virtual Status Stop() = 0;
144da853ecaSopenharmony_ci
145da853ecaSopenharmony_ci    virtual Status Flush() = 0;
146da853ecaSopenharmony_ci
147da853ecaSopenharmony_ci    virtual void ResetEosStatus() = 0;
148da853ecaSopenharmony_ci
149da853ecaSopenharmony_ci    virtual Status ParserRefUpdatePos(int64_t timeStampMs, bool isForward = true) = 0;
150da853ecaSopenharmony_ci    virtual Status ParserRefInfo() = 0;
151da853ecaSopenharmony_ci    virtual Status GetFrameLayerInfo(std::shared_ptr<AVBuffer> videoSample, FrameLayerInfo &frameLayerInfo) = 0;
152da853ecaSopenharmony_ci    virtual Status GetFrameLayerInfo(uint32_t frameId, FrameLayerInfo &frameLayerInfo) = 0;
153da853ecaSopenharmony_ci    virtual Status GetGopLayerInfo(uint32_t gopId, GopLayerInfo &gopLayerInfo) = 0;
154da853ecaSopenharmony_ci    virtual Status GetIFramePos(std::vector<uint32_t> &IFramePos) = 0;
155da853ecaSopenharmony_ci    virtual Status Dts2FrameId(int64_t dts, uint32_t &frameId, bool offset = true) = 0;
156da853ecaSopenharmony_ci
157da853ecaSopenharmony_ci    virtual Status GetDrmInfo(std::multimap<std::string, std::vector<uint8_t>>& drmInfo)
158da853ecaSopenharmony_ci    {
159da853ecaSopenharmony_ci        (void)drmInfo;
160da853ecaSopenharmony_ci        return Status::OK;
161da853ecaSopenharmony_ci    }
162da853ecaSopenharmony_ci
163da853ecaSopenharmony_ci    virtual Status GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,
164da853ecaSopenharmony_ci        const uint64_t relativePresentationTimeUs, uint32_t &index) = 0;
165da853ecaSopenharmony_ci
166da853ecaSopenharmony_ci    virtual Status GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,
167da853ecaSopenharmony_ci        const uint32_t index, uint64_t &relativePresentationTimeUs) = 0;
168da853ecaSopenharmony_ci
169da853ecaSopenharmony_ci    virtual void SetCacheLimit(uint32_t limitSize) = 0;
170da853ecaSopenharmony_ci};
171da853ecaSopenharmony_ci
172da853ecaSopenharmony_ci/// Demuxer plugin api major number.
173da853ecaSopenharmony_ci#define DEMUXER_API_VERSION_MAJOR (1)
174da853ecaSopenharmony_ci
175da853ecaSopenharmony_ci/// Demuxer plugin api minor number
176da853ecaSopenharmony_ci#define DEMUXER_API_VERSION_MINOR (0)
177da853ecaSopenharmony_ci
178da853ecaSopenharmony_ci/// Demuxer plugin version
179da853ecaSopenharmony_ci#define DEMUXER_API_VERSION MAKE_VERSION(DEMUXER_API_VERSION_MAJOR, DEMUXER_API_VERSION_MINOR)
180da853ecaSopenharmony_ci
181da853ecaSopenharmony_ci/**
182da853ecaSopenharmony_ci * @brief Describes the demuxer plugin information.
183da853ecaSopenharmony_ci *
184da853ecaSopenharmony_ci * @since 1.0
185da853ecaSopenharmony_ci * @version 1.0
186da853ecaSopenharmony_ci */
187da853ecaSopenharmony_cistruct DemuxerPluginDef : public PluginDefBase {
188da853ecaSopenharmony_ci    DemuxerPluginDef()
189da853ecaSopenharmony_ci        : PluginDefBase()
190da853ecaSopenharmony_ci    {
191da853ecaSopenharmony_ci        apiVersion = DEMUXER_API_VERSION; ///< Demuxer plugin version.
192da853ecaSopenharmony_ci        pluginType = PluginType::DEMUXER; ///< Plugin type, MUST be DEMUXER.
193da853ecaSopenharmony_ci    }
194da853ecaSopenharmony_ci};
195da853ecaSopenharmony_ci} // namespace Plugins
196da853ecaSopenharmony_ci} // namespace Media
197da853ecaSopenharmony_ci} // namespace OHOS
198da853ecaSopenharmony_ci#endif // AVCODEC_DEMUXER_PLUGIN_H
199