blob: c4f1af354efe04e70cd82a0b1160ccfb9dd24e4d [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
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070035#include <cpustats/CentralTendencyStatistics.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
Glenn Kasten6bf707f2017-02-23 16:53:58 -080040#include <audio_utils/mono_blend.h>
Andy Hung1258c1a2014-05-23 21:22:17 -070041#include <audio_utils/format.h>
Andy Hung068561c2017-01-03 17:09:32 -080042#include <media/AudioMixer.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070043#include "FastMixer.h"
Glenn Kasteneef598c2017-04-03 14:41:13 -070044#include "TypedLogger.h"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070045
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070046namespace android {
47
Glenn Kastene4a7ce22015-03-03 11:23:17 -080048/*static*/ const FastMixerState FastMixer::sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070049
Glenn Kastenf9715e42016-07-13 14:02:03 -070050FastMixer::FastMixer() : FastThread("cycle_ms", "load_us"),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080051 // mFastTrackNames
52 // mGenerations
53 mOutputSink(NULL),
54 mOutputSinkGen(0),
55 mMixer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070056 mSinkBuffer(NULL),
57 mSinkBufferSize(0),
Andy Hung9a592762014-07-21 21:56:01 -070058 mSinkChannelCount(FCC_2),
Andy Hung45d68d32014-05-23 21:13:31 -070059 mMixerBuffer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070060 mMixerBufferSize(0),
61 mMixerBufferFormat(AUDIO_FORMAT_PCM_16_BIT),
Andy Hung45d68d32014-05-23 21:13:31 -070062 mMixerBufferState(UNDEFINED),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080063 mFormat(Format_Invalid),
64 mSampleRate(0),
65 mFastTracksGen(0),
66 mTotalNativeFramesWritten(0),
Glenn Kasten22340022014-04-07 12:04:41 -070067 // timestamp
Andy Hung2ddee192015-12-18 17:34:44 -080068 mNativeFramesWrittenButNotPresented(0), // the = 0 is to silence the compiler
69 mMasterMono(false)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070070{
Glenn Kastene4a7ce22015-03-03 11:23:17 -080071 // FIXME pass sInitial as parameter to base class constructor, and make it static local
72 mPrevious = &sInitial;
73 mCurrent = &sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070074
Glenn Kastene4a7ce22015-03-03 11:23:17 -080075 mDummyDumpState = &mDummyFastMixerDumpState;
Andy Hung9a592762014-07-21 21:56:01 -070076 // TODO: Add channel mask to NBAIO_Format.
77 // We assume that the channel mask must be a valid positional channel mask.
78 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -070079
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070080 unsigned i;
Glenn Kastendc2c50b2016-04-21 08:13:14 -070081 for (i = 0; i < FastMixerState::sMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -080082 mFastTrackNames[i] = -1;
83 mGenerations[i] = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070084 }
Glenn Kasten214b4062015-03-02 14:15:47 -080085#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080086 mOldLoad.tv_sec = 0;
87 mOldLoad.tv_nsec = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070088#endif
Glenn Kasten22340022014-04-07 12:04:41 -070089}
90
91FastMixer::~FastMixer()
92{
93}
94
95FastMixerStateQueue* FastMixer::sq()
96{
97 return &mSQ;
98}
99
100const FastThreadState *FastMixer::poll()
101{
102 return mSQ.poll();
103}
104
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700105void FastMixer::setNBLogWriter(NBLog::Writer *logWriter)
Glenn Kasten22340022014-04-07 12:04:41 -0700106{
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700107 // FIXME If mMixer is set or changed prior to this, we don't inform correctly.
108 // Should cache logWriter and re-apply it at the assignment to mMixer.
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800109 if (mMixer != NULL) {
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700110 mMixer->setNBLogWriter(logWriter);
Glenn Kasten22340022014-04-07 12:04:41 -0700111 }
112}
113
114void FastMixer::onIdle()
115{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800116 mPreIdle = *(const FastMixerState *)mCurrent;
117 mCurrent = &mPreIdle;
Glenn Kasten22340022014-04-07 12:04:41 -0700118}
119
120void FastMixer::onExit()
121{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800122 delete mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -0700123 free(mMixerBuffer);
124 free(mSinkBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700125}
126
127bool FastMixer::isSubClassCommand(FastThreadState::Command command)
128{
129 switch ((FastMixerState::Command) command) {
130 case FastMixerState::MIX:
131 case FastMixerState::WRITE:
132 case FastMixerState::MIX_WRITE:
133 return true;
134 default:
135 return false;
136 }
137}
138
139void FastMixer::onStateChange()
140{
Glenn Kasteneef598c2017-04-03 14:41:13 -0700141 LOG_HIST_FLUSH();
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800142 const FastMixerState * const current = (const FastMixerState *) mCurrent;
143 const FastMixerState * const previous = (const FastMixerState *) mPrevious;
144 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
Glenn Kasten22340022014-04-07 12:04:41 -0700145 const size_t frameCount = current->mFrameCount;
146
Andy Hung818e7a32016-02-16 18:08:07 -0800147 // update boottime offset, in case it has changed
148 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
149 mBoottimeOffset.load();
150
Glenn Kasten22340022014-04-07 12:04:41 -0700151 // handle state change here, but since we want to diff the state,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800152 // we're prepared for previous == &sInitial the first time through
Glenn Kasten22340022014-04-07 12:04:41 -0700153 unsigned previousTrackMask;
154
155 // check for change in output HAL configuration
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800156 NBAIO_Format previousFormat = mFormat;
157 if (current->mOutputSinkGen != mOutputSinkGen) {
158 mOutputSink = current->mOutputSink;
159 mOutputSinkGen = current->mOutputSinkGen;
160 if (mOutputSink == NULL) {
161 mFormat = Format_Invalid;
162 mSampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700163 mSinkChannelCount = 0;
164 mSinkChannelMask = AUDIO_CHANNEL_NONE;
Glenn Kasten22340022014-04-07 12:04:41 -0700165 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800166 mFormat = mOutputSink->format();
167 mSampleRate = Format_sampleRate(mFormat);
168 mSinkChannelCount = Format_channelCount(mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700169 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
170
171 // TODO: Add channel mask to NBAIO_Format
172 // We assume that the channel mask must be a valid positional channel mask.
173 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700174 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800175 dumpState->mSampleRate = mSampleRate;
Glenn Kasten22340022014-04-07 12:04:41 -0700176 }
177
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800178 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
Glenn Kasten22340022014-04-07 12:04:41 -0700179 // FIXME to avoid priority inversion, don't delete here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800180 delete mMixer;
181 mMixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700182 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700183 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700184 free(mSinkBuffer);
185 mSinkBuffer = NULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800186 if (frameCount > 0 && mSampleRate > 0) {
Andy Hung60c545d2015-06-19 17:34:53 -0700187 // The mixer produces either 16 bit PCM or float output, select
188 // float output if the HAL supports higher than 16 bit precision.
189 mMixerBufferFormat = mFormat.mFormat == AUDIO_FORMAT_PCM_16_BIT ?
190 AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_FLOAT;
Glenn Kasten22340022014-04-07 12:04:41 -0700191 // FIXME new may block for unbounded time at internal mutex of the heap
192 // implementation; it would be better to have normal mixer allocate for us
193 // to avoid blocking here and to prevent possible priority inversion
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700194 mMixer = new AudioMixer(frameCount, mSampleRate, FastMixerState::sMaxFastTracks);
Glenn Kasten3ab8d662017-04-03 14:35:09 -0700195 // FIXME See the other FIXME at FastMixer::setNBLogWriter()
Andy Hung9a592762014-07-21 21:56:01 -0700196 const size_t mixerFrameSize = mSinkChannelCount
197 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700198 mMixerBufferSize = mixerFrameSize * frameCount;
199 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700200 const size_t sinkFrameSize = mSinkChannelCount
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800201 * audio_bytes_per_sample(mFormat.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700202 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
203 mSinkBufferSize = sinkFrameSize * frameCount;
204 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
205 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800206 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
207 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
208 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
209 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
210 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
211 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
Glenn Kasten22340022014-04-07 12:04:41 -0700212 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800213 mPeriodNs = 0;
214 mUnderrunNs = 0;
215 mOverrunNs = 0;
216 mForceNs = 0;
217 mWarmupNsMin = 0;
218 mWarmupNsMax = LONG_MAX;
Glenn Kasten22340022014-04-07 12:04:41 -0700219 }
Andy Hung45d68d32014-05-23 21:13:31 -0700220 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700221#if !LOG_NDEBUG
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700222 for (unsigned i = 0; i < FastMixerState::sMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800223 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700224 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700225#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700226 // we need to reconfigure all active tracks
227 previousTrackMask = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800228 mFastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten22340022014-04-07 12:04:41 -0700229 dumpState->mFrameCount = frameCount;
230 } else {
231 previousTrackMask = previous->mTrackMask;
232 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700233
Glenn Kasten22340022014-04-07 12:04:41 -0700234 // check for change in active track set
235 const unsigned currentTrackMask = current->mTrackMask;
236 dumpState->mTrackMask = currentTrackMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800237 if (current->mFastTracksGen != mFastTracksGen) {
Andy Hung45d68d32014-05-23 21:13:31 -0700238 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700239 int name;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700240
Glenn Kasten22340022014-04-07 12:04:41 -0700241 // process removed tracks first to avoid running out of track names
242 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
243 while (removedTracks != 0) {
244 int i = __builtin_ctz(removedTracks);
245 removedTracks &= ~(1 << i);
246 const FastTrack* fastTrack = &current->mFastTracks[i];
247 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800248 if (mMixer != NULL) {
249 name = mFastTrackNames[i];
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700250 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800251 mMixer->deleteTrackName(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700252 }
253#if !LOG_NDEBUG
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800254 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700255#endif
256 // don't reset track dump state, since other side is ignoring it
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800257 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700258 }
259
260 // now process added tracks
261 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
262 while (addedTracks != 0) {
263 int i = __builtin_ctz(addedTracks);
264 addedTracks &= ~(1 << i);
265 const FastTrack* fastTrack = &current->mFastTracks[i];
266 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800267 ALOG_ASSERT(bufferProvider != NULL && mFastTrackNames[i] == -1);
268 if (mMixer != NULL) {
269 name = mMixer->getTrackName(fastTrack->mChannelMask,
Andy Hunge8a1ced2014-05-09 15:02:21 -0700270 fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Glenn Kasten22340022014-04-07 12:04:41 -0700271 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800272 mFastTrackNames[i] = name;
273 mMixer->setBufferProvider(name, bufferProvider);
274 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
Andy Hung9a592762014-07-21 21:56:01 -0700275 (void *)mMixerBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700276 // newly allocated track names default to full scale volume
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800277 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700278 name,
279 AudioMixer::TRACK,
280 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800281 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700282 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800283 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700284 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800285 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700286 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800287 mMixer->enable(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700288 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800289 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700290 }
291
292 // finally process (potentially) modified tracks; these use the same slot
293 // but may have a different buffer provider or volume provider
294 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
295 while (modifiedTracks != 0) {
296 int i = __builtin_ctz(modifiedTracks);
297 modifiedTracks &= ~(1 << i);
298 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800299 if (fastTrack->mGeneration != mGenerations[i]) {
Glenn Kasten22340022014-04-07 12:04:41 -0700300 // this track was actually modified
301 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
302 ALOG_ASSERT(bufferProvider != NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800303 if (mMixer != NULL) {
304 name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700305 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800306 mMixer->setBufferProvider(name, bufferProvider);
Glenn Kasten22340022014-04-07 12:04:41 -0700307 if (fastTrack->mVolumeProvider == NULL) {
Andy Hung6be49402014-05-30 10:42:03 -0700308 float f = AudioMixer::UNITY_GAIN_FLOAT;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800309 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
310 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
Glenn Kasten288ed212012-04-25 17:52:27 -0700311 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800312 mMixer->setParameter(name, AudioMixer::RESAMPLE,
Glenn Kasten22340022014-04-07 12:04:41 -0700313 AudioMixer::REMOVE, NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800314 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700315 name,
316 AudioMixer::TRACK,
317 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800318 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700319 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800320 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700321 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800322 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700323 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700324 // already enabled
325 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800326 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700327 }
328 }
329
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800330 mFastTracksGen = current->mFastTracksGen;
Glenn Kasten22340022014-04-07 12:04:41 -0700331
332 dumpState->mNumTracks = popcount(currentTrackMask);
333 }
334}
335
336void FastMixer::onWork()
337{
Glenn Kasteneef598c2017-04-03 14:41:13 -0700338 LOG_HIST_TS();
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800339 const FastMixerState * const current = (const FastMixerState *) mCurrent;
340 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
341 const FastMixerState::Command command = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700342 const size_t frameCount = current->mFrameCount;
343
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800344 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700345 ALOG_ASSERT(mMixerBuffer != NULL);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530346
347 // AudioMixer::mState.enabledTracks is undefined if mState.hook == process__validate,
348 // so we keep a side copy of enabledTracks
349 bool anyEnabledTracks = false;
350
Glenn Kasten22340022014-04-07 12:04:41 -0700351 // for each track, update volume and check for underrun
352 unsigned currentTrackMask = current->mTrackMask;
353 while (currentTrackMask != 0) {
354 int i = __builtin_ctz(currentTrackMask);
355 currentTrackMask &= ~(1 << i);
356 const FastTrack* fastTrack = &current->mFastTracks[i];
357
Andy Hung818e7a32016-02-16 18:08:07 -0800358 const int64_t trackFramesWrittenButNotPresented =
359 mNativeFramesWrittenButNotPresented;
360 const int64_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
361 ExtendedTimestamp perTrackTimestamp(mTimestamp);
362
363 // Can't provide an ExtendedTimestamp before first frame presented.
364 // Also, timestamp may not go to very last frame on stop().
365 if (trackFramesWritten >= trackFramesWrittenButNotPresented &&
366 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
367 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
368 trackFramesWritten - trackFramesWrittenButNotPresented;
369 } else {
370 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
371 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700372 }
Andy Hung818e7a32016-02-16 18:08:07 -0800373 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = trackFramesWritten;
374 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
Glenn Kasten22340022014-04-07 12:04:41 -0700375
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800376 int name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700377 ALOG_ASSERT(name >= 0);
378 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700379 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700380 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
381 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
382
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800383 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
384 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700385 }
386 // FIXME The current implementation of framesReady() for fast tracks
387 // takes a tryLock, which can block
388 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
389 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
390 size_t framesReady = fastTrack->mBufferProvider->framesReady();
391 if (ATRACE_ENABLED()) {
392 // I wish we had formatted trace names
393 char traceName[16];
394 strcpy(traceName, "fRdy");
395 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
396 traceName[5] = '\0';
397 ATRACE_INT(traceName, framesReady);
398 }
399 FastTrackDump *ftDump = &dumpState->mTracks[i];
400 FastTrackUnderruns underruns = ftDump->mUnderruns;
401 if (framesReady < frameCount) {
402 if (framesReady == 0) {
403 underruns.mBitFields.mEmpty++;
404 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800405 mMixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700406 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700407 // allow mixing partial buffer
408 underruns.mBitFields.mPartial++;
409 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800410 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530411 anyEnabledTracks = true;
Glenn Kasten288ed212012-04-25 17:52:27 -0700412 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700413 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700414 underruns.mBitFields.mFull++;
415 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800416 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530417 anyEnabledTracks = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700418 }
Glenn Kasten22340022014-04-07 12:04:41 -0700419 ftDump->mUnderruns = underruns;
420 ftDump->mFramesReady = framesReady;
Andy Hungb54c8542016-09-21 12:55:15 -0700421 ftDump->mFramesWritten = trackFramesWritten;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700422 }
423
Mohan Kumar947ffa32014-12-12 15:16:46 +0530424 if (anyEnabledTracks) {
425 // process() is CPU-bound
426 mMixer->process();
427 mMixerBufferState = MIXED;
428 } else if (mMixerBufferState != ZEROED) {
429 mMixerBufferState = UNDEFINED;
430 }
431
Andy Hung45d68d32014-05-23 21:13:31 -0700432 } else if (mMixerBufferState == MIXED) {
433 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700434 }
435 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800436 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
Andy Hung45d68d32014-05-23 21:13:31 -0700437 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700438 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700439 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700440 }
Andy Hung2ddee192015-12-18 17:34:44 -0800441
442 if (mMasterMono.load()) { // memory_order_seq_cst
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700443 mono_blend(mMixerBuffer, mMixerBufferFormat, Format_channelCount(mFormat), frameCount,
444 true /*limit*/);
Andy Hung2ddee192015-12-18 17:34:44 -0800445 }
Glenn Kastenf59497b2015-01-26 16:35:47 -0800446 // prepare the buffer used to write to sink
Andy Hung1258c1a2014-05-23 21:22:17 -0700447 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800448 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
449 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
450 frameCount * Format_channelCount(mFormat));
Andy Hung1258c1a2014-05-23 21:22:17 -0700451 }
Glenn Kasten22340022014-04-07 12:04:41 -0700452 // if non-NULL, then duplicate write() to this non-blocking sink
453 NBAIO_Sink* teeSink;
454 if ((teeSink = current->mTeeSink) != NULL) {
Glenn Kasten329f6512014-08-28 16:23:16 -0700455 (void) teeSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700456 }
457 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
458 // but this code should be modified to handle both non-blocking and blocking sinks
459 dumpState->mWriteSequence++;
460 ATRACE_BEGIN("write");
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800461 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700462 ATRACE_END();
463 dumpState->mWriteSequence++;
464 if (framesWritten >= 0) {
465 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800466 mTotalNativeFramesWritten += framesWritten;
467 dumpState->mFramesWritten = mTotalNativeFramesWritten;
Glenn Kasten22340022014-04-07 12:04:41 -0700468 //if ((size_t) framesWritten == frameCount) {
469 // didFullWrite = true;
470 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700471 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700472 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700473 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800474 mAttemptedWrite = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700475 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700476
Andy Hung818e7a32016-02-16 18:08:07 -0800477 ExtendedTimestamp timestamp; // local
478 status_t status = mOutputSink->getTimestamp(timestamp);
479 if (status == NO_ERROR) {
480 const int64_t totalNativeFramesPresented =
481 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800482 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
483 mNativeFramesWrittenButNotPresented =
484 mTotalNativeFramesWritten - totalNativeFramesPresented;
Andy Hung818e7a32016-02-16 18:08:07 -0800485 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
486 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
487 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
488 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
Glenn Kasten22340022014-04-07 12:04:41 -0700489 } else {
490 // HAL reported that more frames were presented than were written
Andy Hung818e7a32016-02-16 18:08:07 -0800491 mNativeFramesWrittenButNotPresented = 0;
Andy Hung818e7a32016-02-16 18:08:07 -0800492 status = INVALID_OPERATION;
Glenn Kasten22340022014-04-07 12:04:41 -0700493 }
494 }
Andy Hung818e7a32016-02-16 18:08:07 -0800495 if (status == NO_ERROR) {
496 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
497 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
498 } else {
499 // fetch server time if we can't get timestamp
500 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
501 systemTime(SYSTEM_TIME_MONOTONIC);
Andy Hung07eee802016-06-21 16:47:16 -0700502 // clear out kernel cached position as this may get rapidly stale
503 // if we never get a new valid timestamp
504 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
505 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
Andy Hung818e7a32016-02-16 18:08:07 -0800506 }
Glenn Kasten22340022014-04-07 12:04:41 -0700507 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700508}
509
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700510} // namespace android