MythTV  master
previewgenerator.cpp
Go to the documentation of this file.
1 // C headers
2 #include <cmath>
3 #include <utility>
4 
5 // POSIX headers
6 #include <sys/types.h> // for utime
7 #include <sys/time.h>
8 #include <fcntl.h>
9 #include <utime.h> // for utime
10 
11 // Qt headers
12 #include <QApplication>
13 #include <QCoreApplication>
14 #include <QDir>
15 #include <QFileInfo>
16 #include <QImage>
17 #include <QMetaType>
18 #include <QTemporaryFile>
19 #include <QUrl>
20 
21 // MythTV headers
22 #include "mythconfig.h"
23 
24 #include "ringbuffer.h"
25 #include "mythplayer.h"
26 #include "previewgenerator.h"
27 #include "tv_rec.h"
28 #include "mythsocket.h"
29 #include "remotefile.h"
30 #include "storagegroup.h"
31 #include "mythdate.h"
32 #include "playercontext.h"
33 #include "mythdirs.h"
34 #include "remoteutil.h"
35 #include "mythsystemlegacy.h"
36 #include "exitcodes.h"
37 #include "mythlogging.h"
38 #include "mythmiscutil.h"
39 
40 #define LOC QString("Preview: ")
41 
74  QString token,
76  : MThread("PreviewGenerator"),
77  m_programInfo(*pginfo), m_mode(mode),
78  m_pathname(pginfo->GetPathname()),
79  m_token(std::move(token))
80 {
81  // Qt requires that a receiver have the same thread affinity as the QThread
82  // sending the event, which is used to dispatch MythEvents sent by
83  // gCoreContext->dispatchNow(me)
84  moveToThread(QApplication::instance()->thread());
85 }
86 
88 {
89  TeardownAll();
90  wait();
91 }
92 
93 void PreviewGenerator::SetOutputFilename(const QString &fileName)
94 {
95  if (fileName.isEmpty())
96  return;
97  QFileInfo fileinfo = QFileInfo(fileName);
98  m_outFileName = fileName;
99  m_outFormat = fileinfo.suffix().toUpper();
100 }
101 
103 {
104  QMutexLocker locker(&m_previewLock);
105  m_previewWaitCondition.wakeAll();
106  m_listener = nullptr;
107 }
108 
110 {
111  TeardownAll();
112  QObject::deleteLater();
113 }
114 
116 {
117  QMutexLocker locker(&m_previewLock);
118  m_listener = obj;
119 }
120 
125 {
126  QString msg;
127  QTime tm = QTime::currentTime();
128  bool ok = false;
129  bool is_local = IsLocal();
130 
131  if (!is_local && !!(m_mode & kRemote))
132  {
133  LOG(VB_GENERAL, LOG_ERR, LOC +
134  QString("RunReal() file not local: '%1'")
135  .arg(m_pathname));
136  }
137  else if (!(m_mode & kLocal) && !(m_mode & kRemote))
138  {
139  LOG(VB_GENERAL, LOG_ERR, LOC +
140  QString("RunReal() Preview of '%1' failed "
141  "because mode was invalid 0x%2")
142  .arg(m_pathname).arg((int)m_mode,0,16));
143  }
144  else if (!!(m_mode & kLocal) && LocalPreviewRun())
145  {
146  ok = true;
147  msg = QString("Generated on %1 in %2 seconds, starting at %3")
148  .arg(gCoreContext->GetHostName())
149  .arg(tm.elapsed()*0.001)
150  .arg(tm.toString(Qt::ISODate));
151  }
152  else if (!!(m_mode & kRemote))
153  {
154  if (is_local && (m_mode & kLocal))
155  {
156  LOG(VB_GENERAL, LOG_WARNING, LOC + "Failed to save preview."
157  "\n\t\t\tYou may need to check user and group ownership on"
158  "\n\t\t\tyour frontend and backend for quicker previews.\n"
159  "\n\t\t\tAttempting to regenerate preview on backend.\n");
160  }
161  ok = RemotePreviewRun();
162  if (ok)
163  {
164  msg = QString("Generated remotely in %1 seconds, starting at %2")
165  .arg(tm.elapsed()*0.001)
166  .arg(tm.toString(Qt::ISODate));
167  }
168  else
169  {
170  msg = "Remote preview failed";
171  }
172  }
173  else
174  {
175  msg = "Could not access recording";
176  }
177 
178  QMutexLocker locker(&m_previewLock);
179  if (m_listener)
180  {
181  // keep in sync with default filename in
182  // PreviewGeneratorQueue::GeneratePreviewImage
183  QString output_fn = m_outFileName.isEmpty() ?
185 
186  QDateTime dt;
187  if (ok)
188  {
189  QFileInfo fi(output_fn);
190  if (fi.exists())
191  dt = fi.lastModified();
192  }
193 
194  QString message = (ok) ? "PREVIEW_SUCCESS" : "PREVIEW_FAILED";
195  QStringList list;
196  list.push_back(QString::number(m_programInfo.GetRecordingID()));
197  list.push_back(output_fn);
198  list.push_back(msg);
199  list.push_back(dt.isValid()?dt.toUTC().toString(Qt::ISODate):"");
200  list.push_back(m_token);
201  QCoreApplication::postEvent(m_listener, new MythEvent(message, list));
202  }
203 
204  return ok;
205 }
206 
208 {
209  QString msg;
210  QTime tm = QTime::currentTime();
211  bool ok = false;
212  QString command = GetAppBinDir() + "mythpreviewgen";
213  bool local_ok = ((IsLocal() || ((m_mode & kForceLocal) != 0)) &&
214  ((m_mode & kLocal) != 0) &&
215  QFileInfo(command).isExecutable());
216  if (!local_ok)
217  {
218  if (!!(m_mode & kRemote))
219  {
220  ok = RemotePreviewRun();
221  if (ok)
222  {
223  msg =
224  QString("Generated remotely in %1 seconds, starting at %2")
225  .arg(tm.elapsed()*0.001)
226  .arg(tm.toString(Qt::ISODate));
227  }
228  }
229  else
230  {
231  LOG(VB_GENERAL, LOG_ERR, LOC +
232  QString("Run() cannot generate preview locally for: '%1'")
233  .arg(m_pathname));
234  msg = "Failed, local preview requested for remote file.";
235  }
236  }
237  else
238  {
239  // This is where we fork and run mythpreviewgen to actually make preview
240  QStringList cmdargs;
241 
242  cmdargs << "--size"
243  << QString("%1x%2").arg(m_outSize.width()).arg(m_outSize.height());
244  if (m_captureTime >= 0)
245  {
246  if (m_timeInSeconds)
247  cmdargs << "--seconds";
248  else
249  cmdargs << "--frame";
250  cmdargs << QString::number(m_captureTime);
251  }
252  cmdargs << "--chanid"
253  << QString::number(m_programInfo.GetChanID())
254  << "--starttime"
256 
257  if (!m_outFileName.isEmpty())
258  cmdargs << "--outfile" << m_outFileName;
259 
260  // Timeout in 30s
261  auto *ms = new MythSystemLegacy(command, cmdargs,
267  ms->SetNice(10);
268  ms->SetIOPrio(7);
269 
270  ms->Run(30);
271  uint ret = ms->Wait();
272  delete ms;
273 
274  if (ret != GENERIC_EXIT_OK)
275  {
276  LOG(VB_GENERAL, LOG_ERR, LOC +
277  QString("Encountered problems running '%1 %2' - (%3)")
278  .arg(command).arg(cmdargs.join(" ")).arg(ret));
279  }
280  else
281  {
282  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Preview process returned 0.");
283  QString outname = (!m_outFileName.isEmpty()) ?
284  m_outFileName : (m_pathname + ".png");
285 
286  QString lpath = QFileInfo(outname).fileName();
287  if (lpath == outname)
288  {
289  StorageGroup sgroup;
290  QString tmpFile = sgroup.FindFile(lpath);
291  outname = (tmpFile.isEmpty()) ? outname : tmpFile;
292  }
293 
294  QFileInfo fi(outname);
295  ok = (fi.exists() && fi.isReadable() && (fi.size() != 0));
296  if (ok)
297  {
298  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Preview process ran ok.");
299  msg = QString("Generated on %1 in %2 seconds, starting at %3")
300  .arg(gCoreContext->GetHostName())
301  .arg(tm.elapsed()*0.001)
302  .arg(tm.toString(Qt::ISODate));
303  }
304  else
305  {
306  LOG(VB_GENERAL, LOG_ERR, LOC + "Preview process not ok." +
307  QString("\n\t\t\tfileinfo(%1)").arg(outname) +
308  QString(" exists: %1").arg(fi.exists()) +
309  QString(" readable: %1").arg(fi.isReadable()) +
310  QString(" size: %1").arg(fi.size()));
311  LOG(VB_GENERAL, LOG_ERR, LOC +
312  QString("Despite command '%1' returning success")
313  .arg(command));
314  msg = QString("Failed to read preview image despite "
315  "preview process returning success.");
316  }
317  }
318  }
319 
320  QMutexLocker locker(&m_previewLock);
321 
322  // Backdate file to start of preview time in case a bookmark was made
323  // while we were generating the preview.
324  QString output_fn = m_outFileName.isEmpty() ?
326 
327  QDateTime dt;
328  if (ok)
329  {
330  QFileInfo fi(output_fn);
331  if (fi.exists())
332  dt = fi.lastModified();
333  }
334 
335  QString message = (ok) ? "PREVIEW_SUCCESS" : "PREVIEW_FAILED";
336  if (m_listener)
337  {
338  QStringList list;
339  list.push_back(QString::number(m_programInfo.GetRecordingID()));
340  list.push_back(output_fn);
341  list.push_back(msg);
342  list.push_back(dt.isValid()?dt.toUTC().toString(Qt::ISODate):"");
343  list.push_back(m_token);
344  QCoreApplication::postEvent(m_listener, new MythEvent(message, list));
345  }
346 
347  return ok;
348 }
349 
351 {
352  RunProlog();
353  Run();
354  RunEpilog();
355 }
356 
358 {
359  QStringList strlist( "QUERY_GENPIXMAP2" );
360  if (m_token.isEmpty())
361  {
362  m_token = QString("%1:%2")
363  .arg(m_programInfo.MakeUniqueKey()).arg(random());
364  }
365  strlist.push_back(m_token);
366  m_programInfo.ToStringList(strlist);
367  strlist.push_back(m_timeInSeconds ? "s" : "f");
368  strlist.push_back(QString::number(m_captureTime));
369  if (m_outFileName.isEmpty())
370  {
371  strlist.push_back("<EMPTY>");
372  }
373  else
374  {
375  QFileInfo fi(m_outFileName);
376  strlist.push_back(fi.fileName());
377  }
378  strlist.push_back(QString::number(m_outSize.width()));
379  strlist.push_back(QString::number(m_outSize.height()));
380 
381  gCoreContext->addListener(this);
382  m_pixmapOk = false;
383 
384  bool ok = gCoreContext->SendReceiveStringList(strlist);
385  if (!ok || strlist.empty() || (strlist[0] != "OK"))
386  {
387  if (!ok)
388  {
389  LOG(VB_GENERAL, LOG_ERR, LOC +
390  "Remote Preview failed due to communications error.");
391  }
392  else if (strlist.size() > 1)
393  {
394  LOG(VB_GENERAL, LOG_ERR, LOC +
395  "Remote Preview failed, reason given: " + strlist[1]);
396  }
397 
399 
400  return false;
401  }
402 
403  QMutexLocker locker(&m_previewLock);
404 
405  // wait up to 35 seconds for the preview to complete
406  // The backend waits 30s for creation
407  if (!m_gotReply)
408  m_previewWaitCondition.wait(&m_previewLock, 35 * 1000);
409 
410  if (!m_gotReply)
411  LOG(VB_GENERAL, LOG_NOTICE, LOC + "RemotePreviewRun() -- no reply..");
412 
414 
415  return m_pixmapOk;
416 }
417 
418 bool PreviewGenerator::event(QEvent *e)
419 {
420  if (e->type() != MythEvent::MythEventMessage)
421  return QObject::event(e);
422 
423  auto *me = dynamic_cast<MythEvent*>(e);
424  if (me == nullptr)
425  return false;
426  if (me->Message() != "GENERATED_PIXMAP" || me->ExtraDataCount() < 3)
427  return QObject::event(e);
428 
429  bool ok = me->ExtraData(0) == "OK";
430  bool ours = false;
431  uint i = ok ? 4 : 3;
432  for (; i < (uint) me->ExtraDataCount() && !ours; i++)
433  ours |= me->ExtraData(i) == m_token;
434  if (!ours)
435  return false;
436 
437  const QString& pginfokey = me->ExtraData(1);
438 
439  QMutexLocker locker(&m_previewLock);
440  m_gotReply = true;
441  m_pixmapOk = ok;
442  if (!ok)
443  {
444  LOG(VB_GENERAL, LOG_ERR, LOC + pginfokey + ": " + me->ExtraData(2));
445  m_previewWaitCondition.wakeAll();
446  return true;
447  }
448 
449  if (me->ExtraDataCount() < 5)
450  {
451  m_pixmapOk = false;
452  m_previewWaitCondition.wakeAll();
453  return true; // could only happen with very broken client...
454  }
455 
456  QDateTime datetime = MythDate::fromString(me->ExtraData(3));
457  if (!datetime.isValid())
458  {
459  m_pixmapOk = false;
460  LOG(VB_GENERAL, LOG_ERR, LOC + pginfokey + "Got invalid date");
461  m_previewWaitCondition.wakeAll();
462  return false;
463  }
464 
465  size_t length = me->ExtraData(4).toULongLong();
466  quint16 checksum16 = me->ExtraData(5).toUInt();
467  QByteArray data = QByteArray::fromBase64(me->ExtraData(6).toLatin1());
468  if ((size_t) data.size() < length)
469  { // (note data.size() may be up to 3
470  // bytes longer after decoding
471  LOG(VB_GENERAL, LOG_ERR, LOC +
472  QString("Preview size check failed %1 < %2")
473  .arg(data.size()).arg(length));
474  data.clear();
475  }
476  data.resize(length);
477 
478  if (checksum16 != qChecksum(data.constData(), data.size()))
479  {
480  LOG(VB_GENERAL, LOG_ERR, LOC + "Preview checksum failed");
481  data.clear();
482  }
483 
484  m_pixmapOk = (data.isEmpty()) ? false : SaveOutFile(data, datetime);
485 
486  m_previewWaitCondition.wakeAll();
487 
488  return true;
489 }
490 
491 bool PreviewGenerator::SaveOutFile(const QByteArray &data, const QDateTime &dt)
492 {
493  if (m_outFileName.isEmpty())
494  {
495  QString remotecachedirname =
496  QString("%1/cache/remotecache").arg(GetConfDir());
497  QDir remotecachedir(remotecachedirname);
498 
499  if (!remotecachedir.exists())
500  {
501  if (!remotecachedir.mkdir(remotecachedirname))
502  {
503  LOG(VB_GENERAL, LOG_ERR, LOC +
504  "Remote Preview failed because we could not create a "
505  "remote cache directory");
506  return false;
507  }
508  }
509 
510  QString filename = m_programInfo.GetBasename() + ".png";
511  SetOutputFilename(QString("%1/%2").arg(remotecachedirname).arg(filename));
512  }
513 
514  QFile file(m_outFileName);
515  bool ok = file.open(QIODevice::Unbuffered|QIODevice::WriteOnly);
516  if (!ok)
517  {
518  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to open: '%1'")
519  .arg(m_outFileName));
520  }
521 
522  off_t offset = 0;
523  size_t remaining = data.size();
524  uint failure_cnt = 0;
525  while ((remaining > 0) && (failure_cnt < 5))
526  {
527  ssize_t written = file.write(data.data() + offset, remaining);
528  if (written < 0)
529  {
530  failure_cnt++;
531  usleep(50000);
532  continue;
533  }
534 
535  failure_cnt = 0;
536  offset += written;
537  remaining -= written;
538  }
539 
540  if (ok && !remaining)
541  {
542  file.close();
543  struct utimbuf times {};
544 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
545  times.actime = times.modtime = dt.toTime_t();
546 #else
547  times.actime = times.modtime = dt.toSecsSinceEpoch();
548 #endif
549  utime(m_outFileName.toLocal8Bit().constData(), &times);
550  LOG(VB_FILE, LOG_INFO, LOC + QString("Saved: '%1'").arg(m_outFileName));
551  }
552  else
553  {
554  file.remove();
555  }
556 
557  return ok;
558 }
559 
561  const unsigned char *data,
562  uint width, uint height, float aspect,
563  int desired_width, int desired_height,
564  const QString &format)
565 {
566  if (!data || !width || !height)
567  return false;
568 
569  const QImage img((unsigned char*) data,
570  width, height, QImage::Format_RGB32);
571 
572  float ppw = max(desired_width, 0);
573  float pph = max(desired_height, 0);
574  bool desired_size_exactly_specified = true;
575  if ((ppw < 1.0F) && (pph < 1.0F))
576  {
577  ppw = img.width();
578  pph = img.height();
579  desired_size_exactly_specified = false;
580  }
581 
582  aspect = (aspect <= 0.0F) ? ((float) width) / height : aspect;
583  pph = (pph < 1.0F) ? (ppw / aspect) : pph;
584  ppw = (ppw < 1.0F) ? (pph * aspect) : ppw;
585 
586  if (!desired_size_exactly_specified)
587  {
588  if (aspect > ppw / pph)
589  pph = (ppw / aspect);
590  else
591  ppw = (pph * aspect);
592  }
593 
594  ppw = max(1.0F, ppw);
595  pph = max(1.0F, pph);;
596 
597  QImage small_img = img.scaled((int) ppw, (int) pph,
598  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
599 
600  QTemporaryFile f(QFileInfo(filename).absoluteFilePath()+".XXXXXX");
601  f.setAutoRemove(false);
602  if (f.open() && small_img.save(&f, format.toLocal8Bit().constData()))
603  {
604  // Let anybody update it
605  bool ret = makeFileAccessible(f.fileName().toLocal8Bit().constData());
606  if (!ret)
607  {
608  LOG(VB_GENERAL, LOG_ERR, "Unable to change permissions on "
609  "preview image. Backends and frontends "
610  "running under different users will be "
611  "unable to access it");
612  }
613  QFile of(filename);
614  of.remove();
615  if (f.rename(filename))
616  {
617  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Saved preview '%0' %1x%2")
618  .arg(filename).arg((int) ppw).arg((int) pph));
619  return true;
620  }
621  f.remove();
622  }
623 
624  return false;
625 }
626 
628 {
632 
633  float aspect = 0;
634  int width, height, sz;
635  long long captime = m_captureTime;
636 
637  QDateTime dt = MythDate::current();
638 
639  if (captime > 0)
640  LOG(VB_GENERAL, LOG_INFO, "Preview from time spec");
641 
642  if (captime < 0)
643  {
644  captime = m_programInfo.QueryBookmark();
645  if (captime > 0)
646  {
647  m_timeInSeconds = false;
648  LOG(VB_GENERAL, LOG_INFO,
649  QString("Preview from bookmark (frame %1)").arg(captime));
650  }
651  else
652  captime = -1;
653  }
654 
655  if (captime <= 0)
656  {
657  m_timeInSeconds = true;
658  int startEarly = 0;
659  int programDuration = 0;
660  int preroll = gCoreContext->GetNumSetting("RecordPreRoll", 0);
661  if (m_programInfo.GetScheduledStartTime().isValid() &&
662  m_programInfo.GetScheduledEndTime().isValid() &&
665  {
666  programDuration = m_programInfo.GetScheduledStartTime()
668  }
669  if (m_programInfo.GetRecordingStartTime().isValid() &&
670  m_programInfo.GetScheduledStartTime().isValid() &&
673  {
674  startEarly = m_programInfo.GetRecordingStartTime()
676  }
677  if (programDuration > 0)
678  {
679  captime = startEarly + (programDuration / 3);
680  }
681  if (captime < 0)
682  captime = 600;
683  captime += preroll;
684  LOG(VB_GENERAL, LOG_INFO,
685  QString("Preview at calculated offset (%1 seconds)").arg(captime));
686  }
687 
688  width = height = sz = 0;
689  auto *data = (unsigned char*) GetScreenGrab(m_programInfo, m_pathname,
690  captime, m_timeInSeconds,
691  sz, width, height, aspect);
692 
694 
695  QString format = (m_outFormat.isEmpty()) ? "PNG" : m_outFormat;
696 
697  int dw = (m_outSize.width() < 0) ? width : m_outSize.width();
698  int dh = (m_outSize.height() < 0) ? height : m_outSize.height();
699 
700  bool ok = SavePreview(outname, data, width, height, aspect, dw, dh,
701  format);
702 
703  if (ok)
704  {
705  // Backdate file to start of preview time in case a bookmark was made
706  // while we were generating the preview.
707  struct utimbuf times {};
708 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
709  times.actime = times.modtime = dt.toTime_t();
710 #else
711  times.actime = times.modtime = dt.toSecsSinceEpoch();
712 #endif
713  utime(outname.toLocal8Bit().constData(), &times);
714  }
715 
716  delete[] data;
717 
719 
720  return ok;
721 }
722 
724  const QString &pathname, const QString &outFileName)
725 {
726  QString outname = pathname + ".png";
727 
728  if (outFileName.isEmpty())
729  return outname;
730 
731  outname = outFileName;
732  QFileInfo fi(outname);
733  if (outname == fi.fileName())
734  {
735  QString dir;
736  if (pathname.contains(':'))
737  {
738  QUrl uinfo(pathname);
739  uinfo.setPath("");
740  dir = uinfo.toString();
741  }
742  else
743  {
744  dir = QFileInfo(pathname).path();
745  }
746  outname = dir + "/" + fi.fileName();
747  LOG(VB_FILE, LOG_INFO, LOC + QString("outfile '%1' -> '%2'")
748  .arg(outFileName).arg(outname));
749  }
750 
751  return outname;
752 }
753 
755 {
756  QString tmppathname = m_pathname;
757 
758  if (tmppathname.startsWith("dvd:"))
759  tmppathname = tmppathname.section(":", 1, 1);
760 
761  if (!QFileInfo(tmppathname).isReadable())
762  return false;
763 
764  tmppathname = m_outFileName.isEmpty() ? tmppathname : m_outFileName;
765  QString pathdir = QFileInfo(tmppathname).path();
766 
767  if (!QFileInfo(pathdir).isWritable())
768  {
769  LOG(VB_GENERAL, LOG_WARNING, LOC +
770  QString("Output path '%1' is not writeable") .arg(pathdir));
771  return false;
772  }
773 
774  return true;
775 }
776 
793  const ProgramInfo &pginfo, const QString &filename,
794  long long seektime, bool time_in_secs,
795  int &bufferlen,
796  int &video_width, int &video_height, float &video_aspect)
797 {
798  (void) pginfo;
799  (void) filename;
800  (void) seektime;
801  (void) time_in_secs;
802  (void) bufferlen;
803  (void) video_width;
804  (void) video_height;
805  char *retbuf = nullptr;
806  bufferlen = 0;
807 
809  {
810  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer could not connect to DB.");
811  return nullptr;
812  }
813 
814  // pre-test local files for existence and size. 500 ms speed-up...
815  if (filename.startsWith("/"))
816  {
817  QFileInfo info(filename);
818  bool invalid = (!info.exists() || !info.isReadable() ||
819  (info.isFile() && (info.size() < 8*1024)));
820  if (invalid)
821  {
822  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer file " +
823  QString("'%1'").arg(filename) + " is not valid.");
824  return nullptr;
825  }
826  }
827 
828  RingBuffer *rbuf = RingBuffer::Create(filename, false, false, 0);
829  if (!rbuf || !rbuf->IsOpen())
830  {
831  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer could not open file: " +
832  QString("'%1'").arg(filename));
833  delete rbuf;
834  return nullptr;
835  }
836 
837  auto *ctx = new PlayerContext(kPreviewGeneratorInUseID);
838  ctx->SetRingBuffer(rbuf);
839  ctx->SetPlayingInfo(&pginfo);
840  ctx->SetPlayer(new MythPlayer((PlayerFlags)(kAudioMuted | kVideoIsNull | kNoITV)));
841  ctx->m_player->SetPlayerInfo(nullptr, nullptr, ctx);
842 
843  if (time_in_secs)
844  retbuf = ctx->m_player->GetScreenGrab(seektime, bufferlen,
845  video_width, video_height, video_aspect);
846  else
847  retbuf = ctx->m_player->GetScreenGrabAtFrame(
848  seektime, true, bufferlen,
849  video_width, video_height, video_aspect);
850 
851  delete ctx;
852 
853  if (retbuf)
854  {
855  LOG(VB_GENERAL, LOG_INFO, LOC +
856  QString("Grabbed preview '%0' %1x%2@%3%4")
857  .arg(filename).arg(video_width).arg(video_height)
858  .arg(seektime).arg((time_in_secs) ? "s" : "f"));
859  }
860  else
861  {
862  LOG(VB_GENERAL, LOG_ERR, LOC +
863  QString("Failed to grab preview '%0' %1x%2@%3%4")
864  .arg(filename).arg(video_width).arg(video_height)
865  .arg(seektime).arg((time_in_secs) ? "s" : "f"));
866  }
867 
868  return retbuf;
869 }
870 
871 /* vim: set expandtab tabstop=4 shiftwidth=4: */
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
bool IsLocal(void) const
virtual ~PreviewGenerator()
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
void SetAllowLastPlayPos(bool allow)
If "ignore" is true QueryLastPlayPos() will return 0, otherwise QueryLastPlayPos() will return the la...
Definition: programinfo.h:558
PlayerFlags
Definition: mythplayer.h:86
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
bool RemotePreviewRun(void)
void AttachSignals(QObject *)
static Type MythEventMessage
Definition: mythevent.h:66
void removeListener(QObject *listener)
Remove a listener to the observable.
QWaitCondition m_previewWaitCondition
void SetOutputFilename(const QString &)
void ToStringList(QStringList &list) const
Serializes ProgramInfo into a QStringList which can be passed over a socket.
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
static QString remotecachedir
Definition: mythdirs.cpp:26
#define LOC
bool m_timeInSeconds
tells us whether to use time as seconds or frame number
static char * GetScreenGrab(const ProgramInfo &pginfo, const QString &filename, long long seektime, bool time_in_secs, int &bufferlen, int &video_width, int &video_height, float &video_aspect)
Returns a AV_PIX_FMT_RGBA32 buffer containg a frame from the video.
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
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
void addListener(QObject *listener)
Add a listener to the observable.
const char * kPreviewGeneratorInUseID
void MarkAsInUse(bool inuse, const QString &usedFor="")
Tracks a recording's in use status, to prevent deletion and to allow the storage scheduler to perform...
ProgramInfo m_programInfo
#define off_t
QString GetConfDir(void)
Definition: mythdirs.cpp:224
QDateTime GetScheduledStartTime(void) const
The scheduled start time of program.
Definition: programinfo.h:382
static QString CreateAccessibleFilename(const QString &pathname, const QString &outFileName)
QString MakeUniqueKey(void) const
Creates a unique string that can be used to identify an existing recording.
Definition: programinfo.h:331
Holds information on recordings and videos.
Definition: programinfo.h:66
bool SendReceiveStringList(QStringList &strlist, bool quickTimeout=false, bool block=true)
Send a message to the backend and wait for a response.
This class is used as a container for messages.
Definition: mythevent.h:16
add arguments for MythTV log propagation
Definition: mythsystem.h:50
bool makeFileAccessible(const QString &filename)
PreviewGenerator(const ProgramInfo *pginfo, QString token, Mode mode=kLocal)
Constructor.
QString GetBasename(void) const
Definition: programinfo.h:336
QString GetAppBinDir(void)
Definition: mythdirs.cpp:221
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
static bool testDBConnection()
Checks DB connection + login (login info via Mythcontext)
Definition: mythdbcon.cpp:850
uint GetRecordingID(void) const
Definition: programinfo.h:438
virtual bool IsOpen(void) const =0
Returns true if open for either reading or writing.
unsigned int uint
Definition: compat.h:140
QDateTime GetScheduledEndTime(void) const
The scheduled end time of the program.
Definition: programinfo.h:389
uint64_t QueryBookmark(void) const
Gets any bookmark position in database, unless the ignore bookmark flag is set.
int GetNumSetting(const QString &key, int defaultval=0)
automatically delete if backgrounded
Definition: mythsystem.h:43
process events while waiting
Definition: mythsystem.h:37
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
bool event(QEvent *e) override
static bool SavePreview(const QString &filename, const unsigned char *data, uint width, uint height, float aspect, int desired_width, int desired_height, const QString &format)
avoid disabling UI drawing
Definition: mythsystem.h:35
QString FindFile(const QString &filename)
uint GetChanID(void) const
This is the unique key used in the database to locate tuning information.
Definition: programinfo.h:364
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
QDateTime GetRecordingStartTime(void) const
Approximate time the recording started.
Definition: programinfo.h:396
long long m_captureTime
snapshot time in seconds or frame number, depending on timeInSeconds
bool LocalPreviewRun(void)
Default UTC.
Definition: mythdate.h:14
Implements a file/stream reader/writer.
static long int random(void)
Definition: compat.h:149
static void usleep(unsigned long time)
Definition: mthread.cpp:348
void SetIgnoreProgStart(bool ignore)
If "ignore" is true QueryProgStart() will return 0, otherwise QueryProgStart() will return the progst...
Definition: programinfo.h:550
bool RunReal(void)
This call creates a preview without starting a new thread.
Default UTC, "yyyyMMddhhmmss".
Definition: mythdate.h:15
QString GetHostName(void)
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:30
QString GetPathname(void) const
Definition: programinfo.h:335
avoid blocking LIRC & Joystick Menu
Definition: mythsystem.h:34
bool SaveOutFile(const QByteArray &data, const QDateTime &dt)