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 <QtGlobal>
6 #include <QApplication>
7 #include <QDir>
8 #include <QString>
9 #include <QSurfaceFormat>
10 #include <QTime>
11 
12 #include "tv_play.h"
13 #include "programinfo.h"
14 #include "commandlineparser.h"
15 #include "mythplayerui.h"
16 #include "jitterometer.h"
17 
18 #include "exitcodes.h"
19 #include "mythcontext.h"
20 #include "mythversion.h"
21 #include "mythdbcon.h"
22 #include "compat.h"
23 #include "dbcheck.h"
24 #include "mythlogging.h"
25 #include "signalhandling.h"
26 #include "mythmiscutil.h"
27 #include "mythvideoout.h"
28 
29 // libmythui
30 #include "mythuihelper.h"
31 #include "mythmainwindow.h"
32 
34 {
35  public:
36  VideoPerformanceTest(QString filename, bool decodeno, bool onlydecode,
37  std::chrono::seconds runfor, bool deint, bool gpu)
38  : m_file(std::move(filename)),
39  m_noDecode(decodeno),
40  m_decodeOnly(onlydecode),
41  m_deinterlace(deint),
42  m_allowGpu(gpu)
43  {
44  m_secondsToRun = std::clamp(runfor, 1s, 3600s);
45  }
46 
48  {
49  delete m_ctx;
50  }
51 
52  void Test(void)
53  {
54  MythMediaBuffer *rb = MythMediaBuffer::Create(m_file, false, true, 2s);
55  m_ctx = new PlayerContext("VideoPerformanceTest");
56  auto *mp = new MythPlayerUI(GetMythMainWindow(), nullptr, m_ctx, static_cast<PlayerFlags>(kAudioMuted | (m_allowGpu ? kDecodeAllowGPU: kNoFlags)));
57  mp->GetAudio()->SetAudioInfo("NULL", "NULL", 0, 0);
58  mp->GetAudio()->SetNoAudio();
59  m_ctx->SetRingBuffer(rb);
60  m_ctx->SetPlayer(mp);
61  auto *pinfo = new ProgramInfo(m_file);
62  m_ctx->SetPlayingInfo(pinfo); // makes a copy
63  delete pinfo;
64 
66  if (!mp->StartPlaying())
67  {
68  LOG(VB_GENERAL, LOG_ERR, "Failed to start playback.");
69  return;
70  }
71 
72  MythVideoOutput *vo = mp->GetVideoOutput();
73  if (!vo)
74  {
75  LOG(VB_GENERAL, LOG_ERR, "No video output.");
76  return;
77  }
78 
79  LOG(VB_GENERAL, LOG_INFO, "-----------------------------------");
80  LOG(VB_GENERAL, LOG_INFO, "Ensure Sync to VBlank is disabled.");
81  LOG(VB_GENERAL, LOG_INFO, "Otherwise rate will be limited to that of the display.");
82  LOG(VB_GENERAL, LOG_INFO, "-----------------------------------");
83  LOG(VB_GENERAL, LOG_INFO, QString("Starting video performance test for '%1'.")
84  .arg(m_file));
85  LOG(VB_GENERAL, LOG_INFO, QString("Test will run for %1 seconds.")
86  .arg(m_secondsToRun.count()));
87 
88  if (m_noDecode)
89  LOG(VB_GENERAL, LOG_INFO, "No decode after startup - checking display performance");
90  else if (m_decodeOnly)
91  LOG(VB_GENERAL, LOG_INFO, "Decoding frames only - skipping display.");
92  DecoderBase* dec = mp->GetDecoder();
93  if (dec)
94  LOG(VB_GENERAL, LOG_INFO, QString("Using decoder: %1").arg(dec->GetCodecDecoderName()));
95 
96  auto *jitter = new Jitterometer("Performance: ", static_cast<int>(mp->GetFrameRate()));
97 
98  QTime start = QTime::currentTime();
99  MythVideoFrame *frame = nullptr;
100  while (true)
101  {
102  mp->ProcessCallbacks();
103  auto duration = std::chrono::milliseconds(start.msecsTo(QTime::currentTime()));
104  if (duration < 0ms || duration > m_secondsToRun)
105  {
106  LOG(VB_GENERAL, LOG_INFO, "Complete.");
107  break;
108  }
109 
110  if (mp->IsErrored())
111  {
112  LOG(VB_GENERAL, LOG_ERR, "Playback error.");
113  break;
114  }
115 
116  if (mp->GetEof() != kEofStateNone)
117  {
118  LOG(VB_GENERAL, LOG_INFO, "End of file.");
119  break;
120  }
121 
122  if (!mp->PrebufferEnoughFrames())
123  continue;
124 
125  mp->SetBuffering(false);
126  vo->StartDisplayingFrame();
127  if ((m_noDecode && !frame) || !m_noDecode)
128  frame = vo->GetLastShownFrame();
129  mp->CheckAspectRatio(frame);
130 
131  if (!m_decodeOnly)
132  {
134  vo->PrepareFrame(frame, scan);
135  vo->RenderFrame(frame, scan);
136  vo->EndFrame();
137 
138  if (doubledeint && m_deinterlace)
139  {
140  doubledeint = frame->GetDoubleRateOption(DEINT_CPU);
142  if (doubledeint && !other)
143  vo->PrepareFrame(frame, kScan_Intr2ndField);
144  vo->RenderFrame(frame, kScan_Intr2ndField);
145  vo->EndFrame();
146  }
147  }
148  if (!m_noDecode)
149  vo->DoneDisplayingFrame(frame);
150  jitter->RecordCycleTime();
151  }
152  LOG(VB_GENERAL, LOG_INFO, "-----------------------------------");
153  delete jitter;
154  }
155 
156  private:
157  QString m_file;
158  bool m_noDecode {false};
159  bool m_decodeOnly {false};
160  std::chrono::seconds m_secondsToRun {1s};
161  bool m_deinterlace {false};
162  bool m_allowGpu {false};
163  PlayerContext *m_ctx {nullptr};
164 };
165 
166 int main(int argc, char *argv[])
167 {
169  if (!cmdline.Parse(argc, argv))
170  {
171  cmdline.PrintHelp();
173  }
174 
175  if (cmdline.toBool("showhelp"))
176  {
177  cmdline.PrintHelp();
178  return GENERIC_EXIT_OK;
179  }
180 
181  if (cmdline.toBool("showversion"))
182  {
184  return GENERIC_EXIT_OK;
185  }
186 
187  int swapinterval = 1;
188  if (cmdline.toBool("test"))
189  {
190  // try and disable sync to vblank on linux x11
191  qputenv("vblank_mode", "0"); // Intel and AMD
192  qputenv("__GL_SYNC_TO_VBLANK", "0"); // NVidia
193  // the default surface format has a swap interval of 1. This is used by
194  // the MythMainwindow widget that then drives vsync for all widgets/children
195  // (i.e. MythPainterWindow) and we cannot override it on some drivers. So
196  // force the default here.
197  swapinterval = 0;
198  }
199 
200  MythDisplay::ConfigureQtGUI(swapinterval, cmdline);
201 
202  QApplication a(argc, argv);
203  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHAVTEST);
204 
205  int retval = cmdline.ConfigureLogging();
206  if (retval != GENERIC_EXIT_OK)
207  return retval;
208 
209  if (!cmdline.toString("geometry").isEmpty())
211 
212  QString filename = "";
213  if (!cmdline.toString("infile").isEmpty())
214  filename = cmdline.toString("infile");
215  else if (!cmdline.GetArgs().empty())
216  filename = cmdline.GetArgs().at(0);
217 
219  if (!gContext->Init())
220  {
221  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
223  }
224 
226 
227  QString themename = gCoreContext->GetSetting("Theme");
228  QString themedir = GetMythUI()->FindThemeDir(themename);
229  if (themedir.isEmpty())
230  {
231  QString msg = QString("Fatal Error: Couldn't find theme '%1'.")
232  .arg(themename);
233  LOG(VB_GENERAL, LOG_ERR, msg);
234  return GENERIC_EXIT_NO_THEME;
235  }
236 
237 #if defined(Q_OS_MACOS)
238  // Mac OS X doesn't define the AudioOutputDevice setting
239 #else
240  QString auddevice = gCoreContext->GetSetting("AudioOutputDevice");
241  if (auddevice.isEmpty())
242  {
243  LOG(VB_GENERAL, LOG_ERR, "Fatal Error: Audio not configured, you need "
244  "to run 'mythfrontend', not 'mythtv'.");
246  }
247 #endif
248 
249  MythMainWindow *mainWindow = GetMythMainWindow();
250  mainWindow->Init();
251 
252 #ifndef _WIN32
253  QList<int> signallist;
254  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
255  << SIGILL;
256 #ifndef Q_OS_DARWIN
257  signallist << SIGRTMIN;
258 #endif
259  SignalHandler::Init(signallist);
260  signal(SIGHUP, SIG_IGN);
261 #endif
262 
263  if (cmdline.toBool("test"))
264  {
265  std::chrono::seconds seconds = 5s;
266  if (!cmdline.toString("seconds").isEmpty())
267  seconds = std::chrono::seconds(cmdline.toInt("seconds"));
268  auto *test = new VideoPerformanceTest(filename,
269  cmdline.toBool("nodecode"),
270  cmdline.toBool("decodeonly"), seconds,
271  cmdline.toBool("deinterlace"),
272  cmdline.toBool("gpu"));
273  test->Test();
274  delete test;
275  }
276  else
277  {
278  TV::InitKeys();
279  setHttpProxy();
280 
281  if (!UpgradeTVDatabaseSchema(false))
282  {
283  LOG(VB_GENERAL, LOG_ERR, "Fatal Error: Incorrect database schema.");
284  delete gContext;
286  }
287 
288  if (filename.isEmpty())
289  {
290  TV::StartTV(nullptr, kStartTVNoFlags);
291  }
292  else
293  {
294  ProgramInfo pginfo(filename);
295  TV::StartTV(&pginfo, kStartTVNoFlags);
296  }
297  }
299 
300  delete gContext;
301 
303 
304  return GENERIC_EXIT_OK;
305 }
306 
307 /* vim: set expandtab tabstop=4 shiftwidth=4: */
themedir
static QString themedir
Definition: mythdirs.cpp:21
TV::InitKeys
static void InitKeys()
Definition: tv_play.cpp:465
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:857
MythMainWindow::Init
void Init(bool MayReInit=true)
Definition: mythmainwindow.cpp:638
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:507
UpgradeTVDatabaseSchema
bool UpgradeTVDatabaseSchema(const bool upgradeAllowed, const bool upgradeIfNoUI, const bool informSystemd)
Called from outside dbcheck.cpp to update the schema.
Definition: dbcheck.cpp:360
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
kStartTVNoFlags
@ kStartTVNoFlags
Definition: tv_play.h:112
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:163
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:158
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:262
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:166
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:1553
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:162
VideoPerformanceTest::Test
void Test(void)
Definition: mythtv/programs/mythavtest/main.cpp:52
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:2088
GENERIC_EXIT_SETUP_ERROR
#define GENERIC_EXIT_SETUP_ERROR
Incorrectly setup system.
Definition: exitcodes.h:21
DestroyMythMainWindow
void DestroyMythMainWindow(void)
Definition: mythmainwindow.cpp:112
SIGHUP
#define SIGHUP
Definition: compat.h:216
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:1381
jitterometer.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1397
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:61
commandlineparser.h
VideoPerformanceTest::m_file
QString m_file
Definition: mythtv/programs/mythavtest/main.cpp:157
MythCommandLineParser::ApplySettingsOverride
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Definition: mythcommandlineparser.cpp:2929
Jitterometer
Definition: jitterometer.h:43
MYTH_APPNAME_MYTHAVTEST
#define MYTH_APPNAME_MYTHAVTEST
Definition: mythcorecontext.h:33
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:75
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:2356
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:2199
VideoPerformanceTest::m_deinterlace
bool m_deinterlace
Definition: mythtv/programs/mythavtest/main.cpp:161
std
Definition: mythchrono.h:23
PlayerContext::SetPlayer
void SetPlayer(MythPlayer *newplayer)
Definition: playercontext.cpp:452
VideoPerformanceTest::VideoPerformanceTest
VideoPerformanceTest(QString filename, bool decodeno, bool onlydecode, std::chrono::seconds runfor, bool deint, bool gpu)
Definition: mythtv/programs/mythavtest/main.cpp:36
PlayerContext::SetRingBuffer
void SetRingBuffer(MythMediaBuffer *Buffer)
Definition: playercontext.cpp:493
VideoPerformanceTest::m_secondsToRun
std::chrono::seconds m_secondsToRun
Definition: mythtv/programs/mythavtest/main.cpp:160
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:102
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:33
VideoPerformanceTest::m_decodeOnly
bool m_decodeOnly
Definition: mythtv/programs/mythavtest/main.cpp:159
VideoPerformanceTest::~VideoPerformanceTest
~VideoPerformanceTest()
Definition: mythtv/programs/mythavtest/main.cpp:47
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2860
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:2221
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:1571
MythMainWindow
Definition: mythmainwindow.h:35
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:134
DecoderBase
Definition: decoderbase.h:120
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:924
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:127
GENERIC_EXIT_DB_OUTOFDATE
#define GENERIC_EXIT_DB_OUTOFDATE
Database needs upgrade.
Definition: exitcodes.h:16