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