blob: ca0d74980dc82ac0884ed2ad303da6e0f013d137 [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>
Glenn Kastenad8510a2015-02-17 16:24:07 -080030#include <utils/Debug.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070031#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070033#include <system/audio.h>
Glenn Kasten214b4062015-03-02 14:15:47 -080034#ifdef FAST_THREAD_STATISTICS
Eric Tan5b13ff82018-07-27 11:20:17 -070035#include <audio_utils/Statistics.h>
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070036#ifdef CPU_FREQUENCY_STATISTICS
Glenn Kasten42d45cf2012-05-02 10:34:47 -070037#include <cpustats/ThreadCpuUsage.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070038#endif
Glenn Kasten0a14c4c2012-06-13 14:58:49 -070039#endif
jiabin245cdd92018-12-07 17:55:15 -080040#include <audio_utils/channels.h>
Andy Hung1258c1a2014-05-23 21:22:17 -070041#include <audio_utils/format.h>
jiabin245cdd92018-12-07 17:55:15 -080042#include <audio_utils/mono_blend.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 Hung8946a282018-04-19 20:04:56 -070051FastMixer::FastMixer(audio_io_handle_t parentIoHandle)
52 : FastThread("cycle_ms", "load_us"),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080053 // mFastTrackNames
54 // mGenerations
55 mOutputSink(NULL),
56 mOutputSinkGen(0),
57 mMixer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070058 mSinkBuffer(NULL),
59 mSinkBufferSize(0),
Andy Hung9a592762014-07-21 21:56:01 -070060 mSinkChannelCount(FCC_2),
Andy Hung45d68d32014-05-23 21:13:31 -070061 mMixerBuffer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070062 mMixerBufferSize(0),
63 mMixerBufferFormat(AUDIO_FORMAT_PCM_16_BIT),
Andy Hung45d68d32014-05-23 21:13:31 -070064 mMixerBufferState(UNDEFINED),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080065 mFormat(Format_Invalid),
66 mSampleRate(0),
67 mFastTracksGen(0),
68 mTotalNativeFramesWritten(0),
Glenn Kasten22340022014-04-07 12:04:41 -070069 // timestamp
Andy Hung2ddee192015-12-18 17:34:44 -080070 mNativeFramesWrittenButNotPresented(0), // the = 0 is to silence the compiler
Andy Hung8946a282018-04-19 20:04:56 -070071 mMasterMono(false),
72 mThreadIoHandle(parentIoHandle)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070073{
Andy Hung8946a282018-04-19 20:04:56 -070074 (void)mThreadIoHandle; // prevent unused warning, see C++17 [[maybe_unused]]
75
Glenn Kastene4a7ce22015-03-03 11:23:17 -080076 // FIXME pass sInitial as parameter to base class constructor, and make it static local
77 mPrevious = &sInitial;
78 mCurrent = &sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070079
Glenn Kastene4a7ce22015-03-03 11:23:17 -080080 mDummyDumpState = &mDummyFastMixerDumpState;
Andy Hung9a592762014-07-21 21:56:01 -070081 // TODO: Add channel mask to NBAIO_Format.
82 // We assume that the channel mask must be a valid positional channel mask.
83 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -070084
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070085 unsigned i;
Glenn Kastendc2c50b2016-04-21 08:13:14 -070086 for (i = 0; i < FastMixerState::sMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -080087 mGenerations[i] = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070088 }
Glenn Kasten214b4062015-03-02 14:15:47 -080089#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080090 mOldLoad.tv_sec = 0;
91 mOldLoad.tv_nsec = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070092#endif
Glenn Kasten22340022014-04-07 12:04:41 -070093}
94
95FastMixer::~FastMixer()
96{
97}
98
99FastMixerStateQueue* FastMixer::sq()
100{
101 return &mSQ;
102}
103
104const FastThreadState *FastMixer::poll()
105{
106 return mSQ.poll();
107}
108
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700109void FastMixer::setNBLogWriter(NBLog::Writer *logWriter)
Glenn Kasten22340022014-04-07 12:04:41 -0700110{
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700111 // FIXME If mMixer is set or changed prior to this, we don't inform correctly.
112 // Should cache logWriter and re-apply it at the assignment to mMixer.
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800113 if (mMixer != NULL) {
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700114 mMixer->setNBLogWriter(logWriter);
Glenn Kasten22340022014-04-07 12:04:41 -0700115 }
116}
117
118void FastMixer::onIdle()
119{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800120 mPreIdle = *(const FastMixerState *)mCurrent;
121 mCurrent = &mPreIdle;
Glenn Kasten22340022014-04-07 12:04:41 -0700122}
123
124void FastMixer::onExit()
125{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800126 delete mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -0700127 free(mMixerBuffer);
128 free(mSinkBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700129}
130
131bool FastMixer::isSubClassCommand(FastThreadState::Command command)
132{
133 switch ((FastMixerState::Command) command) {
134 case FastMixerState::MIX:
135 case FastMixerState::WRITE:
136 case FastMixerState::MIX_WRITE:
137 return true;
138 default:
139 return false;
140 }
141}
142
143void FastMixer::onStateChange()
144{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800145 const FastMixerState * const current = (const FastMixerState *) mCurrent;
146 const FastMixerState * const previous = (const FastMixerState *) mPrevious;
147 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
Glenn Kasten22340022014-04-07 12:04:41 -0700148 const size_t frameCount = current->mFrameCount;
149
Andy Hung818e7a32016-02-16 18:08:07 -0800150 // update boottime offset, in case it has changed
151 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
152 mBoottimeOffset.load();
153
Glenn Kasten22340022014-04-07 12:04:41 -0700154 // handle state change here, but since we want to diff the state,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800155 // we're prepared for previous == &sInitial the first time through
Glenn Kasten22340022014-04-07 12:04:41 -0700156 unsigned previousTrackMask;
157
158 // check for change in output HAL configuration
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800159 NBAIO_Format previousFormat = mFormat;
160 if (current->mOutputSinkGen != mOutputSinkGen) {
161 mOutputSink = current->mOutputSink;
162 mOutputSinkGen = current->mOutputSinkGen;
jiabin245cdd92018-12-07 17:55:15 -0800163 mSinkChannelMask = current->mSinkChannelMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800164 if (mOutputSink == NULL) {
165 mFormat = Format_Invalid;
166 mSampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700167 mSinkChannelCount = 0;
168 mSinkChannelMask = AUDIO_CHANNEL_NONE;
jiabin245cdd92018-12-07 17:55:15 -0800169 mAudioChannelCount = 0;
Glenn Kasten22340022014-04-07 12:04:41 -0700170 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800171 mFormat = mOutputSink->format();
172 mSampleRate = Format_sampleRate(mFormat);
173 mSinkChannelCount = Format_channelCount(mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700174 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
175
jiabin245cdd92018-12-07 17:55:15 -0800176 if (mSinkChannelMask == AUDIO_CHANNEL_NONE) {
177 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
178 }
179 mAudioChannelCount = mSinkChannelCount - audio_channel_count_from_out_mask(
180 mSinkChannelMask & AUDIO_CHANNEL_HAPTIC_ALL);
Glenn Kasten22340022014-04-07 12:04:41 -0700181 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800182 dumpState->mSampleRate = mSampleRate;
Glenn Kasten22340022014-04-07 12:04:41 -0700183 }
184
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800185 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
Glenn Kasten22340022014-04-07 12:04:41 -0700186 // FIXME to avoid priority inversion, don't delete here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800187 delete mMixer;
188 mMixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700189 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700190 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700191 free(mSinkBuffer);
192 mSinkBuffer = NULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800193 if (frameCount > 0 && mSampleRate > 0) {
Andy Hung60c545d2015-06-19 17:34:53 -0700194 // The mixer produces either 16 bit PCM or float output, select
195 // float output if the HAL supports higher than 16 bit precision.
196 mMixerBufferFormat = mFormat.mFormat == AUDIO_FORMAT_PCM_16_BIT ?
197 AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_FLOAT;
Glenn Kasten22340022014-04-07 12:04:41 -0700198 // FIXME new may block for unbounded time at internal mutex of the heap
199 // implementation; it would be better to have normal mixer allocate for us
200 // to avoid blocking here and to prevent possible priority inversion
Andy Hung1bc088a2018-02-09 15:57:31 -0800201 mMixer = new AudioMixer(frameCount, mSampleRate);
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700202 // FIXME See the other FIXME at FastMixer::setNBLogWriter()
Eric Tan0513b5d2018-09-17 10:32:48 -0700203 NBLog::thread_params_t params;
204 params.frameCount = frameCount;
205 params.sampleRate = mSampleRate;
206 LOG_THREAD_PARAMS(params);
Andy Hung9a592762014-07-21 21:56:01 -0700207 const size_t mixerFrameSize = mSinkChannelCount
208 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700209 mMixerBufferSize = mixerFrameSize * frameCount;
210 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700211 const size_t sinkFrameSize = mSinkChannelCount
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800212 * audio_bytes_per_sample(mFormat.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700213 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
214 mSinkBufferSize = sinkFrameSize * frameCount;
215 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
216 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800217 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
218 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
219 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
220 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
221 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
222 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
Glenn Kasten22340022014-04-07 12:04:41 -0700223 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800224 mPeriodNs = 0;
225 mUnderrunNs = 0;
226 mOverrunNs = 0;
227 mForceNs = 0;
228 mWarmupNsMin = 0;
229 mWarmupNsMax = LONG_MAX;
Glenn Kasten22340022014-04-07 12:04:41 -0700230 }
Andy Hung45d68d32014-05-23 21:13:31 -0700231 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700232 // we need to reconfigure all active tracks
233 previousTrackMask = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800234 mFastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten22340022014-04-07 12:04:41 -0700235 dumpState->mFrameCount = frameCount;
Andy Hung8946a282018-04-19 20:04:56 -0700236#ifdef TEE_SINK
237 mTee.set(mFormat, NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
238 mTee.setId(std::string("_") + std::to_string(mThreadIoHandle) + "_F");
239#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700240 } else {
241 previousTrackMask = previous->mTrackMask;
242 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700243
Glenn Kasten22340022014-04-07 12:04:41 -0700244 // check for change in active track set
245 const unsigned currentTrackMask = current->mTrackMask;
246 dumpState->mTrackMask = currentTrackMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800247 if (current->mFastTracksGen != mFastTracksGen) {
Andy Hung45d68d32014-05-23 21:13:31 -0700248 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700249
Glenn Kasten22340022014-04-07 12:04:41 -0700250 // process removed tracks first to avoid running out of track names
251 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
252 while (removedTracks != 0) {
253 int i = __builtin_ctz(removedTracks);
254 removedTracks &= ~(1 << i);
255 const FastTrack* fastTrack = &current->mFastTracks[i];
256 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800257 if (mMixer != NULL) {
Andy Hung1bc088a2018-02-09 15:57:31 -0800258 mMixer->destroy(i);
Glenn Kasten22340022014-04-07 12:04:41 -0700259 }
Glenn Kasten22340022014-04-07 12:04:41 -0700260 // don't reset track dump state, since other side is ignoring it
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800261 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700262 }
263
264 // now process added tracks
265 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
266 while (addedTracks != 0) {
267 int i = __builtin_ctz(addedTracks);
268 addedTracks &= ~(1 << i);
269 const FastTrack* fastTrack = &current->mFastTracks[i];
270 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800271 if (mMixer != NULL) {
Andy Hung1bc088a2018-02-09 15:57:31 -0800272 const int name = i; // for clarity, choose name as fast track index.
273 status_t status = mMixer->create(
274 name,
275 fastTrack->mChannelMask,
Andy Hunge8a1ced2014-05-09 15:02:21 -0700276 fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Andy Hung1bc088a2018-02-09 15:57:31 -0800277 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
278 "%s: cannot create track name"
279 " %d, mask %#x, format %#x, sessionId %d in AudioMixer",
280 __func__, name,
281 fastTrack->mChannelMask, fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800282 mMixer->setBufferProvider(name, bufferProvider);
283 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
Andy Hung9a592762014-07-21 21:56:01 -0700284 (void *)mMixerBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700285 // newly allocated track names default to full scale volume
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800286 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700287 name,
288 AudioMixer::TRACK,
289 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800290 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700291 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800292 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700293 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800294 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700295 (void *)(uintptr_t)mSinkChannelMask);
jiabin245cdd92018-12-07 17:55:15 -0800296 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::HAPTIC_ENABLED,
297 (void *)(uintptr_t)fastTrack->mHapticPlaybackEnabled);
jiabin77270b82018-12-18 15:41:29 -0800298 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::HAPTIC_INTENSITY,
299 (void *)(uintptr_t)fastTrack->mHapticIntensity);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800300 mMixer->enable(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700301 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800302 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700303 }
304
305 // finally process (potentially) modified tracks; these use the same slot
306 // but may have a different buffer provider or volume provider
307 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
308 while (modifiedTracks != 0) {
309 int i = __builtin_ctz(modifiedTracks);
310 modifiedTracks &= ~(1 << i);
311 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800312 if (fastTrack->mGeneration != mGenerations[i]) {
Glenn Kasten22340022014-04-07 12:04:41 -0700313 // this track was actually modified
314 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
315 ALOG_ASSERT(bufferProvider != NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800316 if (mMixer != NULL) {
Andy Hung1bc088a2018-02-09 15:57:31 -0800317 const int name = i;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800318 mMixer->setBufferProvider(name, bufferProvider);
Glenn Kasten22340022014-04-07 12:04:41 -0700319 if (fastTrack->mVolumeProvider == NULL) {
Andy Hung6be49402014-05-30 10:42:03 -0700320 float f = AudioMixer::UNITY_GAIN_FLOAT;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800321 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
322 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
Glenn Kasten288ed212012-04-25 17:52:27 -0700323 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800324 mMixer->setParameter(name, AudioMixer::RESAMPLE,
Glenn Kasten22340022014-04-07 12:04:41 -0700325 AudioMixer::REMOVE, NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800326 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700327 name,
328 AudioMixer::TRACK,
329 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800330 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700331 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800332 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700333 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800334 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700335 (void *)(uintptr_t)mSinkChannelMask);
jiabin245cdd92018-12-07 17:55:15 -0800336 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::HAPTIC_ENABLED,
337 (void *)(uintptr_t)fastTrack->mHapticPlaybackEnabled);
jiabin77270b82018-12-18 15:41:29 -0800338 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::HAPTIC_INTENSITY,
339 (void *)(uintptr_t)fastTrack->mHapticIntensity);
Glenn Kasten22340022014-04-07 12:04:41 -0700340 // already enabled
341 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800342 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700343 }
344 }
345
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800346 mFastTracksGen = current->mFastTracksGen;
Glenn Kasten22340022014-04-07 12:04:41 -0700347
348 dumpState->mNumTracks = popcount(currentTrackMask);
349 }
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 {
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700368 dumpState->mTimestampVerifier.discontinuity();
Eric Tane98dd6f2018-08-22 18:23:50 -0700369 // See comment in if block.
370#ifdef FASTMIXER_LOG_HIST_TS
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700371 LOG_AUDIO_STATE();
Eric Tane98dd6f2018-08-22 18:23:50 -0700372#endif
Sanna Catherine de Treville Wager85768942017-07-26 20:17:30 -0700373 }
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800374 const FastMixerState::Command command = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700375 const size_t frameCount = current->mFrameCount;
376
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800377 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700378 ALOG_ASSERT(mMixerBuffer != NULL);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530379
380 // AudioMixer::mState.enabledTracks is undefined if mState.hook == process__validate,
381 // so we keep a side copy of enabledTracks
382 bool anyEnabledTracks = false;
383
Glenn Kasten22340022014-04-07 12:04:41 -0700384 // for each track, update volume and check for underrun
385 unsigned currentTrackMask = current->mTrackMask;
386 while (currentTrackMask != 0) {
387 int i = __builtin_ctz(currentTrackMask);
388 currentTrackMask &= ~(1 << i);
389 const FastTrack* fastTrack = &current->mFastTracks[i];
390
Andy Hung818e7a32016-02-16 18:08:07 -0800391 const int64_t trackFramesWrittenButNotPresented =
392 mNativeFramesWrittenButNotPresented;
393 const int64_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
394 ExtendedTimestamp perTrackTimestamp(mTimestamp);
395
396 // Can't provide an ExtendedTimestamp before first frame presented.
397 // Also, timestamp may not go to very last frame on stop().
398 if (trackFramesWritten >= trackFramesWrittenButNotPresented &&
399 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
400 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
401 trackFramesWritten - trackFramesWrittenButNotPresented;
402 } else {
403 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
404 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700405 }
Andy Hung818e7a32016-02-16 18:08:07 -0800406 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = trackFramesWritten;
407 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
Glenn Kasten22340022014-04-07 12:04:41 -0700408
Andy Hung1bc088a2018-02-09 15:57:31 -0800409 const int name = i;
Glenn Kasten22340022014-04-07 12:04:41 -0700410 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700411 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700412 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
413 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
414
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800415 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
416 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700417 }
418 // FIXME The current implementation of framesReady() for fast tracks
419 // takes a tryLock, which can block
420 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
421 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
422 size_t framesReady = fastTrack->mBufferProvider->framesReady();
423 if (ATRACE_ENABLED()) {
424 // I wish we had formatted trace names
425 char traceName[16];
426 strcpy(traceName, "fRdy");
427 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
428 traceName[5] = '\0';
429 ATRACE_INT(traceName, framesReady);
430 }
431 FastTrackDump *ftDump = &dumpState->mTracks[i];
432 FastTrackUnderruns underruns = ftDump->mUnderruns;
433 if (framesReady < frameCount) {
434 if (framesReady == 0) {
435 underruns.mBitFields.mEmpty++;
436 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800437 mMixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700438 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700439 // allow mixing partial buffer
440 underruns.mBitFields.mPartial++;
441 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800442 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530443 anyEnabledTracks = true;
Glenn Kasten288ed212012-04-25 17:52:27 -0700444 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700445 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700446 underruns.mBitFields.mFull++;
447 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800448 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530449 anyEnabledTracks = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700450 }
Glenn Kasten22340022014-04-07 12:04:41 -0700451 ftDump->mUnderruns = underruns;
452 ftDump->mFramesReady = framesReady;
Andy Hungb54c8542016-09-21 12:55:15 -0700453 ftDump->mFramesWritten = trackFramesWritten;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700454 }
455
Mohan Kumar947ffa32014-12-12 15:16:46 +0530456 if (anyEnabledTracks) {
457 // process() is CPU-bound
458 mMixer->process();
459 mMixerBufferState = MIXED;
460 } else if (mMixerBufferState != ZEROED) {
461 mMixerBufferState = UNDEFINED;
462 }
463
Andy Hung45d68d32014-05-23 21:13:31 -0700464 } else if (mMixerBufferState == MIXED) {
465 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700466 }
467 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800468 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
Andy Hung45d68d32014-05-23 21:13:31 -0700469 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700470 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700471 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700472 }
Andy Hung2ddee192015-12-18 17:34:44 -0800473
474 if (mMasterMono.load()) { // memory_order_seq_cst
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700475 mono_blend(mMixerBuffer, mMixerBufferFormat, Format_channelCount(mFormat), frameCount,
476 true /*limit*/);
Andy Hung2ddee192015-12-18 17:34:44 -0800477 }
Glenn Kastenf59497b2015-01-26 16:35:47 -0800478 // prepare the buffer used to write to sink
Andy Hung1258c1a2014-05-23 21:22:17 -0700479 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800480 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
481 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
482 frameCount * Format_channelCount(mFormat));
Andy Hung1258c1a2014-05-23 21:22:17 -0700483 }
jiabin245cdd92018-12-07 17:55:15 -0800484 if (mSinkChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) {
485 // When there are haptic channels, the sample data is partially interleaved.
486 // Make the sample data fully interleaved here.
487 adjust_channels_non_destructive(buffer, mAudioChannelCount, buffer, mSinkChannelCount,
488 audio_bytes_per_sample(mFormat.mFormat),
489 frameCount * audio_bytes_per_frame(mAudioChannelCount, mFormat.mFormat));
490 }
Glenn Kasten22340022014-04-07 12:04:41 -0700491 // if non-NULL, then duplicate write() to this non-blocking sink
Andy Hung8946a282018-04-19 20:04:56 -0700492#ifdef TEE_SINK
493 mTee.write(buffer, frameCount);
494#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700495 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
496 // but this code should be modified to handle both non-blocking and blocking sinks
497 dumpState->mWriteSequence++;
498 ATRACE_BEGIN("write");
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800499 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700500 ATRACE_END();
501 dumpState->mWriteSequence++;
502 if (framesWritten >= 0) {
503 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800504 mTotalNativeFramesWritten += framesWritten;
505 dumpState->mFramesWritten = mTotalNativeFramesWritten;
Glenn Kasten22340022014-04-07 12:04:41 -0700506 //if ((size_t) framesWritten == frameCount) {
507 // didFullWrite = true;
508 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700509 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700510 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700511 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800512 mAttemptedWrite = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700513 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700514
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700515 if (mIsWarm) {
516 ExtendedTimestamp timestamp; // local
517 status_t status = mOutputSink->getTimestamp(timestamp);
518 if (status == NO_ERROR) {
519 dumpState->mTimestampVerifier.add(
520 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
521 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
522 mSampleRate);
523 const int64_t totalNativeFramesPresented =
Andy Hung818e7a32016-02-16 18:08:07 -0800524 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700525 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
526 mNativeFramesWrittenButNotPresented =
527 mTotalNativeFramesWritten - totalNativeFramesPresented;
528 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
529 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
530 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
531 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
532 // We don't compensate for server - kernel time difference and
533 // only update latency if we have valid info.
Eric Tane98dd6f2018-08-22 18:23:50 -0700534 const double latencyMs =
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700535 (double)mNativeFramesWrittenButNotPresented * 1000 / mSampleRate;
Eric Tane98dd6f2018-08-22 18:23:50 -0700536 dumpState->mLatencyMs = latencyMs;
537 LOG_LATENCY(latencyMs);
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700538 } else {
539 // HAL reported that more frames were presented than were written
540 mNativeFramesWrittenButNotPresented = 0;
541 status = INVALID_OPERATION;
542 }
Glenn Kasten22340022014-04-07 12:04:41 -0700543 } else {
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700544 dumpState->mTimestampVerifier.error();
Glenn Kasten22340022014-04-07 12:04:41 -0700545 }
Andy Hung2e2c0bb2018-06-11 19:13:11 -0700546 if (status == NO_ERROR) {
547 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
548 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
549 } else {
550 // fetch server time if we can't get timestamp
551 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
552 systemTime(SYSTEM_TIME_MONOTONIC);
553 // clear out kernel cached position as this may get rapidly stale
554 // if we never get a new valid timestamp
555 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
556 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
557 }
Andy Hung818e7a32016-02-16 18:08:07 -0800558 }
Glenn Kasten22340022014-04-07 12:04:41 -0700559 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700560}
561
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700562} // namespace android