| 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 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 24 | #include "utility/AudioClock.h" | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 25 | #include "IsochronousClockModel.h" | 
 | 26 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 27 | #define MIN_LATENESS_NANOS (10 * AAUDIO_NANOS_PER_MICROSECOND) | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 28 |  | 
 | 29 | using namespace android; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 30 | using namespace aaudio; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 31 |  | 
 | 32 | IsochronousClockModel::IsochronousClockModel() | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 33 |         : mMarkerFramePosition(0) | 
 | 34 |         , mMarkerNanoTime(0) | 
 | 35 |         , mSampleRate(48000) | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 36 |         , mFramesPerBurst(64) | 
 | 37 |         , mMaxLatenessInNanos(0) | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 38 |         , mState(STATE_STOPPED) | 
 | 39 | { | 
 | 40 | } | 
 | 41 |  | 
 | 42 | IsochronousClockModel::~IsochronousClockModel() { | 
 | 43 | } | 
 | 44 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 45 | void IsochronousClockModel::start(int64_t nanoTime) | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 46 | { | 
 | 47 |     mMarkerNanoTime = nanoTime; | 
 | 48 |     mState = STATE_STARTING; | 
 | 49 | } | 
 | 50 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 51 | void IsochronousClockModel::stop(int64_t nanoTime) | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 52 | { | 
 | 53 |     mMarkerNanoTime = nanoTime; | 
 | 54 |     mMarkerFramePosition = convertTimeToPosition(nanoTime); // TODO should we do this? | 
 | 55 |     mState = STATE_STOPPED; | 
 | 56 | } | 
 | 57 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 58 | void IsochronousClockModel::processTimestamp(int64_t framePosition, | 
 | 59 |                                              int64_t nanoTime) { | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 60 |     int64_t framesDelta = framePosition - mMarkerFramePosition; | 
 | 61 |     int64_t nanosDelta = nanoTime - mMarkerNanoTime; | 
 | 62 |     if (nanosDelta < 1000) { | 
 | 63 |         return; | 
 | 64 |     } | 
 | 65 |  | 
 | 66 | //    ALOGI("processTimestamp() - mMarkerFramePosition = %lld at mMarkerNanoTime %llu", | 
 | 67 | //         (long long)mMarkerFramePosition, | 
 | 68 | //         (long long)mMarkerNanoTime); | 
 | 69 | //    ALOGI("processTimestamp() - framePosition = %lld at nanoTime %llu", | 
 | 70 | //         (long long)framePosition, | 
 | 71 | //         (long long)nanoTime); | 
 | 72 |  | 
 | 73 |     int64_t expectedNanosDelta = convertDeltaPositionToTime(framesDelta); | 
 | 74 | //    ALOGI("processTimestamp() - expectedNanosDelta = %lld, nanosDelta = %llu", | 
 | 75 | //         (long long)expectedNanosDelta, | 
 | 76 | //         (long long)nanosDelta); | 
 | 77 |  | 
 | 78 | //    ALOGI("processTimestamp() - mSampleRate = %d", mSampleRate); | 
 | 79 | //    ALOGI("processTimestamp() - mState = %d", mState); | 
 | 80 |     switch (mState) { | 
 | 81 |     case STATE_STOPPED: | 
 | 82 |         break; | 
 | 83 |     case STATE_STARTING: | 
 | 84 |         mMarkerFramePosition = framePosition; | 
 | 85 |         mMarkerNanoTime = nanoTime; | 
 | 86 |         mState = STATE_SYNCING; | 
 | 87 |         break; | 
 | 88 |     case STATE_SYNCING: | 
 | 89 |         // This will handle a burst of rapid consumption in the beginning. | 
 | 90 |         if (nanosDelta < expectedNanosDelta) { | 
 | 91 |             mMarkerFramePosition = framePosition; | 
 | 92 |             mMarkerNanoTime = nanoTime; | 
 | 93 |         } else { | 
 | 94 |             ALOGI("processTimestamp() - advance to STATE_RUNNING"); | 
 | 95 |             mState = STATE_RUNNING; | 
 | 96 |         } | 
 | 97 |         break; | 
 | 98 |     case STATE_RUNNING: | 
 | 99 |         if (nanosDelta < expectedNanosDelta) { | 
 | 100 |             // Earlier than expected timestamp. | 
 | 101 |             // This data is probably more accurate so use it. | 
 | 102 |             // or we may be drifting due to a slow HW clock. | 
 | 103 |             mMarkerFramePosition = framePosition; | 
 | 104 |             mMarkerNanoTime = nanoTime; | 
 | 105 |             ALOGI("processTimestamp() - STATE_RUNNING - %d < %d micros - EARLY", | 
 | 106 |                  (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000)); | 
 | 107 |         } else if (nanosDelta > (expectedNanosDelta + mMaxLatenessInNanos)) { | 
 | 108 |             // Later than expected timestamp. | 
 | 109 |             mMarkerFramePosition = framePosition; | 
 | 110 |             mMarkerNanoTime = nanoTime - mMaxLatenessInNanos; | 
 | 111 |             ALOGI("processTimestamp() - STATE_RUNNING - %d > %d + %d micros - LATE", | 
 | 112 |                  (int) (nanosDelta / 1000), (int)(expectedNanosDelta / 1000), | 
 | 113 |                  (int) (mMaxLatenessInNanos / 1000)); | 
 | 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 | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 136 | int64_t IsochronousClockModel::convertDeltaPositionToTime( | 
 | 137 |         int64_t framesDelta) const { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 138 |     return (AAUDIO_NANOS_PER_SECOND * framesDelta) / mSampleRate; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 139 | } | 
 | 140 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 141 | int64_t IsochronousClockModel::convertDeltaTimeToPosition(int64_t nanosDelta) const { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 142 |     return (mSampleRate * nanosDelta) / AAUDIO_NANOS_PER_SECOND; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 143 | } | 
 | 144 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 145 | int64_t IsochronousClockModel::convertPositionToTime( | 
 | 146 |         int64_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 | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 150 |     int64_t nextBurstIndex = (framePosition + mFramesPerBurst - 1) / mFramesPerBurst; | 
 | 151 |     int64_t nextBurstPosition = mFramesPerBurst * nextBurstIndex; | 
 | 152 |     int64_t framesDelta = nextBurstPosition - mMarkerFramePosition; | 
 | 153 |     int64_t nanosDelta = convertDeltaPositionToTime(framesDelta); | 
 | 154 |     int64_t time = (int64_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 | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 161 | int64_t IsochronousClockModel::convertTimeToPosition( | 
 | 162 |         int64_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 | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 166 |     int64_t nanosDelta = nanoTime - mMarkerNanoTime; | 
 | 167 |     int64_t framesDelta = convertDeltaTimeToPosition(nanosDelta); | 
 | 168 |     int64_t nextBurstPosition = mMarkerFramePosition + framesDelta; | 
 | 169 |     int64_t nextBurstIndex = nextBurstPosition / mFramesPerBurst; | 
 | 170 |     int64_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 | } |