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