MythTV  master
mythavtest.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <unistd.h>
3 #include <utility>
4 
5 #include <QtGlobal>
6 #include <QApplication>
7 #include <QDir>
8 #include <QString>
9 #include <QSurfaceFormat>
10 #include <QTime>
11 
12 // libmyth*
13 #include "libmyth/mythcontext.h"
14 #include "libmythbase/compat.h"
15 #include "libmythbase/exitcodes.h"
16 #include "libmythbase/mythdbcon.h"
19 #include "libmythbase/mythversion.h"
22 #include "libmythtv/dbcheck.h"
23 #include "libmythtv/jitterometer.h"
24 #include "libmythtv/mythplayerui.h"
25 #include "libmythtv/mythvideoout.h"
26 #include "libmythtv/tv_play.h"
28 #include "libmythui/mythuihelper.h"
29 
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_secondsToRun(std::clamp(runfor, 1s, 3600s)),
41  m_deinterlace(deint),
42  m_allowGpu(gpu)
43  {
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 
217  gContext = new MythContext(MYTH_BINARY_VERSION, true);
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_MACOS)
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
253 #endif
254 
255  if (cmdline.toBool("test"))
256  {
257  std::chrono::seconds seconds = 5s;
258  if (!cmdline.toString("seconds").isEmpty())
259  seconds = std::chrono::seconds(cmdline.toInt("seconds"));
260  auto *test = new VideoPerformanceTest(filename,
261  cmdline.toBool("nodecode"),
262  cmdline.toBool("decodeonly"), seconds,
263  cmdline.toBool("deinterlace"),
264  cmdline.toBool("gpu"));
265  test->Test();
266  delete test;
267  }
268  else
269  {
270  TV::InitKeys();
271  setHttpProxy();
272 
273  if (!UpgradeTVDatabaseSchema(false))
274  {
275  LOG(VB_GENERAL, LOG_ERR, "Fatal Error: Incorrect database schema.");
276  delete gContext;
278  }
279 
280  if (filename.isEmpty())
281  {
282  TV::StartTV(nullptr, kStartTVNoFlags);
283  }
284  else
285  {
286  ProgramInfo pginfo(filename);
287  TV::StartTV(&pginfo, kStartTVNoFlags);
288  }
289  }
291 
292  delete gContext;
293 
295 
296  return GENERIC_EXIT_OK;
297 }
298 
299 /* vim: set expandtab tabstop=4 shiftwidth=4: */
themedir
static QString themedir
Definition: mythdirs.cpp:21
TV::InitKeys
static void InitKeys()
Definition: tv_play.cpp:457
DEINT_DRIVER
@ DEINT_DRIVER
Definition: mythframe.h:75
GENERIC_EXIT_SETUP_ERROR
@ GENERIC_EXIT_SETUP_ERROR
Incorrectly setup system.
Definition: exitcodes.h:22
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:796
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:40
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:361
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
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: mythavtest.cpp:162
MythMediaBuffer
Definition: mythmediabuffer.h:50
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
PlayerFlags
PlayerFlags
Definition: mythplayer.h:65
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:11
VideoPerformanceTest::m_noDecode
bool m_noDecode
Definition: mythavtest.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:254
MythDisplay::ConfigureQtGUI
static void ConfigureQtGUI(int SwapInterval, const MythCommandLineParser &CmdLine)
Shared static initialisation code for all MythTV GUI applications.
Definition: mythdisplay.cpp:1160
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
GENERIC_EXIT_NO_MYTHCONTEXT
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:14
kScan_Progressive
@ kScan_Progressive
Definition: videoouttypes.h:100
dbcheck.h
signalhandling.h
VideoPerformanceTest::m_allowGpu
bool m_allowGpu
Definition: mythavtest.cpp:161
VideoPerformanceTest::Test
void Test(void)
Definition: mythavtest.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:2088
DestroyMythMainWindow
void DestroyMythMainWindow(void)
Definition: mythmainwindow.cpp:112
MythUIThemeHelper::FindThemeDir
QString FindThemeDir(const QString &ThemeName, bool Fallback=true)
Returns the full path to the theme denoted by themename.
Definition: mythuithemehelper.cpp:48
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
GENERIC_EXIT_NO_THEME
@ GENERIC_EXIT_NO_THEME
No Theme available.
Definition: exitcodes.h:15
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:54
VideoPerformanceTest::m_file
QString m_file
Definition: mythavtest.cpp:156
MythCommandLineParser::ApplySettingsOverride
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Definition: mythcommandlineparser.cpp:2931
Jitterometer
Definition: jitterometer.h:43
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:410
mythuihelper.h
SignalHandler::Init
static void Init(QObject *parent=nullptr)
Definition: signalhandling.cpp:127
MYTH_APPNAME_MYTHAVTEST
static constexpr const char * MYTH_APPNAME_MYTHAVTEST
Definition: mythcorecontext.h:32
MythDeintType
MythDeintType
Definition: mythframe.h:67
MythVideoOutput::RenderFrame
virtual void RenderFrame(MythVideoFrame *Frame, FrameScanType)=0
kNoFlags
@ kNoFlags
Definition: mythplayer.h:67
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
main
int main(int argc, char *argv[])
Definition: mythavtest.cpp:165
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:2358
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:2201
VideoPerformanceTest::m_deinterlace
bool m_deinterlace
Definition: mythavtest.cpp:160
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: mythavtest.cpp:35
PlayerContext::SetRingBuffer
void SetRingBuffer(MythMediaBuffer *Buffer)
Definition: playercontext.cpp:493
VideoPerformanceTest::m_secondsToRun
std::chrono::seconds m_secondsToRun
Definition: mythavtest.cpp:159
GENERIC_EXIT_DB_OUTOFDATE
@ GENERIC_EXIT_DB_OUTOFDATE
Database needs upgrade.
Definition: exitcodes.h:17
mythcontext.h
kAudioMuted
@ kAudioMuted
Definition: mythplayer.h:75
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:102
PlayerContext
Definition: playercontext.h:53
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:98
VideoPerformanceTest
Definition: mythavtest.cpp:32
VideoPerformanceTest::m_decodeOnly
bool m_decodeOnly
Definition: mythavtest.cpp:158
VideoPerformanceTest::~VideoPerformanceTest
~VideoPerformanceTest()
Definition: mythavtest.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:2863
mythavtest_commandlineparser.h
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:2223
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:316
build_compdb.filename
filename
Definition: build_compdb.py:21
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:16
mythmainwindow.h
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:57
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1594
MythMainWindow
Definition: mythmainwindow.h:28
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:896
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:417