1 /* 2 ** Copyright 2008, 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 ANDROID_AUDIO_HARDWARE_H 18 #define ANDROID_AUDIO_HARDWARE_H 19 20 #include <stdint.h> 21 #include <sys/types.h> 22 23 #include <utils/threads.h> 24 #include <utils/SortedVector.h> 25 26 #include <hardware_legacy/AudioHardwareBase.h> 27 #include <hardware/audio_effect.h> 28 29 #include "secril-client.h" 30 31 #include <audio_utils/resampler.h> 32 #include <audio_utils/echo_reference.h> 33 34 extern "C" { 35 struct pcm; 36 struct mixer; 37 struct mixer_ctl; 38 }; 39 40 namespace android_audio_legacy { 41 using android::AutoMutex; 42 using android::Mutex; 43 using android::RefBase; 44 using android::SortedVector; 45 using android::sp; 46 using android::String16; 47 using android::Vector; 48 49 // TODO: determine actual audio DSP and hardware latency 50 // Additional latency introduced by audio DSP and hardware in ms 51 #define AUDIO_HW_OUT_LATENCY_MS 0 52 // Default audio output sample rate 53 #define AUDIO_HW_OUT_SAMPLERATE 44100 54 // Default audio output channel mask 55 #define AUDIO_HW_OUT_CHANNELS (AudioSystem::CHANNEL_OUT_STEREO) 56 // Default audio output sample format 57 #define AUDIO_HW_OUT_FORMAT (AudioSystem::PCM_16_BIT) 58 // Kernel pcm out buffer size in frames at 44.1kHz 59 #define AUDIO_HW_OUT_PERIOD_SZ 880 60 #define AUDIO_HW_OUT_PERIOD_CNT 2 61 // Default audio output buffer size in bytes 62 #define AUDIO_HW_OUT_PERIOD_BYTES (AUDIO_HW_OUT_PERIOD_SZ * 2 * sizeof(int16_t)) 63 64 // Default audio input sample rate 65 #define AUDIO_HW_IN_SAMPLERATE 44100 66 // Default audio input channel mask 67 #define AUDIO_HW_IN_CHANNELS (AudioSystem::CHANNEL_IN_MONO) 68 // Default audio input sample format 69 #define AUDIO_HW_IN_FORMAT (AudioSystem::PCM_16_BIT) 70 // Kernel pcm in buffer size in frames at 44.1kHz (before resampling) 71 #define AUDIO_HW_IN_PERIOD_SZ 1024 72 #define AUDIO_HW_IN_PERIOD_CNT 4 73 // Default audio input buffer size in bytes (8kHz mono) 74 #define AUDIO_HW_IN_PERIOD_BYTES ((AUDIO_HW_IN_PERIOD_SZ*sizeof(int16_t))/8) 75 76 77 class AudioHardware : public AudioHardwareBase 78 { 79 class AudioStreamOutALSA; 80 class AudioStreamInALSA; 81 82 public: 83 84 // input path names used to translate from input sources to driver paths 85 static const char *inputPathNameDefault; 86 static const char *inputPathNameCamcorder; 87 static const char *inputPathNameVoiceRecognition; 88 89 AudioHardware(); 90 virtual ~AudioHardware(); 91 virtual status_t initCheck(); 92 93 virtual status_t setVoiceVolume(float volume); 94 virtual status_t setMasterVolume(float volume); 95 96 virtual status_t setMode(int mode); 97 98 virtual status_t setMicMute(bool state); 99 virtual status_t getMicMute(bool* state); 100 101 virtual status_t setParameters(const String8& keyValuePairs); 102 virtual String8 getParameters(const String8& keys); 103 104 virtual AudioStreamOut* openOutputStream( 105 uint32_t devices, int *format=0, uint32_t *channels=0, 106 uint32_t *sampleRate=0, status_t *status=0); 107 108 virtual AudioStreamIn* openInputStream( 109 uint32_t devices, int *format, uint32_t *channels, 110 uint32_t *sampleRate, status_t *status, 111 AudioSystem::audio_in_acoustics acoustics); 112 113 virtual void closeOutputStream(AudioStreamOut* out); 114 virtual void closeInputStream(AudioStreamIn* in); 115 116 virtual size_t getInputBufferSize( 117 uint32_t sampleRate, int format, int channelCount); 118 119 int mode() { return mMode; } 120 const char *getOutputRouteFromDevice(uint32_t device); 121 const char *getInputRouteFromDevice(uint32_t device); 122 const char *getVoiceRouteFromDevice(uint32_t device); 123 124 status_t setIncallPath_l(uint32_t device); 125 126 status_t setInputSource_l(audio_source source); 127 128 void setVoiceVolume_l(float volume); 129 130 static uint32_t getInputSampleRate(uint32_t sampleRate); 131 sp <AudioStreamInALSA> getActiveInput_l(); 132 133 Mutex& lock() { return mLock; } 134 135 struct pcm *openPcmOut_l(); 136 void closePcmOut_l(); 137 138 struct mixer *openMixer_l(); 139 void closeMixer_l(); 140 141 sp <AudioStreamOutALSA> output() { return mOutput; } 142 143 struct echo_reference_itfe *getEchoReference(audio_format_t format, 144 uint32_t channelCount, 145 uint32_t samplingRate); 146 void releaseEchoReference(struct echo_reference_itfe *reference); 147 148 protected: 149 virtual status_t dump(int fd, const Vector<String16>& args); 150 151 private: 152 153 enum tty_modes { 154 TTY_MODE_OFF, 155 TTY_MODE_VCO, 156 TTY_MODE_HCO, 157 TTY_MODE_FULL 158 }; 159 160 bool mInit; 161 bool mMicMute; 162 sp <AudioStreamOutALSA> mOutput; 163 SortedVector < sp<AudioStreamInALSA> > mInputs; 164 Mutex mLock; 165 struct pcm* mPcm; 166 struct mixer* mMixer; 167 uint32_t mPcmOpenCnt; 168 uint32_t mMixerOpenCnt; 169 bool mInCallAudioMode; 170 float mVoiceVol; 171 172 audio_source mInputSource; 173 bool mBluetoothNrec; 174 int mTTYMode; 175 176 void* mSecRilLibHandle; 177 HRilClient mRilClient; 178 bool mActivatedCP; 179 HRilClient (*openClientRILD) (void); 180 int (*disconnectRILD) (HRilClient); 181 int (*closeClientRILD) (HRilClient); 182 int (*isConnectedRILD) (HRilClient); 183 int (*connectRILD) (HRilClient); 184 int (*setCallVolume) (HRilClient, SoundType, int); 185 int (*setCallAudioPath)(HRilClient, AudioPath); 186 int (*setCallClockSync)(HRilClient, SoundClockCondition); 187 void loadRILD(void); 188 status_t connectRILDIfRequired(void); 189 struct echo_reference_itfe *mEchoReference; 190 191 // trace driver operations for dump 192 int mDriverOp; 193 194 static uint32_t checkInputSampleRate(uint32_t sampleRate); 195 196 // column index in inputConfigTable[][] 197 enum { 198 INPUT_CONFIG_SAMPLE_RATE, 199 INPUT_CONFIG_BUFFER_RATIO, 200 INPUT_CONFIG_CNT 201 }; 202 203 // contains the list of valid sampling rates for input streams as well as the ratio 204 // between the kernel buffer size and audio hal buffer size for each sampling rate 205 static const uint32_t inputConfigTable[][INPUT_CONFIG_CNT]; 206 207 class AudioStreamOutALSA : public AudioStreamOut, public RefBase 208 { 209 public: 210 AudioStreamOutALSA(); 211 virtual ~AudioStreamOutALSA(); 212 status_t set(AudioHardware* mHardware, 213 uint32_t devices, 214 int *pFormat, 215 uint32_t *pChannels, 216 uint32_t *pRate); 217 virtual uint32_t sampleRate() 218 const { return mSampleRate; } 219 virtual size_t bufferSize() 220 const { return mBufferSize; } 221 virtual uint32_t channels() 222 const { return mChannels; } 223 virtual int format() 224 const { return AUDIO_HW_OUT_FORMAT; } 225 virtual uint32_t latency() 226 const { return (1000 * AUDIO_HW_OUT_PERIOD_CNT * 227 (bufferSize()/frameSize()))/sampleRate() + 228 AUDIO_HW_OUT_LATENCY_MS; } 229 virtual status_t setVolume(float left, float right) 230 { return INVALID_OPERATION; } 231 virtual ssize_t write(const void* buffer, size_t bytes); 232 virtual status_t standby(); 233 bool checkStandby(); 234 235 virtual status_t dump(int fd, const Vector<String16>& args); 236 virtual status_t setParameters(const String8& keyValuePairs); 237 virtual String8 getParameters(const String8& keys); 238 uint32_t device() { return mDevices; } 239 virtual status_t getRenderPosition(uint32_t *dspFrames); 240 241 void doStandby_l(); 242 void close_l(); 243 status_t open_l(); 244 int standbyCnt() { return mStandbyCnt; } 245 246 int prepareLock(); 247 void lock(); 248 void unlock(); 249 250 void addEchoReference(struct echo_reference_itfe *reference); 251 void removeEchoReference(struct echo_reference_itfe *reference); 252 253 private: 254 255 int computeEchoReferenceDelay(size_t frames, struct timespec *echoRefRenderTime); 256 int getPlaybackDelay(size_t frames, struct echo_reference_buffer *buffer); 257 258 Mutex mLock; 259 AudioHardware* mHardware; 260 struct pcm *mPcm; 261 struct mixer *mMixer; 262 struct mixer_ctl *mRouteCtl; 263 const char *next_route; 264 bool mStandby; 265 uint32_t mDevices; 266 uint32_t mChannels; 267 uint32_t mSampleRate; 268 size_t mBufferSize; 269 // trace driver operations for dump 270 int mDriverOp; 271 int mStandbyCnt; 272 bool mSleepReq; 273 struct echo_reference_itfe *mEchoReference; 274 }; 275 276 class AudioStreamInALSA : public AudioStreamIn, public RefBase 277 { 278 279 public: 280 AudioStreamInALSA(); 281 virtual ~AudioStreamInALSA(); 282 status_t set(AudioHardware* hw, 283 uint32_t devices, 284 int *pFormat, 285 uint32_t *pChannels, 286 uint32_t *pRate, 287 AudioSystem::audio_in_acoustics acoustics); 288 virtual size_t bufferSize() const { return mBufferSize; } 289 virtual uint32_t channels() const { return mChannels; } 290 virtual int format() const { return AUDIO_HW_IN_FORMAT; } 291 virtual uint32_t sampleRate() const { return mSampleRate; } 292 virtual status_t setGain(float gain) { return INVALID_OPERATION; } 293 virtual ssize_t read(void* buffer, ssize_t bytes); 294 virtual status_t dump(int fd, const Vector<String16>& args); 295 virtual status_t standby(); 296 bool checkStandby(); 297 virtual status_t setParameters(const String8& keyValuePairs); 298 virtual String8 getParameters(const String8& keys); 299 virtual unsigned int getInputFramesLost() const { return 0; } 300 virtual status_t addAudioEffect(effect_handle_t effect); 301 virtual status_t removeAudioEffect(effect_handle_t effect); 302 303 uint32_t device() { return mDevices; } 304 void doStandby_l(); 305 void close_l(); 306 status_t open_l(); 307 int standbyCnt() { return mStandbyCnt; } 308 309 static size_t getBufferSize(uint32_t sampleRate, int channelCount); 310 311 // resampler_buffer_provider 312 static int getNextBufferStatic(struct resampler_buffer_provider *provider, 313 struct resampler_buffer* buffer); 314 static void releaseBufferStatic(struct resampler_buffer_provider *provider, 315 struct resampler_buffer* buffer); 316 317 int prepareLock(); 318 void lock(); 319 void unlock(); 320 321 private: 322 323 struct ResamplerBufferProvider { 324 struct resampler_buffer_provider mProvider; 325 AudioStreamInALSA *mInputStream; 326 }; 327 328 ssize_t readFrames(void* buffer, ssize_t frames); 329 ssize_t processFrames(void* buffer, ssize_t frames); 330 int32_t updateEchoReference(size_t frames); 331 void pushEchoReference(size_t frames); 332 void updateEchoDelay(size_t frames, struct timespec *echoRefRenderTime); 333 void getCaptureDelay(size_t frames, struct echo_reference_buffer *buffer); 334 status_t setPreProcessorEchoDelay(effect_handle_t handle, int32_t delayUs); 335 status_t setPreprocessorParam(effect_handle_t handle, effect_param_t *param); 336 337 // BufferProvider 338 status_t getNextBuffer(struct resampler_buffer* buffer); 339 void releaseBuffer(struct resampler_buffer* buffer); 340 341 Mutex mLock; 342 AudioHardware* mHardware; 343 struct pcm *mPcm; 344 struct mixer *mMixer; 345 struct mixer_ctl *mRouteCtl; 346 const char *next_route; 347 bool mStandby; 348 uint32_t mDevices; 349 uint32_t mChannels; 350 uint32_t mChannelCount; 351 uint32_t mSampleRate; 352 size_t mBufferSize; 353 struct resampler_itfe *mDownSampler; 354 struct ResamplerBufferProvider mBufferProvider; 355 status_t mReadStatus; 356 size_t mInputFramesIn; 357 int16_t *mInputBuf; 358 // trace driver operations for dump 359 int mDriverOp; 360 int mStandbyCnt; 361 bool mSleepReq; 362 SortedVector<effect_handle_t> mPreprocessors; 363 int16_t *mProcBuf; 364 size_t mProcBufSize; 365 size_t mProcFramesIn; 366 int16_t *mRefBuf; 367 size_t mRefBufSize; 368 size_t mRefFramesIn; 369 struct echo_reference_itfe *mEchoReference; 370 bool mNeedEchoReference; 371 }; 372 373 }; 374 375 }; // namespace android 376 377 #endif 378