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 
30 #include "mythcontext.h"
31 #include "mythcorecontext.h"
32 #include "mythversion.h"
33 #include "mythdb.h"
34 #include "exitcodes.h"
35 #include "compat.h"
36 #include "storagegroup.h"
37 #include "programinfo.h"
38 #include "dbcheck.h"
39 #include "previewgenerator.h"
40 #include "commandlineparser.h"
41 #include "mythsystemevent.h"
42 #include "loggingserver.h"
43 #include "mythlogging.h"
44 #include "signalhandling.h"
45 #include "cleanupguard.h"
46 
47 #define LOC QString("MythPreviewGen: ")
48 #define LOC_WARN QString("MythPreviewGen, Warning: ")
49 #define LOC_ERR QString("MythPreviewGen, Error: ")
50 
51 #ifdef Q_OS_MACX
52  // 10.6 uses some file handles for its new Grand Central Dispatch thingy
53  #define UNUSED_FILENO 5
54 #else
55  #define UNUSED_FILENO 3
56 #endif
57 
58 namespace
59 {
60  void cleanup()
61  {
62  delete gContext;
63  gContext = nullptr;
65  }
66 }
67 
68 int preview_helper(uint chanid, QDateTime starttime,
69  long long previewFrameNumber, std::chrono::seconds previewSeconds,
70  const QSize previewSize,
71  const QString &infile, const QString &outfile)
72 {
73  // Lower scheduling priority, to avoid problems with recordings.
74  if (setpriority(PRIO_PROCESS, 0, 9))
75  LOG(VB_GENERAL, LOG_ERR, "Setting priority failed." + ENO);
76 
77  if (!QFileInfo(infile).isReadable() && ((chanid == 0U) || !starttime.isValid()))
78  ProgramInfo::QueryKeyFromPathname(infile, chanid, starttime);
79 
80  ProgramInfo *pginfo = nullptr;
81  if (chanid && starttime.isValid())
82  {
83  pginfo = new ProgramInfo(chanid, starttime);
84  if (!pginfo->GetChanID())
85  {
86  LOG(VB_GENERAL, LOG_ERR,
87  QString("Cannot locate recording made on '%1' at '%2'")
88  .arg(chanid).arg(starttime.toString(Qt::ISODate)));
89  delete pginfo;
90  return GENERIC_EXIT_NOT_OK;
91  }
92  pginfo->SetPathname(pginfo->GetPlaybackURL(false, true));
93  }
94  else if (!infile.isEmpty())
95  {
96  if (!QFileInfo(infile).isReadable())
97  {
98  LOG(VB_GENERAL, LOG_ERR,
99  QString("Cannot read this file '%1'").arg(infile));
100  return GENERIC_EXIT_NOT_OK;
101  }
102  pginfo = new ProgramInfo(
103  infile, ""/*plot*/, ""/*title*/, ""/*sortTitle*/, ""/*subtitle*/,
104  ""/*sortSubtitle*/, ""/*director*/, 0/*season*/, 0/*episode*/,
105  ""/*inetref*/, 120min/*length_in_minutes*/, 1895/*year*/, ""/*id*/);
106  }
107  else
108  {
109  LOG(VB_GENERAL, LOG_ERR, "Cannot locate recording to preview");
110  return GENERIC_EXIT_NOT_OK;
111  }
112 
113  auto *previewgen = new PreviewGenerator(pginfo, QString(),
115 
116  if (previewFrameNumber >= 0)
117  previewgen->SetPreviewTimeAsFrameNumber(previewFrameNumber);
118 
119  if (previewSeconds >= 0s)
120  previewgen->SetPreviewTimeAsSeconds(previewSeconds);
121 
122  previewgen->SetOutputSize(previewSize);
123  previewgen->SetOutputFilename(outfile);
124  bool ok = previewgen->RunReal();
125  previewgen->deleteLater();
126 
127  delete pginfo;
128 
129  return (ok) ? GENERIC_EXIT_OK : GENERIC_EXIT_NOT_OK;
130 }
131 
132 int main(int argc, char **argv)
133 {
135  if (!cmdline.Parse(argc, argv))
136  {
137  cmdline.PrintHelp();
139  }
140 
141  if (cmdline.toBool("showhelp"))
142  {
143  cmdline.PrintHelp();
144  return GENERIC_EXIT_OK;
145  }
146 
147  if (cmdline.toBool("showversion"))
148  {
150  return GENERIC_EXIT_OK;
151  }
152 
153 #ifndef _WIN32
154  for (long i = UNUSED_FILENO; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
155  close(i);
156  QCoreApplication a(argc, argv);
157 #else
158  // MINGW application needs a window to receive messages
159  // such as socket notifications :[
160  QApplication a(argc, argv);
161 #endif
162  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHPREVIEWGEN);
163 
164  int retval = cmdline.ConfigureLogging();
165  if (retval != GENERIC_EXIT_OK)
166  return retval;
167 
168  if ((!cmdline.toBool("chanid") || !cmdline.toBool("starttime")) &&
169  !cmdline.toBool("inputfile"))
170  {
171  std::cerr << "--generate-preview must be accompanied by either " <<std::endl
172  << "\nboth --chanid and --starttime parameters, " << std::endl
173  << "\nor the --infile parameter." << std::endl;
175  }
176 
178 
179  // Don't listen to console input
180  close(0);
181 
182  CleanupGuard callCleanup(cleanup);
183 
184 #ifndef _WIN32
185  QList<int> signallist;
186  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
187  << SIGILL;
188 #if ! CONFIG_DARWIN
189  signallist << SIGRTMIN;
190 #endif
191  SignalHandler::Init(signallist);
192  SignalHandler::SetHandler(SIGHUP, logSigHup);
193 #endif
194 
195  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
196  LOG(VB_GENERAL, LOG_WARNING, LOC + "Unable to ignore SIGPIPE");
197 
199 
200  if (!gContext->Init(false))
201  {
202  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext.");
204  }
205 
206  int ret = preview_helper(
207  cmdline.toUInt("chanid"), cmdline.toDateTime("starttime"),
208  cmdline.toLongLong("frame"), std::chrono::seconds(cmdline.toLongLong("seconds")),
209  cmdline.toSize("size"),
210  cmdline.toString("inputfile"), cmdline.toString("outputfile"));
211  return ret;
212 }
213 
214 /* 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
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
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: mythtv/programs/mythpreviewgen/main.cpp:68
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:1449
ProgramInfo::SetPathname
void SetPathname(const QString &pn)
Definition: programinfo.cpp:2436
programinfo.h
mythlogging.h
UNUSED_FILENO
#define UNUSED_FILENO
Definition: mythtv/programs/mythpreviewgen/main.cpp:53
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:214
SIGPIPE
#define SIGPIPE
Definition: compat.h:219
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1277
MythCommandLineParser::toUInt
uint toUInt(const QString &key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2144
storagegroup.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1293
uint
unsigned int uint
Definition: compat.h:140
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:371
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythtv/programs/mythcommflag/main.cpp:74
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:2252
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:2095
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:2171
setpriority
#define setpriority(x, y, z)
Definition: compat.h:204
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:2544
MythDate::ISODate
@ ISODate
Default UTC.
Definition: mythdate.h:17
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:2225
LOC
#define LOC
Definition: mythtv/programs/mythpreviewgen/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:2756
PreviewGenerator::kLocal
@ kLocal
Definition: previewgenerator.h:42
PRIO_PROCESS
#define PRIO_PROCESS
Definition: compat.h:203
previewgenerator.h
exitcodes.h
loggingserver.h
main
int main(int argc, char **argv)
Definition: mythtv/programs/mythpreviewgen/main.cpp:132
MythCommandLineParser::toDateTime
QDateTime toDateTime(const QString &key) const
Returns stored QVariant as a QDateTime, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2349
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
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:1213