MythTV  master
previewgeneratorqueue.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 #ifndef PREVIEW_GENERATOR_QUEUE_H
3 #define PREVIEW_GENERATOR_QUEUE_H
4 
5 #include <QStringList>
6 #include <QDateTime>
7 #include <QMutex>
8 #include <QMap>
9 #include <QSet>
10 
11 #include "libmythbase/mthread.h"
12 
13 #include "previewgenerator.h"
14 #include "mythtvexp.h"
15 
16 class ProgramInfo;
17 class QSize;
18 
24 {
25  public:
26  PreviewGenState() = default;
27 
29  PreviewGenerator *m_gen {nullptr};
30 
32  bool m_genStarted {false};
33 
37 
40  std::chrono::seconds m_lastBlockTime {0s};
41 
44  QDateTime m_blockRetryUntil;
45 
48  QSet<QString> m_tokens;
49 };
50 using PreviewMap = QMap<QString,PreviewGenState>;
51 
68 class MTV_PUBLIC PreviewGeneratorQueue : public QObject, public MThread
69 {
70  Q_OBJECT
71 
72  public:
73  static void CreatePreviewGeneratorQueue(
75  uint maxAttempts, std::chrono::seconds minBlockSeconds);
76  static void TeardownPreviewGeneratorQueue();
77 
89  static void GetPreviewImage(const ProgramInfo &pginfo, const QString& token)
90  {
91  GetPreviewImage(pginfo, QSize(0,0), "", -1s, -1, token);
92  }
93  static void GetPreviewImage(const ProgramInfo &pginfo, QSize outputsize,
94  const QString &outputfile,
95  std::chrono::seconds time, long long frame,
96  const QString& token);
97  static void AddListener(QObject *listener);
98  static void RemoveListener(QObject *listener);
99 
100  private:
102  uint maxAttempts, std::chrono::seconds minBlockSeconds);
103  ~PreviewGeneratorQueue() override;
104 
105  QString GeneratePreviewImage(ProgramInfo &pginfo, QSize size,
106  const QString &outputfile,
107  std::chrono::seconds time, long long frame,
108  const QString& token);
109 
110  void GetInfo(const QString &key, uint &queue_depth, uint &token_cnt);
111  void SetPreviewGenerator(const QString &key, PreviewGenerator *g);
112  void IncPreviewGeneratorPriority(const QString &key, const QString& token);
113  void UpdatePreviewGeneratorThreads(void);
114  bool IsGeneratingPreview(const QString &key) const;
115  uint IncPreviewGeneratorAttempts(const QString &key);
116  void ClearPreviewGeneratorAttempts(const QString &key);
117 
118  bool event(QEvent *e) override; // QObject
119 
120  void SendEvent(const ProgramInfo &pginfo,
121  const QString &eventname,
122  const QString &filename,
123  const QString &token,
124  const QString &msg,
125  const QDateTime &dt);
126 
127  private:
133  QSet<QObject*> m_listeners;
135  mutable QMutex m_lock;
142  QMap<QString,QString> m_tokenToKeyMap;
145  QStringList m_queue;
147  uint m_running {0};
150  uint m_maxThreads {2};
157  std::chrono::seconds m_minBlockSeconds;
158 };
159 
160 #endif // PREVIEW_GENERATOR_QUEUE_H
PreviewGenState
This class holds all the state information related to a specific preview generator.
Definition: previewgeneratorqueue.h:23
PreviewGenState::PreviewGenState
PreviewGenState()=default
PreviewGenState::m_lastBlockTime
std::chrono::seconds m_lastBlockTime
The amount of time (in seconds) that this generator was blocked before it could start.
Definition: previewgeneratorqueue.h:40
mythtvexp.h
PreviewGenerator
This class creates a preview image of a recording.
Definition: previewgenerator.h:27
PreviewGenState::m_blockRetryUntil
QDateTime m_blockRetryUntil
Any request to generate an image will be ignored until after this time.
Definition: previewgeneratorqueue.h:44
SendEvent
static int SendEvent(const MythUtilCommandLineParser &cmdline)
Definition: backendutils.cpp:47
PreviewGeneratorQueue::m_maxAttempts
uint m_maxAttempts
How many times total will the code attempt to generate a preview for a specific file,...
Definition: previewgeneratorqueue.h:154
PreviewGeneratorQueue::m_mode
PreviewGenerator::Mode m_mode
Definition: previewgeneratorqueue.h:137
PreviewGenState::m_tokens
QSet< QString > m_tokens
The full set of tokens for all callers that have requested this preview.
Definition: previewgeneratorqueue.h:48
PreviewGeneratorQueue::m_lock
QMutex m_lock
The thread interlock for this data structure.
Definition: previewgeneratorqueue.h:135
PreviewMap
QMap< QString, PreviewGenState > PreviewMap
Definition: previewgeneratorqueue.h:50
PreviewGeneratorQueue::m_minBlockSeconds
std::chrono::seconds m_minBlockSeconds
How long after a failed preview generation attempt will the code ignore subsequent requests.
Definition: previewgeneratorqueue.h:157
PreviewGenState::m_gen
PreviewGenerator * m_gen
A pointer to the generator that this state object describes.
Definition: previewgeneratorqueue.h:29
PreviewGenState::m_attempts
uint m_attempts
How many attempts have been made to generate a preview for this file.
Definition: previewgeneratorqueue.h:36
uint
unsigned int uint
Definition: compat.h:81
PreviewGeneratorQueue::m_previewMap
PreviewMap m_previewMap
A mapping from the generated preview name to the state information on the progress of generating the ...
Definition: previewgeneratorqueue.h:140
MTV_PUBLIC
#define MTV_PUBLIC
Definition: mythtvexp.h:15
PreviewGeneratorQueue::m_listeners
QSet< QObject * > m_listeners
The set of all listeners that want messages when a preview request is queued or finishes.
Definition: previewgeneratorqueue.h:133
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
PreviewGeneratorQueue::GetPreviewImage
static void GetPreviewImage(const ProgramInfo &pginfo, const QString &token)
Submit a request for the generation of a preview image.
Definition: previewgeneratorqueue.h:89
PreviewGenState::m_genStarted
bool m_genStarted
The preview generator for this file is currently running.
Definition: previewgeneratorqueue.h:32
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
PreviewGeneratorQueue::m_queue
QStringList m_queue
The queue of previews to be generated.
Definition: previewgeneratorqueue.h:145
mthread.h
previewgenerator.h
PreviewGeneratorQueue::s_pgq
static PreviewGeneratorQueue * s_pgq
The singleton queue.
Definition: previewgeneratorqueue.h:130
PreviewGeneratorQueue
This class implements a queue of preview generation requests.
Definition: previewgeneratorqueue.h:68
build_compdb.filename
filename
Definition: build_compdb.py:21
PreviewGenerator::Mode
Mode
Definition: previewgenerator.h:40
PreviewGeneratorQueue::m_tokenToKeyMap
QMap< QString, QString > m_tokenToKeyMap
A mapping from requestor tokens to internal keys.
Definition: previewgeneratorqueue.h:142