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  QElapsedTimer te; te.start();
129  bool ok = false;
130  bool is_local = IsLocal();
131 
132  if (!is_local && !!(m_mode & kRemote))
133  {
134  LOG(VB_GENERAL, LOG_ERR, LOC +
135  QString("RunReal() file not local: '%1'")
136  .arg(m_pathname));
137  }
138  else if (!(m_mode & kLocal) && !(m_mode & kRemote))
139  {
140  LOG(VB_GENERAL, LOG_ERR, LOC +
141  QString("RunReal() Preview of '%1' failed "
142  "because mode was invalid 0x%2")
143  .arg(m_pathname).arg((int)m_mode,0,16));
144  }
145  else if (!!(m_mode & kLocal) && LocalPreviewRun())
146  {
147  ok = true;
148  msg = QString("Generated on %1 in %2 seconds, starting at %3")
149  .arg(gCoreContext->GetHostName())
150  .arg(te.elapsed()*0.001)
151  .arg(tm.toString(Qt::ISODate));
152  }
153  else if (!!(m_mode & kRemote))
154  {
155  if (is_local && (m_mode & kLocal))
156  {
157  LOG(VB_GENERAL, LOG_WARNING, LOC + "Failed to save preview."
158  "\n\t\t\tYou may need to check user and group ownership on"
159  "\n\t\t\tyour frontend and backend for quicker previews.\n"
160  "\n\t\t\tAttempting to regenerate preview on backend.\n");
161  }
162  ok = RemotePreviewRun();
163  if (ok)
164  {
165  msg = QString("Generated remotely in %1 seconds, starting at %2")
166  .arg(te.elapsed()*0.001)
167  .arg(tm.toString(Qt::ISODate));
168  }
169  else
170  {
171  msg = "Remote preview failed";
172  }
173  }
174  else
175  {
176  msg = "Could not access recording";
177  }
178 
179  QMutexLocker locker(&m_previewLock);
180  if (m_listener)
181  {
182  // keep in sync with default filename in
183  // PreviewGeneratorQueue::GeneratePreviewImage
184  QString output_fn = m_outFileName.isEmpty() ?
186 
187  QDateTime dt;
188  if (ok)
189  {
190  QFileInfo fi(output_fn);
191  if (fi.exists())
192  dt = fi.lastModified();
193  }
194 
195  QString message = (ok) ? "PREVIEW_SUCCESS" : "PREVIEW_FAILED";
196  QStringList list;
197  list.push_back(QString::number(m_programInfo.GetRecordingID()));
198  list.push_back(output_fn);
199  list.push_back(msg);
200  list.push_back(dt.isValid()?dt.toUTC().toString(Qt::ISODate):"");
201  list.push_back(m_token);
202  QCoreApplication::postEvent(m_listener, new MythEvent(message, list));
203  }
204 
205  return ok;
206 }
207 
209 {
210  QString msg;
211  QTime tm = QTime::currentTime();
212  QElapsedTimer te; te.start();
213  bool ok = false;
214  QString command = GetAppBinDir() + "mythpreviewgen";
215  bool local_ok = ((IsLocal() || ((m_mode & kForceLocal) != 0)) &&
216  ((m_mode & kLocal) != 0) &&
217  QFileInfo(command).isExecutable());
218  if (!local_ok)
219  {
220  if (!!(m_mode & kRemote))
221  {
222  ok = RemotePreviewRun();
223  if (ok)
224  {
225  msg =
226  QString("Generated remotely in %1 seconds, starting at %2")
227  .arg(te.elapsed()*0.001)
228  .arg(tm.toString(Qt::ISODate));
229  }
230  }
231  else
232  {
233  LOG(VB_GENERAL, LOG_ERR, LOC +
234  QString("Run() cannot generate preview locally for: '%1'")
235  .arg(m_pathname));
236  msg = "Failed, local preview requested for remote file.";
237  }
238  }
239  else
240  {
241  // This is where we fork and run mythpreviewgen to actually make preview
242  QStringList cmdargs;
243 
244  cmdargs << "--size"
245  << QString("%1x%2").arg(m_outSize.width()).arg(m_outSize.height());
246  if (m_captureTime >= 0)
247  {
248  if (m_timeInSeconds)
249  cmdargs << "--seconds";
250  else
251  cmdargs << "--frame";
252  cmdargs << QString::number(m_captureTime);
253  }
254  cmdargs << "--chanid"
255  << QString::number(m_programInfo.GetChanID())
256  << "--starttime"
258 
259  if (!m_outFileName.isEmpty())
260  cmdargs << "--outfile" << m_outFileName;
261 
262  // Timeout in 30s
263  auto *ms = new MythSystemLegacy(command, cmdargs,
269  ms->SetNice(10);
270  ms->SetIOPrio(7);
271 
272  ms->Run(30);
273  uint ret = ms->Wait();
274  delete ms;
275 
276  if (ret != GENERIC_EXIT_OK)
277  {
278  LOG(VB_GENERAL, LOG_ERR, LOC +
279  QString("Encountered problems running '%1 %2' - (%3)")
280  .arg(command).arg(cmdargs.join(" ")).arg(ret));
281  }
282  else
283  {
284  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Preview process returned 0.");
285  QString outname = (!m_outFileName.isEmpty()) ?
286  m_outFileName : (m_pathname + ".png");
287 
288  QString lpath = QFileInfo(outname).fileName();
289  if (lpath == outname)
290  {
291  StorageGroup sgroup;
292  QString tmpFile = sgroup.FindFile(lpath);
293  outname = (tmpFile.isEmpty()) ? outname : tmpFile;
294  }
295 
296  QFileInfo fi(outname);
297  ok = (fi.exists() && fi.isReadable() && (fi.size() != 0));
298  if (ok)
299  {
300  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Preview process ran ok.");
301  msg = QString("Generated on %1 in %2 seconds, starting at %3")
302  .arg(gCoreContext->GetHostName())
303  .arg(te.elapsed()*0.001)
304  .arg(tm.toString(Qt::ISODate));
305  }
306  else
307  {
308  LOG(VB_GENERAL, LOG_ERR, LOC + "Preview process not ok." +
309  QString("\n\t\t\tfileinfo(%1)").arg(outname) +
310  QString(" exists: %1").arg(fi.exists()) +
311  QString(" readable: %1").arg(fi.isReadable()) +
312  QString(" size: %1").arg(fi.size()));
313  LOG(VB_GENERAL, LOG_ERR, LOC +
314  QString("Despite command '%1' returning success")
315  .arg(command));
316  msg = QString("Failed to read preview image despite "
317  "preview process returning success.");
318  }
319  }
320  }
321 
322  QMutexLocker locker(&m_previewLock);
323 
324  // Backdate file to start of preview time in case a bookmark was made
325  // while we were generating the preview.
326  QString output_fn = m_outFileName.isEmpty() ?
328 
329  QDateTime dt;
330  if (ok)
331  {
332  QFileInfo fi(output_fn);
333  if (fi.exists())
334  dt = fi.lastModified();
335  }
336 
337  QString message = (ok) ? "PREVIEW_SUCCESS" : "PREVIEW_FAILED";
338  if (m_listener)
339  {
340  QStringList list;
341  list.push_back(QString::number(m_programInfo.GetRecordingID()));
342  list.push_back(output_fn);
343  list.push_back(msg);
344  list.push_back(dt.isValid()?dt.toUTC().toString(Qt::ISODate):"");
345  list.push_back(m_token);
346  QCoreApplication::postEvent(m_listener, new MythEvent(message, list));
347  }
348 
349  return ok;
350 }
351 
353 {
354  RunProlog();
355  Run();
356  RunEpilog();
357 }
358 
360 {
361  QStringList strlist( "QUERY_GENPIXMAP2" );
362  if (m_token.isEmpty())
363  {
364  m_token = QString("%1:%2")
365  .arg(m_programInfo.MakeUniqueKey()).arg(random());
366  }
367  strlist.push_back(m_token);
368  m_programInfo.ToStringList(strlist);
369  strlist.push_back(m_timeInSeconds ? "s" : "f");
370  strlist.push_back(QString::number(m_captureTime));
371  if (m_outFileName.isEmpty())
372  {
373  strlist.push_back("<EMPTY>");
374  }
375  else
376  {
377  QFileInfo fi(m_outFileName);
378  strlist.push_back(fi.fileName());
379  }
380  strlist.push_back(QString::number(m_outSize.width()));
381  strlist.push_back(QString::number(m_outSize.height()));
382 
383  gCoreContext->addListener(this);
384  m_pixmapOk = false;
385 
386  bool ok = gCoreContext->SendReceiveStringList(strlist);
387  if (!ok || strlist.empty() || (strlist[0] != "OK"))
388  {
389  if (!ok)
390  {
391  LOG(VB_GENERAL, LOG_ERR, LOC +
392  "Remote Preview failed due to communications error.");
393  }
394  else if (strlist.size() > 1)
395  {
396  LOG(VB_GENERAL, LOG_ERR, LOC +
397  "Remote Preview failed, reason given: " + strlist[1]);
398  }
399 
401 
402  return false;
403  }
404 
405  QMutexLocker locker(&m_previewLock);
406 
407  // wait up to 35 seconds for the preview to complete
408  // The backend waits 30s for creation
409  if (!m_gotReply)
410  m_previewWaitCondition.wait(&m_previewLock, 35 * 1000);
411 
412  if (!m_gotReply)
413  LOG(VB_GENERAL, LOG_NOTICE, LOC + "RemotePreviewRun() -- no reply..");
414 
416 
417  return m_pixmapOk;
418 }
419 
420 bool PreviewGenerator::event(QEvent *e)
421 {
422  if (e->type() != MythEvent::MythEventMessage)
423  return QObject::event(e);
424 
425  auto *me = dynamic_cast<MythEvent*>(e);
426  if (me == nullptr)
427  return false;
428  if (me->Message() != "GENERATED_PIXMAP" || me->ExtraDataCount() < 3)
429  return QObject::event(e);
430 
431  bool ok = me->ExtraData(0) == "OK";
432  bool ours = false;
433  uint i = ok ? 4 : 3;
434  for (; i < (uint) me->ExtraDataCount() && !ours; i++)
435  ours |= me->ExtraData(i) == m_token;
436  if (!ours)
437  return false;
438 
439  const QString& pginfokey = me->ExtraData(1);
440 
441  QMutexLocker locker(&m_previewLock);
442  m_gotReply = true;
443  m_pixmapOk = ok;
444  if (!ok)
445  {
446  LOG(VB_GENERAL, LOG_ERR, LOC + pginfokey + ": " + me->ExtraData(2));
447  m_previewWaitCondition.wakeAll();
448  return true;
449  }
450 
451  if (me->ExtraDataCount() < 5)
452  {
453  m_pixmapOk = false;
454  m_previewWaitCondition.wakeAll();
455  return true; // could only happen with very broken client...
456  }
457 
458  QDateTime datetime = MythDate::fromString(me->ExtraData(3));
459  if (!datetime.isValid())
460  {
461  m_pixmapOk = false;
462  LOG(VB_GENERAL, LOG_ERR, LOC + pginfokey + "Got invalid date");
463  m_previewWaitCondition.wakeAll();
464  return false;
465  }
466 
467  size_t length = me->ExtraData(4).toULongLong();
468  quint16 checksum16 = me->ExtraData(5).toUInt();
469  QByteArray data = QByteArray::fromBase64(me->ExtraData(6).toLatin1());
470  if ((size_t) data.size() < length)
471  { // (note data.size() may be up to 3
472  // bytes longer after decoding
473  LOG(VB_GENERAL, LOG_ERR, LOC +
474  QString("Preview size check failed %1 < %2")
475  .arg(data.size()).arg(length));
476  data.clear();
477  }
478  data.resize(length);
479 
480  if (checksum16 != qChecksum(data.constData(), data.size()))
481  {
482  LOG(VB_GENERAL, LOG_ERR, LOC + "Preview checksum failed");
483  data.clear();
484  }
485 
486  m_pixmapOk = (data.isEmpty()) ? false : SaveOutFile(data, datetime);
487 
488  m_previewWaitCondition.wakeAll();
489 
490  return true;
491 }
492 
493 bool PreviewGenerator::SaveOutFile(const QByteArray &data, const QDateTime &dt)
494 {
495  if (m_outFileName.isEmpty())
496  {
497  QString remotecachedirname =
498  QString("%1/cache/remotecache").arg(GetConfDir());
499  QDir remotecachedir(remotecachedirname);
500 
501  if (!remotecachedir.exists())
502  {
503  if (!remotecachedir.mkdir(remotecachedirname))
504  {
505  LOG(VB_GENERAL, LOG_ERR, LOC +
506  "Remote Preview failed because we could not create a "
507  "remote cache directory");
508  return false;
509  }
510  }
511 
512  QString filename = m_programInfo.GetBasename() + ".png";
513  SetOutputFilename(QString("%1/%2").arg(remotecachedirname).arg(filename));
514  }
515 
516  QFile file(m_outFileName);
517  bool ok = file.open(QIODevice::Unbuffered|QIODevice::WriteOnly);
518  if (!ok)
519  {
520  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to open: '%1'")
521  .arg(m_outFileName));
522  }
523 
524  off_t offset = 0;
525  size_t remaining = data.size();
526  uint failure_cnt = 0;
527  while ((remaining > 0) && (failure_cnt < 5))
528  {
529  ssize_t written = file.write(data.data() + offset, remaining);
530  if (written < 0)
531  {
532  failure_cnt++;
533  usleep(50000);
534  continue;
535  }
536 
537  failure_cnt = 0;
538  offset += written;
539  remaining -= written;
540  }
541 
542  if (ok && !remaining)
543  {
544  file.close();
545  struct utimbuf times {};
546 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
547  times.actime = times.modtime = dt.toTime_t();
548 #else
549  times.actime = times.modtime = dt.toSecsSinceEpoch();
550 #endif
551  utime(m_outFileName.toLocal8Bit().constData(), &times);
552  LOG(VB_FILE, LOG_INFO, LOC + QString("Saved: '%1'").arg(m_outFileName));
553  }
554  else
555  {
556  file.remove();
557  }
558 
559  return ok;
560 }
561 
563  const unsigned char *data,
564  uint width, uint height, float aspect,
565  int desired_width, int desired_height,
566  const QString &format)
567 {
568  if (!data || !width || !height)
569  return false;
570 
571  const QImage img((unsigned char*) data,
572  width, height, QImage::Format_RGB32);
573 
574  float ppw = max(desired_width, 0);
575  float pph = max(desired_height, 0);
576  bool desired_size_exactly_specified = true;
577  if ((ppw < 1.0F) && (pph < 1.0F))
578  {
579  ppw = img.width();
580  pph = img.height();
581  desired_size_exactly_specified = false;
582  }
583 
584  aspect = (aspect <= 0.0F) ? ((float) width) / height : aspect;
585  pph = (pph < 1.0F) ? (ppw / aspect) : pph;
586  ppw = (ppw < 1.0F) ? (pph * aspect) : ppw;
587 
588  if (!desired_size_exactly_specified)
589  {
590  if (aspect > ppw / pph)
591  pph = (ppw / aspect);
592  else
593  ppw = (pph * aspect);
594  }
595 
596  ppw = max(1.0F, ppw);
597  pph = max(1.0F, pph);;
598 
599  QImage small_img = img.scaled((int) ppw, (int) pph,
600  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
601 
602  QTemporaryFile f(QFileInfo(filename).absoluteFilePath()+".XXXXXX");
603  f.setAutoRemove(false);
604  if (f.open() && small_img.save(&f, format.toLocal8Bit().constData()))
605  {
606  // Let anybody update it
607  bool ret = makeFileAccessible(f.fileName().toLocal8Bit().constData());
608  if (!ret)
609  {
610  LOG(VB_GENERAL, LOG_ERR, "Unable to change permissions on "
611  "preview image. Backends and frontends "
612  "running under different users will be "
613  "unable to access it");
614  }
615  QFile of(filename);
616  of.remove();
617  if (f.rename(filename))
618  {
619  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Saved preview '%0' %1x%2")
620  .arg(filename).arg((int) ppw).arg((int) pph));
621  return true;
622  }
623  f.remove();
624  }
625 
626  return false;
627 }
628 
630 {
634 
635  float aspect = 0;
636  long long captime = m_captureTime;
637 
638  QDateTime dt = MythDate::current();
639 
640  if (captime > 0)
641  LOG(VB_GENERAL, LOG_INFO, "Preview from time spec");
642 
643  if (captime < 0)
644  {
645  captime = m_programInfo.QueryBookmark();
646  if (captime > 0)
647  {
648  m_timeInSeconds = false;
649  LOG(VB_GENERAL, LOG_INFO,
650  QString("Preview from bookmark (frame %1)").arg(captime));
651  }
652  else
653  captime = -1;
654  }
655 
656  if (captime <= 0)
657  {
658  m_timeInSeconds = true;
659  int startEarly = 0;
660  int programDuration = 0;
661  int preroll = gCoreContext->GetNumSetting("RecordPreRoll", 0);
662  if (m_programInfo.GetScheduledStartTime().isValid() &&
663  m_programInfo.GetScheduledEndTime().isValid() &&
666  {
667  programDuration = m_programInfo.GetScheduledStartTime()
669  }
670  if (m_programInfo.GetRecordingStartTime().isValid() &&
671  m_programInfo.GetScheduledStartTime().isValid() &&
674  {
675  startEarly = m_programInfo.GetRecordingStartTime()
677  }
678  if (programDuration > 0)
679  {
680  captime = startEarly + (programDuration / 3);
681  }
682  if (captime < 0)
683  captime = 600;
684  captime += preroll;
685  LOG(VB_GENERAL, LOG_INFO,
686  QString("Preview at calculated offset (%1 seconds)").arg(captime));
687  }
688 
689  int width = 0;
690  int height = 0;
691  int sz = 0;
692  auto *data = (unsigned char*) GetScreenGrab(m_programInfo, m_pathname,
693  captime, m_timeInSeconds,
694  sz, width, height, aspect);
695 
697 
698  QString format = (m_outFormat.isEmpty()) ? "PNG" : m_outFormat;
699 
700  int dw = (m_outSize.width() < 0) ? width : m_outSize.width();
701  int dh = (m_outSize.height() < 0) ? height : m_outSize.height();
702 
703  bool ok = SavePreview(outname, data, width, height, aspect, dw, dh,
704  format);
705 
706  if (ok)
707  {
708  // Backdate file to start of preview time in case a bookmark was made
709  // while we were generating the preview.
710  struct utimbuf times {};
711 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
712  times.actime = times.modtime = dt.toTime_t();
713 #else
714  times.actime = times.modtime = dt.toSecsSinceEpoch();
715 #endif
716  utime(outname.toLocal8Bit().constData(), &times);
717  }
718 
719  delete[] data;
720 
722 
723  return ok;
724 }
725 
727  const QString &pathname, const QString &outFileName)
728 {
729  QString outname = pathname + ".png";
730 
731  if (outFileName.isEmpty())
732  return outname;
733 
734  outname = outFileName;
735  QFileInfo fi(outname);
736  if (outname == fi.fileName())
737  {
738  QString dir;
739  if (pathname.contains(':'))
740  {
741  QUrl uinfo(pathname);
742  uinfo.setPath("");
743  dir = uinfo.toString();
744  }
745  else
746  {
747  dir = QFileInfo(pathname).path();
748  }
749  outname = dir + "/" + fi.fileName();
750  LOG(VB_FILE, LOG_INFO, LOC + QString("outfile '%1' -> '%2'")
751  .arg(outFileName).arg(outname));
752  }
753 
754  return outname;
755 }
756 
758 {
759  QString tmppathname = m_pathname;
760 
761  if (tmppathname.startsWith("dvd:"))
762  tmppathname = tmppathname.section(":", 1, 1);
763 
764  if (!QFileInfo(tmppathname).isReadable())
765  return false;
766 
767  tmppathname = m_outFileName.isEmpty() ? tmppathname : m_outFileName;
768  QString pathdir = QFileInfo(tmppathname).path();
769 
770  if (!QFileInfo(pathdir).isWritable())
771  {
772  LOG(VB_GENERAL, LOG_WARNING, LOC +
773  QString("Output path '%1' is not writeable") .arg(pathdir));
774  return false;
775  }
776 
777  return true;
778 }
779 
796  const ProgramInfo &pginfo, const QString &filename,
797  long long seektime, bool time_in_secs,
798  int &bufferlen,
799  int &video_width, int &video_height, float &video_aspect)
800 {
801  (void) pginfo;
802  (void) filename;
803  (void) seektime;
804  (void) time_in_secs;
805  (void) bufferlen;
806  (void) video_width;
807  (void) video_height;
808  char *retbuf = nullptr;
809  bufferlen = 0;
810 
812  {
813  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer could not connect to DB.");
814  return nullptr;
815  }
816 
817  // pre-test local files for existence and size. 500 ms speed-up...
818  if (filename.startsWith("/"))
819  {
820  QFileInfo info(filename);
821  bool invalid = (!info.exists() || !info.isReadable() ||
822  (info.isFile() && (info.size() < 8*1024)));
823  if (invalid)
824  {
825  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer file " +
826  QString("'%1'").arg(filename) + " is not valid.");
827  return nullptr;
828  }
829  }
830 
831  RingBuffer *rbuf = RingBuffer::Create(filename, false, false, 0);
832  if (!rbuf || !rbuf->IsOpen())
833  {
834  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer could not open file: " +
835  QString("'%1'").arg(filename));
836  delete rbuf;
837  return nullptr;
838  }
839 
840  auto *ctx = new PlayerContext(kPreviewGeneratorInUseID);
841  ctx->SetRingBuffer(rbuf);
842  ctx->SetPlayingInfo(&pginfo);
843  ctx->SetPlayer(new MythPlayer((PlayerFlags)(kAudioMuted | kVideoIsNull | kNoITV)));
844  ctx->m_player->SetPlayerInfo(nullptr, nullptr, ctx);
845 
846  if (time_in_secs)
847  {
848  retbuf = ctx->m_player->GetScreenGrab(seektime, bufferlen,
849  video_width, video_height, video_aspect);
850  }
851  else
852  {
853  retbuf = ctx->m_player->GetScreenGrabAtFrame(
854  seektime, true, bufferlen,
855  video_width, video_height, video_aspect);
856  }
857 
858  delete ctx;
859 
860  if (retbuf)
861  {
862  LOG(VB_GENERAL, LOG_INFO, LOC +
863  QString("Grabbed 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  else
868  {
869  LOG(VB_GENERAL, LOG_ERR, LOC +
870  QString("Failed to grab preview '%0' %1x%2@%3%4")
871  .arg(filename).arg(video_width).arg(video_height)
872  .arg(seektime).arg((time_in_secs) ? "s" : "f"));
873  }
874 
875  return retbuf;
876 }
877 
878 /* 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
void SetOutputFilename(const QString &fileName)
bool IsLocal(void) const
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:560
PlayerFlags
Definition: mythplayer.h:86
~PreviewGenerator() override
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
bool RemotePreviewRun(void)
static Type MythEventMessage
Definition: mythevent.h:73
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:30
void removeListener(QObject *listener)
Remove a listener to the observable.
QWaitCondition m_previewWaitCondition
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:384
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:333
Holds information on recordings and videos.
Definition: programinfo.h:67
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:338
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:852
uint GetRecordingID(void) const
Definition: programinfo.h:440
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:391
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:366
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.
void AttachSignals(QObject *obj)
QDateTime GetRecordingStartTime(void) const
Approximate time the recording started.
Definition: programinfo.h:398
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:552
bool RunReal(void)
This call creates a preview without starting a new thread.
Default UTC, "yyyyMMddhhmmss".
Definition: mythdate.h:15
QString GetHostName(void)
QString GetPathname(void) const
Definition: programinfo.h:337
avoid blocking LIRC & Joystick Menu
Definition: mythsystem.h:34
bool SaveOutFile(const QByteArray &data, const QDateTime &dt)