1cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei/*
2cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * Copyright (C) 2015 The Android Open Source Project
3cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei *
4cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * Licensed under the Apache License, Version 2.0 (the "License");
5cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * you may not use this file except in compliance with the License.
6cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * You may obtain a copy of the License at
7cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei *
8cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei *      http://www.apache.org/licenses/LICENSE-2.0
9cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei *
10cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * Unless required by applicable law or agreed to in writing, software
11cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * distributed under the License is distributed on an "AS IS" BASIS,
12cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * See the License for the specific language governing permissions and
14cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei * limitations under the License.
15cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei */
16cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei
17cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei//#define LOG_NDEBUG 0
18cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei#define LOG_TAG "FrameDropper"
19cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei#include <utils/Log.h>
20cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei
21cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei#include "FrameDropper.h"
22cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei
23cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei#include <media/stagefright/foundation/ADebug.h>
24cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei
25cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Weinamespace android {
26cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei
27cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Weistatic const int64_t kMaxJitterUs = 2000;
28cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei
29cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark WeiFrameDropper::FrameDropper()
30cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei    : mDesiredMinTimeUs(-1),
31cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei      mMinIntervalUs(0) {
32cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei}
33cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei
34cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark WeiFrameDropper::~FrameDropper() {
35cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei}
36cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei
37cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Weistatus_t FrameDropper::setMaxFrameRate(float maxFrameRate) {
38cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei    if (maxFrameRate <= 0) {
39cea0c012d538f11b3ee97d4b7e78f4c1ea73d5beMark Wei        ALOGE("framerate should be positive but got %f.", maxFrameRate);
40        return BAD_VALUE;
41    }
42    mMinIntervalUs = (int64_t) (1000000.0f / maxFrameRate);
43    return OK;
44}
45
46bool FrameDropper::shouldDrop(int64_t timeUs) {
47    if (mMinIntervalUs <= 0) {
48        return false;
49    }
50
51    if (mDesiredMinTimeUs < 0) {
52        mDesiredMinTimeUs = timeUs + mMinIntervalUs;
53        ALOGV("first frame %lld, next desired frame %lld",
54                (long long)timeUs, (long long)mDesiredMinTimeUs);
55        return false;
56    }
57
58    if (timeUs < (mDesiredMinTimeUs - kMaxJitterUs)) {
59        ALOGV("drop frame %lld, desired frame %lld, diff %lld",
60                (long long)timeUs, (long long)mDesiredMinTimeUs,
61                (long long)(mDesiredMinTimeUs - timeUs));
62        return true;
63    }
64
65    int64_t n = (timeUs - mDesiredMinTimeUs + kMaxJitterUs) / mMinIntervalUs;
66    mDesiredMinTimeUs += (n + 1) * mMinIntervalUs;
67    ALOGV("keep frame %lld, next desired frame %lld, diff %lld",
68            (long long)timeUs, (long long)mDesiredMinTimeUs,
69            (long long)(mDesiredMinTimeUs - timeUs));
70    return false;
71}
72
73}  // namespace android
74