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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 22 | #include <aaudio/AAudioDefinitions.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 23 | |
| 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() |
| 32 | : mSampleRate(48000) |
| 33 | , mFramesPerBurst(64) |
| 34 | , mMaxLatenessInNanos(0) |
| 35 | , mMarkerFramePosition(0) |
| 36 | , mMarkerNanoTime(0) |
| 37 | , mState(STATE_STOPPED) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | IsochronousClockModel::~IsochronousClockModel() { |
| 42 | } |
| 43 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 44 | void IsochronousClockModel::start(aaudio_nanoseconds_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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | void IsochronousClockModel::stop(aaudio_nanoseconds_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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 57 | void IsochronousClockModel::processTimestamp(aaudio_position_frames_t framePosition, |
| 58 | aaudio_nanoseconds_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; |
| 104 | ALOGI("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY", |
| 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; |
| 110 | ALOGI("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE", |
| 111 | (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000), |
| 112 | (int) (mMaxLatenessInNanos / 1000)); |
| 113 | } |
| 114 | break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
| 118 | ++mTimestampCount; |
| 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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 136 | aaudio_nanoseconds_t IsochronousClockModel::convertDeltaPositionToTime( |
| 137 | aaudio_position_frames_t framesDelta) const { |
| 138 | return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 141 | int64_t IsochronousClockModel::convertDeltaTimeToPosition(aaudio_nanoseconds_t nanosDelta) const { |
| 142 | return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 145 | aaudio_nanoseconds_t IsochronousClockModel::convertPositionToTime( |
| 146 | aaudio_position_frames_t framePosition) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 147 | if (mState == STATE_STOPPED) { |
| 148 | return mMarkerNanoTime; |
| 149 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 150 | aaudio_position_frames_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst; |
| 151 | aaudio_position_frames_t nextBurstPosition = mFramesPerBurst * nextBurstIndex; |
| 152 | aaudio_position_frames_t framesDelta = nextBurstPosition - mMarkerFramePosition; |
| 153 | aaudio_nanoseconds_t nanosDelta = convertDeltaPositionToTime(framesDelta); |
| 154 | aaudio_nanoseconds_t time = (aaudio_nanoseconds_t) (mMarkerNanoTime + nanosDelta); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 155 | // ALOGI("IsochronousClockModel::convertPositionToTime: pos = %llu --> time = %llu", |
| 156 | // (unsigned long long)framePosition, |
| 157 | // (unsigned long long)time); |
| 158 | return time; |
| 159 | } |
| 160 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 161 | aaudio_position_frames_t IsochronousClockModel::convertTimeToPosition( |
| 162 | aaudio_nanoseconds_t nanoTime) const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 163 | if (mState == STATE_STOPPED) { |
| 164 | return mMarkerFramePosition; |
| 165 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 166 | aaudio_nanoseconds_t nanosDelta = nanoTime - mMarkerNanoTime; |
| 167 | aaudio_position_frames_t framesDelta = convertDeltaTimeToPosition(nanosDelta); |
| 168 | aaudio_position_frames_t nextBurstPosition = mMarkerFramePosition + framesDelta; |
| 169 | aaudio_position_frames_t nextBurstIndex = nextBurstPosition / mFramesPerBurst; |
| 170 | aaudio_position_frames_t position = nextBurstIndex * mFramesPerBurst; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 171 | // ALOGI("IsochronousClockModel::convertTimeToPosition: time = %llu --> pos = %llu", |
| 172 | // (unsigned long long)nanoTime, |
| 173 | // (unsigned long long)position); |
| 174 | // ALOGI("IsochronousClockModel::convertTimeToPosition: framesDelta = %llu, mFramesPerBurst = %d", |
| 175 | // (long long) framesDelta, mFramesPerBurst); |
| 176 | return position; |
| 177 | } |