Home | History | Annotate | Download | only in audioflinger
      1 /*
      2 **
      3 ** Copyright 2007, 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_MIXER_H
     19 #define ANDROID_AUDIO_MIXER_H
     20 
     21 #include <stdint.h>
     22 #include <sys/types.h>
     23 
     24 #include <utils/threads.h>
     25 
     26 #include <media/AudioBufferProvider.h>
     27 #include "AudioResampler.h"
     28 
     29 #include <audio_effects/effect_downmix.h>
     30 #include <system/audio.h>
     31 #include <media/nbaio/NBLog.h>
     32 
     33 namespace android {
     34 
     35 // ----------------------------------------------------------------------------
     36 
     37 class AudioMixer
     38 {
     39 public:
     40                             AudioMixer(size_t frameCount, uint32_t sampleRate,
     41                                        uint32_t maxNumTracks = MAX_NUM_TRACKS);
     42 
     43     /*virtual*/             ~AudioMixer();  // non-virtual saves a v-table, restore if sub-classed
     44 
     45 
     46     // This mixer has a hard-coded upper limit of 32 active track inputs.
     47     // Adding support for > 32 tracks would require more than simply changing this value.
     48     static const uint32_t MAX_NUM_TRACKS = 32;
     49     // maximum number of channels supported by the mixer
     50 
     51     // This mixer has a hard-coded upper limit of 2 channels for output.
     52     // There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect.
     53     // Adding support for > 2 channel output would require more than simply changing this value.
     54     static const uint32_t MAX_NUM_CHANNELS = 2;
     55     // maximum number of channels supported for the content
     56     static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = 8;
     57 
     58     static const uint16_t UNITY_GAIN = 0x1000;
     59 
     60     enum { // names
     61 
     62         // track names (MAX_NUM_TRACKS units)
     63         TRACK0          = 0x1000,
     64 
     65         // 0x2000 is unused
     66 
     67         // setParameter targets
     68         TRACK           = 0x3000,
     69         RESAMPLE        = 0x3001,
     70         RAMP_VOLUME     = 0x3002, // ramp to new volume
     71         VOLUME          = 0x3003, // don't ramp
     72 
     73         // set Parameter names
     74         // for target TRACK
     75         CHANNEL_MASK    = 0x4000,
     76         FORMAT          = 0x4001,
     77         MAIN_BUFFER     = 0x4002,
     78         AUX_BUFFER      = 0x4003,
     79         DOWNMIX_TYPE    = 0X4004,
     80         // for target RESAMPLE
     81         SAMPLE_RATE     = 0x4100, // Configure sample rate conversion on this track name;
     82                                   // parameter 'value' is the new sample rate in Hz.
     83                                   // Only creates a sample rate converter the first time that
     84                                   // the track sample rate is different from the mix sample rate.
     85                                   // If the new sample rate is the same as the mix sample rate,
     86                                   // and a sample rate converter already exists,
     87                                   // then the sample rate converter remains present but is a no-op.
     88         RESET           = 0x4101, // Reset sample rate converter without changing sample rate.
     89                                   // This clears out the resampler's input buffer.
     90         REMOVE          = 0x4102, // Remove the sample rate converter on this track name;
     91                                   // the track is restored to the mix sample rate.
     92         // for target RAMP_VOLUME and VOLUME (8 channels max)
     93         VOLUME0         = 0x4200,
     94         VOLUME1         = 0x4201,
     95         AUXLEVEL        = 0x4210,
     96     };
     97 
     98 
     99     // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
    100 
    101     // Allocate a track name.  Returns new track name if successful, -1 on failure.
    102     int         getTrackName(audio_channel_mask_t channelMask, int sessionId);
    103 
    104     // Free an allocated track by name
    105     void        deleteTrackName(int name);
    106 
    107     // Enable or disable an allocated track by name
    108     void        enable(int name);
    109     void        disable(int name);
    110 
    111     void        setParameter(int name, int target, int param, void *value);
    112 
    113     void        setBufferProvider(int name, AudioBufferProvider* bufferProvider);
    114     void        process(int64_t pts);
    115 
    116     uint32_t    trackNames() const { return mTrackNames; }
    117 
    118     size_t      getUnreleasedFrames(int name) const;
    119 
    120 private:
    121 
    122     enum {
    123         NEEDS_CHANNEL_COUNT__MASK   = 0x00000007,
    124         NEEDS_FORMAT__MASK          = 0x000000F0,
    125         NEEDS_MUTE__MASK            = 0x00000100,
    126         NEEDS_RESAMPLE__MASK        = 0x00001000,
    127         NEEDS_AUX__MASK             = 0x00010000,
    128     };
    129 
    130     enum {
    131         NEEDS_CHANNEL_1             = 0x00000000,
    132         NEEDS_CHANNEL_2             = 0x00000001,
    133 
    134         NEEDS_FORMAT_16             = 0x00000010,
    135 
    136         NEEDS_MUTE_DISABLED         = 0x00000000,
    137         NEEDS_MUTE_ENABLED          = 0x00000100,
    138 
    139         NEEDS_RESAMPLE_DISABLED     = 0x00000000,
    140         NEEDS_RESAMPLE_ENABLED      = 0x00001000,
    141 
    142         NEEDS_AUX_DISABLED     = 0x00000000,
    143         NEEDS_AUX_ENABLED      = 0x00010000,
    144     };
    145 
    146     struct state_t;
    147     struct track_t;
    148     class DownmixerBufferProvider;
    149 
    150     typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp,
    151                            int32_t* aux);
    152     static const int BLOCKSIZE = 16; // 4 cache lines
    153 
    154     struct track_t {
    155         uint32_t    needs;
    156 
    157         union {
    158         int16_t     volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point
    159         int32_t     volumeRL;
    160         };
    161 
    162         int32_t     prevVolume[MAX_NUM_CHANNELS];
    163 
    164         // 16-byte boundary
    165 
    166         int32_t     volumeInc[MAX_NUM_CHANNELS];
    167         int32_t     auxInc;
    168         int32_t     prevAuxLevel;
    169 
    170         // 16-byte boundary
    171 
    172         int16_t     auxLevel;       // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
    173         uint16_t    frameCount;
    174 
    175         uint8_t     channelCount;   // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
    176         uint8_t     format;         // always 16
    177         uint16_t    enabled;        // actually bool
    178         audio_channel_mask_t channelMask;
    179 
    180         // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
    181         //  for how the Track buffer provider is wrapped by another one when dowmixing is required
    182         AudioBufferProvider*                bufferProvider;
    183 
    184         // 16-byte boundary
    185 
    186         mutable AudioBufferProvider::Buffer buffer; // 8 bytes
    187 
    188         hook_t      hook;
    189         const void* in;             // current location in buffer
    190 
    191         // 16-byte boundary
    192 
    193         AudioResampler*     resampler;
    194         uint32_t            sampleRate;
    195         int32_t*           mainBuffer;
    196         int32_t*           auxBuffer;
    197 
    198         // 16-byte boundary
    199 
    200         DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes
    201 
    202         int32_t     sessionId;
    203 
    204         int32_t     padding[2];
    205 
    206         // 16-byte boundary
    207 
    208         bool        setResampler(uint32_t sampleRate, uint32_t devSampleRate);
    209         bool        doesResample() const { return resampler != NULL; }
    210         void        resetResampler() { if (resampler != NULL) resampler->reset(); }
    211         void        adjustVolumeRamp(bool aux);
    212         size_t      getUnreleasedFrames() const { return resampler != NULL ?
    213                                                     resampler->getUnreleasedFrames() : 0; };
    214     };
    215 
    216     // pad to 32-bytes to fill cache line
    217     struct state_t {
    218         uint32_t        enabledTracks;
    219         uint32_t        needsChanged;
    220         size_t          frameCount;
    221         void            (*hook)(state_t* state, int64_t pts);   // one of process__*, never NULL
    222         int32_t         *outputTemp;
    223         int32_t         *resampleTemp;
    224         NBLog::Writer*  mLog;
    225         int32_t         reserved[1];
    226         // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
    227         track_t         tracks[MAX_NUM_TRACKS]; __attribute__((aligned(32)));
    228     };
    229 
    230     // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect
    231     class DownmixerBufferProvider : public AudioBufferProvider {
    232     public:
    233         virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
    234         virtual void releaseBuffer(Buffer* buffer);
    235         DownmixerBufferProvider();
    236         virtual ~DownmixerBufferProvider();
    237 
    238         AudioBufferProvider* mTrackBufferProvider;
    239         effect_handle_t    mDownmixHandle;
    240         effect_config_t    mDownmixConfig;
    241     };
    242 
    243     // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
    244     uint32_t        mTrackNames;
    245 
    246     // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
    247     // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
    248     const uint32_t  mConfiguredNames;
    249 
    250     const uint32_t  mSampleRate;
    251 
    252     NBLog::Writer   mDummyLog;
    253 public:
    254     void            setLog(NBLog::Writer* log);
    255 private:
    256     state_t         mState __attribute__((aligned(32)));
    257 
    258     // effect descriptor for the downmixer used by the mixer
    259     static effect_descriptor_t dwnmFxDesc;
    260     // indicates whether a downmix effect has been found and is usable by this mixer
    261     static bool                isMultichannelCapable;
    262 
    263     // Call after changing either the enabled status of a track, or parameters of an enabled track.
    264     // OK to call more often than that, but unnecessary.
    265     void invalidateState(uint32_t mask);
    266 
    267     static status_t initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask);
    268     static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum);
    269     static void unprepareTrackForDownmix(track_t* pTrack, int trackName);
    270 
    271     static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    272             int32_t* aux);
    273     static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
    274     static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    275             int32_t* aux);
    276     static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
    277             int32_t* aux);
    278     static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
    279             int32_t* aux);
    280     static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
    281             int32_t* aux);
    282 
    283     static void process__validate(state_t* state, int64_t pts);
    284     static void process__nop(state_t* state, int64_t pts);
    285     static void process__genericNoResampling(state_t* state, int64_t pts);
    286     static void process__genericResampling(state_t* state, int64_t pts);
    287     static void process__OneTrack16BitsStereoNoResampling(state_t* state,
    288                                                           int64_t pts);
    289 #if 0
    290     static void process__TwoTracks16BitsStereoNoResampling(state_t* state,
    291                                                            int64_t pts);
    292 #endif
    293 
    294     static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
    295                                       int outputFrameIndex);
    296 
    297     static uint64_t         sLocalTimeFreq;
    298     static pthread_once_t   sOnceControl;
    299     static void             sInitRoutine();
    300 };
    301 
    302 // ----------------------------------------------------------------------------
    303 }; // namespace android
    304 
    305 #endif // ANDROID_AUDIO_MIXER_H
    306