IsochronousClockModel.h revision 3316d5e6d375a4f09c681205e9094d30a0bfc4a2
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_ISOCHRONOUSCLOCKMODEL_H
18#define AAUDIO_ISOCHRONOUSCLOCKMODEL_H
19
20#include <stdint.h>
21#include <aaudio/AAudio.h>
22
23namespace aaudio {
24
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
37    void start(int64_t nanoTime);
38    void stop(int64_t nanoTime);
39
40    void processTimestamp(int64_t framePosition, int64_t nanoTime);
41
42    /**
43     * @param sampleRate rate of the stream in frames per second
44     */
45    void setSampleRate(int32_t sampleRate);
46
47    int32_t getSampleRate() const {
48        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     */
56    void setFramesPerBurst(int32_t framesPerBurst);
57
58    int32_t getFramesPerBurst() const {
59        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     */
68    int64_t convertPositionToTime(int64_t framePosition) const;
69
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     */
76    int64_t convertTimeToPosition(int64_t nanoTime) const;
77
78    /**
79     * @param framesDelta difference in frames
80     * @return duration in nanoseconds
81     */
82    int64_t convertDeltaPositionToTime(int64_t framesDelta) const;
83
84    /**
85     * @param nanosDelta duration in nanoseconds
86     * @return frames that stream will advance in that time
87     */
88    int64_t convertDeltaTimeToPosition(int64_t nanosDelta) const;
89
90private:
91    enum clock_model_state_t {
92        STATE_STOPPED,
93        STATE_STARTING,
94        STATE_SYNCING,
95        STATE_RUNNING
96    };
97
98    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;
104
105    void update();
106};
107
108} /* namespace aaudio */
109
110#endif //AAUDIO_ISOCHRONOUSCLOCKMODEL_H
111