blob: efc9c5fdc77790a5b9fb78e0a4f97175fcc83950 [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
20//#include <stdlib.h> // random()
21
Phil Burk5ed503c2017-02-01 09:38:15 -080022#include "IAAudioService.h"
23#include "AAudioServiceDefinitions.h"
Phil Burkdec33ab2017-01-17 14:48:16 -080024#include "AudioStream.h"
25#include "fifo/FifoBuffer.h"
26#include "SharedRingBuffer.h"
27#include "AudioEndpointParcelable.h"
28
Phil Burk5ed503c2017-02-01 09:38:15 -080029namespace aaudio {
Phil Burkdec33ab2017-01-17 14:48:16 -080030
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 */
37class TimestampScheduler
38{
39public:
40 TimestampScheduler() {};
41 virtual ~TimestampScheduler() = default;
42
43 /**
44 * Start the schedule at the given time.
45 */
Phil Burk5ed503c2017-02-01 09:38:15 -080046 void start(aaudio_nanoseconds_t startTime);
Phil Burkdec33ab2017-01-17 14:48:16 -080047
48 /**
49 * Calculate the next time that the read position should be
50 * measured.
51 */
Phil Burk5ed503c2017-02-01 09:38:15 -080052 aaudio_nanoseconds_t nextAbsoluteTime();
Phil Burkdec33ab2017-01-17 14:48:16 -080053
Phil Burk5ed503c2017-02-01 09:38:15 -080054 void setBurstPeriod(aaudio_nanoseconds_t burstPeriod) {
Phil Burkdec33ab2017-01-17 14:48:16 -080055 mBurstPeriod = burstPeriod;
56 }
57
Phil Burk5ed503c2017-02-01 09:38:15 -080058 void setBurstPeriod(aaudio_size_frames_t framesPerBurst,
59 aaudio_sample_rate_t sampleRate) {
60 mBurstPeriod = AAUDIO_NANOS_PER_SECOND * framesPerBurst / sampleRate;
Phil Burkdec33ab2017-01-17 14:48:16 -080061 }
62
Phil Burk5ed503c2017-02-01 09:38:15 -080063 aaudio_nanoseconds_t getBurstPeriod() {
Phil Burkdec33ab2017-01-17 14:48:16 -080064 return mBurstPeriod;
65 }
66
67private:
68 // Start with an arbitrary default so we do not divide by zero.
Phil Burk5ed503c2017-02-01 09:38:15 -080069 aaudio_nanoseconds_t mBurstPeriod = AAUDIO_NANOS_PER_MILLISECOND;
70 aaudio_nanoseconds_t mStartTime;
71 aaudio_nanoseconds_t mLastTime;
Phil Burkdec33ab2017-01-17 14:48:16 -080072};
73
Phil Burk5ed503c2017-02-01 09:38:15 -080074} /* namespace aaudio */
Phil Burkdec33ab2017-01-17 14:48:16 -080075
Phil Burk5ed503c2017-02-01 09:38:15 -080076#endif /* AAUDIO_TIMESTAMP_SCHEDULER_H */