1 /* 2 ** Copyright 2008, Google Inc. 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 28 extern "C" { 29 #include <linux/msm_audio.h> 30 } 31 32 namespace android { 33 34 // ---------------------------------------------------------------------------- 35 // Kernel driver interface 36 // 37 38 #define SAMP_RATE_INDX_8000 0 39 #define SAMP_RATE_INDX_11025 1 40 #define SAMP_RATE_INDX_12000 2 41 #define SAMP_RATE_INDX_16000 3 42 #define SAMP_RATE_INDX_22050 4 43 #define SAMP_RATE_INDX_24000 5 44 #define SAMP_RATE_INDX_32000 6 45 #define SAMP_RATE_INDX_44100 7 46 #define SAMP_RATE_INDX_48000 8 47 48 #define EQ_MAX_BAND_NUM 12 49 50 #define ADRC_ENABLE 0x0001 51 #define ADRC_DISABLE 0x0000 52 #define EQ_ENABLE 0x0002 53 #define EQ_DISABLE 0x0000 54 #define RX_IIR_ENABLE 0x0004 55 #define RX_IIR_DISABLE 0x0000 56 57 struct eq_filter_type { 58 int16_t gain; 59 uint16_t freq; 60 uint16_t type; 61 uint16_t qf; 62 }; 63 64 struct eqalizer { 65 uint16_t bands; 66 uint16_t params[132]; 67 }; 68 69 struct rx_iir_filter { 70 uint16_t num_bands; 71 uint16_t iir_params[48]; 72 }; 73 74 struct msm_audio_config { 75 uint32_t buffer_size; 76 uint32_t buffer_count; 77 uint32_t channel_count; 78 uint32_t sample_rate; 79 uint32_t codec_type; 80 uint32_t unused[3]; 81 }; 82 83 struct msm_audio_stats { 84 uint32_t out_bytes; 85 uint32_t unused[3]; 86 }; 87 88 #define CODEC_TYPE_PCM 0 89 #define AUDIO_HW_NUM_OUT_BUF 2 // Number of buffers in audio driver for output 90 // TODO: determine actual audio DSP and hardware latency 91 #define AUDIO_HW_OUT_LATENCY_MS 0 // Additionnal latency introduced by audio DSP and hardware in ms 92 93 #define AUDIO_HW_IN_SAMPLERATE 8000 // Default audio input sample rate 94 #define AUDIO_HW_IN_CHANNELS (AudioSystem::CHANNEL_IN_MONO) // Default audio input channel mask 95 #define AUDIO_HW_IN_BUFFERSIZE 2048 // Default audio input buffer size 96 #define AUDIO_HW_IN_FORMAT (AudioSystem::PCM_16_BIT) // Default audio input sample format 97 // ---------------------------------------------------------------------------- 98 99 100 class AudioHardware : public AudioHardwareBase 101 { 102 class AudioStreamOutMSM72xx; 103 class AudioStreamInMSM72xx; 104 105 public: 106 AudioHardware(); 107 virtual ~AudioHardware(); 108 virtual status_t initCheck(); 109 110 virtual status_t setVoiceVolume(float volume); 111 virtual status_t setMasterVolume(float volume); 112 113 virtual status_t setMode(int mode); 114 115 // mic mute 116 virtual status_t setMicMute(bool state); 117 virtual status_t getMicMute(bool* state); 118 119 virtual status_t setParameters(const String8& keyValuePairs); 120 virtual String8 getParameters(const String8& keys); 121 122 // create I/O streams 123 virtual AudioStreamOut* openOutputStream( 124 uint32_t devices, 125 int *format=0, 126 uint32_t *channels=0, 127 uint32_t *sampleRate=0, 128 status_t *status=0); 129 130 virtual AudioStreamIn* openInputStream( 131 132 uint32_t devices, 133 int *format, 134 uint32_t *channels, 135 uint32_t *sampleRate, 136 status_t *status, 137 AudioSystem::audio_in_acoustics acoustics); 138 139 virtual void closeOutputStream(AudioStreamOut* out); 140 virtual void closeInputStream(AudioStreamIn* in); 141 142 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount); 143 144 protected: 145 virtual status_t dump(int fd, const Vector<String16>& args); 146 147 private: 148 149 status_t doAudioRouteOrMute(uint32_t device); 150 status_t setMicMute_nosync(bool state); 151 status_t checkMicMute(); 152 status_t dumpInternals(int fd, const Vector<String16>& args); 153 uint32_t getInputSampleRate(uint32_t sampleRate); 154 bool checkOutputStandby(); 155 status_t doRouting(AudioStreamInMSM72xx *input); 156 157 class AudioStreamOutMSM72xx : public AudioStreamOut { 158 public: 159 AudioStreamOutMSM72xx(); 160 virtual ~AudioStreamOutMSM72xx(); 161 status_t set(AudioHardware* mHardware, 162 uint32_t devices, 163 int *pFormat, 164 uint32_t *pChannels, 165 uint32_t *pRate); sampleRate()166 virtual uint32_t sampleRate() const { return 44100; } 167 // must be 32-bit aligned - driver only seems to like 4800 bufferSize()168 virtual size_t bufferSize() const { return 4800; } channels()169 virtual uint32_t channels() const { return AudioSystem::CHANNEL_OUT_STEREO; } format()170 virtual int format() const { return AudioSystem::PCM_16_BIT; } latency()171 virtual uint32_t latency() const { return (1000*AUDIO_HW_NUM_OUT_BUF*(bufferSize()/frameSize()))/sampleRate()+AUDIO_HW_OUT_LATENCY_MS; } setVolume(float left,float right)172 virtual status_t setVolume(float left, float right) { return INVALID_OPERATION; } 173 virtual ssize_t write(const void* buffer, size_t bytes); 174 virtual status_t standby(); 175 virtual status_t dump(int fd, const Vector<String16>& args); 176 bool checkStandby(); 177 virtual status_t setParameters(const String8& keyValuePairs); 178 virtual String8 getParameters(const String8& keys); devices()179 uint32_t devices() { return mDevices; } 180 181 private: 182 AudioHardware* mHardware; 183 int mFd; 184 int mStartCount; 185 int mRetryCount; 186 bool mStandby; 187 uint32_t mDevices; 188 }; 189 190 class AudioStreamInMSM72xx : public AudioStreamIn { 191 public: 192 enum input_state { 193 AUDIO_INPUT_CLOSED, 194 AUDIO_INPUT_OPENED, 195 AUDIO_INPUT_STARTED 196 }; 197 198 AudioStreamInMSM72xx(); 199 virtual ~AudioStreamInMSM72xx(); 200 status_t set(AudioHardware* mHardware, 201 uint32_t devices, 202 int *pFormat, 203 uint32_t *pChannels, 204 uint32_t *pRate, 205 AudioSystem::audio_in_acoustics acoustics); bufferSize()206 virtual size_t bufferSize() const { return mBufferSize; } channels()207 virtual uint32_t channels() const { return mChannels; } format()208 virtual int format() const { return mFormat; } sampleRate()209 virtual uint32_t sampleRate() const { return mSampleRate; } setGain(float gain)210 virtual status_t setGain(float gain) { return INVALID_OPERATION; } 211 virtual ssize_t read(void* buffer, ssize_t bytes); 212 virtual status_t dump(int fd, const Vector<String16>& args); 213 virtual status_t standby(); 214 virtual status_t setParameters(const String8& keyValuePairs); 215 virtual String8 getParameters(const String8& keys); devices()216 uint32_t devices() { return mDevices; } 217 218 private: 219 AudioHardware* mHardware; 220 int mFd; 221 int mState; 222 int mRetryCount; 223 int mFormat; 224 uint32_t mChannels; 225 uint32_t mSampleRate; 226 size_t mBufferSize; 227 AudioSystem::audio_in_acoustics mAcoustics; 228 uint32_t mDevices; 229 }; 230 231 static const uint32_t inputSamplingRates[]; 232 bool mInit; 233 bool mMicMute; 234 bool mBluetoothNrec; 235 uint32_t mBluetoothId; 236 AudioStreamOutMSM72xx* mOutput; 237 SortedVector <AudioStreamInMSM72xx*> mInputs; 238 239 msm_snd_endpoint *mSndEndpoints; 240 int mNumSndEndpoints; 241 int mCurSndDevice; 242 243 friend class AudioStreamInMSM72xx; 244 Mutex mLock; 245 246 int SND_DEVICE_CURRENT; 247 int SND_DEVICE_HANDSET; 248 int SND_DEVICE_SPEAKER; 249 int SND_DEVICE_HEADSET; 250 int SND_DEVICE_BT; 251 int SND_DEVICE_CARKIT; 252 int SND_DEVICE_TTY_FULL; 253 int SND_DEVICE_TTY_VCO; 254 int SND_DEVICE_TTY_HCO; 255 int SND_DEVICE_NO_MIC_HEADSET; 256 int SND_DEVICE_FM_HEADSET; 257 int SND_DEVICE_HEADSET_AND_SPEAKER; 258 int SND_DEVICE_FM_SPEAKER; 259 int SND_DEVICE_BT_EC_OFF; 260 }; 261 262 // ---------------------------------------------------------------------------- 263 264 }; // namespace android 265 266 #endif // ANDROID_AUDIO_HARDWARE_MSM72XX_H 267