• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H
18 #define ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H
19 
20 #include <stdint.h>
21 
22 namespace aaudio {
23 
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  */
30 class IsochronousClockModel {
31 
32 public:
33     IsochronousClockModel();
34     virtual ~IsochronousClockModel();
35 
36     void start(int64_t nanoTime);
37     void stop(int64_t nanoTime);
38 
39     bool isStarting();
40 
41     void processTimestamp(int64_t framePosition, int64_t nanoTime);
42 
43     /**
44      * @param sampleRate rate of the stream in frames per second
45      */
46     void setSampleRate(int32_t sampleRate);
47 
48     void setPositionAndTime(int64_t framePosition, int64_t nanoTime);
49 
getSampleRate()50     int32_t getSampleRate() const {
51         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      */
59     void setFramesPerBurst(int32_t framesPerBurst);
60 
getFramesPerBurst()61     int32_t getFramesPerBurst() const {
62         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      */
71     int64_t convertPositionToTime(int64_t framePosition) const;
72 
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      */
79     int64_t convertTimeToPosition(int64_t nanoTime) const;
80 
81     /**
82      * @param framesDelta difference in frames
83      * @return duration in nanoseconds
84      */
85     int64_t convertDeltaPositionToTime(int64_t framesDelta) const;
86 
87     /**
88      * @param nanosDelta duration in nanoseconds
89      * @return frames that stream will advance in that time
90      */
91     int64_t convertDeltaTimeToPosition(int64_t nanosDelta) const;
92 
93     void dump() const;
94 
95 private:
96     enum clock_model_state_t {
97         STATE_STOPPED,
98         STATE_STARTING,
99         STATE_SYNCING,
100         STATE_RUNNING
101     };
102 
103     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;
109 
110     void update();
111 };
112 
113 } /* namespace aaudio */
114 
115 #endif //ANDROID_AAUDIO_ISOCHRONOUS_CLOCK_MODEL_H
116