blob: 286f54945bc35e0712b4deda77bf88836aca4260 [file] [log] [blame]
Andy Hung398ffa22022-12-13 19:19:53 -08001/*
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// #define LOG_NDEBUG 0
18#define LOG_TAG "MonotonicFrameCounter"
19
20#include <utils/Log.h>
21#include "MonotonicFrameCounter.h"
22
23namespace android::audioflinger {
24
25int64_t MonotonicFrameCounter::updateAndGetMonotonicFrameCount(
26 int64_t newFrameCount, int64_t newTime) {
27 if (newFrameCount < 0 || newTime < 0) {
28 const auto result = getLastReportedFrameCount();
29 ALOGW("%s: invalid (frame, time) pair newFrameCount:%lld newFrameCount:%lld,"
30 " using %lld as frameCount",
31 __func__, (long long) newFrameCount, (long long)newFrameCount,
32 (long long)result);
33 return result;
34 }
35 if (newFrameCount < mLastReceivedFrameCount) {
36 const auto result = getLastReportedFrameCount();
37 ALOGW("%s: retrograde newFrameCount:%lld < mLastReceivedFrameCount:%lld,"
38 " ignoring, returning %lld as frameCount",
39 __func__, (long long) newFrameCount, (long long)mLastReceivedFrameCount,
40 (long long)result);
41 return result;
42 }
43 // Input looks fine.
44 // For better granularity, we could consider extrapolation on newTime.
45 mLastReceivedFrameCount = newFrameCount;
46 return getLastReportedFrameCount();
47}
48
49int64_t MonotonicFrameCounter::onFlush() {
50 ALOGV("%s: Updating mOffsetFrameCount:%lld with mLastReceivedFrameCount:%lld",
51 __func__, (long long)mOffsetFrameCount, (long long)mLastReceivedFrameCount);
52 mOffsetFrameCount += mLastReceivedFrameCount;
53 mLastReceivedFrameCount = 0;
54 return mOffsetFrameCount;
55}
56
57} // namespace android::audioflinger