blob: 45c68b55df4345b9efda105da8345fe6d721321a [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 Hung1258c1a2014-05-23 21:22:17 -070040#include <audio_utils/format.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070041#include "AudioMixer.h"
42#include "FastMixer.h"
43
Glenn Kasten7fc97ba2013-07-16 17:18:58 -070044#define FCC_2 2 // fixed channel count assumption
45
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
50FastMixer::FastMixer() : FastThread(),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080051 mSlopNs(0),
52 // mFastTrackNames
53 // mGenerations
54 mOutputSink(NULL),
55 mOutputSinkGen(0),
56 mMixer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070057 mSinkBuffer(NULL),
58 mSinkBufferSize(0),
Andy Hung9a592762014-07-21 21:56:01 -070059 mSinkChannelCount(FCC_2),
Andy Hung45d68d32014-05-23 21:13:31 -070060 mMixerBuffer(NULL),
Andy Hung1258c1a2014-05-23 21:22:17 -070061 mMixerBufferSize(0),
62 mMixerBufferFormat(AUDIO_FORMAT_PCM_16_BIT),
Andy Hung45d68d32014-05-23 21:13:31 -070063 mMixerBufferState(UNDEFINED),
Glenn Kastene4a7ce22015-03-03 11:23:17 -080064 mFormat(Format_Invalid),
65 mSampleRate(0),
66 mFastTracksGen(0),
67 mTotalNativeFramesWritten(0),
Glenn Kasten22340022014-04-07 12:04:41 -070068 // timestamp
Glenn Kastene4a7ce22015-03-03 11:23:17 -080069 mNativeFramesWrittenButNotPresented(0) // the = 0 is to silence the compiler
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
144 // handle state change here, but since we want to diff the state,
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800145 // we're prepared for previous == &sInitial the first time through
Glenn Kasten22340022014-04-07 12:04:41 -0700146 unsigned previousTrackMask;
147
148 // check for change in output HAL configuration
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800149 NBAIO_Format previousFormat = mFormat;
150 if (current->mOutputSinkGen != mOutputSinkGen) {
151 mOutputSink = current->mOutputSink;
152 mOutputSinkGen = current->mOutputSinkGen;
153 if (mOutputSink == NULL) {
154 mFormat = Format_Invalid;
155 mSampleRate = 0;
Andy Hung9a592762014-07-21 21:56:01 -0700156 mSinkChannelCount = 0;
157 mSinkChannelMask = AUDIO_CHANNEL_NONE;
Glenn Kasten22340022014-04-07 12:04:41 -0700158 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800159 mFormat = mOutputSink->format();
160 mSampleRate = Format_sampleRate(mFormat);
161 mSinkChannelCount = Format_channelCount(mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700162 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
163
164 // TODO: Add channel mask to NBAIO_Format
165 // We assume that the channel mask must be a valid positional channel mask.
166 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700167 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800168 dumpState->mSampleRate = mSampleRate;
Glenn Kasten22340022014-04-07 12:04:41 -0700169 }
170
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800171 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
Glenn Kasten22340022014-04-07 12:04:41 -0700172 // FIXME to avoid priority inversion, don't delete here
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800173 delete mMixer;
174 mMixer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700175 free(mMixerBuffer);
Andy Hung45d68d32014-05-23 21:13:31 -0700176 mMixerBuffer = NULL;
Andy Hung1258c1a2014-05-23 21:22:17 -0700177 free(mSinkBuffer);
178 mSinkBuffer = NULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800179 if (frameCount > 0 && mSampleRate > 0) {
Andy Hung60c545d2015-06-19 17:34:53 -0700180 // The mixer produces either 16 bit PCM or float output, select
181 // float output if the HAL supports higher than 16 bit precision.
182 mMixerBufferFormat = mFormat.mFormat == AUDIO_FORMAT_PCM_16_BIT ?
183 AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_FLOAT;
Glenn Kasten22340022014-04-07 12:04:41 -0700184 // FIXME new may block for unbounded time at internal mutex of the heap
185 // implementation; it would be better to have normal mixer allocate for us
186 // to avoid blocking here and to prevent possible priority inversion
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800187 mMixer = new AudioMixer(frameCount, mSampleRate, FastMixerState::kMaxFastTracks);
Andy Hung9a592762014-07-21 21:56:01 -0700188 const size_t mixerFrameSize = mSinkChannelCount
189 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700190 mMixerBufferSize = mixerFrameSize * frameCount;
191 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700192 const size_t sinkFrameSize = mSinkChannelCount
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800193 * audio_bytes_per_sample(mFormat.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700194 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
195 mSinkBufferSize = sinkFrameSize * frameCount;
196 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
197 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800198 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
199 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
200 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
201 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
202 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
203 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
Glenn Kasten22340022014-04-07 12:04:41 -0700204 } else {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800205 mPeriodNs = 0;
206 mUnderrunNs = 0;
207 mOverrunNs = 0;
208 mForceNs = 0;
209 mWarmupNsMin = 0;
210 mWarmupNsMax = LONG_MAX;
Glenn Kasten22340022014-04-07 12:04:41 -0700211 }
Andy Hung45d68d32014-05-23 21:13:31 -0700212 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700213#if !LOG_NDEBUG
214 for (unsigned i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800215 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700216 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700217#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700218 // we need to reconfigure all active tracks
219 previousTrackMask = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800220 mFastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten22340022014-04-07 12:04:41 -0700221 dumpState->mFrameCount = frameCount;
222 } else {
223 previousTrackMask = previous->mTrackMask;
224 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700225
Glenn Kasten22340022014-04-07 12:04:41 -0700226 // check for change in active track set
227 const unsigned currentTrackMask = current->mTrackMask;
228 dumpState->mTrackMask = currentTrackMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800229 if (current->mFastTracksGen != mFastTracksGen) {
Andy Hung45d68d32014-05-23 21:13:31 -0700230 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700231 int name;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700232
Glenn Kasten22340022014-04-07 12:04:41 -0700233 // process removed tracks first to avoid running out of track names
234 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
235 while (removedTracks != 0) {
236 int i = __builtin_ctz(removedTracks);
237 removedTracks &= ~(1 << i);
238 const FastTrack* fastTrack = &current->mFastTracks[i];
239 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800240 if (mMixer != NULL) {
241 name = mFastTrackNames[i];
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700242 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800243 mMixer->deleteTrackName(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700244 }
245#if !LOG_NDEBUG
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800246 mFastTrackNames[i] = -1;
Glenn Kasten22340022014-04-07 12:04:41 -0700247#endif
248 // don't reset track dump state, since other side is ignoring it
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800249 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700250 }
251
252 // now process added tracks
253 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
254 while (addedTracks != 0) {
255 int i = __builtin_ctz(addedTracks);
256 addedTracks &= ~(1 << i);
257 const FastTrack* fastTrack = &current->mFastTracks[i];
258 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800259 ALOG_ASSERT(bufferProvider != NULL && mFastTrackNames[i] == -1);
260 if (mMixer != NULL) {
261 name = mMixer->getTrackName(fastTrack->mChannelMask,
Andy Hunge8a1ced2014-05-09 15:02:21 -0700262 fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Glenn Kasten22340022014-04-07 12:04:41 -0700263 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800264 mFastTrackNames[i] = name;
265 mMixer->setBufferProvider(name, bufferProvider);
266 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
Andy Hung9a592762014-07-21 21:56:01 -0700267 (void *)mMixerBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700268 // newly allocated track names default to full scale volume
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800269 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700270 name,
271 AudioMixer::TRACK,
272 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800273 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700274 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800275 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700276 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800277 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700278 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800279 mMixer->enable(name);
Glenn Kasten22340022014-04-07 12:04:41 -0700280 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800281 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700282 }
283
284 // finally process (potentially) modified tracks; these use the same slot
285 // but may have a different buffer provider or volume provider
286 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
287 while (modifiedTracks != 0) {
288 int i = __builtin_ctz(modifiedTracks);
289 modifiedTracks &= ~(1 << i);
290 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800291 if (fastTrack->mGeneration != mGenerations[i]) {
Glenn Kasten22340022014-04-07 12:04:41 -0700292 // this track was actually modified
293 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
294 ALOG_ASSERT(bufferProvider != NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800295 if (mMixer != NULL) {
296 name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700297 ALOG_ASSERT(name >= 0);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800298 mMixer->setBufferProvider(name, bufferProvider);
Glenn Kasten22340022014-04-07 12:04:41 -0700299 if (fastTrack->mVolumeProvider == NULL) {
Andy Hung6be49402014-05-30 10:42:03 -0700300 float f = AudioMixer::UNITY_GAIN_FLOAT;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800301 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
302 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
Glenn Kasten288ed212012-04-25 17:52:27 -0700303 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800304 mMixer->setParameter(name, AudioMixer::RESAMPLE,
Glenn Kasten22340022014-04-07 12:04:41 -0700305 AudioMixer::REMOVE, NULL);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800306 mMixer->setParameter(
Andy Hung1258c1a2014-05-23 21:22:17 -0700307 name,
308 AudioMixer::TRACK,
309 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800310 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
Andy Hungef7c7fb2014-05-12 16:51:41 -0700311 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800312 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700313 (void *)(uintptr_t)fastTrack->mChannelMask);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800314 mMixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700315 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700316 // already enabled
317 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800318 mGenerations[i] = fastTrack->mGeneration;
Glenn Kasten22340022014-04-07 12:04:41 -0700319 }
320 }
321
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800322 mFastTracksGen = current->mFastTracksGen;
Glenn Kasten22340022014-04-07 12:04:41 -0700323
324 dumpState->mNumTracks = popcount(currentTrackMask);
325 }
326}
327
328void FastMixer::onWork()
329{
Glenn Kasten4dd03b52015-03-03 11:32:04 -0800330 const FastMixerState * const current = (const FastMixerState *) mCurrent;
331 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
332 const FastMixerState::Command command = mCommand;
Glenn Kasten22340022014-04-07 12:04:41 -0700333 const size_t frameCount = current->mFrameCount;
334
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800335 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700336 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700337 // for each track, update volume and check for underrun
338 unsigned currentTrackMask = current->mTrackMask;
339 while (currentTrackMask != 0) {
340 int i = __builtin_ctz(currentTrackMask);
341 currentTrackMask &= ~(1 << i);
342 const FastTrack* fastTrack = &current->mFastTracks[i];
343
344 // Refresh the per-track timestamp
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800345 if (mTimestampStatus == NO_ERROR) {
Glenn Kasten22340022014-04-07 12:04:41 -0700346 uint32_t trackFramesWrittenButNotPresented =
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800347 mNativeFramesWrittenButNotPresented;
Glenn Kasten22340022014-04-07 12:04:41 -0700348 uint32_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
349 // Can't provide an AudioTimestamp before first frame presented,
350 // or during the brief 32-bit wraparound window
351 if (trackFramesWritten >= trackFramesWrittenButNotPresented) {
352 AudioTimestamp perTrackTimestamp;
353 perTrackTimestamp.mPosition =
354 trackFramesWritten - trackFramesWrittenButNotPresented;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800355 perTrackTimestamp.mTime = mTimestamp.mTime;
Glenn Kasten22340022014-04-07 12:04:41 -0700356 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
357 }
358 }
359
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800360 int name = mFastTrackNames[i];
Glenn Kasten22340022014-04-07 12:04:41 -0700361 ALOG_ASSERT(name >= 0);
362 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700363 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700364 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
365 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
366
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800367 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
368 mMixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700369 }
370 // FIXME The current implementation of framesReady() for fast tracks
371 // takes a tryLock, which can block
372 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
373 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
374 size_t framesReady = fastTrack->mBufferProvider->framesReady();
375 if (ATRACE_ENABLED()) {
376 // I wish we had formatted trace names
377 char traceName[16];
378 strcpy(traceName, "fRdy");
379 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
380 traceName[5] = '\0';
381 ATRACE_INT(traceName, framesReady);
382 }
383 FastTrackDump *ftDump = &dumpState->mTracks[i];
384 FastTrackUnderruns underruns = ftDump->mUnderruns;
385 if (framesReady < frameCount) {
386 if (framesReady == 0) {
387 underruns.mBitFields.mEmpty++;
388 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800389 mMixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700390 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700391 // allow mixing partial buffer
392 underruns.mBitFields.mPartial++;
393 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800394 mMixer->enable(name);
Glenn Kasten288ed212012-04-25 17:52:27 -0700395 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700396 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700397 underruns.mBitFields.mFull++;
398 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800399 mMixer->enable(name);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700400 }
Glenn Kasten22340022014-04-07 12:04:41 -0700401 ftDump->mUnderruns = underruns;
402 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700403 }
404
Glenn Kasten22340022014-04-07 12:04:41 -0700405 int64_t pts;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800406 if (mOutputSink == NULL || (OK != mOutputSink->getNextWriteTimestamp(&pts))) {
Glenn Kasten22340022014-04-07 12:04:41 -0700407 pts = AudioBufferProvider::kInvalidPTS;
408 }
409
410 // process() is CPU-bound
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800411 mMixer->process(pts);
Andy Hung45d68d32014-05-23 21:13:31 -0700412 mMixerBufferState = MIXED;
413 } else if (mMixerBufferState == MIXED) {
414 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700415 }
416 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800417 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
Andy Hung45d68d32014-05-23 21:13:31 -0700418 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700419 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700420 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700421 }
Glenn Kastenf59497b2015-01-26 16:35:47 -0800422 // prepare the buffer used to write to sink
Andy Hung1258c1a2014-05-23 21:22:17 -0700423 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800424 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
425 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
426 frameCount * Format_channelCount(mFormat));
Andy Hung1258c1a2014-05-23 21:22:17 -0700427 }
Glenn Kasten22340022014-04-07 12:04:41 -0700428 // if non-NULL, then duplicate write() to this non-blocking sink
429 NBAIO_Sink* teeSink;
430 if ((teeSink = current->mTeeSink) != NULL) {
Glenn Kasten329f6512014-08-28 16:23:16 -0700431 (void) teeSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700432 }
433 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
434 // but this code should be modified to handle both non-blocking and blocking sinks
435 dumpState->mWriteSequence++;
436 ATRACE_BEGIN("write");
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800437 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700438 ATRACE_END();
439 dumpState->mWriteSequence++;
440 if (framesWritten >= 0) {
441 ALOG_ASSERT((size_t) framesWritten <= frameCount);
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800442 mTotalNativeFramesWritten += framesWritten;
443 dumpState->mFramesWritten = mTotalNativeFramesWritten;
Glenn Kasten22340022014-04-07 12:04:41 -0700444 //if ((size_t) framesWritten == frameCount) {
445 // didFullWrite = true;
446 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700447 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700448 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700449 }
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800450 mAttemptedWrite = true;
Glenn Kasten22340022014-04-07 12:04:41 -0700451 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700452
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800453 mTimestampStatus = mOutputSink->getTimestamp(mTimestamp);
454 if (mTimestampStatus == NO_ERROR) {
455 uint32_t totalNativeFramesPresented = mTimestamp.mPosition;
456 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
457 mNativeFramesWrittenButNotPresented =
458 mTotalNativeFramesWritten - totalNativeFramesPresented;
Glenn Kasten22340022014-04-07 12:04:41 -0700459 } else {
460 // HAL reported that more frames were presented than were written
Glenn Kastene4a7ce22015-03-03 11:23:17 -0800461 mTimestampStatus = INVALID_OPERATION;
Glenn Kasten22340022014-04-07 12:04:41 -0700462 }
463 }
464 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700465}
466
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700467} // namespace android