1 /*
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 // <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
23 #define LOG_TAG "FastMixer"
24 //#define LOG_NDEBUG 0
25
26 #define ATRACE_TAG ATRACE_TAG_AUDIO
27
28 #include "Configuration.h"
29 #include <time.h>
30 #include <utils/Debug.h>
31 #include <utils/Log.h>
32 #include <utils/Trace.h>
33 #include <system/audio.h>
34 #ifdef FAST_THREAD_STATISTICS
35 #include <audio_utils/Statistics.h>
36 #ifdef CPU_FREQUENCY_STATISTICS
37 #include <cpustats/ThreadCpuUsage.h>
38 #endif
39 #endif
40 #include <audio_utils/channels.h>
41 #include <audio_utils/format.h>
42 #include <audio_utils/mono_blend.h>
43 #include <cutils/bitops.h>
44 #include <media/AudioMixer.h>
45 #include "FastMixer.h"
46 #include "TypedLogger.h"
47
48 namespace android {
49
50 /*static*/ const FastMixerState FastMixer::sInitial;
51
FastMixer(audio_io_handle_t parentIoHandle)52 FastMixer::FastMixer(audio_io_handle_t parentIoHandle)
53 : FastThread("cycle_ms", "load_us"),
54 // mFastTrackNames
55 // mGenerations
56 mOutputSink(NULL),
57 mOutputSinkGen(0),
58 mMixer(NULL),
59 mSinkBuffer(NULL),
60 mSinkBufferSize(0),
61 mSinkChannelCount(FCC_2),
62 mMixerBuffer(NULL),
63 mMixerBufferSize(0),
64 mMixerBufferState(UNDEFINED),
65 mFormat(Format_Invalid),
66 mSampleRate(0),
67 mFastTracksGen(0),
68 mTotalNativeFramesWritten(0),
69 // timestamp
70 mNativeFramesWrittenButNotPresented(0), // the = 0 is to silence the compiler
71 mMasterMono(false),
72 mThreadIoHandle(parentIoHandle)
73 {
74 (void)mThreadIoHandle; // prevent unused warning, see C++17 [[maybe_unused]]
75
76 // FIXME pass sInitial as parameter to base class constructor, and make it static local
77 mPrevious = &sInitial;
78 mCurrent = &sInitial;
79
80 mDummyDumpState = &mDummyFastMixerDumpState;
81 // TODO: Add channel mask to NBAIO_Format.
82 // We assume that the channel mask must be a valid positional channel mask.
83 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
84
85 unsigned i;
86 for (i = 0; i < FastMixerState::sMaxFastTracks; ++i) {
87 mGenerations[i] = 0;
88 }
89 #ifdef FAST_THREAD_STATISTICS
90 mOldLoad.tv_sec = 0;
91 mOldLoad.tv_nsec = 0;
92 #endif
93 }
94
~FastMixer()95 FastMixer::~FastMixer()
96 {
97 }
98
sq()99 FastMixerStateQueue* FastMixer::sq()
100 {
101 return &mSQ;
102 }
103
poll()104 const FastThreadState *FastMixer::poll()
105 {
106 return mSQ.poll();
107 }
108
setNBLogWriter(NBLog::Writer * logWriter __unused)109 void FastMixer::setNBLogWriter(NBLog::Writer *logWriter __unused)
110 {
111 }
112
onIdle()113 void FastMixer::onIdle()
114 {
115 mPreIdle = *(const FastMixerState *)mCurrent;
116 mCurrent = &mPreIdle;
117 }
118
onExit()119 void FastMixer::onExit()
120 {
121 delete mMixer;
122 free(mMixerBuffer);
123 free(mSinkBuffer);
124 }
125
isSubClassCommand(FastThreadState::Command command)126 bool FastMixer::isSubClassCommand(FastThreadState::Command command)
127 {
128 switch ((FastMixerState::Command) command) {
129 case FastMixerState::MIX:
130 case FastMixerState::WRITE:
131 case FastMixerState::MIX_WRITE:
132 return true;
133 default:
134 return false;
135 }
136 }
137
updateMixerTrack(int index,Reason reason)138 void FastMixer::updateMixerTrack(int index, Reason reason) {
139 const FastMixerState * const current = (const FastMixerState *) mCurrent;
140 const FastTrack * const fastTrack = ¤t->mFastTracks[index];
141
142 // check and update generation
143 if (reason == REASON_MODIFY && mGenerations[index] == fastTrack->mGeneration) {
144 return; // no change on an already configured track.
145 }
146 mGenerations[index] = fastTrack->mGeneration;
147
148 // mMixer == nullptr on configuration failure (check done after generation update).
149 if (mMixer == nullptr) {
150 return;
151 }
152
153 switch (reason) {
154 case REASON_REMOVE:
155 mMixer->destroy(index);
156 break;
157 case REASON_ADD: {
158 const status_t status = mMixer->create(
159 index, fastTrack->mChannelMask, fastTrack->mFormat, AUDIO_SESSION_OUTPUT_MIX);
160 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
161 "%s: cannot create fast track index"
162 " %d, mask %#x, format %#x in AudioMixer",
163 __func__, index, fastTrack->mChannelMask, fastTrack->mFormat);
164 }
165 [[fallthrough]]; // now fallthrough to update the newly created track.
166 case REASON_MODIFY:
167 mMixer->setBufferProvider(index, fastTrack->mBufferProvider);
168
169 float vlf, vrf;
170 if (fastTrack->mVolumeProvider != nullptr) {
171 const gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
172 vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
173 vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
174 } else {
175 vlf = vrf = AudioMixer::UNITY_GAIN_FLOAT;
176 }
177
178 // set volume to avoid ramp whenever the track is updated (or created).
179 // Note: this does not distinguish from starting fresh or
180 // resuming from a paused state.
181 mMixer->setParameter(index, AudioMixer::VOLUME, AudioMixer::VOLUME0, &vlf);
182 mMixer->setParameter(index, AudioMixer::VOLUME, AudioMixer::VOLUME1, &vrf);
183
184 mMixer->setParameter(index, AudioMixer::RESAMPLE, AudioMixer::REMOVE, nullptr);
185 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
186 (void *)mMixerBuffer);
187 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MIXER_FORMAT,
188 (void *)(uintptr_t)mMixerBufferFormat);
189 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::FORMAT,
190 (void *)(uintptr_t)fastTrack->mFormat);
191 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::CHANNEL_MASK,
192 (void *)(uintptr_t)fastTrack->mChannelMask);
193 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::MIXER_CHANNEL_MASK,
194 (void *)(uintptr_t)mSinkChannelMask);
195 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::HAPTIC_ENABLED,
196 (void *)(uintptr_t)fastTrack->mHapticPlaybackEnabled);
197 mMixer->setParameter(index, AudioMixer::TRACK, AudioMixer::HAPTIC_INTENSITY,
198 (void *)(uintptr_t)fastTrack->mHapticIntensity);
199
200 mMixer->enable(index);
201 break;
202 default:
203 LOG_ALWAYS_FATAL("%s: invalid update reason %d", __func__, reason);
204 }
205 }
206
onStateChange()207 void FastMixer::onStateChange()
208 {
209 const FastMixerState * const current = (const FastMixerState *) mCurrent;
210 const FastMixerState * const previous = (const FastMixerState *) mPrevious;
211 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
212 const size_t frameCount = current->mFrameCount;
213
214 // update boottime offset, in case it has changed
215 mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
216 mBoottimeOffset.load();
217
218 // handle state change here, but since we want to diff the state,
219 // we're prepared for previous == &sInitial the first time through
220 unsigned previousTrackMask;
221
222 // check for change in output HAL configuration
223 NBAIO_Format previousFormat = mFormat;
224 if (current->mOutputSinkGen != mOutputSinkGen) {
225 mOutputSink = current->mOutputSink;
226 mOutputSinkGen = current->mOutputSinkGen;
227 mSinkChannelMask = current->mSinkChannelMask;
228 mBalance.setChannelMask(mSinkChannelMask);
229 if (mOutputSink == NULL) {
230 mFormat = Format_Invalid;
231 mSampleRate = 0;
232 mSinkChannelCount = 0;
233 mSinkChannelMask = AUDIO_CHANNEL_NONE;
234 mAudioChannelCount = 0;
235 } else {
236 mFormat = mOutputSink->format();
237 mSampleRate = Format_sampleRate(mFormat);
238 mSinkChannelCount = Format_channelCount(mFormat);
239 LOG_ALWAYS_FATAL_IF(mSinkChannelCount > AudioMixer::MAX_NUM_CHANNELS);
240
241 if (mSinkChannelMask == AUDIO_CHANNEL_NONE) {
242 mSinkChannelMask = audio_channel_out_mask_from_count(mSinkChannelCount);
243 }
244 mAudioChannelCount = mSinkChannelCount - audio_channel_count_from_out_mask(
245 mSinkChannelMask & AUDIO_CHANNEL_HAPTIC_ALL);
246 }
247 dumpState->mSampleRate = mSampleRate;
248 }
249
250 if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) {
251 // FIXME to avoid priority inversion, don't delete here
252 delete mMixer;
253 mMixer = NULL;
254 free(mMixerBuffer);
255 mMixerBuffer = NULL;
256 free(mSinkBuffer);
257 mSinkBuffer = NULL;
258 if (frameCount > 0 && mSampleRate > 0) {
259 // FIXME new may block for unbounded time at internal mutex of the heap
260 // implementation; it would be better to have normal mixer allocate for us
261 // to avoid blocking here and to prevent possible priority inversion
262 mMixer = new AudioMixer(frameCount, mSampleRate);
263 // FIXME See the other FIXME at FastMixer::setNBLogWriter()
264 NBLog::thread_params_t params;
265 params.frameCount = frameCount;
266 params.sampleRate = mSampleRate;
267 LOG_THREAD_PARAMS(params);
268 const size_t mixerFrameSize = mSinkChannelCount
269 * audio_bytes_per_sample(mMixerBufferFormat);
270 mMixerBufferSize = mixerFrameSize * frameCount;
271 (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
272 const size_t sinkFrameSize = mSinkChannelCount
273 * audio_bytes_per_sample(mFormat.mFormat);
274 if (sinkFrameSize > mixerFrameSize) { // need a sink buffer
275 mSinkBufferSize = sinkFrameSize * frameCount;
276 (void)posix_memalign(&mSinkBuffer, 32, mSinkBufferSize);
277 }
278 mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00
279 mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75
280 mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50
281 mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95
282 mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75
283 mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25
284 } else {
285 mPeriodNs = 0;
286 mUnderrunNs = 0;
287 mOverrunNs = 0;
288 mForceNs = 0;
289 mWarmupNsMin = 0;
290 mWarmupNsMax = LONG_MAX;
291 }
292 mMixerBufferState = UNDEFINED;
293 // we need to reconfigure all active tracks
294 previousTrackMask = 0;
295 mFastTracksGen = current->mFastTracksGen - 1;
296 dumpState->mFrameCount = frameCount;
297 #ifdef TEE_SINK
298 mTee.set(mFormat, NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
299 mTee.setId(std::string("_") + std::to_string(mThreadIoHandle) + "_F");
300 #endif
301 } else {
302 previousTrackMask = previous->mTrackMask;
303 }
304
305 // check for change in active track set
306 const unsigned currentTrackMask = current->mTrackMask;
307 dumpState->mTrackMask = currentTrackMask;
308 dumpState->mNumTracks = popcount(currentTrackMask);
309 if (current->mFastTracksGen != mFastTracksGen) {
310
311 // process removed tracks first to avoid running out of track names
312 unsigned removedTracks = previousTrackMask & ~currentTrackMask;
313 while (removedTracks != 0) {
314 int i = __builtin_ctz(removedTracks);
315 removedTracks &= ~(1 << i);
316 updateMixerTrack(i, REASON_REMOVE);
317 // don't reset track dump state, since other side is ignoring it
318 }
319
320 // now process added tracks
321 unsigned addedTracks = currentTrackMask & ~previousTrackMask;
322 while (addedTracks != 0) {
323 int i = __builtin_ctz(addedTracks);
324 addedTracks &= ~(1 << i);
325 updateMixerTrack(i, REASON_ADD);
326 }
327
328 // finally process (potentially) modified tracks; these use the same slot
329 // but may have a different buffer provider or volume provider
330 unsigned modifiedTracks = currentTrackMask & previousTrackMask;
331 while (modifiedTracks != 0) {
332 int i = __builtin_ctz(modifiedTracks);
333 modifiedTracks &= ~(1 << i);
334 updateMixerTrack(i, REASON_MODIFY);
335 }
336
337 mFastTracksGen = current->mFastTracksGen;
338 }
339 }
340
onWork()341 void FastMixer::onWork()
342 {
343 // TODO: pass an ID parameter to indicate which time series we want to write to in NBLog.cpp
344 // Or: pass both of these into a single call with a boolean
345 const FastMixerState * const current = (const FastMixerState *) mCurrent;
346 FastMixerDumpState * const dumpState = (FastMixerDumpState *) mDumpState;
347
348 if (mIsWarm) {
349 // Logging timestamps for FastMixer is currently disabled to make memory room for logging
350 // other statistics in FastMixer.
351 // To re-enable, delete the #ifdef FASTMIXER_LOG_HIST_TS lines (and the #endif lines).
352 #ifdef FASTMIXER_LOG_HIST_TS
353 LOG_HIST_TS();
354 #endif
355 //ALOGD("Eric FastMixer::onWork() mIsWarm");
356 } else {
357 dumpState->mTimestampVerifier.discontinuity();
358 // See comment in if block.
359 #ifdef FASTMIXER_LOG_HIST_TS
360 LOG_AUDIO_STATE();
361 #endif
362 }
363 const FastMixerState::Command command = mCommand;
364 const size_t frameCount = current->mFrameCount;
365
366 if ((command & FastMixerState::MIX) && (mMixer != NULL) && mIsWarm) {
367 ALOG_ASSERT(mMixerBuffer != NULL);
368
369 // AudioMixer::mState.enabledTracks is undefined if mState.hook == process__validate,
370 // so we keep a side copy of enabledTracks
371 bool anyEnabledTracks = false;
372
373 // for each track, update volume and check for underrun
374 unsigned currentTrackMask = current->mTrackMask;
375 while (currentTrackMask != 0) {
376 int i = __builtin_ctz(currentTrackMask);
377 currentTrackMask &= ~(1 << i);
378 const FastTrack* fastTrack = ¤t->mFastTracks[i];
379
380 const int64_t trackFramesWrittenButNotPresented =
381 mNativeFramesWrittenButNotPresented;
382 const int64_t trackFramesWritten = fastTrack->mBufferProvider->framesReleased();
383 ExtendedTimestamp perTrackTimestamp(mTimestamp);
384
385 // Can't provide an ExtendedTimestamp before first frame presented.
386 // Also, timestamp may not go to very last frame on stop().
387 if (trackFramesWritten >= trackFramesWrittenButNotPresented &&
388 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) {
389 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
390 trackFramesWritten - trackFramesWrittenButNotPresented;
391 } else {
392 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
393 perTrackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
394 }
395 perTrackTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = trackFramesWritten;
396 fastTrack->mBufferProvider->onTimestamp(perTrackTimestamp);
397
398 const int name = i;
399 if (fastTrack->mVolumeProvider != NULL) {
400 gain_minifloat_packed_t vlr = fastTrack->mVolumeProvider->getVolumeLR();
401 float vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
402 float vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
403
404 mMixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::VOLUME0, &vlf);
405 mMixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::VOLUME1, &vrf);
406 }
407 // FIXME The current implementation of framesReady() for fast tracks
408 // takes a tryLock, which can block
409 // up to 1 ms. If enough active tracks all blocked in sequence, this would result
410 // in the overall fast mix cycle being delayed. Should use a non-blocking FIFO.
411 size_t framesReady = fastTrack->mBufferProvider->framesReady();
412 if (ATRACE_ENABLED()) {
413 // I wish we had formatted trace names
414 char traceName[16];
415 strcpy(traceName, "fRdy");
416 traceName[4] = i + (i < 10 ? '0' : 'A' - 10);
417 traceName[5] = '\0';
418 ATRACE_INT(traceName, framesReady);
419 }
420 FastTrackDump *ftDump = &dumpState->mTracks[i];
421 FastTrackUnderruns underruns = ftDump->mUnderruns;
422 if (framesReady < frameCount) {
423 if (framesReady == 0) {
424 underruns.mBitFields.mEmpty++;
425 underruns.mBitFields.mMostRecent = UNDERRUN_EMPTY;
426 mMixer->disable(name);
427 } else {
428 // allow mixing partial buffer
429 underruns.mBitFields.mPartial++;
430 underruns.mBitFields.mMostRecent = UNDERRUN_PARTIAL;
431 mMixer->enable(name);
432 anyEnabledTracks = true;
433 }
434 } else {
435 underruns.mBitFields.mFull++;
436 underruns.mBitFields.mMostRecent = UNDERRUN_FULL;
437 mMixer->enable(name);
438 anyEnabledTracks = true;
439 }
440 ftDump->mUnderruns = underruns;
441 ftDump->mFramesReady = framesReady;
442 ftDump->mFramesWritten = trackFramesWritten;
443 }
444
445 if (anyEnabledTracks) {
446 // process() is CPU-bound
447 mMixer->process();
448 mMixerBufferState = MIXED;
449 } else if (mMixerBufferState != ZEROED) {
450 mMixerBufferState = UNDEFINED;
451 }
452
453 } else if (mMixerBufferState == MIXED) {
454 mMixerBufferState = UNDEFINED;
455 }
456 //bool didFullWrite = false; // dumpsys could display a count of partial writes
457 if ((command & FastMixerState::WRITE) && (mOutputSink != NULL) && (mMixerBuffer != NULL)) {
458 if (mMixerBufferState == UNDEFINED) {
459 memset(mMixerBuffer, 0, mMixerBufferSize);
460 mMixerBufferState = ZEROED;
461 }
462
463 if (mMasterMono.load()) { // memory_order_seq_cst
464 mono_blend(mMixerBuffer, mMixerBufferFormat, Format_channelCount(mFormat), frameCount,
465 true /*limit*/);
466 }
467
468 // Balance must take effect after mono conversion.
469 // mBalance detects zero balance within the class for speed (not needed here).
470 mBalance.setBalance(mMasterBalance.load());
471 mBalance.process((float *)mMixerBuffer, frameCount);
472
473 // prepare the buffer used to write to sink
474 void *buffer = mSinkBuffer != NULL ? mSinkBuffer : mMixerBuffer;
475 if (mFormat.mFormat != mMixerBufferFormat) { // sink format not the same as mixer format
476 memcpy_by_audio_format(buffer, mFormat.mFormat, mMixerBuffer, mMixerBufferFormat,
477 frameCount * Format_channelCount(mFormat));
478 }
479 if (mSinkChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) {
480 // When there are haptic channels, the sample data is partially interleaved.
481 // Make the sample data fully interleaved here.
482 adjust_channels_non_destructive(buffer, mAudioChannelCount, buffer, mSinkChannelCount,
483 audio_bytes_per_sample(mFormat.mFormat),
484 frameCount * audio_bytes_per_frame(mAudioChannelCount, mFormat.mFormat));
485 }
486 // if non-NULL, then duplicate write() to this non-blocking sink
487 #ifdef TEE_SINK
488 mTee.write(buffer, frameCount);
489 #endif
490 // FIXME write() is non-blocking and lock-free for a properly implemented NBAIO sink,
491 // but this code should be modified to handle both non-blocking and blocking sinks
492 dumpState->mWriteSequence++;
493 ATRACE_BEGIN("write");
494 ssize_t framesWritten = mOutputSink->write(buffer, frameCount);
495 ATRACE_END();
496 dumpState->mWriteSequence++;
497 if (framesWritten >= 0) {
498 ALOG_ASSERT((size_t) framesWritten <= frameCount);
499 mTotalNativeFramesWritten += framesWritten;
500 dumpState->mFramesWritten = mTotalNativeFramesWritten;
501 //if ((size_t) framesWritten == frameCount) {
502 // didFullWrite = true;
503 //}
504 } else {
505 dumpState->mWriteErrors++;
506 }
507 mAttemptedWrite = true;
508 // FIXME count # of writes blocked excessively, CPU usage, etc. for dump
509
510 if (mIsWarm) {
511 ExtendedTimestamp timestamp; // local
512 status_t status = mOutputSink->getTimestamp(timestamp);
513 if (status == NO_ERROR) {
514 dumpState->mTimestampVerifier.add(
515 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
516 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
517 mSampleRate);
518 const int64_t totalNativeFramesPresented =
519 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
520 if (totalNativeFramesPresented <= mTotalNativeFramesWritten) {
521 mNativeFramesWrittenButNotPresented =
522 mTotalNativeFramesWritten - totalNativeFramesPresented;
523 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
524 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
525 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
526 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
527 // We don't compensate for server - kernel time difference and
528 // only update latency if we have valid info.
529 const double latencyMs =
530 (double)mNativeFramesWrittenButNotPresented * 1000 / mSampleRate;
531 dumpState->mLatencyMs = latencyMs;
532 LOG_LATENCY(latencyMs);
533 } else {
534 // HAL reported that more frames were presented than were written
535 mNativeFramesWrittenButNotPresented = 0;
536 status = INVALID_OPERATION;
537 }
538 } else {
539 dumpState->mTimestampVerifier.error();
540 }
541 if (status == NO_ERROR) {
542 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
543 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
544 } else {
545 // fetch server time if we can't get timestamp
546 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] =
547 systemTime(SYSTEM_TIME_MONOTONIC);
548 // clear out kernel cached position as this may get rapidly stale
549 // if we never get a new valid timestamp
550 mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 0;
551 mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = -1;
552 }
553 }
554 }
555 }
556
557 } // namespace android
558