Phil Burk | dec33ab | 2017-01-17 14:48:16 -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_TIMESTAMP_SCHEDULER_H |
| 18 | #define AAUDIO_TIMESTAMP_SCHEDULER_H |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 19 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame^] | 20 | #include <aaudio/AAudio.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 21 | #include <utility/AudioClock.h> |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 22 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 23 | namespace aaudio { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Schedule wakeup time for monitoring the position |
| 27 | * of an MMAP/NOIRQ buffer. |
| 28 | * |
| 29 | * Note that this object is not thread safe. Only call it from a single thread. |
| 30 | */ |
| 31 | class TimestampScheduler |
| 32 | { |
| 33 | public: |
| 34 | TimestampScheduler() {}; |
| 35 | virtual ~TimestampScheduler() = default; |
| 36 | |
| 37 | /** |
| 38 | * Start the schedule at the given time. |
| 39 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 40 | void start(int64_t startTime); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 43 | * Calculate the next time that the read position should be measured. |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 44 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 45 | int64_t nextAbsoluteTime(); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 46 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 47 | void setBurstPeriod(int64_t burstPeriod) { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 48 | mBurstPeriod = burstPeriod; |
| 49 | } |
| 50 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 51 | void setBurstPeriod(int32_t framesPerBurst, |
| 52 | int32_t sampleRate) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 53 | mBurstPeriod = AAUDIO_NANOS_PER_SECOND * framesPerBurst / sampleRate; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 56 | int64_t getBurstPeriod() { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 57 | return mBurstPeriod; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | // Start with an arbitrary default so we do not divide by zero. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 62 | int64_t mBurstPeriod = AAUDIO_NANOS_PER_MILLISECOND; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 63 | int64_t mStartTime = 0; |
| 64 | int64_t mLastTime = 0; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 67 | } /* namespace aaudio */ |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 68 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 69 | #endif /* AAUDIO_TIMESTAMP_SCHEDULER_H */ |