blob: 9abdf530209ed1acf86c3fe12e405971d4124fbe [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
29IsochronousClockModel::IsochronousClockModel()
Phil Burk3316d5e2017-02-15 11:23:01 -080030 : mMarkerFramePosition(0)
31 , mMarkerNanoTime(0)
32 , mSampleRate(48000)
Phil Burk204a1632017-01-03 17:23:43 -080033 , mFramesPerBurst(64)
Phil Burkfceeee72019-06-14 11:18:45 -070034 , mMaxMeasuredLatenessNanos(0)
Phil Burk204a1632017-01-03 17:23:43 -080035 , mState(STATE_STOPPED)
36{
37}
38
39IsochronousClockModel::~IsochronousClockModel() {
40}
41
Phil Burkec89b2e2017-06-20 15:05:06 -070042void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070043 ALOGV("setPositionAndTime, %lld, %lld", (long long) framePosition, (long long) nanoTime);
Phil Burkec89b2e2017-06-20 15:05:06 -070044 mMarkerFramePosition = framePosition;
45 mMarkerNanoTime = nanoTime;
46}
47
Phil Burk87c9f642017-05-17 07:22:39 -070048void IsochronousClockModel::start(int64_t nanoTime) {
Phil Burkfbf031e2017-10-12 15:58:31 -070049 ALOGV("start(nanos = %lld)\n", (long long) nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080050 mMarkerNanoTime = nanoTime;
51 mState = STATE_STARTING;
52}
53
Phil Burk87c9f642017-05-17 07:22:39 -070054void IsochronousClockModel::stop(int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070055 ALOGD("stop(nanos = %lld) max lateness = %d micros\n",
56 (long long) nanoTime,
57 (int) (mMaxMeasuredLatenessNanos / 1000));
Phil Burkec89b2e2017-06-20 15:05:06 -070058 setPositionAndTime(convertTimeToPosition(nanoTime), nanoTime);
59 // TODO should we set position?
Phil Burk204a1632017-01-03 17:23:43 -080060 mState = STATE_STOPPED;
61}
62
Phil Burk377c1c22018-12-12 16:06:54 -080063bool IsochronousClockModel::isStarting() const {
Phil Burkbcc36742017-08-31 17:24:51 -070064 return mState == STATE_STARTING;
65}
66
Phil Burk377c1c22018-12-12 16:06:54 -080067bool IsochronousClockModel::isRunning() const {
68 return mState == STATE_RUNNING;
69}
70
Phil Burk87c9f642017-05-17 07:22:39 -070071void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) {
Phil Burkfceeee72019-06-14 11:18:45 -070072 mTimestampCount++;
73// Log position and time in CSV format so we can import it easily into spreadsheets.
74 //ALOGD("%s() CSV, %d, %lld, %lld", __func__,
75 //mTimestampCount, (long long)framePosition, (long long)nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080076 int64_t framesDelta = framePosition - mMarkerFramePosition;
77 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
78 if (nanosDelta < 1000) {
79 return;
80 }
81
Phil Burk87c9f642017-05-17 07:22:39 -070082// ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu",
Phil Burk204a1632017-01-03 17:23:43 -080083// (long long)mMarkerFramePosition,
84// (long long)mMarkerNanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080085
86 int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burk87c9f642017-05-17 07:22:39 -070087// ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu",
Phil Burk204a1632017-01-03 17:23:43 -080088// (long long)expectedNanosDelta,
89// (long long)nanosDelta);
90
Phil Burk87c9f642017-05-17 07:22:39 -070091// ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate);
92// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -080093 switch (mState) {
94 case STATE_STOPPED:
95 break;
96 case STATE_STARTING:
Phil Burkec89b2e2017-06-20 15:05:06 -070097 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080098 mState = STATE_SYNCING;
99 break;
100 case STATE_SYNCING:
Phil Burk87c9f642017-05-17 07:22:39 -0700101 // This will handle a burst of rapid transfer at the beginning.
Phil Burk204a1632017-01-03 17:23:43 -0800102 if (nanosDelta < expectedNanosDelta) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700103 setPositionAndTime(framePosition, nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -0800104 } else {
Phil Burk87c9f642017-05-17 07:22:39 -0700105// ALOGD("processTimestamp() - advance to STATE_RUNNING");
Phil Burk204a1632017-01-03 17:23:43 -0800106 mState = STATE_RUNNING;
107 }
108 break;
109 case STATE_RUNNING:
110 if (nanosDelta < expectedNanosDelta) {
111 // Earlier than expected timestamp.
Phil Burk1815a762019-05-24 08:52:27 -0700112 // This data is probably more accurate, so use it.
113 // Or we may be drifting due to a fast HW clock.
Phil Burkfceeee72019-06-14 11:18:45 -0700114 //int microsDelta = (int) (nanosDelta / 1000);
115 //int expectedMicrosDelta = (int) (expectedNanosDelta / 1000);
116 //ALOGD("%s() - STATE_RUNNING - #%d, %4d micros EARLY",
117 //__func__, mTimestampCount, expectedMicrosDelta - microsDelta);
Phil Burk1815a762019-05-24 08:52:27 -0700118
Phil Burkec89b2e2017-06-20 15:05:06 -0700119 setPositionAndTime(framePosition, nanoTime);
Phil Burkfceeee72019-06-14 11:18:45 -0700120 } else if (nanosDelta > (expectedNanosDelta + (2 * mBurstPeriodNanos))) {
121 // In this case we do not update mMaxMeasuredLatenessNanos because it
122 // would force it too high.
123 // mMaxMeasuredLatenessNanos should range from 1 to 2 * mBurstPeriodNanos
124 //int32_t measuredLatenessNanos = (int32_t)(nanosDelta - expectedNanosDelta);
125 //ALOGD("%s() - STATE_RUNNING - #%d, lateness %d - max %d = %4d micros VERY LATE",
126 //__func__,
127 //mTimestampCount,
128 //measuredLatenessNanos / 1000,
129 //mMaxMeasuredLatenessNanos / 1000,
130 //(measuredLatenessNanos - mMaxMeasuredLatenessNanos) / 1000
131 //);
Phil Burk1815a762019-05-24 08:52:27 -0700132
Phil Burkfceeee72019-06-14 11:18:45 -0700133 // This typically happens when we are modelling a service instead of a DSP.
134 setPositionAndTime(framePosition, nanoTime - (2 * mBurstPeriodNanos));
135 } else if (nanosDelta > (expectedNanosDelta + mMaxMeasuredLatenessNanos)) {
136 //int32_t previousLatenessNanos = mMaxMeasuredLatenessNanos;
137 mMaxMeasuredLatenessNanos = (int32_t)(nanosDelta - expectedNanosDelta);
138
139 //ALOGD("%s() - STATE_RUNNING - #%d, newmax %d - oldmax %d = %4d micros LATE",
140 //__func__,
141 //mTimestampCount,
142 //mMaxMeasuredLatenessNanos / 1000,
143 //previousLatenessNanos / 1000,
144 //(mMaxMeasuredLatenessNanos - previousLatenessNanos) / 1000
145 //);
146
147 // When we are late, it may be because of preemption in the kernel,
148 // or timing jitter caused by resampling in the DSP,
149 // or we may be drifting due to a slow HW clock.
150 // We add slight drift value just in case there is actual long term drift
151 // forward caused by a slower clock.
152 // If the clock is faster than the model will get pushed earlier
153 // by the code in the preceding branch.
154 // The two opposing forces should allow the model to track the real clock
155 // over a long time.
156 int64_t driftingTime = mMarkerNanoTime + expectedNanosDelta + kDriftNanos;
157 setPositionAndTime(framePosition, driftingTime);
158 //ALOGD("%s() - #%d, max lateness = %d micros",
159 //__func__,
160 //mTimestampCount,
161 //(int) (mMaxMeasuredLatenessNanos / 1000));
Phil Burk204a1632017-01-03 17:23:43 -0800162 }
163 break;
164 default:
165 break;
166 }
Phil Burkbcc36742017-08-31 17:24:51 -0700167
168// ALOGD("processTimestamp() - mState = %d", mState);
Phil Burk204a1632017-01-03 17:23:43 -0800169}
170
171void IsochronousClockModel::setSampleRate(int32_t sampleRate) {
172 mSampleRate = sampleRate;
173 update();
174}
175
176void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) {
177 mFramesPerBurst = framesPerBurst;
178 update();
179}
180
Phil Burkfceeee72019-06-14 11:18:45 -0700181// Update expected lateness based on sampleRate and framesPerBurst
Phil Burk204a1632017-01-03 17:23:43 -0800182void IsochronousClockModel::update() {
Phil Burkfceeee72019-06-14 11:18:45 -0700183 mBurstPeriodNanos = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate
184 // Timestamps may be late by up to a burst because we are randomly sampling the time period
185 // after the DSP position is actually updated.
186 mMaxMeasuredLatenessNanos = mBurstPeriodNanos;
Phil Burk204a1632017-01-03 17:23:43 -0800187}
188
Phil Burkec89b2e2017-06-20 15:05:06 -0700189int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800190 return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate;
Phil Burk204a1632017-01-03 17:23:43 -0800191}
192
Phil Burk3316d5e2017-02-15 11:23:01 -0800193int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const {
Phil Burk5ed503c2017-02-01 09:38:15 -0800194 return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND;
Phil Burk204a1632017-01-03 17:23:43 -0800195}
196
Phil Burk87c9f642017-05-17 07:22:39 -0700197int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const {
Phil Burk204a1632017-01-03 17:23:43 -0800198 if (mState == STATE_STOPPED) {
199 return mMarkerNanoTime;
200 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800201 int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst;
202 int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex;
203 int64_t framesDelta = nextBurstPosition - mMarkerFramePosition;
204 int64_t nanosDelta = convertDeltaPositionToTime(framesDelta);
Phil Burkfd34a932017-07-19 07:03:52 -0700205 int64_t time = mMarkerNanoTime + nanosDelta;
Phil Burkfbf031e2017-10-12 15:58:31 -0700206// ALOGD("convertPositionToTime: pos = %llu --> time = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800207// (unsigned long long)framePosition,
208// (unsigned long long)time);
209 return time;
210}
211
Phil Burk87c9f642017-05-17 07:22:39 -0700212int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const {
Phil Burk204a1632017-01-03 17:23:43 -0800213 if (mState == STATE_STOPPED) {
214 return mMarkerFramePosition;
215 }
Phil Burk3316d5e2017-02-15 11:23:01 -0800216 int64_t nanosDelta = nanoTime - mMarkerNanoTime;
217 int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta);
218 int64_t nextBurstPosition = mMarkerFramePosition + framesDelta;
219 int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst;
220 int64_t position = nextBurstIndex * mFramesPerBurst;
Phil Burkfbf031e2017-10-12 15:58:31 -0700221// ALOGD("convertTimeToPosition: time = %llu --> pos = %llu",
Phil Burk204a1632017-01-03 17:23:43 -0800222// (unsigned long long)nanoTime,
223// (unsigned long long)position);
Phil Burkfbf031e2017-10-12 15:58:31 -0700224// ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d",
Phil Burk204a1632017-01-03 17:23:43 -0800225// (long long) framesDelta, mFramesPerBurst);
226 return position;
227}
Phil Burkec89b2e2017-06-20 15:05:06 -0700228
Phil Burkfceeee72019-06-14 11:18:45 -0700229int32_t IsochronousClockModel::getLateTimeOffsetNanos() const {
230 // This will never be < 0 because mMaxLatenessNanos starts at
231 // mBurstPeriodNanos and only gets bigger.
232 return (mMaxMeasuredLatenessNanos - mBurstPeriodNanos) + kExtraLatenessNanos;
233}
234
235int64_t IsochronousClockModel::convertPositionToLatestTime(int64_t framePosition) const {
236 return convertPositionToTime(framePosition) + getLateTimeOffsetNanos();
237}
238
239int64_t IsochronousClockModel::convertLatestTimeToPosition(int64_t nanoTime) const {
240 return convertTimeToPosition(nanoTime - getLateTimeOffsetNanos());
241}
242
Phil Burkec89b2e2017-06-20 15:05:06 -0700243void IsochronousClockModel::dump() const {
Phil Burkfbf031e2017-10-12 15:58:31 -0700244 ALOGD("mMarkerFramePosition = %lld", (long long) mMarkerFramePosition);
245 ALOGD("mMarkerNanoTime = %lld", (long long) mMarkerNanoTime);
246 ALOGD("mSampleRate = %6d", mSampleRate);
247 ALOGD("mFramesPerBurst = %6d", mFramesPerBurst);
Phil Burkfceeee72019-06-14 11:18:45 -0700248 ALOGD("mMaxMeasuredLatenessNanos = %6d", mMaxMeasuredLatenessNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700249 ALOGD("mState = %6d", mState);
Phil Burkec89b2e2017-06-20 15:05:06 -0700250}