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