blob: 080b0a6f661c31fee96b66ae0175452b5a84b8ec [file] [log] [blame]
Glenn Kasten97b5d0d2012-03-23 18:54:19 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Glenn Kastena3d26282012-11-30 07:57:43 -080017// <IMPORTANT_WARNING>
18// Design rules for threadLoop() are given in the comments at section "Fast mixer thread" of
19// StateQueue.h. In particular, avoid library and system calls except at well-known points.
20// The design rules are only for threadLoop(), and don't apply to FastMixerDumpState methods.
21// </IMPORTANT_WARNING>
22
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070023#define LOG_TAG "FastMixer"
Glenn Kasten7f5d3352013-02-15 23:55:04 +000024//#define LOG_NDEBUG 0
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070025
Alex Rayb3a83642012-11-30 19:42:28 -080026#define ATRACE_TAG ATRACE_TAG_AUDIO
Alex Ray371eb972012-11-30 11:11:54 -080027
Glenn Kasten153b9fe2013-07-15 11:23:36 -070028#include "Configuration.h"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070029#include <time.h>
30#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070031#include <utils/Trace.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070032#include <system/audio.h>
Glenn Kasten214b4062015-03-02 14:15:47 -080033#ifdef FAST_THREAD_STATISTICS
Eric Tan5b13ff82018-07-27 11:20:17 -070034#include <audio_utils/Statistics.h>
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070035#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070036#include <cpustats/ThreadCpuUsage.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070037#endif
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070038#endif
jiabin245cdd92018-12-07 17:55:15 -080039#include <audio_utils/channels.h>
Andy Hung1258c1a2014-05-23 21:22:17 -070040#include <audio_utils/format.h>
jiabin245cdd92018-12-07 17:55:15 -080041#include <audio_utils/mono_blend.h>
Glenn Kastenc9a23672020-06-30 12:12:42 -070042#include <cutils/bitops.h>
Andy Hung068561c2017-01-03 17:09:32 -080043#include <media/AudioMixer.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070044#include "FastMixer.h"
Andy Hungb776e372023-05-24 11:53:47 -070045#include <afutils/TypedLogger.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070046
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070047namespace android {
48
Glenn Kastene4a7ce22015-03-03 11:23:17 -080049/*static*/ const FastMixerState FastMixer::sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070050
Andy Hung1b998522021-06-07 16:43:58 -070051static audio_channel_mask_t getChannelMaskFromCount(size_t count) {
52 const audio_channel_mask_t mask = audio_channel_out_mask_from_count(count);
53 if (mask == AUDIO_CHANNEL_INVALID) {
54 // some counts have no positional masks. TODO: Update this to return index count?
55 return audio_channel_mask_for_index_assignment_from_count(count);
56 }
57 return mask;
58}
59
Andy Hung8946a282018-04-19 20:04:56 -070060FastMixer::FastMixer(audio_io_handle_t parentIoHandle)
61 : FastThread("cycle_ms", "load_us"),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080062 // mFastTrackNames
63 // mGenerations
64 mOutputSink(NULL),
65 mOutputSinkGen(0),
66 mMixer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070067 mSinkBuffer(NULL),
68 mSinkBufferSize(0),
Andy Hung9a592762014-07-21 21:56:01 -070069 mSinkChannelCount(FCC_2),
Andy Hung45d68d32014-05-23 21:13:31 -070070 mMixerBuffer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070071 mMixerBufferSize(0),
Andy Hung45d68d32014-05-23 21:13:31 -070072 mMixerBufferState(UNDEFINED),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080073 mFormat(Format_Invalid),
74 mSampleRate(0),
75 mFastTracksGen(0),
76 mTotalNativeFramesWritten(0),
Glenn Kasten22340022014-04-07 12:04:41 -070077 // timestamp
Andy Hung2ddee192015-12-18 17:34:44 -080078 mNativeFramesWrittenButNotPresented(0), // the = 0 is to silence the compiler
Andy Hung8946a282018-04-19 20:04:56 -070079 mMasterMono(false),
80 mThreadIoHandle(parentIoHandle)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070081{
Glenn Kastene4a7ce22015-03-03 11:23:17 -080082 // FIXME pass sInitial as parameter to base class constructor, and make it static local
83 mPrevious = &sInitial;
84 mCurrent = &sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070085
Glenn Kastene4a7ce22015-03-03 11:23:17 -080086 mDummyDumpState = &mDummyFastMixerDumpState;
Andy Hung9a592762014-07-21 21:56:01 -070087 // TODO: Add channel mask to NBAIO_Format.
88 // We assume that the channel mask must be a valid positional channel mask.
Andy Hung1b998522021-06-07 16:43:58 -070089 mSinkChannelMask = getChannelMaskFromCount(mSinkChannelCount);
Andy Hung40964062021-10-27 18:26:48 -070090 mBalance.setChannelMask(mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -070091
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070092 unsigned i;
Glenn Kastendc2c50b2016-04-21 08:13:14 -070093 for (i = 0; i < FastMixerState::sMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -080094 mGenerations[i] = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070095 }
Glenn Kasten214b4062015-03-02 14:15:47 -080096#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080097 mOldLoad.tv_sec = 0;
98 mOldLoad.tv_nsec = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070099#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700100}
101
102FastMixer::~FastMixer()
103{
104}
105
106FastMixerStateQueue* FastMixer::sq()
107{
108 return &mSQ;
109}
110
111const FastThreadState *FastMixer::poll()
112{
113 return mSQ.poll();
114}
115
Mikhail Naganov9b6599e2019-07-29 15:23:21 -0700116void FastMixer::setNBLogWriter(NBLog::Writer *logWriter __unused)
Glenn Kasten22340022014-04-07 12:04:41 -0700117{
Glenn Kasten22340022014-04-07 12:04:41 -0700118}
119
120void FastMixer::onIdle()
121{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800122 mPreIdle = *(const FastMixerState *)mCurrent;
123 mCurrent = &mPreIdle;
Glenn Kasten22340022014-04-07 12:04:41 -0700124}
125
126void FastMixer::onExit()
127{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800128 delete mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -0700129 free(mMixerBuffer);
130 free(mSinkBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700131}
132
133bool FastMixer::isSubClassCommand(FastThreadState::Command command)
134{
135 switch ((FastMixerState::Command) command) {
136 case FastMixerState::MIX:
137 case FastMixerState::WRITE:
138 case FastMixerState::MIX_WRITE:
139 return true;
140 default:
141 return false;
142 }
143}
144
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800145void FastMixer::updateMixerTrack(int index, Reason reason) {
146 const FastMixerState * const current = (const FastMixerState *) mCurrent;
147 const FastTrack * const fastTrack = &current->mFastTracks[index];
148
149 // check and update generation
150 if (reason == REASON_MODIFY && mGenerations[index] == fastTrack->mGeneration) {
151 return; // no change on an already configured track.
152 }
153 mGenerations[index] = fastTrack->mGeneration;
154
155 // mMixer == nullptr on configuration failure (check done after generation update).
156 if (mMixer == nullptr) {
157 return;
158 }
159
160 switch (reason) {
161 case REASON_REMOVE:
162 mMixer->destroy(index);
163 break;
164 case REASON_ADD: {
165 const status_t status = mMixer->create(
166 index, fastTrack->mChannelMask, fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
167 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
168 "%s: cannot create fast track index"
169 " %d, mask %#x, format %#x in AudioMixer",
170 __func__, index, fastTrack->mChannelMask, fastTrack->mFormat);
171 }
172 [[fallthrough]]; // now fallthrough to update the newly created track.
173 case REASON_MODIFY:
174 mMixer->setBufferProvider(index, fastTrack->mBufferProvider);
175
176 float vlf, vrf;
177 if (fastTrack->mVolumeProvider != nullptr) {
178 const gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
179 vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
180 vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
181 } else {
182 vlf = vrf = AudioMixer::UNITY_GAIN_FLOAT;
183 }
184
185 // set volume to avoid ramp whenever the track is updated (or created).
186 // Note: this does not distinguish from starting fresh or
187 // resuming from a paused state.
188 mMixer->setParameter(index, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
189 mMixer->setParameter(index, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
190
191 mMixer->setParameter(index, AudioMixer::RESAMPLE, AudioMixer::REMOVE, nullptr);
192 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
193 (void *)mMixerBuffer);
194 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MIXER_FORMAT,
195 (void *)(uintptr_t)mMixerBufferFormat);
196 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::FORMAT,
197 (void *)(uintptr_t)fastTrack->mFormat);
198 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
199 (void *)(uintptr_t)fastTrack->mChannelMask);
200 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
201 (void *)(uintptr_t)mSinkChannelMask);
202 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::HAPTIC_ENABLED,
203 (void *)(uintptr_t)fastTrack->mHapticPlaybackEnabled);
204 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::HAPTIC_INTENSITY,
205 (void *)(uintptr_t)fastTrack->mHapticIntensity);
Lais Andradebc3f37a2021-07-02 00:13:19 +0100206 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::HAPTIC_MAX_AMPLITUDE,
207 (void *)(&(fastTrack->mHapticMaxAmplitude)));
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800208
209 mMixer->enable(index);
210 break;
211 default:
212 LOG_ALWAYS_FATAL("%s: invalid update reason %d", __func__, reason);
213 }
214}
215
Glenn Kasten22340022014-04-07 12:04:41 -0700216void FastMixer::onStateChange()
217{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800218 const FastMixerState * const current = (const FastMixerState *) mCurrent;
219 const FastMixerState * const previous = (const FastMixerState *) mPrevious;
220 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
Glenn Kasten22340022014-04-07 12:04:41 -0700221 const size_t frameCount = current->mFrameCount;
222
Andy Hung818e7a32016-02-16 18:08:07 -0800223 // update boottime offset, in case it has changed
224 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
225 mBoottimeOffset.load();
226
Glenn Kasten22340022014-04-07 12:04:41 -0700227 // handle state change here, but since we want to diff the state,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800228 // we're prepared for previous == &sInitial the first time through
Glenn Kasten22340022014-04-07 12:04:41 -0700229 unsigned previousTrackMask;
230
231 // check for change in output HAL configuration
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800232 NBAIO_Format previousFormat = mFormat;
233 if (current->mOutputSinkGen != mOutputSinkGen) {
234 mOutputSink = current->mOutputSink;
235 mOutputSinkGen = current->mOutputSinkGen;
jiabin245cdd92018-12-07 17:55:15 -0800236 mSinkChannelMask = current->mSinkChannelMask;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +0100237 mBalance.setChannelMask(mSinkChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800238 if (mOutputSink == NULL) {
239 mFormat = Format_Invalid;
240 mSampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700241 mSinkChannelCount = 0;
242 mSinkChannelMask = AUDIO_CHANNEL_NONE;
jiabin245cdd92018-12-07 17:55:15 -0800243 mAudioChannelCount = 0;
Glenn Kasten22340022014-04-07 12:04:41 -0700244 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800245 mFormat = mOutputSink->format();
246 mSampleRate = Format_sampleRate(mFormat);
247 mSinkChannelCount = Format_channelCount(mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700248 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
249
jiabin245cdd92018-12-07 17:55:15 -0800250 if (mSinkChannelMask == AUDIO_CHANNEL_NONE) {
Andy Hung1b998522021-06-07 16:43:58 -0700251 mSinkChannelMask = getChannelMaskFromCount(mSinkChannelCount);
jiabin245cdd92018-12-07 17:55:15 -0800252 }
253 mAudioChannelCount = mSinkChannelCount - audio_channel_count_from_out_mask(
254 mSinkChannelMask & AUDIO_CHANNEL_HAPTIC_ALL);
Glenn Kasten22340022014-04-07 12:04:41 -0700255 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800256 dumpState->mSampleRate = mSampleRate;
Glenn Kasten22340022014-04-07 12:04:41 -0700257 }
258
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800259 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
Glenn Kasten22340022014-04-07 12:04:41 -0700260 // FIXME to avoid priority inversion, don't delete here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800261 delete mMixer;
262 mMixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700263 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700264 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700265 free(mSinkBuffer);
266 mSinkBuffer = NULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800267 if (frameCount > 0 && mSampleRate > 0) {
Glenn Kasten22340022014-04-07 12:04:41 -0700268 // FIXME new may block for unbounded time at internal mutex of the heap
269 // implementation; it would be better to have normal mixer allocate for us
270 // to avoid blocking here and to prevent possible priority inversion
Andy Hung1bc088a2018-02-09 15:57:31 -0800271 mMixer = new AudioMixer(frameCount, mSampleRate);
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700272 // FIXME See the other FIXME at FastMixer::setNBLogWriter()
Eric Tan0513b5d2018-09-17 10:32:48 -0700273 NBLog::thread_params_t params;
274 params.frameCount = frameCount;
275 params.sampleRate = mSampleRate;
276 LOG_THREAD_PARAMS(params);
Andy Hung9a592762014-07-21 21:56:01 -0700277 const size_t mixerFrameSize = mSinkChannelCount
278 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700279 mMixerBufferSize = mixerFrameSize * frameCount;
280 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700281 const size_t sinkFrameSize = mSinkChannelCount
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800282 * audio_bytes_per_sample(mFormat.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700283 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
284 mSinkBufferSize = sinkFrameSize * frameCount;
285 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
286 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800287 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
288 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
289 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
290 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
291 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
292 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
Glenn Kasten22340022014-04-07 12:04:41 -0700293 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800294 mPeriodNs = 0;
295 mUnderrunNs = 0;
296 mOverrunNs = 0;
297 mForceNs = 0;
298 mWarmupNsMin = 0;
299 mWarmupNsMax = LONG_MAX;
Glenn Kasten22340022014-04-07 12:04:41 -0700300 }
Andy Hung45d68d32014-05-23 21:13:31 -0700301 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700302 // we need to reconfigure all active tracks
303 previousTrackMask = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800304 mFastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten22340022014-04-07 12:04:41 -0700305 dumpState->mFrameCount = frameCount;
Andy Hung8946a282018-04-19 20:04:56 -0700306#ifdef TEE_SINK
307 mTee.set(mFormat, NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
308 mTee.setId(std::string("_") + std::to_string(mThreadIoHandle) + "_F");
309#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700310 } else {
311 previousTrackMask = previous->mTrackMask;
312 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700313
Glenn Kasten22340022014-04-07 12:04:41 -0700314 // check for change in active track set
315 const unsigned currentTrackMask = current->mTrackMask;
316 dumpState->mTrackMask = currentTrackMask;
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800317 dumpState->mNumTracks = popcount(currentTrackMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800318 if (current->mFastTracksGen != mFastTracksGen) {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700319
Glenn Kasten22340022014-04-07 12:04:41 -0700320 // process removed tracks first to avoid running out of track names
321 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
322 while (removedTracks != 0) {
323 int i = __builtin_ctz(removedTracks);
324 removedTracks &= ~(1 << i);
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800325 updateMixerTrack(i, REASON_REMOVE);
Glenn Kasten22340022014-04-07 12:04:41 -0700326 // don't reset track dump state, since other side is ignoring it
Glenn Kasten22340022014-04-07 12:04:41 -0700327 }
328
329 // now process added tracks
330 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
331 while (addedTracks != 0) {
332 int i = __builtin_ctz(addedTracks);
333 addedTracks &= ~(1 << i);
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800334 updateMixerTrack(i, REASON_ADD);
Glenn Kasten22340022014-04-07 12:04:41 -0700335 }
336
337 // finally process (potentially) modified tracks; these use the same slot
338 // but may have a different buffer provider or volume provider
339 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
340 while (modifiedTracks != 0) {
341 int i = __builtin_ctz(modifiedTracks);
342 modifiedTracks &= ~(1 << i);
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800343 updateMixerTrack(i, REASON_MODIFY);
Glenn Kasten22340022014-04-07 12:04:41 -0700344 }
345
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800346 mFastTracksGen = current->mFastTracksGen;
Glenn Kasten22340022014-04-07 12:04:41 -0700347 }
348}
349
350void FastMixer::onWork()
351{
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700352 // TODO: pass an ID parameter to indicate which time series we want to write to in NBLog.cpp
353 // Or: pass both of these into a single call with a boolean
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700354 const FastMixerState * const current = (const FastMixerState *) mCurrent;
355 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
356
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700357 if (mIsWarm) {
Eric Tane98dd6f2018-08-22 18:23:50 -0700358 // Logging timestamps for FastMixer is currently disabled to make memory room for logging
359 // other statistics in FastMixer.
360 // To re-enable, delete the #ifdef FASTMIXER_LOG_HIST_TS lines (and the #endif lines).
361#ifdef FASTMIXER_LOG_HIST_TS
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700362 LOG_HIST_TS();
Eric Tane98dd6f2018-08-22 18:23:50 -0700363#endif
364 //ALOGD("Eric FastMixer::onWork() mIsWarm");
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700365 } else {
Dean Wheatley12473e92021-03-18 23:00:55 +1100366 dumpState->mTimestampVerifier.discontinuity(
367 dumpState->mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS);
Eric Tane98dd6f2018-08-22 18:23:50 -0700368 // See comment in if block.
369#ifdef FASTMIXER_LOG_HIST_TS
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700370 LOG_AUDIO_STATE();
Eric Tane98dd6f2018-08-22 18:23:50 -0700371#endif
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700372 }
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800373 const FastMixerState::Command command = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700374 const size_t frameCount = current->mFrameCount;
375
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800376 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700377 ALOG_ASSERT(mMixerBuffer != NULL);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530378
379 // AudioMixer::mState.enabledTracks is undefined if mState.hook == process__validate,
380 // so we keep a side copy of enabledTracks
381 bool anyEnabledTracks = false;
382
Glenn Kasten22340022014-04-07 12:04:41 -0700383 // for each track, update volume and check for underrun
384 unsigned currentTrackMask = current->mTrackMask;
385 while (currentTrackMask != 0) {
386 int i = __builtin_ctz(currentTrackMask);
387 currentTrackMask &= ~(1 << i);
388 const FastTrack* fastTrack = &current->mFastTracks[i];
389
Andy Hung818e7a32016-02-16 18:08:07 -0800390 const int64_t trackFramesWrittenButNotPresented =
391 mNativeFramesWrittenButNotPresented;
392 const int64_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
393 ExtendedTimestamp perTrackTimestamp(mTimestamp);
394
395 // Can't provide an ExtendedTimestamp before first frame presented.
396 // Also, timestamp may not go to very last frame on stop().
397 if (trackFramesWritten >= trackFramesWrittenButNotPresented &&
398 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
399 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
400 trackFramesWritten - trackFramesWrittenButNotPresented;
401 } else {
402 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
403 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700404 }
Andy Hung818e7a32016-02-16 18:08:07 -0800405 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = trackFramesWritten;
406 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
Glenn Kasten22340022014-04-07 12:04:41 -0700407
Andy Hung1bc088a2018-02-09 15:57:31 -0800408 const int name = i;
Glenn Kasten22340022014-04-07 12:04:41 -0700409 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700410 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700411 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
412 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
413
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800414 mMixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::VOLUME0, &vlf);
415 mMixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700416 }
417 // FIXME The current implementation of framesReady() for fast tracks
418 // takes a tryLock, which can block
419 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
420 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
421 size_t framesReady = fastTrack->mBufferProvider->framesReady();
422 if (ATRACE_ENABLED()) {
423 // I wish we had formatted trace names
424 char traceName[16];
425 strcpy(traceName, "fRdy");
426 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
427 traceName[5] = '\0';
428 ATRACE_INT(traceName, framesReady);
429 }
430 FastTrackDump *ftDump = &dumpState->mTracks[i];
431 FastTrackUnderruns underruns = ftDump->mUnderruns;
432 if (framesReady < frameCount) {
433 if (framesReady == 0) {
434 underruns.mBitFields.mEmpty++;
435 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800436 mMixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700437 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700438 // allow mixing partial buffer
439 underruns.mBitFields.mPartial++;
440 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800441 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530442 anyEnabledTracks = true;
Glenn Kasten288ed212012-04-25 17:52:27 -0700443 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700444 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700445 underruns.mBitFields.mFull++;
446 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800447 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530448 anyEnabledTracks = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700449 }
Glenn Kasten22340022014-04-07 12:04:41 -0700450 ftDump->mUnderruns = underruns;
451 ftDump->mFramesReady = framesReady;
Andy Hungb54c8542016-09-21 12:55:15 -0700452 ftDump->mFramesWritten = trackFramesWritten;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700453 }
454
Mohan Kumar947ffa32014-12-12 15:16:46 +0530455 if (anyEnabledTracks) {
456 // process() is CPU-bound
457 mMixer->process();
458 mMixerBufferState = MIXED;
459 } else if (mMixerBufferState != ZEROED) {
460 mMixerBufferState = UNDEFINED;
461 }
462
Andy Hung45d68d32014-05-23 21:13:31 -0700463 } else if (mMixerBufferState == MIXED) {
464 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700465 }
466 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800467 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
Andy Hung45d68d32014-05-23 21:13:31 -0700468 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700469 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700470 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700471 }
Andy Hung2ddee192015-12-18 17:34:44 -0800472
473 if (mMasterMono.load()) { // memory_order_seq_cst
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700474 mono_blend(mMixerBuffer, mMixerBufferFormat, Format_channelCount(mFormat), frameCount,
475 true /*limit*/);
Andy Hung2ddee192015-12-18 17:34:44 -0800476 }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +0100477
478 // Balance must take effect after mono conversion.
479 // mBalance detects zero balance within the class for speed (not needed here).
480 mBalance.setBalance(mMasterBalance.load());
481 mBalance.process((float *)mMixerBuffer, frameCount);
482
Glenn Kastenf59497b2015-01-26 16:35:47 -0800483 // prepare the buffer used to write to sink
Andy Hung1258c1a2014-05-23 21:22:17 -0700484 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800485 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
486 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
487 frameCount * Format_channelCount(mFormat));
Andy Hung1258c1a2014-05-23 21:22:17 -0700488 }
jiabin245cdd92018-12-07 17:55:15 -0800489 if (mSinkChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) {
490 // When there are haptic channels, the sample data is partially interleaved.
491 // Make the sample data fully interleaved here.
492 adjust_channels_non_destructive(buffer, mAudioChannelCount, buffer, mSinkChannelCount,
493 audio_bytes_per_sample(mFormat.mFormat),
494 frameCount * audio_bytes_per_frame(mAudioChannelCount, mFormat.mFormat));
495 }
Glenn Kasten22340022014-04-07 12:04:41 -0700496 // if non-NULL, then duplicate write() to this non-blocking sink
Andy Hung8946a282018-04-19 20:04:56 -0700497#ifdef TEE_SINK
498 mTee.write(buffer, frameCount);
499#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700500 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
501 // but this code should be modified to handle both non-blocking and blocking sinks
502 dumpState->mWriteSequence++;
503 ATRACE_BEGIN("write");
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800504 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700505 ATRACE_END();
506 dumpState->mWriteSequence++;
507 if (framesWritten >= 0) {
508 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800509 mTotalNativeFramesWritten += framesWritten;
510 dumpState->mFramesWritten = mTotalNativeFramesWritten;
Glenn Kasten22340022014-04-07 12:04:41 -0700511 //if ((size_t) framesWritten == frameCount) {
512 // didFullWrite = true;
513 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700514 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700515 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700516 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800517 mAttemptedWrite = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700518 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700519
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700520 if (mIsWarm) {
521 ExtendedTimestamp timestamp; // local
522 status_t status = mOutputSink->getTimestamp(timestamp);
523 if (status == NO_ERROR) {
524 dumpState->mTimestampVerifier.add(
525 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
526 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
527 mSampleRate);
528 const int64_t totalNativeFramesPresented =
Andy Hung818e7a32016-02-16 18:08:07 -0800529 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700530 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
531 mNativeFramesWrittenButNotPresented =
532 mTotalNativeFramesWritten - totalNativeFramesPresented;
533 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
534 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
535 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
536 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
537 // We don't compensate for server - kernel time difference and
538 // only update latency if we have valid info.
Eric Tane98dd6f2018-08-22 18:23:50 -0700539 const double latencyMs =
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700540 (double)mNativeFramesWrittenButNotPresented * 1000 / mSampleRate;
Eric Tane98dd6f2018-08-22 18:23:50 -0700541 dumpState->mLatencyMs = latencyMs;
542 LOG_LATENCY(latencyMs);
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700543 } else {
544 // HAL reported that more frames were presented than were written
545 mNativeFramesWrittenButNotPresented = 0;
546 status = INVALID_OPERATION;
547 }
Glenn Kasten22340022014-04-07 12:04:41 -0700548 } else {
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700549 dumpState->mTimestampVerifier.error();
Glenn Kasten22340022014-04-07 12:04:41 -0700550 }
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700551 if (status == NO_ERROR) {
552 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
553 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
554 } else {
555 // fetch server time if we can't get timestamp
556 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
557 systemTime(SYSTEM_TIME_MONOTONIC);
558 // clear out kernel cached position as this may get rapidly stale
559 // if we never get a new valid timestamp
560 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
561 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
562 }
Andy Hung818e7a32016-02-16 18:08:07 -0800563 }
Glenn Kasten22340022014-04-07 12:04:41 -0700564 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700565}
566
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700567} // namespace android