Home | History | Annotate | Download | only in libaudio
      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 <media/mediarecorder.h>
     28 
     29 #include "secril-client.h"
     30 
     31 extern "C" {
     32     struct pcm;
     33     struct mixer;
     34     struct mixer_ctl;
     35 };
     36 
     37 namespace android {
     38 
     39 // TODO: determine actual audio DSP and hardware latency
     40 // Additionnal latency introduced by audio DSP and hardware in ms
     41 #define AUDIO_HW_OUT_LATENCY_MS 0
     42 // Default audio output sample rate
     43 #define AUDIO_HW_OUT_SAMPLERATE 44100
     44 // Default audio output channel mask
     45 #define AUDIO_HW_OUT_CHANNELS (AudioSystem::CHANNEL_OUT_STEREO)
     46 // Default audio output sample format
     47 #define AUDIO_HW_OUT_FORMAT (AudioSystem::PCM_16_BIT)
     48 // Kernel pcm out buffer size in frames at 44.1kHz
     49 #define AUDIO_HW_OUT_PERIOD_MULT 8 // (8 * 128 = 1024 frames)
     50 #define AUDIO_HW_OUT_PERIOD_SZ (PCM_PERIOD_SZ_MIN * AUDIO_HW_OUT_PERIOD_MULT)
     51 #define AUDIO_HW_OUT_PERIOD_CNT 4
     52 // Default audio output buffer size in bytes
     53 #define AUDIO_HW_OUT_PERIOD_BYTES (AUDIO_HW_OUT_PERIOD_SZ * 2 * sizeof(int16_t))
     54 
     55 // Default audio input sample rate
     56 #define AUDIO_HW_IN_SAMPLERATE 8000
     57 // Default audio input channel mask
     58 #define AUDIO_HW_IN_CHANNELS (AudioSystem::CHANNEL_IN_MONO)
     59 // Default audio input sample format
     60 #define AUDIO_HW_IN_FORMAT (AudioSystem::PCM_16_BIT)
     61 // Number of buffers in audio driver for input
     62 #define AUDIO_HW_NUM_IN_BUF 2
     63 // Kernel pcm in buffer size in frames at 44.1kHz (before resampling)
     64 #define AUDIO_HW_IN_PERIOD_MULT 16  // (16 * 128 = 2048 frames)
     65 #define AUDIO_HW_IN_PERIOD_SZ (PCM_PERIOD_SZ_MIN * AUDIO_HW_IN_PERIOD_MULT)
     66 #define AUDIO_HW_IN_PERIOD_CNT 2
     67 // Default audio input buffer size in bytes (8kHz mono)
     68 #define AUDIO_HW_IN_PERIOD_BYTES ((AUDIO_HW_IN_PERIOD_SZ*sizeof(int16_t))/8)
     69 
     70 
     71 class AudioHardware : public AudioHardwareBase
     72 {
     73     class AudioStreamOutALSA;
     74     class AudioStreamInALSA;
     75 public:
     76 
     77     // input path names used to translate from input sources to driver paths
     78     static const char *inputPathNameDefault;
     79     static const char *inputPathNameCamcorder;
     80     static const char *inputPathNameVoiceRecognition;
     81     static const char *inputPathNameVoiceCommunication;
     82 
     83     AudioHardware();
     84     virtual ~AudioHardware();
     85     virtual status_t initCheck();
     86 
     87     virtual status_t setVoiceVolume(float volume);
     88     virtual status_t setMasterVolume(float volume);
     89 
     90     virtual status_t setMode(int mode);
     91 
     92     virtual status_t setMicMute(bool state);
     93     virtual status_t getMicMute(bool* state);
     94 
     95     virtual status_t setParameters(const String8& keyValuePairs);
     96     virtual String8 getParameters(const String8& keys);
     97 
     98     virtual AudioStreamOut* openOutputStream(
     99         uint32_t devices, int *format=0, uint32_t *channels=0,
    100         uint32_t *sampleRate=0, status_t *status=0);
    101 
    102     virtual AudioStreamIn* openInputStream(
    103         uint32_t devices, int *format, uint32_t *channels,
    104         uint32_t *sampleRate, status_t *status,
    105         AudioSystem::audio_in_acoustics acoustics);
    106 
    107     virtual void closeOutputStream(AudioStreamOut* out);
    108     virtual void closeInputStream(AudioStreamIn* in);
    109 
    110     virtual size_t getInputBufferSize(
    111         uint32_t sampleRate, int format, int channelCount);
    112 
    113             int  mode() { return mMode; }
    114             const char *getOutputRouteFromDevice(uint32_t device);
    115             const char *getInputRouteFromDevice(uint32_t device);
    116             const char *getVoiceRouteFromDevice(uint32_t device);
    117 
    118             status_t setIncallPath_l(uint32_t device);
    119 
    120             status_t setInputSource_l(audio_source source);
    121 
    122             void setVoiceVolume_l(float volume);
    123 
    124     static uint32_t    getInputSampleRate(uint32_t sampleRate);
    125            sp <AudioStreamInALSA> getActiveInput_l();
    126 
    127            Mutex& lock() { return mLock; }
    128 
    129            struct pcm *openPcmOut_l();
    130            void closePcmOut_l();
    131 
    132            struct mixer *openMixer_l();
    133            void closeMixer_l();
    134 
    135            sp <AudioStreamOutALSA>  output() { return mOutput; }
    136 
    137 protected:
    138     virtual status_t dump(int fd, const Vector<String16>& args);
    139 
    140 private:
    141 
    142     enum tty_modes {
    143         TTY_MODE_OFF,
    144         TTY_MODE_VCO,
    145         TTY_MODE_HCO,
    146         TTY_MODE_FULL
    147     };
    148 
    149     bool            mInit;
    150     bool            mMicMute;
    151     sp <AudioStreamOutALSA>                 mOutput;
    152     SortedVector < sp<AudioStreamInALSA> >   mInputs;
    153     Mutex           mLock;
    154     struct pcm*     mPcm;
    155     struct mixer*   mMixer;
    156     uint32_t        mPcmOpenCnt;
    157     uint32_t        mMixerOpenCnt;
    158     bool            mInCallAudioMode;
    159     float           mVoiceVol;
    160 
    161     audio_source    mInputSource;
    162     bool            mBluetoothNrec;
    163     int             mTTYMode;
    164 
    165     void*           mSecRilLibHandle;
    166     HRilClient      mRilClient;
    167     bool            mActivatedCP;
    168     HRilClient      (*openClientRILD)  (void);
    169     int             (*disconnectRILD)  (HRilClient);
    170     int             (*closeClientRILD) (HRilClient);
    171     int             (*isConnectedRILD) (HRilClient);
    172     int             (*connectRILD)     (HRilClient);
    173     int             (*setCallVolume)   (HRilClient, SoundType, int);
    174     int             (*setCallAudioPath)(HRilClient, AudioPath);
    175     int             (*setCallClockSync)(HRilClient, SoundClockCondition);
    176     void            loadRILD(void);
    177     status_t        connectRILDIfRequired(void);
    178 
    179     //  trace driver operations for dump
    180     int             mDriverOp;
    181 
    182     static uint32_t         checkInputSampleRate(uint32_t sampleRate);
    183     static const uint32_t   inputSamplingRates[];
    184 
    185     class AudioStreamOutALSA : public AudioStreamOut, public RefBase
    186     {
    187     public:
    188         AudioStreamOutALSA();
    189         virtual ~AudioStreamOutALSA();
    190         status_t set(AudioHardware* mHardware,
    191                      uint32_t devices,
    192                      int *pFormat,
    193                      uint32_t *pChannels,
    194                      uint32_t *pRate);
    195         virtual uint32_t sampleRate()
    196             const { return mSampleRate; }
    197         virtual size_t bufferSize()
    198             const { return mBufferSize; }
    199         virtual uint32_t channels()
    200             const { return mChannels; }
    201         virtual int format()
    202             const { return AUDIO_HW_OUT_FORMAT; }
    203         virtual uint32_t latency()
    204             const { return (1000 * AUDIO_HW_OUT_PERIOD_CNT *
    205                             (bufferSize()/frameSize()))/sampleRate() +
    206                 AUDIO_HW_OUT_LATENCY_MS; }
    207         virtual status_t setVolume(float left, float right)
    208         { return INVALID_OPERATION; }
    209         virtual ssize_t write(const void* buffer, size_t bytes);
    210         virtual status_t standby();
    211                 bool checkStandby();
    212 
    213         virtual status_t dump(int fd, const Vector<String16>& args);
    214         virtual status_t setParameters(const String8& keyValuePairs);
    215         virtual String8 getParameters(const String8& keys);
    216         uint32_t device() { return mDevices; }
    217         virtual status_t getRenderPosition(uint32_t *dspFrames);
    218 
    219                 void doStandby_l();
    220                 void close_l();
    221                 status_t open_l();
    222                 int standbyCnt() { return mStandbyCnt; }
    223 
    224                 int prepareLock();
    225                 void lock();
    226                 void unlock();
    227 
    228     private:
    229 
    230         Mutex mLock;
    231         AudioHardware* mHardware;
    232         struct pcm *mPcm;
    233         struct mixer *mMixer;
    234         struct mixer_ctl *mRouteCtl;
    235         const char *next_route;
    236         bool mStandby;
    237         uint32_t mDevices;
    238         uint32_t mChannels;
    239         uint32_t mSampleRate;
    240         size_t mBufferSize;
    241         //  trace driver operations for dump
    242         int mDriverOp;
    243         int mStandbyCnt;
    244         bool mSleepReq;
    245     };
    246 
    247     class DownSampler;
    248 
    249     class BufferProvider
    250     {
    251     public:
    252 
    253         struct Buffer {
    254             union {
    255                 void*       raw;
    256                 short*      i16;
    257                 int8_t*     i8;
    258             };
    259             size_t frameCount;
    260         };
    261 
    262         virtual ~BufferProvider() {}
    263 
    264         virtual status_t getNextBuffer(Buffer* buffer) = 0;
    265         virtual void releaseBuffer(Buffer* buffer) = 0;
    266     };
    267 
    268     class DownSampler {
    269     public:
    270         DownSampler(uint32_t outSampleRate,
    271                   uint32_t channelCount,
    272                   uint32_t frameCount,
    273                   BufferProvider* provider);
    274 
    275         virtual ~DownSampler();
    276 
    277                 void reset();
    278                 status_t initCheck() { return mStatus; }
    279                 int resample(int16_t* out, size_t *outFrameCount);
    280 
    281     private:
    282         status_t    mStatus;
    283         BufferProvider* mProvider;
    284         uint32_t mSampleRate;
    285         uint32_t mChannelCount;
    286         uint32_t mFrameCount;
    287         int16_t *mInLeft;
    288         int16_t *mInRight;
    289         int16_t *mTmpLeft;
    290         int16_t *mTmpRight;
    291         int16_t *mTmp2Left;
    292         int16_t *mTmp2Right;
    293         int16_t *mOutLeft;
    294         int16_t *mOutRight;
    295         int mInInBuf;
    296         int mInTmpBuf;
    297         int mInTmp2Buf;
    298         int mOutBufPos;
    299         int mInOutBuf;
    300     };
    301 
    302 
    303     class AudioStreamInALSA : public AudioStreamIn, public BufferProvider, public RefBase
    304     {
    305 
    306      public:
    307                     AudioStreamInALSA();
    308         virtual     ~AudioStreamInALSA();
    309         status_t    set(AudioHardware* hw,
    310                     uint32_t devices,
    311                     int *pFormat,
    312                     uint32_t *pChannels,
    313                     uint32_t *pRate,
    314                     AudioSystem::audio_in_acoustics acoustics);
    315         virtual size_t bufferSize() const { return mBufferSize; }
    316         virtual uint32_t channels() const { return mChannels; }
    317         virtual int format() const { return AUDIO_HW_IN_FORMAT; }
    318         virtual uint32_t sampleRate() const { return mSampleRate; }
    319         virtual status_t setGain(float gain) { return INVALID_OPERATION; }
    320         virtual ssize_t read(void* buffer, ssize_t bytes);
    321         virtual status_t dump(int fd, const Vector<String16>& args);
    322         virtual status_t standby();
    323                 bool checkStandby();
    324         virtual status_t setParameters(const String8& keyValuePairs);
    325         virtual String8 getParameters(const String8& keys);
    326         virtual unsigned int getInputFramesLost() const { return 0; }
    327                 uint32_t device() { return mDevices; }
    328                 void doStandby_l();
    329                 void close_l();
    330                 status_t open_l();
    331                 int standbyCnt() { return mStandbyCnt; }
    332 
    333         static size_t getBufferSize(uint32_t sampleRate, int channelCount);
    334 
    335         // BufferProvider
    336         virtual status_t getNextBuffer(BufferProvider::Buffer* buffer);
    337         virtual void releaseBuffer(BufferProvider::Buffer* buffer);
    338 
    339         int prepareLock();
    340         void lock();
    341         void unlock();
    342 
    343     private:
    344         Mutex mLock;
    345         AudioHardware* mHardware;
    346         struct pcm *mPcm;
    347         struct mixer *mMixer;
    348         struct mixer_ctl *mRouteCtl;
    349         const char *next_route;
    350         bool mStandby;
    351         uint32_t mDevices;
    352         uint32_t mChannels;
    353         uint32_t mChannelCount;
    354         uint32_t mSampleRate;
    355         size_t mBufferSize;
    356         DownSampler *mDownSampler;
    357         status_t mReadStatus;
    358         size_t mInPcmInBuf;
    359         int16_t *mPcmIn;
    360         //  trace driver operations for dump
    361         int mDriverOp;
    362         int mStandbyCnt;
    363         bool mSleepReq;
    364     };
    365 
    366 };
    367 
    368 }; // namespace android
    369 
    370 #endif
    371