blob: 5991eab6404827c2cfa3aa631a98b391a4789314 [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 Kasten7fc97ba2013-07-16 17:18:58 -070045#define FCC_2 2 // fixed channel count assumption
46
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
51FastMixer::FastMixer() : FastThread(),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080052 mSlopNs(0),
53 // 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
71 mMasterMono(false)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070072{
Glenn Kastene4a7ce22015-03-03 11:23:17 -080073 // FIXME pass sInitial as parameter to base class constructor, and make it static local
74 mPrevious = &sInitial;
75 mCurrent = &sInitial;
Glenn Kasten22340022014-04-07 12:04:41 -070076
Glenn Kastene4a7ce22015-03-03 11:23:17 -080077 mDummyDumpState = &mDummyFastMixerDumpState;
Andy Hung9a592762014-07-21 21:56:01 -070078 // TODO: Add channel mask to NBAIO_Format.
79 // We assume that the channel mask must be a valid positional channel mask.
80 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -070081
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070082 unsigned i;
83 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -080084 mFastTrackNames[i] = -1;
85 mGenerations[i] = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070086 }
Glenn Kasten214b4062015-03-02 14:15:47 -080087#ifdef FAST_THREAD_STATISTICS
Glenn Kastene4a7ce22015-03-03 11:23:17 -080088 mOldLoad.tv_sec = 0;
89 mOldLoad.tv_nsec = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070090#endif
Glenn Kasten22340022014-04-07 12:04:41 -070091}
92
93FastMixer::~FastMixer()
94{
95}
96
97FastMixerStateQueue* FastMixer::sq()
98{
99 return &mSQ;
100}
101
102const FastThreadState *FastMixer::poll()
103{
104 return mSQ.poll();
105}
106
107void FastMixer::setLog(NBLog::Writer *logWriter)
108{
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800109 if (mMixer != NULL) {
110 mMixer->setLog(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 Kasten4dd03b52015-03-03 11:32:04 -0800141 const FastMixerState * const current = (const FastMixerState *) mCurrent;
142 const FastMixerState * const previous = (const FastMixerState *) mPrevious;
143 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
Glenn Kasten22340022014-04-07 12:04:41 -0700144 const size_t frameCount = current->mFrameCount;
145
146 // handle state change here, but since we want to diff the state,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800147 // we're prepared for previous == &sInitial the first time through
Glenn Kasten22340022014-04-07 12:04:41 -0700148 unsigned previousTrackMask;
149
150 // check for change in output HAL configuration
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800151 NBAIO_Format previousFormat = mFormat;
152 if (current->mOutputSinkGen != mOutputSinkGen) {
153 mOutputSink = current->mOutputSink;
154 mOutputSinkGen = current->mOutputSinkGen;
155 if (mOutputSink == NULL) {
156 mFormat = Format_Invalid;
157 mSampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700158 mSinkChannelCount = 0;
159 mSinkChannelMask = AUDIO_CHANNEL_NONE;
Glenn Kasten22340022014-04-07 12:04:41 -0700160 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800161 mFormat = mOutputSink->format();
162 mSampleRate = Format_sampleRate(mFormat);
163 mSinkChannelCount = Format_channelCount(mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700164 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
165
166 // TODO: Add channel mask to NBAIO_Format
167 // We assume that the channel mask must be a valid positional channel mask.
168 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700169 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800170 dumpState->mSampleRate = mSampleRate;
Glenn Kasten22340022014-04-07 12:04:41 -0700171 }
172
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800173 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
Glenn Kasten22340022014-04-07 12:04:41 -0700174 // FIXME to avoid priority inversion, don't delete here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800175 delete mMixer;
176 mMixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700177 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700178 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700179 free(mSinkBuffer);
180 mSinkBuffer = NULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800181 if (frameCount > 0 && mSampleRate > 0) {
Andy Hung60c545d2015-06-19 17:34:53 -0700182 // The mixer produces either 16 bit PCM or float output, select
183 // float output if the HAL supports higher than 16 bit precision.
184 mMixerBufferFormat = mFormat.mFormat == AUDIO_FORMAT_PCM_16_BIT ?
185 AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_FLOAT;
Glenn Kasten22340022014-04-07 12:04:41 -0700186 // FIXME new may block for unbounded time at internal mutex of the heap
187 // implementation; it would be better to have normal mixer allocate for us
188 // to avoid blocking here and to prevent possible priority inversion
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800189 mMixer = new AudioMixer(frameCount, mSampleRate, FastMixerState::kMaxFastTracks);
Andy Hung9a592762014-07-21 21:56:01 -0700190 const size_t mixerFrameSize = mSinkChannelCount
191 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700192 mMixerBufferSize = mixerFrameSize * frameCount;
193 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700194 const size_t sinkFrameSize = mSinkChannelCount
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800195 * audio_bytes_per_sample(mFormat.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700196 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
197 mSinkBufferSize = sinkFrameSize * frameCount;
198 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
199 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800200 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
201 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
202 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
203 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
204 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
205 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
Glenn Kasten22340022014-04-07 12:04:41 -0700206 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800207 mPeriodNs = 0;
208 mUnderrunNs = 0;
209 mOverrunNs = 0;
210 mForceNs = 0;
211 mWarmupNsMin = 0;
212 mWarmupNsMax = LONG_MAX;
Glenn Kasten22340022014-04-07 12:04:41 -0700213 }
Andy Hung45d68d32014-05-23 21:13:31 -0700214 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700215#if !LOG_NDEBUG
216 for (unsigned i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800217 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700218 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700219#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700220 // we need to reconfigure all active tracks
221 previousTrackMask = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800222 mFastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten22340022014-04-07 12:04:41 -0700223 dumpState->mFrameCount = frameCount;
224 } else {
225 previousTrackMask = previous->mTrackMask;
226 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700227
Glenn Kasten22340022014-04-07 12:04:41 -0700228 // check for change in active track set
229 const unsigned currentTrackMask = current->mTrackMask;
230 dumpState->mTrackMask = currentTrackMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800231 if (current->mFastTracksGen != mFastTracksGen) {
Andy Hung45d68d32014-05-23 21:13:31 -0700232 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700233 int name;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700234
Glenn Kasten22340022014-04-07 12:04:41 -0700235 // process removed tracks first to avoid running out of track names
236 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
237 while (removedTracks != 0) {
238 int i = __builtin_ctz(removedTracks);
239 removedTracks &= ~(1 << i);
240 const FastTrack* fastTrack = &current->mFastTracks[i];
241 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800242 if (mMixer != NULL) {
243 name = mFastTrackNames[i];
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700244 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800245 mMixer->deleteTrackName(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700246 }
247#if !LOG_NDEBUG
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800248 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700249#endif
250 // don't reset track dump state, since other side is ignoring it
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800251 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700252 }
253
254 // now process added tracks
255 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
256 while (addedTracks != 0) {
257 int i = __builtin_ctz(addedTracks);
258 addedTracks &= ~(1 << i);
259 const FastTrack* fastTrack = &current->mFastTracks[i];
260 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800261 ALOG_ASSERT(bufferProvider != NULL && mFastTrackNames[i] == -1);
262 if (mMixer != NULL) {
263 name = mMixer->getTrackName(fastTrack->mChannelMask,
Andy Hunge8a1ced2014-05-09 15:02:21 -0700264 fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Glenn Kasten22340022014-04-07 12:04:41 -0700265 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800266 mFastTrackNames[i] = name;
267 mMixer->setBufferProvider(name, bufferProvider);
268 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
Andy Hung9a592762014-07-21 21:56:01 -0700269 (void *)mMixerBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700270 // newly allocated track names default to full scale volume
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800271 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700272 name,
273 AudioMixer::TRACK,
274 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800275 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700276 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800277 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700278 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800279 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700280 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800281 mMixer->enable(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700282 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800283 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700284 }
285
286 // finally process (potentially) modified tracks; these use the same slot
287 // but may have a different buffer provider or volume provider
288 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
289 while (modifiedTracks != 0) {
290 int i = __builtin_ctz(modifiedTracks);
291 modifiedTracks &= ~(1 << i);
292 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800293 if (fastTrack->mGeneration != mGenerations[i]) {
Glenn Kasten22340022014-04-07 12:04:41 -0700294 // this track was actually modified
295 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
296 ALOG_ASSERT(bufferProvider != NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800297 if (mMixer != NULL) {
298 name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700299 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800300 mMixer->setBufferProvider(name, bufferProvider);
Glenn Kasten22340022014-04-07 12:04:41 -0700301 if (fastTrack->mVolumeProvider == NULL) {
Andy Hung6be49402014-05-30 10:42:03 -0700302 float f = AudioMixer::UNITY_GAIN_FLOAT;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800303 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
304 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
Glenn Kasten288ed212012-04-25 17:52:27 -0700305 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800306 mMixer->setParameter(name, AudioMixer::RESAMPLE,
Glenn Kasten22340022014-04-07 12:04:41 -0700307 AudioMixer::REMOVE, NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800308 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700309 name,
310 AudioMixer::TRACK,
311 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800312 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700313 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800314 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700315 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800316 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700317 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700318 // already enabled
319 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800320 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700321 }
322 }
323
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800324 mFastTracksGen = current->mFastTracksGen;
Glenn Kasten22340022014-04-07 12:04:41 -0700325
326 dumpState->mNumTracks = popcount(currentTrackMask);
327 }
328}
329
330void FastMixer::onWork()
331{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800332 const FastMixerState * const current = (const FastMixerState *) mCurrent;
333 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
334 const FastMixerState::Command command = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700335 const size_t frameCount = current->mFrameCount;
336
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800337 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700338 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700339 // for each track, update volume and check for underrun
340 unsigned currentTrackMask = current->mTrackMask;
341 while (currentTrackMask != 0) {
342 int i = __builtin_ctz(currentTrackMask);
343 currentTrackMask &= ~(1 << i);
344 const FastTrack* fastTrack = &current->mFastTracks[i];
345
346 // Refresh the per-track timestamp
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800347 if (mTimestampStatus == NO_ERROR) {
Glenn Kasten22340022014-04-07 12:04:41 -0700348 uint32_t trackFramesWrittenButNotPresented =
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800349 mNativeFramesWrittenButNotPresented;
Glenn Kasten22340022014-04-07 12:04:41 -0700350 uint32_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
351 // Can't provide an AudioTimestamp before first frame presented,
352 // or during the brief 32-bit wraparound window
353 if (trackFramesWritten >= trackFramesWrittenButNotPresented) {
354 AudioTimestamp perTrackTimestamp;
355 perTrackTimestamp.mPosition =
356 trackFramesWritten - trackFramesWrittenButNotPresented;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800357 perTrackTimestamp.mTime = mTimestamp.mTime;
Glenn Kasten22340022014-04-07 12:04:41 -0700358 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
359 }
360 }
361
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800362 int name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700363 ALOG_ASSERT(name >= 0);
364 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700365 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700366 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
367 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
368
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800369 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
370 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700371 }
372 // FIXME The current implementation of framesReady() for fast tracks
373 // takes a tryLock, which can block
374 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
375 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
376 size_t framesReady = fastTrack->mBufferProvider->framesReady();
377 if (ATRACE_ENABLED()) {
378 // I wish we had formatted trace names
379 char traceName[16];
380 strcpy(traceName, "fRdy");
381 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
382 traceName[5] = '\0';
383 ATRACE_INT(traceName, framesReady);
384 }
385 FastTrackDump *ftDump = &dumpState->mTracks[i];
386 FastTrackUnderruns underruns = ftDump->mUnderruns;
387 if (framesReady < frameCount) {
388 if (framesReady == 0) {
389 underruns.mBitFields.mEmpty++;
390 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800391 mMixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700392 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700393 // allow mixing partial buffer
394 underruns.mBitFields.mPartial++;
395 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800396 mMixer->enable(name);
Glenn Kasten288ed212012-04-25 17:52:27 -0700397 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700398 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700399 underruns.mBitFields.mFull++;
400 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800401 mMixer->enable(name);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700402 }
Glenn Kasten22340022014-04-07 12:04:41 -0700403 ftDump->mUnderruns = underruns;
404 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700405 }
406
Glenn Kasten22340022014-04-07 12:04:41 -0700407 int64_t pts;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800408 if (mOutputSink == NULL || (OK != mOutputSink->getNextWriteTimestamp(&pts))) {
Glenn Kasten22340022014-04-07 12:04:41 -0700409 pts = AudioBufferProvider::kInvalidPTS;
410 }
411
412 // process() is CPU-bound
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800413 mMixer->process(pts);
Andy Hung45d68d32014-05-23 21:13:31 -0700414 mMixerBufferState = MIXED;
415 } else if (mMixerBufferState == MIXED) {
416 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700417 }
418 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800419 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
Andy Hung45d68d32014-05-23 21:13:31 -0700420 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700421 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700422 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700423 }
Andy Hung2ddee192015-12-18 17:34:44 -0800424
425 if (mMasterMono.load()) { // memory_order_seq_cst
426 mono_blend(mMixerBuffer, mMixerBufferFormat, Format_channelCount(mFormat), frameCount);
427 }
Glenn Kastenf59497b2015-01-26 16:35:47 -0800428 // prepare the buffer used to write to sink
Andy Hung1258c1a2014-05-23 21:22:17 -0700429 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800430 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
431 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
432 frameCount * Format_channelCount(mFormat));
Andy Hung1258c1a2014-05-23 21:22:17 -0700433 }
Glenn Kasten22340022014-04-07 12:04:41 -0700434 // if non-NULL, then duplicate write() to this non-blocking sink
435 NBAIO_Sink* teeSink;
436 if ((teeSink = current->mTeeSink) != NULL) {
Glenn Kasten329f6512014-08-28 16:23:16 -0700437 (void) teeSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700438 }
439 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
440 // but this code should be modified to handle both non-blocking and blocking sinks
441 dumpState->mWriteSequence++;
442 ATRACE_BEGIN("write");
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800443 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700444 ATRACE_END();
445 dumpState->mWriteSequence++;
446 if (framesWritten >= 0) {
447 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800448 mTotalNativeFramesWritten += framesWritten;
449 dumpState->mFramesWritten = mTotalNativeFramesWritten;
Glenn Kasten22340022014-04-07 12:04:41 -0700450 //if ((size_t) framesWritten == frameCount) {
451 // didFullWrite = true;
452 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700453 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700454 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700455 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800456 mAttemptedWrite = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700457 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700458
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800459 mTimestampStatus = mOutputSink->getTimestamp(mTimestamp);
460 if (mTimestampStatus == NO_ERROR) {
461 uint32_t totalNativeFramesPresented = mTimestamp.mPosition;
462 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
463 mNativeFramesWrittenButNotPresented =
464 mTotalNativeFramesWritten - totalNativeFramesPresented;
Glenn Kasten22340022014-04-07 12:04:41 -0700465 } else {
466 // HAL reported that more frames were presented than were written
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800467 mTimestampStatus = INVALID_OPERATION;
Glenn Kasten22340022014-04-07 12:04:41 -0700468 }
469 }
470 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700471}
472
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700473} // namespace android