1 /*
2 * Copyright (C) 2015 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 "FrameDropper"
19 #include <utils/Log.h>
20
21 #include <media/stagefright/bqhelper/FrameDropper.h>
22 #include <media/stagefright/foundation/ADebug.h>
23
24 namespace android {
25
26 static const int64_t kMaxJitterUs = 2000;
27
FrameDropper()28 FrameDropper::FrameDropper()
29 : mDesiredMinTimeUs(-1),
30 mMinIntervalUs(0) {
31 }
32
~FrameDropper()33 FrameDropper::~FrameDropper() {
34 }
35
setMaxFrameRate(float maxFrameRate)36 status_t FrameDropper::setMaxFrameRate(float maxFrameRate) {
37 if (maxFrameRate < 0) {
38 mMinIntervalUs = -1LL;
39 return OK;
40 }
41
42 if (maxFrameRate == 0) {
43 ALOGE("framerate should be positive but got %f.", maxFrameRate);
44 return BAD_VALUE;
45 }
46 mMinIntervalUs = (int64_t) (1000000.0f / maxFrameRate);
47 return OK;
48 }
49
shouldDrop(int64_t timeUs)50 bool FrameDropper::shouldDrop(int64_t timeUs) {
51 if (mMinIntervalUs <= 0) {
52 return false;
53 }
54
55 if (mDesiredMinTimeUs < 0) {
56 mDesiredMinTimeUs = timeUs + mMinIntervalUs;
57 ALOGV("first frame %lld, next desired frame %lld",
58 (long long)timeUs, (long long)mDesiredMinTimeUs);
59 return false;
60 }
61
62 if (timeUs < (mDesiredMinTimeUs - kMaxJitterUs)) {
63 ALOGV("drop frame %lld, desired frame %lld, diff %lld",
64 (long long)timeUs, (long long)mDesiredMinTimeUs,
65 (long long)(mDesiredMinTimeUs - timeUs));
66 return true;
67 }
68
69 int64_t n = (timeUs - mDesiredMinTimeUs + kMaxJitterUs) / mMinIntervalUs;
70 mDesiredMinTimeUs += (n + 1) * mMinIntervalUs;
71 ALOGV("keep frame %lld, next desired frame %lld, diff %lld",
72 (long long)timeUs, (long long)mDesiredMinTimeUs,
73 (long long)(mDesiredMinTimeUs - timeUs));
74 return false;
75 }
76
77 } // namespace android
78