Home | History | Annotate | Download | only in libaudio
      1 /*
      2 **
      3 ** Copyright 2011, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #ifndef ANDROID_AUDIO_OUTPUT_H
     19 #define ANDROID_AUDIO_OUTPUT_H
     20 
     21 #include <semaphore.h>
     22 #include <tinyalsa/asoundlib.h>
     23 #include <utils/LinearTransform.h>
     24 #include <utils/String16.h>
     25 #include <utils/String8.h>
     26 #include <utils/threads.h>
     27 #include <utils/Vector.h>
     28 
     29 namespace android {
     30 
     31 class AudioStreamOut;
     32 
     33 class AudioOutput : public RefBase {
     34   public:
     35 
     36     // Audio ouput state machine states.
     37     enum State {
     38         // Ouput not yet started or synchronized.
     39         OUT_OF_SYNC,
     40 
     41         // Silence primed to output to start DMA.
     42         PRIMED,
     43 
     44         // DMA started, ready to align to other inputs.
     45         DMA_START,
     46 
     47         // DMA active.
     48         ACTIVE,
     49 
     50         // Fatal, unrecoverable error.
     51         FATAL,
     52     };
     53 
     54                         AudioOutput(const char* alsa_name,
     55                                     enum pcm_format alsa_pcm_format);
     56     virtual            ~AudioOutput();
     57 
     58     virtual status_t    initCheck();
     59     virtual status_t    setupForStream(const AudioStreamOut& stream) = 0;
     60 
     61     // State machine transition functions.
     62     State               getState() { return mState; };
     63     bool                hasFatalError() { return mState == FATAL; }
     64 
     65     // Prime data to output device, go to PRIMED state.
     66     void                primeOutput(bool hasActiveOutputs);
     67 
     68     // Adjust for write timestamp difference, go to ACTIVE state.
     69     void                adjustDelay(int32_t nFrames);
     70 
     71     // Send one chunk of data to ALSA, if state machine permits. This is called
     72     // for every chunk sent down, regardless of the state of the output.
     73     void                processOneChunk(const uint8_t* data, size_t len,
     74                                         bool hasActiveOutputs);
     75 
     76     status_t            getNextWriteTimestamp(int64_t* timestamp,
     77                                               bool* discon);
     78     bool                getLastNextWriteTSValid() const;
     79     int64_t             getLastNextWriteTS() const;
     80 
     81     uint32_t            getExternalDelay_uSec() const;
     82     void                setExternalDelay_uSec(uint32_t delay);
     83     void                setDelayComp_uSec(uint32_t delay_usec);
     84 
     85     void                setVolume(float vol);
     86     void                setMute(bool mute);
     87     void                setOutputIsFixed(bool fixed);
     88     void                setFixedOutputLevel(float level);
     89 
     90     float               getVolume()           const { return mVolume; }
     91     bool                getMute()             const { return mMute; }
     92     bool                getOutputIsFixed()    const { return mOutputFixed; }
     93     float               getFixedOutputLevel() const { return mFixedLvl; }
     94 
     95     int                 getHardwareTimestamp(unsigned int *pAvail,
     96                                 struct timespec *pTimestamp);
     97     uint32_t            getKernelBufferSize() { return mFramesPerChunk * mBufferChunks; }
     98 
     99     virtual void        dump(String8& result) = 0;
    100 
    101     virtual const char* getOutputName() = 0;
    102     virtual uint32_t    devMask() const = 0;
    103 
    104     virtual void        cleanupResources();
    105 
    106     static const uint32_t kMaxDelayCompensationMSec;
    107     static const uint32_t kPrimeTimeoutChunks;
    108 
    109   protected:
    110 
    111     void                pushSilence(uint32_t nFrames);
    112     // Take nBytes of chunkData, convert to output format and write at
    113     // sbuf. sbuf WILL point to enough space to convert from 16 to 32 bit
    114     // if needed.
    115     virtual void        stageChunk(const uint8_t* chunkData,
    116                                    uint8_t* sbuf,
    117                                    uint32_t inBytesPerSample,
    118                                    uint32_t nSamples);
    119     virtual void        openPCMDevice();
    120     virtual void        reset();
    121     virtual status_t    getDMAStartData(int64_t* dma_start_time,
    122                                         int64_t* frames_queued_to_driver);
    123     void                doPCMWrite(const uint8_t* data, size_t len);
    124     void                setupInternal();
    125 
    126     // Current state machine state.
    127     State               mState;
    128 
    129     // Output format
    130     uint32_t            mFramesPerChunk;
    131     uint32_t            mFramesPerSec;
    132     uint32_t            mBufferChunks;
    133     uint32_t            mChannelCnt;
    134     const char*         mALSAName;
    135     enum pcm_format     mALSAFormat;
    136 
    137     // These numbers are relative to the ALSA output.
    138     uint32_t            mBytesPerSample;
    139     uint32_t            mBytesPerFrame;
    140     uint32_t            mBytesPerChunk;
    141     uint8_t*            mStagingBuf;
    142 
    143     // Get next write time stuff.
    144     bool                mLastNextWriteTimeValid;
    145     int64_t             mLastNextWriteTime;
    146     int64_t             mLastDMAStartTime;
    147 
    148     // External delay compensation.
    149     uint32_t            mMaxDelayCompFrames;
    150     uint32_t            mExternalDelayUSec;
    151     uint32_t            mExternalDelayLocalTicks;
    152 
    153     LinearTransform     mFramesToLocalTime;
    154 
    155     // ALSA device stuff.
    156     Mutex               mDeviceLock;
    157     struct pcm*         mDevice;
    158     int                 mDeviceExtFd;
    159     int                 mALSACardID;
    160     uint64_t            mFramesQueuedToDriver;
    161     uint32_t            mPrimeTimeoutChunks;
    162 
    163     // Volume stuff
    164     Mutex               mVolumeLock;
    165     float               mVolume;
    166     float               mFixedLvl;
    167     bool                mMute;
    168     bool                mOutputFixed;
    169     bool                mVolParamsDirty;
    170     virtual void        applyPendingVolParams() = 0;
    171 };
    172 
    173 typedef Vector< sp<AudioOutput> > AudioOutputList;
    174 
    175 }  // namespace android
    176 #endif  // ANDROID_AUDIO_OUTPUT_H
    177