blob: d31b8d3c8059c3c9f25ee6d4f832c0b8b931b9cf [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
Andy Hung2ddee192015-12-18 17:34:44 -080040#include <audio_utils/conversion.h>
Andy Hung1258c1a2014-05-23 21:22:17 -070041#include <audio_utils/format.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070042#include "AudioMixer.h"
43#include "FastMixer.h"
44
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070045namespace android {
46
Glenn Kastene4a7ce22015-03-03 11:23:17 -080047/*static*/ const FastMixerState FastMixer::sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070048
49FastMixer::FastMixer() : FastThread(),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080050 mSlopNs(0),
51 // 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;
81 for (i = 0; i < FastMixerState::kMaxFastTracks; ++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
105void FastMixer::setLog(NBLog::Writer *logWriter)
106{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800107 if (mMixer != NULL) {
108 mMixer->setLog(logWriter);
Glenn Kasten22340022014-04-07 12:04:41 -0700109 }
110}
111
112void FastMixer::onIdle()
113{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800114 mPreIdle = *(const FastMixerState *)mCurrent;
115 mCurrent = &mPreIdle;
Glenn Kasten22340022014-04-07 12:04:41 -0700116}
117
118void FastMixer::onExit()
119{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800120 delete mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -0700121 free(mMixerBuffer);
122 free(mSinkBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700123}
124
125bool FastMixer::isSubClassCommand(FastThreadState::Command command)
126{
127 switch ((FastMixerState::Command) command) {
128 case FastMixerState::MIX:
129 case FastMixerState::WRITE:
130 case FastMixerState::MIX_WRITE:
131 return true;
132 default:
133 return false;
134 }
135}
136
137void FastMixer::onStateChange()
138{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800139 const FastMixerState * const current = (const FastMixerState *) mCurrent;
140 const FastMixerState * const previous = (const FastMixerState *) mPrevious;
141 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
Glenn Kasten22340022014-04-07 12:04:41 -0700142 const size_t frameCount = current->mFrameCount;
143
Andy Hung818e7a32016-02-16 18:08:07 -0800144 // update boottime offset, in case it has changed
145 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
146 mBoottimeOffset.load();
147
Glenn Kasten22340022014-04-07 12:04:41 -0700148 // handle state change here, but since we want to diff the state,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800149 // we're prepared for previous == &sInitial the first time through
Glenn Kasten22340022014-04-07 12:04:41 -0700150 unsigned previousTrackMask;
151
152 // check for change in output HAL configuration
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800153 NBAIO_Format previousFormat = mFormat;
154 if (current->mOutputSinkGen != mOutputSinkGen) {
155 mOutputSink = current->mOutputSink;
156 mOutputSinkGen = current->mOutputSinkGen;
157 if (mOutputSink == NULL) {
158 mFormat = Format_Invalid;
159 mSampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700160 mSinkChannelCount = 0;
161 mSinkChannelMask = AUDIO_CHANNEL_NONE;
Glenn Kasten22340022014-04-07 12:04:41 -0700162 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800163 mFormat = mOutputSink->format();
164 mSampleRate = Format_sampleRate(mFormat);
165 mSinkChannelCount = Format_channelCount(mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700166 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
167
168 // TODO: Add channel mask to NBAIO_Format
169 // We assume that the channel mask must be a valid positional channel mask.
170 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700171 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800172 dumpState->mSampleRate = mSampleRate;
Glenn Kasten22340022014-04-07 12:04:41 -0700173 }
174
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800175 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
Glenn Kasten22340022014-04-07 12:04:41 -0700176 // FIXME to avoid priority inversion, don't delete here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800177 delete mMixer;
178 mMixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700179 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700180 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700181 free(mSinkBuffer);
182 mSinkBuffer = NULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800183 if (frameCount > 0 && mSampleRate > 0) {
Andy Hung60c545d2015-06-19 17:34:53 -0700184 // The mixer produces either 16 bit PCM or float output, select
185 // float output if the HAL supports higher than 16 bit precision.
186 mMixerBufferFormat = mFormat.mFormat == AUDIO_FORMAT_PCM_16_BIT ?
187 AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_FLOAT;
Glenn Kasten22340022014-04-07 12:04:41 -0700188 // FIXME new may block for unbounded time at internal mutex of the heap
189 // implementation; it would be better to have normal mixer allocate for us
190 // to avoid blocking here and to prevent possible priority inversion
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800191 mMixer = new AudioMixer(frameCount, mSampleRate, FastMixerState::kMaxFastTracks);
Andy Hung9a592762014-07-21 21:56:01 -0700192 const size_t mixerFrameSize = mSinkChannelCount
193 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700194 mMixerBufferSize = mixerFrameSize * frameCount;
195 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700196 const size_t sinkFrameSize = mSinkChannelCount
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800197 * audio_bytes_per_sample(mFormat.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700198 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
199 mSinkBufferSize = sinkFrameSize * frameCount;
200 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
201 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800202 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
203 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
204 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
205 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
206 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
207 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
Glenn Kasten22340022014-04-07 12:04:41 -0700208 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800209 mPeriodNs = 0;
210 mUnderrunNs = 0;
211 mOverrunNs = 0;
212 mForceNs = 0;
213 mWarmupNsMin = 0;
214 mWarmupNsMax = LONG_MAX;
Glenn Kasten22340022014-04-07 12:04:41 -0700215 }
Andy Hung45d68d32014-05-23 21:13:31 -0700216 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700217#if !LOG_NDEBUG
218 for (unsigned i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800219 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700220 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700221#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700222 // we need to reconfigure all active tracks
223 previousTrackMask = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800224 mFastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten22340022014-04-07 12:04:41 -0700225 dumpState->mFrameCount = frameCount;
226 } else {
227 previousTrackMask = previous->mTrackMask;
228 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700229
Glenn Kasten22340022014-04-07 12:04:41 -0700230 // check for change in active track set
231 const unsigned currentTrackMask = current->mTrackMask;
232 dumpState->mTrackMask = currentTrackMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800233 if (current->mFastTracksGen != mFastTracksGen) {
Andy Hung45d68d32014-05-23 21:13:31 -0700234 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700235 int name;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700236
Glenn Kasten22340022014-04-07 12:04:41 -0700237 // process removed tracks first to avoid running out of track names
238 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
239 while (removedTracks != 0) {
240 int i = __builtin_ctz(removedTracks);
241 removedTracks &= ~(1 << i);
242 const FastTrack* fastTrack = &current->mFastTracks[i];
243 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800244 if (mMixer != NULL) {
245 name = mFastTrackNames[i];
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700246 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800247 mMixer->deleteTrackName(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700248 }
249#if !LOG_NDEBUG
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800250 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700251#endif
252 // don't reset track dump state, since other side is ignoring it
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800253 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700254 }
255
256 // now process added tracks
257 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
258 while (addedTracks != 0) {
259 int i = __builtin_ctz(addedTracks);
260 addedTracks &= ~(1 << i);
261 const FastTrack* fastTrack = &current->mFastTracks[i];
262 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800263 ALOG_ASSERT(bufferProvider != NULL && mFastTrackNames[i] == -1);
264 if (mMixer != NULL) {
265 name = mMixer->getTrackName(fastTrack->mChannelMask,
Andy Hunge8a1ced2014-05-09 15:02:21 -0700266 fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Glenn Kasten22340022014-04-07 12:04:41 -0700267 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800268 mFastTrackNames[i] = name;
269 mMixer->setBufferProvider(name, bufferProvider);
270 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
Andy Hung9a592762014-07-21 21:56:01 -0700271 (void *)mMixerBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700272 // newly allocated track names default to full scale volume
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800273 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700274 name,
275 AudioMixer::TRACK,
276 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800277 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700278 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800279 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700280 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800281 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700282 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800283 mMixer->enable(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700284 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800285 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700286 }
287
288 // finally process (potentially) modified tracks; these use the same slot
289 // but may have a different buffer provider or volume provider
290 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
291 while (modifiedTracks != 0) {
292 int i = __builtin_ctz(modifiedTracks);
293 modifiedTracks &= ~(1 << i);
294 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800295 if (fastTrack->mGeneration != mGenerations[i]) {
Glenn Kasten22340022014-04-07 12:04:41 -0700296 // this track was actually modified
297 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
298 ALOG_ASSERT(bufferProvider != NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800299 if (mMixer != NULL) {
300 name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700301 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800302 mMixer->setBufferProvider(name, bufferProvider);
Glenn Kasten22340022014-04-07 12:04:41 -0700303 if (fastTrack->mVolumeProvider == NULL) {
Andy Hung6be49402014-05-30 10:42:03 -0700304 float f = AudioMixer::UNITY_GAIN_FLOAT;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800305 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
306 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
Glenn Kasten288ed212012-04-25 17:52:27 -0700307 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800308 mMixer->setParameter(name, AudioMixer::RESAMPLE,
Glenn Kasten22340022014-04-07 12:04:41 -0700309 AudioMixer::REMOVE, NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800310 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700311 name,
312 AudioMixer::TRACK,
313 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800314 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700315 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800316 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700317 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800318 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700319 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700320 // already enabled
321 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800322 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700323 }
324 }
325
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800326 mFastTracksGen = current->mFastTracksGen;
Glenn Kasten22340022014-04-07 12:04:41 -0700327
328 dumpState->mNumTracks = popcount(currentTrackMask);
329 }
330}
331
332void FastMixer::onWork()
333{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800334 const FastMixerState * const current = (const FastMixerState *) mCurrent;
335 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
336 const FastMixerState::Command command = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700337 const size_t frameCount = current->mFrameCount;
338
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800339 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700340 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700341 // for each track, update volume and check for underrun
342 unsigned currentTrackMask = current->mTrackMask;
343 while (currentTrackMask != 0) {
344 int i = __builtin_ctz(currentTrackMask);
345 currentTrackMask &= ~(1 << i);
346 const FastTrack* fastTrack = &current->mFastTracks[i];
347
Andy Hung818e7a32016-02-16 18:08:07 -0800348 const int64_t trackFramesWrittenButNotPresented =
349 mNativeFramesWrittenButNotPresented;
350 const int64_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
351 ExtendedTimestamp perTrackTimestamp(mTimestamp);
352
353 // Can't provide an ExtendedTimestamp before first frame presented.
354 // Also, timestamp may not go to very last frame on stop().
355 if (trackFramesWritten >= trackFramesWrittenButNotPresented &&
356 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
357 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
358 trackFramesWritten - trackFramesWrittenButNotPresented;
359 } else {
360 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
361 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700362 }
Andy Hung818e7a32016-02-16 18:08:07 -0800363 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = trackFramesWritten;
364 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
Glenn Kasten22340022014-04-07 12:04:41 -0700365
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800366 int name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700367 ALOG_ASSERT(name >= 0);
368 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700369 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700370 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
371 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
372
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800373 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
374 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700375 }
376 // FIXME The current implementation of framesReady() for fast tracks
377 // takes a tryLock, which can block
378 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
379 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
380 size_t framesReady = fastTrack->mBufferProvider->framesReady();
381 if (ATRACE_ENABLED()) {
382 // I wish we had formatted trace names
383 char traceName[16];
384 strcpy(traceName, "fRdy");
385 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
386 traceName[5] = '\0';
387 ATRACE_INT(traceName, framesReady);
388 }
389 FastTrackDump *ftDump = &dumpState->mTracks[i];
390 FastTrackUnderruns underruns = ftDump->mUnderruns;
391 if (framesReady < frameCount) {
392 if (framesReady == 0) {
393 underruns.mBitFields.mEmpty++;
394 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800395 mMixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700396 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700397 // allow mixing partial buffer
398 underruns.mBitFields.mPartial++;
399 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800400 mMixer->enable(name);
Glenn Kasten288ed212012-04-25 17:52:27 -0700401 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700402 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700403 underruns.mBitFields.mFull++;
404 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800405 mMixer->enable(name);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700406 }
Glenn Kasten22340022014-04-07 12:04:41 -0700407 ftDump->mUnderruns = underruns;
408 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700409 }
410
Glenn Kasten22340022014-04-07 12:04:41 -0700411 // process() is CPU-bound
Glenn Kastend79072e2016-01-06 08:41:20 -0800412 mMixer->process();
Andy Hung45d68d32014-05-23 21:13:31 -0700413 mMixerBufferState = MIXED;
414 } else if (mMixerBufferState == MIXED) {
415 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700416 }
417 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800418 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
Andy Hung45d68d32014-05-23 21:13:31 -0700419 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700420 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700421 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700422 }
Andy Hung2ddee192015-12-18 17:34:44 -0800423
424 if (mMasterMono.load()) { // memory_order_seq_cst
Glenn Kasten03c48d52016-01-27 17:25:17 -0800425 mono_blend(mMixerBuffer, mMixerBufferFormat, Format_channelCount(mFormat), frameCount, true /*limit*/);
Andy Hung2ddee192015-12-18 17:34:44 -0800426 }
Glenn Kastenf59497b2015-01-26 16:35:47 -0800427 // prepare the buffer used to write to sink
Andy Hung1258c1a2014-05-23 21:22:17 -0700428 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800429 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
430 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
431 frameCount * Format_channelCount(mFormat));
Andy Hung1258c1a2014-05-23 21:22:17 -0700432 }
Glenn Kasten22340022014-04-07 12:04:41 -0700433 // if non-NULL, then duplicate write() to this non-blocking sink
434 NBAIO_Sink* teeSink;
435 if ((teeSink = current->mTeeSink) != NULL) {
Glenn Kasten329f6512014-08-28 16:23:16 -0700436 (void) teeSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700437 }
438 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
439 // but this code should be modified to handle both non-blocking and blocking sinks
440 dumpState->mWriteSequence++;
441 ATRACE_BEGIN("write");
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800442 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700443 ATRACE_END();
444 dumpState->mWriteSequence++;
445 if (framesWritten >= 0) {
446 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800447 mTotalNativeFramesWritten += framesWritten;
448 dumpState->mFramesWritten = mTotalNativeFramesWritten;
Glenn Kasten22340022014-04-07 12:04:41 -0700449 //if ((size_t) framesWritten == frameCount) {
450 // didFullWrite = true;
451 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700452 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700453 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700454 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800455 mAttemptedWrite = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700456 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700457
Andy Hung818e7a32016-02-16 18:08:07 -0800458 ExtendedTimestamp timestamp; // local
459 status_t status = mOutputSink->getTimestamp(timestamp);
460 if (status == NO_ERROR) {
461 const int64_t totalNativeFramesPresented =
462 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800463 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
464 mNativeFramesWrittenButNotPresented =
465 mTotalNativeFramesWritten - totalNativeFramesPresented;
Andy Hung818e7a32016-02-16 18:08:07 -0800466 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
467 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
468 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
469 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
Glenn Kasten22340022014-04-07 12:04:41 -0700470 } else {
471 // HAL reported that more frames were presented than were written
Andy Hung818e7a32016-02-16 18:08:07 -0800472 mNativeFramesWrittenButNotPresented = 0;
473 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
474 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
475 status = INVALID_OPERATION;
Glenn Kasten22340022014-04-07 12:04:41 -0700476 }
477 }
Andy Hung818e7a32016-02-16 18:08:07 -0800478 if (status == NO_ERROR) {
479 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
480 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
481 } else {
482 // fetch server time if we can't get timestamp
483 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
484 systemTime(SYSTEM_TIME_MONOTONIC);
485 }
Glenn Kasten22340022014-04-07 12:04:41 -0700486 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700487}
488
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700489} // namespace android