1 /* 2 * Copyright (C) 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 AVCODEC_AUDIO_ENCODER_H 17 #define AVCODEC_AUDIO_ENCODER_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 Media { 26 class AVCodecAudioEncoder { 27 public: 28 virtual ~AVCodecAudioEncoder() = default; 29 30 /** 31 * @brief Configure the encoder. 32 * 33 * @param format The format of the input data and the desired format of the output data. 34 * @return Returns {@link MSERR_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 MSERR_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 MSERR_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 MSERR_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 encoder. 73 * 74 * @return Returns {@link MSERR_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 encoder to the initial state. 82 * 83 * @return Returns {@link MSERR_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 encoder resources. All methods are unavailable after calling this. 91 * 92 * @return Returns {@link MSERR_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 Returns a {@link AVSharedMemory} object for a input buffer index that contains the data. 100 * 101 * This function must be called during running 102 * 103 * @param index The index of the input buffer. 104 * @return Returns {@link AVSharedMemory} if success; returns nullptr otherwise. 105 * @since 3.1 106 * @version 3.1 107 */ 108 virtual std::shared_ptr<AVSharedMemory> GetInputBuffer(uint32_t index) = 0; 109 110 /** 111 * @brief Submits input buffer to encoder. 112 * 113 * This function must be called during running 114 * 115 * @param index The index of the input buffer. 116 * @param info The info of the input buffer. For details, see {@link AVCodecBufferInfo} 117 * @param flag The flag of the input buffer. For details, see {@link AVCodecBufferFlag} 118 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 119 * @since 3.1 120 * @version 3.1 121 */ 122 virtual int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) = 0; 123 124 /** 125 * @brief Returns a {@link AVSharedMemory} object for a output buffer index that contains the data. 126 * 127 * This function must be called during running 128 * 129 * @param index The index of the output buffer. 130 * @return Returns {@link AVSharedMemory} if success; returns nullptr otherwise. 131 * @since 3.1 132 * @version 3.1 133 */ 134 virtual std::shared_ptr<AVSharedMemory> GetOutputBuffer(uint32_t index) = 0; 135 136 /** 137 * @brief Gets the format of the output data. 138 * 139 * This function must be called after {@link Configure} 140 * 141 * @param format 142 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 143 * @since 3.1 144 * @version 3.1 145 */ 146 virtual int32_t GetOutputFormat(Format &format) = 0; 147 148 /** 149 * @brief Returns the output buffer to the encoder. 150 * 151 * This function must be called during running 152 * 153 * @param index The index of the output buffer. 154 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 155 * @since 3.1 156 * @version 3.1 157 */ 158 virtual int32_t ReleaseOutputBuffer(uint32_t index) = 0; 159 160 /** 161 * @brief Sets the parameters to the encoder. 162 * 163 * This function must be called after {@link Configure} 164 * 165 * @param format The parameters. 166 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 167 * @since 3.1 168 * @version 3.1 169 */ 170 virtual int32_t SetParameter(const Format &format) = 0; 171 172 /** 173 * @brief Registers a encoder listener. 174 * 175 * This function must be called before {@link Configure} 176 * 177 * @param callback Indicates the encoder listener to register. For details, see {@link AVCodecCallback}. 178 * @return Returns {@link MSERR_OK} if success; returns an error code otherwise. 179 * @since 3.1 180 * @version 3.1 181 */ 182 virtual int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) = 0; 183 }; 184 185 class __attribute__((visibility("default"))) AudioEncoderFactory { 186 public: 187 #ifdef UNSUPPORT_CODEC CreateByMime(const std::string & mime)188 static std::shared_ptr<AVCodecAudioEncoder> CreateByMime(const std::string &mime) 189 { 190 (void)mime; 191 return nullptr; 192 } 193 CreateByName(const std::string & name)194 static std::shared_ptr<AVCodecAudioEncoder> CreateByName(const std::string &name) 195 { 196 (void)name; 197 return nullptr; 198 } 199 #else 200 /** 201 * @brief Instantiate the preferred encoder of the given mime type. 202 * 203 * @param mime The mime type. 204 * @return Returns the preferred encoder. 205 * @since 3.1 206 * @version 3.1 207 */ 208 static std::shared_ptr<AVCodecAudioEncoder> CreateByMime(const std::string &mime); 209 210 /** 211 * @brief Instantiates the designated encoder. 212 * 213 * @param name The encoder's name. 214 * @return Returns the designated encoder. 215 * @since 3.1 216 * @version 3.1 217 */ 218 static std::shared_ptr<AVCodecAudioEncoder> CreateByName(const std::string &name); 219 #endif 220 private: 221 AudioEncoderFactory() = default; 222 ~AudioEncoderFactory() = default; 223 }; 224 } // namespace Media 225 } // namespace OHOS 226 #endif // AVCODEC_AUDIO_ENCODER_H