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 | #define LOG_TAG "AudioMixer" |
Glenn Kasten | 7f5d335 | 2013-02-15 23:55:04 +0000 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 20 | |
Mikhail Naganov | 3b73e99 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 21 | #include <sstream> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 25 | #include <math.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Log.h> |
| 30 | |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 31 | #include <system/audio.h> |
| 32 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 33 | #include <audio_utils/primitives.h> |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 34 | #include <audio_utils/format.h> |
Andy Hung | 068561c | 2017-01-03 17:09:32 -0800 | [diff] [blame] | 35 | #include <media/AudioMixer.h> |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 36 | |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 37 | #include "AudioMixerOps.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 39 | // The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer. |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 40 | #ifndef FCC_2 |
| 41 | #define FCC_2 2 |
| 42 | #endif |
| 43 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 44 | // Look for MONO_HACK for any Mono hack involving legacy mono channel to |
| 45 | // stereo channel conversion. |
| 46 | |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 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 | |
Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 59 | // Set to default copy buffer size in frames for input processing. |
Mikhail Naganov | 3b73e99 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 60 | static constexpr size_t kCopyBufferFrameCount = 256; |
Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 61 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | namespace android { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | |
| 64 | // ---------------------------------------------------------------------------- |
Andy Hung | 1b2fdcb | 2014-07-16 17:44:34 -0700 | [diff] [blame] | 65 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 66 | bool AudioMixer::isValidChannelMask(audio_channel_mask_t channelMask) const { |
| 67 | return audio_channel_mask_is_valid(channelMask); // the RemixBufferProvider is flexible. |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 68 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 70 | // Called when channel masks have changed for a track name |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 71 | // TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format, |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 72 | // which will simplify this logic. |
| 73 | bool AudioMixer::setChannelMasks(int name, |
| 74 | audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 75 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 76 | const std::shared_ptr<Track> &track = getTrack(name); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 77 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 78 | if (trackChannelMask == (track->channelMask | track->mHapticChannelMask) |
| 79 | && mixerChannelMask == (track->mMixerChannelMask | track->mMixerHapticChannelMask)) { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 80 | return false; // no need to change |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 81 | } |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 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); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 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); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 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); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 96 | |
| 97 | ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) |
| 98 | && trackChannelCount |
| 99 | && mixerChannelCount); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 100 | track->channelMask = trackChannelMask; |
| 101 | track->channelCount = trackChannelCount; |
| 102 | track->mMixerChannelMask = mixerChannelMask; |
| 103 | track->mMixerChannelCount = mixerChannelCount; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 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; |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 111 | track->mAdjustOutChannelCount = track->channelCount; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 112 | track->mKeepContractedChannels = track->mHapticPlaybackEnabled; |
| 113 | } else { |
| 114 | track->mAdjustInChannelCount = 0; |
| 115 | track->mAdjustOutChannelCount = 0; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 116 | track->mKeepContractedChannels = false; |
| 117 | } |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 118 | |
| 119 | // channel masks have changed, does this track need a downmixer? |
| 120 | // update to try using our desired format (if we aren't already using it) |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 121 | const status_t status = track->prepareForDownmix(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 122 | ALOGE_IF(status != OK, |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 123 | "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x", |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 124 | status, track->channelMask, track->mMixerChannelMask); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 125 | |
Yung Ti Su | 1a0ecc3 | 2018-05-07 11:09:15 +0800 | [diff] [blame] | 126 | // always do reformat since channel mask changed, |
| 127 | // do it after downmix since track format may change! |
| 128 | track->prepareForReformat(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 129 | |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 130 | track->prepareForAdjustChannels(mFrameCount); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 131 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 132 | // Resampler channels may have changed. |
| 133 | track->recreateResampler(mSampleRate); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 137 | void AudioMixer::Track::unprepareForDownmix() { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 138 | ALOGV("AudioMixer::unprepareForDownmix(%p)", this); |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 139 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 140 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
Andy Hung | 8539589 | 2017-04-25 16:47:52 -0700 | [diff] [blame] | 141 | // release any buffers held by the mPostDownmixReformatBufferProvider |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 142 | // before deallocating the mDownmixerBufferProvider. |
Andy Hung | 8539589 | 2017-04-25 16:47:52 -0700 | [diff] [blame] | 143 | mPostDownmixReformatBufferProvider->reset(); |
| 144 | } |
| 145 | |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 146 | mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 147 | if (mDownmixerBufferProvider.get() != nullptr) { |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 148 | // this track had previously been configured with a downmixer, delete it |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 149 | mDownmixerBufferProvider.reset(nullptr); |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 150 | reconfigureBufferProviders(); |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 151 | } else { |
| 152 | ALOGV(" nothing to do, no downmixer to delete"); |
| 153 | } |
| 154 | } |
| 155 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 156 | status_t AudioMixer::Track::prepareForDownmix() |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 157 | { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 158 | ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x", |
| 159 | this, channelMask); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 160 | |
Jean-Michel Trivi | 9bd2322 | 2012-04-16 13:43:48 -0700 | [diff] [blame] | 161 | // discard the previous downmixer if there was one |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 162 | unprepareForDownmix(); |
Andy Hung | 73e62e2 | 2015-04-20 12:06:38 -0700 | [diff] [blame] | 163 | // MONO_HACK Only remix (upmix or downmix) if the track and mixer/device channel masks |
Judy Hsiao | c5cf9e2 | 2019-08-15 11:32:02 +0800 | [diff] [blame] | 164 | // are not the same and not handled internally, as mono for channel position masks is. |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 165 | if (channelMask == mMixerChannelMask |
| 166 | || (channelMask == AUDIO_CHANNEL_OUT_MONO |
Judy Hsiao | c5cf9e2 | 2019-08-15 11:32:02 +0800 | [diff] [blame] | 167 | && isAudioChannelPositionMask(mMixerChannelMask))) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 168 | return NO_ERROR; |
| 169 | } |
Andy Hung | 650ceb9 | 2015-01-29 13:31:12 -0800 | [diff] [blame] | 170 | // DownmixerBufferProvider is only used for position masks. |
| 171 | if (audio_channel_mask_get_representation(channelMask) |
| 172 | == AUDIO_CHANNEL_REPRESENTATION_POSITION |
| 173 | && DownmixerBufferProvider::isMultichannelCapable()) { |
Andy Hung | 6694255 | 2018-12-21 16:07:12 -0800 | [diff] [blame] | 174 | |
| 175 | // Check if we have a float or int16 downmixer, in that order. |
| 176 | for (const audio_format_t format : { AUDIO_FORMAT_PCM_FLOAT, AUDIO_FORMAT_PCM_16_BIT }) { |
| 177 | mDownmixerBufferProvider.reset(new DownmixerBufferProvider( |
| 178 | channelMask, mMixerChannelMask, |
| 179 | format, |
| 180 | sampleRate, sessionId, kCopyBufferFrameCount)); |
| 181 | if (static_cast<DownmixerBufferProvider *>(mDownmixerBufferProvider.get()) |
| 182 | ->isValid()) { |
| 183 | mDownmixRequiresFormat = format; |
| 184 | reconfigureBufferProviders(); |
| 185 | return NO_ERROR; |
| 186 | } |
Andy Hung | 34803d5 | 2014-07-16 21:41:35 -0700 | [diff] [blame] | 187 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 188 | // mDownmixerBufferProvider reset below. |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 189 | } |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 190 | |
Andy Hung | eda3e93 | 2021-10-21 13:44:56 -0700 | [diff] [blame] | 191 | // See if we should use our built-in non-effect downmixer. |
| 192 | if (mMixerInFormat == AUDIO_FORMAT_PCM_FLOAT |
| 193 | && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO |
| 194 | && audio_channel_mask_get_representation(channelMask) |
| 195 | == AUDIO_CHANNEL_REPRESENTATION_POSITION) { |
| 196 | mDownmixerBufferProvider.reset(new ChannelMixBufferProvider(channelMask, |
| 197 | mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount)); |
| 198 | if (static_cast<ChannelMixBufferProvider *>(mDownmixerBufferProvider.get()) |
| 199 | ->isValid()) { |
| 200 | mDownmixRequiresFormat = mMixerInFormat; |
| 201 | reconfigureBufferProviders(); |
| 202 | ALOGD("%s: Fallback using ChannelMix", __func__); |
| 203 | return NO_ERROR; |
| 204 | } else { |
| 205 | ALOGD("%s: ChannelMix not supported for channel mask %#x", __func__, channelMask); |
| 206 | } |
| 207 | } |
| 208 | |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 209 | // Effect downmixer does not accept the channel conversion. Let's use our remixer. |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 210 | mDownmixerBufferProvider.reset(new RemixBufferProvider(channelMask, |
| 211 | mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount)); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 212 | // Remix always finds a conversion whereas Downmixer effect above may fail. |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 213 | reconfigureBufferProviders(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 214 | return NO_ERROR; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 217 | void AudioMixer::Track::unprepareForReformat() { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 218 | ALOGV("AudioMixer::unprepareForReformat(%p)", this); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 219 | bool requiresReconfigure = false; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 220 | if (mReformatBufferProvider.get() != nullptr) { |
| 221 | mReformatBufferProvider.reset(nullptr); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 222 | requiresReconfigure = true; |
| 223 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 224 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
| 225 | mPostDownmixReformatBufferProvider.reset(nullptr); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 226 | requiresReconfigure = true; |
| 227 | } |
| 228 | if (requiresReconfigure) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 229 | reconfigureBufferProviders(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 233 | status_t AudioMixer::Track::prepareForReformat() |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 234 | { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 235 | ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 236 | // discard previous reformatters |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 237 | unprepareForReformat(); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 238 | // only configure reformatters as needed |
| 239 | const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID |
| 240 | ? mDownmixRequiresFormat : mMixerInFormat; |
| 241 | bool requiresReconfigure = false; |
| 242 | if (mFormat != targetFormat) { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 243 | mReformatBufferProvider.reset(new ReformatBufferProvider( |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 244 | audio_channel_count_from_out_mask(channelMask), |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 245 | mFormat, |
| 246 | targetFormat, |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 247 | kCopyBufferFrameCount)); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 248 | requiresReconfigure = true; |
Kevin Rocard | e053bfa | 2017-11-09 22:07:34 -0800 | [diff] [blame] | 249 | } else if (mFormat == AUDIO_FORMAT_PCM_FLOAT) { |
| 250 | // Input and output are floats, make sure application did not provide > 3db samples |
| 251 | // that would break volume application (b/68099072) |
| 252 | // TODO: add a trusted source flag to avoid the overhead |
| 253 | mReformatBufferProvider.reset(new ClampFloatBufferProvider( |
| 254 | audio_channel_count_from_out_mask(channelMask), |
| 255 | kCopyBufferFrameCount)); |
| 256 | requiresReconfigure = true; |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 257 | } |
| 258 | if (targetFormat != mMixerInFormat) { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 259 | mPostDownmixReformatBufferProvider.reset(new ReformatBufferProvider( |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 260 | audio_channel_count_from_out_mask(mMixerChannelMask), |
| 261 | targetFormat, |
| 262 | mMixerInFormat, |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 263 | kCopyBufferFrameCount)); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 264 | requiresReconfigure = true; |
| 265 | } |
| 266 | if (requiresReconfigure) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 267 | reconfigureBufferProviders(); |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 268 | } |
| 269 | return NO_ERROR; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 270 | } |
| 271 | |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 272 | void AudioMixer::Track::unprepareForAdjustChannels() |
| 273 | { |
| 274 | ALOGV("AUDIOMIXER::unprepareForAdjustChannels"); |
| 275 | if (mAdjustChannelsBufferProvider.get() != nullptr) { |
| 276 | mAdjustChannelsBufferProvider.reset(nullptr); |
| 277 | reconfigureBufferProviders(); |
| 278 | } |
| 279 | } |
| 280 | |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 281 | status_t AudioMixer::Track::prepareForAdjustChannels(size_t frames) |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 282 | { |
| 283 | ALOGV("AudioMixer::prepareForAdjustChannels(%p) with inChannelCount: %u, outChannelCount: %u", |
| 284 | this, mAdjustInChannelCount, mAdjustOutChannelCount); |
| 285 | unprepareForAdjustChannels(); |
| 286 | if (mAdjustInChannelCount != mAdjustOutChannelCount) { |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 287 | uint8_t* buffer = mKeepContractedChannels |
| 288 | ? (uint8_t*)mainBuffer + frames * audio_bytes_per_frame( |
| 289 | mMixerChannelCount, mMixerFormat) |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 290 | : nullptr; |
| 291 | mAdjustChannelsBufferProvider.reset(new AdjustChannelsBufferProvider( |
| 292 | mFormat, mAdjustInChannelCount, mAdjustOutChannelCount, frames, |
| 293 | mKeepContractedChannels ? mMixerFormat : AUDIO_FORMAT_INVALID, |
| 294 | buffer, mMixerHapticChannelCount)); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 295 | reconfigureBufferProviders(); |
| 296 | } |
| 297 | return NO_ERROR; |
| 298 | } |
| 299 | |
| 300 | void AudioMixer::Track::clearContractedBuffer() |
| 301 | { |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 302 | if (mAdjustChannelsBufferProvider.get() != nullptr) { |
jiabin | ea8fa7a | 2019-02-22 14:41:50 -0800 | [diff] [blame] | 303 | static_cast<AdjustChannelsBufferProvider*>( |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 304 | mAdjustChannelsBufferProvider.get())->clearContractedFrames(); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 308 | void AudioMixer::Track::reconfigureBufferProviders() |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 309 | { |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 310 | // configure from upstream to downstream buffer providers. |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 311 | bufferProvider = mInputBufferProvider; |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 312 | if (mAdjustChannelsBufferProvider.get() != nullptr) { |
| 313 | mAdjustChannelsBufferProvider->setBufferProvider(bufferProvider); |
| 314 | bufferProvider = mAdjustChannelsBufferProvider.get(); |
| 315 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 316 | if (mReformatBufferProvider.get() != nullptr) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 317 | mReformatBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 318 | bufferProvider = mReformatBufferProvider.get(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 319 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 320 | if (mDownmixerBufferProvider.get() != nullptr) { |
| 321 | mDownmixerBufferProvider->setBufferProvider(bufferProvider); |
| 322 | bufferProvider = mDownmixerBufferProvider.get(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 323 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 324 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 325 | mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 326 | bufferProvider = mPostDownmixReformatBufferProvider.get(); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 327 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 328 | if (mTimestretchBufferProvider.get() != nullptr) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 329 | mTimestretchBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 330 | bufferProvider = mTimestretchBufferProvider.get(); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 331 | } |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 334 | void AudioMixer::setParameter(int name, int target, int param, void *value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 335 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 336 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 337 | const std::shared_ptr<Track> &track = getTrack(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 338 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 339 | int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value)); |
| 340 | int32_t *valueBuf = reinterpret_cast<int32_t*>(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 341 | |
| 342 | switch (target) { |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 343 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 344 | case TRACK: |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 345 | switch (param) { |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 346 | case CHANNEL_MASK: { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 347 | const audio_channel_mask_t trackChannelMask = |
| 348 | static_cast<audio_channel_mask_t>(valueInt); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 349 | if (setChannelMasks(name, trackChannelMask, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 350 | static_cast<audio_channel_mask_t>( |
| 351 | track->mMixerChannelMask | track->mMixerHapticChannelMask))) { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 352 | ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 353 | invalidate(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 354 | } |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 355 | } break; |
| 356 | case MAIN_BUFFER: |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 357 | if (track->mainBuffer != valueBuf) { |
| 358 | track->mainBuffer = valueBuf; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 359 | ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 360 | if (track->mKeepContractedChannels) { |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 361 | track->prepareForAdjustChannels(mFrameCount); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 362 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 363 | invalidate(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 364 | } |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 365 | break; |
| 366 | case AUX_BUFFER: |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 367 | AudioMixerBase::setParameter(name, target, param, value); |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 368 | break; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 369 | case FORMAT: { |
| 370 | audio_format_t format = static_cast<audio_format_t>(valueInt); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 371 | if (track->mFormat != format) { |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 372 | ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 373 | track->mFormat = format; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 374 | ALOGV("setParameter(TRACK, FORMAT, %#x)", format); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 375 | track->prepareForReformat(); |
| 376 | invalidate(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 377 | } |
| 378 | } break; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 379 | // FIXME do we want to support setting the downmix type from AudioFlinger? |
| 380 | // for a specific track? or per mixer? |
| 381 | /* case DOWNMIX_TYPE: |
| 382 | break */ |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 383 | case MIXER_FORMAT: { |
Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 384 | audio_format_t format = static_cast<audio_format_t>(valueInt); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 385 | if (track->mMixerFormat != format) { |
| 386 | track->mMixerFormat = format; |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 387 | ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 388 | if (track->mKeepContractedChannels) { |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 389 | track->prepareForAdjustChannels(mFrameCount); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 390 | } |
Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 391 | } |
| 392 | } break; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 393 | case MIXER_CHANNEL_MASK: { |
| 394 | const audio_channel_mask_t mixerChannelMask = |
| 395 | static_cast<audio_channel_mask_t>(valueInt); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 396 | if (setChannelMasks(name, static_cast<audio_channel_mask_t>( |
| 397 | track->channelMask | track->mHapticChannelMask), |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 398 | mixerChannelMask)) { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 399 | ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 400 | invalidate(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 401 | } |
| 402 | } break; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 403 | case HAPTIC_ENABLED: { |
| 404 | const bool hapticPlaybackEnabled = static_cast<bool>(valueInt); |
| 405 | if (track->mHapticPlaybackEnabled != hapticPlaybackEnabled) { |
| 406 | track->mHapticPlaybackEnabled = hapticPlaybackEnabled; |
| 407 | track->mKeepContractedChannels = hapticPlaybackEnabled; |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 408 | track->prepareForAdjustChannels(mFrameCount); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 409 | } |
| 410 | } break; |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 411 | case HAPTIC_INTENSITY: { |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 412 | const os::HapticScale hapticIntensity = static_cast<os::HapticScale>(valueInt); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 413 | if (track->mHapticIntensity != hapticIntensity) { |
| 414 | track->mHapticIntensity = hapticIntensity; |
| 415 | } |
| 416 | } break; |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 417 | case HAPTIC_MAX_AMPLITUDE: { |
| 418 | const float hapticMaxAmplitude = *reinterpret_cast<float*>(value); |
| 419 | if (track->mHapticMaxAmplitude != hapticMaxAmplitude) { |
| 420 | track->mHapticMaxAmplitude = hapticMaxAmplitude; |
| 421 | } |
| 422 | } break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 423 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 424 | LOG_ALWAYS_FATAL("setParameter track: bad param %d", param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 425 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 426 | break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 427 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 428 | case RESAMPLE: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 429 | case RAMP_VOLUME: |
| 430 | case VOLUME: |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 431 | AudioMixerBase::setParameter(name, target, param, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 432 | break; |
Mikhail Naganov | 3b73e99 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 433 | case TIMESTRETCH: |
| 434 | switch (param) { |
| 435 | case PLAYBACK_RATE: { |
| 436 | const AudioPlaybackRate *playbackRate = |
| 437 | reinterpret_cast<AudioPlaybackRate*>(value); |
| 438 | ALOGW_IF(!isAudioPlaybackRateValid(*playbackRate), |
| 439 | "bad parameters speed %f, pitch %f", |
| 440 | playbackRate->mSpeed, playbackRate->mPitch); |
| 441 | if (track->setPlaybackRate(*playbackRate)) { |
| 442 | ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, STRETCH_MODE, FALLBACK_MODE " |
| 443 | "%f %f %d %d", |
| 444 | playbackRate->mSpeed, |
| 445 | playbackRate->mPitch, |
| 446 | playbackRate->mStretchMode, |
| 447 | playbackRate->mFallbackMode); |
| 448 | // invalidate(); (should not require reconfigure) |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 449 | } |
Mikhail Naganov | 3b73e99 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 450 | } break; |
| 451 | default: |
| 452 | LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param); |
| 453 | } |
| 454 | break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 455 | |
| 456 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 457 | LOG_ALWAYS_FATAL("setParameter: bad target %d", target); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 458 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 461 | bool AudioMixer::Track::setPlaybackRate(const AudioPlaybackRate &playbackRate) |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 462 | { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 463 | if ((mTimestretchBufferProvider.get() == nullptr && |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 464 | fabs(playbackRate.mSpeed - mPlaybackRate.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA && |
| 465 | fabs(playbackRate.mPitch - mPlaybackRate.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) || |
| 466 | isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 467 | return false; |
| 468 | } |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 469 | mPlaybackRate = playbackRate; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 470 | if (mTimestretchBufferProvider.get() == nullptr) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 471 | // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer |
| 472 | // but if none exists, it is the channel count (1 for mono). |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 473 | const int timestretchChannelCount = getOutputChannelCount(); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 474 | mTimestretchBufferProvider.reset(new TimestretchBufferProvider(timestretchChannelCount, |
| 475 | mMixerInFormat, sampleRate, playbackRate)); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 476 | reconfigureBufferProviders(); |
| 477 | } else { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 478 | static_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider.get()) |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 479 | ->setPlaybackRate(playbackRate); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 480 | } |
| 481 | return true; |
| 482 | } |
| 483 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 484 | void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 485 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 486 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 487 | const std::shared_ptr<Track> &track = getTrack(name); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 488 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 489 | if (track->mInputBufferProvider == bufferProvider) { |
Andy Hung | 1d26ddf | 2014-05-29 15:53:09 -0700 | [diff] [blame] | 490 | return; // don't reset any buffer providers if identical. |
| 491 | } |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 492 | // reset order from downstream to upstream buffer providers. |
| 493 | if (track->mTimestretchBufferProvider.get() != nullptr) { |
| 494 | track->mTimestretchBufferProvider->reset(); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 495 | } else if (track->mPostDownmixReformatBufferProvider.get() != nullptr) { |
| 496 | track->mPostDownmixReformatBufferProvider->reset(); |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 497 | } else if (track->mDownmixerBufferProvider != nullptr) { |
| 498 | track->mDownmixerBufferProvider->reset(); |
| 499 | } else if (track->mReformatBufferProvider.get() != nullptr) { |
| 500 | track->mReformatBufferProvider->reset(); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 501 | } else if (track->mAdjustChannelsBufferProvider.get() != nullptr) { |
| 502 | track->mAdjustChannelsBufferProvider->reset(); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 503 | } |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 504 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 505 | track->mInputBufferProvider = bufferProvider; |
| 506 | track->reconfigureBufferProviders(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Glenn Kasten | 52008f8 | 2012-03-18 09:34:41 -0700 | [diff] [blame] | 509 | /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT; |
| 510 | |
| 511 | /*static*/ void AudioMixer::sInitRoutine() |
| 512 | { |
Andy Hung | 34803d5 | 2014-07-16 21:41:35 -0700 | [diff] [blame] | 513 | DownmixerBufferProvider::init(); // for the downmixer |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 516 | std::shared_ptr<AudioMixerBase::TrackBase> AudioMixer::preCreateTrack() |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 517 | { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 518 | return std::make_shared<Track>(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 521 | status_t AudioMixer::postCreateTrack(TrackBase *track) |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 522 | { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 523 | Track* t = static_cast<Track*>(track); |
| 524 | |
| 525 | audio_channel_mask_t channelMask = t->channelMask; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 526 | t->mHapticChannelMask = static_cast<audio_channel_mask_t>( |
| 527 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 528 | t->mHapticChannelCount = audio_channel_count_from_out_mask(t->mHapticChannelMask); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 529 | channelMask = static_cast<audio_channel_mask_t>(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 530 | t->channelCount = audio_channel_count_from_out_mask(channelMask); |
| 531 | ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO, |
| 532 | "Non-stereo channel mask: %d\n", channelMask); |
| 533 | t->channelMask = channelMask; |
| 534 | t->mInputBufferProvider = NULL; |
| 535 | t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required |
| 536 | t->mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT; |
| 537 | // haptic |
| 538 | t->mHapticPlaybackEnabled = false; |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 539 | t->mHapticIntensity = os::HapticScale::NONE; |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 540 | t->mHapticMaxAmplitude = NAN; |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 541 | t->mMixerHapticChannelMask = AUDIO_CHANNEL_NONE; |
| 542 | t->mMixerHapticChannelCount = 0; |
| 543 | t->mAdjustInChannelCount = t->channelCount + t->mHapticChannelCount; |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 544 | t->mAdjustOutChannelCount = t->channelCount; |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 545 | t->mKeepContractedChannels = false; |
| 546 | // Check the downmixing (or upmixing) requirements. |
| 547 | status_t status = t->prepareForDownmix(); |
| 548 | if (status != OK) { |
| 549 | ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask); |
| 550 | return BAD_VALUE; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 551 | } |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 552 | // prepareForDownmix() may change mDownmixRequiresFormat |
| 553 | ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat); |
| 554 | t->prepareForReformat(); |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame] | 555 | t->prepareForAdjustChannels(mFrameCount); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 556 | return OK; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 559 | void AudioMixer::preProcess() |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 560 | { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 561 | for (const auto &pair : mTracks) { |
| 562 | // Clear contracted buffer before processing if contracted channels are saved |
| 563 | const std::shared_ptr<TrackBase> &tb = pair.second; |
| 564 | Track *t = static_cast<Track*>(tb.get()); |
| 565 | if (t->mKeepContractedChannels) { |
| 566 | t->clearContractedBuffer(); |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 571 | void AudioMixer::postProcess() |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 572 | { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 573 | // Process haptic data. |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 574 | // Need to keep consistent with VibrationEffect.scale(int, float, int) |
| 575 | for (const auto &pair : mGroups) { |
| 576 | // process by group of tracks with same output main buffer. |
| 577 | const auto &group = pair.second; |
| 578 | for (const int name : group) { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 579 | const std::shared_ptr<Track> &t = getTrack(name); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 580 | if (t->mHapticPlaybackEnabled) { |
| 581 | size_t sampleCount = mFrameCount * t->mMixerHapticChannelCount; |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 582 | uint8_t* buffer = (uint8_t*)pair.first + mFrameCount * audio_bytes_per_frame( |
| 583 | t->mMixerChannelCount, t->mMixerFormat); |
| 584 | switch (t->mMixerFormat) { |
| 585 | // Mixer format should be AUDIO_FORMAT_PCM_FLOAT. |
| 586 | case AUDIO_FORMAT_PCM_FLOAT: { |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 587 | os::scaleHapticData((float*) buffer, sampleCount, t->mHapticIntensity, |
| 588 | t->mHapticMaxAmplitude); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 589 | } break; |
| 590 | default: |
| 591 | LOG_ALWAYS_FATAL("bad mMixerFormat: %#x", t->mMixerFormat); |
| 592 | break; |
| 593 | } |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 600 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 601 | } // namespace android |