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