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(const 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 
73  MythCCExtractorPlayer *ccp = new MythCCExtractorPlayer(flags, true,
74  filename, destdir);
76  ctx->SetPlayingInfo(&program_info);
77  ctx->SetRingBuffer(tmprbuf);
78  ctx->SetPlayer(ccp);
79 
80  ccp->SetPlayerInfo(nullptr, nullptr, ctx);
81  if (ccp->OpenFile() < 0)
82  {
83  cerr << "Failed to open " << qPrintable(filename) << endl;
84  return GENERIC_EXIT_NOT_OK;
85  }
86  if (!ccp->run())
87  {
88  cerr << "Failed to decode " << qPrintable(filename) << endl;
89  return GENERIC_EXIT_NOT_OK;
90  }
91 
92  delete ctx;
93 
94  return GENERIC_EXIT_OK;
95 }
96 
97 int main(int argc, char *argv[])
98 {
99  QCoreApplication a(argc, argv);
100 
101  bool useDB;
102 
103  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHCCEXTRACTOR);
104 
106  if (!cmdline.Parse(argc, argv))
107  {
108  cmdline.PrintHelp();
110  }
111 
112  int retval = cmdline.ConfigureLogging("none");
113  if (retval != GENERIC_EXIT_OK)
114  return retval;
115 
116  if (cmdline.toBool("showhelp"))
117  {
118  cmdline.PrintHelp();
119  return GENERIC_EXIT_OK;
120  }
121 
122  if (cmdline.toBool("showversion"))
123  {
125  return GENERIC_EXIT_OK;
126  }
127 
128  QString infile = cmdline.toString("inputfile");
129  if (infile.isEmpty())
130  {
131  cerr << "The input file --infile is required" << endl;
133  }
134 
135  QString destdir = cmdline.toString("destdir");
136 
137  useDB = !QFile::exists(infile);
138 
139  CleanupGuard callCleanup(cleanup);
140 
141 #ifndef _WIN32
142  QList<int> signallist;
143  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
144  << SIGILL;
145 #if ! CONFIG_DARWIN
146  signallist << SIGRTMIN;
147 #endif
148  SignalHandler::Init(signallist);
149  SignalHandler::SetHandler(SIGHUP, logSigHup);
150 #endif
151 
153  if (!gContext->Init(
154  false/*use gui*/, false/*prompt for backend*/,
155  false/*bypass auto discovery*/, !useDB/*ignoreDB*/))
156  {
157  cerr << "Failed to init MythContext, exiting." << endl;
159  }
160 
161  ProgramInfo pginfo(infile);
162  return RunCCExtract(pginfo, destdir);
163 }
164 
165 
166 /* vim: set expandtab tabstop=4 shiftwidth=4: */
Startup context for MythTV.
Definition: mythcontext.h:42
const char * kCCExtractorInUseID
PlayerFlags
Definition: mythplayer.h:88
#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.
void PrintHelp(void) const
Print command line option help.
static void Init(QList< int > &signallist, QObject *parent=nullptr)
void SetRingBuffer(RingBuffer *buf)
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:63
#define MYTH_APPNAME_MYTHCCEXTRACTOR
virtual int OpenFile(uint retries=4)
Definition: mythplayer.cpp:933
Holds information on recordings and videos.
Definition: programinfo.h:66
void SetPlayingInfo(const ProgramInfo *info)
assign programinfo to the context
void SetWaitForWrite(void)
void SetPlayer(MythPlayer *newplayer)
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
void SetPlayerInfo(TV *tv, QWidget *widget, PlayerContext *ctx)
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
static int RunCCExtract(const ProgramInfo &program_info, const QString &destdir)
static void cleanup(VideoFilter *filter)
QString GetPlaybackURL(bool checkMaster=false, bool forceCheckLocal=false) const
Returns filename or URL to be used to play back this recording.
#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
bool Init(const bool gui=true, const bool promptForBackend=false, const bool disableAutoDiscovery=false, const bool ignoreDB=false)
Implements a file/stream reader/writer.
int ConfigureLogging(QString mask="general", unsigned int progress=0)
Read in logging options and initialize the logging interface.
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
#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
void PrintVersion(void) const
Print application version information.
Default UTC.
Definition: mythdate.h:14