1dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar/*
2dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * Copyright 2014, The Android Open Source Project
3dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar *
4dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * Licensed under the Apache License, Version 2.0 (the "License");
5dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * you may not use this file except in compliance with the License.
6dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * You may obtain a copy of the License at
7dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar *
8dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar *     http://www.apache.org/licenses/LICENSE-2.0
9dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar *
10dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * Unless required by applicable law or agreed to in writing, software
11dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * distributed under the License is distributed on an "AS IS" BASIS,
12dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * See the License for the specific language governing permissions and
14dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar * limitations under the License.
15dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar */
16dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
17dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar#ifndef VIDEO_FRAME_SCHEDULER_H_
18dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar#define VIDEO_FRAME_SCHEDULER_H_
19dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
20dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar#include <utils/RefBase.h>
21dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar#include <utils/Timers.h>
22dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
23dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar#include <media/stagefright/foundation/ABase.h>
24dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
25dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnarnamespace android {
26dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
276d339f1f764bbd32e3381dae7bfa7c6c575bb493Lajos Molnarclass ISurfaceComposer;
28dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
29dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnarstruct VideoFrameScheduler : public RefBase {
30dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    VideoFrameScheduler();
31dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
32dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    // (re)initialize scheduler
33c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    void init(float videoFps = -1);
34c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    // use in case of video render-time discontinuity, e.g. seek
35c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    void restart();
36dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    // get adjusted nanotime for a video frame render at renderTime
37dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    nsecs_t schedule(nsecs_t renderTime);
38dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
39dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    // returns the vsync period for the main display
40dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    nsecs_t getVsyncPeriod();
41dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
42a3725d7b0cb79ddb49f81cba00a0164d8e645acdLajos Molnar    // returns the current frames-per-second, or 0.f if not primed
43a3725d7b0cb79ddb49f81cba00a0164d8e645acdLajos Molnar    float getFrameRate();
44a3725d7b0cb79ddb49f81cba00a0164d8e645acdLajos Molnar
45dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    void release();
46dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
47c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    static const size_t kHistorySize = 8;
48c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
49dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnarprotected:
50dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    virtual ~VideoFrameScheduler();
51dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
52dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnarprivate:
53c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    struct PLL {
54c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        PLL();
55c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
56c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        // reset PLL to new PLL
57c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        void reset(float fps = -1);
58c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        // keep current estimate, but restart phase
59c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        void restart();
60a3725d7b0cb79ddb49f81cba00a0164d8e645acdLajos Molnar        // returns period or 0 if not yet primed
61c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        nsecs_t addSample(nsecs_t time);
62a3725d7b0cb79ddb49f81cba00a0164d8e645acdLajos Molnar        nsecs_t getPeriod() const;
63c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
64c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    private:
65c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        nsecs_t mPeriod;
66c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        nsecs_t mPhase;
67c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
68c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        bool    mPrimed;        // have an estimate for the period
69c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        size_t  mSamplesUsedForPriming;
70c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
71c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        nsecs_t mLastTime;      // last input time
72c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        nsecs_t mRefitAt;       // next input time to fit at
73c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
74c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        size_t  mNumSamples;    // can go past kHistorySize
75c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        nsecs_t mTimes[kHistorySize];
76c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
77c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        void test();
785d6fb5e41f57a71bd5b2902dc8334825de7bdcc0Lajos Molnar        // returns whether fit was successful
795d6fb5e41f57a71bd5b2902dc8334825de7bdcc0Lajos Molnar        bool fit(nsecs_t phase, nsecs_t period, size_t numSamples,
80c851b5de495169d7e9528644c2592746021bd968Lajos Molnar                int64_t *a, int64_t *b, int64_t *err);
81c851b5de495169d7e9528644c2592746021bd968Lajos Molnar        void prime(size_t numSamples);
82c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    };
83c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
84dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    void updateVsync();
85dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
86dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    nsecs_t mVsyncTime;        // vsync timing from display
87dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    nsecs_t mVsyncPeriod;
88dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    nsecs_t mVsyncRefreshAt;   // next time to refresh timing info
89dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
90c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    nsecs_t mLastVsyncTime;    // estimated vsync time for last frame
91c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    nsecs_t mTimeCorrection;   // running adjustment
92c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
93c851b5de495169d7e9528644c2592746021bd968Lajos Molnar    PLL mPll;                  // PLL for video frame rate based on render time
94c851b5de495169d7e9528644c2592746021bd968Lajos Molnar
95dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    sp<ISurfaceComposer> mComposer;
96dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
97dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar    DISALLOW_EVIL_CONSTRUCTORS(VideoFrameScheduler);
98dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar};
99dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
100dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar}  // namespace android
101dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
102dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar#endif  // VIDEO_FRAME_SCHEDULER_H_
103dc43dfa1294470a4413c37e863ef3b621da8681fLajos Molnar
104