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 | #ifndef AAUDIO_ISOCHRONOUSCLOCKMODEL_H |
| 18 | #define AAUDIO_ISOCHRONOUSCLOCKMODEL_H |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 21 | #include <aaudio/AAudio.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 | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 40 | void processTimestamp(int64_t framePosition, int64_t nanoTime); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * @param sampleRate rate of the stream in frames per second |
| 44 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 45 | void setSampleRate(int32_t sampleRate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 46 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 47 | int32_t getSampleRate() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 48 | return mSampleRate; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * This must be set accurately in order to track the isochronous stream. |
| 53 | * |
| 54 | * @param framesPerBurst number of frames that stream advance at one time. |
| 55 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 56 | void setFramesPerBurst(int32_t framesPerBurst); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 57 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 58 | int32_t getFramesPerBurst() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 59 | return mFramesPerBurst; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Calculate an estimated time when the stream will be at that position. |
| 64 | * |
| 65 | * @param framePosition position of the stream in frames |
| 66 | * @return time in nanoseconds |
| 67 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 68 | int64_t convertPositionToTime(int64_t framePosition) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Calculate an estimated position where the stream will be at the specified time. |
| 72 | * |
| 73 | * @param nanoTime time of interest |
| 74 | * @return position in frames |
| 75 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 76 | int64_t convertTimeToPosition(int64_t nanoTime) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * @param framesDelta difference in frames |
| 80 | * @return duration in nanoseconds |
| 81 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 82 | int64_t convertDeltaPositionToTime(int64_t framesDelta) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * @param nanosDelta duration in nanoseconds |
| 86 | * @return frames that stream will advance in that time |
| 87 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 88 | int64_t convertDeltaTimeToPosition(int64_t nanosDelta) const; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | enum clock_model_state_t { |
| 92 | STATE_STOPPED, |
| 93 | STATE_STARTING, |
| 94 | STATE_SYNCING, |
| 95 | STATE_RUNNING |
| 96 | }; |
| 97 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 98 | int64_t mMarkerFramePosition; |
| 99 | int64_t mMarkerNanoTime; |
| 100 | int32_t mSampleRate; |
| 101 | int32_t mFramesPerBurst; |
| 102 | int32_t mMaxLatenessInNanos; |
| 103 | clock_model_state_t mState; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 104 | |
| 105 | void update(); |
| 106 | }; |
| 107 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 108 | } /* namespace aaudio */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 109 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 110 | #endif //AAUDIO_ISOCHRONOUSCLOCKMODEL_H |