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 | |
| 191 | // 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] | 192 | mDownmixerBufferProvider.reset(new RemixBufferProvider(channelMask, |
| 193 | mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount)); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 194 | // Remix always finds a conversion whereas Downmixer effect above may fail. |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 195 | reconfigureBufferProviders(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 196 | return NO_ERROR; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 199 | void AudioMixer::Track::unprepareForReformat() { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 200 | ALOGV("AudioMixer::unprepareForReformat(%p)", this); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 201 | bool requiresReconfigure = false; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 202 | if (mReformatBufferProvider.get() != nullptr) { |
| 203 | mReformatBufferProvider.reset(nullptr); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 204 | requiresReconfigure = true; |
| 205 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 206 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
| 207 | mPostDownmixReformatBufferProvider.reset(nullptr); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 208 | requiresReconfigure = true; |
| 209 | } |
| 210 | if (requiresReconfigure) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 211 | reconfigureBufferProviders(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 215 | status_t AudioMixer::Track::prepareForReformat() |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 216 | { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 217 | ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 218 | // discard previous reformatters |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 219 | unprepareForReformat(); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 220 | // only configure reformatters as needed |
| 221 | const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID |
| 222 | ? mDownmixRequiresFormat : mMixerInFormat; |
| 223 | bool requiresReconfigure = false; |
| 224 | if (mFormat != targetFormat) { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 225 | mReformatBufferProvider.reset(new ReformatBufferProvider( |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 226 | audio_channel_count_from_out_mask(channelMask), |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 227 | mFormat, |
| 228 | targetFormat, |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 229 | kCopyBufferFrameCount)); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 230 | requiresReconfigure = true; |
Kevin Rocard | e053bfa | 2017-11-09 22:07:34 -0800 | [diff] [blame] | 231 | } else if (mFormat == AUDIO_FORMAT_PCM_FLOAT) { |
| 232 | // Input and output are floats, make sure application did not provide > 3db samples |
| 233 | // that would break volume application (b/68099072) |
| 234 | // TODO: add a trusted source flag to avoid the overhead |
| 235 | mReformatBufferProvider.reset(new ClampFloatBufferProvider( |
| 236 | audio_channel_count_from_out_mask(channelMask), |
| 237 | kCopyBufferFrameCount)); |
| 238 | requiresReconfigure = true; |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 239 | } |
| 240 | if (targetFormat != mMixerInFormat) { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 241 | mPostDownmixReformatBufferProvider.reset(new ReformatBufferProvider( |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 242 | audio_channel_count_from_out_mask(mMixerChannelMask), |
| 243 | targetFormat, |
| 244 | mMixerInFormat, |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 245 | kCopyBufferFrameCount)); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 246 | requiresReconfigure = true; |
| 247 | } |
| 248 | if (requiresReconfigure) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 249 | reconfigureBufferProviders(); |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 250 | } |
| 251 | return NO_ERROR; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 252 | } |
| 253 | |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 254 | void AudioMixer::Track::unprepareForAdjustChannels() |
| 255 | { |
| 256 | ALOGV("AUDIOMIXER::unprepareForAdjustChannels"); |
| 257 | if (mAdjustChannelsBufferProvider.get() != nullptr) { |
| 258 | mAdjustChannelsBufferProvider.reset(nullptr); |
| 259 | reconfigureBufferProviders(); |
| 260 | } |
| 261 | } |
| 262 | |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 263 | status_t AudioMixer::Track::prepareForAdjustChannels(size_t frames) |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 264 | { |
| 265 | ALOGV("AudioMixer::prepareForAdjustChannels(%p) with inChannelCount: %u, outChannelCount: %u", |
| 266 | this, mAdjustInChannelCount, mAdjustOutChannelCount); |
| 267 | unprepareForAdjustChannels(); |
| 268 | if (mAdjustInChannelCount != mAdjustOutChannelCount) { |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 269 | uint8_t* buffer = mKeepContractedChannels |
| 270 | ? (uint8_t*)mainBuffer + frames * audio_bytes_per_frame( |
| 271 | mMixerChannelCount, mMixerFormat) |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 272 | : nullptr; |
| 273 | mAdjustChannelsBufferProvider.reset(new AdjustChannelsBufferProvider( |
| 274 | mFormat, mAdjustInChannelCount, mAdjustOutChannelCount, frames, |
| 275 | mKeepContractedChannels ? mMixerFormat : AUDIO_FORMAT_INVALID, |
| 276 | buffer, mMixerHapticChannelCount)); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 277 | reconfigureBufferProviders(); |
| 278 | } |
| 279 | return NO_ERROR; |
| 280 | } |
| 281 | |
| 282 | void AudioMixer::Track::clearContractedBuffer() |
| 283 | { |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 284 | if (mAdjustChannelsBufferProvider.get() != nullptr) { |
jiabin | ea8fa7a | 2019-02-22 14:41:50 -0800 | [diff] [blame] | 285 | static_cast<AdjustChannelsBufferProvider*>( |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 286 | mAdjustChannelsBufferProvider.get())->clearContractedFrames(); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 290 | void AudioMixer::Track::reconfigureBufferProviders() |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 291 | { |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 292 | // configure from upstream to downstream buffer providers. |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 293 | bufferProvider = mInputBufferProvider; |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 294 | if (mAdjustChannelsBufferProvider.get() != nullptr) { |
| 295 | mAdjustChannelsBufferProvider->setBufferProvider(bufferProvider); |
| 296 | bufferProvider = mAdjustChannelsBufferProvider.get(); |
| 297 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 298 | if (mReformatBufferProvider.get() != nullptr) { |
Andy Hung | 0f451e9 | 2014-08-04 21:28:47 -0700 | [diff] [blame] | 299 | mReformatBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 300 | bufferProvider = mReformatBufferProvider.get(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 301 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 302 | if (mDownmixerBufferProvider.get() != nullptr) { |
| 303 | mDownmixerBufferProvider->setBufferProvider(bufferProvider); |
| 304 | bufferProvider = mDownmixerBufferProvider.get(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 305 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 306 | if (mPostDownmixReformatBufferProvider.get() != nullptr) { |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 307 | mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 308 | bufferProvider = mPostDownmixReformatBufferProvider.get(); |
Andy Hung | 7f47549 | 2014-08-25 16:36:37 -0700 | [diff] [blame] | 309 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 310 | if (mTimestretchBufferProvider.get() != nullptr) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 311 | mTimestretchBufferProvider->setBufferProvider(bufferProvider); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 312 | bufferProvider = mTimestretchBufferProvider.get(); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 313 | } |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 316 | void AudioMixer::setParameter(int name, int target, int param, void *value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 317 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 318 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 319 | const std::shared_ptr<Track> &track = getTrack(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 320 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 321 | int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value)); |
| 322 | int32_t *valueBuf = reinterpret_cast<int32_t*>(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 323 | |
| 324 | switch (target) { |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 325 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 326 | case TRACK: |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 327 | switch (param) { |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 328 | case CHANNEL_MASK: { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 329 | const audio_channel_mask_t trackChannelMask = |
| 330 | static_cast<audio_channel_mask_t>(valueInt); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 331 | if (setChannelMasks(name, trackChannelMask, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 332 | static_cast<audio_channel_mask_t>( |
| 333 | track->mMixerChannelMask | track->mMixerHapticChannelMask))) { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 334 | ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 335 | invalidate(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 336 | } |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 337 | } break; |
| 338 | case MAIN_BUFFER: |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 339 | if (track->mainBuffer != valueBuf) { |
| 340 | track->mainBuffer = valueBuf; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 341 | ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 342 | if (track->mKeepContractedChannels) { |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 343 | track->prepareForAdjustChannels(mFrameCount); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 344 | } |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 345 | invalidate(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 346 | } |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 347 | break; |
| 348 | case AUX_BUFFER: |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 349 | AudioMixerBase::setParameter(name, target, param, value); |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 350 | break; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 351 | case FORMAT: { |
| 352 | audio_format_t format = static_cast<audio_format_t>(valueInt); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 353 | if (track->mFormat != format) { |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 354 | ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 355 | track->mFormat = format; |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 356 | ALOGV("setParameter(TRACK, FORMAT, %#x)", format); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 357 | track->prepareForReformat(); |
| 358 | invalidate(); |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 359 | } |
| 360 | } break; |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 361 | // FIXME do we want to support setting the downmix type from AudioFlinger? |
| 362 | // for a specific track? or per mixer? |
| 363 | /* case DOWNMIX_TYPE: |
| 364 | break */ |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 365 | case MIXER_FORMAT: { |
Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 366 | audio_format_t format = static_cast<audio_format_t>(valueInt); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 367 | if (track->mMixerFormat != format) { |
| 368 | track->mMixerFormat = format; |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 369 | ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 370 | if (track->mKeepContractedChannels) { |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 371 | track->prepareForAdjustChannels(mFrameCount); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 372 | } |
Andy Hung | a1ab7cc | 2014-02-24 19:26:52 -0800 | [diff] [blame] | 373 | } |
| 374 | } break; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 375 | case MIXER_CHANNEL_MASK: { |
| 376 | const audio_channel_mask_t mixerChannelMask = |
| 377 | static_cast<audio_channel_mask_t>(valueInt); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 378 | if (setChannelMasks(name, static_cast<audio_channel_mask_t>( |
| 379 | track->channelMask | track->mHapticChannelMask), |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 380 | mixerChannelMask)) { |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 381 | ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 382 | invalidate(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 383 | } |
| 384 | } break; |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 385 | case HAPTIC_ENABLED: { |
| 386 | const bool hapticPlaybackEnabled = static_cast<bool>(valueInt); |
| 387 | if (track->mHapticPlaybackEnabled != hapticPlaybackEnabled) { |
| 388 | track->mHapticPlaybackEnabled = hapticPlaybackEnabled; |
| 389 | track->mKeepContractedChannels = hapticPlaybackEnabled; |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 390 | track->prepareForAdjustChannels(mFrameCount); |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 391 | } |
| 392 | } break; |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 393 | case HAPTIC_INTENSITY: { |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 394 | const os::HapticScale hapticIntensity = static_cast<os::HapticScale>(valueInt); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 395 | if (track->mHapticIntensity != hapticIntensity) { |
| 396 | track->mHapticIntensity = hapticIntensity; |
| 397 | } |
| 398 | } break; |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 399 | case HAPTIC_MAX_AMPLITUDE: { |
| 400 | const float hapticMaxAmplitude = *reinterpret_cast<float*>(value); |
| 401 | if (track->mHapticMaxAmplitude != hapticMaxAmplitude) { |
| 402 | track->mHapticMaxAmplitude = hapticMaxAmplitude; |
| 403 | } |
| 404 | } break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 405 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 406 | LOG_ALWAYS_FATAL("setParameter track: bad param %d", param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 407 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 408 | break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 409 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 410 | case RESAMPLE: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 411 | case RAMP_VOLUME: |
| 412 | case VOLUME: |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 413 | AudioMixerBase::setParameter(name, target, param, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 414 | break; |
Mikhail Naganov | 3b73e99 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 415 | case TIMESTRETCH: |
| 416 | switch (param) { |
| 417 | case PLAYBACK_RATE: { |
| 418 | const AudioPlaybackRate *playbackRate = |
| 419 | reinterpret_cast<AudioPlaybackRate*>(value); |
| 420 | ALOGW_IF(!isAudioPlaybackRateValid(*playbackRate), |
| 421 | "bad parameters speed %f, pitch %f", |
| 422 | playbackRate->mSpeed, playbackRate->mPitch); |
| 423 | if (track->setPlaybackRate(*playbackRate)) { |
| 424 | ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, STRETCH_MODE, FALLBACK_MODE " |
| 425 | "%f %f %d %d", |
| 426 | playbackRate->mSpeed, |
| 427 | playbackRate->mPitch, |
| 428 | playbackRate->mStretchMode, |
| 429 | playbackRate->mFallbackMode); |
| 430 | // invalidate(); (should not require reconfigure) |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 431 | } |
Mikhail Naganov | 3b73e99 | 2019-07-31 14:53:29 -0700 | [diff] [blame] | 432 | } break; |
| 433 | default: |
| 434 | LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param); |
| 435 | } |
| 436 | break; |
Glenn Kasten | 788040c | 2011-05-05 08:19:00 -0700 | [diff] [blame] | 437 | |
| 438 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 439 | LOG_ALWAYS_FATAL("setParameter: bad target %d", target); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 440 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 443 | bool AudioMixer::Track::setPlaybackRate(const AudioPlaybackRate &playbackRate) |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 444 | { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 445 | if ((mTimestretchBufferProvider.get() == nullptr && |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 446 | fabs(playbackRate.mSpeed - mPlaybackRate.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA && |
| 447 | fabs(playbackRate.mPitch - mPlaybackRate.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) || |
| 448 | isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 449 | return false; |
| 450 | } |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 451 | mPlaybackRate = playbackRate; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 452 | if (mTimestretchBufferProvider.get() == nullptr) { |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 453 | // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer |
| 454 | // but if none exists, it is the channel count (1 for mono). |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 455 | const int timestretchChannelCount = getOutputChannelCount(); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 456 | mTimestretchBufferProvider.reset(new TimestretchBufferProvider(timestretchChannelCount, |
| 457 | mMixerInFormat, sampleRate, playbackRate)); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 458 | reconfigureBufferProviders(); |
| 459 | } else { |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 460 | static_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider.get()) |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 461 | ->setPlaybackRate(playbackRate); |
Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 462 | } |
| 463 | return true; |
| 464 | } |
| 465 | |
Glenn Kasten | 01c4ebf | 2012-02-22 10:47:35 -0800 | [diff] [blame] | 466 | void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 467 | { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 468 | LOG_ALWAYS_FATAL_IF(!exists(name), "invalid name: %d", name); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 469 | const std::shared_ptr<Track> &track = getTrack(name); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 470 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 471 | if (track->mInputBufferProvider == bufferProvider) { |
Andy Hung | 1d26ddf | 2014-05-29 15:53:09 -0700 | [diff] [blame] | 472 | return; // don't reset any buffer providers if identical. |
| 473 | } |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 474 | // reset order from downstream to upstream buffer providers. |
| 475 | if (track->mTimestretchBufferProvider.get() != nullptr) { |
| 476 | track->mTimestretchBufferProvider->reset(); |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 477 | } else if (track->mPostDownmixReformatBufferProvider.get() != nullptr) { |
| 478 | track->mPostDownmixReformatBufferProvider->reset(); |
Andy Hung | 3a34df9 | 2018-08-21 12:32:30 -0700 | [diff] [blame] | 479 | } else if (track->mDownmixerBufferProvider != nullptr) { |
| 480 | track->mDownmixerBufferProvider->reset(); |
| 481 | } else if (track->mReformatBufferProvider.get() != nullptr) { |
| 482 | track->mReformatBufferProvider->reset(); |
jiabin | dce8f8c | 2018-12-10 17:49:31 -0800 | [diff] [blame] | 483 | } else if (track->mAdjustChannelsBufferProvider.get() != nullptr) { |
| 484 | track->mAdjustChannelsBufferProvider->reset(); |
Jean-Michel Trivi | 7d5b262 | 2012-04-04 18:54:36 -0700 | [diff] [blame] | 485 | } |
Andy Hung | ef7c7fb | 2014-05-12 16:51:41 -0700 | [diff] [blame] | 486 | |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 487 | track->mInputBufferProvider = bufferProvider; |
| 488 | track->reconfigureBufferProviders(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Glenn Kasten | 52008f8 | 2012-03-18 09:34:41 -0700 | [diff] [blame] | 491 | /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT; |
| 492 | |
| 493 | /*static*/ void AudioMixer::sInitRoutine() |
| 494 | { |
Andy Hung | 34803d5 | 2014-07-16 21:41:35 -0700 | [diff] [blame] | 495 | DownmixerBufferProvider::init(); // for the downmixer |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 498 | std::shared_ptr<AudioMixerBase::TrackBase> AudioMixer::preCreateTrack() |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 499 | { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 500 | return std::make_shared<Track>(); |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 503 | status_t AudioMixer::postCreateTrack(TrackBase *track) |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 504 | { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 505 | Track* t = static_cast<Track*>(track); |
| 506 | |
| 507 | audio_channel_mask_t channelMask = t->channelMask; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 508 | t->mHapticChannelMask = static_cast<audio_channel_mask_t>( |
| 509 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 510 | t->mHapticChannelCount = audio_channel_count_from_out_mask(t->mHapticChannelMask); |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 511 | channelMask = static_cast<audio_channel_mask_t>(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 512 | t->channelCount = audio_channel_count_from_out_mask(channelMask); |
| 513 | ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO, |
| 514 | "Non-stereo channel mask: %d\n", channelMask); |
| 515 | t->channelMask = channelMask; |
| 516 | t->mInputBufferProvider = NULL; |
| 517 | t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required |
| 518 | t->mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT; |
| 519 | // haptic |
| 520 | t->mHapticPlaybackEnabled = false; |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 521 | t->mHapticIntensity = os::HapticScale::NONE; |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 522 | t->mHapticMaxAmplitude = NAN; |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 523 | t->mMixerHapticChannelMask = AUDIO_CHANNEL_NONE; |
| 524 | t->mMixerHapticChannelCount = 0; |
| 525 | t->mAdjustInChannelCount = t->channelCount + t->mHapticChannelCount; |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 526 | t->mAdjustOutChannelCount = t->channelCount; |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 527 | t->mKeepContractedChannels = false; |
| 528 | // Check the downmixing (or upmixing) requirements. |
| 529 | status_t status = t->prepareForDownmix(); |
| 530 | if (status != OK) { |
| 531 | ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask); |
| 532 | return BAD_VALUE; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 533 | } |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 534 | // prepareForDownmix() may change mDownmixRequiresFormat |
| 535 | ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat); |
| 536 | t->prepareForReformat(); |
jiabin | 0e6babc | 2021-10-21 20:35:05 +0000 | [diff] [blame^] | 537 | t->prepareForAdjustChannels(mFrameCount); |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 538 | return OK; |
Andy Hung | e93b6b7 | 2014-07-17 21:30:53 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 541 | void AudioMixer::preProcess() |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 542 | { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 543 | for (const auto &pair : mTracks) { |
| 544 | // Clear contracted buffer before processing if contracted channels are saved |
| 545 | const std::shared_ptr<TrackBase> &tb = pair.second; |
| 546 | Track *t = static_cast<Track*>(tb.get()); |
| 547 | if (t->mKeepContractedChannels) { |
| 548 | t->clearContractedBuffer(); |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 553 | void AudioMixer::postProcess() |
Andy Hung | 296b741 | 2014-06-17 15:25:47 -0700 | [diff] [blame] | 554 | { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 555 | // Process haptic data. |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 556 | // Need to keep consistent with VibrationEffect.scale(int, float, int) |
| 557 | for (const auto &pair : mGroups) { |
| 558 | // process by group of tracks with same output main buffer. |
| 559 | const auto &group = pair.second; |
| 560 | for (const int name : group) { |
Mikhail Naganov | 7ad7a25 | 2019-07-30 14:42:32 -0700 | [diff] [blame] | 561 | const std::shared_ptr<Track> &t = getTrack(name); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 562 | if (t->mHapticPlaybackEnabled) { |
| 563 | size_t sampleCount = mFrameCount * t->mMixerHapticChannelCount; |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 564 | uint8_t* buffer = (uint8_t*)pair.first + mFrameCount * audio_bytes_per_frame( |
| 565 | t->mMixerChannelCount, t->mMixerFormat); |
| 566 | switch (t->mMixerFormat) { |
| 567 | // Mixer format should be AUDIO_FORMAT_PCM_FLOAT. |
| 568 | case AUDIO_FORMAT_PCM_FLOAT: { |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 569 | os::scaleHapticData((float*) buffer, sampleCount, t->mHapticIntensity, |
| 570 | t->mHapticMaxAmplitude); |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 571 | } break; |
| 572 | default: |
| 573 | LOG_ALWAYS_FATAL("bad mMixerFormat: %#x", t->mMixerFormat); |
| 574 | break; |
| 575 | } |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 582 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 583 | } // namespace android |