MythTV  master
mythtv/programs/mythtranscode/main.cpp
Go to the documentation of this file.
1 // C++ headers
2 #include <cerrno>
3 #include <fcntl.h> // for open flags
4 #include <fstream>
5 #include <iostream>
6 using namespace std;
7 
8 // Qt headers
9 #include <QCoreApplication>
10 #include <QDir>
11 #include <utility>
12 
13 // MythTV headers
14 #include "mythmiscutil.h"
15 #include "exitcodes.h"
16 #include "programinfo.h"
17 #include "jobqueue.h"
18 #include "mythcontext.h"
19 #include "mythdb.h"
20 #include "mythversion.h"
21 #include "mythdate.h"
22 #include "transcode.h"
23 #include "mpeg2fix.h"
24 #include "remotefile.h"
25 #include "mythtranslation.h"
26 #include "loggingserver.h"
27 #include "mythlogging.h"
28 #include "commandlineparser.h"
29 #include "recordinginfo.h"
30 #include "signalhandling.h"
31 #include "HLS/httplivestream.h"
32 #include "cleanupguard.h"
33 
34 static void CompleteJob(int jobID, ProgramInfo *pginfo, bool useCutlist,
35  frm_dir_map_t *deleteMap, int &exitCode,
36  int resultCode, bool forceDelete);
37 
38 static int glbl_jobID = -1;
39 static QString recorderOptions = "";
40 
41 static void UpdatePositionMap(frm_pos_map_t &posMap, frm_pos_map_t &durMap, const QString& mapfile,
42  ProgramInfo *pginfo)
43 {
44  if (pginfo && mapfile.isEmpty())
45  {
48  pginfo->SavePositionMap(posMap, MARK_GOP_BYFRAME);
49  pginfo->SavePositionMap(durMap, MARK_DURATION_MS);
50  }
51  else if (!mapfile.isEmpty())
52  {
53  MarkTypes keyType = MARK_GOP_BYFRAME;
54  FILE *mapfh = fopen(mapfile.toLocal8Bit().constData(), "w");
55  if (!mapfh)
56  {
57  LOG(VB_GENERAL, LOG_ERR, QString("Could not open map file '%1'")
58  .arg(mapfile) + ENO);
59  return;
60  }
61  frm_pos_map_t::const_iterator it;
62  fprintf (mapfh, "Type: %d\n", keyType);
63  for (it = posMap.begin(); it != posMap.end(); ++it)
64  {
65  QString str = QString("%1 %2\n").arg(it.key()).arg(*it);
66  fprintf(mapfh, "%s", qPrintable(str));
67  }
68  fclose(mapfh);
69  }
70 }
71 
72 static int BuildKeyframeIndex(MPEG2fixup *m2f, QString &infile,
73  frm_pos_map_t &posMap, frm_pos_map_t &durMap, int jobID)
74 {
75  if (!m2f)
76  return 0;
77 
78  if (jobID < 0 || JobQueue::GetJobCmd(jobID) != JOB_STOP)
79  {
80  if (jobID >= 0)
82  QObject::tr("Generating Keyframe Index"));
83  int err = m2f->BuildKeyframeIndex(infile, posMap, durMap);
84  if (err)
85  return err;
86  if (jobID >= 0)
88  QObject::tr("Transcode Completed"));
89  }
90  return 0;
91 }
92 
93 static void UpdateJobQueue(float percent_done)
94 {
96  QString("%1% ").arg(percent_done, 0, 'f', 1) +
97  QObject::tr("Completed"));
98 }
99 
100 static int CheckJobQueue()
101 {
103  {
104  LOG(VB_GENERAL, LOG_NOTICE, "Transcoding stopped by JobQueue");
105  return 1;
106  }
107  return 0;
108 }
109 
110 static int QueueTranscodeJob(ProgramInfo *pginfo, const QString& profile,
111  const QString& hostname, bool usecutlist)
112 {
113  RecordingInfo recinfo(*pginfo);
114  if (!profile.isEmpty())
116 
118  pginfo->GetRecordingStartTime(),
119  hostname, "", "",
120  usecutlist ? JOB_USE_CUTLIST : 0))
121  {
122  LOG(VB_GENERAL, LOG_NOTICE,
123  QString("Queued transcode job for chanid %1 @ %2")
124  .arg(pginfo->GetChanID())
126  return GENERIC_EXIT_OK;
127  }
128 
129  LOG(VB_GENERAL, LOG_ERR, QString("Error queuing job for chanid %1 @ %2")
130  .arg(pginfo->GetChanID())
132  return GENERIC_EXIT_DB_ERROR;
133 }
134 
135 namespace
136 {
137  void cleanup()
138  {
139  delete gContext;
140  gContext = nullptr;
142  }
143 }
144 
145 int main(int argc, char *argv[])
146 {
147  uint chanid = 0;
148  QDateTime starttime;
149  QString infile;
150  QString outfile;
151  QString profilename = QString("autodetect");
152  QString fifodir = nullptr;
153  int jobID = -1;
154  int jobType = JOB_NONE;
155  int otype = REPLEX_MPEG2;
156  bool useCutlist = false;
157  bool keyframesonly = false;
158  bool build_index = false;
159  bool fifosync = false;
160  bool mpeg2 = false;
161  bool fifo_info = false;
162  bool cleanCut = false;
163  QMap<QString, QString> settingsOverride;
164  frm_dir_map_t deleteMap;
165  frm_pos_map_t posMap;
166  frm_pos_map_t durMap;
167  int AudioTrackNo = -1;
168 
169  bool found_starttime = false;
170  bool found_chanid = false;
171  bool found_infile = false;
172  int update_index = 1;
173  bool isVideo = false;
174  bool passthru = false;
175 
177  if (!cmdline.Parse(argc, argv))
178  {
179  cmdline.PrintHelp();
181  }
182 
183  if (cmdline.toBool("showhelp"))
184  {
185  cmdline.PrintHelp();
186  return GENERIC_EXIT_OK;
187  }
188 
189  if (cmdline.toBool("showversion"))
190  {
192  return GENERIC_EXIT_OK;
193  }
194 
195  QCoreApplication a(argc, argv);
196  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHTRANSCODE);
197 
198  if (cmdline.toBool("outputfile"))
199  {
200  outfile = cmdline.toString("outputfile");
201  update_index = 0;
202  }
203 
204  bool showprogress = cmdline.toBool("showprogress");
205 
206  QString mask("general");
207  bool quiet = (outfile == "-") || showprogress;
208  int retval = cmdline.ConfigureLogging(mask, quiet);
209  if (retval != GENERIC_EXIT_OK)
210  return retval;
211 
212  if (cmdline.toBool("starttime"))
213  {
214  starttime = cmdline.toDateTime("starttime");
215  found_starttime = true;
216  }
217  if (cmdline.toBool("chanid"))
218  {
219  chanid = cmdline.toUInt("chanid");
220  found_chanid = true;
221  }
222  if (cmdline.toBool("jobid"))
223  jobID = cmdline.toInt("jobid");
224  if (cmdline.toBool("inputfile"))
225  {
226  infile = cmdline.toString("inputfile");
227  found_infile = true;
228  }
229  if (cmdline.toBool("video"))
230  isVideo = true;
231  if (cmdline.toBool("profile"))
232  profilename = cmdline.toString("profile");
233 
234  if (cmdline.toBool("usecutlist"))
235  {
236  useCutlist = true;
237  if (!cmdline.toString("usecutlist").isEmpty())
238  {
239  if (!cmdline.toBool("inputfile") && !cmdline.toBool("hls"))
240  {
241  LOG(VB_GENERAL, LOG_CRIT, "External cutlists are only allowed "
242  "when using the --infile option.");
244  }
245 
246  uint64_t last = 0;
247  QStringList cutlist = cmdline.toStringList("usecutlist", " ");
248  for (auto it = cutlist.begin(); it != cutlist.end(); ++it)
249  {
250  QStringList startend =
251  (*it).split("-", QString::SkipEmptyParts);
252  if (startend.size() == 2)
253  {
254  uint64_t start = startend.first().toULongLong();
255  uint64_t end = startend.last().toULongLong();
256 
257  if (cmdline.toBool("inversecut"))
258  {
259  LOG(VB_GENERAL, LOG_DEBUG,
260  QString("Cutting section %1-%2.")
261  .arg(last).arg(start));
262  deleteMap[start] = MARK_CUT_END;
263  deleteMap[end] = MARK_CUT_START;
264  last = end;
265  }
266  else
267  {
268  LOG(VB_GENERAL, LOG_DEBUG,
269  QString("Cutting section %1-%2.")
270  .arg(start).arg(end));
271  deleteMap[start] = MARK_CUT_START;
272  deleteMap[end] = MARK_CUT_END;
273  }
274  }
275  }
276 
277  if (cmdline.toBool("inversecut"))
278  {
279  if (deleteMap.contains(0) && (deleteMap[0] == MARK_CUT_END))
280  deleteMap.remove(0);
281  else
282  deleteMap[0] = MARK_CUT_START;
283  deleteMap[999999999] = MARK_CUT_END;
284  LOG(VB_GENERAL, LOG_DEBUG,
285  QString("Cutting section %1-999999999.")
286  .arg(last));
287  }
288 
289  // sanitize cutlist
290  if (deleteMap.count() >= 2)
291  {
292  frm_dir_map_t::iterator cur = deleteMap.begin();
293  frm_dir_map_t::iterator prev;
294  prev = cur++;
295  while (cur != deleteMap.end())
296  {
297  if (prev.value() == cur.value())
298  {
299  // two of the same type next to each other
300  QString err("Cut %1points found at %3 and %4, with no "
301  "%2 point in between.");
302  if (prev.value() == MARK_CUT_END)
303  err = err.arg("end").arg("start");
304  else
305  err = err.arg("start").arg("end");
306  LOG(VB_GENERAL, LOG_CRIT, "Invalid cutlist defined!");
307  LOG(VB_GENERAL, LOG_CRIT, err.arg(prev.key())
308  .arg(cur.key()));
310  }
311  if ( (prev.value() == MARK_CUT_START) &&
312  ((cur.key() - prev.key()) < 2) )
313  {
314  LOG(VB_GENERAL, LOG_WARNING, QString("Discarding "
315  "insufficiently long cut: %1-%2")
316  .arg(prev.key()).arg(cur.key()));
317  prev = deleteMap.erase(prev);
318  cur = deleteMap.erase(cur);
319 
320  if (cur == deleteMap.end())
321  continue;
322  }
323  prev = cur++;
324  }
325  }
326  }
327  else if (cmdline.toBool("inversecut"))
328  {
329  cerr << "Cutlist inversion requires an external cutlist be" << endl
330  << "provided using the --honorcutlist option." << endl;
332  }
333  }
334 
335  if (cmdline.toBool("cleancut"))
336  cleanCut = true;
337 
338  if (cmdline.toBool("allkeys"))
339  keyframesonly = true;
340  if (cmdline.toBool("reindex"))
341  build_index = true;
342  if (cmdline.toBool("fifodir"))
343  fifodir = cmdline.toString("fifodir");
344  if (cmdline.toBool("fifoinfo"))
345  fifo_info = true;
346  if (cmdline.toBool("fifosync"))
347  fifosync = true;
348  if (cmdline.toBool("recopt"))
349  recorderOptions = cmdline.toString("recopt");
350  if (cmdline.toBool("mpeg2"))
351  mpeg2 = true;
352  if (cmdline.toBool("ostream"))
353  {
354  if (cmdline.toString("ostream") == "dvd")
355  otype = REPLEX_DVD;
356  else if (cmdline.toString("ostream") == "ps")
357  otype = REPLEX_MPEG2;
358  else if (cmdline.toString("ostream") == "ts")
359  otype = REPLEX_TS_SD;
360  else
361  {
362  cerr << "Invalid 'ostream' type: "
363  << cmdline.toString("ostream").toLocal8Bit().constData()
364  << endl;
366  }
367  }
368  if (cmdline.toBool("audiotrack"))
369  AudioTrackNo = cmdline.toInt("audiotrack");
370  if (cmdline.toBool("passthru"))
371  passthru = true;
372  // Set if we want to delete the original file once conversion succeeded.
373  bool deleteOriginal = cmdline.toBool("delete");
374 
375  CleanupGuard callCleanup(cleanup);
376 
377 #ifndef _WIN32
378  QList<int> signallist;
379  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
380  << SIGILL;
381 #if ! CONFIG_DARWIN
382  signallist << SIGRTMIN;
383 #endif
384  SignalHandler::Init(signallist);
385  SignalHandler::SetHandler(SIGHUP, logSigHup);
386 #endif
387 
388  // Load the context
390  if (!gContext->Init(false))
391  {
392  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
394  }
395 
396  MythTranslation::load("mythfrontend");
397 
399 
400  if (jobID != -1)
401  {
402  if (JobQueue::GetJobInfoFromID(jobID, jobType, chanid, starttime))
403  {
404  found_starttime = true;
405  found_chanid = true;
406  }
407  else
408  {
409  cerr << "mythtranscode: ERROR: Unable to find DB info for "
410  << "JobQueue ID# " << jobID << endl;
412  }
413  }
414 
415  if (((!found_infile && !(found_chanid && found_starttime)) ||
416  (found_infile && (found_chanid || found_starttime))) &&
417  (!cmdline.toBool("hls")))
418  {
419  cerr << "Must specify -i OR -c AND -s options!" << endl;
421  }
422  if (isVideo && !found_infile && !cmdline.toBool("hls"))
423  {
424  cerr << "Must specify --infile to use --video" << endl;
426  }
427  if (jobID >= 0 && (found_infile || build_index))
428  {
429  cerr << "Can't specify -j with --buildindex, --video or --infile"
430  << endl;
432  }
433  if ((jobID >= 0) && build_index)
434  {
435  cerr << "Can't specify both -j and --buildindex" << endl;
437  }
438  if (keyframesonly && !fifodir.isEmpty())
439  {
440  cerr << "Cannot specify both --fifodir and --allkeys" << endl;
442  }
443  if (fifosync && fifodir.isEmpty())
444  {
445  cerr << "Must specify --fifodir to use --fifosync" << endl;
447  }
448  if (fifo_info && !fifodir.isEmpty())
449  {
450  cerr << "Cannot specify both --fifodir and --fifoinfo" << endl;
452  }
453  if (cleanCut && fifodir.isEmpty() && !fifo_info)
454  {
455  cerr << "Clean cutting works only in fifodir mode" << endl;
457  }
458  if (cleanCut && !useCutlist)
459  {
460  cerr << "--cleancut is pointless without --honorcutlist" << endl;
462  }
463 
464  if (fifo_info)
465  {
466  // Setup a dummy fifodir path, so that the "fifodir" code path
467  // is taken. The path wont actually be used.
468  fifodir = "DummyFifoPath";
469  }
470 
472  {
473  LOG(VB_GENERAL, LOG_ERR, "couldn't open db");
474  return GENERIC_EXIT_DB_ERROR;
475  }
476 
477  ProgramInfo *pginfo = nullptr;
478  if (cmdline.toBool("hls"))
479  {
480  if (cmdline.toBool("hlsstreamid"))
481  {
482  HTTPLiveStream hls(cmdline.toInt("hlsstreamid"));
483  pginfo = new ProgramInfo(hls.GetSourceFile());
484  }
485  if (pginfo == nullptr)
486  pginfo = new ProgramInfo();
487  }
488  else if (isVideo)
489  {
490  // We want the absolute file path for the filemarkup table
491  QFileInfo inf(infile);
492  infile = inf.absoluteFilePath();
493  pginfo = new ProgramInfo(infile);
494  }
495  else if (!found_infile)
496  {
497  pginfo = new ProgramInfo(chanid, starttime);
498 
499  if (!pginfo->GetChanID())
500  {
501  LOG(VB_GENERAL, LOG_ERR,
502  QString("Couldn't find recording for chanid %1 @ %2")
503  .arg(chanid).arg(starttime.toString(Qt::ISODate)));
504  delete pginfo;
506  }
507 
508  infile = pginfo->GetPlaybackURL(false, true);
509  }
510  else
511  {
512  pginfo = new ProgramInfo(infile);
513  if (!pginfo->GetChanID())
514  {
515  LOG(VB_GENERAL, LOG_ERR,
516  QString("Couldn't find a recording for filename '%1'")
517  .arg(infile));
518  delete pginfo;
520  }
521  }
522 
523  if (!pginfo)
524  {
525  LOG(VB_GENERAL, LOG_ERR, "No program info found!");
527  }
528 
529  if (cmdline.toBool("queue"))
530  {
531  QString hostname = cmdline.toString("queue");
532  return QueueTranscodeJob(pginfo, profilename, hostname, useCutlist);
533  }
534 
535  if (infile.startsWith("myth://") && (outfile.isEmpty() || outfile != "-") &&
536  fifodir.isEmpty() && !cmdline.toBool("hls") && !cmdline.toBool("avf"))
537  {
538  LOG(VB_GENERAL, LOG_ERR,
539  QString("Attempted to transcode %1. Mythtranscode is currently "
540  "unable to transcode remote files.") .arg(infile));
541  delete pginfo;
543  }
544 
545  if (outfile.isEmpty() && !build_index && fifodir.isEmpty())
546  outfile = infile + ".tmp";
547 
548  if (jobID >= 0)
549  JobQueue::ChangeJobStatus(jobID, JOB_RUNNING);
550 
551  auto *transcode = new Transcode(pginfo);
552 
553  if (!build_index)
554  {
555  if (cmdline.toBool("hlsstreamid"))
556  LOG(VB_GENERAL, LOG_NOTICE,
557  QString("Transcoding HTTP Live Stream ID %1")
558  .arg(cmdline.toInt("hlsstreamid")));
559  else if (fifodir.isEmpty())
560  LOG(VB_GENERAL, LOG_NOTICE, QString("Transcoding from %1 to %2")
561  .arg(infile).arg(outfile));
562  else
563  LOG(VB_GENERAL, LOG_NOTICE, QString("Transcoding from %1 to FIFO")
564  .arg(infile));
565  }
566 
567  if (cmdline.toBool("avf"))
568  {
569  transcode->SetAVFMode();
570 
571  if (cmdline.toBool("container"))
572  transcode->SetCMDContainer(cmdline.toString("container"));
573  if (cmdline.toBool("acodec"))
574  transcode->SetCMDAudioCodec(cmdline.toString("acodec"));
575  if (cmdline.toBool("vcodec"))
576  transcode->SetCMDVideoCodec(cmdline.toString("vcodec"));
577  }
578  else if (cmdline.toBool("hls"))
579  {
580  transcode->SetHLSMode();
581 
582  if (cmdline.toBool("hlsstreamid"))
583  transcode->SetHLSStreamID(cmdline.toInt("hlsstreamid"));
584  if (cmdline.toBool("maxsegments"))
585  transcode->SetHLSMaxSegments(cmdline.toInt("maxsegments"));
586  if (cmdline.toBool("noaudioonly"))
587  transcode->DisableAudioOnlyHLS();
588  }
589 
590  if (cmdline.toBool("avf") || cmdline.toBool("hls"))
591  {
592  if (cmdline.toBool("width"))
593  transcode->SetCMDWidth(cmdline.toInt("width"));
594  if (cmdline.toBool("height"))
595  transcode->SetCMDHeight(cmdline.toInt("height"));
596  if (cmdline.toBool("bitrate"))
597  transcode->SetCMDBitrate(cmdline.toInt("bitrate") * 1000);
598  if (cmdline.toBool("audiobitrate"))
599  transcode->SetCMDAudioBitrate(cmdline.toInt("audiobitrate") * 1000);
600  }
601 
602  if (showprogress)
603  transcode->ShowProgress(true);
604  if (!recorderOptions.isEmpty())
605  transcode->SetRecorderOptions(recorderOptions);
606  int result = 0;
607  if ((!mpeg2 && !build_index) || cmdline.toBool("hls"))
608  {
609  result = transcode->TranscodeFile(infile, outfile,
610  profilename, useCutlist,
611  (fifosync || keyframesonly), jobID,
612  fifodir, fifo_info, cleanCut, deleteMap,
613  AudioTrackNo, passthru);
614 
615  if ((result == REENCODE_OK) && (jobID >= 0))
616  {
617  JobQueue::ChangeJobArgs(jobID, "RENAME_TO_NUV");
618  RecordingInfo recInfo(pginfo->GetRecordingID());
619  RecordingFile *recFile = recInfo.GetRecordingFile();
620  recFile->m_containerFormat = formatNUV;
621  recFile->Save();
622  }
623  }
624 
625  if (fifo_info)
626  {
627  delete transcode;
628  return GENERIC_EXIT_OK;
629  }
630 
631  int exitcode = GENERIC_EXIT_OK;
632  if ((result == REENCODE_MPEG2TRANS) || mpeg2 || build_index)
633  {
634  void (*update_func)(float) = nullptr;
635  int (*check_func)() = nullptr;
636  if (useCutlist)
637  {
638  LOG(VB_GENERAL, LOG_INFO, "Honoring the cutlist while transcoding");
639  if (deleteMap.isEmpty())
640  pginfo->QueryCutList(deleteMap);
641  }
642  if (jobID >= 0)
643  {
644  glbl_jobID = jobID;
645  update_func = &UpdateJobQueue;
646  check_func = &CheckJobQueue;
647  }
648 
649  auto *m2f = new MPEG2fixup(infile, outfile,
650  &deleteMap, nullptr, false, false, 20,
651  showprogress, otype, update_func,
652  check_func);
653 
654  if (cmdline.toBool("allaudio"))
655  {
656  m2f->SetAllAudio(true);
657  }
658 
659  if (build_index)
660  {
661  int err = BuildKeyframeIndex(m2f, infile, posMap, durMap, jobID);
662  if (err)
663  {
664  delete m2f;
665  m2f = nullptr;
666  return err;
667  }
668  if (update_index)
669  UpdatePositionMap(posMap, durMap, nullptr, pginfo);
670  else
671  UpdatePositionMap(posMap, durMap, outfile + QString(".map"), pginfo);
672  }
673  else
674  {
675  result = m2f->Start();
676  if (result == REENCODE_OK)
677  {
678  result = BuildKeyframeIndex(m2f, outfile, posMap, durMap, jobID);
679  if (result == REENCODE_OK)
680  {
681  if (update_index)
682  UpdatePositionMap(posMap, durMap, nullptr, pginfo);
683  else
684  UpdatePositionMap(posMap, durMap, outfile + QString(".map"),
685  pginfo);
686  }
687  RecordingInfo recInfo(*pginfo);
688  RecordingFile *recFile = recInfo.GetRecordingFile();
689  if (otype == REPLEX_DVD || otype == REPLEX_MPEG2 ||
690  otype == REPLEX_HDTV)
691  {
693  JobQueue::ChangeJobArgs(jobID, "RENAME_TO_MPG");
694  }
695  else
696  {
698  }
699  recFile->Save();
700  }
701  }
702  delete m2f;
703  m2f = nullptr;
704  }
705 
706  if (result == REENCODE_OK)
707  {
708  if (jobID >= 0)
709  JobQueue::ChangeJobStatus(jobID, JOB_STOPPING);
710  LOG(VB_GENERAL, LOG_NOTICE, QString("%1 %2 done")
711  .arg(build_index ? "Building Index for" : "Transcoding")
712  .arg(infile));
713  }
714  else if (result == REENCODE_CUTLIST_CHANGE)
715  {
716  if (jobID >= 0)
717  JobQueue::ChangeJobStatus(jobID, JOB_RETRY);
718  LOG(VB_GENERAL, LOG_NOTICE,
719  QString("Transcoding %1 aborted because of cutlist update")
720  .arg(infile));
721  exitcode = GENERIC_EXIT_RESTART;
722  }
723  else if (result == REENCODE_STOPPED)
724  {
725  if (jobID >= 0)
726  JobQueue::ChangeJobStatus(jobID, JOB_ABORTING);
727  LOG(VB_GENERAL, LOG_NOTICE,
728  QString("Transcoding %1 stopped because of stop command")
729  .arg(infile));
730  exitcode = GENERIC_EXIT_KILLED;
731  }
732  else
733  {
734  if (jobID >= 0)
735  JobQueue::ChangeJobStatus(jobID, JOB_ERRORING);
736  LOG(VB_GENERAL, LOG_ERR, QString("Transcoding %1 failed").arg(infile));
737  exitcode = result;
738  }
739 
740  if (deleteOriginal || jobID >= 0)
741  CompleteJob(jobID, pginfo, useCutlist, &deleteMap, exitcode, result, deleteOriginal);
742 
743  transcode->deleteLater();
744 
745  return exitcode;
746 }
747 
748 static int transUnlink(const QString& filename, ProgramInfo *pginfo)
749 {
750  QString hostname = pginfo->GetHostname();
751 
752  if (!pginfo->GetStorageGroup().isEmpty() &&
753  !hostname.isEmpty())
754  {
756  QString basename = filename.section('/', -1);
757  QString uri = MythCoreContext::GenMythURL(hostname, port, basename,
758  pginfo->GetStorageGroup());
759 
760  LOG(VB_GENERAL, LOG_NOTICE, QString("Requesting delete for file '%1'.")
761  .arg(uri));
762  bool ok = RemoteFile::DeleteFile(uri);
763  if (ok)
764  return 0;
765  }
766 
767  LOG(VB_GENERAL, LOG_NOTICE, QString("Deleting file '%1'.").arg(filename));
768  return unlink(filename.toLocal8Bit().constData());
769 }
770 
771 static uint64_t ComputeNewBookmark(uint64_t oldBookmark,
772  frm_dir_map_t *deleteMap)
773 {
774  if (deleteMap == nullptr)
775  return oldBookmark;
776 
777  uint64_t subtraction = 0;
778  uint64_t startOfCutRegion = 0;
779  frm_dir_map_t delMap = *deleteMap;
780  bool withinCut = false;
781  bool firstMark = true;
782  while (!delMap.empty() && delMap.begin().key() <= oldBookmark)
783  {
784  uint64_t key = delMap.begin().key();
785  MarkTypes mark = delMap.begin().value();
786 
787  if (mark == MARK_CUT_START && !withinCut)
788  {
789  withinCut = true;
790  startOfCutRegion = key;
791  }
792  else if (mark == MARK_CUT_END && firstMark)
793  {
794  subtraction += key;
795  }
796  else if (mark == MARK_CUT_END && withinCut)
797  {
798  withinCut = false;
799  subtraction += (key - startOfCutRegion);
800  }
801  delMap.remove(key);
802  firstMark = false;
803  }
804  if (withinCut)
805  subtraction += (oldBookmark - startOfCutRegion);
806  return oldBookmark - subtraction;
807 }
808 
809 static uint64_t ReloadBookmark(ProgramInfo *pginfo)
810 {
811  MSqlQuery query(MSqlQuery::InitCon());
812  uint64_t currentBookmark = 0;
813  query.prepare("SELECT DISTINCT mark FROM recordedmarkup "
814  "WHERE chanid = :CHANID "
815  "AND starttime = :STARTIME "
816  "AND type = :MARKTYPE ;");
817  query.bindValue(":CHANID", pginfo->GetChanID());
818  query.bindValue(":STARTTIME", pginfo->GetRecordingStartTime());
819  query.bindValue(":MARKTYPE", MARK_BOOKMARK);
820  if (query.exec() && query.next())
821  {
822  currentBookmark = query.value(0).toLongLong();
823  }
824  return currentBookmark;
825 }
826 
827 static void WaitToDelete(ProgramInfo *pginfo)
828 {
829  LOG(VB_GENERAL, LOG_NOTICE,
830  "Transcode: delete old file: waiting while program is in use.");
831 
832  bool inUse = true;
833  MSqlQuery query(MSqlQuery::InitCon());
834  while (inUse)
835  {
836  query.prepare("SELECT count(*) FROM inuseprograms "
837  "WHERE chanid = :CHANID "
838  "AND starttime = :STARTTIME "
839  "AND recusage = 'player' ;");
840  query.bindValue(":CHANID", pginfo->GetChanID());
841  query.bindValue(":STARTTIME", pginfo->GetRecordingStartTime());
842  if (!query.exec() || !query.next())
843  {
844  LOG(VB_GENERAL, LOG_ERR,
845  "Transcode: delete old file: in-use query failed;");
846  inUse = false;
847  }
848  else
849  {
850  inUse = (query.value(0).toUInt() != 0);
851  }
852 
853  if (inUse)
854  {
855  const unsigned kSecondsToWait = 10;
856  LOG(VB_GENERAL, LOG_NOTICE,
857  QString("Transcode: program in use, rechecking in %1 seconds.")
858  .arg(kSecondsToWait));
859  sleep(kSecondsToWait);
860  }
861  }
862  LOG(VB_GENERAL, LOG_NOTICE, "Transcode: program is no longer in use.");
863 }
864 
865 static void CompleteJob(int jobID, ProgramInfo *pginfo, bool useCutlist,
866  frm_dir_map_t *deleteMap, int &exitCode, int resultCode, bool forceDelete)
867 {
868  int status = JOB_UNKNOWN;
869  if (jobID >= 0)
870  status = JobQueue::GetJobStatus(jobID);
871 
872  if (!pginfo)
873  {
874  if (jobID >= 0)
875  JobQueue::ChangeJobStatus(jobID, JOB_ERRORED,
876  QObject::tr("Job errored, unable to find Program Info for job"));
877  LOG(VB_GENERAL, LOG_CRIT, "MythTranscode: Cleanup errored, unable to find Program Info");
878  return;
879  }
880 
881  const QString filename = pginfo->GetPlaybackURL(false, true);
882  const QByteArray fname = filename.toLocal8Bit();
883 
884  if (resultCode == REENCODE_OK)
885  {
886  WaitToDelete(pginfo);
887 
888  // Transcoding may take several minutes. Reload the bookmark
889  // in case it changed, then save its translated value back.
890  uint64_t previousBookmark =
891  ComputeNewBookmark(ReloadBookmark(pginfo), deleteMap);
892  pginfo->SaveBookmark(previousBookmark);
893 
894  QString jobArgs;
895  if (jobID >= 0)
896  jobArgs = JobQueue::GetJobArgs(jobID);
897 
898  const QString tmpfile = filename + ".tmp";
899  const QByteArray atmpfile = tmpfile.toLocal8Bit();
900 
901  // To save the original file...
902  const QString oldfile = filename + ".old";
903  const QByteArray aoldfile = oldfile.toLocal8Bit();
904 
905  QFileInfo st(tmpfile);
906  qint64 newSize = 0;
907  if (st.exists())
908  newSize = st.size();
909 
910  QString cnf = filename;
911  if (jobID >= 0)
912  {
913  if (filename.endsWith(".mpg") && jobArgs == "RENAME_TO_NUV")
914  {
915  QString newbase = pginfo->QueryBasename();
916  cnf.replace(".mpg", ".nuv");
917  newbase.replace(".mpg", ".nuv");
918  pginfo->SaveBasename(newbase);
919  }
920  else if (filename.endsWith(".ts") &&
921  (jobArgs == "RENAME_TO_MPG"))
922  {
923  QString newbase = pginfo->QueryBasename();
924  // MPEG-TS to MPEG-PS
925  cnf.replace(".ts", ".mpg");
926  newbase.replace(".ts", ".mpg");
927  pginfo->SaveBasename(newbase);
928  }
929  }
930 
931  const QString newfile = cnf;
932  const QByteArray anewfile = newfile.toLocal8Bit();
933 
934  if (rename(fname.constData(), aoldfile.constData()) == -1)
935  {
936  LOG(VB_GENERAL, LOG_ERR,
937  QString("mythtranscode: Error Renaming '%1' to '%2'")
938  .arg(filename).arg(oldfile) + ENO);
939  }
940 
941  if (rename(atmpfile.constData(), anewfile.constData()) == -1)
942  {
943  LOG(VB_GENERAL, LOG_ERR,
944  QString("mythtranscode: Error Renaming '%1' to '%2'")
945  .arg(tmpfile).arg(newfile) + ENO);
946  }
947 
948  if (!gCoreContext->GetBoolSetting("SaveTranscoding", false) || forceDelete)
949  {
950  bool followLinks =
951  gCoreContext->GetBoolSetting("DeletesFollowLinks", false);
952 
953  LOG(VB_FILE, LOG_INFO,
954  QString("mythtranscode: About to unlink/delete file: %1")
955  .arg(oldfile));
956 
957  QFileInfo finfo(oldfile);
958  if (followLinks && finfo.isSymLink())
959  {
960  QString link = getSymlinkTarget(oldfile);
961  QByteArray alink = link.toLocal8Bit();
962  int err = transUnlink(alink.constData(), pginfo);
963  if (err)
964  {
965  LOG(VB_GENERAL, LOG_ERR,
966  QString("mythtranscode: Error deleting '%1' "
967  "pointed to by '%2'")
968  .arg(alink.constData())
969  .arg(aoldfile.constData()) + ENO);
970  }
971 
972  err = unlink(aoldfile.constData());
973  if (err)
974  {
975  LOG(VB_GENERAL, LOG_ERR,
976  QString("mythtranscode: Error deleting '%1', "
977  "a link pointing to '%2'")
978  .arg(aoldfile.constData())
979  .arg(alink.constData()) + ENO);
980  }
981  }
982  else
983  {
984  int err = transUnlink(aoldfile.constData(), pginfo);
985  if (err)
986  LOG(VB_GENERAL, LOG_ERR,
987  QString("mythtranscode: Error deleting '%1': ")
988  .arg(oldfile) + ENO);
989  }
990  }
991 
992  // Rename or delete all preview thumbnails.
993  //
994  // TODO: This cleanup should be moved to RecordingInfo, and triggered
995  // when SaveBasename() is called
996  QFileInfo fInfo(filename);
997  QStringList nameFilters;
998  nameFilters.push_back(fInfo.fileName() + "*.png");
999  nameFilters.push_back(fInfo.fileName() + "*.jpg");
1000 
1001  QDir dir (fInfo.path());
1002  QFileInfoList previewFiles = dir.entryInfoList(nameFilters);
1003 
1004  for (int nIdx = 0; nIdx < previewFiles.size(); nIdx++)
1005  {
1006  const QFileInfo& previewFile = previewFiles.at(nIdx);
1007  QString oldFileName = previewFile.absoluteFilePath();
1008 
1009  // Delete previews if cutlist was applied. They will be re-created as
1010  // required. This prevents the user from being stuck with a preview
1011  // from a cut area and ensures that the "dimensioned" previews
1012  // correspond to the new timeline
1013  if (useCutlist)
1014  {
1015  // If unlink fails, keeping the old preview is not a problem.
1016  // The RENAME_TO_NUV check below will attempt to rename the
1017  // file, if required.
1018  if (transUnlink(oldFileName.toLocal8Bit().constData(), pginfo) != -1)
1019  continue;
1020  }
1021 
1022  if (jobArgs == "RENAME_TO_NUV" || jobArgs == "RENAME_TO_MPG")
1023  {
1024  QString newExtension = "mpg";
1025  if (jobArgs == "RENAME_TO_NUV")
1026  newExtension = "nuv";
1027 
1028  QString oldSuffix = previewFile.completeSuffix();
1029 
1030  if (!oldSuffix.startsWith(newExtension))
1031  {
1032  QString newSuffix = oldSuffix;
1033  QString oldExtension = oldSuffix.section(".", 0, 0);
1034  newSuffix.replace(oldExtension, newExtension);
1035 
1036  QString newFileName = oldFileName;
1037  newFileName.replace(oldSuffix, newSuffix);
1038 
1039  if (!QFile::rename(oldFileName, newFileName))
1040  {
1041  LOG(VB_GENERAL, LOG_ERR,
1042  QString("mythtranscode: Error renaming %1 to %2")
1043  .arg(oldFileName).arg(newFileName));
1044  }
1045  }
1046  }
1047  }
1048 
1049  MSqlQuery query(MSqlQuery::InitCon());
1050 
1051  if (useCutlist)
1052  {
1053  query.prepare("DELETE FROM recordedmarkup "
1054  "WHERE chanid = :CHANID "
1055  "AND starttime = :STARTTIME "
1056  "AND type != :BOOKMARK ");
1057  query.bindValue(":CHANID", pginfo->GetChanID());
1058  query.bindValue(":STARTTIME", pginfo->GetRecordingStartTime());
1059  query.bindValue(":BOOKMARK", MARK_BOOKMARK);
1060 
1061  if (!query.exec())
1062  MythDB::DBError("Error in mythtranscode", query);
1063 
1064  query.prepare("UPDATE recorded "
1065  "SET cutlist = :CUTLIST "
1066  "WHERE chanid = :CHANID "
1067  "AND starttime = :STARTTIME ;");
1068  query.bindValue(":CUTLIST", "0");
1069  query.bindValue(":CHANID", pginfo->GetChanID());
1070  query.bindValue(":STARTTIME", pginfo->GetRecordingStartTime());
1071 
1072  if (!query.exec())
1073  MythDB::DBError("Error in mythtranscode", query);
1074 
1076  }
1077  else
1078  {
1079  query.prepare("DELETE FROM recordedmarkup "
1080  "WHERE chanid = :CHANID "
1081  "AND starttime = :STARTTIME "
1082  "AND type not in ( :COMM_START, "
1083  " :COMM_END, :BOOKMARK, "
1084  " :CUTLIST_START, :CUTLIST_END) ;");
1085  query.bindValue(":CHANID", pginfo->GetChanID());
1086  query.bindValue(":STARTTIME", pginfo->GetRecordingStartTime());
1087  query.bindValue(":COMM_START", MARK_COMM_START);
1088  query.bindValue(":COMM_END", MARK_COMM_END);
1089  query.bindValue(":BOOKMARK", MARK_BOOKMARK);
1090  query.bindValue(":CUTLIST_START", MARK_CUT_START);
1091  query.bindValue(":CUTLIST_END", MARK_CUT_END);
1092 
1093  if (!query.exec())
1094  MythDB::DBError("Error in mythtranscode", query);
1095  }
1096 
1097  if (newSize)
1098  pginfo->SaveFilesize(newSize);
1099 
1100  if (jobID >= 0)
1101  JobQueue::ChangeJobStatus(jobID, JOB_FINISHED);
1102  }
1103  else
1104  {
1105  // Not a successful run, so remove the files we created
1106  QString filename_tmp = filename + ".tmp";
1107  QByteArray fname_tmp = filename_tmp.toLocal8Bit();
1108  LOG(VB_GENERAL, LOG_NOTICE, QString("Deleting %1").arg(filename_tmp));
1109  transUnlink(fname_tmp.constData(), pginfo);
1110 
1111  QString filename_map = filename + ".tmp.map";
1112  QByteArray fname_map = filename_map.toLocal8Bit();
1113  unlink(fname_map.constData());
1114 
1115  if (jobID >= 0)
1116  {
1117  if (status == JOB_ABORTING) // Stop command was sent
1118  JobQueue::ChangeJobStatus(jobID, JOB_ABORTED,
1119  QObject::tr("Job Aborted"));
1120  else if (status != JOB_ERRORING) // Recoverable error
1121  exitCode = GENERIC_EXIT_RESTART;
1122  else // Unrecoverable error
1123  JobQueue::ChangeJobStatus(jobID, JOB_ERRORED,
1124  QObject::tr("Unrecoverable error"));
1125  }
1126  }
1127 }
1128 /* vim: set expandtab tabstop=4 shiftwidth=4: */
QDateTime toDateTime(const QString &key) const
Returns stored QVariant as a QDateTime, falling to default if not provided.
#define GENERIC_EXIT_DB_ERROR
Database error.
Definition: exitcodes.h:17
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:782
Startup context for MythTV.
Definition: mythcontext.h:42
#define REENCODE_MPEG2TRANS
Definition: transcodedefs.h:4
static bool DeleteFile(const QString &url)
Definition: remotefile.cpp:418
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:863
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
#define GENERIC_EXIT_REMOTE_FILE
Can't transcode a remote file.
Definition: exitcodes.h:30
static uint64_t ComputeNewBookmark(uint64_t oldBookmark, frm_dir_map_t *deleteMap)
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
static void PrintVersion(void)
Print application version information.
void SaveCommFlagged(CommFlagStatus flag)
Set "commflagged" field in "recorded" table to "flag".
int BuildKeyframeIndex(QString &file, frm_pos_map_t &posMap, frm_pos_map_t &durMap)
Definition: mpeg2fix.cpp:2743
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
void PrintHelp(void) const
Print command line option help.
#define GENERIC_EXIT_RESTART
Need to restart transcoding.
Definition: exitcodes.h:31
bool QueryCutList(frm_dir_map_t &, bool loadAutosave=false) const
#define GENERIC_EXIT_KILLED
Process killed or stopped.
Definition: exitcodes.h:23
static void Init(QList< int > &signallist, QObject *parent=nullptr)
static void WaitToDelete(ProgramInfo *pginfo)
static enum JobCmds GetJobCmd(int jobID)
Definition: jobqueue.cpp:1464
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:34
QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries, unsigned maxLinks)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: lang.c:20
#define REPLEX_HDTV
Definition: multiplex.h:40
RecordingFile * GetRecordingFile() const
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:62
static bool isVideo(const QString &mimeType)
Definition: newssite.cpp:293
static QString recorderOptions
unsigned sleep(unsigned int x)
Definition: compat.h:159
QString GetStorageGroup(void) const
Definition: programinfo.h:414
int GetBackendServerPort(void)
Returns the locally defined backend control port.
QMap< long long, long long > frm_pos_map_t
Frame # -> File offset map.
Definition: programtypes.h:46
static bool ChangeJobComment(int jobID, const QString &comment="")
Definition: jobqueue.cpp:1016
QVariant value(int i) const
Definition: mythdbcon.h:198
MarkTypes
Definition: programtypes.h:48
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Holds information on recordings and videos.
Definition: programinfo.h:66
static int transUnlink(const QString &filename, ProgramInfo *pginfo)
virtual void SaveFilesize(uint64_t fsize)
Sets recording file size in database, and sets "filesize" field.
static bool GetJobInfoFromID(int jobID, int &jobType, uint &chanid, QDateTime &recstartts)
Definition: jobqueue.cpp:671
static int QueueTranscodeJob(ProgramInfo *pginfo, const QString &profile, const QString &hostname, bool usecutlist)
#define GENERIC_EXIT_NO_RECORDING_DATA
No program/recording data.
Definition: exitcodes.h:29
static void CompleteJob(int jobID, ProgramInfo *pginfo, bool useCutlist, frm_dir_map_t *deleteMap, int &exitCode, int resultCode, bool forceDelete)
static QString GenMythURL(const QString &host=QString(), int port=0, QString path=QString(), const QString &storageGroup=QString())
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
int main(int argc, char *argv[])
bool SaveBasename(const QString &basename)
Sets a recording's basename in the database.
#define REENCODE_OK
Definition: transcodedefs.h:6
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
static bool testDBConnection()
Checks DB connection + login (login info via Mythcontext)
Definition: mythdbcon.cpp:851
uint GetRecordingID(void) const
Definition: programinfo.h:438
void SaveBookmark(uint64_t frame)
TODO Move to RecordingInfo.
string hostname
Definition: caa.py:17
static void Done(void)
unsigned int uint
Definition: compat.h:140
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
static void SetHandler(int signum, SigHandlerFunc handler)
MythCommFlagCommandLineParser cmdline
static int BuildKeyframeIndex(MPEG2fixup *m2f, QString &infile, frm_pos_map_t &posMap, frm_pos_map_t &durMap, int jobID)
Holds information on a recording file and it's video and audio streams.
Definition: recordingfile.h:29
#define ENO
This can be appended to the LOG args with "+".
Definition: mythlogging.h:99
int FILE
Definition: mythburn.py:110
#define REPLEX_DVD
Definition: multiplex.h:39
void ApplyTranscoderProfileChange(const QString &profile) const
Sets the transcoder profile for a recording.
#define MYTH_APPNAME_MYTHTRANSCODE
#define REENCODE_CUTLIST_CHANGE
Definition: transcodedefs.h:5
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:807
static bool ChangeJobArgs(int jobID, const QString &args="")
Definition: jobqueue.cpp:1041
static void UpdatePositionMap(frm_pos_map_t &posMap, frm_pos_map_t &durMap, const QString &mapfile, ProgramInfo *pginfo)
QStringList toStringList(const QString &key, const QString &sep="") const
Returns stored QVariant as a QStringList, falling to default if not provided.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
#define REPLEX_MPEG2
Definition: multiplex.h:38
static void UpdateJobQueue(float percent_done)
bool GetBoolSetting(const QString &key, bool defaultval=false)
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
AVContainer m_containerFormat
Definition: recordingfile.h:47
uint GetChanID(void) const
This is the unique key used in the database to locate tuning information.
Definition: programinfo.h:364
QString QueryBasename(void) const
Gets the basename, from the DB if necessary.
static uint64_t ReloadBookmark(ProgramInfo *pginfo)
#define SIGHUP
Definition: compat.h:213
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:16
static QString GetJobArgs(int jobID)
Definition: jobqueue.cpp:1485
QDateTime GetRecordingStartTime(void) const
Approximate time the recording started.
Definition: programinfo.h:396
static QString cleanup(const QString &str)
static bool ChangeJobStatus(int jobID, int newStatus, const QString &comment="")
Definition: jobqueue.cpp:989
bool Init(const bool gui=true, const bool promptForBackend=false, const bool disableAutoDiscovery=false, const bool ignoreDB=false)
QString GetHostname(void) const
Definition: programinfo.h:413
Default UTC.
Definition: mythdate.h:14
QMap< uint64_t, MarkTypes > frm_dir_map_t
Frame # -> Mark map.
Definition: programtypes.h:81
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
#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.
static int CheckJobQueue()
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
void SavePositionMap(frm_pos_map_t &, MarkTypes type, int64_t min_frame=-1, int64_t max_frame=-1) const
static enum JobStatus GetJobStatus(int jobID)
Definition: jobqueue.cpp:1527
uint toUInt(const QString &key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.
#define REENCODE_STOPPED
Definition: transcodedefs.h:8
static bool QueueJob(int jobType, uint chanid, const QDateTime &recstartts, const QString &args="", const QString &comment="", QString host="", int flags=0, int status=JOB_QUEUED, QDateTime schedruntime=QDateTime())
Definition: jobqueue.cpp:520
void ClearPositionMap(MarkTypes type) const
int toInt(const QString &key) const
Returns stored QVariant as an integer, falling to default if not provided.
int ConfigureLogging(const QString &mask="general", unsigned int progress=0)
Read in logging options and initialize the logging interface.
#define REPLEX_TS_SD
Definition: multiplex.h:41