blob: 07b6ad00599b161f0eff370a3e3e8655513819a2 [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
21#include <stdint.h>
Phil Burkfceeee72019-06-14 11:18:45 -070022#include <algorithm>
Phil Burk204a1632017-01-03 17:23:43 -080023
Phil Burk3316d5e2017-02-15 11:23:01 -080024#include "utility/AudioClock.h"
Phil Burk204a1632017-01-03 17:23:43 -080025#include "IsochronousClockModel.h"
26
Phil Burk5ed503c2017-02-01 09:38:15 -080027using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080028
Phil Burk34e2d2d2019-09-26 12:46:10 -070029#ifndef ICM_LOG_DRIFT
30#define ICM_LOG_DRIFT 0
31#endif // ICM_LOG_DRIFT
32
Phil Burk204a1632017-01-03 17:23:43 -080033IsochronousClockModel::IsochronousClockModel()
Phil Burk3316d5e2017-02-15 11:23:01 -080034 : mMarkerFramePosition(0)
35 , mMarkerNanoTime(0)
36 , mSampleRate(48000)
Phil Burk34e2d2d2019-09-26 12:46:10 -070037 , mFramesPerBurst(48)
Phil Burkfceeee72019-06-14 11:18:45 -070038 , mMaxMeasuredLatenessNanos(0)
Phil Burk34e2d2d2019-09-26 12:46:10 -070039 , mLatenessForDriftNanos(kInitialLatenessForDriftNanos)
Phil Burk204a1632017-01-03 17:23:43 -080040 , mState(STATE_STOPPED)
41{
42}
43
44IsochronousClockModel::~IsochronousClockModel() {
45}
46
Phil Burkec89b2e2017-06-20 15:05:06 -070047void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070048 ALOGV("setPositionAndTime, %lld, %lld", (long long) framePosition, (long long) nanoTime);
Phil Burkec89b2e2017-06-20 15:05:06 -070049 mMarkerFramePosition = framePosition;
50 mMarkerNanoTime = nanoTime;
51}
52
Phil Burk87c9f642017-05-17 07:22:39 -070053void IsochronousClockModel::start(int64_t nanoTime) {
Phil Burkfbf031e2017-10-12 15:58:31 -070054 ALOGV("start(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080055 mMarkerNanoTime = nanoTime;
56 mState = STATE_STARTING;
57}
58
Phil Burk87c9f642017-05-17 07:22:39 -070059void IsochronousClockModel::stop(int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070060 ALOGD("stop(nanos = %lld) max lateness = %d micros\n",
61 (long long) nanoTime,
62 (int) (mMaxMeasuredLatenessNanos / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -070063 setPositionAndTime(convertTimeToPosition(nanoTime), nanoTime);
64 // TODO should we set position?
Phil Burk204a1632017-01-03 17:23:43 -080065 mState = STATE_STOPPED;
66}
67
Phil Burk377c1c22018-12-12 16:06:54 -080068bool IsochronousClockModel::isStarting() const {
Phil Burkbcc36742017-08-31 17:24:51 -070069 return mState == STATE_STARTING;
70}
71
Phil Burk377c1c22018-12-12 16:06:54 -080072bool IsochronousClockModel::isRunning() const {
73 return mState == STATE_RUNNING;
74}
75
Phil Burk87c9f642017-05-17 07:22:39 -070076void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070077 mTimestampCount++;
78// Log position and time in CSV format so we can import it easily into spreadsheets.
79 //ALOGD("%s() CSV, %d, %lld, %lld", __func__,
80 //mTimestampCount, (long long)framePosition, (long long)nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080081 int64_t framesDelta = framePosition - mMarkerFramePosition;
82 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
83 if (nanosDelta < 1000) {
84 return;
85 }
86
Phil Burk87c9f642017-05-17 07:22:39 -070087// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080088// (long long)mMarkerFramePosition,
89// (long long)mMarkerNanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080090
91 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -070092// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -080093// (long long)expectedNanosDelta,
94// (long long)nanosDelta);
95
Phil Burk87c9f642017-05-17 07:22:39 -070096// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
97// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk34e2d2d2019-09-26 12:46:10 -070098 int64_t latenessNanos = nanosDelta - expectedNanosDelta;
Phil Burk204a1632017-01-03 17:23:43 -080099 switch (mState) {
100 case STATE_STOPPED:
101 break;
102 case STATE_STARTING:
Phil Burkec89b2e2017-06-20 15:05:06 -0700103 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800104 mState = STATE_SYNCING;
105 break;
106 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -0700107 // This will handle a burst of rapid transfer at the beginning.
Phil Burk34e2d2d2019-09-26 12:46:10 -0700108 if (latenessNanos < 0) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700109 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800110 } else {
Phil Burk87c9f642017-05-17 07:22:39 -0700111// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -0800112 mState = STATE_RUNNING;
113 }
114 break;
115 case STATE_RUNNING:
Phil Burk34e2d2d2019-09-26 12:46:10 -0700116 // Modify estimated position based on lateness.
117 // This affects the "early" side of the window, which controls output glitches.
118 if (latenessNanos < 0) {
Phil Burk204a1632017-01-03 17:23:43 -0800119 // Earlier than expected timestamp.
Phil Burk1815a762019-05-24 08:52:27 -0700120 // This data is probably more accurate, so use it.
121 // Or we may be drifting due to a fast HW clock.
Phil Burkec89b2e2017-06-20 15:05:06 -0700122 setPositionAndTime(framePosition, nanoTime);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700123#if ICM_LOG_DRIFT
124 int microsDelta = (int) (nanosDelta / 1000);
125 int expectedMicrosDelta = (int) (expectedNanosDelta / 1000);
126 ALOGD("%s() - STATE_RUNNING - #%d, %4d micros EARLY",
127 __func__, mTimestampCount, expectedMicrosDelta - microsDelta);
128#endif
129 } else if (latenessNanos > mLatenessForDriftNanos) {
130 // When we are on the late side, it may be because of preemption in the kernel,
Phil Burkfceeee72019-06-14 11:18:45 -0700131 // or timing jitter caused by resampling in the DSP,
132 // or we may be drifting due to a slow HW clock.
133 // We add slight drift value just in case there is actual long term drift
134 // forward caused by a slower clock.
135 // If the clock is faster than the model will get pushed earlier
Phil Burk34e2d2d2019-09-26 12:46:10 -0700136 // by the code in the earlier branch.
Phil Burkfceeee72019-06-14 11:18:45 -0700137 // The two opposing forces should allow the model to track the real clock
138 // over a long time.
139 int64_t driftingTime = mMarkerNanoTime + expectedNanosDelta + kDriftNanos;
140 setPositionAndTime(framePosition, driftingTime);
Phil Burk34e2d2d2019-09-26 12:46:10 -0700141#if ICM_LOG_DRIFT
142 ALOGD("%s() - STATE_RUNNING - #%d, DRIFT, lateness = %d micros",
143 __func__,
144 mTimestampCount,
145 (int) (latenessNanos / 1000));
146#endif
147 }
148
149 // Modify mMaxMeasuredLatenessNanos.
150 // This affects the "late" side of the window, which controls input glitches.
151 if (latenessNanos > mMaxMeasuredLatenessNanos) { // increase
152#if ICM_LOG_DRIFT
153 ALOGD("%s() - STATE_RUNNING - #%d, newmax %d - oldmax %d = %4d micros LATE",
154 __func__,
155 mTimestampCount,
156 (int) (latenessNanos / 1000),
157 mMaxMeasuredLatenessNanos / 1000,
158 (int) ((latenessNanos - mMaxMeasuredLatenessNanos) / 1000)
159 );
160#endif
161 mMaxMeasuredLatenessNanos = (int32_t) latenessNanos;
162 // Calculate upper region that will trigger a drift forwards.
163 mLatenessForDriftNanos = mMaxMeasuredLatenessNanos - (mMaxMeasuredLatenessNanos >> 4);
164 } else { // decrease
165 // If these is an outlier in lateness then mMaxMeasuredLatenessNanos can go high
166 // and stay there. So we slowly reduce mMaxMeasuredLatenessNanos for better
167 // long term stability. The two opposing forces will keep mMaxMeasuredLatenessNanos
168 // within a reasonable range.
169 mMaxMeasuredLatenessNanos -= kDriftNanos;
Phil Burk204a1632017-01-03 17:23:43 -0800170 }
171 break;
172 default:
173 break;
174 }
Phil Burk204a1632017-01-03 17:23:43 -0800175}
176
177void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
178 mSampleRate = sampleRate;
179 update();
180}
181
182void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
183 mFramesPerBurst = framesPerBurst;
184 update();
185}
186
Phil Burkfceeee72019-06-14 11:18:45 -0700187// Update expected lateness based on sampleRate and framesPerBurst
Phil Burk204a1632017-01-03 17:23:43 -0800188void IsochronousClockModel::update() {
Phil Burkfceeee72019-06-14 11:18:45 -0700189 mBurstPeriodNanos = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
Phil Burk204a1632017-01-03 17:23:43 -0800190}
191
Phil Burkec89b2e2017-06-20 15:05:06 -0700192int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800193 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800194}
195
Phil Burk3316d5e2017-02-15 11:23:01 -0800196int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800197 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800198}
199
Phil Burk87c9f642017-05-17 07:22:39 -0700200int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800201 if (mState == STATE_STOPPED) {
202 return mMarkerNanoTime;
203 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800204 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
205 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
206 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
207 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burkfd34a932017-07-19 07:03:52 -0700208 int64_t time = mMarkerNanoTime + nanosDelta;
Phil Burkfbf031e2017-10-12 15:58:31 -0700209// ALOGD("convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800210// (unsigned long long)framePosition,
211// (unsigned long long)time);
212 return time;
213}
214
Phil Burk87c9f642017-05-17 07:22:39 -0700215int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800216 if (mState == STATE_STOPPED) {
217 return mMarkerFramePosition;
218 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800219 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
220 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
221 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
222 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
223 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burkfbf031e2017-10-12 15:58:31 -0700224// ALOGD("convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800225// (unsigned long long)nanoTime,
226// (unsigned long long)position);
Phil Burkfbf031e2017-10-12 15:58:31 -0700227// ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800228// (long long) framesDelta, mFramesPerBurst);
229 return position;
230}
Phil Burkec89b2e2017-06-20 15:05:06 -0700231
Phil Burkfceeee72019-06-14 11:18:45 -0700232int32_t IsochronousClockModel::getLateTimeOffsetNanos() const {
Phil Burk34e2d2d2019-09-26 12:46:10 -0700233 return mMaxMeasuredLatenessNanos + kExtraLatenessNanos;
Phil Burkfceeee72019-06-14 11:18:45 -0700234}
235
236int64_t IsochronousClockModel::convertPositionToLatestTime(int64_t framePosition) const {
237 return convertPositionToTime(framePosition) + getLateTimeOffsetNanos();
238}
239
240int64_t IsochronousClockModel::convertLatestTimeToPosition(int64_t nanoTime) const {
241 return convertTimeToPosition(nanoTime - getLateTimeOffsetNanos());
242}
243
Phil Burkec89b2e2017-06-20 15:05:06 -0700244void IsochronousClockModel::dump() const {
Phil Burk34e2d2d2019-09-26 12:46:10 -0700245 ALOGD("mMarkerFramePosition = %16lld", (long long) mMarkerFramePosition);
246 ALOGD("mMarkerNanoTime = %16lld", (long long) mMarkerNanoTime);
247 ALOGD("mSampleRate = %8d", mSampleRate);
248 ALOGD("mFramesPerBurst = %8d", mFramesPerBurst);
249 ALOGD("mState = %8d", mState);
250 ALOGD("max lateness nanos = %8d", mMaxMeasuredLatenessNanos);
Phil Burkec89b2e2017-06-20 15:05:06 -0700251}