blob: baa5c41b60fc03d83b9331791bb1841e856a73c9 [file] [log] [blame]
Phil Burkdec33ab2017-01-17 14:48:16 -08001/*
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 Burk5ed503c2017-02-01 09:38:15 -080017#ifndef AAUDIO_TIMESTAMP_SCHEDULER_H
18#define AAUDIO_TIMESTAMP_SCHEDULER_H
Phil Burkdec33ab2017-01-17 14:48:16 -080019
Phil Burka4eb0d82017-04-12 15:44:06 -070020#include <aaudio/AAudio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080021#include <utility/AudioClock.h>
Phil Burkdec33ab2017-01-17 14:48:16 -080022
Phil Burk5ed503c2017-02-01 09:38:15 -080023namespace aaudio {
Phil Burkdec33ab2017-01-17 14:48:16 -080024
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 */
31class TimestampScheduler
32{
33public:
34 TimestampScheduler() {};
35 virtual ~TimestampScheduler() = default;
36
37 /**
38 * Start the schedule at the given time.
39 */
Phil Burk3316d5e2017-02-15 11:23:01 -080040 void start(int64_t startTime);
Phil Burkdec33ab2017-01-17 14:48:16 -080041
42 /**
Phil Burkc0c70e32017-02-09 13:18:38 -080043 * Calculate the next time that the read position should be measured.
Phil Burkdec33ab2017-01-17 14:48:16 -080044 */
Phil Burk3316d5e2017-02-15 11:23:01 -080045 int64_t nextAbsoluteTime();
Phil Burkdec33ab2017-01-17 14:48:16 -080046
Phil Burk3316d5e2017-02-15 11:23:01 -080047 void setBurstPeriod(int64_t burstPeriod) {
Phil Burkdec33ab2017-01-17 14:48:16 -080048 mBurstPeriod = burstPeriod;
49 }
50
Phil Burk3316d5e2017-02-15 11:23:01 -080051 void setBurstPeriod(int32_t framesPerBurst,
52 int32_t sampleRate) {
Phil Burk5ed503c2017-02-01 09:38:15 -080053 mBurstPeriod = AAUDIO_NANOS_PER_SECOND * framesPerBurst / sampleRate;
Phil Burkdec33ab2017-01-17 14:48:16 -080054 }
55
Phil Burk3316d5e2017-02-15 11:23:01 -080056 int64_t getBurstPeriod() {
Phil Burkdec33ab2017-01-17 14:48:16 -080057 return mBurstPeriod;
58 }
59
60private:
61 // Start with an arbitrary default so we do not divide by zero.
Phil Burk3316d5e2017-02-15 11:23:01 -080062 int64_t mBurstPeriod = AAUDIO_NANOS_PER_MILLISECOND;
Phil Burkc0c70e32017-02-09 13:18:38 -080063 int64_t mStartTime = 0;
64 int64_t mLastTime = 0;
Phil Burkdec33ab2017-01-17 14:48:16 -080065};
66
Phil Burk5ed503c2017-02-01 09:38:15 -080067} /* namespace aaudio */
Phil Burkdec33ab2017-01-17 14:48:16 -080068
Phil Burk5ed503c2017-02-01 09:38:15 -080069#endif /* AAUDIO_TIMESTAMP_SCHEDULER_H */