From a3725d7b0cb79ddb49f81cba00a0164d8e645acd Mon Sep 17 00:00:00 2001 From: Lajos Molnar Date: Wed, 1 Jul 2015 16:47:22 -0700 Subject: stagefright: MediaSync: use VideoFrameScheduler Move VideoFrameScheduler to libstagefright as part of this change. Bug: 22234976 Change-Id: Ib23fb52399cb700a1dcf789e8486b94a3edf9d95 --- include/media/stagefright/MediaSync.h | 13 +-- include/media/stagefright/VideoFrameScheduler.h | 103 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 include/media/stagefright/VideoFrameScheduler.h (limited to 'include') diff --git a/include/media/stagefright/MediaSync.h b/include/media/stagefright/MediaSync.h index 1eef211..1b7d7e8 100644 --- a/include/media/stagefright/MediaSync.h +++ b/include/media/stagefright/MediaSync.h @@ -37,6 +37,7 @@ class GraphicBuffer; class IGraphicBufferConsumer; class IGraphicBufferProducer; struct MediaClock; +struct VideoFrameScheduler; // MediaSync manages media playback and its synchronization to a media clock // source. It can be also used for video-only playback. @@ -131,11 +132,10 @@ protected: private: enum { - kWhatDrainVideo = 'dVid', + kWhatDrainVideo = 'dVid', + kWhatCheckFrameAvailable = 'cFrA', }; - static const int MAX_OUTSTANDING_BUFFERS = 2; - // This is a thin wrapper class that lets us listen to // IConsumerListener::onFrameAvailable from mInput. class InputListener : public BnConsumerListener, @@ -194,6 +194,7 @@ private: sp mInput; sp mOutput; int mUsageFlagsFromOutput; + uint32_t mMaxAcquiredBufferCount; // max acquired buffer count sp mAudioTrack; uint32_t mNativeSampleRateInHz; @@ -202,6 +203,7 @@ private: int64_t mNextBufferItemMediaUs; List mBufferItems; + sp mFrameScheduler; // Keep track of buffers received from |mInput|. This is needed because // it's possible the consumer of |mOutput| could return a different @@ -242,8 +244,9 @@ private: // onBufferReleasedByOutput releases a buffer back to the input. void onFrameAvailableFromInput(); - // Send |bufferItem| to the output for rendering. - void renderOneBufferItem_l(const BufferItem &bufferItem); + // Send |bufferItem| to the output for rendering. If this is not the only + // buffer sent for rendering, check for any dropped frames in |checkInUs| us. + void renderOneBufferItem_l(const BufferItem &bufferItem, int64_t checkInUs); // This implements the onBufferReleased callback from IProducerListener. // It gets called from an OutputListener. diff --git a/include/media/stagefright/VideoFrameScheduler.h b/include/media/stagefright/VideoFrameScheduler.h new file mode 100644 index 0000000..9d97dfd --- /dev/null +++ b/include/media/stagefright/VideoFrameScheduler.h @@ -0,0 +1,103 @@ +/* + * Copyright 2014, 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. + */ + +#ifndef VIDEO_FRAME_SCHEDULER_H_ +#define VIDEO_FRAME_SCHEDULER_H_ + +#include +#include + +#include + +namespace android { + +class ISurfaceComposer; + +struct VideoFrameScheduler : public RefBase { + VideoFrameScheduler(); + + // (re)initialize scheduler + void init(float videoFps = -1); + // use in case of video render-time discontinuity, e.g. seek + void restart(); + // get adjusted nanotime for a video frame render at renderTime + nsecs_t schedule(nsecs_t renderTime); + + // returns the vsync period for the main display + nsecs_t getVsyncPeriod(); + + // returns the current frames-per-second, or 0.f if not primed + float getFrameRate(); + + void release(); + + static const size_t kHistorySize = 8; + +protected: + virtual ~VideoFrameScheduler(); + +private: + struct PLL { + PLL(); + + // reset PLL to new PLL + void reset(float fps = -1); + // keep current estimate, but restart phase + void restart(); + // returns period or 0 if not yet primed + nsecs_t addSample(nsecs_t time); + nsecs_t getPeriod() const; + + private: + nsecs_t mPeriod; + nsecs_t mPhase; + + bool mPrimed; // have an estimate for the period + size_t mSamplesUsedForPriming; + + nsecs_t mLastTime; // last input time + nsecs_t mRefitAt; // next input time to fit at + + size_t mNumSamples; // can go past kHistorySize + nsecs_t mTimes[kHistorySize]; + + void test(); + // returns whether fit was successful + bool fit(nsecs_t phase, nsecs_t period, size_t numSamples, + int64_t *a, int64_t *b, int64_t *err); + void prime(size_t numSamples); + }; + + void updateVsync(); + + nsecs_t mVsyncTime; // vsync timing from display + nsecs_t mVsyncPeriod; + nsecs_t mVsyncRefreshAt; // next time to refresh timing info + + nsecs_t mLastVsyncTime; // estimated vsync time for last frame + nsecs_t mTimeCorrection; // running adjustment + + PLL mPll; // PLL for video frame rate based on render time + + sp mComposer; + + DISALLOW_EVIL_CONSTRUCTORS(VideoFrameScheduler); +}; + +} // namespace android + +#endif // VIDEO_FRAME_SCHEDULER_H_ + -- cgit v1.1