1 /* 2 * Copyright (c) 2020-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 AUDIO_ENCODER_H_ 17 #define AUDIO_ENCODER_H_ 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <memory> 22 #include <time.h> 23 #include <vector> 24 25 #include "codec_interface.h" 26 #include "format.h" 27 #include "media_errors.h" 28 #include "media_info.h" 29 30 namespace OHOS { 31 namespace Audio { 32 constexpr int32_t AUDIO_ENC_PARAM_NUM = 8; 33 /* count of audio frame in Buffer */ 34 constexpr uint32_t AUDIO_FRAME_NUM_IN_BUF = 30; 35 /* sample per frame for all encoder(aacplus:2048) */ 36 constexpr uint32_t AUDIO_POINT_NUM = 1024; 37 38 struct AudioEncodeConfig { 39 AudioCodecFormat audioFormat; 40 uint32_t bitRate = 0; 41 uint32_t sampleRate = 0; 42 uint32_t channelCount = 0; 43 AudioBitWidth bitWidth = BIT_WIDTH_16; 44 }; 45 46 struct AudioStream { 47 uint8_t *buffer = nullptr; /* the virtual address of stream */ 48 uint32_t bufferLen = 0; /* stream lenth, by bytes */ 49 int64_t timeStamp = 0; 50 }; 51 52 class AudioEncoder { 53 public: 54 AudioEncoder(); 55 ~AudioEncoder(); 56 57 /** 58 * Initailizes the audio source according to a specific configuration. 59 * 60 * @param config a configuration of audio source. 61 * @return Returns SUCCESS if success, other values otherwise. 62 */ 63 int32_t Initialize(const AudioEncodeConfig &config); 64 65 /** 66 * Binds audio source to a specific device. 67 * 68 * @param deviceId specifies the identity of device to bind. 69 * @return Returns SUCCESS if success, other values otherwise. 70 */ 71 int32_t BindSource(uint32_t deviceId); 72 73 /** 74 * Gets mute status of current encoder, dummy implemented currently. 75 * 76 * @param muted holds mute status if success. 77 * @return Returns SUCCESS if success, other values otherwise. 78 */ 79 int32_t GetMute(bool &muted); 80 81 /** 82 * Sets mute status of current encoder, dummy implemented currently. 83 * 84 * @param muted mute status to set. 85 * @return Returns SUCCESS if success, other values otherwise. 86 */ 87 int32_t SetMute(bool muted); 88 89 /** 90 * Starts audio source. 91 * 92 * @return Returns SUCCESS if success, other values otherwise. 93 */ 94 int32_t Start(); 95 96 /** 97 * 98 * Reads stream from a audio source. 99 * 100 * @param stream source stream to read from. 101 * @param isBlockingRead reading mode. 102 * @return Returns size of data actually read. 103 */ 104 int32_t ReadStream(AudioStream &stream, bool isBlockingRead); 105 106 /** 107 * Stops audio source. 108 * 109 * @return Returns SUCCESS if success, other values otherwise. 110 */ 111 int32_t Stop(); 112 113 /** 114 * release. 115 */ 116 int32_t Release(); 117 118 private: 119 int32_t InitAudioEncoderAttr(const AudioEncodeConfig &config); 120 121 private: 122 bool initialized_; 123 CODEC_HANDLETYPE encHandle_; 124 CodecType domainKind_ = AUDIO_ENCODER; 125 AvCodecMime codecMime_ = MEDIA_MIMETYPE_AUDIO_AAC; 126 Profile profile_ = INVALID_PROFILE; 127 AudioSampleRate sampleRate_ = AUD_SAMPLE_RATE_INVALID; 128 uint32_t bitRate_ = 0; 129 AudioSoundMode soundMode_ = AUD_SOUND_MODE_INVALID; 130 uint32_t ptNumPerFrm_ = AUDIO_POINT_NUM; 131 uint32_t bufSize_ = AUDIO_FRAME_NUM_IN_BUF; 132 Param encAttr_[AUDIO_ENC_PARAM_NUM]; 133 bool started_ = false; 134 }; 135 } // namespace Audio 136 } // namespace OHOS 137 #endif // AUDIO_ENCODER_H_ 138