1 /* 2 * Copyright (C) 2022 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 NATIVE_AVCODEC_AUDIOENCODER_H 17 #define NATIVE_AVCODEC_AUDIOENCODER_H 18 19 #include <stdint.h> 20 #include <stdio.h> 21 #include "native_averrors.h" 22 #include "native_avformat.h" 23 #include "native_avmemory.h" 24 #include "native_avcodec_base.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 /** 31 * @brief Creates an audio encoder instance from the mime type, this interface is recommended in most cases. 32 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 33 * @param mime mime type description string, refer to {@link AVCODEC_MIME_TYPE} 34 * @return Returns a Pointer to an OH_AVCodec instance 35 * @since 9 36 * @version 1.0 37 */ 38 OH_AVCodec *OH_AudioEncoder_CreateByMime(const char *mime); 39 40 /** 41 * @brief Create an audio encoder instance through the audio encoder name. 42 * The premise of using this interface is to know the exact name of the encoder. 43 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 44 * @param name Audio encoder name 45 * @return Returns a Pointer to an OH_AVCodec instance 46 * @since 9 47 * @version 1.0 48 */ 49 OH_AVCodec *OH_AudioEncoder_CreateByName(const char *name); 50 51 /** 52 * @brief Clear the internal resources of the encoder and destroy the encoder instance 53 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 54 * @param codec Pointer to an OH_AVCodec instance 55 * @return Returns AV_ERR_OK if the execution is successful, 56 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 57 * @since 9 58 * @version 1.0 59 */ 60 OH_AVErrCode OH_AudioEncoder_Destroy(OH_AVCodec *codec); 61 62 /** 63 * @brief Set the asynchronous callback function so that your application can respond to 64 * the events generated by the audio encoder. This interface must be called before Prepare is called. 65 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 66 * @param codec Pointer to an OH_AVCodec instance 67 * @param callback A collection of all callback functions, see {@link OH_AVCodecAsyncCallback} 68 * @param userData User specific data 69 * @return Returns AV_ERR_OK if the execution is successful, 70 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 71 * @since 9 72 * @version 1.0 73 */ 74 OH_AVErrCode OH_AudioEncoder_SetCallback(OH_AVCodec *codec, OH_AVCodecAsyncCallback callback, void *userData); 75 76 /** 77 * @brief To configure the audio encoder, typically, you need to configure the description information of 78 * the encoded audio track. This interface must be called before Prepare is called. 79 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 80 * @param codec Pointer to an OH_AVCodec instance 81 * @param format OH_AVFormat handle pointer 82 * @return Returns AV_ERR_OK if the execution is successful, 83 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 84 * @since 9 85 * @version 1.0 86 */ 87 OH_AVErrCode OH_AudioEncoder_Configure(OH_AVCodec *codec, OH_AVFormat *format); 88 89 /** 90 * @brief To prepare the internal resources of the encoder, 91 * the Configure interface must be called before calling this interface. 92 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 93 * @param codec Pointer to an OH_AVCodec instance 94 * @return Returns AV_ERR_OK if the execution is successful, 95 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 96 * @since 9 97 * @version 1.0 98 */ 99 OH_AVErrCode OH_AudioEncoder_Prepare(OH_AVCodec *codec); 100 101 /** 102 * @brief Start the encoder, this interface must be called after the Prepare is successful. 103 * After being successfully started, the encoder will start reporting NeedInputData events. 104 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 105 * @param codec Pointer to an OH_AVCodec instance 106 * @return Returns AV_ERR_OK if the execution is successful, 107 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 108 * @since 9 109 * @version 1.0 110 */ 111 OH_AVErrCode OH_AudioEncoder_Start(OH_AVCodec *codec); 112 113 /** 114 * @brief Stop the encoder. After stopping, you can re-enter the Started state through Start. 115 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 116 * @param codec Pointer to an OH_AVCodec instance 117 * @return Returns AV_ERR_OK if the execution is successful, 118 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 119 * @since 9 120 * @version 1.0 121 */ 122 OH_AVErrCode OH_AudioEncoder_Stop(OH_AVCodec *codec); 123 124 /** 125 * @brief Clear the input and output data buffered in the encoder. After this interface is called, 126 * all the Buffer indexes previously reported through the asynchronous callback will be invalidated, 127 * make sure not to access the Buffers corresponding to these indexes. 128 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 129 * @param codec Pointer to an OH_AVCodec instance 130 * @return Returns AV_ERR_OK if the execution is successful, 131 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 132 * @since 9 133 * @version 1.0 134 */ 135 OH_AVErrCode OH_AudioEncoder_Flush(OH_AVCodec *codec); 136 137 /** 138 * @brief Reset the encoder. To continue coding, you need to call the Configure interface 139 * again to configure the encoder instance. 140 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 141 * @param codec Pointer to an OH_AVCodec instance 142 * @return Returns AV_ERR_OK if the execution is successful, 143 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 144 * @since 9 145 * @version 1.0 146 */ 147 OH_AVErrCode OH_AudioEncoder_Reset(OH_AVCodec *codec); 148 149 /** 150 * @brief Get the description information of the output data of the encoder, refer to {@link OH_AVFormat} for details. 151 * It should be noted that the life cycle of the OH_AVFormat instance pointed to by the return value * needs to 152 * be manually released by the caller. 153 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 154 * @param codec Pointer to an OH_AVCodec instance 155 * @return Returns the OH_AVFormat handle pointer, the life cycle is refreshed with the next GetOutputMediaDescription, 156 * or destroyed with OH_AVCodec; 157 * @since 9 158 * @version 1.0 159 */ 160 OH_AVFormat *OH_AudioEncoder_GetOutputDescription(OH_AVCodec *codec); 161 162 /** 163 * @brief Set dynamic parameters to the encoder. Note: This interface can only be called after the encoder is started. 164 * At the same time, incorrect parameter settings may cause the encoding to fail. 165 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 166 * @param codec Pointer to an OH_AVCodec instance 167 * @param format OH_AVFormat handle pointer 168 * @return Returns AV_ERR_OK if the execution is successful, 169 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 170 * @since 9 171 * @version 1.0 172 */ 173 OH_AVErrCode OH_AudioEncoder_SetParameter(OH_AVCodec *codec, OH_AVFormat *format); 174 175 /** 176 * @brief Submit the input buffer filled with data to the audio encoder. The {@link OH_AVCodecOnNeedInputData} 177 * callback will report the available input buffer and the corresponding index value. Once the buffer with the 178 * specified index is submitted to the audio encoder, the buffer cannot be accessed again until the 179 * callback is received again reporting that the buffer with the same index is available 180 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 181 * @param codec Pointer to an OH_AVCodec instance 182 * @param index Enter the index value corresponding to the Buffer 183 * @param attr Information describing the data contained in the Buffer 184 * @return Returns AV_ERR_OK if the execution is successful, 185 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 186 * @since 9 187 * @version 1.0 188 */ 189 OH_AVErrCode OH_AudioEncoder_PushInputData(OH_AVCodec *codec, uint32_t index, OH_AVCodecBufferAttr attr); 190 191 /** 192 * @brief Return the processed output Buffer to the encoder. 193 * @syscap SystemCapability.Multimedia.Media.AudioEncoder 194 * @param codec Pointer to an OH_AVCodec instance 195 * @param index The index value corresponding to the output Buffer 196 * @return Returns AV_ERR_OK if the execution is successful, 197 * otherwise returns a specific error code, refer to {@link OH_AVErrCode} 198 * @since 9 199 * @version 1.0 200 */ 201 OH_AVErrCode OH_AudioEncoder_FreeOutputData(OH_AVCodec *codec, uint32_t index); 202 203 #ifdef __cplusplus 204 } 205 #endif 206 207 #endif // NATIVE_AVCODEC_AUDIOENCODER_H