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