1 /** 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef IMSMEDIA_AUDIO_SOURCE_INCLUDED 18 #define IMSMEDIA_AUDIO_SOURCE_INCLUDED 19 20 #include <ImsMediaDefine.h> 21 #include <ImsMediaAudioDefine.h> 22 #include <mutex> 23 #include <IImsMediaThread.h> 24 #include <ImsMediaCondition.h> 25 #include <IFrameCallback.h> 26 #include <aaudio/AAudio.h> 27 #include <media/NdkMediaCodec.h> 28 #include <media/NdkMediaFormat.h> 29 30 #define MAX_EVS_BW_STRLEN 5 31 32 using android::sp; 33 34 class ImsMediaAudioSource : public IImsMediaThread 35 { 36 public: 37 ImsMediaAudioSource(); 38 virtual ~ImsMediaAudioSource(); 39 40 /** 41 * @brief Sets the uplink callback object to pass the encoded audio frame to the client 42 * 43 * @param callback the callback object 44 */ 45 void SetUplinkCallback(IFrameCallback* callback); 46 47 /** 48 * @brief Sets the codec type 49 * 50 * @param type kAudioCodecType defined in ImsMediaDefine.h 51 */ 52 void SetCodec(int32_t type); 53 54 /** 55 * @brief Sets the encoder mode. 56 * 57 * @param mode enum of codec bitrate 58 */ 59 void SetCodecMode(uint32_t mode); 60 61 /** 62 * @brief Sets the bitrate of the evs encoder 63 * 64 * @param mode enum of codec bitrate 65 */ 66 void SetEvsBitRate(uint32_t mode); 67 68 /** 69 * @brief Sets the ptime 70 * 71 * @param time Recommended length of time in milliseconds 72 */ 73 void SetPtime(uint32_t time); 74 75 /** 76 * @brief Sets the evs bandwidth 77 * 78 * @param evsBandwidth enum of the bandwidth defined as the kEvsBandwidth 79 */ 80 void SetEvsBandwidth(int32_t evsBandwidth); 81 82 /** 83 * @brief Sets the audio sampling rate in Hz units 84 * 85 * @param samplingRate audio sampling rate to get the audio frame in Hz units 86 */ 87 void SetSamplingRate(int32_t samplingRate); 88 89 /** 90 * @brief Sets the evs channel aware mode offset 91 * 92 * @param offset Permissible values are -1, 0, 2, 3, 5, and 7. If ch-aw-recv is -1, 93 * channel-aware mode is disabled 94 */ 95 void SetEvsChAwOffset(int32_t offset); 96 97 /** 98 * @brief Sets audio media direction of the RTP session 99 * 100 * @param direction can be NO_FLOW, SEND_ONLY, RECEIVE_ONLY, SEND_RECEIVE, INACTIVE 101 */ 102 void SetMediaDirection(int32_t direction); 103 104 /** 105 * @brief Set Whether discontinuous transmission is enabled or not 106 * 107 * @params isDtxEnabled, if set to true then enable discontinuous transmission 108 */ 109 void SetDtxEnabled(bool isDtxEnabled); 110 111 /** 112 * @brief Setting octet-align for AMR/AMR-WB 113 * 114 * @params isOctetAligned, If it's set to true then all fields in the AMR/AMR-WB header 115 * shall be aligned to octet boundaries by adding padding bits. 116 */ 117 void SetOctetAligned(bool isOctetAligned); 118 119 /** 120 * @brief Starts aaudio and ndk audio codec to get the audio frame and encode the audio frames 121 * with given configuration 122 * 123 * @return true Returns when the audio codec and aaduio starts without error 124 * @return false Returns when the audio codec and aaudio configuration is invalid during the 125 * start 126 */ 127 bool Start(); 128 129 /** 130 * @brief Stops the audio encoder and aaudio 131 * 132 */ 133 void Stop(); 134 135 /** 136 * @brief Change bitrate of the encoding frames with given CMR value 137 * 138 * @param cmr The codec mode request value 139 */ 140 void ProcessCmr(const uint32_t cmr); 141 virtual void* run(); 142 143 private: 144 void openAudioStream(); 145 void restartAudioStream(); 146 bool startCodec(); 147 void stopCodec(); 148 void queueInputBuffer(int16_t* buffer, uint32_t size); 149 void dequeueOutputBuffer(); 150 static void audioErrorCallback(AAudioStream* stream, void* userData, aaudio_result_t error); 151 152 std::mutex mMutexUplink; 153 IFrameCallback* mCallback; 154 AAudioStream* mAudioStream; 155 AMediaCodec* mCodec; 156 AMediaFormat* mFormat; 157 int32_t mCodecType; 158 uint32_t mMode; 159 uint32_t mPtime; 160 uint32_t mSamplingRate; 161 uint32_t mBufferSize; 162 kEvsBandwidth mEvsBandwidth; 163 char mEvsbandwidthStr[MAX_EVS_BW_STRLEN]; 164 int32_t mEvsBitRate; 165 int32_t mEvsChAwOffset; 166 ImsMediaCondition mConditionExit; 167 bool mIsEvsInitialized; 168 int32_t mMediaDirection; 169 bool mIsDtxEnabled; 170 bool mIsOctetAligned; 171 }; 172 173 #endif 174