MythTV  master
vsync.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 Doug Larrick <doug@ties.org>.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #ifndef VSYNC_H_INCLUDED
20 #define VSYNC_H_INCLUDED
21 
22 #include <sys/time.h>
23 #include <ctime>
24 
25 class VideoOutput;
26 
27 extern bool tryingVideoSync;
28 
45 class VideoSync
46 // virtual base class
47 {
48  public:
49  VideoSync(VideoOutput*, int refreshint);
50  virtual ~VideoSync() = default;
51 
53  virtual QString getName(void) const = 0;
55  virtual bool TryInit(void) = 0;
56 
63  virtual void Start(void);
64 
76  virtual int WaitForFrame(int nominal_frame_interval, int extra_delay) = 0;
77 
79  int getRefreshInterval(void) const { return m_refresh_interval; }
81  void setRefreshInterval(int ri) { m_refresh_interval = ri; }
82 
89  virtual void Stop(void) {}
90 
91  // documented in vsync.cpp
92  static VideoSync *BestMethod(VideoOutput *, uint refresh_interval);
93 
94  protected:
95  int64_t GetTime(void);
96  int CalcDelay(int nominal_frame_interval);
97 
99  int m_refresh_interval; // of display
100  int64_t m_nexttrigger {0};
101  int m_delay {-1};
102 
103  static int s_forceskip;
104 };
105 
106 #ifndef _WIN32
107 
113 class DRMVideoSync : public VideoSync
114 {
115  public:
116  DRMVideoSync(VideoOutput *, int refresh_interval);
117  ~DRMVideoSync();
118 
119  QString getName(void) const override // VideoSync
120  { return QString("DRM"); }
121  bool TryInit(void) override; // VideoSync
122  void Start(void) override; // VideoSync
123  int WaitForFrame(int nominal_frame_interval, int extra_delay) override; // VideoSync
124 
125  private:
126  int m_dri_fd;
127  static const char *s_dri_dev;
128 
129 };
130 #endif // !_WIN32
131 
132 #ifdef __linux__
133 
143 class RTCVideoSync : public VideoSync
144 {
145  public:
146  RTCVideoSync(VideoOutput *, int refresh_interval);
147  ~RTCVideoSync();
148 
149  QString getName(void) const override // VideoSync
150  { return QString("RTC"); }
151  bool TryInit(void) override; // VideoSync
152  int WaitForFrame(int nominal_frame_interval, int extra_delay) override; // VideoSync
153 
154  private:
155  int m_rtcfd;
156 };
157 #endif
158 
170 {
171  public:
172  BusyWaitVideoSync(VideoOutput *vo, int ri) : VideoSync(vo, ri) {};
173  ~BusyWaitVideoSync() = default;
174 
175  QString getName(void) const override // VideoSync
176  { return QString("USleep with busy wait"); }
177  bool TryInit(void) override {return true; } // VideoSync
178  int WaitForFrame(int nominal_frame_interval, int extra_delay) override; // VideoSync
179 
180  private:
181  int m_cheat {5000};
182  int m_fudge {0};
183 };
184 
196 {
197  public:
198  USleepVideoSync(VideoOutput *vo, int ri) : VideoSync(vo, ri) {}
199  ~USleepVideoSync() = default;
200 
201  QString getName(void) const override // VideoSync
202  { return QString("USleep"); }
203  bool TryInit(void) override {return true; } // VideoSync
204  int WaitForFrame(int nominal_frame_interval, int extra_delay) override; // VideoSync
205 };
206 
207 class DummyVideoSync : public VideoSync
208 {
209  public:
210  DummyVideoSync(VideoOutput* vo, int ri) : VideoSync(vo, ri) { }
211  ~DummyVideoSync() = default;
212 
213  QString getName(void) const override // VideoSync
214  { return QString("Dummy"); }
215  bool TryInit(void) override // VideoSync
216  { return true; }
217  int WaitForFrame(int /*nominal_frame_interval*/, int /*extra_delay*/) override // VideoSync
218  { return 0; }
219 };
220 #endif /* VSYNC_H_INCLUDED */
virtual bool TryInit(void)=0
Tries to initialize VSync method.
void setRefreshInterval(int ri)
Sets the refresh interval of the output device.
Definition: vsync.h:81
DummyVideoSync(VideoOutput *vo, int ri)
Definition: vsync.h:210
VideoSync(VideoOutput *, int refreshint)
Used by BestMethod(VideoOutput*,uint) to initialize video synchronization method.
Definition: vsync.cpp:115
bool TryInit(void) override
Tries to initialize VSync method.
Definition: vsync.h:177
~USleepVideoSync()=default
virtual int WaitForFrame(int nominal_frame_interval, int extra_delay)=0
Waits for next a frame or field.
unsigned int uint
Definition: compat.h:140
~BusyWaitVideoSync()=default
int WaitForFrame(int nominal_frame_interval, int extra_delay) override
Waits for next a frame or field.
Definition: vsync.cpp:402
int m_delay
Definition: vsync.h:101
int CalcDelay(int nominal_frame_interval)
Calculates the delay to the next frame.
Definition: vsync.cpp:143
This class serves as the base class for all video output methods.
Definition: videooutbase.h:46
int64_t GetTime(void)
Definition: vsync.cpp:120
QString getName(void) const override
Returns name of instanciated VSync method.
Definition: vsync.h:213
int WaitForFrame(int, int) override
Waits for next a frame or field.
Definition: vsync.h:217
static int s_forceskip
Definition: vsync.h:103
Virtual base class for all video synchronization classes.
Definition: vsync.h:45
Video synchronization classes employing only usleep().
Definition: vsync.h:195
bool TryInit(void) override
Tries to initialize VSync method.
Definition: vsync.h:203
virtual void Start(void)
Start VSync; must be called from main thread.
Definition: vsync.cpp:127
Video synchronization classes employing usleep() and busy-waits.
Definition: vsync.h:169
int m_refresh_interval
Definition: vsync.h:99
VideoOutput * m_video_output
Definition: vsync.h:98
virtual ~VideoSync()=default
BusyWaitVideoSync(VideoOutput *vo, int ri)
Definition: vsync.h:172
USleepVideoSync(VideoOutput *vo, int ri)
Definition: vsync.h:198
static VideoSync * BestMethod(VideoOutput *, uint refresh_interval)
Returns the most sophisticated video sync method available.
Definition: vsync.cpp:77
bool tryingVideoSync
Definition: vsync.cpp:56
QString getName(void) const override
Returns name of instanciated VSync method.
Definition: vsync.h:175
virtual QString getName(void) const =0
Returns name of instanciated VSync method.
bool TryInit(void) override
Tries to initialize VSync method.
Definition: vsync.h:215
int WaitForFrame(int nominal_frame_interval, int extra_delay) override
Waits for next a frame or field.
Definition: vsync.cpp:370
virtual void Stop(void)
Stops VSync; must be called from main thread.
Definition: vsync.h:89
int64_t m_nexttrigger
Definition: vsync.h:100
QString getName(void) const override
Returns name of instanciated VSync method.
Definition: vsync.h:201
int getRefreshInterval(void) const
Returns the (minimum) refresh interval of the output device.
Definition: vsync.h:79
~DummyVideoSync()=default