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 #include "io/mythmediabuffer.h"
24 #include "mythplayer.h"
25 #include "previewgenerator.h"
26 #include "tv_rec.h"
27 #include "mythsocket.h"
28 #include "remotefile.h"
29 #include "storagegroup.h"
30 #include "mythdate.h"
31 #include "playercontext.h"
32 #include "mythdirs.h"
33 #include "remoteutil.h"
34 #include "mythsystemlegacy.h"
35 #include "exitcodes.h"
36 #include "mythlogging.h"
37 #include "mythmiscutil.h"
38 
39 #define LOC QString("Preview: ")
40 
73  QString token,
75  : MThread("PreviewGenerator"),
76  m_programInfo(*pginfo), m_mode(mode),
77  m_pathname(pginfo->GetPathname()),
78  m_token(std::move(token))
79 {
80  // Qt requires that a receiver have the same thread affinity as the QThread
81  // sending the event, which is used to dispatch MythEvents sent by
82  // gCoreContext->dispatchNow(me)
83  moveToThread(QApplication::instance()->thread());
84 }
85 
87 {
88  TeardownAll();
89  wait();
90 }
91 
92 void PreviewGenerator::SetOutputFilename(const QString &fileName)
93 {
94  if (fileName.isEmpty())
95  return;
96  QFileInfo fileinfo = QFileInfo(fileName);
97  m_outFileName = fileName;
98  m_outFormat = fileinfo.suffix().toUpper();
99 }
100 
102 {
103  QMutexLocker locker(&m_previewLock);
104  m_previewWaitCondition.wakeAll();
105  m_listener = nullptr;
106 }
107 
109 {
110  TeardownAll();
111  QObject::deleteLater();
112 }
113 
115 {
116  QMutexLocker locker(&m_previewLock);
117  m_listener = obj;
118 }
119 
124 {
125  QString msg;
126  QTime tm = QTime::currentTime();
127  QElapsedTimer te; te.start();
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(te.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(te.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  QElapsedTimer te; te.start();
212  bool ok = false;
213  QString command = GetAppBinDir() + "mythpreviewgen";
214  bool local_ok = ((IsLocal() || ((m_mode & kForceLocal) != 0)) &&
215  ((m_mode & kLocal) != 0) &&
216  QFileInfo(command).isExecutable());
217  if (!local_ok)
218  {
219  if (!!(m_mode & kRemote))
220  {
221  ok = RemotePreviewRun();
222  if (ok)
223  {
224  msg =
225  QString("Generated remotely in %1 seconds, starting at %2")
226  .arg(te.elapsed()*0.001)
227  .arg(tm.toString(Qt::ISODate));
228  }
229  }
230  else
231  {
232  LOG(VB_GENERAL, LOG_ERR, LOC +
233  QString("Run() cannot generate preview locally for: '%1'")
234  .arg(m_pathname));
235  msg = "Failed, local preview requested for remote file.";
236  }
237  }
238  else
239  {
240  // This is where we fork and run mythpreviewgen to actually make preview
241  QStringList cmdargs;
242 
243  cmdargs << "--size"
244  << QString("%1x%2").arg(m_outSize.width()).arg(m_outSize.height());
245  if (m_captureTime >= 0)
246  {
247  if (m_timeInSeconds)
248  cmdargs << "--seconds";
249  else
250  cmdargs << "--frame";
251  cmdargs << QString::number(m_captureTime);
252  }
253  cmdargs << "--chanid"
254  << QString::number(m_programInfo.GetChanID())
255  << "--starttime"
257 
258  if (!m_outFileName.isEmpty())
259  cmdargs << "--outfile" << m_outFileName;
260 
261  // Timeout in 30s
262  auto *ms = new MythSystemLegacy(command, cmdargs,
268  ms->SetNice(10);
269  ms->SetIOPrio(7);
270 
271  ms->Run(30);
272  uint ret = ms->Wait();
273  delete ms;
274 
275  if (ret != GENERIC_EXIT_OK)
276  {
277  LOG(VB_GENERAL, LOG_ERR, LOC +
278  QString("Encountered problems running '%1 %2' - (%3)")
279  .arg(command).arg(cmdargs.join(" ")).arg(ret));
280  }
281  else
282  {
283  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Preview process returned 0.");
284  QString outname = (!m_outFileName.isEmpty()) ?
285  m_outFileName : (m_pathname + ".png");
286 
287  QString lpath = QFileInfo(outname).fileName();
288  if (lpath == outname)
289  {
290  StorageGroup sgroup;
291  QString tmpFile = sgroup.FindFile(lpath);
292  outname = (tmpFile.isEmpty()) ? outname : tmpFile;
293  }
294 
295  QFileInfo fi(outname);
296  ok = (fi.exists() && fi.isReadable() && (fi.size() != 0));
297  if (ok)
298  {
299  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Preview process ran ok.");
300  msg = QString("Generated on %1 in %2 seconds, starting at %3")
301  .arg(gCoreContext->GetHostName())
302  .arg(te.elapsed()*0.001)
303  .arg(tm.toString(Qt::ISODate));
304  }
305  else
306  {
307  LOG(VB_GENERAL, LOG_ERR, LOC + "Preview process not ok." +
308  QString("\n\t\t\tfileinfo(%1)").arg(outname) +
309  QString(" exists: %1").arg(fi.exists()) +
310  QString(" readable: %1").arg(fi.isReadable()) +
311  QString(" size: %1").arg(fi.size()));
312  LOG(VB_GENERAL, LOG_ERR, LOC +
313  QString("Despite command '%1' returning success")
314  .arg(command));
315  msg = QString("Failed to read preview image despite "
316  "preview process returning success.");
317  }
318  }
319  }
320 
321  QMutexLocker locker(&m_previewLock);
322 
323  // Backdate file to start of preview time in case a bookmark was made
324  // while we were generating the preview.
325  QString output_fn = m_outFileName.isEmpty() ?
327 
328  QDateTime dt;
329  if (ok)
330  {
331  QFileInfo fi(output_fn);
332  if (fi.exists())
333  dt = fi.lastModified();
334  }
335 
336  QString message = (ok) ? "PREVIEW_SUCCESS" : "PREVIEW_FAILED";
337  if (m_listener)
338  {
339  QStringList list;
340  list.push_back(QString::number(m_programInfo.GetRecordingID()));
341  list.push_back(output_fn);
342  list.push_back(msg);
343  list.push_back(dt.isValid()?dt.toUTC().toString(Qt::ISODate):"");
344  list.push_back(m_token);
345  QCoreApplication::postEvent(m_listener, new MythEvent(message, list));
346  }
347 
348  return ok;
349 }
350 
352 {
353  RunProlog();
354  Run();
355  RunEpilog();
356 }
357 
359 {
360  QStringList strlist( "QUERY_GENPIXMAP2" );
361  if (m_token.isEmpty())
362  {
363  m_token = QString("%1:%2")
364  .arg(m_programInfo.MakeUniqueKey()).arg(random());
365  }
366  strlist.push_back(m_token);
367  m_programInfo.ToStringList(strlist);
368  strlist.push_back(m_timeInSeconds ? "s" : "f");
369  strlist.push_back(QString::number(m_captureTime));
370  if (m_outFileName.isEmpty())
371  {
372  strlist.push_back("<EMPTY>");
373  }
374  else
375  {
376  QFileInfo fi(m_outFileName);
377  strlist.push_back(fi.fileName());
378  }
379  strlist.push_back(QString::number(m_outSize.width()));
380  strlist.push_back(QString::number(m_outSize.height()));
381 
382  gCoreContext->addListener(this);
383  m_pixmapOk = false;
384 
385  bool ok = gCoreContext->SendReceiveStringList(strlist);
386  if (!ok || strlist.empty() || (strlist[0] != "OK"))
387  {
388  if (!ok)
389  {
390  LOG(VB_GENERAL, LOG_ERR, LOC +
391  "Remote Preview failed due to communications error.");
392  }
393  else if (strlist.size() > 1)
394  {
395  LOG(VB_GENERAL, LOG_ERR, LOC +
396  "Remote Preview failed, reason given: " + strlist[1]);
397  }
398 
400 
401  return false;
402  }
403 
404  QMutexLocker locker(&m_previewLock);
405 
406  // wait up to 35 seconds for the preview to complete
407  // The backend waits 30s for creation
408  if (!m_gotReply)
409  m_previewWaitCondition.wait(&m_previewLock, 35 * 1000);
410 
411  if (!m_gotReply)
412  LOG(VB_GENERAL, LOG_NOTICE, LOC + "RemotePreviewRun() -- no reply..");
413 
415 
416  return m_pixmapOk;
417 }
418 
420 {
421  if (e->type() != MythEvent::MythEventMessage)
422  return QObject::event(e);
423 
424  auto *me = dynamic_cast<MythEvent*>(e);
425  if (me == nullptr)
426  return false;
427  if (me->Message() != "GENERATED_PIXMAP" || me->ExtraDataCount() < 3)
428  return QObject::event(e);
429 
430  bool ok = me->ExtraData(0) == "OK";
431  bool ours = false;
432  uint i = ok ? 4 : 3;
433  for (; i < (uint) me->ExtraDataCount() && !ours; i++)
434  ours |= me->ExtraData(i) == m_token;
435  if (!ours)
436  return false;
437 
438  const QString& pginfokey = me->ExtraData(1);
439 
440  QMutexLocker locker(&m_previewLock);
441  m_gotReply = true;
442  m_pixmapOk = ok;
443  if (!ok)
444  {
445  LOG(VB_GENERAL, LOG_ERR, LOC + pginfokey + ": " + me->ExtraData(2));
446  m_previewWaitCondition.wakeAll();
447  return true;
448  }
449 
450  if (me->ExtraDataCount() < 5)
451  {
452  m_pixmapOk = false;
453  m_previewWaitCondition.wakeAll();
454  return true; // could only happen with very broken client...
455  }
456 
457  QDateTime datetime = MythDate::fromString(me->ExtraData(3));
458  if (!datetime.isValid())
459  {
460  m_pixmapOk = false;
461  LOG(VB_GENERAL, LOG_ERR, LOC + pginfokey + "Got invalid date");
462  m_previewWaitCondition.wakeAll();
463  return false;
464  }
465 
466  size_t length = me->ExtraData(4).toULongLong();
467  quint16 checksum16 = me->ExtraData(5).toUInt();
468  QByteArray data = QByteArray::fromBase64(me->ExtraData(6).toLatin1());
469  if ((size_t) data.size() < length)
470  { // (note data.size() may be up to 3
471  // bytes longer after decoding
472  LOG(VB_GENERAL, LOG_ERR, LOC +
473  QString("Preview size check failed %1 < %2")
474  .arg(data.size()).arg(length));
475  data.clear();
476  }
477  data.resize(length);
478 
479  if (checksum16 != qChecksum(data.constData(), data.size()))
480  {
481  LOG(VB_GENERAL, LOG_ERR, LOC + "Preview checksum failed");
482  data.clear();
483  }
484 
485  m_pixmapOk = (data.isEmpty()) ? false : SaveOutFile(data, datetime);
486 
487  m_previewWaitCondition.wakeAll();
488 
489  return true;
490 }
491 
492 bool PreviewGenerator::SaveOutFile(const QByteArray &data, const QDateTime &dt)
493 {
494  if (m_outFileName.isEmpty())
495  {
496  QString remotecachedirname =
497  QString("%1/cache/remotecache").arg(GetConfDir());
498  QDir remotecachedir(remotecachedirname);
499 
500  if (!remotecachedir.exists())
501  {
502  if (!remotecachedir.mkdir(remotecachedirname))
503  {
504  LOG(VB_GENERAL, LOG_ERR, LOC +
505  "Remote Preview failed because we could not create a "
506  "remote cache directory");
507  return false;
508  }
509  }
510 
511  QString filename = m_programInfo.GetBasename() + ".png";
512  SetOutputFilename(QString("%1/%2").arg(remotecachedirname).arg(filename));
513  }
514 
515  QFile file(m_outFileName);
516  bool ok = file.open(QIODevice::Unbuffered|QIODevice::WriteOnly);
517  if (!ok)
518  {
519  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to open: '%1'")
520  .arg(m_outFileName));
521  }
522 
523  off_t offset = 0;
524  size_t remaining = data.size();
525  uint failure_cnt = 0;
526  while ((remaining > 0) && (failure_cnt < 5))
527  {
528  ssize_t written = file.write(data.data() + offset, remaining);
529  if (written < 0)
530  {
531  failure_cnt++;
532  usleep(50000);
533  continue;
534  }
535 
536  failure_cnt = 0;
537  offset += written;
538  remaining -= written;
539  }
540 
541  if (ok && !remaining)
542  {
543  file.close();
544  struct utimbuf times {};
545 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
546  times.actime = times.modtime = dt.toTime_t();
547 #else
548  times.actime = times.modtime = dt.toSecsSinceEpoch();
549 #endif
550  utime(m_outFileName.toLocal8Bit().constData(), &times);
551  LOG(VB_FILE, LOG_INFO, LOC + QString("Saved: '%1'").arg(m_outFileName));
552  }
553  else
554  {
555  file.remove();
556  }
557 
558  return ok;
559 }
560 
562  const unsigned char *data,
563  uint width, uint height, float aspect,
564  int desired_width, int desired_height,
565  const QString &format)
566 {
567  if (!data || !width || !height)
568  return false;
569 
570  const QImage img((unsigned char*) data,
571  width, height, QImage::Format_RGB32);
572 
573  float ppw = max(desired_width, 0);
574  float pph = max(desired_height, 0);
575  bool desired_size_exactly_specified = true;
576  if ((ppw < 1.0F) && (pph < 1.0F))
577  {
578  ppw = img.width();
579  pph = img.height();
580  desired_size_exactly_specified = false;
581  }
582 
583  aspect = (aspect <= 0.0F) ? ((float) width) / height : aspect;
584  pph = (pph < 1.0F) ? (ppw / aspect) : pph;
585  ppw = (ppw < 1.0F) ? (pph * aspect) : ppw;
586 
587  if (!desired_size_exactly_specified)
588  {
589  if (aspect > ppw / pph)
590  pph = (ppw / aspect);
591  else
592  ppw = (pph * aspect);
593  }
594 
595  ppw = max(1.0F, ppw);
596  pph = max(1.0F, pph);;
597 
598  QImage small_img = img.scaled((int) ppw, (int) pph,
599  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
600 
601  QTemporaryFile f(QFileInfo(filename).absoluteFilePath()+".XXXXXX");
602  f.setAutoRemove(false);
603  if (f.open() && small_img.save(&f, format.toLocal8Bit().constData()))
604  {
605  // Let anybody update it
606  bool ret = makeFileAccessible(f.fileName().toLocal8Bit().constData());
607  if (!ret)
608  {
609  LOG(VB_GENERAL, LOG_ERR, "Unable to change permissions on "
610  "preview image. Backends and frontends "
611  "running under different users will be "
612  "unable to access it");
613  }
614  QFile of(filename);
615  of.remove();
616  if (f.rename(filename))
617  {
618  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Saved preview '%0' %1x%2")
619  .arg(filename).arg((int) ppw).arg((int) pph));
620  return true;
621  }
622  f.remove();
623  }
624 
625  return false;
626 }
627 
629 {
633 
634  float aspect = 0;
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  int width = 0;
689  int height = 0;
690  int sz = 0;
691  auto *data = (unsigned char*) GetScreenGrab(m_programInfo, m_pathname,
692  captime, m_timeInSeconds,
693  sz, width, height, aspect);
694 
696 
697  QString format = (m_outFormat.isEmpty()) ? "PNG" : m_outFormat;
698 
699  int dw = (m_outSize.width() < 0) ? width : m_outSize.width();
700  int dh = (m_outSize.height() < 0) ? height : m_outSize.height();
701 
702  bool ok = SavePreview(outname, data, width, height, aspect, dw, dh,
703  format);
704 
705  if (ok)
706  {
707  // Backdate file to start of preview time in case a bookmark was made
708  // while we were generating the preview.
709  struct utimbuf times {};
710 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
711  times.actime = times.modtime = dt.toTime_t();
712 #else
713  times.actime = times.modtime = dt.toSecsSinceEpoch();
714 #endif
715  utime(outname.toLocal8Bit().constData(), &times);
716  }
717 
718  delete[] data;
719 
721 
722  return ok;
723 }
724 
726  const QString &pathname, const QString &outFileName)
727 {
728  QString outname = pathname + ".png";
729 
730  if (outFileName.isEmpty())
731  return outname;
732 
733  outname = outFileName;
734  QFileInfo fi(outname);
735  if (outname == fi.fileName())
736  {
737  QString dir;
738  if (pathname.contains(':'))
739  {
740  QUrl uinfo(pathname);
741  uinfo.setPath("");
742  dir = uinfo.toString();
743  }
744  else
745  {
746  dir = QFileInfo(pathname).path();
747  }
748  outname = dir + "/" + fi.fileName();
749  LOG(VB_FILE, LOG_INFO, LOC + QString("outfile '%1' -> '%2'")
750  .arg(outFileName).arg(outname));
751  }
752 
753  return outname;
754 }
755 
757 {
758  QString tmppathname = m_pathname;
759 
760  if (tmppathname.startsWith("dvd:"))
761  tmppathname = tmppathname.section(":", 1, 1);
762 
763  if (!QFileInfo(tmppathname).isReadable())
764  return false;
765 
766  tmppathname = m_outFileName.isEmpty() ? tmppathname : m_outFileName;
767  QString pathdir = QFileInfo(tmppathname).path();
768 
769  if (!QFileInfo(pathdir).isWritable())
770  {
771  LOG(VB_GENERAL, LOG_WARNING, LOC +
772  QString("Output path '%1' is not writeable") .arg(pathdir));
773  return false;
774  }
775 
776  return true;
777 }
778 
795  const ProgramInfo &pginfo, const QString &filename,
796  long long seektime, bool time_in_secs,
797  int &bufferlen,
798  int &video_width, int &video_height, float &video_aspect)
799 {
800  (void) pginfo;
801  (void) filename;
802  (void) seektime;
803  (void) time_in_secs;
804  (void) bufferlen;
805  (void) video_width;
806  (void) video_height;
807  char *retbuf = nullptr;
808  bufferlen = 0;
809 
811  {
812  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer could not connect to DB.");
813  return nullptr;
814  }
815 
816  // pre-test local files for existence and size. 500 ms speed-up...
817  if (filename.startsWith("/"))
818  {
819  QFileInfo info(filename);
820  bool invalid = (!info.exists() || !info.isReadable() ||
821  (info.isFile() && (info.size() < 8*1024)));
822  if (invalid)
823  {
824  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer file " +
825  QString("'%1'").arg(filename) + " is not valid.");
826  return nullptr;
827  }
828  }
829 
830  MythMediaBuffer* buffer = MythMediaBuffer::Create(filename, false, false, 0);
831  if (!buffer || !buffer->IsOpen())
832  {
833  LOG(VB_GENERAL, LOG_ERR, LOC + "Previewer could not open file: " +
834  QString("'%1'").arg(filename));
835  delete buffer;
836  return nullptr;
837  }
838 
839  auto *ctx = new PlayerContext(kPreviewGeneratorInUseID);
840  ctx->SetRingBuffer(buffer);
841  ctx->SetPlayingInfo(&pginfo);
842  ctx->SetPlayer(new MythPlayer((PlayerFlags)(kAudioMuted | kVideoIsNull | kNoITV)));
843  ctx->m_player->SetPlayerInfo(nullptr, nullptr, ctx);
844 
845  if (time_in_secs)
846  {
847  retbuf = ctx->m_player->GetScreenGrab(seektime, bufferlen,
848  video_width, video_height, video_aspect);
849  }
850  else
851  {
852  retbuf = ctx->m_player->GetScreenGrabAtFrame(
853  seektime, true, bufferlen,
854  video_width, video_height, video_aspect);
855  }
856 
857  delete ctx;
858 
859  if (retbuf)
860  {
861  LOG(VB_GENERAL, LOG_INFO, LOC +
862  QString("Grabbed preview '%0' %1x%2@%3%4")
863  .arg(filename).arg(video_width).arg(video_height)
864  .arg(seektime).arg((time_in_secs) ? "s" : "f"));
865  }
866  else
867  {
868  LOG(VB_GENERAL, LOG_ERR, LOC +
869  QString("Failed to grab preview '%0' %1x%2@%3%4")
870  .arg(filename).arg(video_width).arg(video_height)
871  .arg(seektime).arg((time_in_secs) ? "s" : "f"));
872  }
873 
874  return retbuf;
875 }
876 
877 /* 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
return token
Definition: musicutils.cpp:74
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:87
~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.
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
static MythMediaBuffer * Create(const QString &Filename, bool Write, bool UseReadAhead=true, int Timeout=kDefaultOpenTimeout, bool StreamOnly=false)
Creates a RingBuffer instance.
#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
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
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
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)
virtual bool IsOpen(void) const =0
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)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23