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 | |
| 29 | IsochronousClockModel::IsochronousClockModel() |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 30 | : mMarkerFramePosition(0) |
| 31 | , mMarkerNanoTime(0) |
| 32 | , mSampleRate(48000) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | , mFramesPerBurst(64) |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 34 | , mMaxMeasuredLatenessNanos(0) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 35 | , mState(STATE_STOPPED) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | IsochronousClockModel::~IsochronousClockModel() { |
| 40 | } |
| 41 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 42 | void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) { |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 43 | ALOGV("setPositionAndTime, %lld, %lld", (long long) framePosition, (long long) nanoTime); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 44 | mMarkerFramePosition = framePosition; |
| 45 | mMarkerNanoTime = nanoTime; |
| 46 | } |
| 47 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 48 | void IsochronousClockModel::start(int64_t nanoTime) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 49 | ALOGV("start(nanos = %lld)\n", (long long) nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 50 | mMarkerNanoTime = nanoTime; |
| 51 | mState = STATE_STARTING; |
| 52 | } |
| 53 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 54 | void IsochronousClockModel::stop(int64_t nanoTime) { |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 55 | ALOGD("stop(nanos = %lld) max lateness = %d micros\n", |
| 56 | (long long) nanoTime, |
| 57 | (int) (mMaxMeasuredLatenessNanos / 1000)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 58 | setPositionAndTime(convertTimeToPosition(nanoTime), nanoTime); |
| 59 | // TODO should we set position? |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 60 | mState = STATE_STOPPED; |
| 61 | } |
| 62 | |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 63 | bool IsochronousClockModel::isStarting() const { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 64 | return mState == STATE_STARTING; |
| 65 | } |
| 66 | |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 67 | bool IsochronousClockModel::isRunning() const { |
| 68 | return mState == STATE_RUNNING; |
| 69 | } |
| 70 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 71 | void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) { |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 72 | 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 Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 76 | int64_t framesDelta = framePosition - mMarkerFramePosition; |
| 77 | int64_t nanosDelta = nanoTime - mMarkerNanoTime; |
| 78 | if (nanosDelta < 1000) { |
| 79 | return; |
| 80 | } |
| 81 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 82 | // ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 83 | // (long long)mMarkerFramePosition, |
| 84 | // (long long)mMarkerNanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 85 | |
| 86 | int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 87 | // ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 88 | // (long long)expectedNanosDelta, |
| 89 | // (long long)nanosDelta); |
| 90 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 91 | // ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate); |
| 92 | // ALOGD("processTimestamp() - mState = %d", mState); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | switch (mState) { |
| 94 | case STATE_STOPPED: |
| 95 | break; |
| 96 | case STATE_STARTING: |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 97 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 98 | mState = STATE_SYNCING; |
| 99 | break; |
| 100 | case STATE_SYNCING: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 101 | // This will handle a burst of rapid transfer at the beginning. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 102 | if (nanosDelta < expectedNanosDelta) { |
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 | } else { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 105 | // ALOGD("processTimestamp() - advance to STATE_RUNNING"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 106 | mState = STATE_RUNNING; |
| 107 | } |
| 108 | break; |
| 109 | case STATE_RUNNING: |
| 110 | if (nanosDelta < expectedNanosDelta) { |
| 111 | // Earlier than expected timestamp. |
Phil Burk | 1815a76 | 2019-05-24 08:52:27 -0700 | [diff] [blame] | 112 | // This data is probably more accurate, so use it. |
| 113 | // Or we may be drifting due to a fast HW clock. |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 114 | //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 Burk | 1815a76 | 2019-05-24 08:52:27 -0700 | [diff] [blame] | 118 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 119 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 120 | } 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 Burk | 1815a76 | 2019-05-24 08:52:27 -0700 | [diff] [blame] | 132 | |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 133 | // 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 Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 162 | } |
| 163 | break; |
| 164 | default: |
| 165 | break; |
| 166 | } |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 167 | |
| 168 | // ALOGD("processTimestamp() - mState = %d", mState); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void IsochronousClockModel::setSampleRate(int32_t sampleRate) { |
| 172 | mSampleRate = sampleRate; |
| 173 | update(); |
| 174 | } |
| 175 | |
| 176 | void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) { |
| 177 | mFramesPerBurst = framesPerBurst; |
| 178 | update(); |
| 179 | } |
| 180 | |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 181 | // Update expected lateness based on sampleRate and framesPerBurst |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 182 | void IsochronousClockModel::update() { |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 183 | 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 Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 189 | int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 190 | return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 193 | int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 194 | return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 197 | int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 198 | if (mState == STATE_STOPPED) { |
| 199 | return mMarkerNanoTime; |
| 200 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 201 | 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 Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 205 | int64_t time = mMarkerNanoTime + nanosDelta; |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 206 | // ALOGD("convertPositionToTime: pos = %llu --> time = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 207 | // (unsigned long long)framePosition, |
| 208 | // (unsigned long long)time); |
| 209 | return time; |
| 210 | } |
| 211 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 212 | int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 213 | if (mState == STATE_STOPPED) { |
| 214 | return mMarkerFramePosition; |
| 215 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 216 | 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 Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 221 | // ALOGD("convertTimeToPosition: time = %llu --> pos = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 222 | // (unsigned long long)nanoTime, |
| 223 | // (unsigned long long)position); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 224 | // ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 225 | // (long long) framesDelta, mFramesPerBurst); |
| 226 | return position; |
| 227 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 228 | |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 229 | int32_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 | |
| 235 | int64_t IsochronousClockModel::convertPositionToLatestTime(int64_t framePosition) const { |
| 236 | return convertPositionToTime(framePosition) + getLateTimeOffsetNanos(); |
| 237 | } |
| 238 | |
| 239 | int64_t IsochronousClockModel::convertLatestTimeToPosition(int64_t nanoTime) const { |
| 240 | return convertTimeToPosition(nanoTime - getLateTimeOffsetNanos()); |
| 241 | } |
| 242 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 243 | void IsochronousClockModel::dump() const { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 244 | ALOGD("mMarkerFramePosition = %lld", (long long) mMarkerFramePosition); |
| 245 | ALOGD("mMarkerNanoTime = %lld", (long long) mMarkerNanoTime); |
| 246 | ALOGD("mSampleRate = %6d", mSampleRate); |
| 247 | ALOGD("mFramesPerBurst = %6d", mFramesPerBurst); |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 248 | ALOGD("mMaxMeasuredLatenessNanos = %6d", mMaxMeasuredLatenessNanos); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 249 | ALOGD("mState = %6d", mState); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 250 | } |