MythTV  master
mythtv/programs/mythccextractor/main.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 // C++ headers
4 #include <iostream>
5 using namespace std;
6 
7 // Qt headers
8 #include <QCoreApplication>
9 #include <QString>
10 #include <QtCore>
11 #include <QtGui>
12 
13 // MythTV headers
14 #include "mythccextractorplayer.h"
15 #include "commandlineparser.h"
16 #include "mythcontext.h"
17 #include "mythversion.h"
18 #include "programinfo.h"
19 #include "ringbuffer.h"
20 #include "exitcodes.h"
21 #include "signalhandling.h"
22 #include "loggingserver.h"
23 #include "cleanupguard.h"
24 
25 namespace {
26  void cleanup()
27  {
28  delete gContext;
29  gContext = nullptr;
31  }
32 }
33 
34 static int RunCCExtract(ProgramInfo &program_info, const QString & destdir)
35 {
36  QString filename = program_info.GetPlaybackURL();
37  if (filename.startsWith("myth://"))
38  {
39  QString msg =
40  QString("Only locally accessible files are supported (%1).")
41  .arg(program_info.GetPathname());
42  cerr << qPrintable(msg) << endl;
44  }
45 
46  if (!QFile::exists(filename))
47  {
48  cerr << qPrintable(
49  QString("Could not open input file (%1).").arg(filename)) << endl;
51  }
52 
53  RingBuffer *tmprbuf = RingBuffer::Create(filename, false);
54  if (!tmprbuf)
55  {
56  cerr << qPrintable(QString("Unable to create RingBuffer for %1")
57  .arg(filename)) << endl;
59  }
60 
61  if (program_info.GetRecordingEndTime() > MythDate::current())
62  {
63  cout << "Program will end @ "
64  << qPrintable(program_info.GetRecordingEndTime(MythDate::ISODate))
65  << endl;
66  tmprbuf->SetWaitForWrite();
67  }
68 
69  auto flags = (PlayerFlags)(kVideoIsNull | kAudioMuted |
73  auto *ccp = new MythCCExtractorPlayer(flags, true, filename, destdir);
74  auto *ctx = new PlayerContext(kCCExtractorInUseID);
75  ctx->SetPlayingInfo(&program_info);
76  ctx->SetRingBuffer(tmprbuf);
77  ctx->SetPlayer(ccp);
78 
79  ccp->SetPlayerInfo(nullptr, nullptr, ctx);
80  if (ccp->OpenFile() < 0)
81  {
82  cerr << "Failed to open " << qPrintable(filename) << endl;
83  return GENERIC_EXIT_NOT_OK;
84  }
85  if (!ccp->run())
86  {
87  cerr << "Failed to decode " << qPrintable(filename) << endl;
88  return GENERIC_EXIT_NOT_OK;
89  }
90 
91  delete ctx;
92 
93  return GENERIC_EXIT_OK;
94 }
95 
96 int main(int argc, char *argv[])
97 {
98  QCoreApplication a(argc, argv);
99 
100  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHCCEXTRACTOR);
101 
103  if (!cmdline.Parse(argc, argv))
104  {
105  cmdline.PrintHelp();
107  }
108 
109  int retval = cmdline.ConfigureLogging("none");
110  if (retval != GENERIC_EXIT_OK)
111  return retval;
112 
113  if (cmdline.toBool("showhelp"))
114  {
115  cmdline.PrintHelp();
116  return GENERIC_EXIT_OK;
117  }
118 
119  if (cmdline.toBool("showversion"))
120  {
122  return GENERIC_EXIT_OK;
123  }
124 
125  QString infile = cmdline.toString("inputfile");
126  if (infile.isEmpty())
127  {
128  cerr << "The input file --infile is required" << endl;
130  }
131 
132  QString destdir = cmdline.toString("destdir");
133 
134  bool useDB = !QFile::exists(infile);
135 
136  CleanupGuard callCleanup(cleanup);
137 
138 #ifndef _WIN32
139  QList<int> signallist;
140  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
141  << SIGILL;
142 #if ! CONFIG_DARWIN
143  signallist << SIGRTMIN;
144 #endif
145  SignalHandler::Init(signallist);
146  SignalHandler::SetHandler(SIGHUP, logSigHup);
147 #endif
148 
150  if (!gContext->Init(
151  false/*use gui*/, false/*prompt for backend*/,
152  false/*bypass auto discovery*/, !useDB/*ignoreDB*/))
153  {
154  cerr << "Failed to init MythContext, exiting." << endl;
156  }
157 
158  ProgramInfo pginfo(infile);
159  return RunCCExtract(pginfo, destdir);
160 }
161 
162 
163 /* vim: set expandtab tabstop=4 shiftwidth=4: */
Startup context for MythTV.
Definition: mythcontext.h:42
const char * kCCExtractorInUseID
PlayerFlags
Definition: mythplayer.h:86
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
#define GENERIC_EXIT_PERMISSIONS_ERROR
File permissions error.
Definition: exitcodes.h:19
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
static void PrintVersion(void)
Print application version information.
void PrintHelp(void) const
Print command line option help.
static void Init(QList< int > &signallist, QObject *parent=nullptr)
static RingBuffer * Create(const QString &xfilename, bool write, bool usereadahead=true, int timeout_ms=kDefaultOpenTimeout, bool stream_only=false)
Creates a RingBuffer instance.
Definition: ringbuffer.cpp:104
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:62
#define MYTH_APPNAME_MYTHCCEXTRACTOR
Holds information on recordings and videos.
Definition: programinfo.h:66
void SetWaitForWrite(void)
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
static void Done(void)
static void SetHandler(int signum, SigHandlerFunc handler)
MythCommFlagCommandLineParser cmdline
static int RunCCExtract(ProgramInfo &program_info, const QString &destdir)
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
#define SIGHUP
Definition: compat.h:213
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:16
int main(int argc, char *argv[])
QDateTime GetRecordingEndTime(void) const
Approximate time the recording should have ended, did end, or is intended to end.
Definition: programinfo.h:404
static QString cleanup(const QString &str)
bool Init(const bool gui=true, const bool promptForBackend=false, const bool disableAutoDiscovery=false, const bool ignoreDB=false)
Default UTC.
Definition: mythdate.h:14
Implements a file/stream reader/writer.
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
QString GetPlaybackURL(bool checkMaster=false, bool forceCheckLocal=false)
Returns filename or URL to be used to play back this recording.
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
#define GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:11
QString GetPathname(void) const
Definition: programinfo.h:335
int ConfigureLogging(const QString &mask="general", unsigned int progress=0)
Read in logging options and initialize the logging interface.