blob: 7182376163a8d5d376169b9206c12b93e837b524 [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 Burk5204d312017-05-04 17:16:13 -070017#ifndef ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H
18#define ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H
Phil Burk204a1632017-01-03 17:23:43 -080019
20#include <stdint.h>
Phil Burk204a1632017-01-03 17:23:43 -080021
Phil Burk5ed503c2017-02-01 09:38:15 -080022namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080023
24/**
25 * Model an isochronous data stream using occasional timestamps as input.
26 * This can be used to predict the position of the stream at a given time.
27 *
28 * This class is not thread safe and should only be called from one thread.
29 */
30class IsochronousClockModel {
31
32public:
33 IsochronousClockModel();
34 virtual ~IsochronousClockModel();
35
Phil Burk3316d5e2017-02-15 11:23:01 -080036 void start(int64_t nanoTime);
37 void stop(int64_t nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080038
Phil Burkbcc36742017-08-31 17:24:51 -070039 bool isStarting();
40
Phil Burk3316d5e2017-02-15 11:23:01 -080041 void processTimestamp(int64_t framePosition, int64_t nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080042
43 /**
44 * @param sampleRate rate of the stream in frames per second
45 */
Phil Burk3316d5e2017-02-15 11:23:01 -080046 void setSampleRate(int32_t sampleRate);
Phil Burk204a1632017-01-03 17:23:43 -080047
Phil Burkec89b2e2017-06-20 15:05:06 -070048 void setPositionAndTime(int64_t framePosition, int64_t nanoTime);
49
Phil Burk3316d5e2017-02-15 11:23:01 -080050 int32_t getSampleRate() const {
Phil Burk204a1632017-01-03 17:23:43 -080051 return mSampleRate;
52 }
53
54 /**
55 * This must be set accurately in order to track the isochronous stream.
56 *
57 * @param framesPerBurst number of frames that stream advance at one time.
58 */
Phil Burk3316d5e2017-02-15 11:23:01 -080059 void setFramesPerBurst(int32_t framesPerBurst);
Phil Burk204a1632017-01-03 17:23:43 -080060
Phil Burk3316d5e2017-02-15 11:23:01 -080061 int32_t getFramesPerBurst() const {
Phil Burk204a1632017-01-03 17:23:43 -080062 return mFramesPerBurst;
63 }
64
65 /**
66 * Calculate an estimated time when the stream will be at that position.
67 *
68 * @param framePosition position of the stream in frames
69 * @return time in nanoseconds
70 */
Phil Burk3316d5e2017-02-15 11:23:01 -080071 int64_t convertPositionToTime(int64_t framePosition) const;
Phil Burk204a1632017-01-03 17:23:43 -080072
73 /**
74 * Calculate an estimated position where the stream will be at the specified time.
75 *
76 * @param nanoTime time of interest
77 * @return position in frames
78 */
Phil Burk3316d5e2017-02-15 11:23:01 -080079 int64_t convertTimeToPosition(int64_t nanoTime) const;
Phil Burk204a1632017-01-03 17:23:43 -080080
81 /**
82 * @param framesDelta difference in frames
83 * @return duration in nanoseconds
84 */
Phil Burk3316d5e2017-02-15 11:23:01 -080085 int64_t convertDeltaPositionToTime(int64_t framesDelta) const;
Phil Burk204a1632017-01-03 17:23:43 -080086
87 /**
88 * @param nanosDelta duration in nanoseconds
89 * @return frames that stream will advance in that time
90 */
Phil Burk3316d5e2017-02-15 11:23:01 -080091 int64_t convertDeltaTimeToPosition(int64_t nanosDelta) const;
Phil Burk204a1632017-01-03 17:23:43 -080092
Phil Burkec89b2e2017-06-20 15:05:06 -070093 void dump() const;
94
Phil Burk204a1632017-01-03 17:23:43 -080095private:
96 enum clock_model_state_t {
97 STATE_STOPPED,
98 STATE_STARTING,
99 STATE_SYNCING,
100 STATE_RUNNING
101 };
102
Phil Burk3316d5e2017-02-15 11:23:01 -0800103 int64_t mMarkerFramePosition;
104 int64_t mMarkerNanoTime;
105 int32_t mSampleRate;
106 int32_t mFramesPerBurst;
107 int32_t mMaxLatenessInNanos;
108 clock_model_state_t mState;
Phil Burk204a1632017-01-03 17:23:43 -0800109
110 void update();
111};
112
Phil Burk5ed503c2017-02-01 09:38:15 -0800113} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800114
Phil Burk5204d312017-05-04 17:16:13 -0700115#endif //ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H