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