Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 37b2b389 authored by Ronghua Wu's avatar Ronghua Wu
Browse files

stagefright: add support for limiting framerate in GraphicBufferSource

Bug: 19014096
Change-Id: I6de781e4d140a247dfd8fd8f12c3ddd7baa39ad4
parent e2cce813
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -147,6 +147,7 @@ public:
        INTERNAL_OPTION_SUSPEND,  // data is a bool
        INTERNAL_OPTION_REPEAT_PREVIOUS_FRAME_DELAY,  // data is an int64_t
        INTERNAL_OPTION_MAX_TIMESTAMP_GAP, // data is int64_t
        INTERNAL_OPTION_MAX_FPS, // data is float
        INTERNAL_OPTION_START_TIME, // data is an int64_t
        INTERNAL_OPTION_TIME_LAPSE, // data is an int64_t[2]
    };
+1 −0
Original line number Diff line number Diff line
@@ -214,6 +214,7 @@ private:

    int64_t mRepeatFrameDelayUs;
    int64_t mMaxPtsGapUs;
    float mMaxFps;

    int64_t mTimePerFrameUs;
    int64_t mTimePerCaptureUs;
+20 −0
Original line number Diff line number Diff line
@@ -419,6 +419,7 @@ ACodec::ACodec()
      mMetaDataBuffersToSubmit(0),
      mRepeatFrameDelayUs(-1ll),
      mMaxPtsGapUs(-1ll),
      mMaxFps(-1),
      mTimePerFrameUs(-1ll),
      mTimePerCaptureUs(-1ll),
      mCreateInputBuffersSuspended(false),
@@ -1259,6 +1260,10 @@ status_t ACodec::configureCodec(
            mMaxPtsGapUs = -1ll;
        }

        if (!msg->findFloat("max-fps-to-encoder", &mMaxFps)) {
            mMaxFps = -1;
        }

        if (!msg->findInt64("time-lapse", &mTimePerCaptureUs)) {
            mTimePerCaptureUs = -1ll;
        }
@@ -5110,6 +5115,21 @@ void ACodec::LoadedState::onCreateInputSurface(
        }
    }

    if (err == OK && mCodec->mMaxFps > 0) {
        err = mCodec->mOMX->setInternalOption(
                mCodec->mNode,
                kPortIndexInput,
                IOMX::INTERNAL_OPTION_MAX_FPS,
                &mCodec->mMaxFps,
                sizeof(mCodec->mMaxFps));

        if (err != OK) {
            ALOGE("[%s] Unable to configure max fps (err %d)",
                    mCodec->mComponentName.c_str(),
                    err);
        }
    }

    if (err == OK && mCodec->mTimePerCaptureUs > 0ll
            && mCodec->mTimePerFrameUs > 0ll) {
        int64_t timeLapse[2];
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ ifeq ($(TARGET_DEVICE), manta)
endif

LOCAL_SRC_FILES:=                     \
        FrameDropper.cpp              \
        GraphicBufferSource.cpp       \
        OMX.cpp                       \
        OMXMaster.cpp                 \
+70 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "FrameDropper"
#include <utils/Log.h>

#include "FrameDropper.h"

#include <media/stagefright/foundation/ADebug.h>

namespace android {

static const int64_t kMaxJitterUs = 2000;

FrameDropper::FrameDropper()
    : mDesiredMinTimeUs(-1),
      mMinIntervalUs(0) {
}

FrameDropper::~FrameDropper() {
}

status_t FrameDropper::setMaxFrameRate(float maxFrameRate) {
    if (maxFrameRate <= 0) {
        ALOGE("framerate should be positive but got %f.", maxFrameRate);
        return BAD_VALUE;
    }
    mMinIntervalUs = (int64_t) (1000000.0f / maxFrameRate);
    return OK;
}

bool FrameDropper::shouldDrop(int64_t timeUs) {
    if (mMinIntervalUs <= 0) {
        return false;
    }

    if (mDesiredMinTimeUs < 0) {
        mDesiredMinTimeUs = timeUs + mMinIntervalUs;
        ALOGV("first frame %lld, next desired frame %lld", timeUs, mDesiredMinTimeUs);
        return false;
    }

    if (timeUs < (mDesiredMinTimeUs - kMaxJitterUs)) {
        ALOGV("drop frame %lld, desired frame %lld, diff %lld",
                timeUs, mDesiredMinTimeUs, mDesiredMinTimeUs - timeUs);
        return true;
    }

    int64_t n = (timeUs - mDesiredMinTimeUs + kMaxJitterUs) / mMinIntervalUs;
    mDesiredMinTimeUs += (n + 1) * mMinIntervalUs;
    ALOGV("keep frame %lld, next desired frame %lld, diff %lld",
            timeUs, mDesiredMinTimeUs, mDesiredMinTimeUs - timeUs);
    return false;
}

}  // namespace android
Loading