MythTV  master
previewgeneratorqueue.cpp
Go to the documentation of this file.
1 
3 
4 // C++
5 #include <algorithm>
6 using std::max;
7 
8 // QT
9 #include <QCoreApplication>
10 #include <QFileInfo>
11 
12 // libmythbase
13 #include "mythcorecontext.h"
14 #include "mythlogging.h"
15 #include "mythdirs.h"
16 #include "mthread.h"
17 
18 // libmyth
19 #include "mythcontext.h"
20 #include "remoteutil.h"
21 
22 // libmythtv
23 #include "previewgenerator.h"
24 
25 #define LOC QString("PreviewQueue: ")
26 
28 
45  uint maxAttempts, uint minBlockSeconds)
46 {
47  s_pgq = new PreviewGeneratorQueue(mode, maxAttempts, minBlockSeconds);
48 }
49 
57 {
58  s_pgq->exit(0);
59  s_pgq->wait();
60  delete s_pgq;
61  s_pgq = nullptr;
62 }
63 
64 /*
65  * Create the queue object for holding preview generators.
66  *
67  * Create the singleton queue of preview generators. This should be
68  * called once at program start-up. All generation requests on this
69  * queue will will be created with the maxAttempts and minBlockSeconds
70  * parameters supplied here.
71  *
72  * \param[in] mode Local or Remote (or both)
73  * \param[in] maxAttempts How many times total will the code attempt
74  * to generate a preview for a specific file, before giving
75  * up and ignoring all future requests.
76  * \param[in] minBlockSeconds How long after a failed preview
77  * generation attempt will the code ignore subsequent
78  * requests.
79  *
80  * \note Never call this routine directly. Call the
81  * CreatePreviewGeneratorQueue function instead.
82  */
85  uint maxAttempts, uint minBlockSeconds) :
86  MThread("PreviewGeneratorQueue"),
87  m_mode(mode),
88  m_maxAttempts(maxAttempts), m_minBlockSeconds(minBlockSeconds)
89 {
90  if (PreviewGenerator::kLocal & mode)
91  {
92  int idealThreads = QThread::idealThreadCount();
93  m_maxThreads = (idealThreads >= 1) ? idealThreads * 2 : 2;
94  }
95 
96  moveToThread(qthread());
97  start();
98 }
99 
109 {
110  // disconnect preview generators
111  QMutexLocker locker(&m_lock);
112  // NOLINTNEXTLINE(modernize-loop-convert)
113  for (auto it = m_previewMap.begin(); it != m_previewMap.end(); ++it)
114  {
115  if ((*it).m_gen)
116  (*it).m_gen->deleteLater();
117  (*it).m_gen = nullptr;
118  }
119  locker.unlock();
120  wait();
121 }
122 
145  const ProgramInfo &pginfo,
146  const QSize &outputsize,
147  const QString &outputfile,
148  long long time, bool in_seconds,
149  const QString& token)
150 {
151  if (!s_pgq)
152  return;
153 
154  if (pginfo.GetPathname().isEmpty() ||
155  pginfo.GetBasename() == pginfo.GetPathname())
156  {
157  return;
158  }
159 
160  if (gCoreContext->GetNumSetting("JobAllowPreview", 1) == 0)
161  return;
162 
163  QStringList extra;
164  pginfo.ToStringList(extra);
165  extra += token;
166  extra += QString::number(outputsize.width());
167  extra += QString::number(outputsize.height());
168  extra += outputfile;
169  extra += QString::number(time);
170  extra += (in_seconds ? "1" : "0");
171  auto *e = new MythEvent("GET_PREVIEW", extra);
172  QCoreApplication::postEvent(s_pgq, e);
173 }
174 
182 void PreviewGeneratorQueue::AddListener(QObject *listener)
183 {
184  if (!s_pgq)
185  return;
186 
187  QMutexLocker locker(&s_pgq->m_lock);
188  s_pgq->m_listeners.insert(listener);
189 }
190 
199 {
200  if (!s_pgq)
201  return;
202 
203  QMutexLocker locker(&s_pgq->m_lock);
204  s_pgq->m_listeners.remove(listener);
205 }
206 
224 {
225  if (e->type() != MythEvent::MythEventMessage)
226  return QObject::event(e);
227 
228  auto *me = dynamic_cast<MythEvent*>(e);
229  if (me == nullptr)
230  return false;
231  if (me->Message() == "GET_PREVIEW")
232  {
233  const QStringList &list = me->ExtraDataList();
234  QStringList::const_iterator it = list.begin();
235  ProgramInfo evinfo(it, list.end());
236  QString token;
237  QSize outputsize;
238  QString outputfile;
239  long long time = -1LL;
240  if (it != list.end())
241  token = (*it++);
242  if (it != list.end())
243  outputsize.setWidth((*it++).toInt());
244  if (it != list.end())
245  outputsize.setHeight((*it++).toInt());
246  if (it != list.end())
247  outputfile = (*it++);
248  if (it != list.end())
249  time = (*it++).toLongLong();
250  if (it != list.end())
251  {
252  bool time_fmt_sec = (*it++).toInt() != 0;
253  GeneratePreviewImage(evinfo, outputsize, outputfile,
254  time, time_fmt_sec, token);
255  }
256  return true;
257  }
258  if (me->Message() == "PREVIEW_SUCCESS" ||
259  me->Message() == "PREVIEW_FAILED")
260  {
261  uint recordedingID = me->ExtraData(0).toUInt(); // pginfo->GetRecordingID()
262  const QString& filename = me->ExtraData(1); // outFileName
263  const QString& msg = me->ExtraData(2);
264  const QString& datetime = me->ExtraData(3);
265  const QString& token = me->ExtraData(4);
266 
267  {
268  QMutexLocker locker(&m_lock);
269  QMap<QString,QString>::iterator kit = m_tokenToKeyMap.find(token);
270  if (kit == m_tokenToKeyMap.end())
271  {
272  LOG(VB_GENERAL, LOG_ERR, LOC +
273  QString("Failed to find token %1 in map.").arg(token));
274  return true;
275  }
276  PreviewMap::iterator it = m_previewMap.find(*kit);
277  if (it == m_previewMap.end())
278  {
279  LOG(VB_GENERAL, LOG_ERR, LOC +
280  QString("Failed to find key %1 in map.").arg(*kit));
281  return true;
282  }
283 
284  if ((*it).m_gen)
285  (*it).m_gen->deleteLater();
286  (*it).m_gen = nullptr;
287  (*it).m_genStarted = false;
288  if (me->Message() == "PREVIEW_SUCCESS")
289  {
290  (*it).m_attempts = 0;
291  (*it).m_lastBlockTime = 0;
292  (*it).m_blockRetryUntil = QDateTime();
293  }
294  else
295  {
296  (*it).m_lastBlockTime =
297  max(m_minBlockSeconds, (*it).m_lastBlockTime * 2);
298  (*it).m_blockRetryUntil =
299  MythDate::current().addSecs((*it).m_lastBlockTime);
300  }
301 
302  QStringList list;
303  list.push_back(QString::number(recordedingID));
304  list.push_back(filename);
305  list.push_back(msg);
306  list.push_back(datetime);
307  foreach (const auto & tok, (*it).m_tokens)
308  {
309  kit = m_tokenToKeyMap.find(tok);
310  if (kit != m_tokenToKeyMap.end())
311  m_tokenToKeyMap.erase(kit);
312  list.push_back(tok);
313  }
314 
315  if (list.size() > 4)
316  {
317  foreach (auto listener, m_listeners)
318  {
319  auto *le = new MythEvent(me->Message(), list);
320  QCoreApplication::postEvent(listener, le);
321  }
322  (*it).m_tokens.clear();
323  }
324 
325  m_running = (m_running > 0) ? m_running - 1 : 0;
326  }
327 
329 
330  return true;
331  }
332  return false;
333 }
334 
357  const ProgramInfo &pginfo,
358  const QString &eventname,
359  const QString &filename, const QString &token, const QString &msg,
360  const QDateTime &dt)
361 {
362  QStringList list;
363  list.push_back(QString::number(pginfo.GetRecordingID()));
364  list.push_back(filename);
365  list.push_back(msg);
366  list.push_back(dt.toUTC().toString(Qt::ISODate));
367  list.push_back(token);
368 
369  QMutexLocker locker(&m_lock);
370  foreach (auto listener, m_listeners)
371  {
372  auto *e = new MythEvent(eventname, list);
373  QCoreApplication::postEvent(listener, e);
374  }
375 }
376 
413  ProgramInfo &pginfo,
414  const QSize &size,
415  const QString &outputfile,
416  long long time, bool in_seconds,
417  const QString& token)
418 {
419  QString key = QString("%1_%2x%3_%4%5")
420  .arg(pginfo.GetBasename()).arg(size.width()).arg(size.height())
421  .arg(time).arg(in_seconds?"s":"f");
422 
423  if (pginfo.GetAvailableStatus() == asPendingDelete)
424  {
425  SendEvent(pginfo, "PREVIEW_FAILED", key, token,
426  "Pending Delete", QDateTime());
427  return QString();
428  }
429 
430  // keep in sync with default filename in PreviewGenerator::RunReal
431  QString filename = (outputfile.isEmpty()) ?
432  pginfo.GetPathname() + ".png" : outputfile;
433  QString ret_file = filename;
434  QString ret;
435 
436  bool is_special = !outputfile.isEmpty() || time >= 0 ||
437  size.width() || size.height();
438 
439  bool needs_gen = true;
440  if (!is_special)
441  {
442  QDateTime previewLastModified;
443  bool streaming = !filename.startsWith("/");
444  bool locally_accessible = false;
445  bool bookmark_updated = false;
446 
447  QDateTime bookmark_ts = pginfo.GetBookmarkUpdate();
448  QDateTime cmp_ts;
449  if (bookmark_ts.isValid())
450  cmp_ts = bookmark_ts;
451  else if (MythDate::current() >= pginfo.GetRecordingEndTime())
452  cmp_ts = pginfo.GetLastModifiedTime();
453  else
454  cmp_ts = pginfo.GetRecordingStartTime();
455 
456  if (streaming)
457  {
458  ret_file = QString("%1/%2")
459  .arg(GetRemoteCacheDir()).arg(filename.section('/', -1));
460 
461  QFileInfo finfo(ret_file);
462  if (finfo.isReadable() && finfo.lastModified() >= cmp_ts)
463  {
464  // This is just an optimization to avoid
465  // hitting the backend if our cached copy
466  // is newer than the bookmark, or if we have
467  // a preview and do not update it when the
468  // bookmark changes.
469  previewLastModified = finfo.lastModified();
470  }
471  else if (!IsGeneratingPreview(key))
472  {
473  previewLastModified =
474  RemoteGetPreviewIfModified(pginfo, ret_file);
475  }
476  }
477  else
478  {
479  QFileInfo fi(filename);
480  if ((locally_accessible = fi.isReadable()))
481  previewLastModified = fi.lastModified();
482  }
483 
484  bookmark_updated =
485  (!previewLastModified.isValid() || (previewLastModified <= cmp_ts));
486 
487  if (bookmark_updated && bookmark_ts.isValid() &&
488  previewLastModified.isValid())
489  {
491  }
492 
493  bool preview_exists = previewLastModified.isValid();
494 
495 #if 0
496  QString alttext = (bookmark_ts.isValid()) ? QString() :
497  QString("\n\t\t\tcmp_ts: %1")
498  .arg(cmp_ts.toString(Qt::ISODate));
499  LOG(VB_GENERAL, LOG_INFO,
500  QString("previewLastModified: %1\n\t\t\t"
501  "bookmark_ts: %2%3\n\t\t\t"
502  "pginfo.lastmodified: %4")
503  .arg(previewLastModified.toString(Qt::ISODate))
504  .arg(bookmark_ts.toString(Qt::ISODate))
505  .arg(alttext)
506  .arg(pginfo.GetLastModifiedTime(MythDate::ISODate)) +
507  QString("Title: %1\n\t\t\t")
508  .arg(pginfo.toString(ProgramInfo::kTitleSubtitle)) +
509  QString("File '%1' \n\t\t\tCache '%2'")
510  .arg(filename).arg(ret_file) +
511  QString("\n\t\t\tPreview Exists: %1, Bookmark Updated: %2, "
512  "Need Preview: %3")
513  .arg(preview_exists).arg(bookmark_updated)
514  .arg((bookmark_updated || !preview_exists)));
515 #endif
516 
517  needs_gen = bookmark_updated || !preview_exists;
518 
519  if (!needs_gen)
520  {
521  if (locally_accessible)
522  ret = filename;
523  else if (preview_exists && QFileInfo(ret_file).isReadable())
524  ret = ret_file;
525  }
526  }
527 
528  if (needs_gen && !IsGeneratingPreview(key))
529  {
530  uint attempts = IncPreviewGeneratorAttempts(key);
531  if (attempts < m_maxAttempts)
532  {
533  LOG(VB_PLAYBACK, LOG_INFO, LOC +
534  QString("Requesting preview for '%1'") .arg(key));
535  auto *pg = new PreviewGenerator(&pginfo, token, m_mode);
536  if (!outputfile.isEmpty() || time >= 0 ||
537  size.width() || size.height())
538  {
539  pg->SetPreviewTime(time, in_seconds);
540  pg->SetOutputFilename(outputfile);
541  pg->SetOutputSize(size);
542  }
543 
544  SetPreviewGenerator(key, pg);
545 
546  LOG(VB_PLAYBACK, LOG_INFO, LOC +
547  QString("Requested preview for '%1'").arg(key));
548  }
549  else
550  {
551  LOG(VB_GENERAL, LOG_ERR, LOC +
552  QString("Attempted to generate preview for '%1' "
553  "%2 times; >= max(%3)")
554  .arg(key).arg(attempts).arg(m_maxAttempts));
555  }
556  }
557  else if (needs_gen)
558  {
559  LOG(VB_PLAYBACK, LOG_INFO, LOC +
560  QString("Not requesting preview for %1,"
561  "as it is already being generated")
562  .arg(pginfo.toString(ProgramInfo::kTitleSubtitle)));
563  IncPreviewGeneratorPriority(key, token);
564  }
565 
567 
568  if (!ret.isEmpty())
569  {
570  QString msg = "On Disk";
571  QDateTime dt = QFileInfo(ret).lastModified();
572  SendEvent(pginfo, "PREVIEW_SUCCESS", ret, token, msg, dt);
573  }
574  else
575  {
576  uint queue_depth = 0;
577  uint token_cnt = 0;
578  GetInfo(key, queue_depth, token_cnt);
579  QString msg = QString("Queue depth %1, our tokens %2")
580  .arg(queue_depth).arg(token_cnt);
581  SendEvent(pginfo, "PREVIEW_QUEUED", ret, token, msg, QDateTime());
582  }
583 
584  return ret;
585 }
586 
600  const QString &key, uint &queue_depth, uint &token_cnt)
601 {
602  QMutexLocker locker(&m_lock);
603  queue_depth = m_queue.size();
604  PreviewMap::iterator pit = m_previewMap.find(key);
605  token_cnt = (pit == m_previewMap.end()) ? 0 : (*pit).m_tokens.size();
606 }
607 
616  const QString &key, const QString& token)
617 {
618  QMutexLocker locker(&m_lock);
619  m_queue.removeAll(key);
620 
621  PreviewMap::iterator pit = m_previewMap.find(key);
622  if (pit == m_previewMap.end())
623  return;
624 
625  if ((*pit).m_gen && !(*pit).m_genStarted)
626  m_queue.push_back(key);
627 
628  if (!token.isEmpty())
629  {
630  m_tokenToKeyMap[token] = key;
631  (*pit).m_tokens.insert(token);
632  }
633 }
634 
640 {
641  QMutexLocker locker(&m_lock);
642  QStringList &q = m_queue;
643  if (!q.empty() && (m_running < m_maxThreads))
644  {
645  QString fn = q.back();
646  q.pop_back();
647  PreviewMap::iterator it = m_previewMap.find(fn);
648  if (it != m_previewMap.end() && (*it).m_gen && !(*it).m_genStarted)
649  {
650  m_running++;
651  (*it).m_gen->start();
652  (*it).m_genStarted = true;
653  }
654  }
655 }
656 
666  const QString &key, PreviewGenerator *g)
667 {
668  if (!g)
669  return;
670 
671  {
672  QMutexLocker locker(&m_lock);
673  m_tokenToKeyMap[g->GetToken()] = key;
674  PreviewGenState &state = m_previewMap[key];
675  if (state.m_gen)
676  {
677  if (g && state.m_gen != g)
678  {
679  if (!g->GetToken().isEmpty())
680  state.m_tokens.insert(g->GetToken());
681  g->deleteLater();
682  g = nullptr;
683  }
684  }
685  else
686  {
687  g->AttachSignals(this);
688  state.m_gen = g;
689  state.m_genStarted = false;
690  if (!g->GetToken().isEmpty())
691  state.m_tokens.insert(g->GetToken());
692  }
693  }
694 
696 }
697 
712 bool PreviewGeneratorQueue::IsGeneratingPreview(const QString &key) const
713 {
714  PreviewMap::const_iterator it;
715  QMutexLocker locker(&m_lock);
716 
717  if ((it = m_previewMap.find(key)) == m_previewMap.end())
718  return false;
719 
720  if ((*it).m_blockRetryUntil.isValid())
721  return MythDate::current() < (*it).m_blockRetryUntil;
722 
723  return (*it).m_gen;
724 }
725 
735 {
736  QMutexLocker locker(&m_lock);
737  return m_previewMap[key].m_attempts++;
738 }
739 
750 {
751  QMutexLocker locker(&m_lock);
752  m_previewMap[key].m_attempts = 0;
753  m_previewMap[key].m_lastBlockTime = 0;
754  m_previewMap[key].m_blockRetryUntil =
755  MythDate::current().addSecs(-60);
756 }
757 
758 
PreviewGeneratorQueue(PreviewGenerator::Mode mode, uint maxAttempts, uint minBlockSeconds)
PreviewGenerator::Mode m_mode
uint m_minBlockSeconds
How long after a failed preview generation attempt will the code ignore subsequent requests.
QStringList m_queue
The queue of previews to be generated.
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
static void GetPreviewImage(const ProgramInfo &pginfo, const QString &token)
Submit a request for the generation of a preview image.
QDateTime GetBookmarkUpdate(void) const
Definition: programinfo.h:469
static Type MythEventMessage
Definition: mythevent.h:73
#define LOC
This class holds all the state information related to a specific preview generator.
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 void TeardownPreviewGeneratorQueue()
Destroy the singleton queue of preview generators.
QString toString(Verbosity v=kLongDescription, const QString &sep=":", const QString &grp="\"") const
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
void ClearPreviewGeneratorAttempts(const QString &key)
Clears the number of times we have started a PreviewGenerator to create this file.
void GetInfo(const QString &key, uint &queue_depth, uint &token_cnt)
static void RemoveListener(QObject *listener)
Stop receiving notifications when a preview event is generated.
bool m_genStarted
The preview generator for this file is currently running.
This class creates a preview image of a recording.
QString GetToken(void) const
Holds information on recordings and videos.
Definition: programinfo.h:67
void SetPreviewGenerator(const QString &key, PreviewGenerator *g)
Sets the PreviewGenerator for a specific file.
uint IncPreviewGeneratorAttempts(const QString &key)
Increments and returns number of times we have started a PreviewGenerator to create this file.
This class is used as a container for messages.
Definition: mythevent.h:16
QMap< QString, QString > m_tokenToKeyMap
A mapping from requestor tokens to internal keys.
QDateTime RemoteGetPreviewIfModified(const ProgramInfo &pginfo, const QString &cachefile)
Download preview & get timestamp if newer than cachefile's last modified time, otherwise just get the...
Definition: remoteutil.cpp:238
PreviewGenerator * m_gen
A pointer to the generator that this state object describes.
QString GetBasename(void) const
Definition: programinfo.h:338
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
void exit(int retcode=0)
Use this to exit from the thread if you are using a Qt event loop.
Definition: mthread.cpp:289
~PreviewGeneratorQueue() override
Destroy the preview generation queue.
AvailableStatusType GetAvailableStatus(void) const
Definition: programinfo.h:811
QSet< QObject * > m_listeners
The set of all listeners that want messages when a preview request is queued or finishes.
uint GetRecordingID(void) const
Definition: programinfo.h:440
unsigned int uint
Definition: compat.h:140
QString GeneratePreviewImage(ProgramInfo &pginfo, const QSize &size, const QString &outputfile, long long time, bool in_seconds, const QString &token)
Generate a preview image for the specified program.
int GetNumSetting(const QString &key, int defaultval=0)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QMutex m_lock
The thread interlock for this data structure.
void SendEvent(const ProgramInfo &pginfo, const QString &eventname, const QString &filename, const QString &token, const QString &msg, const QDateTime &dt)
Send a message back to all objects that have requested creation of a specific preview.
PreviewMap m_previewMap
A mapping from the generated preview name to the state information on the progress of generating the ...
uint m_running
The number of threads currently generating previews.
void AttachSignals(QObject *obj)
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
static PreviewGeneratorQueue * s_pgq
The singleton queue.
bool event(QEvent *e) override
The event handler running on the preview generation thread.
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
Definition: mthread.cpp:244
QDateTime GetRecordingStartTime(void) const
Approximate time the recording started.
Definition: programinfo.h:398
QDateTime GetRecordingEndTime(void) const
Approximate time the recording should have ended, did end, or is intended to end.
Definition: programinfo.h:406
QDateTime GetLastModifiedTime(void) const
Definition: programinfo.h:423
bool IsGeneratingPreview(const QString &key) const
Is a preview currently being generated for this key.
Default UTC.
Definition: mythdate.h:14
void UpdatePreviewGeneratorThreads(void)
As long as there are items in the queue, make sure we're running the maximum allowed number of previe...
uint m_maxAttempts
How many times total will the code attempt to generate a preview for a specific file,...
uint m_maxThreads
The maximum number of threads that may concurrently generate previews.
QString GetPathname(void) const
Definition: programinfo.h:337
QString GetRemoteCacheDir(void)
Returns the directory for all files cached from the backend.
Definition: mythdirs.cpp:241
QSet< QString > m_tokens
The full set of tokens for all callers that have requested this preview.
void IncPreviewGeneratorPriority(const QString &key, const QString &token)
This class implements a queue of preview generation requests.
static void CreatePreviewGeneratorQueue(PreviewGenerator::Mode mode, uint maxAttempts, uint minBlockSeconds)
Create the singleton queue of preview generators.
static void AddListener(QObject *listener)
Request notifications when a preview event is generated.