blob: bf264be028fbe8350d5f98f92d91f57fdf719234 [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
17#define LOG_TAG "FastMixer"
18//#define LOG_NDEBUG 0
19
20#include <sys/atomics.h>
21#include <time.h>
22#include <utils/Log.h>
23#include <system/audio.h>
24#ifdef FAST_MIXER_STATISTICS
25#include <cpustats/CentralTendencyStatistics.h>
26#endif
27#include "AudioMixer.h"
28#include "FastMixer.h"
29
30#define FAST_HOT_IDLE_NS 1000000L // 1 ms: time to sleep while hot idling
31#define FAST_DEFAULT_NS 999999999L // ~1 sec: default time to sleep
Glenn Kasten288ed212012-04-25 17:52:27 -070032#define MAX_WARMUP_CYCLES 10 // maximum number of loop cycles to wait for warmup
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070033
34namespace android {
35
36// Fast mixer thread
37bool FastMixer::threadLoop()
38{
39 static const FastMixerState initial;
40 const FastMixerState *previous = &initial, *current = &initial;
41 FastMixerState preIdle; // copy of state before we went into idle
42 struct timespec oldTs = {0, 0};
43 bool oldTsValid = false;
44 long slopNs = 0; // accumulated time we've woken up too early (> 0) or too late (< 0)
45 long sleepNs = -1; // -1: busy wait, 0: sched_yield, > 0: nanosleep
46 int fastTrackNames[FastMixerState::kMaxFastTracks]; // handles used by mixer to identify tracks
47 int generations[FastMixerState::kMaxFastTracks]; // last observed mFastTracks[i].mGeneration
48 unsigned i;
49 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
50 fastTrackNames[i] = -1;
51 generations[i] = 0;
52 }
53 NBAIO_Sink *outputSink = NULL;
54 int outputSinkGen = 0;
55 AudioMixer* mixer = NULL;
56 short *mixBuffer = NULL;
57 enum {UNDEFINED, MIXED, ZEROED} mixBufferState = UNDEFINED;
58 NBAIO_Format format = Format_Invalid;
59 unsigned sampleRate = 0;
60 int fastTracksGen = 0;
61 long periodNs = 0; // expected period; the time required to render one mix buffer
Glenn Kasten288ed212012-04-25 17:52:27 -070062 long underrunNs = 0; // underrun likely when write cycle is greater than this value
63 long overrunNs = 0; // overrun likely when write cycle is less than this value
64 long warmupNs = 0; // warmup complete when write cycle is greater than to this value
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070065 FastMixerDumpState dummyDumpState, *dumpState = &dummyDumpState;
66 bool ignoreNextOverrun = true; // used to ignore initial overrun and first after an underrun
67#ifdef FAST_MIXER_STATISTICS
68 CentralTendencyStatistics cts; // cycle times in seconds
69 static const unsigned kMaxSamples = 1000;
70#endif
71 unsigned coldGen = 0; // last observed mColdGen
Glenn Kasten288ed212012-04-25 17:52:27 -070072 bool isWarm = false; // true means ready to mix, false means wait for warmup before mixing
73 struct timespec measuredWarmupTs = {0, 0}; // how long did it take for warmup to complete
74 uint32_t warmupCycles = 0; // counter of number of loop cycles required to warmup
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070075
76 for (;;) {
77
78 // either nanosleep, sched_yield, or busy wait
79 if (sleepNs >= 0) {
80 if (sleepNs > 0) {
81 ALOG_ASSERT(sleepNs < 1000000000);
82 const struct timespec req = {0, sleepNs};
83 nanosleep(&req, NULL);
84 } else {
85 sched_yield();
86 }
87 }
88 // default to long sleep for next cycle
89 sleepNs = FAST_DEFAULT_NS;
90
91 // poll for state change
92 const FastMixerState *next = mSQ.poll();
93 if (next == NULL) {
94 // continue to use the default initial state until a real state is available
95 ALOG_ASSERT(current == &initial && previous == &initial);
96 next = current;
97 }
98
99 FastMixerState::Command command = next->mCommand;
100 if (next != current) {
101
102 // As soon as possible of learning of a new dump area, start using it
103 dumpState = next->mDumpState != NULL ? next->mDumpState : &dummyDumpState;
104
105 // We want to always have a valid reference to the previous (non-idle) state.
106 // However, the state queue only guarantees access to current and previous states.
107 // So when there is a transition from a non-idle state into an idle state, we make a
108 // copy of the last known non-idle state so it is still available on return from idle.
109 // The possible transitions are:
110 // non-idle -> non-idle update previous from current in-place
111 // non-idle -> idle update previous from copy of current
112 // idle -> idle don't update previous
113 // idle -> non-idle don't update previous
114 if (!(current->mCommand & FastMixerState::IDLE)) {
115 if (command & FastMixerState::IDLE) {
116 preIdle = *current;
117 current = &preIdle;
118 oldTsValid = false;
119 ignoreNextOverrun = true;
120 }
121 previous = current;
122 }
123 current = next;
124 }
125#if !LOG_NDEBUG
126 next = NULL; // not referenced again
127#endif
128
129 dumpState->mCommand = command;
130
131 switch (command) {
132 case FastMixerState::INITIAL:
133 case FastMixerState::HOT_IDLE:
134 sleepNs = FAST_HOT_IDLE_NS;
135 continue;
136 case FastMixerState::COLD_IDLE:
137 // only perform a cold idle command once
Glenn Kasten21e8c502012-04-12 09:39:42 -0700138 // FIXME consider checking previous state and only perform if previous != COLD_IDLE
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700139 if (current->mColdGen != coldGen) {
140 int32_t *coldFutexAddr = current->mColdFutexAddr;
141 ALOG_ASSERT(coldFutexAddr != NULL);
142 int32_t old = android_atomic_dec(coldFutexAddr);
143 if (old <= 0) {
144 __futex_syscall4(coldFutexAddr, FUTEX_WAIT_PRIVATE, old - 1, NULL);
145 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700146 // This may be overly conservative; there could be times that the normal mixer
147 // requests such a brief cold idle that it doesn't require resetting this flag.
148 isWarm = false;
149 measuredWarmupTs.tv_sec = 0;
150 measuredWarmupTs.tv_nsec = 0;
151 warmupCycles = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700152 sleepNs = -1;
153 coldGen = current->mColdGen;
154 } else {
155 sleepNs = FAST_HOT_IDLE_NS;
156 }
157 continue;
158 case FastMixerState::EXIT:
159 delete mixer;
160 delete[] mixBuffer;
161 return false;
162 case FastMixerState::MIX:
163 case FastMixerState::WRITE:
164 case FastMixerState::MIX_WRITE:
165 break;
166 default:
167 LOG_FATAL("bad command %d", command);
168 }
169
170 // there is a non-idle state available to us; did the state change?
171 size_t frameCount = current->mFrameCount;
172 if (current != previous) {
173
174 // handle state change here, but since we want to diff the state,
175 // we're prepared for previous == &initial the first time through
176 unsigned previousTrackMask;
177
178 // check for change in output HAL configuration
179 NBAIO_Format previousFormat = format;
180 if (current->mOutputSinkGen != outputSinkGen) {
181 outputSink = current->mOutputSink;
182 outputSinkGen = current->mOutputSinkGen;
183 if (outputSink == NULL) {
184 format = Format_Invalid;
185 sampleRate = 0;
186 } else {
187 format = outputSink->format();
188 sampleRate = Format_sampleRate(format);
189 ALOG_ASSERT(Format_channelCount(format) == 2);
190 }
Glenn Kasten21e8c502012-04-12 09:39:42 -0700191 dumpState->mSampleRate = sampleRate;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700192 }
193
194 if ((format != previousFormat) || (frameCount != previous->mFrameCount)) {
195 // FIXME to avoid priority inversion, don't delete here
196 delete mixer;
197 mixer = NULL;
198 delete[] mixBuffer;
199 mixBuffer = NULL;
200 if (frameCount > 0 && sampleRate > 0) {
201 // FIXME new may block for unbounded time at internal mutex of the heap
202 // implementation; it would be better to have normal mixer allocate for us
203 // to avoid blocking here and to prevent possible priority inversion
204 mixer = new AudioMixer(frameCount, sampleRate, FastMixerState::kMaxFastTracks);
205 mixBuffer = new short[frameCount * 2];
206 periodNs = (frameCount * 1000000000LL) / sampleRate; // 1.00
207 underrunNs = (frameCount * 1750000000LL) / sampleRate; // 1.75
208 overrunNs = (frameCount * 250000000LL) / sampleRate; // 0.25
Glenn Kasten288ed212012-04-25 17:52:27 -0700209 warmupNs = (frameCount * 500000000LL) / sampleRate; // 0.50
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700210 } else {
211 periodNs = 0;
212 underrunNs = 0;
213 overrunNs = 0;
214 }
215 mixBufferState = UNDEFINED;
216#if !LOG_NDEBUG
217 for (i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
218 fastTrackNames[i] = -1;
219 }
220#endif
221 // we need to reconfigure all active tracks
222 previousTrackMask = 0;
223 fastTracksGen = current->mFastTracksGen - 1;
Glenn Kasten21e8c502012-04-12 09:39:42 -0700224 dumpState->mFrameCount = frameCount;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700225 } else {
226 previousTrackMask = previous->mTrackMask;
227 }
228
229 // check for change in active track set
230 unsigned currentTrackMask = current->mTrackMask;
231 if (current->mFastTracksGen != fastTracksGen) {
232 ALOG_ASSERT(mixBuffer != NULL);
233 int name;
234
235 // process removed tracks first to avoid running out of track names
236 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
237 while (removedTracks != 0) {
238 i = __builtin_ctz(removedTracks);
239 removedTracks &= ~(1 << i);
240 const FastTrack* fastTrack = &current->mFastTracks[i];
Glenn Kasten288ed212012-04-25 17:52:27 -0700241 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700242 if (mixer != NULL) {
243 name = fastTrackNames[i];
244 ALOG_ASSERT(name >= 0);
245 mixer->deleteTrackName(name);
246 }
247#if !LOG_NDEBUG
248 fastTrackNames[i] = -1;
249#endif
Glenn Kasten288ed212012-04-25 17:52:27 -0700250 // don't reset track dump state, since other side is ignoring it
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700251 generations[i] = fastTrack->mGeneration;
252 }
253
254 // now process added tracks
255 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
256 while (addedTracks != 0) {
257 i = __builtin_ctz(addedTracks);
258 addedTracks &= ~(1 << i);
259 const FastTrack* fastTrack = &current->mFastTracks[i];
260 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
261 ALOG_ASSERT(bufferProvider != NULL && fastTrackNames[i] == -1);
262 if (mixer != NULL) {
Jean-Michel Trivi9bd23222012-04-16 13:43:48 -0700263 // calling getTrackName with default channel mask
264 name = mixer->getTrackName(AUDIO_CHANNEL_OUT_STEREO);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700265 ALOG_ASSERT(name >= 0);
266 fastTrackNames[i] = name;
267 mixer->setBufferProvider(name, bufferProvider);
268 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
269 (void *) mixBuffer);
270 // newly allocated track names default to full scale volume
Glenn Kasten21e8c502012-04-12 09:39:42 -0700271 if (fastTrack->mSampleRate != 0 && fastTrack->mSampleRate != sampleRate) {
272 mixer->setParameter(name, AudioMixer::RESAMPLE,
273 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate);
274 }
275 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
276 (void *) fastTrack->mChannelMask);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700277 mixer->enable(name);
278 }
279 generations[i] = fastTrack->mGeneration;
280 }
281
282 // finally process modified tracks; these use the same slot
283 // but may have a different buffer provider or volume provider
284 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
285 while (modifiedTracks != 0) {
286 i = __builtin_ctz(modifiedTracks);
287 modifiedTracks &= ~(1 << i);
288 const FastTrack* fastTrack = &current->mFastTracks[i];
289 if (fastTrack->mGeneration != generations[i]) {
290 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
291 ALOG_ASSERT(bufferProvider != NULL);
292 if (mixer != NULL) {
293 name = fastTrackNames[i];
294 ALOG_ASSERT(name >= 0);
295 mixer->setBufferProvider(name, bufferProvider);
296 if (fastTrack->mVolumeProvider == NULL) {
297 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0,
298 (void *)0x1000);
299 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1,
300 (void *)0x1000);
301 }
Glenn Kasten21e8c502012-04-12 09:39:42 -0700302 if (fastTrack->mSampleRate != 0 &&
303 fastTrack->mSampleRate != sampleRate) {
304 mixer->setParameter(name, AudioMixer::RESAMPLE,
305 AudioMixer::SAMPLE_RATE, (void*) fastTrack->mSampleRate);
306 } else {
307 mixer->setParameter(name, AudioMixer::RESAMPLE,
308 AudioMixer::REMOVE, NULL);
309 }
310 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
311 (void *) fastTrack->mChannelMask);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700312 // already enabled
313 }
314 generations[i] = fastTrack->mGeneration;
315 }
316 }
317
318 fastTracksGen = current->mFastTracksGen;
319
320 dumpState->mNumTracks = popcount(currentTrackMask);
321 }
322
323#if 1 // FIXME shouldn't need this
324 // only process state change once
325 previous = current;
326#endif
327 }
328
329 // do work using current state here
Glenn Kasten288ed212012-04-25 17:52:27 -0700330 if ((command & FastMixerState::MIX) && (mixer != NULL) && isWarm) {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700331 ALOG_ASSERT(mixBuffer != NULL);
Glenn Kasten288ed212012-04-25 17:52:27 -0700332 // for each track, update volume and check for underrun
333 unsigned currentTrackMask = current->mTrackMask;
334 while (currentTrackMask != 0) {
335 i = __builtin_ctz(currentTrackMask);
336 currentTrackMask &= ~(1 << i);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700337 const FastTrack* fastTrack = &current->mFastTracks[i];
338 int name = fastTrackNames[i];
339 ALOG_ASSERT(name >= 0);
340 if (fastTrack->mVolumeProvider != NULL) {
341 uint32_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
342 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0,
343 (void *)(vlr & 0xFFFF));
344 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1,
345 (void *)(vlr >> 16));
346 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700347 // FIXME The current implementation of framesReady() for fast tracks
348 // takes a tryLock, which can block
349 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
350 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
351 size_t framesReady = fastTrack->mBufferProvider->framesReady();
352 FastTrackDump *ftDump = &dumpState->mTracks[i];
353 uint32_t underruns = ftDump->mUnderruns;
354 if (framesReady < frameCount) {
355 ftDump->mUnderruns = (underruns + 2) | 1;
356 if (framesReady == 0) {
357 mixer->disable(name);
358 } else {
359 // allow mixing partial buffer
360 mixer->enable(name);
361 }
362 } else if (underruns & 1) {
363 ftDump->mUnderruns = underruns & ~1;
364 mixer->enable(name);
365 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700366 }
367 // process() is CPU-bound
368 mixer->process(AudioBufferProvider::kInvalidPTS);
369 mixBufferState = MIXED;
370 } else if (mixBufferState == MIXED) {
371 mixBufferState = UNDEFINED;
372 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700373 bool attemptedWrite = false;
374 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700375 if ((command & FastMixerState::WRITE) && (outputSink != NULL) && (mixBuffer != NULL)) {
376 if (mixBufferState == UNDEFINED) {
377 memset(mixBuffer, 0, frameCount * 2 * sizeof(short));
378 mixBufferState = ZEROED;
379 }
380 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
381 // but this code should be modified to handle both non-blocking and blocking sinks
382 dumpState->mWriteSequence++;
383 ssize_t framesWritten = outputSink->write(mixBuffer, frameCount);
384 dumpState->mWriteSequence++;
385 if (framesWritten >= 0) {
Glenn Kasten288ed212012-04-25 17:52:27 -0700386 ALOG_ASSERT(framesWritten <= frameCount);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700387 dumpState->mFramesWritten += framesWritten;
Glenn Kasten288ed212012-04-25 17:52:27 -0700388 //if ((size_t) framesWritten == frameCount) {
389 // didFullWrite = true;
390 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700391 } else {
392 dumpState->mWriteErrors++;
393 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700394 attemptedWrite = true;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700395 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
396 }
397
398 // To be exactly periodic, compute the next sleep time based on current time.
399 // This code doesn't have long-term stability when the sink is non-blocking.
400 // FIXME To avoid drift, use the local audio clock or watch the sink's fill status.
401 struct timespec newTs;
402 int rc = clock_gettime(CLOCK_MONOTONIC, &newTs);
403 if (rc == 0) {
404 if (oldTsValid) {
405 time_t sec = newTs.tv_sec - oldTs.tv_sec;
406 long nsec = newTs.tv_nsec - oldTs.tv_nsec;
407 if (nsec < 0) {
408 --sec;
409 nsec += 1000000000;
410 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700411 // To avoid an initial underrun on fast tracks after exiting standby,
412 // do not start pulling data from tracks and mixing until warmup is complete.
413 // Warmup is considered complete after the earlier of:
414 // first successful single write() that blocks for more than warmupNs
415 // MAX_WARMUP_CYCLES write() attempts.
416 // This is overly conservative, but to get better accuracy requires a new HAL API.
417 if (!isWarm && attemptedWrite) {
418 measuredWarmupTs.tv_sec += sec;
419 measuredWarmupTs.tv_nsec += nsec;
420 if (measuredWarmupTs.tv_nsec >= 1000000000) {
421 measuredWarmupTs.tv_sec++;
422 measuredWarmupTs.tv_nsec -= 1000000000;
423 }
424 ++warmupCycles;
425 if ((attemptedWrite && nsec > warmupNs) ||
426 (warmupCycles >= MAX_WARMUP_CYCLES)) {
427 isWarm = true;
428 dumpState->mMeasuredWarmupTs = measuredWarmupTs;
429 dumpState->mWarmupCycles = warmupCycles;
430 }
431 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700432 if (sec > 0 || nsec > underrunNs) {
433 // FIXME only log occasionally
434 ALOGV("underrun: time since last cycle %d.%03ld sec",
435 (int) sec, nsec / 1000000L);
436 dumpState->mUnderruns++;
437 sleepNs = -1;
438 ignoreNextOverrun = true;
439 } else if (nsec < overrunNs) {
440 if (ignoreNextOverrun) {
441 ignoreNextOverrun = false;
442 } else {
443 // FIXME only log occasionally
444 ALOGV("overrun: time since last cycle %d.%03ld sec",
445 (int) sec, nsec / 1000000L);
446 dumpState->mOverruns++;
447 }
448 sleepNs = periodNs - overrunNs;
449 } else {
450 sleepNs = -1;
451 ignoreNextOverrun = false;
452 }
453#ifdef FAST_MIXER_STATISTICS
454 // long-term statistics
455 cts.sample(sec + nsec * 1e-9);
456 if (cts.n() >= kMaxSamples) {
457 dumpState->mMean = cts.mean();
458 dumpState->mMinimum = cts.minimum();
459 dumpState->mMaximum = cts.maximum();
460 dumpState->mStddev = cts.stddev();
461 cts.reset();
462 }
463#endif
464 } else {
465 // first time through the loop
466 oldTsValid = true;
467 sleepNs = periodNs;
468 ignoreNextOverrun = true;
469 }
470 oldTs = newTs;
471 } else {
472 // monotonic clock is broken
473 oldTsValid = false;
474 sleepNs = periodNs;
475 }
476
477 } // for (;;)
478
479 // never return 'true'; Thread::_threadLoop() locks mutex which can result in priority inversion
480}
481
482FastMixerDumpState::FastMixerDumpState() :
483 mCommand(FastMixerState::INITIAL), mWriteSequence(0), mFramesWritten(0),
Glenn Kasten21e8c502012-04-12 09:39:42 -0700484 mNumTracks(0), mWriteErrors(0), mUnderruns(0), mOverruns(0),
Glenn Kasten288ed212012-04-25 17:52:27 -0700485 mSampleRate(0), mFrameCount(0), /* mMeasuredWarmupTs({0, 0}), */ mWarmupCycles(0)
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700486#ifdef FAST_MIXER_STATISTICS
487 , mMean(0.0), mMinimum(0.0), mMaximum(0.0), mStddev(0.0)
488#endif
489{
Glenn Kasten288ed212012-04-25 17:52:27 -0700490 mMeasuredWarmupTs.tv_sec = 0;
491 mMeasuredWarmupTs.tv_nsec = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700492}
493
494FastMixerDumpState::~FastMixerDumpState()
495{
496}
497
498void FastMixerDumpState::dump(int fd)
499{
500#define COMMAND_MAX 32
501 char string[COMMAND_MAX];
502 switch (mCommand) {
503 case FastMixerState::INITIAL:
504 strcpy(string, "INITIAL");
505 break;
506 case FastMixerState::HOT_IDLE:
507 strcpy(string, "HOT_IDLE");
508 break;
509 case FastMixerState::COLD_IDLE:
510 strcpy(string, "COLD_IDLE");
511 break;
512 case FastMixerState::EXIT:
513 strcpy(string, "EXIT");
514 break;
515 case FastMixerState::MIX:
516 strcpy(string, "MIX");
517 break;
518 case FastMixerState::WRITE:
519 strcpy(string, "WRITE");
520 break;
521 case FastMixerState::MIX_WRITE:
522 strcpy(string, "MIX_WRITE");
523 break;
524 default:
525 snprintf(string, COMMAND_MAX, "%d", mCommand);
526 break;
527 }
Glenn Kasten288ed212012-04-25 17:52:27 -0700528 double mMeasuredWarmupMs = (mMeasuredWarmupTs.tv_sec * 1000.0) +
529 (mMeasuredWarmupTs.tv_nsec / 1000000.0);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700530 fdprintf(fd, "FastMixer command=%s writeSequence=%u framesWritten=%u\n"
Glenn Kasten21e8c502012-04-12 09:39:42 -0700531 " numTracks=%u writeErrors=%u underruns=%u overruns=%u\n"
Glenn Kasten288ed212012-04-25 17:52:27 -0700532 " sampleRate=%u frameCount=%u measuredWarmup=%.3g ms, warmupCycles=%u\n",
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700533 string, mWriteSequence, mFramesWritten,
Glenn Kasten21e8c502012-04-12 09:39:42 -0700534 mNumTracks, mWriteErrors, mUnderruns, mOverruns,
Glenn Kasten288ed212012-04-25 17:52:27 -0700535 mSampleRate, mFrameCount, mMeasuredWarmupMs, mWarmupCycles);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700536#ifdef FAST_MIXER_STATISTICS
537 fdprintf(fd, " cycle time in ms: mean=%.1f min=%.1f max=%.1f stddev=%.1f\n",
538 mMean*1e3, mMinimum*1e3, mMaximum*1e3, mStddev*1e3);
539#endif
540}
541
542} // namespace android