blob: 97be325d1d9f39819c03213d0471c73a4dc29ef6 [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
17#ifndef OBOE_ISOCHRONOUSCLOCKMODEL_H
18#define OBOE_ISOCHRONOUSCLOCKMODEL_H
19
20#include <stdint.h>
21#include <oboe/OboeAudio.h>
22
23namespace oboe {
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(oboe_nanoseconds_t nanoTime);
38 void stop(oboe_nanoseconds_t nanoTime);
39
40 void processTimestamp(oboe_position_frames_t framePosition, oboe_nanoseconds_t nanoTime);
41
42 /**
43 * @param sampleRate rate of the stream in frames per second
44 */
45 void setSampleRate(oboe_sample_rate_t sampleRate);
46
47 oboe_sample_rate_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(oboe_size_frames_t framesPerBurst);
57
58 oboe_size_frames_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 oboe_nanoseconds_t convertPositionToTime(oboe_position_frames_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 oboe_position_frames_t convertTimeToPosition(oboe_nanoseconds_t nanoTime) const;
77
78 /**
79 * @param framesDelta difference in frames
80 * @return duration in nanoseconds
81 */
82 oboe_nanoseconds_t convertDeltaPositionToTime(oboe_position_frames_t framesDelta) const;
83
84 /**
85 * @param nanosDelta duration in nanoseconds
86 * @return frames that stream will advance in that time
87 */
88 oboe_position_frames_t convertDeltaTimeToPosition(oboe_nanoseconds_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 oboe_sample_rate_t mSampleRate;
99 oboe_size_frames_t mFramesPerBurst;
100 int32_t mMaxLatenessInNanos;
101 oboe_position_frames_t mMarkerFramePosition;
102 oboe_nanoseconds_t mMarkerNanoTime;
103 int32_t mTimestampCount;
104 clock_model_state_t mState;
105
106 void update();
107};
108
109} /* namespace oboe */
110
111#endif //OBOE_ISOCHRONOUSCLOCKMODEL_H