blob: 91a24771ee2dabdbd49fba20d9722ba8cae1fe5e [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 Burk3316d5e2017-02-15 11:23:01 -080020
Phil Burkdec33ab2017-01-17 14:48:16 -080021
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"
Phil Burk3316d5e2017-02-15 11:23:01 -080028#include "utility/AudioClock.h"
Phil Burkdec33ab2017-01-17 14:48:16 -080029
Phil Burk5ed503c2017-02-01 09:38:15 -080030namespace aaudio {
Phil Burkdec33ab2017-01-17 14:48:16 -080031
32/**
33 * Schedule wakeup time for monitoring the position
34 * of an MMAP/NOIRQ buffer.
35 *
36 * Note that this object is not thread safe. Only call it from a single thread.
37 */
38class TimestampScheduler
39{
40public:
41 TimestampScheduler() {};
42 virtual ~TimestampScheduler() = default;
43
44 /**
45 * Start the schedule at the given time.
46 */
Phil Burk3316d5e2017-02-15 11:23:01 -080047 void start(int64_t startTime);
Phil Burkdec33ab2017-01-17 14:48:16 -080048
49 /**
50 * Calculate the next time that the read position should be
51 * measured.
52 */
Phil Burk3316d5e2017-02-15 11:23:01 -080053 int64_t nextAbsoluteTime();
Phil Burkdec33ab2017-01-17 14:48:16 -080054
Phil Burk3316d5e2017-02-15 11:23:01 -080055 void setBurstPeriod(int64_t burstPeriod) {
Phil Burkdec33ab2017-01-17 14:48:16 -080056 mBurstPeriod = burstPeriod;
57 }
58
Phil Burk3316d5e2017-02-15 11:23:01 -080059 void setBurstPeriod(int32_t framesPerBurst,
60 int32_t sampleRate) {
Phil Burk5ed503c2017-02-01 09:38:15 -080061 mBurstPeriod = AAUDIO_NANOS_PER_SECOND * framesPerBurst / sampleRate;
Phil Burkdec33ab2017-01-17 14:48:16 -080062 }
63
Phil Burk3316d5e2017-02-15 11:23:01 -080064 int64_t getBurstPeriod() {
Phil Burkdec33ab2017-01-17 14:48:16 -080065 return mBurstPeriod;
66 }
67
68private:
69 // Start with an arbitrary default so we do not divide by zero.
Phil Burk3316d5e2017-02-15 11:23:01 -080070 int64_t mBurstPeriod = AAUDIO_NANOS_PER_MILLISECOND;
71 int64_t mStartTime;
72 int64_t mLastTime;
Phil Burkdec33ab2017-01-17 14:48:16 -080073};
74
Phil Burk5ed503c2017-02-01 09:38:15 -080075} /* namespace aaudio */
Phil Burkdec33ab2017-01-17 14:48:16 -080076
Phil Burk5ed503c2017-02-01 09:38:15 -080077#endif /* AAUDIO_TIMESTAMP_SCHEDULER_H */