blob: ca77f58f727be8afc6eb9f9f1082b1010e9bcf52 [file] [log] [blame]
Iliyan Malchev4765c432012-06-11 14:36:16 -07001/* AudioHardwareALSA.h
2 **
3 ** Copyright 2008-2010, Wind River Systems
4 ** Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
5 **
6 ** Licensed under the Apache License, Version 2.0 (the "License");
7 ** you may not use this file except in compliance with the License.
8 ** You may obtain a copy of the License at
9 **
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 **
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 */
18
19#ifndef ANDROID_AUDIO_HARDWARE_ALSA_H
20#define ANDROID_AUDIO_HARDWARE_ALSA_H
21
22#include <utils/List.h>
23#include <hardware_legacy/AudioHardwareBase.h>
24
25#include <hardware_legacy/AudioHardwareInterface.h>
26#include <hardware_legacy/AudioSystemLegacy.h>
27#include <system/audio.h>
28#include <hardware/audio.h>
29#include <utils/threads.h>
30#include <AudioUsbALSA.h>
31
32extern "C" {
33 #include <sound/asound.h>
34 #include "alsa_audio.h"
35 #include "msm8960_use_cases.h"
36}
37
38#include <hardware/hardware.h>
39
40namespace android_audio_legacy
41{
42using android::List;
43using android::Mutex;
44class AudioHardwareALSA;
45
46/**
47 * The id of ALSA module
48 */
49#define ALSA_HARDWARE_MODULE_ID "alsa"
50#define ALSA_HARDWARE_NAME "alsa"
51
52#define DEFAULT_SAMPLING_RATE 48000
53#define DEFAULT_CHANNEL_MODE 2
54#define VOICE_SAMPLING_RATE 8000
55#define VOICE_CHANNEL_MODE 1
56#define PLAYBACK_LATENCY 96000
57#define RECORD_LATENCY 96000
58#define VOICE_LATENCY 85333
59#define DEFAULT_BUFFER_SIZE 2048
60#define DEFAULT_IN_BUFFER_SIZE 320
61#define FM_BUFFER_SIZE 1024
62
63#define VOIP_SAMPLING_RATE_8K 8000
64#define VOIP_SAMPLING_RATE_16K 16000
65#define VOIP_DEFAULT_CHANNEL_MODE 1
66#define VOIP_BUFFER_SIZE_8K 320
67#define VOIP_BUFFER_SIZE_16K 640
68#define VOIP_BUFFER_MAX_SIZE VOIP_BUFFER_SIZE_16K
69#define VOIP_PLAYBACK_LATENCY 6400
70#define VOIP_RECORD_LATENCY 6400
71
72#define MODE_IS127 0x2
73#define MODE_4GV_NB 0x3
74#define MODE_4GV_WB 0x4
75#define MODE_AMR 0x5
76#define MODE_AMR_WB 0xD
77#define MODE_PCM 0xC
78
79#define DUALMIC_KEY "dualmic_enabled"
80#define FLUENCE_KEY "fluence"
81#define ANC_KEY "anc_enabled"
82#define TTY_MODE_KEY "tty_mode"
83#define BT_SAMPLERATE_KEY "bt_samplerate"
84#define BTHEADSET_VGS "bt_headset_vgs"
85#define WIDEVOICE_KEY "wide_voice_enable"
86#define VOIPRATE_KEY "voip_rate"
87#define FENS_KEY "fens_enable"
88#define ST_KEY "st_enable"
89#define INCALLMUSIC_KEY "incall_music_enabled"
90
91#define ANC_FLAG 0x00000001
92#define DMIC_FLAG 0x00000002
93#define QMIC_FLAG 0x00000004
94
95#ifdef SSR_ENABLED
96#define SSRQMIC_FLAG 0x00000008
97#endif
98
99#define TTY_OFF 0x00000010
100#define TTY_FULL 0x00000020
101#define TTY_VCO 0x00000040
102#define TTY_HCO 0x00000080
103#define TTY_CLEAR 0xFFFFFF0F
104
105#define LPA_SESSION_ID 1
106#define TUNNEL_SESSION_ID 2
107static int USBPLAYBACKBIT_MUSIC = (1 << 0);
108static int USBPLAYBACKBIT_VOICECALL = (1 << 1);
109static int USBPLAYBACKBIT_VOIPCALL = (1 << 2);
110static int USBPLAYBACKBIT_FM = (1 << 3);
111static int USBPLAYBACKBIT_LPA = (1 << 4);
112
113static int USBRECBIT_REC = (1 << 0);
114static int USBRECBIT_VOICECALL = (1 << 1);
115static int USBRECBIT_VOIPCALL = (1 << 2);
116static int USBRECBIT_FM = (1 << 3);
117
118#define DEVICE_SPEAKER_HEADSET "Speaker Headset"
119#define DEVICE_HEADSET "Headset"
120#define DEVICE_HEADPHONES "Headphones"
121
122#ifdef SSR_ENABLED
123#define COEFF_ARRAY_SIZE 4
124#define FILT_SIZE ((512+1)* 6) /* # ((FFT bins)/2+1)*numOutputs */
125#define SSR_FRAME_SIZE 512
126#define SSR_INPUT_FRAME_SIZE (SSR_FRAME_SIZE * 4)
127#define SSR_OUTPUT_FRAME_SIZE (SSR_FRAME_SIZE * 6)
128#endif
129
130#define MODE_CALL_KEY "CALL_KEY"
131
132struct alsa_device_t;
133static uint32_t FLUENCE_MODE_ENDFIRE = 0;
134static uint32_t FLUENCE_MODE_BROADSIDE = 1;
135
136enum {
137 INCALL_REC_MONO,
138 INCALL_REC_STEREO,
139};
140
141struct alsa_handle_t {
142 alsa_device_t * module;
143 uint32_t devices;
144 char useCase[MAX_STR_LEN];
145 struct pcm * handle;
146 snd_pcm_format_t format;
147 uint32_t channels;
148 uint32_t sampleRate;
149 unsigned int latency; // Delay in usec
150 unsigned int bufferSize; // Size of sample buffer
151 unsigned int periodSize;
152 struct pcm * rxHandle;
153 snd_use_case_mgr_t *ucMgr;
154};
155
156typedef List < alsa_handle_t > ALSAHandleList;
157
158struct use_case_t {
159 char useCase[MAX_STR_LEN];
160};
161
162typedef List < use_case_t > ALSAUseCaseList;
163
164struct alsa_device_t {
165 hw_device_t common;
166
167 status_t (*init)(alsa_device_t *, ALSAHandleList &);
168 status_t (*open)(alsa_handle_t *);
169 status_t (*close)(alsa_handle_t *);
170 status_t (*standby)(alsa_handle_t *);
171 status_t (*route)(alsa_handle_t *, uint32_t, int);
172 status_t (*startVoiceCall)(alsa_handle_t *);
173 status_t (*startVoipCall)(alsa_handle_t *);
174 status_t (*startFm)(alsa_handle_t *);
175 void (*setVoiceVolume)(int);
176 void (*setVoipVolume)(int);
177 void (*setMicMute)(int);
178 void (*setVoipMicMute)(int);
179 void (*setVoipConfig)(int, int);
180 status_t (*setFmVolume)(int);
181 void (*setBtscoRate)(int);
182 status_t (*setLpaVolume)(int);
183 void (*enableWideVoice)(bool);
184 void (*enableFENS)(bool);
185 void (*setFlags)(uint32_t);
186 status_t (*setCompressedVolume)(int);
187 void (*enableSlowTalk)(bool);
188 void (*setVocRecMode)(uint8_t);
189 void (*setVoLTEMicMute)(int);
190 void (*setVoLTEVolume)(int);
191};
192
193// ----------------------------------------------------------------------------
194
195class ALSAMixer
196{
197public:
198 ALSAMixer();
199 virtual ~ALSAMixer();
200
201 bool isValid() { return 1;}
202 status_t setMasterVolume(float volume);
203 status_t setMasterGain(float gain);
204
205 status_t setVolume(uint32_t device, float left, float right);
206 status_t setGain(uint32_t device, float gain);
207
208 status_t setCaptureMuteState(uint32_t device, bool state);
209 status_t getCaptureMuteState(uint32_t device, bool *state);
210 status_t setPlaybackMuteState(uint32_t device, bool state);
211 status_t getPlaybackMuteState(uint32_t device, bool *state);
212
213};
214
215class ALSAControl
216{
217public:
218 ALSAControl(const char *device = "/dev/snd/controlC0");
219 virtual ~ALSAControl();
220
221 status_t get(const char *name, unsigned int &value, int index = 0);
222 status_t set(const char *name, unsigned int value, int index = -1);
223 status_t set(const char *name, const char *);
224 status_t setext(const char *name, int count, char **setValues);
225
226private:
227 struct mixer* mHandle;
228};
229
230class ALSAStreamOps
231{
232public:
233 ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle);
234 virtual ~ALSAStreamOps();
235
236 status_t set(int *format, uint32_t *channels, uint32_t *rate, uint32_t device);
237
238 status_t setParameters(const String8& keyValuePairs);
239 String8 getParameters(const String8& keys);
240
241 uint32_t sampleRate() const;
242 size_t bufferSize() const;
243 int format() const;
244 uint32_t channels() const;
245
246 status_t open(int mode);
247 void close();
248
249protected:
250 friend class AudioHardwareALSA;
251
252 AudioHardwareALSA * mParent;
253 alsa_handle_t * mHandle;
254 uint32_t mDevices;
255};
256
257// ----------------------------------------------------------------------------
258
259class AudioStreamOutALSA : public AudioStreamOut, public ALSAStreamOps
260{
261public:
262 AudioStreamOutALSA(AudioHardwareALSA *parent, alsa_handle_t *handle);
263 virtual ~AudioStreamOutALSA();
264
265 virtual uint32_t sampleRate() const
266 {
267 return ALSAStreamOps::sampleRate();
268 }
269
270 virtual size_t bufferSize() const
271 {
272 return ALSAStreamOps::bufferSize();
273 }
274
275 virtual uint32_t channels() const;
276
277 virtual int format() const
278 {
279 return ALSAStreamOps::format();
280 }
281
282 virtual uint32_t latency() const;
283
284 virtual ssize_t write(const void *buffer, size_t bytes);
285 virtual status_t dump(int fd, const Vector<String16>& args);
286
287 status_t setVolume(float left, float right);
288
289 virtual status_t standby();
290
291 virtual status_t setParameters(const String8& keyValuePairs) {
292 return ALSAStreamOps::setParameters(keyValuePairs);
293 }
294
295 virtual String8 getParameters(const String8& keys) {
296 return ALSAStreamOps::getParameters(keys);
297 }
298
299 // return the number of audio frames written by the audio dsp to DAC since
300 // the output has exited standby
301 virtual status_t getRenderPosition(uint32_t *dspFrames);
302
303 status_t open(int mode);
304 status_t close();
305
306private:
307 uint32_t mFrameCount;
308
309protected:
310 AudioHardwareALSA * mParent;
311};
312
313class AudioStreamInALSA : public AudioStreamIn, public ALSAStreamOps
314{
315public:
316 AudioStreamInALSA(AudioHardwareALSA *parent,
317 alsa_handle_t *handle,
318 AudioSystem::audio_in_acoustics audio_acoustics);
319 virtual ~AudioStreamInALSA();
320
321 virtual uint32_t sampleRate() const
322 {
323 return ALSAStreamOps::sampleRate();
324 }
325
326 virtual size_t bufferSize() const
327 {
328 return ALSAStreamOps::bufferSize();
329 }
330
331 virtual uint32_t channels() const
332 {
333 return ALSAStreamOps::channels();
334 }
335
336 virtual int format() const
337 {
338 return ALSAStreamOps::format();
339 }
340
341 virtual ssize_t read(void* buffer, ssize_t bytes);
342 virtual status_t dump(int fd, const Vector<String16>& args);
343
344 virtual status_t setGain(float gain);
345
346 virtual status_t standby();
347
348 virtual status_t setParameters(const String8& keyValuePairs)
349 {
350 return ALSAStreamOps::setParameters(keyValuePairs);
351 }
352
353 virtual String8 getParameters(const String8& keys)
354 {
355 return ALSAStreamOps::getParameters(keys);
356 }
357
358 // Return the amount of input frames lost in the audio driver since the last call of this function.
359 // Audio driver is expected to reset the value to 0 and restart counting upon returning the current value by this function call.
360 // Such loss typically occurs when the user space process is blocked longer than the capacity of audio driver buffers.
361 // Unit: the number of input audio frames
362 virtual unsigned int getInputFramesLost() const;
363
364 virtual status_t addAudioEffect(effect_handle_t effect)
365 {
366 return BAD_VALUE;
367 }
368
369 virtual status_t removeAudioEffect(effect_handle_t effect)
370 {
371 return BAD_VALUE;
372 }
373 status_t setAcousticParams(void* params);
374
375 status_t open(int mode);
376 status_t close();
377#ifdef SSR_ENABLED
378 // Helper function to initialize the Surround Sound library.
379 status_t initSurroundSoundLibrary(unsigned long buffersize);
380#endif
381
382private:
383 void resetFramesLost();
384
385 unsigned int mFramesLost;
386 AudioSystem::audio_in_acoustics mAcoustics;
387
388#ifdef SSR_ENABLED
389 // Function to read coefficients from files.
390 status_t readCoeffsFromFile();
391
392 FILE *mFp_4ch;
393 FILE *mFp_6ch;
394 int16_t **mRealCoeffs;
395 int16_t **mImagCoeffs;
396 void *mSurroundObj;
397
398 int16_t *mSurroundInputBuffer;
399 int16_t *mSurroundOutputBuffer;
400 int mSurroundInputBufferIdx;
401 int mSurroundOutputBufferIdx;
402#endif
403
404protected:
405 AudioHardwareALSA * mParent;
406};
407
408class AudioHardwareALSA : public AudioHardwareBase
409{
410public:
411 AudioHardwareALSA();
412 virtual ~AudioHardwareALSA();
413
414 /**
415 * check to see if the audio hardware interface has been initialized.
416 * return status based on values defined in include/utils/Errors.h
417 */
418 virtual status_t initCheck();
419
420 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
421 virtual status_t setVoiceVolume(float volume);
422
423 /**
424 * set the audio volume for all audio activities other than voice call.
425 * Range between 0.0 and 1.0. If any value other than NO_ERROR is returned,
426 * the software mixer will emulate this capability.
427 */
428 virtual status_t setMasterVolume(float volume);
429#ifdef FM_ENABLED
430 virtual status_t setFmVolume(float volume);
431#endif
432 /**
433 * setMode is called when the audio mode changes. NORMAL mode is for
434 * standard audio playback, RINGTONE when a ringtone is playing, and IN_CALL
435 * when a call is in progress.
436 */
437 virtual status_t setMode(int mode);
438
439 // mic mute
440 virtual status_t setMicMute(bool state);
441 virtual status_t getMicMute(bool* state);
442
443 // set/get global audio parameters
444 virtual status_t setParameters(const String8& keyValuePairs);
445 virtual String8 getParameters(const String8& keys);
446
447 // Returns audio input buffer size according to parameters passed or 0 if one of the
448 // parameters is not supported
449 virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channels);
450
451 /** This method creates and opens the audio hardware output
452 * session for LPA */
453 virtual AudioStreamOut* openOutputSession(
454 uint32_t devices,
455 int *format,
456 status_t *status,
457 int sessionId,
458 uint32_t samplingRate=0,
459 uint32_t channels=0);
460 virtual void closeOutputSession(AudioStreamOut* out);
461
462 /** This method creates and opens the audio hardware output stream */
463 virtual AudioStreamOut* openOutputStream(
464 uint32_t devices,
465 int *format=0,
466 uint32_t *channels=0,
467 uint32_t *sampleRate=0,
468 status_t *status=0);
469 virtual void closeOutputStream(AudioStreamOut* out);
470
471 /** This method creates and opens the audio hardware input stream */
472 virtual AudioStreamIn* openInputStream(
473 uint32_t devices,
474 int *format,
475 uint32_t *channels,
476 uint32_t *sampleRate,
477 status_t *status,
478 AudioSystem::audio_in_acoustics acoustics);
479 virtual void closeInputStream(AudioStreamIn* in);
480
481 /**This method dumps the state of the audio hardware */
482 //virtual status_t dumpState(int fd, const Vector<String16>& args);
483
484 static AudioHardwareInterface* create();
485
486 int mode()
487 {
488 return mMode;
489 }
490
491protected:
492 virtual status_t dump(int fd, const Vector<String16>& args);
493 virtual uint32_t getVoipMode(int format);
494 void doRouting(int device);
495#ifdef EM_ENABLED
496 void handleFm(int device);
497#endif
498 void closeUSBPlayback();
499 void closeUSBRecording();
500 void closeUsbRecordingIfNothingActive();
501 void closeUsbPlaybackIfNothingActive();
502 void startUsbPlaybackIfNotStarted();
503 void startUsbRecordingIfNotStarted();
504
505 void disableVoiceCall(char* verb, char* modifier, int mode, int device);
506 void enableVoiceCall(char* verb, char* modifier, int mode, int device);
507 bool routeVoiceCall(int device, int newMode);
508 bool routeVoLTECall(int device, int newMode);
509 friend class AudioStreamOutALSA;
510 friend class AudioStreamInALSA;
511 friend class ALSAStreamOps;
512
513 alsa_device_t * mALSADevice;
514
515 ALSAHandleList mDeviceList;
516
517 AudioUsbALSA *mAudioUsbALSA;
518
519 Mutex mLock;
520
521 snd_use_case_mgr_t *mUcMgr;
522
523 uint32_t mCurDevice;
524 /* The flag holds all the audio related device settings from
525 * Settings and Qualcomm Settings applications */
526 uint32_t mDevSettingsFlag;
527 uint32_t mVoipStreamCount;
528 bool mVoipMicMute;
529 uint32_t mVoipBitRate;
530 uint32_t mIncallMode;
531
532 bool mMicMute;
533 int mCSCallActive;
534 int mVolteCallActive;
535 int mCallState;
536 int mIsFmActive;
537 bool mBluetoothVGS;
538 bool mFusion3Platform;
539
540 int musbPlaybackState;
541 int musbRecordingState;
542};
543
544// ----------------------------------------------------------------------------
545
546}; // namespace android_audio_legacy
547#endif // ANDROID_AUDIO_HARDWARE_ALSA_H