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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 17 | #define LOG_TAG "AAudio" |
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) { |
| 44 | ALOGV("IsochronousClockModel::setPositionAndTime(%lld, %lld)", |
| 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) { |
| 51 | ALOGD("IsochronousClockModel::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) { |
| 57 | ALOGD("IsochronousClockModel::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 | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 63 | void IsochronousClockModel::processTimestamp(int64_t framePosition, int64_t nanoTime) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 64 | int64_t framesDelta = framePosition - mMarkerFramePosition; |
| 65 | int64_t nanosDelta = nanoTime - mMarkerNanoTime; |
| 66 | if (nanosDelta < 1000) { |
| 67 | return; |
| 68 | } |
| 69 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 70 | // ALOGD("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 71 | // (long long)mMarkerFramePosition, |
| 72 | // (long long)mMarkerNanoTime); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 73 | // ALOGD("processTimestamp() - framePosition = %lld at nanoTime %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 74 | // (long long)framePosition, |
| 75 | // (long long)nanoTime); |
| 76 | |
| 77 | int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 78 | // ALOGD("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 79 | // (long long)expectedNanosDelta, |
| 80 | // (long long)nanosDelta); |
| 81 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 82 | // ALOGD("processTimestamp() - mSampleRate = %d", mSampleRate); |
| 83 | // ALOGD("processTimestamp() - mState = %d", mState); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 84 | switch (mState) { |
| 85 | case STATE_STOPPED: |
| 86 | break; |
| 87 | case STATE_STARTING: |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 88 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 89 | mState = STATE_SYNCING; |
| 90 | break; |
| 91 | case STATE_SYNCING: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 92 | // This will handle a burst of rapid transfer at the beginning. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | if (nanosDelta < expectedNanosDelta) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 94 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 95 | } else { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 96 | // ALOGD("processTimestamp() - advance to STATE_RUNNING"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 97 | mState = STATE_RUNNING; |
| 98 | } |
| 99 | break; |
| 100 | case STATE_RUNNING: |
| 101 | if (nanosDelta < expectedNanosDelta) { |
| 102 | // Earlier than expected timestamp. |
| 103 | // This data is probably more accurate so use it. |
| 104 | // or we may be drifting due to a slow HW clock. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 105 | // ALOGD("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY", |
| 106 | // (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 107 | setPositionAndTime(framePosition, nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 108 | } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) { |
| 109 | // Later than expected timestamp. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 110 | // ALOGD("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE", |
| 111 | // (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000), |
| 112 | // (int) (mMaxLatenessInNanos / 1000)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 113 | setPositionAndTime(framePosition - mFramesPerBurst, nanoTime - mMaxLatenessInNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | default: |
| 117 | break; |
| 118 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void IsochronousClockModel::setSampleRate(int32_t sampleRate) { |
| 122 | mSampleRate = sampleRate; |
| 123 | update(); |
| 124 | } |
| 125 | |
| 126 | void IsochronousClockModel::setFramesPerBurst(int32_t framesPerBurst) { |
| 127 | mFramesPerBurst = framesPerBurst; |
| 128 | update(); |
| 129 | } |
| 130 | |
| 131 | void IsochronousClockModel::update() { |
| 132 | int64_t nanosLate = convertDeltaPositionToTime(mFramesPerBurst); // uses mSampleRate |
| 133 | mMaxLatenessInNanos = (nanosLate > MIN_LATENESS_NANOS) ? nanosLate : MIN_LATENESS_NANOS; |
| 134 | } |
| 135 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 136 | int64_t IsochronousClockModel::convertDeltaPositionToTime(int64_t framesDelta) const { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 137 | return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 140 | int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 141 | return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 144 | int64_t IsochronousClockModel::convertPositionToTime(int64_t framePosition) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 145 | if (mState == STATE_STOPPED) { |
| 146 | return mMarkerNanoTime; |
| 147 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 148 | int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst; |
| 149 | int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex; |
| 150 | int64_t framesDelta = nextBurstPosition - mMarkerFramePosition; |
| 151 | int64_t nanosDelta = convertDeltaPositionToTime(framesDelta); |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 152 | int64_t time = mMarkerNanoTime + nanosDelta; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 153 | // ALOGD("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 154 | // (unsigned long long)framePosition, |
| 155 | // (unsigned long long)time); |
| 156 | return time; |
| 157 | } |
| 158 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 159 | int64_t IsochronousClockModel::convertTimeToPosition(int64_t nanoTime) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 160 | if (mState == STATE_STOPPED) { |
| 161 | return mMarkerFramePosition; |
| 162 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 163 | int64_t nanosDelta = nanoTime - mMarkerNanoTime; |
| 164 | int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta); |
| 165 | int64_t nextBurstPosition = mMarkerFramePosition + framesDelta; |
| 166 | int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst; |
| 167 | int64_t position = nextBurstIndex * mFramesPerBurst; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 168 | // ALOGD("IsochronousClockModel::convertTimeToPosition: time = %llu --> pos = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 169 | // (unsigned long long)nanoTime, |
| 170 | // (unsigned long long)position); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 171 | // ALOGD("IsochronousClockModel::convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 172 | // (long long) framesDelta, mFramesPerBurst); |
| 173 | return position; |
| 174 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 175 | |
| 176 | void IsochronousClockModel::dump() const { |
| 177 | ALOGD("IsochronousClockModel::mMarkerFramePosition = %lld", (long long) mMarkerFramePosition); |
| 178 | ALOGD("IsochronousClockModel::mMarkerNanoTime = %lld", (long long) mMarkerNanoTime); |
| 179 | ALOGD("IsochronousClockModel::mSampleRate = %6d", mSampleRate); |
| 180 | ALOGD("IsochronousClockModel::mFramesPerBurst = %6d", mFramesPerBurst); |
| 181 | ALOGD("IsochronousClockModel::mMaxLatenessInNanos = %6d", mMaxLatenessInNanos); |
| 182 | ALOGD("IsochronousClockModel::mState = %6d", mState); |
| 183 | } |