Home | History | Annotate | Download | only in libaudio
      1 /*
      2 ** Copyright 2010, 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_POST_PROCESSOR_H
     18 #define ANDROID_AUDIO_POST_PROCESSOR_H
     19 #ifdef USE_PROPRIETARY_AUDIO_EXTENSIONS
     20 
     21 #include <utils/threads.h>
     22 
     23 extern "C" {
     24 #include "cto_audio_mm.h"
     25 }
     26 #include "mot_acoustics.h"
     27 
     28 namespace android_audio_legacy {
     29     using android::Mutex;
     30     using android::AutoMutex;
     31     using android::Condition;
     32     using android::Thread;
     33     using android::sp;
     34 
     35 class AudioPostProcessor
     36 {
     37 public:
     38                         AudioPostProcessor();
     39                         ~AudioPostProcessor();
     40             void        setPlayAudioRate(int rate);
     41             void        setAudioDev(struct cpcap_audio_stream *outDev,
     42                                     struct cpcap_audio_stream *inDev,
     43                                     bool is_bt, bool is_bt_ec, bool is_spdif);
     44             void        doMmProcessing(void * buffer, int numSamples);
     45             int         getEcnsRate(void);
     46 
     47             // voice processing IDs for enableEcns()
     48             enum {
     49                 AEC = 0x1,   // AEC is enabled
     50                 NS = 0x2     // NS is enabled
     51             };
     52             // enable or disable voice processing according to bit field passed
     53             void        enableEcns(int value);
     54             bool        isEcnsEnabled(void) { return (mEcnsEnabled != 0); }
     55             bool        isEcEnabled(void) { return !!(mEcnsEnabled & AEC); }
     56 
     57             int         writeDownlinkEcns(int fd, void * buffer,
     58                                           bool stereo, int bytes, Mutex * fdLockp);
     59             int         read(int fd, void * buffer, int bytes, int rate);
     60             int         applyUplinkEcns(void * buffer, int bytes, int rate);
     61 
     62 private:
     63             void        configMmAudio(void);
     64             uint32_t    convOutDevToCTO(uint32_t outDev);
     65             uint32_t    convRateToCto(uint32_t rate);
     66 
     67             void        initEcns(int rate, int bytes);
     68             void        stopEcns(void);
     69             void        cleanupEcns(void);
     70             void        ecnsLogToRam(int bytes);
     71             void        ecnsLogToFile(void);
     72             int         read_dock_prop(char const *path);
     73 
     74         // CTO Multimedia Audio Processing storage buffers
     75             int16_t     mPcmLoggingBuf[((CTO_AUDIO_MM_DATALOGGING_BUFFER_BLOCK_BYTESIZE)/2)];
     76             uint32_t    mNoiseEst[((CTO_AUDIO_MM_NOISE_EST_BLOCK_BYTESIZE)/4)];
     77             uint16_t    mRuntimeParam[((CTO_AUDIO_MM_RUNTIME_PARAM_BYTESIZE)/2)];
     78             uint16_t    mStaticMem[((CTO_AUDIO_MM_STATICMEM_BLOCK_BYTESIZE)/2)];
     79             uint16_t    mScratchMem[((CTO_AUDIO_MM_SCRATCHMEM_BLOCK_BYTESIZE)/2)];
     80             CTO_AUDIO_MM_ENV_VAR mAudioMmEnvVar;
     81             Mutex       mMmLock;
     82 
     83         // EC/NS configuration etc.
     84             Mutex       mEcnsBufLock;
     85             Condition   mEcnsBufCond;  // Signal to unblock write thread
     86             int         mEcnsEnabled; // Enabled by libaudio
     87             bool        mEcnsRunning; // ECNS module init done by read thread
     88             int         mEcnsRate;
     89             void *      mEcnsScratchBuf;  // holding cell for downlink speech "consumed".
     90             int         mEcnsScratchBufSize;
     91             void *      mEcnsOutBuf;      // buffer from downlink "write()"
     92             int         mEcnsOutBufSize;
     93             int         mEcnsOutBufReadOffset;
     94             int         mEcnsOutFd;       // fd pointing to output driver
     95             Mutex *     mEcnsOutFdLockp;
     96             CTO_AUDIO_USECASES_CTRL mEcnsMode;
     97             char *      mLogBuf[15];
     98             int         mLogOffset;
     99             int         mLogSize;
    100             int         mLogNumPoints;
    101             uint16_t    mLogPoint[15];
    102             int16_t *   mEcnsDlBuf;
    103             int         mEcnsDlBufSize;
    104             bool        mEcnsOutStereo;
    105 
    106         // EC/NS Module memory
    107             T_MOT_MEM_BLOCKS mMemBlocks;
    108             T_MOT_CTRL  mEcnsCtrl;
    109             uint16_t    mStaticMemory_1[API_MOT_STATIC_MEM_WORD16_SIZE];
    110             uint16_t    mMotDatalog[API_MOT_DATALOGGING_MEM_WORD16_SIZE];
    111             uint16_t    mParamTable[AUDIO_PROFILE_PARAMETER_BLOCK_WORD16_SIZE*CTO_AUDIO_USECASE_TOTAL_NUMBER];
    112 
    113         // ECNS Thread
    114             class EcnsThread : public Thread {
    115 public:
    116                         EcnsThread();
    117                         ~EcnsThread();
    118             int         readData(int fd, void * buffer, int bytes, int rate,
    119                                  AudioPostProcessor * pp);
    120             void        broadcastReadCond() { mEcnsReadCond.broadcast(); }
    121 
    122 private:
    123             bool        threadLoop();
    124             Mutex       mEcnsReadLock;
    125             Condition   mEcnsReadCond;  // Signal to unblock read thread
    126             AudioPostProcessor * mProcessor;
    127             void *      mClientBuf;
    128             int         mReadSize;
    129             int16_t *   mReadBuf;
    130             int         mFd;
    131             int         mRate;
    132             bool        mIsRunning;
    133             };
    134             sp <EcnsThread> mEcnsThread;
    135 };
    136 } // namespace android
    137 
    138 #endif // USE_PROPRIETARY_AUDIO_EXTENSIONS
    139 #endif // ANDROID_AUDIO_POST_PROCESSOR_H
    140