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 | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H |
| 18 | #define ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Phil Burk | ef34be5 | 2019-09-26 13:45:25 -0700 | [diff] [blame] | 21 | |
| 22 | #include <audio_utils/Histogram.h> |
| 23 | |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 24 | #include "utility/AudioClock.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 25 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | namespace aaudio { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Model an isochronous data stream using occasional timestamps as input. |
| 30 | * This can be used to predict the position of the stream at a given time. |
| 31 | * |
| 32 | * This class is not thread safe and should only be called from one thread. |
| 33 | */ |
| 34 | class IsochronousClockModel { |
| 35 | |
| 36 | public: |
| 37 | IsochronousClockModel(); |
Phil Burk | 9e1f44b | 2020-04-08 16:02:05 -0700 | [diff] [blame^] | 38 | virtual ~IsochronousClockModel() = default; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 39 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 40 | void start(int64_t nanoTime); |
| 41 | void stop(int64_t nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 42 | |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 43 | /** |
| 44 | * @return true if the model is starting up |
| 45 | */ |
| 46 | bool isStarting() const; |
| 47 | |
| 48 | /** |
| 49 | * @return true if the model is running and producing valid results |
| 50 | */ |
| 51 | bool isRunning() const; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 52 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 53 | void processTimestamp(int64_t framePosition, int64_t nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * @param sampleRate rate of the stream in frames per second |
| 57 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 58 | void setSampleRate(int32_t sampleRate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 59 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 60 | void setPositionAndTime(int64_t framePosition, int64_t nanoTime); |
| 61 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 62 | int32_t getSampleRate() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 63 | return mSampleRate; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * This must be set accurately in order to track the isochronous stream. |
| 68 | * |
| 69 | * @param framesPerBurst number of frames that stream advance at one time. |
| 70 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 71 | void setFramesPerBurst(int32_t framesPerBurst); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 73 | int32_t getFramesPerBurst() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 74 | return mFramesPerBurst; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Calculate an estimated time when the stream will be at that position. |
| 79 | * |
| 80 | * @param framePosition position of the stream in frames |
| 81 | * @return time in nanoseconds |
| 82 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 83 | int64_t convertPositionToTime(int64_t framePosition) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 86 | * Calculate the latest estimated time that the stream will be at that position. |
| 87 | * The more jittery the clock is then the later this will be. |
| 88 | * |
| 89 | * @param framePosition |
| 90 | * @return time in nanoseconds |
| 91 | */ |
| 92 | int64_t convertPositionToLatestTime(int64_t framePosition) const; |
| 93 | |
| 94 | /** |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 95 | * Calculate an estimated position where the stream will be at the specified time. |
| 96 | * |
| 97 | * @param nanoTime time of interest |
| 98 | * @return position in frames |
| 99 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 100 | int64_t convertTimeToPosition(int64_t nanoTime) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 103 | * Calculate the corresponding estimated position based on the specified time being |
| 104 | * the latest possible time. |
| 105 | * |
| 106 | * For the same nanoTime, this may return an earlier position than |
| 107 | * convertTimeToPosition(). |
| 108 | * |
| 109 | * @param nanoTime |
| 110 | * @return position in frames |
| 111 | */ |
| 112 | int64_t convertLatestTimeToPosition(int64_t nanoTime) const; |
| 113 | |
| 114 | /** |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 115 | * @param framesDelta difference in frames |
| 116 | * @return duration in nanoseconds |
| 117 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 118 | int64_t convertDeltaPositionToTime(int64_t framesDelta) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * @param nanosDelta duration in nanoseconds |
| 122 | * @return frames that stream will advance in that time |
| 123 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 124 | int64_t convertDeltaTimeToPosition(int64_t nanosDelta) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 125 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 126 | void dump() const; |
| 127 | |
Phil Burk | ef34be5 | 2019-09-26 13:45:25 -0700 | [diff] [blame] | 128 | void dumpHistogram() const; |
| 129 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 130 | private: |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 131 | |
| 132 | int32_t getLateTimeOffsetNanos() const; |
Phil Burk | 9e1f44b | 2020-04-08 16:02:05 -0700 | [diff] [blame^] | 133 | void update(); |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 134 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 135 | enum clock_model_state_t { |
| 136 | STATE_STOPPED, |
| 137 | STATE_STARTING, |
| 138 | STATE_SYNCING, |
| 139 | STATE_RUNNING |
| 140 | }; |
| 141 | |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 142 | // Amount of time to drift forward when we get a late timestamp. |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 143 | static constexpr int32_t kDriftNanos = 1 * 1000; |
| 144 | // Safety margin to add to the late edge of the timestamp window. |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 145 | static constexpr int32_t kExtraLatenessNanos = 100 * 1000; |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 146 | // Initial small threshold for causing a drift later in time. |
| 147 | static constexpr int32_t kInitialLatenessForDriftNanos = 10 * 1000; |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 148 | |
Phil Burk | ef34be5 | 2019-09-26 13:45:25 -0700 | [diff] [blame] | 149 | static constexpr int32_t kHistogramBinWidthMicros = 50; |
| 150 | static constexpr int32_t kHistogramBinCount = 128; |
| 151 | |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 152 | int64_t mMarkerFramePosition; // Estimated HW position. |
| 153 | int64_t mMarkerNanoTime; // Estimated HW time. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 154 | int32_t mSampleRate; |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 155 | int32_t mFramesPerBurst; // number of frames transferred at one time. |
| 156 | int32_t mBurstPeriodNanos; // Time between HW bursts. |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 157 | // Includes mBurstPeriodNanos because we sample randomly over time. |
| 158 | int32_t mMaxMeasuredLatenessNanos; |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 159 | // Threshold for lateness that triggers a drift later in time. |
| 160 | int32_t mLatenessForDriftNanos; |
| 161 | clock_model_state_t mState; // State machine handles startup sequence. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 162 | |
Phil Burk | 34e2d2d | 2019-09-26 12:46:10 -0700 | [diff] [blame] | 163 | int32_t mTimestampCount = 0; // For logging. |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 164 | |
Phil Burk | ef34be5 | 2019-09-26 13:45:25 -0700 | [diff] [blame] | 165 | // distribution of timestamps relative to earliest |
| 166 | std::unique_ptr<android::audio_utils::Histogram> mHistogramMicros; |
| 167 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 170 | } /* namespace aaudio */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 171 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 172 | #endif //ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H |