blob: f0dcd448594e64cd79b13717839d4f3e2b723357 [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 Burk9e1f44b2020-04-08 16:02:05 -070050 , mBurstPeriodNanos(0) // this will be updated before use
Phil Burkfceeee72019-06-14 11:18:45 -070051 , mMaxMeasuredLatenessNanos(0)
Phil Burk34e2d2d2019-09-26 12:46:10 -070052 , mLatenessForDriftNanos(kInitialLatenessForDriftNanos)
Phil Burk204a1632017-01-03 17:23:43 -080053 , mState(STATE_STOPPED)
54{
Phil Burkef34be52019-09-26 13:45:25 -070055 if ((AAudioProperty_getLogMask() & AAUDIO_LOG_CLOCK_MODEL_HISTOGRAM) != 0) {
56 mHistogramMicros = std::make_unique<Histogram>(kHistogramBinCount,
57 kHistogramBinWidthMicros);
58 }
Phil Burk204a1632017-01-03 17:23:43 -080059}
60
Phil Burkec89b2e2017-06-20 15:05:06 -070061void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070062 ALOGV("setPositionAndTime, %lld, %lld", (long long) framePosition, (long long) nanoTime);
Phil Burkec89b2e2017-06-20 15:05:06 -070063 mMarkerFramePosition = framePosition;
64 mMarkerNanoTime = nanoTime;
65}
66
Phil Burk87c9f642017-05-17 07:22:39 -070067void IsochronousClockModel::start(int64_t nanoTime) {
Phil Burkfbf031e2017-10-12 15:58:31 -070068 ALOGV("start(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080069 mMarkerNanoTime = nanoTime;
70 mState = STATE_STARTING;
Phil Burkef34be52019-09-26 13:45:25 -070071 if (mHistogramMicros) {
72 mHistogramMicros->clear();
73 }
Phil Burk204a1632017-01-03 17:23:43 -080074}
75
Phil Burk87c9f642017-05-17 07:22:39 -070076void IsochronousClockModel::stop(int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070077 ALOGD("stop(nanos = %lld) max lateness = %d micros\n",
78 (long long) nanoTime,
79 (int) (mMaxMeasuredLatenessNanos / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -070080 setPositionAndTime(convertTimeToPosition(nanoTime), nanoTime);
81 // TODO should we set position?
Phil Burk204a1632017-01-03 17:23:43 -080082 mState = STATE_STOPPED;
Phil Burkef34be52019-09-26 13:45:25 -070083 if (mHistogramMicros) {
84 dumpHistogram();
85 }
Phil Burk204a1632017-01-03 17:23:43 -080086}
87
Phil Burk377c1c22018-12-12 16:06:54 -080088bool IsochronousClockModel::isStarting() const {
Phil Burkbcc36742017-08-31 17:24:51 -070089 return mState == STATE_STARTING;
90}
91
Phil Burk377c1c22018-12-12 16:06:54 -080092bool IsochronousClockModel::isRunning() const {
93 return mState == STATE_RUNNING;
94}
95
Phil Burk87c9f642017-05-17 07:22:39 -070096void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070097 mTimestampCount++;
98// Log position and time in CSV format so we can import it easily into spreadsheets.
99 //ALOGD("%s() CSV, %d, %lld, %lld", __func__,
100 //mTimestampCount, (long long)framePosition, (long long)nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800101 int64_t framesDelta = framePosition - mMarkerFramePosition;
102 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
103 if (nanosDelta < 1000) {
104 return;
105 }
106
Phil Burk87c9f642017-05-17 07:22:39 -0700107// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800108// (long long)mMarkerFramePosition,
109// (long long)mMarkerNanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800110
111 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -0700112// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800113// (long long)expectedNanosDelta,
114// (long long)nanosDelta);
115
Phil Burk87c9f642017-05-17 07:22:39 -0700116// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
117// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700118 int64_t latenessNanos = nanosDelta - expectedNanosDelta;
Phil Burk204a1632017-01-03 17:23:43 -0800119 switch (mState) {
120 case STATE_STOPPED:
121 break;
122 case STATE_STARTING:
Phil Burkec89b2e2017-06-20 15:05:06 -0700123 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800124 mState = STATE_SYNCING;
125 break;
126 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -0700127 // This will handle a burst of rapid transfer at the beginning.
Phil Burk34e2d2d2019-09-26 12:46:10 -0700128 if (latenessNanos < 0) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700129 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800130 } else {
Phil Burk87c9f642017-05-17 07:22:39 -0700131// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -0800132 mState = STATE_RUNNING;
133 }
134 break;
135 case STATE_RUNNING:
Phil Burkef34be52019-09-26 13:45:25 -0700136 if (mHistogramMicros) {
137 mHistogramMicros->add(latenessNanos / AAUDIO_NANOS_PER_MICROSECOND);
138 }
Phil Burk34e2d2d2019-09-26 12:46:10 -0700139 // Modify estimated position based on lateness.
140 // This affects the "early" side of the window, which controls output glitches.
141 if (latenessNanos < 0) {
Phil Burk204a1632017-01-03 17:23:43 -0800142 // Earlier than expected timestamp.
Phil Burk1815a762019-05-24 08:52:27 -0700143 // This data is probably more accurate, so use it.
144 // Or we may be drifting due to a fast HW clock.
Phil Burkec89b2e2017-06-20 15:05:06 -0700145 setPositionAndTime(framePosition, nanoTime);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700146#if ICM_LOG_DRIFT
Phil Burkef34be52019-09-26 13:45:25 -0700147 int earlyDeltaMicros = (int) ((expectedNanosDelta - nanosDelta)/ 1000);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700148 ALOGD("%s() - STATE_RUNNING - #%d, %4d micros EARLY",
Phil Burkef34be52019-09-26 13:45:25 -0700149 __func__, mTimestampCount, earlyDeltaMicros);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700150#endif
151 } else if (latenessNanos > mLatenessForDriftNanos) {
152 // When we are on the late side, it may be because of preemption in the kernel,
Phil Burkfceeee72019-06-14 11:18:45 -0700153 // or timing jitter caused by resampling in the DSP,
154 // or we may be drifting due to a slow HW clock.
155 // We add slight drift value just in case there is actual long term drift
156 // forward caused by a slower clock.
157 // If the clock is faster than the model will get pushed earlier
Phil Burk34e2d2d2019-09-26 12:46:10 -0700158 // by the code in the earlier branch.
Phil Burkfceeee72019-06-14 11:18:45 -0700159 // The two opposing forces should allow the model to track the real clock
160 // over a long time.
161 int64_t driftingTime = mMarkerNanoTime + expectedNanosDelta + kDriftNanos;
162 setPositionAndTime(framePosition, driftingTime);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700163#if ICM_LOG_DRIFT
164 ALOGD("%s() - STATE_RUNNING - #%d, DRIFT, lateness = %d micros",
165 __func__,
166 mTimestampCount,
167 (int) (latenessNanos / 1000));
168#endif
169 }
170
171 // Modify mMaxMeasuredLatenessNanos.
172 // This affects the "late" side of the window, which controls input glitches.
173 if (latenessNanos > mMaxMeasuredLatenessNanos) { // increase
174#if ICM_LOG_DRIFT
175 ALOGD("%s() - STATE_RUNNING - #%d, newmax %d - oldmax %d = %4d micros LATE",
176 __func__,
177 mTimestampCount,
178 (int) (latenessNanos / 1000),
179 mMaxMeasuredLatenessNanos / 1000,
180 (int) ((latenessNanos - mMaxMeasuredLatenessNanos) / 1000)
181 );
182#endif
183 mMaxMeasuredLatenessNanos = (int32_t) latenessNanos;
184 // Calculate upper region that will trigger a drift forwards.
185 mLatenessForDriftNanos = mMaxMeasuredLatenessNanos - (mMaxMeasuredLatenessNanos >> 4);
186 } else { // decrease
Phil Burk9e1f44b2020-04-08 16:02:05 -0700187 // If this is an outlier in lateness then mMaxMeasuredLatenessNanos can go high
Phil Burk34e2d2d2019-09-26 12:46:10 -0700188 // and stay there. So we slowly reduce mMaxMeasuredLatenessNanos for better
189 // long term stability. The two opposing forces will keep mMaxMeasuredLatenessNanos
190 // within a reasonable range.
191 mMaxMeasuredLatenessNanos -= kDriftNanos;
Phil Burk204a1632017-01-03 17:23:43 -0800192 }
193 break;
194 default:
195 break;
196 }
Phil Burk204a1632017-01-03 17:23:43 -0800197}
198
199void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
200 mSampleRate = sampleRate;
201 update();
202}
203
204void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
205 mFramesPerBurst = framesPerBurst;
206 update();
207}
208
Phil Burkfceeee72019-06-14 11:18:45 -0700209// Update expected lateness based on sampleRate and framesPerBurst
Phil Burk204a1632017-01-03 17:23:43 -0800210void IsochronousClockModel::update() {
Phil Burkfceeee72019-06-14 11:18:45 -0700211 mBurstPeriodNanos = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
Phil Burk204a1632017-01-03 17:23:43 -0800212}
213
Phil Burkec89b2e2017-06-20 15:05:06 -0700214int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800215 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800216}
217
Phil Burk3316d5e2017-02-15 11:23:01 -0800218int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800219 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800220}
221
Phil Burk87c9f642017-05-17 07:22:39 -0700222int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800223 if (mState == STATE_STOPPED) {
224 return mMarkerNanoTime;
225 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800226 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
227 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
228 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
229 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burkfd34a932017-07-19 07:03:52 -0700230 int64_t time = mMarkerNanoTime + nanosDelta;
Phil Burkfbf031e2017-10-12 15:58:31 -0700231// ALOGD("convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800232// (unsigned long long)framePosition,
233// (unsigned long long)time);
234 return time;
235}
236
Phil Burk87c9f642017-05-17 07:22:39 -0700237int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800238 if (mState == STATE_STOPPED) {
239 return mMarkerFramePosition;
240 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800241 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
242 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
243 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
244 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
245 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burkfbf031e2017-10-12 15:58:31 -0700246// ALOGD("convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800247// (unsigned long long)nanoTime,
248// (unsigned long long)position);
Phil Burkfbf031e2017-10-12 15:58:31 -0700249// ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800250// (long long) framesDelta, mFramesPerBurst);
251 return position;
252}
Phil Burkec89b2e2017-06-20 15:05:06 -0700253
Phil Burkfceeee72019-06-14 11:18:45 -0700254int32_t IsochronousClockModel::getLateTimeOffsetNanos() const {
Phil Burk34e2d2d2019-09-26 12:46:10 -0700255 return mMaxMeasuredLatenessNanos + kExtraLatenessNanos;
Phil Burkfceeee72019-06-14 11:18:45 -0700256}
257
258int64_t IsochronousClockModel::convertPositionToLatestTime(int64_t framePosition) const {
259 return convertPositionToTime(framePosition) + getLateTimeOffsetNanos();
260}
261
262int64_t IsochronousClockModel::convertLatestTimeToPosition(int64_t nanoTime) const {
263 return convertTimeToPosition(nanoTime - getLateTimeOffsetNanos());
264}
265
Phil Burkec89b2e2017-06-20 15:05:06 -0700266void IsochronousClockModel::dump() const {
Phil Burkef34be52019-09-26 13:45:25 -0700267 ALOGD("mMarkerFramePosition = %" PRIu64, mMarkerFramePosition);
268 ALOGD("mMarkerNanoTime = %" PRIu64, mMarkerNanoTime);
269 ALOGD("mSampleRate = %6d", mSampleRate);
270 ALOGD("mFramesPerBurst = %6d", mFramesPerBurst);
271 ALOGD("mMaxMeasuredLatenessNanos = %6d", mMaxMeasuredLatenessNanos);
272 ALOGD("mState = %6d", mState);
273}
274
275void IsochronousClockModel::dumpHistogram() const {
276 if (!mHistogramMicros) return;
277 std::istringstream istr(mHistogramMicros->dump());
278 std::string line;
279 while (std::getline(istr, line)) {
280 ALOGD("lateness, %s", line.c_str());
281 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700282}