• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <cstdint>
20 
21 namespace android::audioflinger {
22 
23 /**
24  * MonotonicFrameCounter
25  *
26  * Advances a monotonic frame count based on input timestamp pairs (frames, time).
27  * It takes into account a possible flush, which will "reset" the frames to 0.
28  *
29  * This class is used to drive VolumeShaper volume automation.
30  *
31  * The timestamps provided in updateAndGetMonotonicFrameCount should
32  * be of sufficient granularity for the purpose at hand.  Currently no temporal
33  * extrapolation is done.
34  *
35  * This class is not thread safe.
36  */
37 class MonotonicFrameCounter {
38 public:
39     /**
40      * Receives a new timestamp pair (frames, time) and returns a monotonic frameCount.
41      *
42      * \param newFrameCount the frameCount currently played.
43      * \param newTime       the time corresponding to the frameCount.
44      * \return              a monotonic frame count usable for automation timing.
45      */
46     int64_t updateAndGetMonotonicFrameCount(int64_t newFrameCount, int64_t newTime);
47 
48     /**
49      * Notifies when a flush occurs, whereupon the received frameCount sequence restarts at 0.
50      *
51      * \return the last reported frameCount.
52      */
53     int64_t onFlush();
54 
55     /**
56      * Returns the received (input) frameCount to reported (output) frameCount offset.
57      *
58      * This offset is sufficient to ensure monotonicity after flush is called,
59      * suitability for any other purpose is *not* guaranteed.
60      */
getOffsetFrameCount()61     [[nodiscard]] int64_t getOffsetFrameCount() const { return mOffsetFrameCount; }
62 
63     /**
64      * Returns the last received frameCount.
65      */
getLastReceivedFrameCount()66     [[nodiscard]] int64_t getLastReceivedFrameCount() const {
67         return mLastReceivedFrameCount;
68     }
69 
70     /**
71      * Returns the last reported frameCount from updateAndGetMonotonicFrameCount().
72      */
getLastReportedFrameCount()73     [[nodiscard]] int64_t getLastReportedFrameCount() const {
74         // This is consistent after onFlush().
75         return mOffsetFrameCount + mLastReceivedFrameCount;
76     }
77 
78 private:
79     int64_t mOffsetFrameCount = 0;
80     int64_t mLastReceivedFrameCount = 0;
81 };
82 
83 } // namespace android::audioflinger
84