blob: 524c286d63741130122055e775da47eb7c16af13 [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -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_ISOCHRONOUSCLOCKMODEL_H
18#define AAUDIO_ISOCHRONOUSCLOCKMODEL_H
Phil Burk204a1632017-01-03 17:23:43 -080019
20#include <stdint.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080021#include <aaudio/AAudio.h>
Phil Burk204a1632017-01-03 17:23:43 -080022
Phil Burk5ed503c2017-02-01 09:38:15 -080023namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080024
25/**
26 * Model an isochronous data stream using occasional timestamps as input.
27 * This can be used to predict the position of the stream at a given time.
28 *
29 * This class is not thread safe and should only be called from one thread.
30 */
31class IsochronousClockModel {
32
33public:
34 IsochronousClockModel();
35 virtual ~IsochronousClockModel();
36
Phil Burk3316d5e2017-02-15 11:23:01 -080037 void start(int64_t nanoTime);
38 void stop(int64_t nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080039
Phil Burk3316d5e2017-02-15 11:23:01 -080040 void processTimestamp(int64_t framePosition, int64_t nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080041
42 /**
43 * @param sampleRate rate of the stream in frames per second
44 */
Phil Burk3316d5e2017-02-15 11:23:01 -080045 void setSampleRate(int32_t sampleRate);
Phil Burk204a1632017-01-03 17:23:43 -080046
Phil Burk3316d5e2017-02-15 11:23:01 -080047 int32_t getSampleRate() const {
Phil Burk204a1632017-01-03 17:23:43 -080048 return mSampleRate;
49 }
50
51 /**
52 * This must be set accurately in order to track the isochronous stream.
53 *
54 * @param framesPerBurst number of frames that stream advance at one time.
55 */
Phil Burk3316d5e2017-02-15 11:23:01 -080056 void setFramesPerBurst(int32_t framesPerBurst);
Phil Burk204a1632017-01-03 17:23:43 -080057
Phil Burk3316d5e2017-02-15 11:23:01 -080058 int32_t getFramesPerBurst() const {
Phil Burk204a1632017-01-03 17:23:43 -080059 return mFramesPerBurst;
60 }
61
62 /**
63 * Calculate an estimated time when the stream will be at that position.
64 *
65 * @param framePosition position of the stream in frames
66 * @return time in nanoseconds
67 */
Phil Burk3316d5e2017-02-15 11:23:01 -080068 int64_t convertPositionToTime(int64_t framePosition) const;
Phil Burk204a1632017-01-03 17:23:43 -080069
70 /**
71 * Calculate an estimated position where the stream will be at the specified time.
72 *
73 * @param nanoTime time of interest
74 * @return position in frames
75 */
Phil Burk3316d5e2017-02-15 11:23:01 -080076 int64_t convertTimeToPosition(int64_t nanoTime) const;
Phil Burk204a1632017-01-03 17:23:43 -080077
78 /**
79 * @param framesDelta difference in frames
80 * @return duration in nanoseconds
81 */
Phil Burk3316d5e2017-02-15 11:23:01 -080082 int64_t convertDeltaPositionToTime(int64_t framesDelta) const;
Phil Burk204a1632017-01-03 17:23:43 -080083
84 /**
85 * @param nanosDelta duration in nanoseconds
86 * @return frames that stream will advance in that time
87 */
Phil Burk3316d5e2017-02-15 11:23:01 -080088 int64_t convertDeltaTimeToPosition(int64_t nanosDelta) const;
Phil Burk204a1632017-01-03 17:23:43 -080089
90private:
91 enum clock_model_state_t {
92 STATE_STOPPED,
93 STATE_STARTING,
94 STATE_SYNCING,
95 STATE_RUNNING
96 };
97
Phil Burk3316d5e2017-02-15 11:23:01 -080098 int64_t mMarkerFramePosition;
99 int64_t mMarkerNanoTime;
100 int32_t mSampleRate;
101 int32_t mFramesPerBurst;
102 int32_t mMaxLatenessInNanos;
103 clock_model_state_t mState;
Phil Burk204a1632017-01-03 17:23:43 -0800104
105 void update();
106};
107
Phil Burk5ed503c2017-02-01 09:38:15 -0800108} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800109
Phil Burk5ed503c2017-02-01 09:38:15 -0800110#endif //AAUDIO_ISOCHRONOUSCLOCKMODEL_H