VideoFrameScheduler.h revision 5d6fb5e41f57a71bd5b2902dc8334825de7bdcc0
1/*
2 * Copyright 2014, 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#ifndef VIDEO_FRAME_SCHEDULER_H_
18#define VIDEO_FRAME_SCHEDULER_H_
19
20#include <utils/RefBase.h>
21#include <utils/Timers.h>
22
23#include <media/stagefright/foundation/ABase.h>
24
25namespace android {
26
27struct ISurfaceComposer;
28
29struct VideoFrameScheduler : public RefBase {
30    VideoFrameScheduler();
31
32    // (re)initialize scheduler
33    void init(float videoFps = -1);
34    // use in case of video render-time discontinuity, e.g. seek
35    void restart();
36    // get adjusted nanotime for a video frame render at renderTime
37    nsecs_t schedule(nsecs_t renderTime);
38
39    // returns the vsync period for the main display
40    nsecs_t getVsyncPeriod();
41
42    void release();
43
44    static const size_t kHistorySize = 8;
45
46protected:
47    virtual ~VideoFrameScheduler();
48
49private:
50    struct PLL {
51        PLL();
52
53        // reset PLL to new PLL
54        void reset(float fps = -1);
55        // keep current estimate, but restart phase
56        void restart();
57        // returns period
58        nsecs_t addSample(nsecs_t time);
59
60    private:
61        nsecs_t mPeriod;
62        nsecs_t mPhase;
63
64        bool    mPrimed;        // have an estimate for the period
65        size_t  mSamplesUsedForPriming;
66
67        nsecs_t mLastTime;      // last input time
68        nsecs_t mRefitAt;       // next input time to fit at
69
70        size_t  mNumSamples;    // can go past kHistorySize
71        nsecs_t mTimes[kHistorySize];
72
73        void test();
74        // returns whether fit was successful
75        bool fit(nsecs_t phase, nsecs_t period, size_t numSamples,
76                int64_t *a, int64_t *b, int64_t *err);
77        void prime(size_t numSamples);
78    };
79
80    void updateVsync();
81
82    nsecs_t mVsyncTime;        // vsync timing from display
83    nsecs_t mVsyncPeriod;
84    nsecs_t mVsyncRefreshAt;   // next time to refresh timing info
85
86    nsecs_t mLastVsyncTime;    // estimated vsync time for last frame
87    nsecs_t mTimeCorrection;   // running adjustment
88
89    PLL mPll;                  // PLL for video frame rate based on render time
90
91    sp<ISurfaceComposer> mComposer;
92
93    DISALLOW_EVIL_CONSTRUCTORS(VideoFrameScheduler);
94};
95
96}  // namespace android
97
98#endif  // VIDEO_FRAME_SCHEDULER_H_
99
100