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