MythTV  master
mythtv/programs/mythavtest/main.cpp
Go to the documentation of this file.
1 #include <unistd.h>
2 #include <iostream>
3 #include <utility>
4 
5 #include <QApplication>
6 #include <QDir>
7 #include <QString>
8 #include <QSurfaceFormat>
9 #include <QTime>
10 
11 #include "tv_play.h"
12 #include "programinfo.h"
13 #include "commandlineparser.h"
14 #include "mythplayerui.h"
15 #include "jitterometer.h"
16 
17 #include "exitcodes.h"
18 #include "mythcontext.h"
19 #include "mythversion.h"
20 #include "mythdbcon.h"
21 #include "compat.h"
22 #include "dbcheck.h"
23 #include "mythlogging.h"
24 #include "signalhandling.h"
25 #include "mythmiscutil.h"
26 #include "mythvideoout.h"
27 
28 // libmythui
29 #include "mythuihelper.h"
30 #include "mythmainwindow.h"
31 
33 {
34  public:
35  VideoPerformanceTest(QString filename, bool decodeno, bool onlydecode,
36  std::chrono::seconds runfor, bool deint, bool gpu)
37  : m_file(std::move(filename)),
38  m_noDecode(decodeno),
39  m_decodeOnly(onlydecode),
40  m_deinterlace(deint),
41  m_allowGpu(gpu)
42  {
43  m_secondsToRun = std::clamp(runfor, 1s, 3600s);
44  }
45 
47  {
48  delete m_ctx;
49  }
50 
51  void Test(void)
52  {
53  MythMediaBuffer *rb = MythMediaBuffer::Create(m_file, false, true, 2s);
54  m_ctx = new PlayerContext("VideoPerformanceTest");
55  auto *mp = new MythPlayerUI(GetMythMainWindow(), nullptr, m_ctx, static_cast<PlayerFlags>(kAudioMuted | (m_allowGpu ? kDecodeAllowGPU: kNoFlags)));
56  mp->GetAudio()->SetAudioInfo("NULL", "NULL", 0, 0);
57  mp->GetAudio()->SetNoAudio();
58  m_ctx->SetRingBuffer(rb);
59  m_ctx->SetPlayer(mp);
60  auto *pinfo = new ProgramInfo(m_file);
61  m_ctx->SetPlayingInfo(pinfo); // makes a copy
62  delete pinfo;
63 
65  if (!mp->StartPlaying())
66  {
67  LOG(VB_GENERAL, LOG_ERR, "Failed to start playback.");
68  return;
69  }
70 
71  MythVideoOutput *vo = mp->GetVideoOutput();
72  if (!vo)
73  {
74  LOG(VB_GENERAL, LOG_ERR, "No video output.");
75  return;
76  }
77 
78  LOG(VB_GENERAL, LOG_INFO, "-----------------------------------");
79  LOG(VB_GENERAL, LOG_INFO, "Ensure Sync to VBlank is disabled.");
80  LOG(VB_GENERAL, LOG_INFO, "Otherwise rate will be limited to that of the display.");
81  LOG(VB_GENERAL, LOG_INFO, "-----------------------------------");
82  LOG(VB_GENERAL, LOG_INFO, QString("Starting video performance test for '%1'.")
83  .arg(m_file));
84  LOG(VB_GENERAL, LOG_INFO, QString("Test will run for %1 seconds.")
85  .arg(m_secondsToRun.count()));
86 
87  if (m_noDecode)
88  LOG(VB_GENERAL, LOG_INFO, "No decode after startup - checking display performance");
89  else if (m_decodeOnly)
90  LOG(VB_GENERAL, LOG_INFO, "Decoding frames only - skipping display.");
91  DecoderBase* dec = mp->GetDecoder();
92  if (dec)
93  LOG(VB_GENERAL, LOG_INFO, QString("Using decoder: %1").arg(dec->GetCodecDecoderName()));
94 
95  auto *jitter = new Jitterometer("Performance: ", static_cast<int>(mp->GetFrameRate()));
96 
97  QTime start = QTime::currentTime();
98  MythVideoFrame *frame = nullptr;
99  while (true)
100  {
101  mp->ProcessCallbacks();
102  auto duration = std::chrono::milliseconds(start.msecsTo(QTime::currentTime()));
103  if (duration < 0ms || duration > m_secondsToRun)
104  {
105  LOG(VB_GENERAL, LOG_INFO, "Complete.");
106  break;
107  }
108 
109  if (mp->IsErrored())
110  {
111  LOG(VB_GENERAL, LOG_ERR, "Playback error.");
112  break;
113  }
114 
115  if (mp->GetEof() != kEofStateNone)
116  {
117  LOG(VB_GENERAL, LOG_INFO, "End of file.");
118  break;
119  }
120 
121  if (!mp->PrebufferEnoughFrames())
122  continue;
123 
124  mp->SetBuffering(false);
125  vo->StartDisplayingFrame();
126  if ((m_noDecode && !frame) || !m_noDecode)
127  frame = vo->GetLastShownFrame();
128  mp->CheckAspectRatio(frame);
129 
130  if (!m_decodeOnly)
131  {
133  vo->PrepareFrame(frame, scan);
134  vo->RenderFrame(frame, scan);
135  vo->EndFrame();
136 
137  if (doubledeint && m_deinterlace)
138  {
139  doubledeint = frame->GetDoubleRateOption(DEINT_CPU);
141  if (doubledeint && !other)
142  vo->PrepareFrame(frame, kScan_Intr2ndField);
143  vo->RenderFrame(frame, kScan_Intr2ndField);
144  vo->EndFrame();
145  }
146  }
147  if (!m_noDecode)
148  vo->DoneDisplayingFrame(frame);
149  jitter->RecordCycleTime();
150  }
151  LOG(VB_GENERAL, LOG_INFO, "-----------------------------------");
152  delete jitter;
153  }
154 
155  private:
156  QString m_file;
157  bool m_noDecode {false};
158  bool m_decodeOnly {false};
159  std::chrono::seconds m_secondsToRun {1s};
160  bool m_deinterlace {false};
161  bool m_allowGpu {false};
162  PlayerContext *m_ctx {nullptr};
163 };
164 
165 int main(int argc, char *argv[])
166 {
168  if (!cmdline.Parse(argc, argv))
169  {
170  cmdline.PrintHelp();
172  }
173 
174  if (cmdline.toBool("showhelp"))
175  {
176  cmdline.PrintHelp();
177  return GENERIC_EXIT_OK;
178  }
179 
180  if (cmdline.toBool("showversion"))
181  {
183  return GENERIC_EXIT_OK;
184  }
185 
186  int swapinterval = 1;
187  if (cmdline.toBool("test"))
188  {
189  // try and disable sync to vblank on linux x11
190  qputenv("vblank_mode", "0"); // Intel and AMD
191  qputenv("__GL_SYNC_TO_VBLANK", "0"); // NVidia
192  // the default surface format has a swap interval of 1. This is used by
193  // the MythMainwindow widget that then drives vsync for all widgets/children
194  // (i.e. MythPainterWindow) and we cannot override it on some drivers. So
195  // force the default here.
196  swapinterval = 0;
197  }
198 
199  MythDisplay::ConfigureQtGUI(swapinterval, cmdline);
200 
201  QApplication a(argc, argv);
202  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHAVTEST);
203 
204  int retval = cmdline.ConfigureLogging();
205  if (retval != GENERIC_EXIT_OK)
206  return retval;
207 
208  if (!cmdline.toString("geometry").isEmpty())
210 
211  QString filename = "";
212  if (!cmdline.toString("infile").isEmpty())
213  filename = cmdline.toString("infile");
214  else if (!cmdline.GetArgs().empty())
215  filename = cmdline.GetArgs().at(0);
216 
218  if (!gContext->Init())
219  {
220  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
222  }
223 
225 
226  QString themename = gCoreContext->GetSetting("Theme");
227  QString themedir = GetMythUI()->FindThemeDir(themename);
228  if (themedir.isEmpty())
229  {
230  QString msg = QString("Fatal Error: Couldn't find theme '%1'.")
231  .arg(themename);
232  LOG(VB_GENERAL, LOG_ERR, msg);
233  return GENERIC_EXIT_NO_THEME;
234  }
235 
236 #if defined(Q_OS_MACX)
237  // Mac OS X doesn't define the AudioOutputDevice setting
238 #else
239  QString auddevice = gCoreContext->GetSetting("AudioOutputDevice");
240  if (auddevice.isEmpty())
241  {
242  LOG(VB_GENERAL, LOG_ERR, "Fatal Error: Audio not configured, you need "
243  "to run 'mythfrontend', not 'mythtv'.");
245  }
246 #endif
247 
248  MythMainWindow *mainWindow = GetMythMainWindow();
249  mainWindow->Init();
250 
251 #ifndef _WIN32
252  QList<int> signallist;
253  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
254  << SIGILL;
255 #if ! CONFIG_DARWIN
256  signallist << SIGRTMIN;
257 #endif
258  SignalHandler::Init(signallist);
259  signal(SIGHUP, SIG_IGN);
260 #endif
261 
262  if (cmdline.toBool("test"))
263  {
264  std::chrono::seconds seconds = 5s;
265  if (!cmdline.toString("seconds").isEmpty())
266  seconds = std::chrono::seconds(cmdline.toInt("seconds"));
267  auto *test = new VideoPerformanceTest(filename,
268  cmdline.toBool("nodecode"),
269  cmdline.toBool("decodeonly"), seconds,
270  cmdline.toBool("deinterlace"),
271  cmdline.toBool("gpu"));
272  test->Test();
273  delete test;
274  }
275  else
276  {
277  TV::InitKeys();
278  setHttpProxy();
279 
280  if (!UpgradeTVDatabaseSchema(false))
281  {
282  LOG(VB_GENERAL, LOG_ERR, "Fatal Error: Incorrect database schema.");
283  delete gContext;
285  }
286 
287  if (filename.isEmpty())
288  {
289  TV::StartTV(nullptr, kStartTVNoFlags);
290  }
291  else
292  {
293  ProgramInfo pginfo(filename);
294  TV::StartTV(&pginfo, kStartTVNoFlags);
295  }
296  }
298 
299  delete gContext;
300 
302 
303  return GENERIC_EXIT_OK;
304 }
305 
306 /* vim: set expandtab tabstop=4 shiftwidth=4: */
themedir
static QString themedir
Definition: mythdirs.cpp:21
TV::InitKeys
static void InitKeys()
Definition: tv_play.cpp:468
DEINT_DRIVER
@ DEINT_DRIVER
Definition: mythframe.h:75
GENERIC_EXIT_OK
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
MythVideoFrame::GetDoubleRateOption
MythDeintType GetDoubleRateOption(MythDeintType Type, MythDeintType Override=DEINT_NONE) const
Definition: mythframe.cpp:449
setHttpProxy
void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
Definition: mythmiscutil.cpp:854
MythMainWindow::Init
void Init(bool MayReInit=true)
Definition: mythmainwindow.cpp:640
MythUIScreenBounds::ParseGeometryOverride
static void ParseGeometryOverride(const QString &Geometry)
Parse an X11 style command line geometry string.
Definition: mythuiscreenbounds.cpp:39
MythVideoOutput
Definition: mythvideoout.h:35
mythvideoout.h
mythplayerui.h
DEINT_SHADER
@ DEINT_SHADER
Definition: mythframe.h:74
kEofStateNone
@ kEofStateNone
Definition: decoderbase.h:69
PlayerContext::SetPlayingInfo
void SetPlayingInfo(const ProgramInfo *info)
assign programinfo to the context
Definition: playercontext.cpp:502
UpgradeTVDatabaseSchema
bool UpgradeTVDatabaseSchema(const bool upgradeAllowed, const bool upgradeIfNoUI, const bool informSystemd)
Called from outside dbcheck.cpp to update the schema.
Definition: dbcheck.cpp:361
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
kStartTVNoFlags
@ kStartTVNoFlags
Definition: tv_play.h:113
MythAVTestCommandLineParser
Definition: mythavtest/commandlineparser.h:6
MythVideoOutput::PrepareFrame
virtual void PrepareFrame(MythVideoFrame *Frame, FrameScanType Scan=kScan_Ignore)=0
mythdbcon.h
FrameScanType
FrameScanType
Definition: videoouttypes.h:94
VideoPerformanceTest::m_ctx
PlayerContext * m_ctx
Definition: mythtv/programs/mythavtest/main.cpp:162
MythMediaBuffer
Definition: mythmediabuffer.h:50
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
PlayerFlags
PlayerFlags
Definition: mythplayer.h:65
GENERIC_EXIT_INVALID_CMDLINE
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
VideoPerformanceTest::m_noDecode
bool m_noDecode
Definition: mythtv/programs/mythavtest/main.cpp:157
hardwareprofile.scan.scan
def scan(profile, smoonURL, gate)
Definition: scan.py:57
TV::StartTV
static bool StartTV(ProgramInfo *TVRec, uint Flags, const ChannelInfoList &Selection=ChannelInfoList())
Start playback of media.
Definition: tv_play.cpp:265
mythversion.h
MythDisplay::ConfigureQtGUI
static void ConfigureQtGUI(int SwapInterval, const MythCommandLineParser &CmdLine)
Shared static initialistaion code for all MythTV GUI applications.
Definition: mythdisplay.cpp:1152
main
int main(int argc, char *argv[])
Definition: mythtv/programs/mythavtest/main.cpp:165
DEINT_CPU
@ DEINT_CPU
Definition: mythframe.h:73
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1449
MythPlayerUI
Definition: mythplayerui.h:10
MythVideoOutput::EndFrame
virtual void EndFrame()=0
DecoderBase::GetCodecDecoderName
virtual QString GetCodecDecoderName(void) const =0
kScan_Intr2ndField
@ kScan_Intr2ndField
Definition: videoouttypes.h:99
programinfo.h
mythlogging.h
kScan_Progressive
@ kScan_Progressive
Definition: videoouttypes.h:100
dbcheck.h
signalhandling.h
VideoPerformanceTest::m_allowGpu
bool m_allowGpu
Definition: mythtv/programs/mythavtest/main.cpp:161
VideoPerformanceTest::Test
void Test(void)
Definition: mythtv/programs/mythavtest/main.cpp:51
compat.h
MythCommandLineParser::GetArgs
QStringList GetArgs(void) const
Return list of additional values provided on the command line independent of any keyword.
Definition: mythcommandlineparser.cpp:1984
GENERIC_EXIT_SETUP_ERROR
#define GENERIC_EXIT_SETUP_ERROR
Incorrectly setup system.
Definition: exitcodes.h:21
DestroyMythMainWindow
void DestroyMythMainWindow(void)
Definition: mythmainwindow.cpp:114
clamp
float clamp(float val, float minimum, float maximum)
Definition: mythmiscutil.h:56
SIGHUP
#define SIGHUP
Definition: compat.h:214
MythUIThemeHelper::FindThemeDir
QString FindThemeDir(const QString &ThemeName, bool Fallback=true)
Returns the full path to the theme denoted by themename.
Definition: mythuithemehelper.cpp:47
kDecodeAllowGPU
@ kDecodeAllowGPU
Definition: mythplayer.h:73
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1277
jitterometer.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1293
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
commandlineparser.h
VideoPerformanceTest::m_file
QString m_file
Definition: mythtv/programs/mythavtest/main.cpp:156
MythCommandLineParser::ApplySettingsOverride
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Definition: mythcommandlineparser.cpp:2825
Jitterometer
Definition: jitterometer.h:43
MYTH_APPNAME_MYTHAVTEST
#define MYTH_APPNAME_MYTHAVTEST
Definition: mythcorecontext.h:31
MythVideoOutput::StartDisplayingFrame
virtual void StartDisplayingFrame()
Tell GetLastShownFrame() to return the next frame from the head of the queue of frames to display.
Definition: mythvideoout.cpp:409
mythuihelper.h
MYTH_BINARY_VERSION
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:15
MythDeintType
MythDeintType
Definition: mythframe.h:67
MythVideoOutput::RenderFrame
virtual void RenderFrame(MythVideoFrame *Frame, FrameScanType)=0
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythtv/programs/mythcommflag/main.cpp:74
kNoFlags
@ kNoFlags
Definition: mythplayer.h:67
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
mythmiscutil.h
MythCommandLineParser::toString
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2252
kScan_Interlaced
@ kScan_Interlaced
Definition: videoouttypes.h:98
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2095
VideoPerformanceTest::m_deinterlace
bool m_deinterlace
Definition: mythtv/programs/mythavtest/main.cpp:160
std
Definition: mythchrono.h:23
PlayerContext::SetPlayer
void SetPlayer(MythPlayer *newplayer)
Definition: playercontext.cpp:447
VideoPerformanceTest::VideoPerformanceTest
VideoPerformanceTest(QString filename, bool decodeno, bool onlydecode, std::chrono::seconds runfor, bool deint, bool gpu)
Definition: mythtv/programs/mythavtest/main.cpp:35
PlayerContext::SetRingBuffer
void SetRingBuffer(MythMediaBuffer *Buffer)
Definition: playercontext.cpp:488
VideoPerformanceTest::m_secondsToRun
std::chrono::seconds m_secondsToRun
Definition: mythtv/programs/mythavtest/main.cpp:159
mythcontext.h
GENERIC_EXIT_NO_MYTHCONTEXT
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
kAudioMuted
@ kAudioMuted
Definition: mythplayer.h:75
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
PlayerContext
Definition: playercontext.h:52
MythMediaBuffer::Create
static MythMediaBuffer * Create(const QString &Filename, bool Write, bool UseReadAhead=true, std::chrono::milliseconds Timeout=kDefaultOpenTimeout, bool StreamOnly=false)
Creates a RingBuffer instance.
Definition: mythmediabuffer.cpp:97
VideoPerformanceTest
Definition: mythtv/programs/mythavtest/main.cpp:32
VideoPerformanceTest::m_decodeOnly
bool m_decodeOnly
Definition: mythtv/programs/mythavtest/main.cpp:158
VideoPerformanceTest::~VideoPerformanceTest
~VideoPerformanceTest()
Definition: mythtv/programs/mythavtest/main.cpp:46
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2756
MythVideoFrame
Definition: mythframe.h:88
exitcodes.h
MythCommandLineParser::toInt
int toInt(const QString &key) const
Returns stored QVariant as an integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2117
GetMythUI
MythUIHelper * GetMythUI()
Definition: mythuihelper.cpp:66
MythVideoOutput::GetLastShownFrame
virtual MythVideoFrame * GetLastShownFrame()
Returns frame from the head of the ready to be displayed queue, if StartDisplayingFrame has been call...
Definition: mythvideoout.cpp:315
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
GENERIC_EXIT_NO_THEME
#define GENERIC_EXIT_NO_THEME
No Theme available.
Definition: exitcodes.h:14
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1570
MythMainWindow
Definition: mythmainwindow.h:35
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:131
DecoderBase
Definition: decoderbase.h:120
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:922
tv_play.h
MythVideoOutput::DoneDisplayingFrame
virtual void DoneDisplayingFrame(MythVideoFrame *Frame)
Releases frame returned from GetLastShownFrame() onto the queue of frames ready for decoding onto.
Definition: mythvideoout.cpp:416
SignalHandler::Init
static void Init(QList< int > &signallist, QObject *parent=nullptr)
Definition: signalhandling.cpp:124
GENERIC_EXIT_DB_OUTOFDATE
#define GENERIC_EXIT_DB_OUTOFDATE
Database needs upgrade.
Definition: exitcodes.h:16