blob: b0780a46ef4015d911dbdf8306f41569398db6ef [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
Glenn Kastenf9715e42016-07-13 14:02:03 -070049FastMixer::FastMixer() : FastThread("cycle_ms", "load_us"),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080050 // mFastTrackNames
51 // mGenerations
52 mOutputSink(NULL),
53 mOutputSinkGen(0),
54 mMixer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070055 mSinkBuffer(NULL),
56 mSinkBufferSize(0),
Andy Hung9a592762014-07-21 21:56:01 -070057 mSinkChannelCount(FCC_2),
Andy Hung45d68d32014-05-23 21:13:31 -070058 mMixerBuffer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070059 mMixerBufferSize(0),
60 mMixerBufferFormat(AUDIO_FORMAT_PCM_16_BIT),
Andy Hung45d68d32014-05-23 21:13:31 -070061 mMixerBufferState(UNDEFINED),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080062 mFormat(Format_Invalid),
63 mSampleRate(0),
64 mFastTracksGen(0),
65 mTotalNativeFramesWritten(0),
Glenn Kasten22340022014-04-07 12:04:41 -070066 // timestamp
Andy Hung2ddee192015-12-18 17:34:44 -080067 mNativeFramesWrittenButNotPresented(0), // the = 0 is to silence the compiler
68 mMasterMono(false)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070069{
Glenn Kastene4a7ce22015-03-03 11:23:17 -080070 // FIXME pass sInitial as parameter to base class constructor, and make it static local
71 mPrevious = &sInitial;
72 mCurrent = &sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070073
Glenn Kastene4a7ce22015-03-03 11:23:17 -080074 mDummyDumpState = &mDummyFastMixerDumpState;
Andy Hung9a592762014-07-21 21:56:01 -070075 // TODO: Add channel mask to NBAIO_Format.
76 // We assume that the channel mask must be a valid positional channel mask.
77 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -070078
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070079 unsigned i;
Glenn Kastendc2c50b2016-04-21 08:13:14 -070080 for (i = 0; i < FastMixerState::sMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -080081 mFastTrackNames[i] = -1;
82 mGenerations[i] = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070083 }
Glenn Kasten214b4062015-03-02 14:15:47 -080084#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080085 mOldLoad.tv_sec = 0;
86 mOldLoad.tv_nsec = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070087#endif
Glenn Kasten22340022014-04-07 12:04:41 -070088}
89
90FastMixer::~FastMixer()
91{
92}
93
94FastMixerStateQueue* FastMixer::sq()
95{
96 return &mSQ;
97}
98
99const FastThreadState *FastMixer::poll()
100{
101 return mSQ.poll();
102}
103
104void FastMixer::setLog(NBLog::Writer *logWriter)
105{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800106 if (mMixer != NULL) {
107 mMixer->setLog(logWriter);
Glenn Kasten22340022014-04-07 12:04:41 -0700108 }
109}
110
111void FastMixer::onIdle()
112{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800113 mPreIdle = *(const FastMixerState *)mCurrent;
114 mCurrent = &mPreIdle;
Glenn Kasten22340022014-04-07 12:04:41 -0700115}
116
117void FastMixer::onExit()
118{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800119 delete mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -0700120 free(mMixerBuffer);
121 free(mSinkBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700122}
123
124bool FastMixer::isSubClassCommand(FastThreadState::Command command)
125{
126 switch ((FastMixerState::Command) command) {
127 case FastMixerState::MIX:
128 case FastMixerState::WRITE:
129 case FastMixerState::MIX_WRITE:
130 return true;
131 default:
132 return false;
133 }
134}
135
136void FastMixer::onStateChange()
137{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800138 const FastMixerState * const current = (const FastMixerState *) mCurrent;
139 const FastMixerState * const previous = (const FastMixerState *) mPrevious;
140 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
Glenn Kasten22340022014-04-07 12:04:41 -0700141 const size_t frameCount = current->mFrameCount;
142
Andy Hung818e7a32016-02-16 18:08:07 -0800143 // update boottime offset, in case it has changed
144 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
145 mBoottimeOffset.load();
146
Glenn Kasten22340022014-04-07 12:04:41 -0700147 // handle state change here, but since we want to diff the state,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800148 // we're prepared for previous == &sInitial the first time through
Glenn Kasten22340022014-04-07 12:04:41 -0700149 unsigned previousTrackMask;
150
151 // check for change in output HAL configuration
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800152 NBAIO_Format previousFormat = mFormat;
153 if (current->mOutputSinkGen != mOutputSinkGen) {
154 mOutputSink = current->mOutputSink;
155 mOutputSinkGen = current->mOutputSinkGen;
156 if (mOutputSink == NULL) {
157 mFormat = Format_Invalid;
158 mSampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700159 mSinkChannelCount = 0;
160 mSinkChannelMask = AUDIO_CHANNEL_NONE;
Glenn Kasten22340022014-04-07 12:04:41 -0700161 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800162 mFormat = mOutputSink->format();
163 mSampleRate = Format_sampleRate(mFormat);
164 mSinkChannelCount = Format_channelCount(mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700165 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
166
167 // TODO: Add channel mask to NBAIO_Format
168 // We assume that the channel mask must be a valid positional channel mask.
169 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700170 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800171 dumpState->mSampleRate = mSampleRate;
Glenn Kasten22340022014-04-07 12:04:41 -0700172 }
173
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800174 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
Glenn Kasten22340022014-04-07 12:04:41 -0700175 // FIXME to avoid priority inversion, don't delete here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800176 delete mMixer;
177 mMixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700178 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700179 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700180 free(mSinkBuffer);
181 mSinkBuffer = NULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800182 if (frameCount > 0 && mSampleRate > 0) {
Andy Hung60c545d2015-06-19 17:34:53 -0700183 // The mixer produces either 16 bit PCM or float output, select
184 // float output if the HAL supports higher than 16 bit precision.
185 mMixerBufferFormat = mFormat.mFormat == AUDIO_FORMAT_PCM_16_BIT ?
186 AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_FLOAT;
Glenn Kasten22340022014-04-07 12:04:41 -0700187 // FIXME new may block for unbounded time at internal mutex of the heap
188 // implementation; it would be better to have normal mixer allocate for us
189 // to avoid blocking here and to prevent possible priority inversion
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700190 mMixer = new AudioMixer(frameCount, mSampleRate, FastMixerState::sMaxFastTracks);
Andy Hung9a592762014-07-21 21:56:01 -0700191 const size_t mixerFrameSize = mSinkChannelCount
192 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700193 mMixerBufferSize = mixerFrameSize * frameCount;
194 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700195 const size_t sinkFrameSize = mSinkChannelCount
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800196 * audio_bytes_per_sample(mFormat.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700197 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
198 mSinkBufferSize = sinkFrameSize * frameCount;
199 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
200 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800201 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
202 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
203 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
204 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
205 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
206 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
Glenn Kasten22340022014-04-07 12:04:41 -0700207 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800208 mPeriodNs = 0;
209 mUnderrunNs = 0;
210 mOverrunNs = 0;
211 mForceNs = 0;
212 mWarmupNsMin = 0;
213 mWarmupNsMax = LONG_MAX;
Glenn Kasten22340022014-04-07 12:04:41 -0700214 }
Andy Hung45d68d32014-05-23 21:13:31 -0700215 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700216#if !LOG_NDEBUG
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700217 for (unsigned i = 0; i < FastMixerState::sMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800218 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700219 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700220#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700221 // we need to reconfigure all active tracks
222 previousTrackMask = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800223 mFastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten22340022014-04-07 12:04:41 -0700224 dumpState->mFrameCount = frameCount;
225 } else {
226 previousTrackMask = previous->mTrackMask;
227 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700228
Glenn Kasten22340022014-04-07 12:04:41 -0700229 // check for change in active track set
230 const unsigned currentTrackMask = current->mTrackMask;
231 dumpState->mTrackMask = currentTrackMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800232 if (current->mFastTracksGen != mFastTracksGen) {
Andy Hung45d68d32014-05-23 21:13:31 -0700233 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700234 int name;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700235
Glenn Kasten22340022014-04-07 12:04:41 -0700236 // process removed tracks first to avoid running out of track names
237 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
238 while (removedTracks != 0) {
239 int i = __builtin_ctz(removedTracks);
240 removedTracks &= ~(1 << i);
241 const FastTrack* fastTrack = &current->mFastTracks[i];
242 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800243 if (mMixer != NULL) {
244 name = mFastTrackNames[i];
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700245 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800246 mMixer->deleteTrackName(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700247 }
248#if !LOG_NDEBUG
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800249 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700250#endif
251 // don't reset track dump state, since other side is ignoring it
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800252 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700253 }
254
255 // now process added tracks
256 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
257 while (addedTracks != 0) {
258 int i = __builtin_ctz(addedTracks);
259 addedTracks &= ~(1 << i);
260 const FastTrack* fastTrack = &current->mFastTracks[i];
261 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800262 ALOG_ASSERT(bufferProvider != NULL && mFastTrackNames[i] == -1);
263 if (mMixer != NULL) {
264 name = mMixer->getTrackName(fastTrack->mChannelMask,
Andy Hunge8a1ced2014-05-09 15:02:21 -0700265 fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Glenn Kasten22340022014-04-07 12:04:41 -0700266 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800267 mFastTrackNames[i] = name;
268 mMixer->setBufferProvider(name, bufferProvider);
269 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
Andy Hung9a592762014-07-21 21:56:01 -0700270 (void *)mMixerBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700271 // newly allocated track names default to full scale volume
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800272 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700273 name,
274 AudioMixer::TRACK,
275 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800276 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700277 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800278 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700279 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800280 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700281 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800282 mMixer->enable(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700283 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800284 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700285 }
286
287 // finally process (potentially) modified tracks; these use the same slot
288 // but may have a different buffer provider or volume provider
289 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
290 while (modifiedTracks != 0) {
291 int i = __builtin_ctz(modifiedTracks);
292 modifiedTracks &= ~(1 << i);
293 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800294 if (fastTrack->mGeneration != mGenerations[i]) {
Glenn Kasten22340022014-04-07 12:04:41 -0700295 // this track was actually modified
296 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
297 ALOG_ASSERT(bufferProvider != NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800298 if (mMixer != NULL) {
299 name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700300 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800301 mMixer->setBufferProvider(name, bufferProvider);
Glenn Kasten22340022014-04-07 12:04:41 -0700302 if (fastTrack->mVolumeProvider == NULL) {
Andy Hung6be49402014-05-30 10:42:03 -0700303 float f = AudioMixer::UNITY_GAIN_FLOAT;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800304 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
305 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
Glenn Kasten288ed212012-04-25 17:52:27 -0700306 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800307 mMixer->setParameter(name, AudioMixer::RESAMPLE,
Glenn Kasten22340022014-04-07 12:04:41 -0700308 AudioMixer::REMOVE, NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800309 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700310 name,
311 AudioMixer::TRACK,
312 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800313 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700314 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800315 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700316 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800317 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700318 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700319 // already enabled
320 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800321 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700322 }
323 }
324
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800325 mFastTracksGen = current->mFastTracksGen;
Glenn Kasten22340022014-04-07 12:04:41 -0700326
327 dumpState->mNumTracks = popcount(currentTrackMask);
328 }
329}
330
331void FastMixer::onWork()
332{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800333 const FastMixerState * const current = (const FastMixerState *) mCurrent;
334 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
335 const FastMixerState::Command command = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700336 const size_t frameCount = current->mFrameCount;
337
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800338 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700339 ALOG_ASSERT(mMixerBuffer != NULL);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530340
341 // AudioMixer::mState.enabledTracks is undefined if mState.hook == process__validate,
342 // so we keep a side copy of enabledTracks
343 bool anyEnabledTracks = false;
344
Glenn Kasten22340022014-04-07 12:04:41 -0700345 // for each track, update volume and check for underrun
346 unsigned currentTrackMask = current->mTrackMask;
347 while (currentTrackMask != 0) {
348 int i = __builtin_ctz(currentTrackMask);
349 currentTrackMask &= ~(1 << i);
350 const FastTrack* fastTrack = &current->mFastTracks[i];
351
Andy Hung818e7a32016-02-16 18:08:07 -0800352 const int64_t trackFramesWrittenButNotPresented =
353 mNativeFramesWrittenButNotPresented;
354 const int64_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
355 ExtendedTimestamp perTrackTimestamp(mTimestamp);
356
357 // Can't provide an ExtendedTimestamp before first frame presented.
358 // Also, timestamp may not go to very last frame on stop().
359 if (trackFramesWritten >= trackFramesWrittenButNotPresented &&
360 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
361 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
362 trackFramesWritten - trackFramesWrittenButNotPresented;
363 } else {
364 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
365 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700366 }
Andy Hung818e7a32016-02-16 18:08:07 -0800367 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = trackFramesWritten;
368 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
Glenn Kasten22340022014-04-07 12:04:41 -0700369
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800370 int name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700371 ALOG_ASSERT(name >= 0);
372 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700373 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700374 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
375 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
376
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800377 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
378 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700379 }
380 // FIXME The current implementation of framesReady() for fast tracks
381 // takes a tryLock, which can block
382 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
383 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
384 size_t framesReady = fastTrack->mBufferProvider->framesReady();
385 if (ATRACE_ENABLED()) {
386 // I wish we had formatted trace names
387 char traceName[16];
388 strcpy(traceName, "fRdy");
389 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
390 traceName[5] = '\0';
391 ATRACE_INT(traceName, framesReady);
392 }
393 FastTrackDump *ftDump = &dumpState->mTracks[i];
394 FastTrackUnderruns underruns = ftDump->mUnderruns;
395 if (framesReady < frameCount) {
396 if (framesReady == 0) {
397 underruns.mBitFields.mEmpty++;
398 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800399 mMixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700400 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700401 // allow mixing partial buffer
402 underruns.mBitFields.mPartial++;
403 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800404 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530405 anyEnabledTracks = true;
Glenn Kasten288ed212012-04-25 17:52:27 -0700406 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700407 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700408 underruns.mBitFields.mFull++;
409 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800410 mMixer->enable(name);
Mohan Kumar947ffa32014-12-12 15:16:46 +0530411 anyEnabledTracks = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700412 }
Glenn Kasten22340022014-04-07 12:04:41 -0700413 ftDump->mUnderruns = underruns;
414 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700415 }
416
Mohan Kumar947ffa32014-12-12 15:16:46 +0530417 if (anyEnabledTracks) {
418 // process() is CPU-bound
419 mMixer->process();
420 mMixerBufferState = MIXED;
421 } else if (mMixerBufferState != ZEROED) {
422 mMixerBufferState = UNDEFINED;
423 }
424
Andy Hung45d68d32014-05-23 21:13:31 -0700425 } else if (mMixerBufferState == MIXED) {
426 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700427 }
428 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800429 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
Andy Hung45d68d32014-05-23 21:13:31 -0700430 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700431 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700432 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700433 }
Andy Hung2ddee192015-12-18 17:34:44 -0800434
435 if (mMasterMono.load()) { // memory_order_seq_cst
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700436 mono_blend(mMixerBuffer, mMixerBufferFormat, Format_channelCount(mFormat), frameCount,
437 true /*limit*/);
Andy Hung2ddee192015-12-18 17:34:44 -0800438 }
Glenn Kastenf59497b2015-01-26 16:35:47 -0800439 // prepare the buffer used to write to sink
Andy Hung1258c1a2014-05-23 21:22:17 -0700440 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800441 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
442 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
443 frameCount * Format_channelCount(mFormat));
Andy Hung1258c1a2014-05-23 21:22:17 -0700444 }
Glenn Kasten22340022014-04-07 12:04:41 -0700445 // if non-NULL, then duplicate write() to this non-blocking sink
446 NBAIO_Sink* teeSink;
447 if ((teeSink = current->mTeeSink) != NULL) {
Glenn Kasten329f6512014-08-28 16:23:16 -0700448 (void) teeSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700449 }
450 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
451 // but this code should be modified to handle both non-blocking and blocking sinks
452 dumpState->mWriteSequence++;
453 ATRACE_BEGIN("write");
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800454 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700455 ATRACE_END();
456 dumpState->mWriteSequence++;
457 if (framesWritten >= 0) {
458 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800459 mTotalNativeFramesWritten += framesWritten;
460 dumpState->mFramesWritten = mTotalNativeFramesWritten;
Glenn Kasten22340022014-04-07 12:04:41 -0700461 //if ((size_t) framesWritten == frameCount) {
462 // didFullWrite = true;
463 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700464 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700465 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700466 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800467 mAttemptedWrite = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700468 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700469
Andy Hung818e7a32016-02-16 18:08:07 -0800470 ExtendedTimestamp timestamp; // local
471 status_t status = mOutputSink->getTimestamp(timestamp);
472 if (status == NO_ERROR) {
473 const int64_t totalNativeFramesPresented =
474 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800475 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
476 mNativeFramesWrittenButNotPresented =
477 mTotalNativeFramesWritten - totalNativeFramesPresented;
Andy Hung818e7a32016-02-16 18:08:07 -0800478 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
479 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
480 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
481 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
Glenn Kasten22340022014-04-07 12:04:41 -0700482 } else {
483 // HAL reported that more frames were presented than were written
Andy Hung818e7a32016-02-16 18:08:07 -0800484 mNativeFramesWrittenButNotPresented = 0;
Andy Hung818e7a32016-02-16 18:08:07 -0800485 status = INVALID_OPERATION;
Glenn Kasten22340022014-04-07 12:04:41 -0700486 }
487 }
Andy Hung818e7a32016-02-16 18:08:07 -0800488 if (status == NO_ERROR) {
489 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
490 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
491 } else {
492 // fetch server time if we can't get timestamp
493 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
494 systemTime(SYSTEM_TIME_MONOTONIC);
Andy Hung07eee802016-06-21 16:47:16 -0700495 // clear out kernel cached position as this may get rapidly stale
496 // if we never get a new valid timestamp
497 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
498 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
Andy Hung818e7a32016-02-16 18:08:07 -0800499 }
Glenn Kasten22340022014-04-07 12:04:41 -0700500 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700501}
502
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700503} // namespace android