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 
17 #ifndef AAUDIO_TIMESTAMP_SCHEDULER_H
18 #define AAUDIO_TIMESTAMP_SCHEDULER_H
19 
20 #include <aaudio/AAudio.h>
21 #include <utility/AudioClock.h>
22 
23 namespace aaudio {
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:
TimestampScheduler()34     TimestampScheduler() {};
35     virtual ~TimestampScheduler() = default;
36 
37     /**
38      * Start the schedule at the given time.
39      */
40     void start(int64_t startTime);
41 
42     /**
43      * Calculate the next time that the read position should be measured.
44      */
45     int64_t nextAbsoluteTime();
46 
setBurstPeriod(int64_t burstPeriod)47     void setBurstPeriod(int64_t burstPeriod) {
48         mBurstPeriod = burstPeriod;
49     }
50 
setBurstPeriod(int32_t framesPerBurst,int32_t sampleRate)51     void setBurstPeriod(int32_t framesPerBurst,
52                         int32_t sampleRate) {
53         mBurstPeriod = AAUDIO_NANOS_PER_SECOND * framesPerBurst / sampleRate;
54     }
55 
getBurstPeriod()56     int64_t getBurstPeriod() {
57         return mBurstPeriod;
58     }
59 
60 private:
61     // Start with an arbitrary default so we do not divide by zero.
62     int64_t mBurstPeriod = AAUDIO_NANOS_PER_MILLISECOND;
63     int64_t mStartTime = 0;
64     int64_t mLastTime = 0;
65 };
66 
67 } /* namespace aaudio */
68 
69 #endif /* AAUDIO_TIMESTAMP_SCHEDULER_H */
70