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 FRAMEWORKS_AUDIO_ENCODER_INCLUDE_AUDIO_ENCODER_H_ 17 #define FRAMEWORKS_AUDIO_ENCODER_INCLUDE_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 length, by bytes */ 49 int64_t timeStamp = 0; 50 }; 51 52 typedef struct { 53 CodecBuffer info; 54 CodecBufferInfo buffer; 55 } AudioBufferInfo; 56 57 class AudioEncoder { 58 public: 59 AudioEncoder(); 60 ~AudioEncoder(); 61 62 /** 63 * Initializes the audio source according to a specific configuration. 64 * 65 * @param config a configuration of audio source. 66 * @return Returns SUCCESS if success, other values otherwise. 67 */ 68 int32_t Initialize(const AudioEncodeConfig &config); 69 70 /** 71 * Binds audio source to a specific device. 72 * 73 * @param deviceId specifies the identity of device to bind. 74 * @return Returns SUCCESS if success, other values otherwise. 75 */ 76 int32_t BindSource(uint32_t deviceId); 77 78 /** 79 * Gets mute status of current encoder, dummy implemented currently. 80 * 81 * @param muted holds mute status if success. 82 * @return Returns SUCCESS if success, other values otherwise. 83 */ 84 int32_t GetMute(bool &muted); 85 86 /** 87 * Sets mute status of current encoder, dummy implemented currently. 88 * 89 * @param muted mute status to set. 90 * @return Returns SUCCESS if success, other values otherwise. 91 */ 92 int32_t SetMute(bool muted); 93 94 /** 95 * Starts audio source. 96 * 97 * @return Returns SUCCESS if success, other values otherwise. 98 */ 99 int32_t Start(); 100 101 /** 102 * 103 * Reads stream from a audio source. 104 * 105 * @param stream source stream to read from. 106 * @param isBlockingRead reading mode. 107 * @return Returns size of data actually read. 108 */ 109 int32_t ReadStream(AudioStream &stream, bool isBlockingRead); 110 111 /** 112 * Stops audio source. 113 * 114 * @return Returns SUCCESS if success, other values otherwise. 115 */ 116 int32_t Stop(); 117 118 /** 119 * release. 120 */ 121 int32_t Release(); 122 123 private: 124 int32_t InitAudioEncoderAttr(const AudioEncodeConfig &config); 125 void setEncAttrValue(const AudioEncodeConfig &config); 126 127 private: 128 bool initialized_; 129 CODEC_HANDLETYPE encHandle_; 130 CodecType domainKind_ = AUDIO_ENCODER; 131 AvCodecMime codecMime_ = MEDIA_MIMETYPE_AUDIO_AAC; 132 Profile profile_ = INVALID_PROFILE; 133 uint32_t sampleRate_ = 0; 134 uint32_t bitRate_ = 0; 135 AudioSoundMode soundMode_ = AUD_SOUND_MODE_INVALID; 136 uint32_t ptNumPerFrm_ = AUDIO_POINT_NUM; 137 uint32_t bufSize_ = AUDIO_FRAME_NUM_IN_BUF; 138 Param encAttr_[AUDIO_ENC_PARAM_NUM]; 139 bool started_ = false; 140 }; 141 } // namespace Audio 142 } // namespace OHOS 143 144 #endif // FRAMEWORKS_AUDIO_ENCODER_INCLUDE_AUDIO_ENCODER_H_ 145