• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <pthread.h>
22 #include <sstream>
23 #include <stdint.h>
24 #include <sys/types.h>
25 #include <unordered_map>
26 
27 #include <media/AudioBufferProvider.h>
28 #include <media/AudioResampler.h>
29 #include <media/AudioResamplerPublic.h>
30 #include <media/BufferProviders.h>
31 #include <media/nblog/NBLog.h>
32 #include <system/audio.h>
33 #include <utils/Compat.h>
34 #include <utils/threads.h>
35 
36 // FIXME This is actually unity gain, which might not be max in future, expressed in U.12
37 #define MAX_GAIN_INT AudioMixer::UNITY_GAIN_INT
38 
39 // This must match frameworks/av/services/audioflinger/Configuration.h
40 #define FLOAT_AUX
41 
42 namespace android {
43 
44 // ----------------------------------------------------------------------------
45 
46 class AudioMixer
47 {
48 public:
49     // Do not change these unless underlying code changes.
50     // This mixer has a hard-coded upper limit of 8 channels for output.
51     static constexpr uint32_t MAX_NUM_CHANNELS = FCC_8;
52     static constexpr uint32_t MAX_NUM_VOLUMES = FCC_2; // stereo volume only
53     // maximum number of channels supported for the content
54     static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = AUDIO_CHANNEL_COUNT_MAX;
55 
56     static const uint16_t UNITY_GAIN_INT = 0x1000;
57     static const CONSTEXPR float UNITY_GAIN_FLOAT = 1.0f;
58 
59     enum { // names
60         // setParameter targets
61         TRACK           = 0x3000,
62         RESAMPLE        = 0x3001,
63         RAMP_VOLUME     = 0x3002, // ramp to new volume
64         VOLUME          = 0x3003, // don't ramp
65         TIMESTRETCH     = 0x3004,
66 
67         // set Parameter names
68         // for target TRACK
69         CHANNEL_MASK    = 0x4000,
70         FORMAT          = 0x4001,
71         MAIN_BUFFER     = 0x4002,
72         AUX_BUFFER      = 0x4003,
73         DOWNMIX_TYPE    = 0X4004,
74         MIXER_FORMAT    = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
75         MIXER_CHANNEL_MASK = 0x4006, // Channel mask for mixer output
76         // for target RESAMPLE
77         SAMPLE_RATE     = 0x4100, // Configure sample rate conversion on this track name;
78                                   // parameter 'value' is the new sample rate in Hz.
79                                   // Only creates a sample rate converter the first time that
80                                   // the track sample rate is different from the mix sample rate.
81                                   // If the new sample rate is the same as the mix sample rate,
82                                   // and a sample rate converter already exists,
83                                   // then the sample rate converter remains present but is a no-op.
84         RESET           = 0x4101, // Reset sample rate converter without changing sample rate.
85                                   // This clears out the resampler's input buffer.
86         REMOVE          = 0x4102, // Remove the sample rate converter on this track name;
87                                   // the track is restored to the mix sample rate.
88         // for target RAMP_VOLUME and VOLUME (8 channels max)
89         // FIXME use float for these 3 to improve the dynamic range
90         VOLUME0         = 0x4200,
91         VOLUME1         = 0x4201,
92         AUXLEVEL        = 0x4210,
93         // for target TIMESTRETCH
94         PLAYBACK_RATE   = 0x4300, // Configure timestretch on this track name;
95                                   // parameter 'value' is a pointer to the new playback rate.
96     };
97 
AudioMixer(size_t frameCount,uint32_t sampleRate)98     AudioMixer(size_t frameCount, uint32_t sampleRate)
99         : mSampleRate(sampleRate)
100         , mFrameCount(frameCount) {
101         pthread_once(&sOnceControl, &sInitRoutine);
102     }
103 
104     // Create a new track in the mixer.
105     //
106     // \param name        a unique user-provided integer associated with the track.
107     //                    If name already exists, the function will abort.
108     // \param channelMask output channel mask.
109     // \param format      PCM format
110     // \param sessionId   Session id for the track. Tracks with the same
111     //                    session id will be submixed together.
112     //
113     // \return OK        on success.
114     //         BAD_VALUE if the format does not satisfy isValidFormat()
115     //                   or the channelMask does not satisfy isValidChannelMask().
116     status_t    create(
117             int name, audio_channel_mask_t channelMask, audio_format_t format, int sessionId);
118 
exists(int name)119     bool        exists(int name) const {
120         return mTracks.count(name) > 0;
121     }
122 
123     // Free an allocated track by name.
124     void        destroy(int name);
125 
126     // Enable or disable an allocated track by name
127     void        enable(int name);
128     void        disable(int name);
129 
130     void        setParameter(int name, int target, int param, void *value);
131 
132     void        setBufferProvider(int name, AudioBufferProvider* bufferProvider);
133 
process()134     void        process() {
135         (this->*mHook)();
136     }
137 
138     size_t      getUnreleasedFrames(int name) const;
139 
trackNames()140     std::string trackNames() const {
141         std::stringstream ss;
142         for (const auto &pair : mTracks) {
143             ss << pair.first << " ";
144         }
145         return ss.str();
146     }
147 
setNBLogWriter(NBLog::Writer * logWriter)148     void        setNBLogWriter(NBLog::Writer *logWriter) {
149         mNBLogWriter = logWriter;
150     }
151 
isValidFormat(audio_format_t format)152     static inline bool isValidFormat(audio_format_t format) {
153         switch (format) {
154         case AUDIO_FORMAT_PCM_8_BIT:
155         case AUDIO_FORMAT_PCM_16_BIT:
156         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
157         case AUDIO_FORMAT_PCM_32_BIT:
158         case AUDIO_FORMAT_PCM_FLOAT:
159             return true;
160         default:
161             return false;
162         }
163     }
164 
isValidChannelMask(audio_channel_mask_t channelMask)165     static inline bool isValidChannelMask(audio_channel_mask_t channelMask) {
166         return audio_channel_mask_is_valid(channelMask); // the RemixBufferProvider is flexible.
167     }
168 
169 private:
170 
171     /* For multi-format functions (calls template functions
172      * in AudioMixerOps.h).  The template parameters are as follows:
173      *
174      *   MIXTYPE     (see AudioMixerOps.h MIXTYPE_* enumeration)
175      *   USEFLOATVOL (set to true if float volume is used)
176      *   ADJUSTVOL   (set to true if volume ramp parameters needs adjustment afterwards)
177      *   TO: int32_t (Q4.27) or float
178      *   TI: int32_t (Q4.27) or int16_t (Q0.15) or float
179      *   TA: int32_t (Q4.27)
180      */
181 
182     enum {
183         // FIXME this representation permits up to 8 channels
184         NEEDS_CHANNEL_COUNT__MASK   = 0x00000007,
185     };
186 
187     enum {
188         NEEDS_CHANNEL_1             = 0x00000000,   // mono
189         NEEDS_CHANNEL_2             = 0x00000001,   // stereo
190 
191         // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT
192 
193         NEEDS_MUTE                  = 0x00000100,
194         NEEDS_RESAMPLE              = 0x00001000,
195         NEEDS_AUX                   = 0x00010000,
196     };
197 
198     // hook types
199     enum {
200         PROCESSTYPE_NORESAMPLEONETRACK, // others set elsewhere
201     };
202 
203     enum {
204         TRACKTYPE_NOP,
205         TRACKTYPE_RESAMPLE,
206         TRACKTYPE_NORESAMPLE,
207         TRACKTYPE_NORESAMPLEMONO,
208     };
209 
210     // process hook functionality
211     using process_hook_t = void(AudioMixer::*)();
212 
213     struct Track;
214     using hook_t = void(Track::*)(int32_t* output, size_t numOutFrames, int32_t* temp, int32_t* aux);
215 
216     struct Track {
TrackTrack217         Track()
218             : bufferProvider(nullptr)
219         {
220             // TODO: move additional initialization here.
221         }
222 
~TrackTrack223         ~Track()
224         {
225             // bufferProvider, mInputBufferProvider need not be deleted.
226             mResampler.reset(nullptr);
227             // Ensure the order of destruction of buffer providers as they
228             // release the upstream provider in the destructor.
229             mTimestretchBufferProvider.reset(nullptr);
230             mPostDownmixReformatBufferProvider.reset(nullptr);
231             mDownmixerBufferProvider.reset(nullptr);
232             mReformatBufferProvider.reset(nullptr);
233         }
234 
needsRampTrack235         bool        needsRamp() { return (volumeInc[0] | volumeInc[1] | auxInc) != 0; }
236         bool        setResampler(uint32_t trackSampleRate, uint32_t devSampleRate);
doesResampleTrack237         bool        doesResample() const { return mResampler.get() != nullptr; }
resetResamplerTrack238         void        resetResampler() { if (mResampler.get() != nullptr) mResampler->reset(); }
239         void        adjustVolumeRamp(bool aux, bool useFloat = false);
getUnreleasedFramesTrack240         size_t      getUnreleasedFrames() const { return mResampler.get() != nullptr ?
241                                                     mResampler->getUnreleasedFrames() : 0; };
242 
243         status_t    prepareForDownmix();
244         void        unprepareForDownmix();
245         status_t    prepareForReformat();
246         void        unprepareForReformat();
247         bool        setPlaybackRate(const AudioPlaybackRate &playbackRate);
248         void        reconfigureBufferProviders();
249 
250         static hook_t getTrackHook(int trackType, uint32_t channelCount,
251                 audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
252 
253         void track__nop(int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
254 
255         template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
256             typename TO, typename TI, typename TA>
257         void volumeMix(TO *out, size_t outFrames, const TI *in, TA *aux, bool ramp);
258 
259         uint32_t    needs;
260 
261         // TODO: Eventually remove legacy integer volume settings
262         union {
263         int16_t     volume[MAX_NUM_VOLUMES]; // U4.12 fixed point (top bit should be zero)
264         int32_t     volumeRL;
265         };
266 
267         int32_t     prevVolume[MAX_NUM_VOLUMES];
268         int32_t     volumeInc[MAX_NUM_VOLUMES];
269         int32_t     auxInc;
270         int32_t     prevAuxLevel;
271         int16_t     auxLevel;       // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
272 
273         uint16_t    frameCount;
274 
275         uint8_t     channelCount;   // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
276         uint8_t     unused_padding; // formerly format, was always 16
277         uint16_t    enabled;        // actually bool
278         audio_channel_mask_t channelMask;
279 
280         // actual buffer provider used by the track hooks, see DownmixerBufferProvider below
281         //  for how the Track buffer provider is wrapped by another one when dowmixing is required
282         AudioBufferProvider*                bufferProvider;
283 
284         mutable AudioBufferProvider::Buffer buffer; // 8 bytes
285 
286         hook_t      hook;
287         const void  *mIn;             // current location in buffer
288 
289         std::unique_ptr<AudioResampler> mResampler;
290         uint32_t            sampleRate;
291         int32_t*           mainBuffer;
292         int32_t*           auxBuffer;
293 
294         /* Buffer providers are constructed to translate the track input data as needed.
295          *
296          * TODO: perhaps make a single PlaybackConverterProvider class to move
297          * all pre-mixer track buffer conversions outside the AudioMixer class.
298          *
299          * 1) mInputBufferProvider: The AudioTrack buffer provider.
300          * 2) mReformatBufferProvider: If not NULL, performs the audio reformat to
301          *    match either mMixerInFormat or mDownmixRequiresFormat, if the downmixer
302          *    requires reformat. For example, it may convert floating point input to
303          *    PCM_16_bit if that's required by the downmixer.
304          * 3) mDownmixerBufferProvider: If not NULL, performs the channel remixing to match
305          *    the number of channels required by the mixer sink.
306          * 4) mPostDownmixReformatBufferProvider: If not NULL, performs reformatting from
307          *    the downmixer requirements to the mixer engine input requirements.
308          * 5) mTimestretchBufferProvider: Adds timestretching for playback rate
309          */
310         AudioBufferProvider*     mInputBufferProvider;    // externally provided buffer provider.
311         std::unique_ptr<PassthruBufferProvider> mReformatBufferProvider;
312         std::unique_ptr<PassthruBufferProvider> mDownmixerBufferProvider;
313         std::unique_ptr<PassthruBufferProvider> mPostDownmixReformatBufferProvider;
314         std::unique_ptr<PassthruBufferProvider> mTimestretchBufferProvider;
315 
316         int32_t     sessionId;
317 
318         audio_format_t mMixerFormat;     // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
319         audio_format_t mFormat;          // input track format
320         audio_format_t mMixerInFormat;   // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
321                                          // each track must be converted to this format.
322         audio_format_t mDownmixRequiresFormat;  // required downmixer format
323                                                 // AUDIO_FORMAT_PCM_16_BIT if 16 bit necessary
324                                                 // AUDIO_FORMAT_INVALID if no required format
325 
326         float          mVolume[MAX_NUM_VOLUMES];     // floating point set volume
327         float          mPrevVolume[MAX_NUM_VOLUMES]; // floating point previous volume
328         float          mVolumeInc[MAX_NUM_VOLUMES];  // floating point volume increment
329 
330         float          mAuxLevel;                     // floating point set aux level
331         float          mPrevAuxLevel;                 // floating point prev aux level
332         float          mAuxInc;                       // floating point aux increment
333 
334         audio_channel_mask_t mMixerChannelMask;
335         uint32_t             mMixerChannelCount;
336 
337         AudioPlaybackRate    mPlaybackRate;
338 
339     private:
340         // hooks
341         void track__genericResample(int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
342         void track__16BitsStereo(int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
343         void track__16BitsMono(int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
344 
345         void volumeRampStereo(int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
346         void volumeStereo(int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux);
347 
348         // multi-format track hooks
349         template <int MIXTYPE, typename TO, typename TI, typename TA>
350         void track__Resample(TO* out, size_t frameCount, TO* temp __unused, TA* aux);
351         template <int MIXTYPE, typename TO, typename TI, typename TA>
352         void track__NoResample(TO* out, size_t frameCount, TO* temp __unused, TA* aux);
353     };
354 
355     // TODO: remove BLOCKSIZE unit of processing - it isn't needed anymore.
356     static constexpr int BLOCKSIZE = 16;
357 
358     bool setChannelMasks(int name,
359             audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask);
360 
361     // Called when track info changes and a new process hook should be determined.
invalidate()362     void invalidate() {
363         mHook = &AudioMixer::process__validate;
364     }
365 
366     void process__validate();
367     void process__nop();
368     void process__genericNoResampling();
369     void process__genericResampling();
370     void process__oneTrack16BitsStereoNoResampling();
371 
372     template <int MIXTYPE, typename TO, typename TI, typename TA>
373     void process__noResampleOneTrack();
374 
375     static process_hook_t getProcessHook(int processType, uint32_t channelCount,
376             audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
377 
378     static void convertMixerFormat(void *out, audio_format_t mixerOutFormat,
379             void *in, audio_format_t mixerInFormat, size_t sampleCount);
380 
381     static void sInitRoutine();
382 
383     // initialization constants
384     const uint32_t mSampleRate;
385     const size_t mFrameCount;
386 
387     NBLog::Writer *mNBLogWriter = nullptr;   // associated NBLog::Writer
388 
389     process_hook_t mHook = &AudioMixer::process__nop;   // one of process__*, never nullptr
390 
391     // the size of the type (int32_t) should be the largest of all types supported
392     // by the mixer.
393     std::unique_ptr<int32_t[]> mOutputTemp;
394     std::unique_ptr<int32_t[]> mResampleTemp;
395 
396     // track names grouped by main buffer, in no particular order of main buffer.
397     // however names for a particular main buffer are in order (by construction).
398     std::unordered_map<void * /* mainBuffer */, std::vector<int /* name */>> mGroups;
399 
400     // track names that are enabled, in increasing order (by construction).
401     std::vector<int /* name */> mEnabled;
402 
403     // track smart pointers, by name, in increasing order of name.
404     std::map<int /* name */, std::shared_ptr<Track>> mTracks;
405 
406     static pthread_once_t sOnceControl; // initialized in constructor by first new
407 };
408 
409 // ----------------------------------------------------------------------------
410 } // namespace android
411 
412 #endif // ANDROID_AUDIO_MIXER_H
413