blob: 67e2e6ecde34b9235c0507e3c1727330a50e4d02 [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>
34#ifdef FAST_MIXER_STATISTICS
35#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 Kasten22340022014-04-07 12:04:41 -070048/*static*/ const FastMixerState FastMixer::initial;
49
50FastMixer::FastMixer() : FastThread(),
51 slopNs(0),
52 // fastTrackNames
53 // generations
54 outputSink(NULL),
55 outputSinkGen(0),
56 mixer(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 Kasten22340022014-04-07 12:04:41 -070064 format(Format_Invalid),
65 sampleRate(0),
66 fastTracksGen(0),
67 totalNativeFramesWritten(0),
68 // timestamp
69 nativeFramesWrittenButNotPresented(0) // the = 0 is to silence the compiler
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070070{
Glenn Kasten22340022014-04-07 12:04:41 -070071 // FIXME pass initial as parameter to base class constructor, and make it static local
72 previous = &initial;
73 current = &initial;
74
75 mDummyDumpState = &dummyDumpState;
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) {
82 fastTrackNames[i] = -1;
83 generations[i] = 0;
84 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070085#ifdef FAST_MIXER_STATISTICS
Glenn Kasten22340022014-04-07 12:04:41 -070086 oldLoad.tv_sec = 0;
87 oldLoad.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{
107 if (mixer != NULL) {
108 mixer->setLog(logWriter);
109 }
110}
111
112void FastMixer::onIdle()
113{
114 preIdle = *(const FastMixerState *)current;
115 current = &preIdle;
116}
117
118void FastMixer::onExit()
119{
120 delete mixer;
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{
139 const FastMixerState * const current = (const FastMixerState *) this->current;
140 const FastMixerState * const previous = (const FastMixerState *) this->previous;
141 FastMixerDumpState * const dumpState = (FastMixerDumpState *) this->dumpState;
142 const size_t frameCount = current->mFrameCount;
143
144 // handle state change here, but since we want to diff the state,
145 // we're prepared for previous == &initial the first time through
146 unsigned previousTrackMask;
147
148 // check for change in output HAL configuration
149 NBAIO_Format previousFormat = format;
150 if (current->mOutputSinkGen != outputSinkGen) {
151 outputSink = current->mOutputSink;
152 outputSinkGen = current->mOutputSinkGen;
153 if (outputSink == NULL) {
154 format = Format_Invalid;
155 sampleRate = 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 {
159 format = outputSink->format();
160 sampleRate = Format_sampleRate(format);
Andy Hung9a592762014-07-21 21:56:01 -0700161 mSinkChannelCount = Format_channelCount(format);
162 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 }
168 dumpState->mSampleRate = sampleRate;
169 }
170
171 if ((!Format_isEqual(format, previousFormat)) || (frameCount != previous->mFrameCount)) {
172 // FIXME to avoid priority inversion, don't delete here
173 delete mixer;
174 mixer = 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 Kasten22340022014-04-07 12:04:41 -0700179 if (frameCount > 0 && sampleRate > 0) {
180 // FIXME new may block for unbounded time at internal mutex of the heap
181 // implementation; it would be better to have normal mixer allocate for us
182 // to avoid blocking here and to prevent possible priority inversion
183 mixer = new AudioMixer(frameCount, sampleRate, FastMixerState::kMaxFastTracks);
Andy Hung9a592762014-07-21 21:56:01 -0700184 const size_t mixerFrameSize = mSinkChannelCount
185 * audio_bytes_per_sample(mMixerBufferFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700186 mMixerBufferSize = mixerFrameSize * frameCount;
187 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
Andy Hung9a592762014-07-21 21:56:01 -0700188 const size_t sinkFrameSize = mSinkChannelCount
189 * audio_bytes_per_sample(format.mFormat);
Andy Hung1258c1a2014-05-23 21:22:17 -0700190 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
191 mSinkBufferSize = sinkFrameSize * frameCount;
192 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
193 }
Glenn Kasten22340022014-04-07 12:04:41 -0700194 periodNs = (frameCount * 1000000000LL) / sampleRate; // 1.00
195 underrunNs = (frameCount * 1750000000LL) / sampleRate; // 1.75
196 overrunNs = (frameCount * 500000000LL) / sampleRate; // 0.50
197 forceNs = (frameCount * 950000000LL) / sampleRate; // 0.95
198 warmupNs = (frameCount * 500000000LL) / sampleRate; // 0.50
199 } else {
200 periodNs = 0;
201 underrunNs = 0;
202 overrunNs = 0;
203 forceNs = 0;
204 warmupNs = 0;
205 }
Andy Hung45d68d32014-05-23 21:13:31 -0700206 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700207#if !LOG_NDEBUG
208 for (unsigned i = 0; i < FastMixerState::kMaxFastTracks; ++i) {
209 fastTrackNames[i] = -1;
210 }
Glenn Kasten0a14c4c2012-06-13 14:58:49 -0700211#endif
Glenn Kasten22340022014-04-07 12:04:41 -0700212 // we need to reconfigure all active tracks
213 previousTrackMask = 0;
214 fastTracksGen = current->mFastTracksGen - 1;
215 dumpState->mFrameCount = frameCount;
216 } else {
217 previousTrackMask = previous->mTrackMask;
218 }
Glenn Kasten732845c2013-08-23 09:26:31 -0700219
Glenn Kasten22340022014-04-07 12:04:41 -0700220 // check for change in active track set
221 const unsigned currentTrackMask = current->mTrackMask;
222 dumpState->mTrackMask = currentTrackMask;
223 if (current->mFastTracksGen != fastTracksGen) {
Andy Hung45d68d32014-05-23 21:13:31 -0700224 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700225 int name;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700226
Glenn Kasten22340022014-04-07 12:04:41 -0700227 // process removed tracks first to avoid running out of track names
228 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
229 while (removedTracks != 0) {
230 int i = __builtin_ctz(removedTracks);
231 removedTracks &= ~(1 << i);
232 const FastTrack* fastTrack = &current->mFastTracks[i];
233 ALOG_ASSERT(fastTrack->mBufferProvider == NULL);
Glenn Kastenab7d72f2013-02-27 09:05:28 -0800234 if (mixer != NULL) {
Glenn Kasten22340022014-04-07 12:04:41 -0700235 name = fastTrackNames[i];
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700236 ALOG_ASSERT(name >= 0);
Glenn Kasten22340022014-04-07 12:04:41 -0700237 mixer->deleteTrackName(name);
238 }
239#if !LOG_NDEBUG
240 fastTrackNames[i] = -1;
241#endif
242 // don't reset track dump state, since other side is ignoring it
243 generations[i] = fastTrack->mGeneration;
244 }
245
246 // now process added tracks
247 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
248 while (addedTracks != 0) {
249 int i = __builtin_ctz(addedTracks);
250 addedTracks &= ~(1 << i);
251 const FastTrack* fastTrack = &current->mFastTracks[i];
252 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
253 ALOG_ASSERT(bufferProvider != NULL && fastTrackNames[i] == -1);
254 if (mixer != NULL) {
Andy Hunge8a1ced2014-05-09 15:02:21 -0700255 name = mixer->getTrackName(fastTrack->mChannelMask,
256 fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
Glenn Kasten22340022014-04-07 12:04:41 -0700257 ALOG_ASSERT(name >= 0);
258 fastTrackNames[i] = name;
259 mixer->setBufferProvider(name, bufferProvider);
260 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
Andy Hung9a592762014-07-21 21:56:01 -0700261 (void *)mMixerBuffer);
Glenn Kasten22340022014-04-07 12:04:41 -0700262 // newly allocated track names default to full scale volume
Andy Hung1258c1a2014-05-23 21:22:17 -0700263 mixer->setParameter(
264 name,
265 AudioMixer::TRACK,
266 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700267 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
268 (void *)(uintptr_t)fastTrack->mFormat);
Andy Hung9a592762014-07-21 21:56:01 -0700269 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
270 (void *)(uintptr_t)fastTrack->mChannelMask);
271 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
272 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700273 mixer->enable(name);
274 }
275 generations[i] = fastTrack->mGeneration;
276 }
277
278 // finally process (potentially) modified tracks; these use the same slot
279 // but may have a different buffer provider or volume provider
280 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
281 while (modifiedTracks != 0) {
282 int i = __builtin_ctz(modifiedTracks);
283 modifiedTracks &= ~(1 << i);
284 const FastTrack* fastTrack = &current->mFastTracks[i];
285 if (fastTrack->mGeneration != generations[i]) {
286 // this track was actually modified
287 AudioBufferProvider *bufferProvider = fastTrack->mBufferProvider;
288 ALOG_ASSERT(bufferProvider != NULL);
289 if (mixer != NULL) {
290 name = fastTrackNames[i];
291 ALOG_ASSERT(name >= 0);
292 mixer->setBufferProvider(name, bufferProvider);
293 if (fastTrack->mVolumeProvider == NULL) {
Andy Hung6be49402014-05-30 10:42:03 -0700294 float f = AudioMixer::UNITY_GAIN_FLOAT;
295 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
296 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
Glenn Kasten288ed212012-04-25 17:52:27 -0700297 }
Glenn Kasten22340022014-04-07 12:04:41 -0700298 mixer->setParameter(name, AudioMixer::RESAMPLE,
299 AudioMixer::REMOVE, NULL);
Andy Hung1258c1a2014-05-23 21:22:17 -0700300 mixer->setParameter(
301 name,
302 AudioMixer::TRACK,
303 AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
Andy Hungef7c7fb2014-05-12 16:51:41 -0700304 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::FORMAT,
305 (void *)(uintptr_t)fastTrack->mFormat);
Glenn Kasten22340022014-04-07 12:04:41 -0700306 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
Andy Hung9a592762014-07-21 21:56:01 -0700307 (void *)(uintptr_t)fastTrack->mChannelMask);
308 mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
309 (void *)(uintptr_t)mSinkChannelMask);
Glenn Kasten22340022014-04-07 12:04:41 -0700310 // already enabled
311 }
312 generations[i] = fastTrack->mGeneration;
313 }
314 }
315
316 fastTracksGen = current->mFastTracksGen;
317
318 dumpState->mNumTracks = popcount(currentTrackMask);
319 }
320}
321
322void FastMixer::onWork()
323{
324 const FastMixerState * const current = (const FastMixerState *) this->current;
325 FastMixerDumpState * const dumpState = (FastMixerDumpState *) this->dumpState;
326 const FastMixerState::Command command = this->command;
327 const size_t frameCount = current->mFrameCount;
328
329 if ((command & FastMixerState::MIX) && (mixer != NULL) && isWarm) {
Andy Hungef7c7fb2014-05-12 16:51:41 -0700330 ALOG_ASSERT(mMixerBuffer != NULL);
Glenn Kasten22340022014-04-07 12:04:41 -0700331 // for each track, update volume and check for underrun
332 unsigned currentTrackMask = current->mTrackMask;
333 while (currentTrackMask != 0) {
334 int i = __builtin_ctz(currentTrackMask);
335 currentTrackMask &= ~(1 << i);
336 const FastTrack* fastTrack = &current->mFastTracks[i];
337
338 // Refresh the per-track timestamp
339 if (timestampStatus == NO_ERROR) {
340 uint32_t trackFramesWrittenButNotPresented =
341 nativeFramesWrittenButNotPresented;
342 uint32_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
343 // Can't provide an AudioTimestamp before first frame presented,
344 // or during the brief 32-bit wraparound window
345 if (trackFramesWritten >= trackFramesWrittenButNotPresented) {
346 AudioTimestamp perTrackTimestamp;
347 perTrackTimestamp.mPosition =
348 trackFramesWritten - trackFramesWrittenButNotPresented;
349 perTrackTimestamp.mTime = timestamp.mTime;
350 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
351 }
352 }
353
354 int name = fastTrackNames[i];
355 ALOG_ASSERT(name >= 0);
356 if (fastTrack->mVolumeProvider != NULL) {
Glenn Kastenc56f3422014-03-21 17:53:17 -0700357 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
Andy Hung6be49402014-05-30 10:42:03 -0700358 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
359 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
360
361 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
362 mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
Glenn Kasten22340022014-04-07 12:04:41 -0700363 }
364 // FIXME The current implementation of framesReady() for fast tracks
365 // takes a tryLock, which can block
366 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
367 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
368 size_t framesReady = fastTrack->mBufferProvider->framesReady();
369 if (ATRACE_ENABLED()) {
370 // I wish we had formatted trace names
371 char traceName[16];
372 strcpy(traceName, "fRdy");
373 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
374 traceName[5] = '\0';
375 ATRACE_INT(traceName, framesReady);
376 }
377 FastTrackDump *ftDump = &dumpState->mTracks[i];
378 FastTrackUnderruns underruns = ftDump->mUnderruns;
379 if (framesReady < frameCount) {
380 if (framesReady == 0) {
381 underruns.mBitFields.mEmpty++;
382 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
383 mixer->disable(name);
Glenn Kasten09474df2012-05-10 14:48:07 -0700384 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700385 // allow mixing partial buffer
386 underruns.mBitFields.mPartial++;
387 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
Glenn Kasten288ed212012-04-25 17:52:27 -0700388 mixer->enable(name);
389 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700390 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700391 underruns.mBitFields.mFull++;
392 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
393 mixer->enable(name);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700394 }
Glenn Kasten22340022014-04-07 12:04:41 -0700395 ftDump->mUnderruns = underruns;
396 ftDump->mFramesReady = framesReady;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700397 }
398
Glenn Kasten22340022014-04-07 12:04:41 -0700399 int64_t pts;
400 if (outputSink == NULL || (OK != outputSink->getNextWriteTimestamp(&pts))) {
401 pts = AudioBufferProvider::kInvalidPTS;
402 }
403
404 // process() is CPU-bound
405 mixer->process(pts);
Andy Hung45d68d32014-05-23 21:13:31 -0700406 mMixerBufferState = MIXED;
407 } else if (mMixerBufferState == MIXED) {
408 mMixerBufferState = UNDEFINED;
Glenn Kasten22340022014-04-07 12:04:41 -0700409 }
410 //bool didFullWrite = false; // dumpsys could display a count of partial writes
Andy Hung45d68d32014-05-23 21:13:31 -0700411 if ((command & FastMixerState::WRITE) && (outputSink != NULL) && (mMixerBuffer != NULL)) {
412 if (mMixerBufferState == UNDEFINED) {
Andy Hung1258c1a2014-05-23 21:22:17 -0700413 memset(mMixerBuffer, 0, mMixerBufferSize);
Andy Hung45d68d32014-05-23 21:13:31 -0700414 mMixerBufferState = ZEROED;
Glenn Kasten22340022014-04-07 12:04:41 -0700415 }
Andy Hung1258c1a2014-05-23 21:22:17 -0700416 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
417 if (format.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
418 memcpy_by_audio_format(buffer, format.mFormat, mMixerBuffer, mMixerBufferFormat,
419 frameCount * Format_channelCount(format));
420 }
Glenn Kasten22340022014-04-07 12:04:41 -0700421 // if non-NULL, then duplicate write() to this non-blocking sink
422 NBAIO_Sink* teeSink;
423 if ((teeSink = current->mTeeSink) != NULL) {
Glenn Kasten329f6512014-08-28 16:23:16 -0700424 (void) teeSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700425 }
426 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
427 // but this code should be modified to handle both non-blocking and blocking sinks
428 dumpState->mWriteSequence++;
429 ATRACE_BEGIN("write");
Andy Hung1258c1a2014-05-23 21:22:17 -0700430 ssize_t framesWritten = outputSink->write(buffer, frameCount);
Glenn Kasten22340022014-04-07 12:04:41 -0700431 ATRACE_END();
432 dumpState->mWriteSequence++;
433 if (framesWritten >= 0) {
434 ALOG_ASSERT((size_t) framesWritten <= frameCount);
435 totalNativeFramesWritten += framesWritten;
436 dumpState->mFramesWritten = totalNativeFramesWritten;
437 //if ((size_t) framesWritten == frameCount) {
438 // didFullWrite = true;
439 //}
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700440 } else {
Glenn Kasten22340022014-04-07 12:04:41 -0700441 dumpState->mWriteErrors++;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700442 }
Glenn Kasten22340022014-04-07 12:04:41 -0700443 attemptedWrite = true;
444 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700445
Glenn Kasten22340022014-04-07 12:04:41 -0700446 timestampStatus = outputSink->getTimestamp(timestamp);
447 if (timestampStatus == NO_ERROR) {
448 uint32_t totalNativeFramesPresented = timestamp.mPosition;
449 if (totalNativeFramesPresented <= totalNativeFramesWritten) {
450 nativeFramesWrittenButNotPresented =
451 totalNativeFramesWritten - totalNativeFramesPresented;
452 } else {
453 // HAL reported that more frames were presented than were written
454 timestampStatus = INVALID_OPERATION;
455 }
456 }
457 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700458}
459
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700460} // namespace android