From 7efe2b8832b0e9a0cd4c07b9caa4a8a7a27b550c Mon Sep 17 00:00:00 2001 From: Andy Hung Date: Tue, 31 Mar 2020 10:24:42 -0700 Subject: [PATCH] AudioRecord: Add retrograde detection to timestamps Test: atest AudioRecordTest Test: camera record stop/start with debug log Bug: 147198344 Change-Id: Idd7a0bbd1938073b7b7751595b15e20e54ea3373 --- media/libaudioclient/AudioRecord.cpp | 36 +++++++++++++++++++ .../include/media/AudioRecord.h | 4 +++ 2 files changed, 40 insertions(+) diff --git a/media/libaudioclient/AudioRecord.cpp b/media/libaudioclient/AudioRecord.cpp index f9d179811b..c791b3839b 100644 --- a/media/libaudioclient/AudioRecord.cpp +++ b/media/libaudioclient/AudioRecord.cpp @@ -411,6 +411,9 @@ status_t AudioRecord::start(AudioSystem::sync_event_t event, audio_session_t tri mFramesReadServerOffset -= mFramesRead + framesFlushed; mFramesRead = 0; mProxy->clearTimestamp(); // timestamp is invalid until next server push + mPreviousTimestamp.clear(); + mTimestampRetrogradePositionReported = false; + mTimestampRetrogradeTimeReported = false; // reset current position as seen by client to 0 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition()); @@ -600,6 +603,39 @@ status_t AudioRecord::getTimestamp(ExtendedTimestamp *timestamp) timestamp->mPosition[i] += mFramesReadServerOffset; } } + + bool timestampRetrogradeTimeReported = false; + bool timestampRetrogradePositionReported = false; + for (int i = 0; i < ExtendedTimestamp::LOCATION_MAX; ++i) { + if (timestamp->mTimeNs[i] >= 0 && mPreviousTimestamp.mTimeNs[i] >= 0) { + if (timestamp->mTimeNs[i] < mPreviousTimestamp.mTimeNs[i]) { + if (!mTimestampRetrogradeTimeReported) { + ALOGD("%s: retrograde time adjusting [%d] current:%lld to previous:%lld", + __func__, i, (long long)timestamp->mTimeNs[i], + (long long)mPreviousTimestamp.mTimeNs[i]); + timestampRetrogradeTimeReported = true; + } + timestamp->mTimeNs[i] = mPreviousTimestamp.mTimeNs[i]; + } + if (timestamp->mPosition[i] < mPreviousTimestamp.mPosition[i]) { + if (!mTimestampRetrogradePositionReported) { + ALOGD("%s: retrograde position" + " adjusting [%d] current:%lld to previous:%lld", + __func__, i, (long long)timestamp->mPosition[i], + (long long)mPreviousTimestamp.mPosition[i]); + timestampRetrogradePositionReported = true; + } + timestamp->mPosition[i] = mPreviousTimestamp.mPosition[i]; + } + } + } + mPreviousTimestamp = *timestamp; + if (timestampRetrogradeTimeReported) { + mTimestampRetrogradeTimeReported = true; + } + if (timestampRetrogradePositionReported) { + mTimestampRetrogradePositionReported = true; + } } return status; } diff --git a/media/libaudioclient/include/media/AudioRecord.h b/media/libaudioclient/include/media/AudioRecord.h index 5c300ed3e0..b3c1cdf68c 100644 --- a/media/libaudioclient/include/media/AudioRecord.h +++ b/media/libaudioclient/include/media/AudioRecord.h @@ -711,6 +711,10 @@ private: bool mInOverrun; // whether recorder is currently in overrun state + ExtendedTimestamp mPreviousTimestamp{}; // used to detect retrograde motion + bool mTimestampRetrogradePositionReported = false; // reduce log spam + bool mTimestampRetrogradeTimeReported = false; // reduce log spam + private: class DeathNotifier : public IBinder::DeathRecipient { public: