-
Notifications
You must be signed in to change notification settings - Fork 344
/
main.cpp
216 lines (185 loc) · 5.96 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "mythconfig.h"
#if CONFIG_DARWIN
#include <sys/aio.h> // O_SYNC
#endif
// C++ headers
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/time.h> // for setpriority
#include <sys/types.h>
#include <unistd.h>
using namespace std;
#ifndef _WIN32
#include <QCoreApplication>
#else
#include <QApplication>
#endif
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QMap>
#include <QRegExp>
#include "mythcontext.h"
#include "mythcorecontext.h"
#include "mythversion.h"
#include "mythdb.h"
#include "exitcodes.h"
#include "compat.h"
#include "storagegroup.h"
#include "programinfo.h"
#include "dbcheck.h"
#include "previewgenerator.h"
#include "commandlineparser.h"
#include "mythsystemevent.h"
#include "loggingserver.h"
#include "mythlogging.h"
#include "signalhandling.h"
#include "cleanupguard.h"
#define LOC QString("MythPreviewGen: ")
#define LOC_WARN QString("MythPreviewGen, Warning: ")
#define LOC_ERR QString("MythPreviewGen, Error: ")
#ifdef Q_OS_MACX
// 10.6 uses some file handles for its new Grand Central Dispatch thingy
#define UNUSED_FILENO 5
#else
#define UNUSED_FILENO 3
#endif
namespace
{
void cleanup()
{
delete gContext;
gContext = nullptr;
SignalHandler::Done();
}
}
int preview_helper(uint chanid, QDateTime starttime,
long long previewFrameNumber, long long previewSeconds,
const QSize &previewSize,
const QString &infile, const QString &outfile)
{
// Lower scheduling priority, to avoid problems with recordings.
if (setpriority(PRIO_PROCESS, 0, 9))
LOG(VB_GENERAL, LOG_ERR, "Setting priority failed." + ENO);
if (!QFileInfo(infile).isReadable() && (!chanid || !starttime.isValid()))
ProgramInfo::QueryKeyFromPathname(infile, chanid, starttime);
ProgramInfo *pginfo = nullptr;
if (chanid && starttime.isValid())
{
pginfo = new ProgramInfo(chanid, starttime);
if (!pginfo->GetChanID())
{
LOG(VB_GENERAL, LOG_ERR,
QString("Cannot locate recording made on '%1' at '%2'")
.arg(chanid).arg(starttime.toString(Qt::ISODate)));
delete pginfo;
return GENERIC_EXIT_NOT_OK;
}
pginfo->SetPathname(pginfo->GetPlaybackURL(false, true));
}
else if (!infile.isEmpty())
{
if (!QFileInfo(infile).isReadable())
{
LOG(VB_GENERAL, LOG_ERR,
QString("Cannot read this file '%1'").arg(infile));
return GENERIC_EXIT_NOT_OK;
}
pginfo = new ProgramInfo(
infile, ""/*plot*/, ""/*title*/, ""/*sortTitle*/, ""/*subtitle*/,
""/*sortSubtitle*/, ""/*director*/, 0/*season*/, 0/*episode*/,
""/*inetref*/, 120/*length_in_minutes*/, 1895/*year*/, ""/*id*/);
}
else
{
LOG(VB_GENERAL, LOG_ERR, "Cannot locate recording to preview");
return GENERIC_EXIT_NOT_OK;
}
auto *previewgen = new PreviewGenerator(pginfo, QString(),
PreviewGenerator::kLocal);
if (previewFrameNumber >= 0)
previewgen->SetPreviewTimeAsFrameNumber(previewFrameNumber);
if (previewSeconds >= 0)
previewgen->SetPreviewTimeAsSeconds(previewSeconds);
previewgen->SetOutputSize(previewSize);
previewgen->SetOutputFilename(outfile);
bool ok = previewgen->RunReal();
previewgen->deleteLater();
delete pginfo;
return (ok) ? GENERIC_EXIT_OK : GENERIC_EXIT_NOT_OK;
}
int main(int argc, char **argv)
{
MythPreviewGeneratorCommandLineParser cmdline;
if (!cmdline.Parse(argc, argv))
{
cmdline.PrintHelp();
return GENERIC_EXIT_INVALID_CMDLINE;
}
if (cmdline.toBool("showhelp"))
{
cmdline.PrintHelp();
return GENERIC_EXIT_OK;
}
if (cmdline.toBool("showversion"))
{
MythPreviewGeneratorCommandLineParser::PrintVersion();
return GENERIC_EXIT_OK;
}
#ifndef _WIN32
for (long i = UNUSED_FILENO; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
close(i);
QCoreApplication a(argc, argv);
#else
// MINGW application needs a window to receive messages
// such as socket notifications :[
QApplication a(argc, argv);
#endif
QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHPREVIEWGEN);
int retval = cmdline.ConfigureLogging();
if (retval != GENERIC_EXIT_OK)
return retval;
if ((!cmdline.toBool("chanid") || !cmdline.toBool("starttime")) &&
!cmdline.toBool("inputfile"))
{
cerr << "--generate-preview must be accompanied by either " <<endl
<< "\nboth --chanid and --starttime parameters, " << endl
<< "\nor the --infile parameter." << endl;
return GENERIC_EXIT_INVALID_CMDLINE;
}
///////////////////////////////////////////////////////////////////////
// Don't listen to console input
close(0);
CleanupGuard callCleanup(cleanup);
#ifndef _WIN32
QList<int> signallist;
signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
<< SIGILL;
#if ! CONFIG_DARWIN
signallist << SIGRTMIN;
#endif
SignalHandler::Init(signallist);
SignalHandler::SetHandler(SIGHUP, logSigHup);
#endif
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
LOG(VB_GENERAL, LOG_WARNING, LOC + "Unable to ignore SIGPIPE");
gContext = new MythContext(MYTH_BINARY_VERSION);
if (!gContext->Init(false))
{
LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext.");
return GENERIC_EXIT_NO_MYTHCONTEXT;
}
int ret = preview_helper(
cmdline.toUInt("chanid"), cmdline.toDateTime("starttime"),
cmdline.toLongLong("frame"), cmdline.toLongLong("seconds"),
cmdline.toSize("size"),
cmdline.toString("inputfile"), cmdline.toString("outputfile"));
return ret;
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */