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