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 | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 21 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 22 | namespace aaudio { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Model an isochronous data stream using occasional timestamps as input. |
| 26 | * This can be used to predict the position of the stream at a given time. |
| 27 | * |
| 28 | * This class is not thread safe and should only be called from one thread. |
| 29 | */ |
| 30 | class IsochronousClockModel { |
| 31 | |
| 32 | public: |
| 33 | IsochronousClockModel(); |
| 34 | virtual ~IsochronousClockModel(); |
| 35 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 36 | void start(int64_t nanoTime); |
| 37 | void stop(int64_t nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 38 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 39 | void processTimestamp(int64_t framePosition, int64_t nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * @param sampleRate rate of the stream in frames per second |
| 43 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 44 | void setSampleRate(int32_t sampleRate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 45 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 46 | void setPositionAndTime(int64_t framePosition, int64_t nanoTime); |
| 47 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 48 | int32_t getSampleRate() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 49 | return mSampleRate; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * This must be set accurately in order to track the isochronous stream. |
| 54 | * |
| 55 | * @param framesPerBurst number of frames that stream advance at one time. |
| 56 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 57 | void setFramesPerBurst(int32_t framesPerBurst); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 58 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 59 | int32_t getFramesPerBurst() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 60 | return mFramesPerBurst; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Calculate an estimated time when the stream will be at that position. |
| 65 | * |
| 66 | * @param framePosition position of the stream in frames |
| 67 | * @return time in nanoseconds |
| 68 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 69 | int64_t convertPositionToTime(int64_t framePosition) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Calculate an estimated position where the stream will be at the specified time. |
| 73 | * |
| 74 | * @param nanoTime time of interest |
| 75 | * @return position in frames |
| 76 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 77 | int64_t convertTimeToPosition(int64_t nanoTime) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @param framesDelta difference in frames |
| 81 | * @return duration in nanoseconds |
| 82 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 83 | int64_t convertDeltaPositionToTime(int64_t framesDelta) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * @param nanosDelta duration in nanoseconds |
| 87 | * @return frames that stream will advance in that time |
| 88 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 89 | int64_t convertDeltaTimeToPosition(int64_t nanosDelta) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 90 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 91 | void dump() const; |
| 92 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | private: |
| 94 | enum clock_model_state_t { |
| 95 | STATE_STOPPED, |
| 96 | STATE_STARTING, |
| 97 | STATE_SYNCING, |
| 98 | STATE_RUNNING |
| 99 | }; |
| 100 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 101 | int64_t mMarkerFramePosition; |
| 102 | int64_t mMarkerNanoTime; |
| 103 | int32_t mSampleRate; |
| 104 | int32_t mFramesPerBurst; |
| 105 | int32_t mMaxLatenessInNanos; |
| 106 | clock_model_state_t mState; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 107 | |
| 108 | void update(); |
| 109 | }; |
| 110 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 111 | } /* namespace aaudio */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 112 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 113 | #endif //ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H |