MythTV  master
mythpreviewgen.cpp
Go to the documentation of this file.
1 // C++ headers
2 #include <cerrno>
3 #include <csignal>
4 #include <cstdlib>
5 #include <fcntl.h>
6 #include <fstream>
7 #include <iostream>
8 #include <libgen.h>
9 #include <sys/stat.h>
10 #include <sys/time.h> // for setpriority
11 #include <sys/types.h>
12 #include <unistd.h>
13 
14 // Qt
15 #include <QtGlobal>
16 #ifndef _WIN32
17 #include <QCoreApplication>
18 #else
19 #include <QApplication>
20 #endif
21 #include <QDir>
22 #include <QFile>
23 #include <QFileInfo>
24 #include <QMap>
25 
26 // MythTV
27 #include "libmyth/mythcontext.h"
29 #include "libmythbase/compat.h"
30 #include "libmythbase/exitcodes.h"
32 #include "libmythbase/mythdb.h"
34 #include "libmythbase/mythversion.h"
38 #include "libmythtv/dbcheck.h"
41 
42 //MythPreviewGen
44 
45 #define LOC QString("MythPreviewGen: ")
46 #define LOC_WARN QString("MythPreviewGen, Warning: ")
47 #define LOC_ERR QString("MythPreviewGen, Error: ")
48 
49 #ifdef Q_OS_MACOS
50 // 10.6 uses some file handles for its new Grand Central Dispatch thingy
51 static constexpr long UNUSED_FILENO { 5 };
52 #else
53 static constexpr long UNUSED_FILENO { 3 };
54 #endif
55 
56 namespace
57 {
58  void cleanup()
59  {
60  delete gContext;
61  gContext = nullptr;
63  }
64 }
65 
66 int preview_helper(uint chanid, QDateTime starttime,
67  long long previewFrameNumber, std::chrono::seconds previewSeconds,
68  const QSize previewSize,
69  const QString &infile, const QString &outfile)
70 {
71  // Lower scheduling priority, to avoid problems with recordings.
72  if (setpriority(PRIO_PROCESS, 0, 9))
73  LOG(VB_GENERAL, LOG_ERR, "Setting priority failed." + ENO);
74 
75  if (!QFileInfo(infile).isReadable() && ((chanid == 0U) || !starttime.isValid()))
76  ProgramInfo::QueryKeyFromPathname(infile, chanid, starttime);
77 
78  ProgramInfo *pginfo = nullptr;
79  if (chanid && starttime.isValid())
80  {
81  pginfo = new ProgramInfo(chanid, starttime);
82  if (!pginfo->GetChanID())
83  {
84  LOG(VB_GENERAL, LOG_ERR,
85  QString("Cannot locate recording made on '%1' at '%2'")
86  .arg(chanid).arg(starttime.toString(Qt::ISODate)));
87  delete pginfo;
88  return GENERIC_EXIT_NOT_OK;
89  }
90  pginfo->SetPathname(pginfo->GetPlaybackURL(false, true));
91  }
92  else if (!infile.isEmpty())
93  {
94  if (!QFileInfo(infile).isReadable())
95  {
96  LOG(VB_GENERAL, LOG_ERR,
97  QString("Cannot read this file '%1'").arg(infile));
98  return GENERIC_EXIT_NOT_OK;
99  }
100  pginfo = new ProgramInfo(
101  infile, ""/*plot*/, ""/*title*/, ""/*sortTitle*/, ""/*subtitle*/,
102  ""/*sortSubtitle*/, ""/*director*/, 0/*season*/, 0/*episode*/,
103  ""/*inetref*/, 120min/*length_in_minutes*/, 1895/*year*/, ""/*id*/);
104  }
105  else
106  {
107  LOG(VB_GENERAL, LOG_ERR, "Cannot locate recording to preview");
108  return GENERIC_EXIT_NOT_OK;
109  }
110 
111  auto *previewgen = new PreviewGenerator(pginfo, QString(),
113 
114  if (previewFrameNumber >= 0)
115  previewgen->SetPreviewTimeAsFrameNumber(previewFrameNumber);
116 
117  if (previewSeconds >= 0s)
118  previewgen->SetPreviewTimeAsSeconds(previewSeconds);
119 
120  previewgen->SetOutputSize(previewSize);
121  previewgen->SetOutputFilename(outfile);
122  bool ok = previewgen->RunReal();
123  previewgen->deleteLater();
124 
125  delete pginfo;
126 
127  return (ok) ? GENERIC_EXIT_OK : GENERIC_EXIT_NOT_OK;
128 }
129 
130 int main(int argc, char **argv)
131 {
133  if (!cmdline.Parse(argc, argv))
134  {
135  cmdline.PrintHelp();
137  }
138 
139  if (cmdline.toBool("showhelp"))
140  {
141  cmdline.PrintHelp();
142  return GENERIC_EXIT_OK;
143  }
144 
145  if (cmdline.toBool("showversion"))
146  {
148  return GENERIC_EXIT_OK;
149  }
150 
151 #ifndef _WIN32
152  for (long i = UNUSED_FILENO; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
153  close(i);
154  QCoreApplication a(argc, argv);
155 #else
156  // MINGW application needs a window to receive messages
157  // such as socket notifications :[
158  QApplication a(argc, argv);
159 #endif
160  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHPREVIEWGEN);
161 
162  int retval = cmdline.ConfigureLogging();
163  if (retval != GENERIC_EXIT_OK)
164  return retval;
165 
166  if ((!cmdline.toBool("chanid") || !cmdline.toBool("starttime")) &&
167  !cmdline.toBool("inputfile"))
168  {
169  std::cerr << "--generate-preview must be accompanied by either " <<std::endl
170  << "\nboth --chanid and --starttime parameters, " << std::endl
171  << "\nor the --infile parameter." << std::endl;
173  }
174 
176 
177  // Don't listen to console input
178  close(0);
179 
180  CleanupGuard callCleanup(cleanup);
181 
182 #ifndef _WIN32
184 #endif
185 
186  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
187  LOG(VB_GENERAL, LOG_WARNING, LOC + "Unable to ignore SIGPIPE");
188 
189  gContext = new MythContext(MYTH_BINARY_VERSION);
190 
191  if (!gContext->Init(false))
192  {
193  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext.");
195  }
196 
197  int ret = preview_helper(
198  cmdline.toUInt("chanid"), cmdline.toDateTime("starttime"),
199  cmdline.toLongLong("frame"), std::chrono::seconds(cmdline.toLongLong("seconds")),
200  cmdline.toSize("size"),
201  cmdline.toString("inputfile"), cmdline.toString("outputfile"));
202  return ret;
203 }
204 
205 /* vim: set expandtab tabstop=4 shiftwidth=4: */
ENO
#define ENO
This can be appended to the LOG args with "+".
Definition: mythlogging.h:73
mythdb.h
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
UNUSED_FILENO
static constexpr long UNUSED_FILENO
Definition: mythpreviewgen.cpp:51
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
PreviewGenerator
This class creates a preview image of a recording.
Definition: previewgenerator.h:27
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
mythsystemevent.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:11
close
#define close
Definition: compat.h:43
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1544
ProgramInfo::SetPathname
void SetPathname(const QString &pn)
Definition: programinfo.cpp:2438
programinfo.h
mythlogging.h
GENERIC_EXIT_NO_MYTHCONTEXT
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:14
preview_helper
int preview_helper(uint chanid, QDateTime starttime, long long previewFrameNumber, std::chrono::seconds previewSeconds, const QSize previewSize, const QString &infile, const QString &outfile)
Definition: mythpreviewgen.cpp:66
dbcheck.h
signalhandling.h
main
int main(int argc, char **argv)
Definition: mythpreviewgen.cpp:130
MythPreviewGeneratorCommandLineParser
Definition: mythpreviewgen_commandlineparser.h:6
CleanupGuard
Definition: cleanupguard.h:6
compat.h
SIGPIPE
#define SIGPIPE
Definition: compat.h:139
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1372
MythCommandLineParser::toUInt
uint toUInt(const QString &key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2236
mythpreviewgen_commandlineparser.h
storagegroup.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1388
GENERIC_EXIT_NOT_OK
@ GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:12
uint
unsigned int uint
Definition: compat.h:81
cleanup
static QString cleanup(const QString &str)
Definition: remoteencoder.cpp:673
SignalHandler::Init
static void Init(QObject *parent=nullptr)
Definition: signalhandling.cpp:127
ProgramInfo::GetChanID
uint GetChanID(void) const
This is the unique key used in the database to locate tuning information.
Definition: programinfo.h:372
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
MythCommandLineParser::toString
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2344
mythcorecontext.h
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2187
cleanupguard.h
MythCommandLineParser::toLongLong
long long toLongLong(const QString &key) const
Returns stored QVariant as a long integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2263
setpriority
#define setpriority(x, y, z)
Definition: compat.h:130
ProgramInfo::GetPlaybackURL
QString GetPlaybackURL(bool checkMaster=false, bool forceCheckLocal=false)
Returns filename or URL to be used to play back this recording.
Definition: programinfo.cpp:2546
MythDate::ISODate
@ ISODate
Default UTC.
Definition: mythdate.h:17
mythcontext.h
MythCommandLineParser::toSize
QSize toSize(const QString &key) const
Returns stored QVariant as a QSize value, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2317
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2849
PreviewGenerator::kLocal
@ kLocal
Definition: previewgenerator.h:43
PRIO_PROCESS
#define PRIO_PROCESS
Definition: compat.h:129
previewgenerator.h
exitcodes.h
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:16
MYTH_APPNAME_MYTHPREVIEWGEN
static constexpr const char * MYTH_APPNAME_MYTHPREVIEWGEN
Definition: mythcorecontext.h:26
MythCommandLineParser::toDateTime
QDateTime toDateTime(const QString &key) const
Returns stored QVariant as a QDateTime, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2441
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:1603
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:134
LOC
#define LOC
Definition: mythpreviewgen.cpp:45
ProgramInfo::QueryKeyFromPathname
static bool QueryKeyFromPathname(const QString &pathname, uint &chanid, QDateTime &recstartts)
Definition: programinfo.cpp:1210