blob: b188a3dece5e42fe2bff501e10ed60cbb1d8a341 [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 Burk5ed503c2017-02-01 09:38:15 -080037 void start(aaudio_nanoseconds_t nanoTime);
38 void stop(aaudio_nanoseconds_t nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080039
Phil Burk5ed503c2017-02-01 09:38:15 -080040 void processTimestamp(aaudio_position_frames_t framePosition, aaudio_nanoseconds_t nanoTime);
Phil Burk204a1632017-01-03 17:23:43 -080041
42 /**
43 * @param sampleRate rate of the stream in frames per second
44 */
Phil Burk5ed503c2017-02-01 09:38:15 -080045 void setSampleRate(aaudio_sample_rate_t sampleRate);
Phil Burk204a1632017-01-03 17:23:43 -080046
Phil Burk5ed503c2017-02-01 09:38:15 -080047 aaudio_sample_rate_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 Burk5ed503c2017-02-01 09:38:15 -080056 void setFramesPerBurst(aaudio_size_frames_t framesPerBurst);
Phil Burk204a1632017-01-03 17:23:43 -080057
Phil Burk5ed503c2017-02-01 09:38:15 -080058 aaudio_size_frames_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 Burk5ed503c2017-02-01 09:38:15 -080068 aaudio_nanoseconds_t convertPositionToTime(aaudio_position_frames_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 Burk5ed503c2017-02-01 09:38:15 -080076 aaudio_position_frames_t convertTimeToPosition(aaudio_nanoseconds_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 Burk5ed503c2017-02-01 09:38:15 -080082 aaudio_nanoseconds_t convertDeltaPositionToTime(aaudio_position_frames_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 Burk5ed503c2017-02-01 09:38:15 -080088 aaudio_position_frames_t convertDeltaTimeToPosition(aaudio_nanoseconds_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 Burk5ed503c2017-02-01 09:38:15 -080098 aaudio_sample_rate_t mSampleRate;
99 aaudio_size_frames_t mFramesPerBurst;
Phil Burk204a1632017-01-03 17:23:43 -0800100 int32_t mMaxLatenessInNanos;
Phil Burk5ed503c2017-02-01 09:38:15 -0800101 aaudio_position_frames_t mMarkerFramePosition;
102 aaudio_nanoseconds_t mMarkerNanoTime;
Phil Burk204a1632017-01-03 17:23:43 -0800103 int32_t mTimestampCount;
104 clock_model_state_t mState;
105
106 void update();
107};
108
Phil Burk5ed503c2017-02-01 09:38:15 -0800109} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800110
Phil Burk5ed503c2017-02-01 09:38:15 -0800111#endif //AAUDIO_ISOCHRONOUSCLOCKMODEL_H