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