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 #define LOG_TAG "AudioMixer"
19 //#define LOG_NDEBUG 0
20 
21 #include <sstream>
22 #include <stdint.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <sys/types.h>
27 
28 #include <utils/Errors.h>
29 #include <utils/Log.h>
30 
31 #include <system/audio.h>
32 
33 #include <audio_utils/primitives.h>
34 #include <audio_utils/format.h>
35 #include <media/AudioMixer.h>
36 
37 #include "AudioMixerOps.h"
38 
39 // The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer.
40 #ifndef FCC_2
41 #define FCC_2 2
42 #endif
43 
44 // Look for MONO_HACK for any Mono hack involving legacy mono channel to
45 // stereo channel conversion.
46 
47 /* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is
48  * being used. This is a considerable amount of log spam, so don't enable unless you
49  * are verifying the hook based code.
50  */
51 //#define VERY_VERY_VERBOSE_LOGGING
52 #ifdef VERY_VERY_VERBOSE_LOGGING
53 #define ALOGVV ALOGV
54 //define ALOGVV printf  // for test-mixer.cpp
55 #else
56 #define ALOGVV(a...) do { } while (0)
57 #endif
58 
59 // Set to default copy buffer size in frames for input processing.
60 static constexpr size_t kCopyBufferFrameCount = 256;
61 
62 namespace android {
63 
64 // ----------------------------------------------------------------------------
65 
isValidChannelMask(audio_channel_mask_t channelMask) const66 bool AudioMixer::isValidChannelMask(audio_channel_mask_t channelMask) const {
67     return audio_channel_mask_is_valid(channelMask); // the RemixBufferProvider is flexible.
68 }
69 
70 // Called when channel masks have changed for a track name
71 // TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format,
72 // which will simplify this logic.
setChannelMasks(int name,audio_channel_mask_t trackChannelMask,audio_channel_mask_t mixerChannelMask)73 bool AudioMixer::setChannelMasks(int name,
74         audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) {
75     LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
76     const std::shared_ptr<Track> &track = getTrack(name);
77 
78     if (trackChannelMask == (track->channelMask | track->mHapticChannelMask)
79             && mixerChannelMask == (track->mMixerChannelMask | track->mMixerHapticChannelMask)) {
80         return false;  // no need to change
81     }
82     const audio_channel_mask_t hapticChannelMask =
83             static_cast<audio_channel_mask_t>(trackChannelMask & AUDIO_CHANNEL_HAPTIC_ALL);
84     trackChannelMask = static_cast<audio_channel_mask_t>(
85             trackChannelMask & ~AUDIO_CHANNEL_HAPTIC_ALL);
86     const audio_channel_mask_t mixerHapticChannelMask = static_cast<audio_channel_mask_t>(
87             mixerChannelMask & AUDIO_CHANNEL_HAPTIC_ALL);
88     mixerChannelMask = static_cast<audio_channel_mask_t>(
89             mixerChannelMask & ~AUDIO_CHANNEL_HAPTIC_ALL);
90     // always recompute for both channel masks even if only one has changed.
91     const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask);
92     const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask);
93     const uint32_t hapticChannelCount = audio_channel_count_from_out_mask(hapticChannelMask);
94     const uint32_t mixerHapticChannelCount =
95             audio_channel_count_from_out_mask(mixerHapticChannelMask);
96 
97     ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX)
98             && trackChannelCount
99             && mixerChannelCount);
100     track->channelMask = trackChannelMask;
101     track->channelCount = trackChannelCount;
102     track->mMixerChannelMask = mixerChannelMask;
103     track->mMixerChannelCount = mixerChannelCount;
104     track->mHapticChannelMask = hapticChannelMask;
105     track->mHapticChannelCount = hapticChannelCount;
106     track->mMixerHapticChannelMask = mixerHapticChannelMask;
107     track->mMixerHapticChannelCount = mixerHapticChannelCount;
108 
109     if (track->mHapticChannelCount > 0) {
110         track->mAdjustInChannelCount = track->channelCount + track->mHapticChannelCount;
111         track->mAdjustOutChannelCount = track->channelCount;
112         track->mKeepContractedChannels = track->mHapticPlaybackEnabled;
113     } else {
114         track->mAdjustInChannelCount = 0;
115         track->mAdjustOutChannelCount = 0;
116         track->mKeepContractedChannels = false;
117     }
118 
119     track->mInputFrameSize = audio_bytes_per_frame(
120             track->channelCount + track->mHapticChannelCount, track->mFormat);
121 
122     // channel masks have changed, does this track need a downmixer?
123     // update to try using our desired format (if we aren't already using it)
124     const status_t status = track->prepareForDownmix();
125     ALOGE_IF(status != OK,
126             "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x",
127             status, track->channelMask, track->mMixerChannelMask);
128 
129     // always do reformat since channel mask changed,
130     // do it after downmix since track format may change!
131     track->prepareForReformat();
132 
133     track->prepareForAdjustChannels(mFrameCount);
134 
135     // Resampler channels may have changed.
136     track->recreateResampler(mSampleRate);
137     return true;
138 }
139 
unprepareForDownmix()140 void AudioMixer::Track::unprepareForDownmix() {
141     ALOGV("AudioMixer::unprepareForDownmix(%p)", this);
142 
143     if (mPostDownmixReformatBufferProvider.get() != nullptr) {
144         // release any buffers held by the mPostDownmixReformatBufferProvider
145         // before deallocating the mDownmixerBufferProvider.
146         mPostDownmixReformatBufferProvider->reset();
147     }
148 
149     mDownmixRequiresFormat = AUDIO_FORMAT_INVALID;
150     if (mDownmixerBufferProvider.get() != nullptr) {
151         // this track had previously been configured with a downmixer, delete it
152         mDownmixerBufferProvider.reset(nullptr);
153         reconfigureBufferProviders();
154     } else {
155         ALOGV(" nothing to do, no downmixer to delete");
156     }
157 }
158 
prepareForDownmix()159 status_t AudioMixer::Track::prepareForDownmix()
160 {
161     ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x",
162             this, channelMask);
163 
164     // discard the previous downmixer if there was one
165     unprepareForDownmix();
166     // MONO_HACK Only remix (upmix or downmix) if the track and mixer/device channel masks
167     // are not the same and not handled internally, as mono for channel position masks is.
168     if (channelMask == mMixerChannelMask
169             || (channelMask == AUDIO_CHANNEL_OUT_MONO
170                     && isAudioChannelPositionMask(mMixerChannelMask))) {
171         return NO_ERROR;
172     }
173     // DownmixerBufferProvider is only used for position masks.
174     if (audio_channel_mask_get_representation(channelMask)
175                 == AUDIO_CHANNEL_REPRESENTATION_POSITION
176             && DownmixerBufferProvider::isMultichannelCapable()) {
177 
178         // Check if we have a float or int16 downmixer, in that order.
179         for (const audio_format_t format : { AUDIO_FORMAT_PCM_FLOAT, AUDIO_FORMAT_PCM_16_BIT }) {
180             mDownmixerBufferProvider.reset(new DownmixerBufferProvider(
181                     channelMask, mMixerChannelMask,
182                     format,
183                     sampleRate, sessionId, kCopyBufferFrameCount));
184             if (static_cast<DownmixerBufferProvider *>(mDownmixerBufferProvider.get())
185                     ->isValid()) {
186                 mDownmixRequiresFormat = format;
187                 reconfigureBufferProviders();
188                 return NO_ERROR;
189             }
190         }
191         // mDownmixerBufferProvider reset below.
192     }
193 
194     // See if we should use our built-in non-effect downmixer.
195     if (mMixerInFormat == AUDIO_FORMAT_PCM_FLOAT
196             && ChannelMixBufferProvider::isOutputChannelMaskSupported(mMixerChannelMask)
197             && audio_channel_mask_get_representation(channelMask)
198                     == AUDIO_CHANNEL_REPRESENTATION_POSITION) {
199         mDownmixerBufferProvider.reset(new ChannelMixBufferProvider(channelMask,
200                 mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount));
201         if (static_cast<ChannelMixBufferProvider *>(mDownmixerBufferProvider.get())
202                 ->isValid()) {
203             mDownmixRequiresFormat = mMixerInFormat;
204             reconfigureBufferProviders();
205             ALOGD("%s: Fallback using ChannelMix", __func__);
206             return NO_ERROR;
207         } else {
208             ALOGD("%s: ChannelMix not supported for channel mask %#x", __func__, channelMask);
209         }
210     }
211 
212     // Effect downmixer does not accept the channel conversion.  Let's use our remixer.
213     mDownmixerBufferProvider.reset(new RemixBufferProvider(channelMask,
214             mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount));
215     // Remix always finds a conversion whereas Downmixer effect above may fail.
216     reconfigureBufferProviders();
217     return NO_ERROR;
218 }
219 
unprepareForReformat()220 void AudioMixer::Track::unprepareForReformat() {
221     ALOGV("AudioMixer::unprepareForReformat(%p)", this);
222     bool requiresReconfigure = false;
223     if (mReformatBufferProvider.get() != nullptr) {
224         mReformatBufferProvider.reset(nullptr);
225         requiresReconfigure = true;
226     }
227     if (mPostDownmixReformatBufferProvider.get() != nullptr) {
228         mPostDownmixReformatBufferProvider.reset(nullptr);
229         requiresReconfigure = true;
230     }
231     if (requiresReconfigure) {
232         reconfigureBufferProviders();
233     }
234 }
235 
prepareForReformat()236 status_t AudioMixer::Track::prepareForReformat()
237 {
238     ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat);
239     // discard previous reformatters
240     unprepareForReformat();
241     // only configure reformatters as needed
242     const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID
243             ? mDownmixRequiresFormat : mMixerInFormat;
244     bool requiresReconfigure = false;
245     if (mFormat != targetFormat) {
246         mReformatBufferProvider.reset(new ReformatBufferProvider(
247                 audio_channel_count_from_out_mask(channelMask),
248                 mFormat,
249                 targetFormat,
250                 kCopyBufferFrameCount));
251         requiresReconfigure = true;
252     } else if (mFormat == AUDIO_FORMAT_PCM_FLOAT) {
253         // Input and output are floats, make sure application did not provide > 3db samples
254         // that would break volume application (b/68099072)
255         // TODO: add a trusted source flag to avoid the overhead
256         mReformatBufferProvider.reset(new ClampFloatBufferProvider(
257                 audio_channel_count_from_out_mask(channelMask),
258                 kCopyBufferFrameCount));
259         requiresReconfigure = true;
260     }
261     if (targetFormat != mMixerInFormat) {
262         mPostDownmixReformatBufferProvider.reset(new ReformatBufferProvider(
263                 audio_channel_count_from_out_mask(mMixerChannelMask),
264                 targetFormat,
265                 mMixerInFormat,
266                 kCopyBufferFrameCount));
267         requiresReconfigure = true;
268     }
269     if (requiresReconfigure) {
270         reconfigureBufferProviders();
271     }
272     return NO_ERROR;
273 }
274 
unprepareForAdjustChannels()275 void AudioMixer::Track::unprepareForAdjustChannels()
276 {
277     ALOGV("AUDIOMIXER::unprepareForAdjustChannels");
278     if (mAdjustChannelsBufferProvider.get() != nullptr) {
279         mAdjustChannelsBufferProvider.reset(nullptr);
280         reconfigureBufferProviders();
281     }
282 }
283 
prepareForAdjustChannels(size_t frames)284 status_t AudioMixer::Track::prepareForAdjustChannels(size_t frames)
285 {
286     ALOGV("AudioMixer::prepareForAdjustChannels(%p) with inChannelCount: %u, outChannelCount: %u",
287             this, mAdjustInChannelCount, mAdjustOutChannelCount);
288     unprepareForAdjustChannels();
289     if (mAdjustInChannelCount != mAdjustOutChannelCount) {
290         uint8_t* buffer = mKeepContractedChannels
291                 ? (uint8_t*)mainBuffer + frames * audio_bytes_per_frame(
292                         mMixerChannelCount, mMixerFormat)
293                 : nullptr;
294         mAdjustChannelsBufferProvider.reset(new AdjustChannelsBufferProvider(
295                 mFormat, mAdjustInChannelCount, mAdjustOutChannelCount, frames,
296                 mKeepContractedChannels ? mMixerFormat : AUDIO_FORMAT_INVALID,
297                 buffer, mMixerHapticChannelCount));
298         reconfigureBufferProviders();
299     }
300     return NO_ERROR;
301 }
302 
unprepareForTee()303 void AudioMixer::Track::unprepareForTee() {
304     ALOGV("AudioMixer::%s", __func__);
305     if (mTeeBufferProvider.get() != nullptr) {
306         mTeeBufferProvider.reset(nullptr);
307         reconfigureBufferProviders();
308     }
309 }
310 
prepareForTee()311 status_t AudioMixer::Track::prepareForTee() {
312     ALOGV("AudioMixer::%s(%p) teeBuffer=%p", __func__, this, teeBuffer);
313     unprepareForTee();
314     if (teeBuffer != nullptr) {
315         mTeeBufferProvider.reset(new TeeBufferProvider(
316                 mInputFrameSize, mInputFrameSize, kCopyBufferFrameCount,
317                 (uint8_t*)teeBuffer, mTeeBufferFrameCount));
318         reconfigureBufferProviders();
319     }
320     return NO_ERROR;
321 }
322 
clearContractedBuffer()323 void AudioMixer::Track::clearContractedBuffer()
324 {
325     if (mAdjustChannelsBufferProvider.get() != nullptr) {
326         static_cast<AdjustChannelsBufferProvider*>(
327                 mAdjustChannelsBufferProvider.get())->clearContractedFrames();
328     }
329 }
330 
clearTeeFrameCopied()331 void AudioMixer::Track::clearTeeFrameCopied() {
332     if (mTeeBufferProvider.get() != nullptr) {
333         static_cast<TeeBufferProvider*>(mTeeBufferProvider.get())->clearFramesCopied();
334     }
335 }
336 
reconfigureBufferProviders()337 void AudioMixer::Track::reconfigureBufferProviders()
338 {
339     // configure from upstream to downstream buffer providers.
340     bufferProvider = mInputBufferProvider;
341     if (mTeeBufferProvider != nullptr) {
342         mTeeBufferProvider->setBufferProvider(bufferProvider);
343         bufferProvider = mTeeBufferProvider.get();
344     }
345     if (mAdjustChannelsBufferProvider.get() != nullptr) {
346         mAdjustChannelsBufferProvider->setBufferProvider(bufferProvider);
347         bufferProvider = mAdjustChannelsBufferProvider.get();
348     }
349     if (mReformatBufferProvider.get() != nullptr) {
350         mReformatBufferProvider->setBufferProvider(bufferProvider);
351         bufferProvider = mReformatBufferProvider.get();
352     }
353     if (mDownmixerBufferProvider.get() != nullptr) {
354         mDownmixerBufferProvider->setBufferProvider(bufferProvider);
355         bufferProvider = mDownmixerBufferProvider.get();
356     }
357     if (mPostDownmixReformatBufferProvider.get() != nullptr) {
358         mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider);
359         bufferProvider = mPostDownmixReformatBufferProvider.get();
360     }
361     if (mTimestretchBufferProvider.get() != nullptr) {
362         mTimestretchBufferProvider->setBufferProvider(bufferProvider);
363         bufferProvider = mTimestretchBufferProvider.get();
364     }
365 }
366 
setParameter(int name,int target,int param,void * value)367 void AudioMixer::setParameter(int name, int target, int param, void *value)
368 {
369     LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
370     const std::shared_ptr<Track> &track = getTrack(name);
371 
372     int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value));
373     int32_t *valueBuf = reinterpret_cast<int32_t*>(value);
374 
375     switch (target) {
376 
377     case TRACK:
378         switch (param) {
379         case CHANNEL_MASK: {
380             const audio_channel_mask_t trackChannelMask =
381                 static_cast<audio_channel_mask_t>(valueInt);
382             if (setChannelMasks(name, trackChannelMask,
383                     static_cast<audio_channel_mask_t>(
384                             track->mMixerChannelMask | track->mMixerHapticChannelMask))) {
385                 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask);
386                 invalidate();
387             }
388             } break;
389         case MAIN_BUFFER:
390             if (track->mainBuffer != valueBuf) {
391                 track->mainBuffer = valueBuf;
392                 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
393                 if (track->mKeepContractedChannels) {
394                     track->prepareForAdjustChannels(mFrameCount);
395                 }
396                 invalidate();
397             }
398             break;
399         case AUX_BUFFER:
400             AudioMixerBase::setParameter(name, target, param, value);
401             break;
402         case FORMAT: {
403             audio_format_t format = static_cast<audio_format_t>(valueInt);
404             if (track->mFormat != format) {
405                 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format);
406                 track->mFormat = format;
407                 ALOGV("setParameter(TRACK, FORMAT, %#x)", format);
408                 track->prepareForReformat();
409                 invalidate();
410             }
411             } break;
412         // FIXME do we want to support setting the downmix type from AudioFlinger?
413         //         for a specific track? or per mixer?
414         /* case DOWNMIX_TYPE:
415             break          */
416         case MIXER_FORMAT: {
417             audio_format_t format = static_cast<audio_format_t>(valueInt);
418             if (track->mMixerFormat != format) {
419                 track->mMixerFormat = format;
420                 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format);
421                 if (track->mKeepContractedChannels) {
422                     track->prepareForAdjustChannels(mFrameCount);
423                 }
424             }
425             } break;
426         case MIXER_CHANNEL_MASK: {
427             const audio_channel_mask_t mixerChannelMask =
428                     static_cast<audio_channel_mask_t>(valueInt);
429             if (setChannelMasks(name, static_cast<audio_channel_mask_t>(
430                                     track->channelMask | track->mHapticChannelMask),
431                     mixerChannelMask)) {
432                 ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask);
433                 invalidate();
434             }
435             } break;
436         case HAPTIC_ENABLED: {
437             const bool hapticPlaybackEnabled = static_cast<bool>(valueInt);
438             if (track->mHapticPlaybackEnabled != hapticPlaybackEnabled) {
439                 track->mHapticPlaybackEnabled = hapticPlaybackEnabled;
440                 track->mKeepContractedChannels = hapticPlaybackEnabled;
441                 track->prepareForAdjustChannels(mFrameCount);
442             }
443             } break;
444         case HAPTIC_SCALE: {
445             const os::HapticScale hapticScale = *reinterpret_cast<os::HapticScale*>(value);
446             if (track->mHapticScale != hapticScale) {
447                 track->mHapticScale = hapticScale;
448             }
449             } break;
450         case HAPTIC_MAX_AMPLITUDE: {
451             const float hapticMaxAmplitude = *reinterpret_cast<float*>(value);
452             if (track->mHapticMaxAmplitude != hapticMaxAmplitude) {
453                 track->mHapticMaxAmplitude = hapticMaxAmplitude;
454             }
455             } break;
456         case TEE_BUFFER:
457             if (track->teeBuffer != valueBuf) {
458                 track->teeBuffer = valueBuf;
459                 ALOGV("setParameter(TRACK, TEE_BUFFER, %p)", valueBuf);
460                 track->prepareForTee();
461             }
462             break;
463         case TEE_BUFFER_FRAME_COUNT:
464             if (track->mTeeBufferFrameCount != valueInt) {
465                 track->mTeeBufferFrameCount = valueInt;
466                 ALOGV("setParameter(TRACK, TEE_BUFFER_FRAME_COUNT, %i)", valueInt);
467                 track->prepareForTee();
468             }
469             break;
470         default:
471             LOG_ALWAYS_FATAL("setParameter track: bad param %d", param);
472         }
473         break;
474 
475     case RESAMPLE:
476     case RAMP_VOLUME:
477     case VOLUME:
478         AudioMixerBase::setParameter(name, target, param, value);
479         break;
480     case TIMESTRETCH:
481         switch (param) {
482         case PLAYBACK_RATE: {
483             const AudioPlaybackRate *playbackRate =
484                     reinterpret_cast<AudioPlaybackRate*>(value);
485             ALOGW_IF(!isAudioPlaybackRateValid(*playbackRate),
486                     "bad parameters speed %f, pitch %f",
487                     playbackRate->mSpeed, playbackRate->mPitch);
488             if (track->setPlaybackRate(*playbackRate)) {
489                 ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, STRETCH_MODE, FALLBACK_MODE "
490                         "%f %f %d %d",
491                         playbackRate->mSpeed,
492                         playbackRate->mPitch,
493                         playbackRate->mStretchMode,
494                         playbackRate->mFallbackMode);
495                 // invalidate();  (should not require reconfigure)
496             }
497         } break;
498         default:
499             LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param);
500         }
501         break;
502 
503     default:
504         LOG_ALWAYS_FATAL("setParameter: bad target %d", target);
505     }
506 }
507 
setPlaybackRate(const AudioPlaybackRate & playbackRate)508 bool AudioMixer::Track::setPlaybackRate(const AudioPlaybackRate &playbackRate)
509 {
510     if ((mTimestretchBufferProvider.get() == nullptr &&
511             fabs(playbackRate.mSpeed - mPlaybackRate.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA &&
512             fabs(playbackRate.mPitch - mPlaybackRate.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) ||
513             isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) {
514         return false;
515     }
516     mPlaybackRate = playbackRate;
517     if (mTimestretchBufferProvider.get() == nullptr) {
518         // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
519         // but if none exists, it is the channel count (1 for mono).
520         const int timestretchChannelCount = getOutputChannelCount();
521         mTimestretchBufferProvider.reset(new TimestretchBufferProvider(timestretchChannelCount,
522                 mMixerInFormat, sampleRate, playbackRate));
523         reconfigureBufferProviders();
524     } else {
525         static_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider.get())
526                 ->setPlaybackRate(playbackRate);
527     }
528     return true;
529 }
530 
setBufferProvider(int name,AudioBufferProvider * bufferProvider)531 void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
532 {
533     LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name);
534     const std::shared_ptr<Track> &track = getTrack(name);
535 
536     if (track->mInputBufferProvider == bufferProvider) {
537         return; // don't reset any buffer providers if identical.
538     }
539     // reset order from downstream to upstream buffer providers.
540     if (track->mTimestretchBufferProvider.get() != nullptr) {
541         track->mTimestretchBufferProvider->reset();
542     } else if (track->mPostDownmixReformatBufferProvider.get() != nullptr) {
543         track->mPostDownmixReformatBufferProvider->reset();
544     } else if (track->mDownmixerBufferProvider != nullptr) {
545         track->mDownmixerBufferProvider->reset();
546     } else if (track->mReformatBufferProvider.get() != nullptr) {
547         track->mReformatBufferProvider->reset();
548     } else if (track->mAdjustChannelsBufferProvider.get() != nullptr) {
549         track->mAdjustChannelsBufferProvider->reset();
550     } else if (track->mTeeBufferProvider.get() != nullptr) {
551         track->mTeeBufferProvider->reset();
552     }
553 
554     track->mInputBufferProvider = bufferProvider;
555     track->reconfigureBufferProviders();
556 }
557 
558 /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
559 
sInitRoutine()560 /*static*/ void AudioMixer::sInitRoutine()
561 {
562     DownmixerBufferProvider::init(); // for the downmixer
563 }
564 
preCreateTrack()565 std::shared_ptr<AudioMixerBase::TrackBase> AudioMixer::preCreateTrack()
566 {
567     return std::make_shared<Track>();
568 }
569 
postCreateTrack(TrackBase * track)570 status_t AudioMixer::postCreateTrack(TrackBase *track)
571 {
572     Track* t = static_cast<Track*>(track);
573 
574     audio_channel_mask_t channelMask = t->channelMask;
575     t->mHapticChannelMask = static_cast<audio_channel_mask_t>(
576             channelMask & AUDIO_CHANNEL_HAPTIC_ALL);
577     t->mHapticChannelCount = audio_channel_count_from_out_mask(t->mHapticChannelMask);
578     channelMask = static_cast<audio_channel_mask_t>(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL);
579     t->channelCount = audio_channel_count_from_out_mask(channelMask);
580     ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO,
581             "Non-stereo channel mask: %d\n", channelMask);
582     t->channelMask = channelMask;
583     t->mInputBufferProvider = NULL;
584     t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required
585     t->mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT;
586     // haptic
587     t->mHapticPlaybackEnabled = false;
588     t->mHapticScale = {/*level=*/os::HapticLevel::NONE };
589     t->mHapticMaxAmplitude = NAN;
590     t->mMixerHapticChannelMask = AUDIO_CHANNEL_NONE;
591     t->mMixerHapticChannelCount = 0;
592     t->mAdjustInChannelCount = t->channelCount + t->mHapticChannelCount;
593     t->mAdjustOutChannelCount = t->channelCount;
594     t->mKeepContractedChannels = false;
595     t->mInputFrameSize = audio_bytes_per_frame(
596             t->channelCount + t->mHapticChannelCount, t->mFormat);
597     // Check the downmixing (or upmixing) requirements.
598     status_t status = t->prepareForDownmix();
599     if (status != OK) {
600         ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask);
601         return BAD_VALUE;
602     }
603     // prepareForDownmix() may change mDownmixRequiresFormat
604     ALOGVV("mMixerFormat:%#x  mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat);
605     t->prepareForReformat();
606     t->prepareForAdjustChannels(mFrameCount);
607     return OK;
608 }
609 
preProcess()610 void AudioMixer::preProcess()
611 {
612     for (const auto &pair : mTracks) {
613         // Clear contracted buffer before processing if contracted channels are saved
614         const std::shared_ptr<TrackBase> &tb = pair.second;
615         Track *t = static_cast<Track*>(tb.get());
616         if (t->mKeepContractedChannels) {
617             t->clearContractedBuffer();
618         }
619         t->clearTeeFrameCopied();
620     }
621 }
622 
postProcess()623 void AudioMixer::postProcess()
624 {
625     // Process haptic data.
626     // Need to keep consistent with VibrationEffect.scale(int, float, int)
627     for (const auto &pair : mGroups) {
628         // process by group of tracks with same output main buffer.
629         const auto &group = pair.second;
630         for (const int name : group) {
631             const std::shared_ptr<Track> &t = getTrack(name);
632             if (t->mHapticPlaybackEnabled) {
633                 size_t sampleCount = mFrameCount * t->mMixerHapticChannelCount;
634                 uint8_t* buffer = (uint8_t*)pair.first + mFrameCount * audio_bytes_per_frame(
635                         t->mMixerChannelCount, t->mMixerFormat);
636                 switch (t->mMixerFormat) {
637                 // Mixer format should be AUDIO_FORMAT_PCM_FLOAT.
638                 case AUDIO_FORMAT_PCM_FLOAT: {
639                     os::scaleHapticData((float*) buffer, sampleCount, t->mHapticScale,
640                                         t->mHapticMaxAmplitude);
641                 } break;
642                 default:
643                     LOG_ALWAYS_FATAL("bad mMixerFormat: %#x", t->mMixerFormat);
644                     break;
645                 }
646                 break;
647             }
648             if (t->teeBuffer != nullptr && t->volumeRL == 0) {
649                 // Need to mute tee
650                 memset(t->teeBuffer, 0, t->mTeeBufferFrameCount * t->mInputFrameSize);
651             }
652         }
653     }
654 }
655 
656 // ----------------------------------------------------------------------------
657 } // namespace android
658