1e41f4b71Sopenharmony_ci# Audio Encoding 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can call the native APIs provided by the AudioCodec module to encode audio, that is, to compress audio PCM data into a desired format. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciPCM data can be from any source. For example, you can use a microphone to record audio data or import edited PCM data. After audio encoding, you can output streams in the desired format and encapsulate the streams into a target file. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciCurrently, the following encoding capabilities are supported: 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci| Container Format| Audio Encoding Type | 10e41f4b71Sopenharmony_ci| -------- | :--------------- | 11e41f4b71Sopenharmony_ci| mp4 | AAC, FLAC | 12e41f4b71Sopenharmony_ci| m4a | AAC | 13e41f4b71Sopenharmony_ci| flac | FLAC | 14e41f4b71Sopenharmony_ci| aac | AAC | 15e41f4b71Sopenharmony_ci| mp3 | MP3 | 16e41f4b71Sopenharmony_ci| raw | G711mu | 17e41f4b71Sopenharmony_ci<!--RP1--><!--RP1End--> 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**Usage Scenario** 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- Audio recording 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci Record and pass in PCM data, and encode the data into streams in the desired format. 24e41f4b71Sopenharmony_ci- Audio editing 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci Export edited PCM data, and encode the data into streams in the desired format. 27e41f4b71Sopenharmony_ci> **NOTE** 28e41f4b71Sopenharmony_ci> 29e41f4b71Sopenharmony_ci> AAC encoders adopt the VBR mode by default, which may differ in the configured parameters. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## How to Develop 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciRead [AudioCodec](../../reference/apis-avcodec-kit/_audio_codec.md) for the API reference. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciRefer to the code snippet below to complete the entire audio encoding process, including creating an encoder, setting encoding parameters (such as the sampling rate, bit rate, and number of audio channels), and starting, refreshing, resetting, and destroying the encoder. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciDuring application development, you must call the APIs in the defined sequence. Otherwise, an exception or undefined behavior may occur. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciThe figure below shows the call relationship of audio encoding. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- The dotted line indicates an optional operation. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci- The solid line indicates a mandatory operation. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci### Linking the Dynamic Libraries in the CMake Script 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci```cmake 50e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libnative_media_codecbase.so) 51e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libnative_media_core.so) 52e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libnative_media_acodec.so) 53e41f4b71Sopenharmony_ci``` 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci### How to Develop 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci1. Add the header files. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci ```cpp 60e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avcodec_audiocodec.h> 61e41f4b71Sopenharmony_ci #include <multimedia/native_audio_channel_layout.h> 62e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avcapability.h> 63e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avcodec_base.h> 64e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avformat.h> 65e41f4b71Sopenharmony_ci #include <multimedia/player_framework/native_avbuffer.h> 66e41f4b71Sopenharmony_ci ``` 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci2. Create an encoder instance. In the code snippet below, **OH_AVCodec *** is the pointer to the encoder instance created. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci You can create an encoder by name or MIME type. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci ```cpp 73e41f4b71Sopenharmony_ci // Namespace of the C++ standard library. 74e41f4b71Sopenharmony_ci using namespace std; 75e41f4b71Sopenharmony_ci // Create an encoder by name. 76e41f4b71Sopenharmony_ci OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true); 77e41f4b71Sopenharmony_ci const char *name = OH_AVCapability_GetName(capability); 78e41f4b71Sopenharmony_ci OH_AVCodec *audioEnc_ = OH_AudioCodec_CreateByName(name); 79e41f4b71Sopenharmony_ci ``` 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci ```cpp 82e41f4b71Sopenharmony_ci // Specify whether encoding is used. The value true means encoding. 83e41f4b71Sopenharmony_ci bool isEncoder = true; 84e41f4b71Sopenharmony_ci // Create an encoder by MIME type. 85e41f4b71Sopenharmony_ci OH_AVCodec *audioEnc_ = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, isEncoder); 86e41f4b71Sopenharmony_ci ``` 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci ```cpp 89e41f4b71Sopenharmony_ci // Initialize the queues. 90e41f4b71Sopenharmony_ci class AEncBufferSignal { 91e41f4b71Sopenharmony_ci public: 92e41f4b71Sopenharmony_ci std::mutex inMutex_; 93e41f4b71Sopenharmony_ci std::mutex outMutex_; 94e41f4b71Sopenharmony_ci std::mutex startMutex_; 95e41f4b71Sopenharmony_ci std::condition_variable inCond_; 96e41f4b71Sopenharmony_ci std::condition_variable outCond_; 97e41f4b71Sopenharmony_ci std::condition_variable startCond_; 98e41f4b71Sopenharmony_ci std::queue<uint32_t> inQueue_; 99e41f4b71Sopenharmony_ci std::queue<uint32_t> outQueue_; 100e41f4b71Sopenharmony_ci std::queue<OH_AVBuffer *> inBufferQueue_; 101e41f4b71Sopenharmony_ci std::queue<OH_AVBuffer *> outBufferQueue_; 102e41f4b71Sopenharmony_ci }; 103e41f4b71Sopenharmony_ci AEncBufferSignal *signal_; 104e41f4b71Sopenharmony_ci ``` 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci3. Call **OH_AudioCodec_RegisterCallback()** to register callback functions. 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci Register the **OH_AVCodecCallback** struct that defines the following callback function pointers: 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci - **OH_AVCodecOnError**, a callback used to report a codec operation error 111e41f4b71Sopenharmony_ci - **OH_AVCodecOnStreamChanged**, a callback used to report a codec stream change, for example, audio channel change 112e41f4b71Sopenharmony_ci - **OH_AVCodecOnNeedInputBuffer**, a callback used to report input data required, which means that the encoder is ready for receiving PCM data 113e41f4b71Sopenharmony_ci - **OH_AVCodecOnNewOutputBuffer**, a callback used to report output data generated, which means that encoding is complete 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci You need to process the callback functions to ensure that the encoder runs properly. 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci ```cpp 118e41f4b71Sopenharmony_ci // Implement the OH_AVCodecOnError callback function. 119e41f4b71Sopenharmony_ci static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData) 120e41f4b71Sopenharmony_ci { 121e41f4b71Sopenharmony_ci (void)codec; 122e41f4b71Sopenharmony_ci (void)errorCode; 123e41f4b71Sopenharmony_ci (void)userData; 124e41f4b71Sopenharmony_ci } 125e41f4b71Sopenharmony_ci // Implement the OH_AVCodecOnStreamChanged callback function. 126e41f4b71Sopenharmony_ci static void OnOutputFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData) 127e41f4b71Sopenharmony_ci { 128e41f4b71Sopenharmony_ci (void)codec; 129e41f4b71Sopenharmony_ci (void)format; 130e41f4b71Sopenharmony_ci (void)userData; 131e41f4b71Sopenharmony_ci } 132e41f4b71Sopenharmony_ci // Implement the OH_AVCodecOnNeedInputBuffer callback function. 133e41f4b71Sopenharmony_ci static void OnInputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData) 134e41f4b71Sopenharmony_ci { 135e41f4b71Sopenharmony_ci (void)codec; 136e41f4b71Sopenharmony_ci // The input stream is sent to the InputBuffer queue. 137e41f4b71Sopenharmony_ci AEncBufferSignal *signal = static_cast<AEncBufferSignal *>(userData); 138e41f4b71Sopenharmony_ci unique_lock<mutex> lock(signal->inMutex_); 139e41f4b71Sopenharmony_ci signal->inQueue_.push(index); 140e41f4b71Sopenharmony_ci signal->inBufferQueue_.push(data); 141e41f4b71Sopenharmony_ci signal->inCond_.notify_all(); 142e41f4b71Sopenharmony_ci } 143e41f4b71Sopenharmony_ci // Implement the OH_AVCodecOnNewOutputBuffer callback function. 144e41f4b71Sopenharmony_ci static void OnOutputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData) 145e41f4b71Sopenharmony_ci { 146e41f4b71Sopenharmony_ci (void)codec; 147e41f4b71Sopenharmony_ci // The index of the output buffer is sent to OutputQueue_. 148e41f4b71Sopenharmony_ci // The encoded data is sent to the outBuffer queue. 149e41f4b71Sopenharmony_ci AEncBufferSignal *signal = static_cast<AEncBufferSignal *>(userData); 150e41f4b71Sopenharmony_ci unique_lock<mutex> lock(signal->outMutex_); 151e41f4b71Sopenharmony_ci signal->outQueue_.push(index); 152e41f4b71Sopenharmony_ci signal->outBufferQueue_.push(data); 153e41f4b71Sopenharmony_ci } 154e41f4b71Sopenharmony_ci signal_ = new AEncBufferSignal(); 155e41f4b71Sopenharmony_ci OH_AVCodecCallback cb_ = {&OnError, &OnOutputFormatChanged, &OnInputBufferAvailable, &OnOutputBufferAvailable}; 156e41f4b71Sopenharmony_ci // Set the asynchronous callbacks. 157e41f4b71Sopenharmony_ci int32_t ret = OH_AudioCodec_RegisterCallback(audioEnc_, cb_, signal_); 158e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 159e41f4b71Sopenharmony_ci // Exception handling. 160e41f4b71Sopenharmony_ci } 161e41f4b71Sopenharmony_ci ``` 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci4. Call **OH_AudioCodec_Configure** to configure the encoder. 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci The following options are mandatory: sampling rate, bit rate, number of audio channels, audio channel type, and bit depth. 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci The maximum input length is optional. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci For FLAC encoding, the compliance level and sampling precision are also mandatory. 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci The sample below lists the value range of each audio encoding type. 172e41f4b71Sopenharmony_ci | Audio Encoding Type| Sampling Rate (Hz) | Audio Channel Count | 173e41f4b71Sopenharmony_ci | ----------- | ------------------------------------------------------------------------------- | :----------------: | 174e41f4b71Sopenharmony_ci | AAC | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000| 1, 2, 3, 4, 5, 6, and 8| 175e41f4b71Sopenharmony_ci | FLAC | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000| 1–8 | 176e41f4b71Sopenharmony_ci | MP3 | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 | 1–2 | 177e41f4b71Sopenharmony_ci | G711mu | 8000 | 1 | 178e41f4b71Sopenharmony_ci <!--RP3--><!--RP3End--> 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci The code snippet below shows the API call process, where AAC encoding at the bit rate of 32000 bit/s is carried out on the PCM audio with the 44100 Hz sampling rate, 2-channel stereo, and SAMPLE_S16LE sampling format. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci ```cpp 183e41f4b71Sopenharmony_ci int32_t ret; 184e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio sampling rate. 185e41f4b71Sopenharmony_ci constexpr uint32_t DEFAULT_SAMPLERATE = 44100; 186e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio bit rate. 187e41f4b71Sopenharmony_ci constexpr uint64_t DEFAULT_BITRATE = 32000; 188e41f4b71Sopenharmony_ci // (Mandatory) Configure the number of audio channels. 189e41f4b71Sopenharmony_ci constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2; 190e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio channel type. 191e41f4b71Sopenharmony_ci constexpr OH_AudioChannelLayout CHANNEL_LAYOUT = OH_AudioChannelLayout::CH_LAYOUT_STEREO; 192e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio bit depth. 193e41f4b71Sopenharmony_ci constexpr OH_BitsPerSample SAMPLE_FORMAT = OH_BitsPerSample::SAMPLE_S16LE; 194e41f4b71Sopenharmony_ci // Configure the audio compliance level. The default value is 0, and the value ranges from -2 to 2. 195e41f4b71Sopenharmony_ci constexpr int32_t COMPLIANCE_LEVEL = 0; 196e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio sampling precision (SAMPLE_S16LE used as an example). 197e41f4b71Sopenharmony_ci constexpr OH_BitsPerSample BITS_PER_CODED_SAMPLE = OH_BitsPerSample::SAMPLE_S16LE; 198e41f4b71Sopenharmony_ci // A frame of audio data takes 20 ms. 199e41f4b71Sopenharmony_ci constexpr float TIME_PER_FRAME = 0.02; 200e41f4b71Sopenharmony_ci // (Optional) Configure the maximum input length and the size of each frame of audio data. 201e41f4b71Sopenharmony_ci constexpr uint32_t DEFAULT_MAX_INPUT_SIZE = DEFAULT_SAMPLERATE * TIME_PER_FRAME * DEFAULT_CHANNEL_COUNT * sizeof(short); // aac 202e41f4b71Sopenharmony_ci OH_AVFormat *format = OH_AVFormat_Create(); 203e41f4b71Sopenharmony_ci // Set the format. 204e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format,OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT); 205e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format,OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE); 206e41f4b71Sopenharmony_ci OH_AVFormat_SetLongValue(format,OH_MD_KEY_BITRATE, DEFAULT_BITRATE); 207e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT); 208e41f4b71Sopenharmony_ci OH_AVFormat_SetLongValue(format,OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT); 209e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format,OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE); 210e41f4b71Sopenharmony_ci // Configure the encoder. 211e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Configure(audioEnc_, format); 212e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 213e41f4b71Sopenharmony_ci // Exception handling. 214e41f4b71Sopenharmony_ci } 215e41f4b71Sopenharmony_ci ``` 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci The following shows the API call process in the case of FLAC encoding. 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci ```cpp 220e41f4b71Sopenharmony_ci int32_t ret; 221e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio sampling rate. 222e41f4b71Sopenharmony_ci constexpr uint32_t DEFAULT_SAMPLERATE = 44100; 223e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio bit rate. 224e41f4b71Sopenharmony_ci constexpr uint64_t DEFAULT_BITRATE = 261000; 225e41f4b71Sopenharmony_ci // (Mandatory) Configure the number of audio channels. 226e41f4b71Sopenharmony_ci constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2; 227e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio channel type. 228e41f4b71Sopenharmony_ci constexpr OH_AudioChannelLayout CHANNEL_LAYOUT = OH_AudioChannelLayout::CH_LAYOUT_STEREO; 229e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio bit depth. Only SAMPLE_S16LE and SAMPLE_S32LE are available for FLAC encoding. 230e41f4b71Sopenharmony_ci constexpr OH_BitsPerSample SAMPLE_FORMAT = OH_BitsPerSample::SAMPLE_S32LE; 231e41f4b71Sopenharmony_ci // Configure the audio compliance level. The default value is 0, and the value ranges from -2 to 2. 232e41f4b71Sopenharmony_ci constexpr int32_t COMPLIANCE_LEVEL = 0; 233e41f4b71Sopenharmony_ci // (Mandatory) Configure the audio sampling precision. SAMPLE_S16LE, SAMPLE_S24LE, and SAMPLE_S32LE are available. 234e41f4b71Sopenharmony_ci constexpr OH_BitsPerSample BITS_PER_CODED_SAMPLE = OH_BitsPerSample::SAMPLE_S24LE; 235e41f4b71Sopenharmony_ci OH_AVFormat *format = OH_AVFormat_Create(); 236e41f4b71Sopenharmony_ci // Set the format. 237e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT); 238e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE); 239e41f4b71Sopenharmony_ci OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE); 240e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, BITS_PER_CODED_SAMPLE); 241e41f4b71Sopenharmony_ci OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT); 242e41f4b71Sopenharmony_ci OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT); 243e41f4b71Sopenharmony_ci OH_AVFormat_SetLongValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, COMPLIANCE_LEVEL); 244e41f4b71Sopenharmony_ci // Configure the encoder. 245e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Configure(audioEnc_, format); 246e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 247e41f4b71Sopenharmony_ci // Exception handling. 248e41f4b71Sopenharmony_ci } 249e41f4b71Sopenharmony_ci ``` 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci <!--RP2--><!--RP2End--> 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci5. Call **OH_AudioCodec_Prepare()** to prepare internal resources for the encoder. 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci ```cpp 256e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Prepare(audioEnc_); 257e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 258e41f4b71Sopenharmony_ci // Exception handling. 259e41f4b71Sopenharmony_ci } 260e41f4b71Sopenharmony_ci ``` 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci6. Call **OH_AudioCodec_Start()** to start the encoder. 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci ```c++ 265e41f4b71Sopenharmony_ci unique_ptr<ifstream> inputFile_ = make_unique<ifstream>(); 266e41f4b71Sopenharmony_ci unique_ptr<ofstream> outFile_ = make_unique<ofstream>(); 267e41f4b71Sopenharmony_ci // Open the path of the binary file to be encoded. (A PCM file is used as an example.) 268e41f4b71Sopenharmony_ci inputFile_->open(inputFilePath.data(), ios::in | ios::binary); 269e41f4b71Sopenharmony_ci // Configure the path of the output file. (An encoded stream file is used as an example.) 270e41f4b71Sopenharmony_ci outFile_->open(outputFilePath.data(), ios::out | ios::binary); 271e41f4b71Sopenharmony_ci // Start encoding. 272e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Start(audioEnc_); 273e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 274e41f4b71Sopenharmony_ci // Exception handling. 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci ``` 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci7. Call **OH_AudioCodec_PushInputBuffer()** to write the data to encode. 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci To indicate the End of Stream (EOS), pass in the **AVCODEC_BUFFER_FLAGS_EOS** flag. 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci For AAC encoding, set **SAMPLES_PER_FRAME** to the number of PCM samples every 20 ms, that is, sampling rate x 0.02. 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci For FLAC encoding, set **SAMPLES_PER_FRAME** based on the table below. 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci | Sampling Rate| Sample Count| 287e41f4b71Sopenharmony_ci | :----: | :----: | 288e41f4b71Sopenharmony_ci | 8000 | 576 | 289e41f4b71Sopenharmony_ci | 16000 | 1152 | 290e41f4b71Sopenharmony_ci | 22050 | 2304 | 291e41f4b71Sopenharmony_ci | 24000 | 2304 | 292e41f4b71Sopenharmony_ci | 32000 | 2304 | 293e41f4b71Sopenharmony_ci | 44100 | 4608 | 294e41f4b71Sopenharmony_ci | 48000 | 4608 | 295e41f4b71Sopenharmony_ci | 88200 | 8192 | 296e41f4b71Sopenharmony_ci | 96000 | 8192 | 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci > **NOTE** 299e41f4b71Sopenharmony_ci > 300e41f4b71Sopenharmony_ci > It is recommended that **SAMPLES_PER_FRAME** in AAC encoding be the number of PCM samples every 20 ms, that is, sampling rate x 0.02. In the case of FLAC encoding, if the number of samples is greater than the corresponding value provided in the table, an error code is returned. If the number is less than the corresponding value provided in the table, the encoded file may be damaged. 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci ```c++ 303e41f4b71Sopenharmony_ci // Number of samples per frame. 304e41f4b71Sopenharmony_ci constexpr int32_t SAMPLES_PER_FRAME = DEFAULT_SAMPLERATE * TIME_PER_FRAME; 305e41f4b71Sopenharmony_ci // Number of audio channels. For AMR encoding, only mono audio input is supported. 306e41f4b71Sopenharmony_ci constexpr int32_t DEFAULT_CHANNEL_COUNT = 2; 307e41f4b71Sopenharmony_ci // Length of the input data of each frame, that is, number of audio channels x number of samples per frame x number of bytes per sample (SAMPLE_S16LE used as an example). 308e41f4b71Sopenharmony_ci constexpr int32_t INPUT_FRAME_BYTES = DEFAULT_CHANNEL_COUNT * SAMPLES_PER_FRAME * sizeof(short); 309e41f4b71Sopenharmony_ci uint32_t index = signal_->inQueue_.front(); 310e41f4b71Sopenharmony_ci auto buffer = signal_->inBufferQueue_.front(); 311e41f4b71Sopenharmony_ci OH_AVCodecBufferAttr attr = {0}; 312e41f4b71Sopenharmony_ci if (!inputFile_->eof()) { 313e41f4b71Sopenharmony_ci inputFile_->read((char *)OH_AVBuffer_GetAddr(buffer), INPUT_FRAME_BYTES); 314e41f4b71Sopenharmony_ci attr.size = INPUT_FRAME_BYTES; 315e41f4b71Sopenharmony_ci attr.flags = AVCODEC_BUFFER_FLAGS_NONE; 316e41f4b71Sopenharmony_ci } else { 317e41f4b71Sopenharmony_ci attr.size = 0; 318e41f4b71Sopenharmony_ci attr.flags = AVCODEC_BUFFER_FLAGS_EOS; 319e41f4b71Sopenharmony_ci } 320e41f4b71Sopenharmony_ci OH_AVBuffer_SetBufferAttr(buffer, &attr); 321e41f4b71Sopenharmony_ci // Send the data to the input queue for encoding. The index is the subscript of the queue. 322e41f4b71Sopenharmony_ci ret = OH_AudioCodec_PushInputBuffer(audioEnc_, index); 323e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 324e41f4b71Sopenharmony_ci // Exception handling. 325e41f4b71Sopenharmony_ci } 326e41f4b71Sopenharmony_ci ``` 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci8. Call **OH_AudioCodec_FreeOutputBuffer()** to output the encoded stream. 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci ```c++ 331e41f4b71Sopenharmony_ci uint32_t index = signal_->outQueue_.front(); 332e41f4b71Sopenharmony_ci OH_AVBuffer *avBuffer = signal_->outBufferQueue_.front(); 333e41f4b71Sopenharmony_ci // Obtain the buffer attributes. 334e41f4b71Sopenharmony_ci OH_AVCodecBufferAttr attr = {0}; 335e41f4b71Sopenharmony_ci ret = OH_AVBuffer_GetBufferAttr(avBuffer, &attr); 336e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 337e41f4b71Sopenharmony_ci // Exception handling. 338e41f4b71Sopenharmony_ci } 339e41f4b71Sopenharmony_ci // Write the encoded data (specified by data) to the output file. 340e41f4b71Sopenharmony_ci outputFile_->write(reinterpret_cast<char *>(OH_AVBuffer_GetAddr(avBuffer)), attr.size); 341e41f4b71Sopenharmony_ci // Release the output buffer. 342e41f4b71Sopenharmony_ci ret = OH_AudioCodec_FreeOutputBuffer(audioEnc_, index); 343e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 344e41f4b71Sopenharmony_ci // Exception handling. 345e41f4b71Sopenharmony_ci } 346e41f4b71Sopenharmony_ci if (attr.flags == AVCODEC_BUFFER_FLAGS_EOS) { 347e41f4b71Sopenharmony_ci // End 348e41f4b71Sopenharmony_ci } 349e41f4b71Sopenharmony_ci ``` 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci9. (Optional) Call **OH_AudioCodec_Flush()** to refresh the encoder. 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci After **OH_AudioCodec_Flush()** is called, the current encoding queue is cleared. 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci To continue encoding, you must call **OH_AudioCodec_Start()** again. 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci You need to call **OH_AudioCodec_Flush()** in the following cases: 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci * The EOS of the file is reached. 360e41f4b71Sopenharmony_ci * An error with **OH_AudioCodec_IsValid** set to **true** (indicating that the execution can continue) occurs. 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci ```c++ 363e41f4b71Sopenharmony_ci // Refresh the encoder. 364e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Flush(audioEnc_); 365e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 366e41f4b71Sopenharmony_ci // Exception handling. 367e41f4b71Sopenharmony_ci } 368e41f4b71Sopenharmony_ci // Start encoding again. 369e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Start(audioEnc_); 370e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 371e41f4b71Sopenharmony_ci // Exception handling. 372e41f4b71Sopenharmony_ci } 373e41f4b71Sopenharmony_ci ``` 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci10. (Optional) Call **OH_AudioCodec_Reset()** to reset the encoder. 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci After **OH_AudioCodec_Reset()** is called, the encoder returns to the initialized state. To continue encoding, you must call **OH_AudioCodec_Configure()** and then **OH_AudioCodec_Start()**. 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci ```c++ 380e41f4b71Sopenharmony_ci // Reset the encoder. 381e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Reset(audioEnc_); 382e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 383e41f4b71Sopenharmony_ci // Exception handling. 384e41f4b71Sopenharmony_ci } 385e41f4b71Sopenharmony_ci // Reconfigure the encoder. 386e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Configure(audioEnc_, format); 387e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 388e41f4b71Sopenharmony_ci // Exception handling. 389e41f4b71Sopenharmony_ci } 390e41f4b71Sopenharmony_ci ``` 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci11. Call **OH_AudioCodec_Stop()** to stop the encoder. 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci ```c++ 395e41f4b71Sopenharmony_ci // Stop the encoder. 396e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Stop(audioEnc_); 397e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 398e41f4b71Sopenharmony_ci // Exception handling. 399e41f4b71Sopenharmony_ci } 400e41f4b71Sopenharmony_ci ``` 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci12. Call **OH_AudioCodec_Destroy()** to destroy the encoder instance and release resources. 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci > **NOTE** 405e41f4b71Sopenharmony_ci > 406e41f4b71Sopenharmony_ci > You only need to call the API once. 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci ```c++ 409e41f4b71Sopenharmony_ci // Call OH_AudioCodec_Destroy to destroy the encoder. 410e41f4b71Sopenharmony_ci ret = OH_AudioCodec_Destroy(audioEnc_); 411e41f4b71Sopenharmony_ci if (ret != AV_ERR_OK) { 412e41f4b71Sopenharmony_ci // Exception handling. 413e41f4b71Sopenharmony_ci } else { 414e41f4b71Sopenharmony_ci audioEnc_ = NULL; // The encoder cannot be destroyed repeatedly. 415e41f4b71Sopenharmony_ci } 416e41f4b71Sopenharmony_ci ``` 417