blob: bd46d058415045d1bb68c13e38307062d2607c18 [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -08001/*
2 * Copyright (C) 2016 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
Phil Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "IsochronousClockModel"
Phil Burk204a1632017-01-03 17:23:43 -080018//#define LOG_NDEBUG 0
Tom Cherry357552e2017-07-12 13:48:26 -070019#include <log/log.h>
Phil Burk204a1632017-01-03 17:23:43 -080020
Phil Burkef34be52019-09-26 13:45:25 -070021#define __STDC_FORMAT_MACROS
22#include <inttypes.h>
Phil Burk204a1632017-01-03 17:23:43 -080023#include <stdint.h>
Phil Burkfceeee72019-06-14 11:18:45 -070024#include <algorithm>
Phil Burk204a1632017-01-03 17:23:43 -080025
Phil Burk3316d5e2017-02-15 11:23:01 -080026#include "utility/AudioClock.h"
Phil Burkef34be52019-09-26 13:45:25 -070027#include "utility/AAudioUtilities.h"
Phil Burk204a1632017-01-03 17:23:43 -080028#include "IsochronousClockModel.h"
29
Phil Burk5ed503c2017-02-01 09:38:15 -080030using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080031
Phil Burkef34be52019-09-26 13:45:25 -070032using namespace android::audio_utils;
33
Phil Burk34e2d2d2019-09-26 12:46:10 -070034#ifndef ICM_LOG_DRIFT
35#define ICM_LOG_DRIFT 0
36#endif // ICM_LOG_DRIFT
37
Phil Burkef34be52019-09-26 13:45:25 -070038// To enable the timestamp histogram, enter this before opening the stream:
39// adb root
40// adb shell setprop aaudio.log_mask 1
41// A histogram of the lateness of the timestamps will be cleared when the stream is started.
42// It will be updated when the model is stable and receives a timestamp,
43// and dumped to the log when the stream is stopped.
44
Phil Burk204a1632017-01-03 17:23:43 -080045IsochronousClockModel::IsochronousClockModel()
Phil Burk3316d5e2017-02-15 11:23:01 -080046 : mMarkerFramePosition(0)
47 , mMarkerNanoTime(0)
48 , mSampleRate(48000)
Phil Burk34e2d2d2019-09-26 12:46:10 -070049 , mFramesPerBurst(48)
Phil Burkfceeee72019-06-14 11:18:45 -070050 , mMaxMeasuredLatenessNanos(0)
Phil Burk34e2d2d2019-09-26 12:46:10 -070051 , mLatenessForDriftNanos(kInitialLatenessForDriftNanos)
Phil Burk204a1632017-01-03 17:23:43 -080052 , mState(STATE_STOPPED)
53{
Phil Burkef34be52019-09-26 13:45:25 -070054 if ((AAudioProperty_getLogMask() & AAUDIO_LOG_CLOCK_MODEL_HISTOGRAM) != 0) {
55 mHistogramMicros = std::make_unique<Histogram>(kHistogramBinCount,
56 kHistogramBinWidthMicros);
57 }
Phil Burk204a1632017-01-03 17:23:43 -080058}
59
60IsochronousClockModel::~IsochronousClockModel() {
61}
62
Phil Burkec89b2e2017-06-20 15:05:06 -070063void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070064 ALOGV("setPositionAndTime, %lld, %lld", (long long) framePosition, (long long) nanoTime);
Phil Burkec89b2e2017-06-20 15:05:06 -070065 mMarkerFramePosition = framePosition;
66 mMarkerNanoTime = nanoTime;
67}
68
Phil Burk87c9f642017-05-17 07:22:39 -070069void IsochronousClockModel::start(int64_t nanoTime) {
Phil Burkfbf031e2017-10-12 15:58:31 -070070 ALOGV("start(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080071 mMarkerNanoTime = nanoTime;
72 mState = STATE_STARTING;
Phil Burkef34be52019-09-26 13:45:25 -070073 if (mHistogramMicros) {
74 mHistogramMicros->clear();
75 }
Phil Burk204a1632017-01-03 17:23:43 -080076}
77
Phil Burk87c9f642017-05-17 07:22:39 -070078void IsochronousClockModel::stop(int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070079 ALOGD("stop(nanos = %lld) max lateness = %d micros\n",
80 (long long) nanoTime,
81 (int) (mMaxMeasuredLatenessNanos / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -070082 setPositionAndTime(convertTimeToPosition(nanoTime), nanoTime);
83 // TODO should we set position?
Phil Burk204a1632017-01-03 17:23:43 -080084 mState = STATE_STOPPED;
Phil Burkef34be52019-09-26 13:45:25 -070085 if (mHistogramMicros) {
86 dumpHistogram();
87 }
Phil Burk204a1632017-01-03 17:23:43 -080088}
89
Phil Burk377c1c22018-12-12 16:06:54 -080090bool IsochronousClockModel::isStarting() const {
Phil Burkbcc36742017-08-31 17:24:51 -070091 return mState == STATE_STARTING;
92}
93
Phil Burk377c1c22018-12-12 16:06:54 -080094bool IsochronousClockModel::isRunning() const {
95 return mState == STATE_RUNNING;
96}
97
Phil Burk87c9f642017-05-17 07:22:39 -070098void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070099 mTimestampCount++;
100// Log position and time in CSV format so we can import it easily into spreadsheets.
101 //ALOGD("%s() CSV, %d, %lld, %lld", __func__,
102 //mTimestampCount, (long long)framePosition, (long long)nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800103 int64_t framesDelta = framePosition - mMarkerFramePosition;
104 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
105 if (nanosDelta < 1000) {
106 return;
107 }
108
Phil Burk87c9f642017-05-17 07:22:39 -0700109// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800110// (long long)mMarkerFramePosition,
111// (long long)mMarkerNanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800112
113 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -0700114// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800115// (long long)expectedNanosDelta,
116// (long long)nanosDelta);
117
Phil Burk87c9f642017-05-17 07:22:39 -0700118// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
119// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700120 int64_t latenessNanos = nanosDelta - expectedNanosDelta;
Phil Burk204a1632017-01-03 17:23:43 -0800121 switch (mState) {
122 case STATE_STOPPED:
123 break;
124 case STATE_STARTING:
Phil Burkec89b2e2017-06-20 15:05:06 -0700125 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800126 mState = STATE_SYNCING;
127 break;
128 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -0700129 // This will handle a burst of rapid transfer at the beginning.
Phil Burk34e2d2d2019-09-26 12:46:10 -0700130 if (latenessNanos < 0) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700131 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800132 } else {
Phil Burk87c9f642017-05-17 07:22:39 -0700133// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -0800134 mState = STATE_RUNNING;
135 }
136 break;
137 case STATE_RUNNING:
Phil Burkef34be52019-09-26 13:45:25 -0700138 if (mHistogramMicros) {
139 mHistogramMicros->add(latenessNanos / AAUDIO_NANOS_PER_MICROSECOND);
140 }
Phil Burk34e2d2d2019-09-26 12:46:10 -0700141 // Modify estimated position based on lateness.
142 // This affects the "early" side of the window, which controls output glitches.
143 if (latenessNanos < 0) {
Phil Burk204a1632017-01-03 17:23:43 -0800144 // Earlier than expected timestamp.
Phil Burk1815a762019-05-24 08:52:27 -0700145 // This data is probably more accurate, so use it.
146 // Or we may be drifting due to a fast HW clock.
Phil Burkec89b2e2017-06-20 15:05:06 -0700147 setPositionAndTime(framePosition, nanoTime);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700148#if ICM_LOG_DRIFT
Phil Burkef34be52019-09-26 13:45:25 -0700149 int earlyDeltaMicros = (int) ((expectedNanosDelta - nanosDelta)/ 1000);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700150 ALOGD("%s() - STATE_RUNNING - #%d, %4d micros EARLY",
Phil Burkef34be52019-09-26 13:45:25 -0700151 __func__, mTimestampCount, earlyDeltaMicros);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700152#endif
153 } else if (latenessNanos > mLatenessForDriftNanos) {
154 // When we are on the late side, it may be because of preemption in the kernel,
Phil Burkfceeee72019-06-14 11:18:45 -0700155 // or timing jitter caused by resampling in the DSP,
156 // or we may be drifting due to a slow HW clock.
157 // We add slight drift value just in case there is actual long term drift
158 // forward caused by a slower clock.
159 // If the clock is faster than the model will get pushed earlier
Phil Burk34e2d2d2019-09-26 12:46:10 -0700160 // by the code in the earlier branch.
Phil Burkfceeee72019-06-14 11:18:45 -0700161 // The two opposing forces should allow the model to track the real clock
162 // over a long time.
163 int64_t driftingTime = mMarkerNanoTime + expectedNanosDelta + kDriftNanos;
164 setPositionAndTime(framePosition, driftingTime);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700165#if ICM_LOG_DRIFT
166 ALOGD("%s() - STATE_RUNNING - #%d, DRIFT, lateness = %d micros",
167 __func__,
168 mTimestampCount,
169 (int) (latenessNanos / 1000));
170#endif
171 }
172
173 // Modify mMaxMeasuredLatenessNanos.
174 // This affects the "late" side of the window, which controls input glitches.
175 if (latenessNanos > mMaxMeasuredLatenessNanos) { // increase
176#if ICM_LOG_DRIFT
177 ALOGD("%s() - STATE_RUNNING - #%d, newmax %d - oldmax %d = %4d micros LATE",
178 __func__,
179 mTimestampCount,
180 (int) (latenessNanos / 1000),
181 mMaxMeasuredLatenessNanos / 1000,
182 (int) ((latenessNanos - mMaxMeasuredLatenessNanos) / 1000)
183 );
184#endif
185 mMaxMeasuredLatenessNanos = (int32_t) latenessNanos;
186 // Calculate upper region that will trigger a drift forwards.
187 mLatenessForDriftNanos = mMaxMeasuredLatenessNanos - (mMaxMeasuredLatenessNanos >> 4);
188 } else { // decrease
189 // If these is an outlier in lateness then mMaxMeasuredLatenessNanos can go high
190 // and stay there. So we slowly reduce mMaxMeasuredLatenessNanos for better
191 // long term stability. The two opposing forces will keep mMaxMeasuredLatenessNanos
192 // within a reasonable range.
193 mMaxMeasuredLatenessNanos -= kDriftNanos;
Phil Burk204a1632017-01-03 17:23:43 -0800194 }
195 break;
196 default:
197 break;
198 }
Phil Burk204a1632017-01-03 17:23:43 -0800199}
200
201void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
202 mSampleRate = sampleRate;
203 update();
204}
205
206void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
207 mFramesPerBurst = framesPerBurst;
208 update();
209}
210
Phil Burkfceeee72019-06-14 11:18:45 -0700211// Update expected lateness based on sampleRate and framesPerBurst
Phil Burk204a1632017-01-03 17:23:43 -0800212void IsochronousClockModel::update() {
Phil Burkfceeee72019-06-14 11:18:45 -0700213 mBurstPeriodNanos = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
Phil Burk204a1632017-01-03 17:23:43 -0800214}
215
Phil Burkec89b2e2017-06-20 15:05:06 -0700216int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800217 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800218}
219
Phil Burk3316d5e2017-02-15 11:23:01 -0800220int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800221 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800222}
223
Phil Burk87c9f642017-05-17 07:22:39 -0700224int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800225 if (mState == STATE_STOPPED) {
226 return mMarkerNanoTime;
227 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800228 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
229 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
230 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
231 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burkfd34a932017-07-19 07:03:52 -0700232 int64_t time = mMarkerNanoTime + nanosDelta;
Phil Burkfbf031e2017-10-12 15:58:31 -0700233// ALOGD("convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800234// (unsigned long long)framePosition,
235// (unsigned long long)time);
236 return time;
237}
238
Phil Burk87c9f642017-05-17 07:22:39 -0700239int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800240 if (mState == STATE_STOPPED) {
241 return mMarkerFramePosition;
242 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800243 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
244 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
245 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
246 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
247 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burkfbf031e2017-10-12 15:58:31 -0700248// ALOGD("convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800249// (unsigned long long)nanoTime,
250// (unsigned long long)position);
Phil Burkfbf031e2017-10-12 15:58:31 -0700251// ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800252// (long long) framesDelta, mFramesPerBurst);
253 return position;
254}
Phil Burkec89b2e2017-06-20 15:05:06 -0700255
Phil Burkfceeee72019-06-14 11:18:45 -0700256int32_t IsochronousClockModel::getLateTimeOffsetNanos() const {
Phil Burk34e2d2d2019-09-26 12:46:10 -0700257 return mMaxMeasuredLatenessNanos + kExtraLatenessNanos;
Phil Burkfceeee72019-06-14 11:18:45 -0700258}
259
260int64_t IsochronousClockModel::convertPositionToLatestTime(int64_t framePosition) const {
261 return convertPositionToTime(framePosition) + getLateTimeOffsetNanos();
262}
263
264int64_t IsochronousClockModel::convertLatestTimeToPosition(int64_t nanoTime) const {
265 return convertTimeToPosition(nanoTime - getLateTimeOffsetNanos());
266}
267
Phil Burkec89b2e2017-06-20 15:05:06 -0700268void IsochronousClockModel::dump() const {
Phil Burkef34be52019-09-26 13:45:25 -0700269 ALOGD("mMarkerFramePosition = %" PRIu64, mMarkerFramePosition);
270 ALOGD("mMarkerNanoTime = %" PRIu64, mMarkerNanoTime);
271 ALOGD("mSampleRate = %6d", mSampleRate);
272 ALOGD("mFramesPerBurst = %6d", mFramesPerBurst);
273 ALOGD("mMaxMeasuredLatenessNanos = %6d", mMaxMeasuredLatenessNanos);
274 ALOGD("mState = %6d", mState);
275}
276
277void IsochronousClockModel::dumpHistogram() const {
278 if (!mHistogramMicros) return;
279 std::istringstream istr(mHistogramMicros->dump());
280 std::string line;
281 while (std::getline(istr, line)) {
282 ALOGD("lateness, %s", line.c_str());
283 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700284}