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