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
27class 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    // returns the current frames-per-second, or 0.f if not primed
43    float getFrameRate();
44
45    void release();
46
47    static const size_t kHistorySize = 8;
48
49protected:
50    virtual ~VideoFrameScheduler();
51
52private:
53    struct PLL {
54        PLL();
55
56        // reset PLL to new PLL
57        void reset(float fps = -1);
58        // keep current estimate, but restart phase
59        void restart();
60        // returns period or 0 if not yet primed
61        nsecs_t addSample(nsecs_t time);
62        nsecs_t getPeriod() const;
63
64    private:
65        nsecs_t mPeriod;
66        nsecs_t mPhase;
67
68        bool    mPrimed;        // have an estimate for the period
69        size_t  mSamplesUsedForPriming;
70
71        nsecs_t mLastTime;      // last input time
72        nsecs_t mRefitAt;       // next input time to fit at
73
74        size_t  mNumSamples;    // can go past kHistorySize
75        nsecs_t mTimes[kHistorySize];
76
77        void test();
78        // returns whether fit was successful
79        bool fit(nsecs_t phase, nsecs_t period, size_t numSamples,
80                int64_t *a, int64_t *b, int64_t *err);
81        void prime(size_t numSamples);
82    };
83
84    void updateVsync();
85
86    nsecs_t mVsyncTime;        // vsync timing from display
87    nsecs_t mVsyncPeriod;
88    nsecs_t mVsyncRefreshAt;   // next time to refresh timing info
89
90    nsecs_t mLastVsyncTime;    // estimated vsync time for last frame
91    nsecs_t mTimeCorrection;   // running adjustment
92
93    PLL mPll;                  // PLL for video frame rate based on render time
94
95    sp<ISurfaceComposer> mComposer;
96
97    DISALLOW_EVIL_CONSTRUCTORS(VideoFrameScheduler);
98};
99
100}  // namespace android
101
102#endif  // VIDEO_FRAME_SCHEDULER_H_
103
104