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 | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 22 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 23 | #include "utility/AudioClock.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 24 | #include "IsochronousClockModel.h" |
| 25 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | #define MIN_LATENESS_NANOS (10 * AAUDIO_NANOS_PER_MICROSECOND) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 27 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 28 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 29 | |
| 30 | IsochronousClockModel::IsochronousClockModel() |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 31 | : mMarkerFramePosition(0) |
| 32 | , mMarkerNanoTime(0) |
| 33 | , mSampleRate(48000) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 34 | , mFramesPerBurst(64) |
| 35 | , mMaxLatenessInNanos(0) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 36 | , mState(STATE_STOPPED) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | IsochronousClockModel::~IsochronousClockModel() { |
| 41 | } |
| 42 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 43 | void IsochronousClockModel::setPositionAndTime(int64_t framePosition, int64_t nanoTime) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 44 | ALOGV("setPositionAndTime(%lld, %lld)", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 45 | (long long) framePosition, (long long) nanoTime); |
| 46 | mMarkerFramePosition = framePosition; |
| 47 | mMarkerNanoTime = nanoTime; |
| 48 | } |
| 49 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 50 | void IsochronousClockModel::start(int64_t nanoTime) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 51 | ALOGV("start(nanos = %lld)\n", (long long) nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 52 | mMarkerNanoTime = nanoTime; |
| 53 | mState = STATE_STARTING; |
| 54 | } |
| 55 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 56 | void IsochronousClockModel::stop(int64_t nanoTime) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 57 | ALOGV("stop(nanos = %lld)\n", (long long) nanoTime); |
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 | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 72 | // ALOGD("processTimestamp() - framePosition = %lld at nanoTime %llu", |
| 73 | // (long long)framePosition, |
| 74 | // (long long)nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 75 | int64_t framesDelta = framePosition - mMarkerFramePosition; |
| 76 | int64_t nanosDelta = nanoTime - mMarkerNanoTime; |
| 77 | if (nanosDelta < 1000) { |
| 78 | return; |
| 79 | } |
| 80 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 81 | // ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 82 | // (long long)mMarkerFramePosition, |
| 83 | // (long long)mMarkerNanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 84 | |
| 85 | int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 86 | // ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 87 | // (long long)expectedNanosDelta, |
| 88 | // (long long)nanosDelta); |
| 89 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 90 | // ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate); |
| 91 | // ALOGD("processTimestamp() - mState = %d", mState); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 92 | switch (mState) { |
| 93 | case STATE_STOPPED: |
| 94 | break; |
| 95 | case STATE_STARTING: |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 96 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 97 | mState = STATE_SYNCING; |
| 98 | break; |
| 99 | case STATE_SYNCING: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 100 | // This will handle a burst of rapid transfer at the beginning. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 101 | if (nanosDelta < expectedNanosDelta) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 102 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 103 | } else { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 104 | // ALOGD("processTimestamp() - advance to STATE_RUNNING"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 105 | mState = STATE_RUNNING; |
| 106 | } |
| 107 | break; |
| 108 | case STATE_RUNNING: |
| 109 | if (nanosDelta < expectedNanosDelta) { |
| 110 | // Earlier than expected timestamp. |
| 111 | // This data is probably more accurate so use it. |
| 112 | // or we may be drifting due to a slow HW clock. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 113 | // ALOGD("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY", |
| 114 | // (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 115 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 116 | } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) { |
| 117 | // Later than expected timestamp. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 118 | // ALOGD("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE", |
| 119 | // (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000), |
| 120 | // (int) (mMaxLatenessInNanos / 1000)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 121 | setPositionAndTime(framePosition - mFramesPerBurst, nanoTime - mMaxLatenessInNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 122 | } |
| 123 | break; |
| 124 | default: |
| 125 | break; |
| 126 | } |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 127 | |
| 128 | // ALOGD("processTimestamp() - mState = %d", mState); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void IsochronousClockModel::setSampleRate(int32_t sampleRate) { |
| 132 | mSampleRate = sampleRate; |
| 133 | update(); |
| 134 | } |
| 135 | |
| 136 | void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) { |
| 137 | mFramesPerBurst = framesPerBurst; |
| 138 | update(); |
| 139 | } |
| 140 | |
| 141 | void IsochronousClockModel::update() { |
| 142 | int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate |
| 143 | mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS; |
| 144 | } |
| 145 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 146 | int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 147 | return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 150 | int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 151 | return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 154 | int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 155 | if (mState == STATE_STOPPED) { |
| 156 | return mMarkerNanoTime; |
| 157 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 158 | int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst; |
| 159 | int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex; |
| 160 | int64_t framesDelta = nextBurstPosition - mMarkerFramePosition; |
| 161 | int64_t nanosDelta = convertDeltaPositionToTime(framesDelta); |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 162 | int64_t time = mMarkerNanoTime + nanosDelta; |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 163 | // ALOGD("convertPositionToTime: pos = %llu --> time = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 164 | // (unsigned long long)framePosition, |
| 165 | // (unsigned long long)time); |
| 166 | return time; |
| 167 | } |
| 168 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 169 | int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 170 | if (mState == STATE_STOPPED) { |
| 171 | return mMarkerFramePosition; |
| 172 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 173 | int64_t nanosDelta = nanoTime - mMarkerNanoTime; |
| 174 | int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta); |
| 175 | int64_t nextBurstPosition = mMarkerFramePosition + framesDelta; |
| 176 | int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst; |
| 177 | int64_t position = nextBurstIndex * mFramesPerBurst; |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 178 | // ALOGD("convertTimeToPosition: time = %llu --> pos = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 179 | // (unsigned long long)nanoTime, |
| 180 | // (unsigned long long)position); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 181 | // ALOGD("convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 182 | // (long long) framesDelta, mFramesPerBurst); |
| 183 | return position; |
| 184 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 185 | |
| 186 | void IsochronousClockModel::dump() const { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 187 | ALOGD("mMarkerFramePosition = %lld", (long long) mMarkerFramePosition); |
| 188 | ALOGD("mMarkerNanoTime = %lld", (long long) mMarkerNanoTime); |
| 189 | ALOGD("mSampleRate = %6d", mSampleRate); |
| 190 | ALOGD("mFramesPerBurst = %6d", mFramesPerBurst); |
| 191 | ALOGD("mMaxLatenessInNanos = %6d", mMaxLatenessInNanos); |
| 192 | ALOGD("mState = %6d", mState); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 193 | } |