MythTV  master
scheduler.h
Go to the documentation of this file.
1 #ifndef SCHEDULER_H_
2 #define SCHEDULER_H_
3 
4 // C++ headers
5 #include <deque>
6 #include <vector>
7 using namespace std;
8 
9 // Qt headers
10 #include <QWaitCondition>
11 #include <QObject>
12 #include <QString>
13 #include <QMutex>
14 #include <QMap>
15 #include <QSet>
16 
17 // MythTV headers
18 #include "filesysteminfo.h"
19 #include "recordinginfo.h"
20 #include "remoteutil.h"
21 #include "mythdeque.h"
22 #include "mythscheduler.h"
23 #include "mthread.h"
24 #include "scheduledrecording.h"
25 
26 class EncoderLink;
27 class MainServer;
28 class AutoExpire;
29 
30 class Scheduler;
31 
33 {
34  public:
36  inputid(0),
37  sgroupid(0),
38  schedgroup(false),
39  group_inputs(),
40  conflicting_inputs(),
41  conflictlist(nullptr) {};
42  ~SchedInputInfo(void) = default;
43 
46  bool schedgroup;
47  vector<uint> group_inputs;
48  vector<uint> conflicting_inputs;
50 };
51 
52 class Scheduler : public MThread, public MythScheduler
53 {
54  public:
55  Scheduler(bool runthread, QMap<int, EncoderLink *> *tvList,
56  QString recordTbl = "record", Scheduler *master_sched = nullptr);
57  ~Scheduler();
58 
59  void Stop(void);
60  void Wait(void) { MThread::wait(); }
61 
62  void SetExpirer(AutoExpire *autoExpirer) { m_expirer = autoExpirer; }
63 
64  void Reschedule(const QStringList &request);
65  void RescheduleMatch(uint recordid, uint sourceid, uint mplexid,
66  const QDateTime &maxstarttime, const QString &why)
68  mplexid, maxstarttime, why)); };
69  void RescheduleCheck(const RecordingInfo &recinfo, const QString &why)
71  void ReschedulePlace(const QString &why)
73 
74  void AddRecording(const RecordingInfo&);
75  void AddRecording(const ProgramInfo& prog)
76  { AddRecording(RecordingInfo(prog)); };
77  void FillRecordListFromDB(uint recordid = 0);
78  void FillRecordListFromMaster(void);
79 
80  void UpdateRecStatus(RecordingInfo *pginfo);
81  void UpdateRecStatus(uint cardid, uint chanid,
82  const QDateTime &startts, RecStatus::Type recstatus,
83  const QDateTime &recendts);
84  // Returns a list of all pending recordings and returns
85  // true iff there are conflicts
86  bool GetAllPending(RecList &retList, int recRuleId = 0) const;
87  bool GetAllPending(ProgramList &retList, int recRuleId = 0) const;
88  void GetAllPending(QStringList &strList) const override; // MythScheduler
89  QMap<QString,ProgramInfo*> GetRecording(void) const override; // MythScheduler
90 
91  enum SchedSortColumn { kSortTitle, kSortLastRecorded, kSortNextRecording,
92  kSortPriority, kSortType };
93  static void GetAllScheduled(QStringList &strList,
94  SchedSortColumn sortBy = kSortTitle,
95  bool ascending = true);
96  static void GetAllScheduled(RecList &proglist,
97  SchedSortColumn sortBy = kSortTitle,
98  bool ascending = true);
99 
100  void getConflicting(RecordingInfo *pginfo, QStringList &strlist);
101  void getConflicting(RecordingInfo *pginfo, RecList *retlist);
102 
103  void PrintList(bool onlyFutureRecordings = false)
104  { PrintList(reclist, onlyFutureRecordings); };
105  void PrintList(RecList &list, bool onlyFutureRecordings = false);
106  void PrintRec(const RecordingInfo *p, const QString &prefix = "");
107 
108  void SetMainServer(MainServer *ms);
109 
110  void SlaveConnected(RecordingList &slavelist);
111  void SlaveDisconnected(uint cardid);
112 
113  void DisableScheduling(void) { schedulingEnabled = false; }
114  void EnableScheduling(void) { schedulingEnabled = true; }
115  void GetNextLiveTVDir(uint cardid);
116  void ResetIdleTime(void);
117 
118  bool WasStartedAutomatically();
119 
120  RecStatus::Type GetRecStatus(const ProgramInfo &pginfo);
121 
122  int GetError(void) const { return error; }
123 
124  void AddChildInput(uint parentid, uint childid);
125 
126  protected:
127  void run(void) override; // MThread
128 
129  private:
130  enum OpenEndType {
131  openEndNever = 0,
132  openEndDiffChannel = 1,
133  openEndAlways = 2
134  };
135 
136  QString recordTable;
137  QString priorityTable;
138 
139  bool VerifyCards(void);
140 
141  bool InitInputInfoMap(void);
142  void CreateTempTables(void);
143  void DeleteTempTables(void);
144  void UpdateDuplicates(void);
145  bool FillRecordList(void);
146  void UpdateMatches(uint recordid, uint sourceid, uint mplexid,
147  const QDateTime &maxstarttime);
148  void UpdateManuals(uint recordid);
149  void BuildWorkList(void);
150  bool ClearWorkList(void);
151  void AddNewRecords(void);
152  void AddNotListed(void);
153  void BuildNewRecordsQueries(uint recordid, QStringList &from,
154  QStringList &where, MSqlBindings &bindings);
155  void PruneOverlaps(void);
156  void BuildListMaps(void);
157  void ClearListMaps(void);
158 
159  bool IsBusyRecording(const RecordingInfo *rcinfo);
160 
161  bool IsSameProgram(const RecordingInfo *a, const RecordingInfo *b) const;
162 
163  bool FindNextConflict(const RecList &cardlist,
164  const RecordingInfo *p, RecConstIter &iter,
165  OpenEndType openEnd = openEndNever,
166  uint *paffinity = nullptr) const;
167  const RecordingInfo *FindConflict(const RecordingInfo *p,
168  OpenEndType openEnd = openEndNever,
169  uint *affinity = nullptr,
170  bool checkAll = false)
171  const;
172  void MarkOtherShowings(RecordingInfo *p);
173  void MarkShowingsList(RecList &showinglist, RecordingInfo *p);
174  void BackupRecStatus(void);
175  void RestoreRecStatus(void);
176  bool TryAnotherShowing(RecordingInfo *p, bool samePriority,
177  bool livetv = false);
178  void SchedNewRecords(void);
179  void SchedNewFirstPass(RecIter &start, RecIter end,
180  int recpriority, int recpriority2);
181  void SchedNewRetryPass(RecIter start, RecIter end,
182  bool samePriority, bool livetv = false);
183  void SchedLiveTV(void);
184  void PruneRedundants(void);
185  void UpdateNextRecord(void);
186 
187  bool ChangeRecordingEnd(RecordingInfo *oldp, RecordingInfo *newp);
188 
189  bool CheckShutdownServer(int prerollseconds, QDateTime &idleSince,
190  bool &blockShutdown, uint logmask);
191  void ShutdownServer(int prerollseconds, QDateTime &idleSince);
192  void PutInactiveSlavesToSleep(void);
193  bool WakeUpSlave(QString slaveHostname, bool setWakingStatus = true);
194  void WakeUpSlaves(void);
195 
196  int FillRecordingDir(const QString &title,
197  const QString &hostname,
198  const QString &storagegroup,
199  const QDateTime &recstartts,
200  const QDateTime &recendts,
201  uint cardid,
202  QString &recording_dir,
203  const RecList &reclist);
204  void FillDirectoryInfoCache(void);
205 
206  void OldRecordedFixups(void);
207  void ResetDuplicates(uint recordid, uint findid, const QString &title,
208  const QString &subtitle, const QString &descrip,
209  const QString &programid);
210  bool HandleReschedule(void);
211  bool HandleRunSchedulerStartup(
212  int prerollseconds, int idleWaitForRecordingTime);
213  void HandleWakeSlave(RecordingInfo &ri, int prerollseconds);
214  bool HandleRecording(RecordingInfo &ri, bool &statuschanged,
215  QDateTime &nextStartTime, QDateTime &nextWakeTime,
216  int prerollseconds);
217  void HandleRecordingStatusChange(
218  RecordingInfo &ri, RecStatus::Type recStatus, const QString &details);
219  bool AssignGroupInput(RecordingInfo &ri, int prerollseconds);
220  void HandleIdleShutdown(
221  bool &blockShutdown, QDateTime &idleSince, int prerollseconds,
223  bool &statuschanged);
224 
225  void EnqueueMatch(uint recordid, uint sourceid, uint mplexid,
226  const QDateTime &maxstarttime, const QString &why)
227  { reschedQueue.enqueue(ScheduledRecording::BuildMatchRequest(recordid,
228  sourceid, mplexid, maxstarttime, why)); };
229  void EnqueueCheck(const RecordingInfo &recinfo, const QString &why)
230  { reschedQueue.enqueue(ScheduledRecording::BuildCheckRequest(recinfo,
231  why)); };
232  void EnqueuePlace(const QString &why)
233  { reschedQueue.enqueue(ScheduledRecording::BuildPlaceRequest(why)); };
234 
236  { return !reschedQueue.empty(); };
237  void ClearRequestQueue(void)
238  { reschedQueue.clear(); };
239 
240  bool CreateConflictLists(void);
241 
243  mutable QMutex schedLock;
245  QWaitCondition reschedWait;
249  QMap<uint, SchedInputInfo> sinputinfomap;
250  vector<RecList *> conflictlists;
251  QMap<uint, RecList> recordidlistmap;
252  QMap<QString, RecList> titlelistmap;
253 
254  QDateTime schedTime;
256 
257  bool specsched;
259  QMap<int, bool> schedAfterStartMap;
260 
261  QMap<int, EncoderLink *> *m_tvList;
263 
265 
266  bool doRun;
267 
269 
272 
275 
276  QMap<QString, FileSystemInfo> fsInfoCache;
277 
278  int error;
279 
280  QSet<QString> sysEvents[4];
281 
282  // Try to avoid LiveTV sessions until this time
283  QDateTime livetvTime;
284 
285  QDateTime lastPrepareTime;
286 
288 
289  // cache IsSameProgram()
290  typedef pair<const RecordingInfo*,const RecordingInfo*> IsSameKey;
291  typedef QMap<IsSameKey,bool> IsSameCacheType;
294 };
295 
296 #endif
bool m_isShuttingDown
Definition: scheduler.h:273
vector< RecList * > conflictlists
Definition: scheduler.h:250
This is an generic interface to a program scheduler.
Definition: mythscheduler.h:20
RecList reclist
Definition: scheduler.h:246
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
void ClearRequestQueue(void)
Definition: scheduler.h:237
SchedSortColumn
Definition: scheduler.h:91
QMap< QString, FileSystemInfo > fsInfoCache
Definition: scheduler.h:276
QMutex schedLock
Definition: scheduler.h:243
QSet< uint > m_schedorder_warned
Definition: scheduler.h:264
RecList * conflictlist
Definition: scheduler.h:49
SchedInputInfo(void)
Definition: scheduler.h:35
static void error(const char *str,...)
Definition: vbi.c:41
MSqlQueryInfo dbConn
Definition: scheduler.h:274
void AddRecording(const ProgramInfo &prog)
Definition: scheduler.h:75
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:312
pair< const RecordingInfo *, const RecordingInfo * > IsSameKey
Definition: scheduler.h:290
void EnqueuePlace(const QString &why)
Definition: scheduler.h:232
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:34
QMap< IsSameKey, bool > IsSameCacheType
Definition: scheduler.h:291
unsigned int uint
Definition: compat.h:140
bool schedulingEnabled
Definition: scheduler.h:258
void DisableScheduling(void)
Definition: scheduler.h:113
bool doRun
Definition: scheduler.h:266
bool specsched
Definition: scheduler.h:257
QString priorityTable
Definition: scheduler.h:137
RecList::const_iterator RecConstIter
Definition: mythscheduler.h:16
bool reclist_changed
Definition: scheduler.h:255
QDateTime lastPrepareTime
Definition: scheduler.h:285
RecList::iterator RecIter
Definition: mythscheduler.h:17
void RescheduleMatch(uint recordid, uint sourceid, uint mplexid, const QDateTime &maxstarttime, const QString &why)
Definition: scheduler.h:65
unsigned char b
Definition: ParseText.cpp:340
static QStringList BuildCheckRequest(const RecordingInfo &recinfo, const QString &why)
Holds information on recordings and videos.
Definition: programinfo.h:66
QWaitCondition reschedWait
Definition: scheduler.h:245
static int run(MythMediaDevice *dev=nullptr, bool startRandomShow=false)
void EnableScheduling(void)
Definition: scheduler.h:114
static QStringList BuildMatchRequest(uint recordid, uint sourceid, uint mplexid, const QDateTime &maxstarttime, const QString &why)
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
void RescheduleCheck(const RecordingInfo &recinfo, const QString &why)
Definition: scheduler.h:69
static GlobalSpinBoxSetting * idleTimeoutSecs()
QMap< uint, RecList > recordidlistmap
Definition: scheduler.h:251
void EnqueueMatch(uint recordid, uint sourceid, uint mplexid, const QDateTime &maxstarttime, const QString &why)
Definition: scheduler.h:225
QMap< uint, SchedInputInfo > sinputinfomap
Definition: scheduler.h:249
void ReschedulePlace(const QString &why)
Definition: scheduler.h:71
void PrintList(bool onlyFutureRecordings=false)
Definition: scheduler.h:103
QDateTime livetvTime
Definition: scheduler.h:283
string hostname
Definition: caa.py:17
bool HaveQueuedRequests(void)
Definition: scheduler.h:235
int error
Definition: scheduler.h:278
void SetExpirer(AutoExpire *autoExpirer)
Definition: scheduler.h:62
void Wait(void)
Definition: scheduler.h:60
MSqlDatabase Info, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:90
static GlobalSpinBoxSetting * idleWaitForRecordingTime()
int tmLastLog
Definition: scheduler.h:293
void EnqueueCheck(const RecordingInfo &recinfo, const QString &why)
Definition: scheduler.h:229
bool resetIdleTime
Definition: scheduler.h:271
bool schedgroup
Definition: scheduler.h:46
AutoExpire * m_expirer
Definition: scheduler.h:262
Used to expire recordings to make space for new recordings.
Definition: autoexpire.h:61
static QStringList BuildPlaceRequest(const QString &why)
vector< uint > group_inputs
Definition: scheduler.h:47
MainServer * m_mainServer
Definition: scheduler.h:268
QMap< int, bool > schedAfterStartMap
Definition: scheduler.h:259
static int Reschedule(const MythUtilCommandLineParser &)
QMap< QString, RecList > titlelistmap
Definition: scheduler.h:252
MythDeque< QStringList > reschedQueue
Definition: scheduler.h:242
QString recordTable
Definition: scheduler.h:136
RecList worklist
Definition: scheduler.h:247
OpenEndType m_openEnd
Definition: scheduler.h:287
QDateTime schedTime
Definition: scheduler.h:254
QMap< QString, QVariant > MSqlBindings
typedef for a map of string -> string bindings for generic queries.
Definition: mythdbcon.h:98
std::deque< RecordingInfo * > RecList
Definition: mythscheduler.h:10
QMutex resetIdleTime_lock
Definition: scheduler.h:270
vector< uint > conflicting_inputs
Definition: scheduler.h:48
RecList livetvlist
Definition: scheduler.h:248
IsSameCacheType cache_is_same_program
Definition: scheduler.h:292
QMap< int, EncoderLink * > * m_tvList
Definition: scheduler.h:261
QMap< int, EncoderLink * > tvList
QMutex recordmatchLock
Definition: scheduler.h:244
int GetError(void) const
Definition: scheduler.h:122