1 /*
2  * Copyright (C) 2023 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 MEDIA_AVCODEC_AUDIO_DECODER_H
17 #define MEDIA_AVCODEC_AUDIO_DECODER_H
18 
19 #include "avcodec_common.h"
20 #include "avcodec_info.h"
21 #include "buffer/avsharedmemory.h"
22 #include "meta/format.h"
23 
24 namespace OHOS {
25 namespace MediaAVCodec {
26 class AVCodecAudioDecoder {
27 public:
28     virtual ~AVCodecAudioDecoder() = default;
29 
30     /**
31      * @brief Configure the decoder.
32      *
33      * @param format The format of the input data and the desired format of the output data.
34      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
35      * @since 3.1
36      * @version 3.1
37      */
38     virtual int32_t Configure(const Format &format) = 0;
39 
40     /**
41      * @brief Prepare for decoding.
42      *
43      * This function must be called after {@link Configure} and before {@link Start}
44      *
45      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
46      * @since 3.1
47      * @version 3.1
48      */
49     virtual int32_t Prepare() = 0;
50 
51     /**
52      * @brief Start decoding.
53      *
54      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
55      * @since 3.1
56      * @version 3.1
57      */
58     virtual int32_t Start() = 0;
59 
60     /**
61      * @brief Stop decoding.
62      *
63      * This function must be called during running
64      *
65      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
66      * @since 3.1
67      * @version 3.1
68      */
69     virtual int32_t Stop() = 0;
70 
71     /**
72      * @brief Flush both input and output buffers of the decoder.
73      *
74      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
75      * @since 3.1
76      * @version 3.1
77      */
78     virtual int32_t Flush() = 0;
79 
80     /**
81      * @brief Restores the decoder to the initial state.
82      *
83      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
84      * @since 3.1
85      * @version 3.1
86      */
87     virtual int32_t Reset() = 0;
88 
89     /**
90      * @brief Releases decoder resources. All methods are unavailable after calling this.
91      *
92      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
93      * @since 3.1
94      * @version 3.1
95      */
96     virtual int32_t Release() = 0;
97 
98     /**
99      * @brief Submits input buffer to decoder.
100      *
101      * This function must be called during running
102      *
103      * @param index The index of the input buffer.
104      * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo}
105      * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag}
106      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
107      * @since 3.1
108      * @version 3.1
109      */
110     virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0;
111 
112     /**
113      * @brief Gets the format of the output data.
114      *
115      * This function must be called after {@link Configure}
116      *
117      * @param format
118      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
119      * @since 3.1
120      * @version 3.1
121      */
122     virtual int32_t GetOutputFormat(Format &format) = 0;
123 
124     /**
125      * @brief Returns the output buffer to the decoder.
126      *
127      * This function must be called during running
128      *
129      * @param index The index of the output buffer.
130      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
131      * @since 3.1
132      * @version 3.1
133      */
134     virtual int32_t ReleaseOutputBuffer(uint32_t index) = 0;
135 
136     /**
137      * @brief Sets the parameters to the decoder.
138      *
139      * This function must be called after {@link Configure}
140      *
141      * @param format The parameters.
142      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
143      * @since 3.1
144      * @version 3.1
145      */
146     virtual int32_t SetParameter(const Format &format) = 0;
147 
148     /**
149      * @brief Registers a decoder listener.
150      *
151      * This function must be called before {@link Configure}
152      *
153      * @param callback Indicates the decoder listener to register. For details, see {@link AVCodecCallback}.
154      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
155      * @since 3.1
156      * @version 3.1
157      */
158     virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0;
159 };
160 
161 class __attribute__((visibility("default"))) AudioDecoderFactory {
162 public:
163 #ifdef UNSUPPORT_CODEC
CreateByMime(const std::string & mime)164     static std::shared_ptr<AVCodecAudioDecoder> CreateByMime(const std::string &mime)
165     {
166         (void)mime;
167         return nullptr;
168     }
169 
CreateByName(const std::string & name)170     static std::shared_ptr<AVCodecAudioDecoder> CreateByName(const std::string &name)
171     {
172         (void)name;
173         return nullptr;
174     }
175 #else
176     /**
177      * @brief Instantiate the preferred decoder of the given mime type.
178      *
179      * @param mime The mime type.
180      * @return Returns the preferred decoder.
181      * @since 3.1
182      * @version 3.1
183      */
184     static std::shared_ptr<AVCodecAudioDecoder> CreateByMime(const std::string &mime);
185 
186     /**
187      * @brief Instantiates the designated decoder.
188      *
189      * @param name The decoder's name.
190      * @return Returns the designated decoder.
191      * @since 3.1
192      * @version 3.1
193      */
194     static std::shared_ptr<AVCodecAudioDecoder> CreateByName(const std::string &name);
195 #endif
196 private:
197     AudioDecoderFactory() = default;
198     ~AudioDecoderFactory() = default;
199 };
200 } // namespace MediaAVCodec
201 } // namespace OHOS
202 #endif // MEDIA_AVCODEC_AUDIO_DECODER_H