Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 17 | #define LOG_TAG "IsochronousClockModel" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Tom Cherry | 357552e | 2017-07-12 13:48:26 -0700 | [diff] [blame] | 19 | #include <log/log.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stdint.h> |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 22 | #include <algorithm> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 23 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 24 | #include "utility/AudioClock.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 25 | #include "IsochronousClockModel.h" |
| 26 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 27 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 28 | |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 29 | #ifndef ICM_LOG_DRIFT |
| 30 | #define ICM_LOG_DRIFT 0 |
| 31 | #endif // ICM_LOG_DRIFT |
| 32 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | IsochronousClockModel::IsochronousClockModel() |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 34 | : mMarkerFramePosition(0) |
| 35 | , mMarkerNanoTime(0) |
| 36 | , mSampleRate(48000) |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 37 | , mFramesPerBurst(48) |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 38 | , mMaxMeasuredLatenessNanos(0) |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 39 | , mLatenessForDriftNanos(kInitialLatenessForDriftNanos) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 40 | , mState(STATE_STOPPED) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | IsochronousClockModel::~IsochronousClockModel() { |
| 45 | } |
| 46 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 47 | void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) { |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 48 | ALOGV("setPositionAndTime, %lld, %lld", (long long) framePosition, (long long) nanoTime); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 49 | mMarkerFramePosition = framePosition; |
| 50 | mMarkerNanoTime = nanoTime; |
| 51 | } |
| 52 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 53 | void IsochronousClockModel::start(int64_t nanoTime) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 54 | ALOGV("start(nanos = %lld)\n", (long long) nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 55 | mMarkerNanoTime = nanoTime; |
| 56 | mState = STATE_STARTING; |
| 57 | } |
| 58 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 59 | void IsochronousClockModel::stop(int64_t nanoTime) { |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 60 | ALOGD("stop(nanos = %lld) max lateness = %d micros\n", |
| 61 | (long long) nanoTime, |
| 62 | (int) (mMaxMeasuredLatenessNanos / 1000)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 63 | setPositionAndTime(convertTimeToPosition(nanoTime), nanoTime); |
| 64 | // TODO should we set position? |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 65 | mState = STATE_STOPPED; |
| 66 | } |
| 67 | |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 68 | bool IsochronousClockModel::isStarting() const { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 69 | return mState == STATE_STARTING; |
| 70 | } |
| 71 | |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 72 | bool IsochronousClockModel::isRunning() const { |
| 73 | return mState == STATE_RUNNING; |
| 74 | } |
| 75 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 76 | void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) { |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 77 | 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 Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 81 | int64_t framesDelta = framePosition - mMarkerFramePosition; |
| 82 | int64_t nanosDelta = nanoTime - mMarkerNanoTime; |
| 83 | if (nanosDelta < 1000) { |
| 84 | return; |
| 85 | } |
| 86 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 87 | // ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 88 | // (long long)mMarkerFramePosition, |
| 89 | // (long long)mMarkerNanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 90 | |
| 91 | int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 92 | // ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | // (long long)expectedNanosDelta, |
| 94 | // (long long)nanosDelta); |
| 95 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 96 | // ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate); |
| 97 | // ALOGD("processTimestamp() - mState = %d", mState); |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 98 | int64_t latenessNanos = nanosDelta - expectedNanosDelta; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 99 | switch (mState) { |
| 100 | case STATE_STOPPED: |
| 101 | break; |
| 102 | case STATE_STARTING: |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 103 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 104 | mState = STATE_SYNCING; |
| 105 | break; |
| 106 | case STATE_SYNCING: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 107 | // This will handle a burst of rapid transfer at the beginning. |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 108 | if (latenessNanos < 0) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 109 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 110 | } else { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 111 | // ALOGD("processTimestamp() - advance to STATE_RUNNING"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 112 | mState = STATE_RUNNING; |
| 113 | } |
| 114 | break; |
| 115 | case STATE_RUNNING: |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 116 | // Modify estimated position based on lateness. |
| 117 | // This affects the "early" side of the window, which controls output glitches. |
| 118 | if (latenessNanos < 0) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 119 | // Earlier than expected timestamp. |
Phil Burk | 1815a76 | 2019-05-24 08:52:27 -0700 | [diff] [blame] | 120 | // This data is probably more accurate, so use it. |
| 121 | // Or we may be drifting due to a fast HW clock. |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 122 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 123 | #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 Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 131 | // 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 Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 136 | // by the code in the earlier branch. |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 137 | // 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 Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 141 | #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 Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 170 | } |
| 171 | break; |
| 172 | default: |
| 173 | break; |
| 174 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void IsochronousClockModel::setSampleRate(int32_t sampleRate) { |
| 178 | mSampleRate = sampleRate; |
| 179 | update(); |
| 180 | } |
| 181 | |
| 182 | void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) { |
| 183 | mFramesPerBurst = framesPerBurst; |
| 184 | update(); |
| 185 | } |
| 186 | |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 187 | // Update expected lateness based on sampleRate and framesPerBurst |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 188 | void IsochronousClockModel::update() { |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 189 | mBurstPeriodNanos = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 192 | int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 193 | return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 196 | int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 197 | return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 200 | int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 201 | if (mState == STATE_STOPPED) { |
| 202 | return mMarkerNanoTime; |
| 203 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 204 | 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 Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 208 | int64_t time = mMarkerNanoTime + nanosDelta; |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 209 | // ALOGD("convertPositionToTime: pos = %llu --> time = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 210 | // (unsigned long long)framePosition, |
| 211 | // (unsigned long long)time); |
| 212 | return time; |
| 213 | } |
| 214 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 215 | int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 216 | if (mState == STATE_STOPPED) { |
| 217 | return mMarkerFramePosition; |
| 218 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 219 | 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 Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 224 | // ALOGD("convertTimeToPosition: time = %llu --> pos = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 225 | // (unsigned long long)nanoTime, |
| 226 | // (unsigned long long)position); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 227 | // ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 228 | // (long long) framesDelta, mFramesPerBurst); |
| 229 | return position; |
| 230 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 231 | |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 232 | int32_t IsochronousClockModel::getLateTimeOffsetNanos() const { |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 233 | return mMaxMeasuredLatenessNanos + kExtraLatenessNanos; |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | int64_t IsochronousClockModel::convertPositionToLatestTime(int64_t framePosition) const { |
| 237 | return convertPositionToTime(framePosition) + getLateTimeOffsetNanos(); |
| 238 | } |
| 239 | |
| 240 | int64_t IsochronousClockModel::convertLatestTimeToPosition(int64_t nanoTime) const { |
| 241 | return convertTimeToPosition(nanoTime - getLateTimeOffsetNanos()); |
| 242 | } |
| 243 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 244 | void IsochronousClockModel::dump() const { |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 245 | 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 Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 251 | } |