Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 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 | |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 24 | #include <utils/threads.h> |
| 25 | |
Glenn Kasten | 2dd4bdd | 2012-08-29 11:10:32 -0700 | [diff] [blame] | 26 | #include <media/AudioBufferProvider.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 27 | #include "AudioResampler.h" |
| 28 | |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 29 | #include <audio_effects/effect_downmix.h> |
| 30 | #include <system/audio.h> |
Glenn Kasten | ab7d72f | 2013-02-27 09:05:28 -0800 | [diff] [blame] | 31 | #include <media/nbaio/NBLog.h> |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 32 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 33 | // FIXME This is actually unity gain, which might not be max in future, expressed in U.12 |
Andy Hung | 97ae824 | 2014-05-30 10:35:47 -0700 | [diff] [blame] | 34 | #define MAX_GAIN_INT AudioMixer::UNITY_GAIN_INT |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 35 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | class AudioMixer |
| 41 | { |
| 42 | public: |
Glenn Kasten | 5c94b6c | 2012-03-20 17:01:29 -0700 | [diff] [blame] | 43 | AudioMixer(size_t frameCount, uint32_t sampleRate, |
| 44 | uint32_t maxNumTracks = MAX_NUM_TRACKS); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | |
Glenn Kasten | c19e224 | 2012-01-30 14:54:39 -0800 | [diff] [blame] | 46 | /*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 47 | |
Glenn Kasten | 599fabc | 2012-03-08 12:33:37 -0800 | [diff] [blame] | 48 | |
| 49 | // This mixer has a hard-coded upper limit of 32 active track inputs. |
| 50 | // Adding support for > 32 tracks would require more than simply changing this value. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | static const uint32_t MAX_NUM_TRACKS = 32; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 52 | // maximum number of channels supported by the mixer |
Glenn Kasten | 599fabc | 2012-03-08 12:33:37 -0800 | [diff] [blame] | 53 | |
| 54 | // This mixer has a hard-coded upper limit of 2 channels for output. |
| 55 | // There is support for > 2 channel tracks down-mixed to 2 channel output via a down-mix effect. |
| 56 | // Adding support for > 2 channel output would require more than simply changing this value. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | static const uint32_t MAX_NUM_CHANNELS = 2; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 58 | // maximum number of channels supported for the content |
| 59 | static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = 8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | |
Andy Hung | 97ae824 | 2014-05-30 10:35:47 -0700 | [diff] [blame] | 61 | static const uint16_t UNITY_GAIN_INT = 0x1000; |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame^] | 62 | static const float UNITY_GAIN_FLOAT = 1.0f; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | |
| 64 | enum { // names |
| 65 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 66 | // track names (MAX_NUM_TRACKS units) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 67 | TRACK0 = 0x1000, |
| 68 | |
Glenn Kasten | 1c48c3c | 2011-12-15 14:54:01 -0800 | [diff] [blame] | 69 | // 0x2000 is unused |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 70 | |
| 71 | // setParameter targets |
| 72 | TRACK = 0x3000, |
| 73 | RESAMPLE = 0x3001, |
| 74 | RAMP_VOLUME = 0x3002, // ramp to new volume |
| 75 | VOLUME = 0x3003, // don't ramp |
| 76 | |
| 77 | // set Parameter names |
| 78 | // for target TRACK |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 79 | CHANNEL_MASK = 0x4000, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 80 | FORMAT = 0x4001, |
| 81 | MAIN_BUFFER = 0x4002, |
| 82 | AUX_BUFFER = 0x4003, |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 83 | DOWNMIX_TYPE = 0X4004, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 84 | MIXER_FORMAT = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT) |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 85 | // for target RESAMPLE |
Glenn Kasten | 4e2293f | 2012-04-12 09:39:07 -0700 | [diff] [blame] | 86 | SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name; |
| 87 | // parameter 'value' is the new sample rate in Hz. |
| 88 | // Only creates a sample rate converter the first time that |
| 89 | // the track sample rate is different from the mix sample rate. |
| 90 | // If the new sample rate is the same as the mix sample rate, |
| 91 | // and a sample rate converter already exists, |
| 92 | // then the sample rate converter remains present but is a no-op. |
| 93 | RESET = 0x4101, // Reset sample rate converter without changing sample rate. |
| 94 | // This clears out the resampler's input buffer. |
| 95 | REMOVE = 0x4102, // Remove the sample rate converter on this track name; |
| 96 | // the track is restored to the mix sample rate. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 97 | // for target RAMP_VOLUME and VOLUME (8 channels max) |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 98 | // FIXME use float for these 3 to improve the dynamic range |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 99 | VOLUME0 = 0x4200, |
| 100 | VOLUME1 = 0x4201, |
| 101 | AUXLEVEL = 0x4210, |
| 102 | }; |
| 103 | |
| 104 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 105 | // For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 106 | |
| 107 | // Allocate a track name. Returns new track name if successful, -1 on failure. |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 108 | // The failure could be because of an invalid channelMask or format, or that |
| 109 | // the track capacity of the mixer is exceeded. |
| 110 | int getTrackName(audio_channel_mask_t channelMask, |
| 111 | audio_format_t format, int sessionId); |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 112 | |
| 113 | // Free an allocated track by name |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 114 | void deleteTrackName(int name); |
| 115 | |
Glenn Kasten | 17a736c | 2012-02-14 08:52:15 -0800 | [diff] [blame] | 116 | // Enable or disable an allocated track by name |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 117 | void enable(int name); |
| 118 | void disable(int name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 119 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 120 | void setParameter(int name, int target, int param, void *value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 121 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 122 | void setBufferProvider(int name, AudioBufferProvider* bufferProvider); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 123 | void process(int64_t pts); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 124 | |
| 125 | uint32_t trackNames() const { return mTrackNames; } |
| 126 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 127 | size_t getUnreleasedFrames(int name) const; |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 128 | |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 129 | static inline bool isValidPcmTrackFormat(audio_format_t format) { |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 130 | return format == AUDIO_FORMAT_PCM_16_BIT || |
| 131 | format == AUDIO_FORMAT_PCM_24_BIT_PACKED || |
| 132 | format == AUDIO_FORMAT_PCM_32_BIT || |
| 133 | format == AUDIO_FORMAT_PCM_FLOAT; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 136 | private: |
| 137 | |
| 138 | enum { |
Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 139 | // FIXME this representation permits up to 8 channels |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 140 | NEEDS_CHANNEL_COUNT__MASK = 0x00000007, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | enum { |
Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 144 | NEEDS_CHANNEL_1 = 0x00000000, // mono |
| 145 | NEEDS_CHANNEL_2 = 0x00000001, // stereo |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 146 | |
Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 147 | // sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 148 | |
Glenn Kasten | d6fadf0 | 2013-10-30 14:37:29 -0700 | [diff] [blame] | 149 | NEEDS_MUTE = 0x00000100, |
| 150 | NEEDS_RESAMPLE = 0x00001000, |
| 151 | NEEDS_AUX = 0x00010000, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 154 | struct state_t; |
| 155 | struct track_t; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 156 | class DownmixerBufferProvider; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 157 | class ReformatBufferProvider; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 158 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 159 | typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp, |
| 160 | int32_t* aux); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 161 | static const int BLOCKSIZE = 16; // 4 cache lines |
| 162 | |
| 163 | struct track_t { |
| 164 | uint32_t needs; |
| 165 | |
| 166 | union { |
Glenn Kasten | bf71f1e | 2011-12-13 11:52:35 -0800 | [diff] [blame] | 167 | int16_t volume[MAX_NUM_CHANNELS]; // [0]3.12 fixed point |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 168 | int32_t volumeRL; |
| 169 | }; |
| 170 | |
Glenn Kasten | bf71f1e | 2011-12-13 11:52:35 -0800 | [diff] [blame] | 171 | int32_t prevVolume[MAX_NUM_CHANNELS]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 172 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 173 | // 16-byte boundary |
| 174 | |
Glenn Kasten | bf71f1e | 2011-12-13 11:52:35 -0800 | [diff] [blame] | 175 | int32_t volumeInc[MAX_NUM_CHANNELS]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 176 | int32_t auxInc; |
| 177 | int32_t prevAuxLevel; |
| 178 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 179 | // 16-byte boundary |
| 180 | |
| 181 | int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 182 | uint16_t frameCount; |
| 183 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 184 | uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK) |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 185 | uint8_t unused_padding; // formerly format, was always 16 |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 186 | uint16_t enabled; // actually bool |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 187 | audio_channel_mask_t channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 188 | |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 189 | // actual buffer provider used by the track hooks, see DownmixerBufferProvider below |
| 190 | // for how the Track buffer provider is wrapped by another one when dowmixing is required |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 191 | AudioBufferProvider* bufferProvider; |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 192 | |
| 193 | // 16-byte boundary |
| 194 | |
| 195 | mutable AudioBufferProvider::Buffer buffer; // 8 bytes |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 196 | |
| 197 | hook_t hook; |
Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 198 | const void* in; // current location in buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 199 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 200 | // 16-byte boundary |
| 201 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 202 | AudioResampler* resampler; |
| 203 | uint32_t sampleRate; |
| 204 | int32_t* mainBuffer; |
| 205 | int32_t* auxBuffer; |
| 206 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 207 | // 16-byte boundary |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 208 | AudioBufferProvider* mInputBufferProvider; // 4 bytes |
| 209 | ReformatBufferProvider* mReformatBufferProvider; // 4 bytes |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 210 | DownmixerBufferProvider* downmixerBufferProvider; // 4 bytes |
| 211 | |
Jean-Michel Trivi | d06e132 | 2012-09-12 15:47:07 -0700 | [diff] [blame] | 212 | int32_t sessionId; |
| 213 | |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 214 | // 16-byte boundary |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 215 | audio_format_t mMixerFormat; // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT) |
| 216 | audio_format_t mFormat; // input track format |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 217 | audio_format_t mMixerInFormat; // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT) |
| 218 | // each track must be converted to this format. |
| 219 | |
| 220 | int32_t mUnused[1]; // alignment padding |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 221 | |
| 222 | // 16-byte boundary |
| 223 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 224 | bool setResampler(uint32_t sampleRate, uint32_t devSampleRate); |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 225 | bool doesResample() const { return resampler != NULL; } |
| 226 | void resetResampler() { if (resampler != NULL) resampler->reset(); } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 227 | void adjustVolumeRamp(bool aux); |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 228 | size_t getUnreleasedFrames() const { return resampler != NULL ? |
| 229 | resampler->getUnreleasedFrames() : 0; }; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | // pad to 32-bytes to fill cache line |
| 233 | struct state_t { |
| 234 | uint32_t enabledTracks; |
| 235 | uint32_t needsChanged; |
| 236 | size_t frameCount; |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 237 | void (*hook)(state_t* state, int64_t pts); // one of process__*, never NULL |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 238 | int32_t *outputTemp; |
| 239 | int32_t *resampleTemp; |
Glenn Kasten | ab7d72f | 2013-02-27 09:05:28 -0800 | [diff] [blame] | 240 | NBLog::Writer* mLog; |
| 241 | int32_t reserved[1]; |
Glenn Kasten | 5c94b6c | 2012-03-20 17:01:29 -0700 | [diff] [blame] | 242 | // FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 243 | track_t tracks[MAX_NUM_TRACKS] __attribute__((aligned(32))); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 246 | // AudioBufferProvider that wraps a track AudioBufferProvider by a call to a downmix effect |
| 247 | class DownmixerBufferProvider : public AudioBufferProvider { |
| 248 | public: |
| 249 | virtual status_t getNextBuffer(Buffer* buffer, int64_t pts); |
| 250 | virtual void releaseBuffer(Buffer* buffer); |
| 251 | DownmixerBufferProvider(); |
| 252 | virtual ~DownmixerBufferProvider(); |
| 253 | |
| 254 | AudioBufferProvider* mTrackBufferProvider; |
| 255 | effect_handle_t mDownmixHandle; |
| 256 | effect_config_t mDownmixConfig; |
| 257 | }; |
| 258 | |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 259 | // AudioBufferProvider wrapper that reformats track to acceptable mixer input type |
| 260 | class ReformatBufferProvider : public AudioBufferProvider { |
| 261 | public: |
| 262 | ReformatBufferProvider(int32_t channels, |
| 263 | audio_format_t inputFormat, audio_format_t outputFormat); |
| 264 | virtual ~ReformatBufferProvider(); |
| 265 | |
| 266 | // overrides AudioBufferProvider methods |
| 267 | virtual status_t getNextBuffer(Buffer* buffer, int64_t pts); |
| 268 | virtual void releaseBuffer(Buffer* buffer); |
| 269 | |
| 270 | void reset(); |
| 271 | inline bool requiresInternalBuffers() { |
| 272 | return true; //mInputFrameSize < mOutputFrameSize; |
| 273 | } |
| 274 | |
| 275 | AudioBufferProvider* mTrackBufferProvider; |
| 276 | int32_t mChannels; |
| 277 | audio_format_t mInputFormat; |
| 278 | audio_format_t mOutputFormat; |
| 279 | size_t mInputFrameSize; |
| 280 | size_t mOutputFrameSize; |
| 281 | // (only) required for reformatting to a larger size. |
| 282 | AudioBufferProvider::Buffer mBuffer; |
| 283 | void* mOutputData; |
| 284 | size_t mOutputCount; |
| 285 | size_t mConsumed; |
| 286 | }; |
| 287 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 288 | // bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 289 | uint32_t mTrackNames; |
Glenn Kasten | 5c94b6c | 2012-03-20 17:01:29 -0700 | [diff] [blame] | 290 | |
| 291 | // bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS, |
| 292 | // but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS |
| 293 | const uint32_t mConfiguredNames; |
| 294 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 295 | const uint32_t mSampleRate; |
| 296 | |
Glenn Kasten | ab7d72f | 2013-02-27 09:05:28 -0800 | [diff] [blame] | 297 | NBLog::Writer mDummyLog; |
| 298 | public: |
| 299 | void setLog(NBLog::Writer* log); |
| 300 | private: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 301 | state_t mState __attribute__((aligned(32))); |
| 302 | |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 303 | // effect descriptor for the downmixer used by the mixer |
Glenn Kasten | 49c34ac | 2013-10-30 14:37:01 -0700 | [diff] [blame] | 304 | static effect_descriptor_t sDwnmFxDesc; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 305 | // indicates whether a downmix effect has been found and is usable by this mixer |
Glenn Kasten | 49c34ac | 2013-10-30 14:37:01 -0700 | [diff] [blame] | 306 | static bool sIsMultichannelCapable; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 307 | |
Glenn Kasten | 4e2293f | 2012-04-12 09:39:07 -0700 | [diff] [blame] | 308 | // Call after changing either the enabled status of a track, or parameters of an enabled track. |
| 309 | // OK to call more often than that, but unnecessary. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 310 | void invalidateState(uint32_t mask); |
Glenn Kasten | 4e2293f | 2012-04-12 09:39:07 -0700 | [diff] [blame] | 311 | |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 312 | static status_t initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 313 | static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum); |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 314 | static void unprepareTrackForDownmix(track_t* pTrack, int trackName); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 315 | static status_t prepareTrackForReformat(track_t* pTrack, int trackNum); |
| 316 | static void unprepareTrackForReformat(track_t* pTrack, int trackName); |
| 317 | static void reconfigureBufferProviders(track_t* pTrack); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 318 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 319 | static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, |
| 320 | int32_t* aux); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 321 | static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux); |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 322 | static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, |
| 323 | int32_t* aux); |
| 324 | static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, |
| 325 | int32_t* aux); |
| 326 | static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, |
| 327 | int32_t* aux); |
| 328 | static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, |
| 329 | int32_t* aux); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 330 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 331 | static void process__validate(state_t* state, int64_t pts); |
| 332 | static void process__nop(state_t* state, int64_t pts); |
| 333 | static void process__genericNoResampling(state_t* state, int64_t pts); |
| 334 | static void process__genericResampling(state_t* state, int64_t pts); |
| 335 | static void process__OneTrack16BitsStereoNoResampling(state_t* state, |
| 336 | int64_t pts); |
Glenn Kasten | 81a028f | 2011-12-15 09:53:12 -0800 | [diff] [blame] | 337 | #if 0 |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 338 | static void process__TwoTracks16BitsStereoNoResampling(state_t* state, |
| 339 | int64_t pts); |
Glenn Kasten | 81a028f | 2011-12-15 09:53:12 -0800 | [diff] [blame] | 340 | #endif |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 341 | |
| 342 | static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS, |
| 343 | int outputFrameIndex); |
Glenn Kasten | 52008f8 | 2012-03-18 09:34:41 -0700 | [diff] [blame] | 344 | |
| 345 | static uint64_t sLocalTimeFreq; |
| 346 | static pthread_once_t sOnceControl; |
| 347 | static void sInitRoutine(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | // ---------------------------------------------------------------------------- |
| 351 | }; // namespace android |
| 352 | |
| 353 | #endif // ANDROID_AUDIO_MIXER_H |