blob: 26bd92d10cab793e00b0bb6cbe0f4999da1dfeb9 [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"
Glenn Kasteneef598c2017-04-03 14:41:13 -070045#include "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{
Andy Hung8946a282018-04-19 20:04:56 -070082 (void)mThreadIoHandle; // prevent unused warning, see C++17 [[maybe_unused]]
83
Glenn Kastene4a7ce22015-03-03 11:23:17 -080084 // FIXME pass sInitial as parameter to base class constructor, and make it static local
85 mPrevious = &sInitial;
86 mCurrent = &sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070087
Glenn Kastene4a7ce22015-03-03 11:23:17 -080088 mDummyDumpState = &mDummyFastMixerDumpState;
Andy Hung9a592762014-07-21 21:56:01 -070089 // TODO: Add channel mask to NBAIO_Format.
90 // We assume that the channel mask must be a valid positional channel mask.
Andy Hung1b998522021-06-07 16:43:58 -070091 mSinkChannelMask = getChannelMaskFromCount(mSinkChannelCount);
Andy Hung40964062021-10-27 18:26:48 -070092 mBalance.setChannelMask(mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -070093
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070094 unsigned i;
Glenn Kastendc2c50b2016-04-21 08:13:14 -070095 for (i = 0; i < FastMixerState::sMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -080096 mGenerations[i] = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070097 }
Glenn Kasten214b4062015-03-02 14:15:47 -080098#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080099 mOldLoad.tv_sec = 0;
100 mOldLoad.tv_nsec = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700101#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700102}
103
104FastMixer::~FastMixer()
105{
106}
107
108FastMixerStateQueue* FastMixer::sq()
109{
110 return &mSQ;
111}
112
113const FastThreadState *FastMixer::poll()
114{
115 return mSQ.poll();
116}
117
Mikhail Naganov9b6599e2019-07-29 15:23:21 -0700118void FastMixer::setNBLogWriter(NBLog::Writer *logWriter __unused)
Glenn Kasten22340022014-04-07 12:04:41 -0700119{
Glenn Kasten22340022014-04-07 12:04:41 -0700120}
121
122void FastMixer::onIdle()
123{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800124 mPreIdle = *(const FastMixerState *)mCurrent;
125 mCurrent = &mPreIdle;
Glenn Kasten22340022014-04-07 12:04:41 -0700126}
127
128void FastMixer::onExit()
129{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800130 delete mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -0700131 free(mMixerBuffer);
132 free(mSinkBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700133}
134
135bool FastMixer::isSubClassCommand(FastThreadState::Command command)
136{
137 switch ((FastMixerState::Command) command) {
138 case FastMixerState::MIX:
139 case FastMixerState::WRITE:
140 case FastMixerState::MIX_WRITE:
141 return true;
142 default:
143 return false;
144 }
145}
146
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800147void FastMixer::updateMixerTrack(int index, Reason reason) {
148 const FastMixerState * const current = (const FastMixerState *) mCurrent;
149 const FastTrack * const fastTrack = &current->mFastTracks[index];
150
151 // check and update generation
152 if (reason == REASON_MODIFY && mGenerations[index] == fastTrack->mGeneration) {
153 return; // no change on an already configured track.
154 }
155 mGenerations[index] = fastTrack->mGeneration;
156
157 // mMixer == nullptr on configuration failure (check done after generation update).
158 if (mMixer == nullptr) {
159 return;
160 }
161
162 switch (reason) {
163 case REASON_REMOVE:
164 mMixer->destroy(index);
165 break;
166 case REASON_ADD: {
167 const status_t status = mMixer->create(
168 index, fastTrack->mChannelMask, fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
169 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
170 "%s: cannot create fast track index"
171 " %d, mask %#x, format %#x in AudioMixer",
172 __func__, index, fastTrack->mChannelMask, fastTrack->mFormat);
173 }
174 [[fallthrough]]; // now fallthrough to update the newly created track.
175 case REASON_MODIFY:
176 mMixer->setBufferProvider(index, fastTrack->mBufferProvider);
177
178 float vlf, vrf;
179 if (fastTrack->mVolumeProvider != nullptr) {
180 const gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
181 vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
182 vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
183 } else {
184 vlf = vrf = AudioMixer::UNITY_GAIN_FLOAT;
185 }
186
187 // set volume to avoid ramp whenever the track is updated (or created).
188 // Note: this does not distinguish from starting fresh or
189 // resuming from a paused state.
190 mMixer->setParameter(index, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
191 mMixer->setParameter(index, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
192
193 mMixer->setParameter(index, AudioMixer::RESAMPLE, AudioMixer::REMOVE, nullptr);
194 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
195 (void *)mMixerBuffer);
196 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MIXER_FORMAT,
197 (void *)(uintptr_t)mMixerBufferFormat);
198 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::FORMAT,
199 (void *)(uintptr_t)fastTrack->mFormat);
200 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
201 (void *)(uintptr_t)fastTrack->mChannelMask);
202 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
203 (void *)(uintptr_t)mSinkChannelMask);
204 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::HAPTIC_ENABLED,
205 (void *)(uintptr_t)fastTrack->mHapticPlaybackEnabled);
206 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::HAPTIC_INTENSITY,
207 (void *)(uintptr_t)fastTrack->mHapticIntensity);
Lais Andradebc3f37a2021-07-02 00:13:19 +0100208 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::HAPTIC_MAX_AMPLITUDE,
209 (void *)(&(fastTrack->mHapticMaxAmplitude)));
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800210
211 mMixer->enable(index);
212 break;
213 default:
214 LOG_ALWAYS_FATAL("%s: invalid update reason %d", __func__, reason);
215 }
216}
217
Glenn Kasten22340022014-04-07 12:04:41 -0700218void FastMixer::onStateChange()
219{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800220 const FastMixerState * const current = (const FastMixerState *) mCurrent;
221 const FastMixerState * const previous = (const FastMixerState *) mPrevious;
222 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
Glenn Kasten22340022014-04-07 12:04:41 -0700223 const size_t frameCount = current->mFrameCount;
224
Andy Hung818e7a32016-02-16 18:08:07 -0800225 // update boottime offset, in case it has changed
226 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
227 mBoottimeOffset.load();
228
Glenn Kasten22340022014-04-07 12:04:41 -0700229 // handle state change here, but since we want to diff the state,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800230 // we're prepared for previous == &sInitial the first time through
Glenn Kasten22340022014-04-07 12:04:41 -0700231 unsigned previousTrackMask;
232
233 // check for change in output HAL configuration
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800234 NBAIO_Format previousFormat = mFormat;
235 if (current->mOutputSinkGen != mOutputSinkGen) {
236 mOutputSink = current->mOutputSink;
237 mOutputSinkGen = current->mOutputSinkGen;
jiabin245cdd92018-12-07 17:55:15 -0800238 mSinkChannelMask = current->mSinkChannelMask;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +0100239 mBalance.setChannelMask(mSinkChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800240 if (mOutputSink == NULL) {
241 mFormat = Format_Invalid;
242 mSampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700243 mSinkChannelCount = 0;
244 mSinkChannelMask = AUDIO_CHANNEL_NONE;
jiabin245cdd92018-12-07 17:55:15 -0800245 mAudioChannelCount = 0;
Glenn Kasten22340022014-04-07 12:04:41 -0700246 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800247 mFormat = mOutputSink->format();
248 mSampleRate = Format_sampleRate(mFormat);
249 mSinkChannelCount = Format_channelCount(mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700250 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
251
jiabin245cdd92018-12-07 17:55:15 -0800252 if (mSinkChannelMask == AUDIO_CHANNEL_NONE) {
Andy Hung1b998522021-06-07 16:43:58 -0700253 mSinkChannelMask = getChannelMaskFromCount(mSinkChannelCount);
jiabin245cdd92018-12-07 17:55:15 -0800254 }
255 mAudioChannelCount = mSinkChannelCount - audio_channel_count_from_out_mask(
256 mSinkChannelMask & AUDIO_CHANNEL_HAPTIC_ALL);
Glenn Kasten22340022014-04-07 12:04:41 -0700257 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800258 dumpState->mSampleRate = mSampleRate;
Glenn Kasten22340022014-04-07 12:04:41 -0700259 }
260
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800261 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
Glenn Kasten22340022014-04-07 12:04:41 -0700262 // FIXME to avoid priority inversion, don't delete here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800263 delete mMixer;
264 mMixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700265 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700266 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700267 free(mSinkBuffer);
268 mSinkBuffer = NULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800269 if (frameCount > 0 && mSampleRate > 0) {
Glenn Kasten22340022014-04-07 12:04:41 -0700270 // FIXME new may block for unbounded time at internal mutex of the heap
271 // implementation; it would be better to have normal mixer allocate for us
272 // to avoid blocking here and to prevent possible priority inversion
Andy Hung1bc088a2018-02-09 15:57:31 -0800273 mMixer = new AudioMixer(frameCount, mSampleRate);
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700274 // FIXME See the other FIXME at FastMixer::setNBLogWriter()
Eric Tan0513b5d2018-09-17 10:32:48 -0700275 NBLog::thread_params_t params;
276 params.frameCount = frameCount;
277 params.sampleRate = mSampleRate;
278 LOG_THREAD_PARAMS(params);
Andy Hung9a592762014-07-21 21:56:01 -0700279 const size_t mixerFrameSize = mSinkChannelCount
280 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700281 mMixerBufferSize = mixerFrameSize * frameCount;
282 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700283 const size_t sinkFrameSize = mSinkChannelCount
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800284 * audio_bytes_per_sample(mFormat.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700285 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
286 mSinkBufferSize = sinkFrameSize * frameCount;
287 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
288 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800289 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
290 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
291 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
292 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
293 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
294 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
Glenn Kasten22340022014-04-07 12:04:41 -0700295 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800296 mPeriodNs = 0;
297 mUnderrunNs = 0;
298 mOverrunNs = 0;
299 mForceNs = 0;
300 mWarmupNsMin = 0;
301 mWarmupNsMax = LONG_MAX;
Glenn Kasten22340022014-04-07 12:04:41 -0700302 }
Andy Hung45d68d32014-05-23 21:13:31 -0700303 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700304 // we need to reconfigure all active tracks
305 previousTrackMask = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800306 mFastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten22340022014-04-07 12:04:41 -0700307 dumpState->mFrameCount = frameCount;
Andy Hung8946a282018-04-19 20:04:56 -0700308#ifdef TEE_SINK
309 mTee.set(mFormat, NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
310 mTee.setId(std::string("_") + std::to_string(mThreadIoHandle) + "_F");
311#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700312 } else {
313 previousTrackMask = previous->mTrackMask;
314 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700315
Glenn Kasten22340022014-04-07 12:04:41 -0700316 // check for change in active track set
317 const unsigned currentTrackMask = current->mTrackMask;
318 dumpState->mTrackMask = currentTrackMask;
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800319 dumpState->mNumTracks = popcount(currentTrackMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800320 if (current->mFastTracksGen != mFastTracksGen) {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700321
Glenn Kasten22340022014-04-07 12:04:41 -0700322 // process removed tracks first to avoid running out of track names
323 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
324 while (removedTracks != 0) {
325 int i = __builtin_ctz(removedTracks);
326 removedTracks &= ~(1 << i);
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800327 updateMixerTrack(i, REASON_REMOVE);
Glenn Kasten22340022014-04-07 12:04:41 -0700328 // don't reset track dump state, since other side is ignoring it
Glenn Kasten22340022014-04-07 12:04:41 -0700329 }
330
331 // now process added tracks
332 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
333 while (addedTracks != 0) {
334 int i = __builtin_ctz(addedTracks);
335 addedTracks &= ~(1 << i);
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800336 updateMixerTrack(i, REASON_ADD);
Glenn Kasten22340022014-04-07 12:04:41 -0700337 }
338
339 // finally process (potentially) modified tracks; these use the same slot
340 // but may have a different buffer provider or volume provider
341 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
342 while (modifiedTracks != 0) {
343 int i = __builtin_ctz(modifiedTracks);
344 modifiedTracks &= ~(1 << i);
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800345 updateMixerTrack(i, REASON_MODIFY);
Glenn Kasten22340022014-04-07 12:04:41 -0700346 }
347
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800348 mFastTracksGen = current->mFastTracksGen;
Glenn Kasten22340022014-04-07 12:04:41 -0700349 }
350}
351
352void FastMixer::onWork()
353{
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700354 // TODO: pass an ID parameter to indicate which time series we want to write to in NBLog.cpp
355 // Or: pass both of these into a single call with a boolean
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700356 const FastMixerState * const current = (const FastMixerState *) mCurrent;
357 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
358
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700359 if (mIsWarm) {
Eric Tane98dd6f2018-08-22 18:23:50 -0700360 // Logging timestamps for FastMixer is currently disabled to make memory room for logging
361 // other statistics in FastMixer.
362 // To re-enable, delete the #ifdef FASTMIXER_LOG_HIST_TS lines (and the #endif lines).
363#ifdef FASTMIXER_LOG_HIST_TS
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700364 LOG_HIST_TS();
Eric Tane98dd6f2018-08-22 18:23:50 -0700365#endif
366 //ALOGD("Eric FastMixer::onWork() mIsWarm");
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700367 } else {
Dean Wheatley12473e92021-03-18 23:00:55 +1100368 dumpState->mTimestampVerifier.discontinuity(
369 dumpState->mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS);
Eric Tane98dd6f2018-08-22 18:23:50 -0700370 // See comment in if block.
371#ifdef FASTMIXER_LOG_HIST_TS
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700372 LOG_AUDIO_STATE();
Eric Tane98dd6f2018-08-22 18:23:50 -0700373#endif
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700374 }
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800375 const FastMixerState::Command command = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700376 const size_t frameCount = current->mFrameCount;
377
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800378 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700379 ALOG_ASSERT(mMixerBuffer != NULL);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530380
381 // AudioMixer::mState.enabledTracks is undefined if mState.hook == process__validate,
382 // so we keep a side copy of enabledTracks
383 bool anyEnabledTracks = false;
384
Glenn Kasten22340022014-04-07 12:04:41 -0700385 // for each track, update volume and check for underrun
386 unsigned currentTrackMask = current->mTrackMask;
387 while (currentTrackMask != 0) {
388 int i = __builtin_ctz(currentTrackMask);
389 currentTrackMask &= ~(1 << i);
390 const FastTrack* fastTrack = &current->mFastTracks[i];
391
Andy Hung818e7a32016-02-16 18:08:07 -0800392 const int64_t trackFramesWrittenButNotPresented =
393 mNativeFramesWrittenButNotPresented;
394 const int64_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
395 ExtendedTimestamp perTrackTimestamp(mTimestamp);
396
397 // Can't provide an ExtendedTimestamp before first frame presented.
398 // Also, timestamp may not go to very last frame on stop().
399 if (trackFramesWritten >= trackFramesWrittenButNotPresented &&
400 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
401 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
402 trackFramesWritten - trackFramesWrittenButNotPresented;
403 } else {
404 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
405 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700406 }
Andy Hung818e7a32016-02-16 18:08:07 -0800407 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = trackFramesWritten;
408 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
Glenn Kasten22340022014-04-07 12:04:41 -0700409
Andy Hung1bc088a2018-02-09 15:57:31 -0800410 const int name = i;
Glenn Kasten22340022014-04-07 12:04:41 -0700411 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700412 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700413 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
414 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
415
Andy Hung4d4ca6a2019-02-01 18:23:37 -0800416 mMixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::VOLUME0, &vlf);
417 mMixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700418 }
419 // FIXME The current implementation of framesReady() for fast tracks
420 // takes a tryLock, which can block
421 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
422 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
423 size_t framesReady = fastTrack->mBufferProvider->framesReady();
424 if (ATRACE_ENABLED()) {
425 // I wish we had formatted trace names
426 char traceName[16];
427 strcpy(traceName, "fRdy");
428 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
429 traceName[5] = '\0';
430 ATRACE_INT(traceName, framesReady);
431 }
432 FastTrackDump *ftDump = &dumpState->mTracks[i];
433 FastTrackUnderruns underruns = ftDump->mUnderruns;
434 if (framesReady < frameCount) {
435 if (framesReady == 0) {
436 underruns.mBitFields.mEmpty++;
437 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800438 mMixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700439 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700440 // allow mixing partial buffer
441 underruns.mBitFields.mPartial++;
442 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800443 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530444 anyEnabledTracks = true;
Glenn Kasten288ed212012-04-25 17:52:27 -0700445 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700446 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700447 underruns.mBitFields.mFull++;
448 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800449 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530450 anyEnabledTracks = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700451 }
Glenn Kasten22340022014-04-07 12:04:41 -0700452 ftDump->mUnderruns = underruns;
453 ftDump->mFramesReady = framesReady;
Andy Hungb54c8542016-09-21 12:55:15 -0700454 ftDump->mFramesWritten = trackFramesWritten;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700455 }
456
Mohan Kumar947ffa32014-12-12 15:16:46 +0530457 if (anyEnabledTracks) {
458 // process() is CPU-bound
459 mMixer->process();
460 mMixerBufferState = MIXED;
461 } else if (mMixerBufferState != ZEROED) {
462 mMixerBufferState = UNDEFINED;
463 }
464
Andy Hung45d68d32014-05-23 21:13:31 -0700465 } else if (mMixerBufferState == MIXED) {
466 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700467 }
468 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800469 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
Andy Hung45d68d32014-05-23 21:13:31 -0700470 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700471 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700472 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700473 }
Andy Hung2ddee192015-12-18 17:34:44 -0800474
475 if (mMasterMono.load()) { // memory_order_seq_cst
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700476 mono_blend(mMixerBuffer, mMixerBufferFormat, Format_channelCount(mFormat), frameCount,
477 true /*limit*/);
Andy Hung2ddee192015-12-18 17:34:44 -0800478 }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +0100479
480 // Balance must take effect after mono conversion.
481 // mBalance detects zero balance within the class for speed (not needed here).
482 mBalance.setBalance(mMasterBalance.load());
483 mBalance.process((float *)mMixerBuffer, frameCount);
484
Glenn Kastenf59497b2015-01-26 16:35:47 -0800485 // prepare the buffer used to write to sink
Andy Hung1258c1a2014-05-23 21:22:17 -0700486 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800487 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
488 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
489 frameCount * Format_channelCount(mFormat));
Andy Hung1258c1a2014-05-23 21:22:17 -0700490 }
jiabin245cdd92018-12-07 17:55:15 -0800491 if (mSinkChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) {
492 // When there are haptic channels, the sample data is partially interleaved.
493 // Make the sample data fully interleaved here.
494 adjust_channels_non_destructive(buffer, mAudioChannelCount, buffer, mSinkChannelCount,
495 audio_bytes_per_sample(mFormat.mFormat),
496 frameCount * audio_bytes_per_frame(mAudioChannelCount, mFormat.mFormat));
497 }
Glenn Kasten22340022014-04-07 12:04:41 -0700498 // if non-NULL, then duplicate write() to this non-blocking sink
Andy Hung8946a282018-04-19 20:04:56 -0700499#ifdef TEE_SINK
500 mTee.write(buffer, frameCount);
501#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700502 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
503 // but this code should be modified to handle both non-blocking and blocking sinks
504 dumpState->mWriteSequence++;
505 ATRACE_BEGIN("write");
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800506 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700507 ATRACE_END();
508 dumpState->mWriteSequence++;
509 if (framesWritten >= 0) {
510 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800511 mTotalNativeFramesWritten += framesWritten;
512 dumpState->mFramesWritten = mTotalNativeFramesWritten;
Glenn Kasten22340022014-04-07 12:04:41 -0700513 //if ((size_t) framesWritten == frameCount) {
514 // didFullWrite = true;
515 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700516 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700517 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700518 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800519 mAttemptedWrite = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700520 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700521
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700522 if (mIsWarm) {
523 ExtendedTimestamp timestamp; // local
524 status_t status = mOutputSink->getTimestamp(timestamp);
525 if (status == NO_ERROR) {
526 dumpState->mTimestampVerifier.add(
527 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
528 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
529 mSampleRate);
530 const int64_t totalNativeFramesPresented =
Andy Hung818e7a32016-02-16 18:08:07 -0800531 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700532 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
533 mNativeFramesWrittenButNotPresented =
534 mTotalNativeFramesWritten - totalNativeFramesPresented;
535 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
536 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
537 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
538 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
539 // We don't compensate for server - kernel time difference and
540 // only update latency if we have valid info.
Eric Tane98dd6f2018-08-22 18:23:50 -0700541 const double latencyMs =
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700542 (double)mNativeFramesWrittenButNotPresented * 1000 / mSampleRate;
Eric Tane98dd6f2018-08-22 18:23:50 -0700543 dumpState->mLatencyMs = latencyMs;
544 LOG_LATENCY(latencyMs);
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700545 } else {
546 // HAL reported that more frames were presented than were written
547 mNativeFramesWrittenButNotPresented = 0;
548 status = INVALID_OPERATION;
549 }
Glenn Kasten22340022014-04-07 12:04:41 -0700550 } else {
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700551 dumpState->mTimestampVerifier.error();
Glenn Kasten22340022014-04-07 12:04:41 -0700552 }
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700553 if (status == NO_ERROR) {
554 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
555 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
556 } else {
557 // fetch server time if we can't get timestamp
558 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
559 systemTime(SYSTEM_TIME_MONOTONIC);
560 // clear out kernel cached position as this may get rapidly stale
561 // if we never get a new valid timestamp
562 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
563 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
564 }
Andy Hung818e7a32016-02-16 18:08:07 -0800565 }
Glenn Kasten22340022014-04-07 12:04:41 -0700566 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700567}
568
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700569} // namespace android