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  m_group_inputs(),
37  m_conflicting_inputs() {};
38  ~SchedInputInfo(void) = default;
39 
40  uint m_inputid {0};
41  uint m_sgroupid {0};
42  bool m_schedgroup {false};
43  vector<uint> m_group_inputs;
44  vector<uint> m_conflicting_inputs;
45  RecList *m_conflictlist {nullptr};
46 };
47 
48 class Scheduler : public MThread, public MythScheduler
49 {
50  public:
51  Scheduler(bool runthread, QMap<int, EncoderLink *> *tvList,
52  const QString& tmptable = "record", Scheduler *master_sched = nullptr);
53  ~Scheduler();
54 
55  void Stop(void);
56  void Wait(void) { MThread::wait(); }
57 
58  void SetExpirer(AutoExpire *autoExpirer) { m_expirer = autoExpirer; }
59 
60  void Reschedule(const QStringList &request);
61  void RescheduleMatch(uint recordid, uint sourceid, uint mplexid,
62  const QDateTime &maxstarttime, const QString &why)
64  mplexid, maxstarttime, why)); };
65  void RescheduleCheck(const RecordingInfo &recinfo, const QString &why)
67  void ReschedulePlace(const QString &why)
69 
70  void AddRecording(const RecordingInfo&);
71  void AddRecording(const ProgramInfo& prog)
72  { AddRecording(RecordingInfo(prog)); };
73  void FillRecordListFromDB(uint recordid = 0);
74  void FillRecordListFromMaster(void);
75 
76  void UpdateRecStatus(RecordingInfo *pginfo);
77  void UpdateRecStatus(uint cardid, uint chanid,
78  const QDateTime &startts, RecStatus::Type recstatus,
79  const QDateTime &recendts);
80  // Returns a list of all pending recordings and returns
81  // true iff there are conflicts
82  bool GetAllPending(RecList &retList, int recRuleId = 0) const;
83  bool GetAllPending(ProgramList &retList, int recRuleId = 0) const;
84  void GetAllPending(QStringList &strList) const override; // MythScheduler
85  QMap<QString,ProgramInfo*> GetRecording(void) const override; // MythScheduler
86 
87  enum SchedSortColumn { kSortTitle, kSortLastRecorded, kSortNextRecording,
88  kSortPriority, kSortType };
89  static void GetAllScheduled(QStringList &strList,
90  SchedSortColumn sortBy = kSortTitle,
91  bool ascending = true);
92  static void GetAllScheduled(RecList &proglist,
93  SchedSortColumn sortBy = kSortTitle,
94  bool ascending = true);
95 
96  void getConflicting(RecordingInfo *pginfo, QStringList &strlist);
97  void getConflicting(RecordingInfo *pginfo, RecList *retlist);
98 
99  void PrintList(bool onlyFutureRecordings = false)
100  { PrintList(m_reclist, onlyFutureRecordings); };
101  void PrintList(RecList &list, bool onlyFutureRecordings = false);
102  void PrintRec(const RecordingInfo *p, const QString &prefix = "");
103 
104  void SetMainServer(MainServer *ms);
105 
106  void SlaveConnected(RecordingList &slavelist);
107  void SlaveDisconnected(uint cardid);
108 
109  void DisableScheduling(void) { m_schedulingEnabled = false; }
110  void EnableScheduling(void) { m_schedulingEnabled = true; }
111  void GetNextLiveTVDir(uint cardid);
112  void ResetIdleTime(void);
113 
114  bool WasStartedAutomatically();
115 
116  RecStatus::Type GetRecStatus(const ProgramInfo &pginfo);
117 
118  int GetError(void) const { return m_error; }
119 
120  void AddChildInput(uint parentid, uint inputid);
121 
122  protected:
123  void run(void) override; // MThread
124 
125  private:
126  enum OpenEndType {
127  openEndNever = 0,
128  openEndDiffChannel = 1,
129  openEndAlways = 2
130  };
131 
132  QString m_recordTable;
134 
135  bool VerifyCards(void);
136 
137  bool InitInputInfoMap(void);
138  void CreateTempTables(void);
139  void DeleteTempTables(void);
140  void UpdateDuplicates(void);
141  bool FillRecordList(void);
142  void UpdateMatches(uint recordid, uint sourceid, uint mplexid,
143  const QDateTime &maxstarttime);
144  void UpdateManuals(uint recordid);
145  void BuildWorkList(void);
146  bool ClearWorkList(void);
147  void AddNewRecords(void);
148  void AddNotListed(void);
149  void BuildNewRecordsQueries(uint recordid, QStringList &from,
150  QStringList &where, MSqlBindings &bindings);
151  void PruneOverlaps(void);
152  void BuildListMaps(void);
153  void ClearListMaps(void);
154 
155  bool IsBusyRecording(const RecordingInfo *rcinfo);
156 
157  bool IsSameProgram(const RecordingInfo *a, const RecordingInfo *b) const;
158 
159  bool FindNextConflict(const RecList &cardlist,
160  const RecordingInfo *p, RecConstIter &iter,
161  OpenEndType openEnd = openEndNever,
162  uint *paffinity = nullptr) const;
163  const RecordingInfo *FindConflict(const RecordingInfo *p,
164  OpenEndType openEnd = openEndNever,
165  uint *affinity = nullptr,
166  bool checkAll = false)
167  const;
168  void MarkOtherShowings(RecordingInfo *p);
169  void MarkShowingsList(RecList &showinglist, RecordingInfo *p);
170  void BackupRecStatus(void);
171  void RestoreRecStatus(void);
172  bool TryAnotherShowing(RecordingInfo *p, bool samePriority,
173  bool livetv = false);
174  void SchedNewRecords(void);
175  void SchedNewFirstPass(RecIter &start, const RecIter& end,
176  int recpriority, int recpriority2);
177  void SchedNewRetryPass(const RecIter& start, const RecIter& end,
178  bool samePriority, bool livetv = false);
179  void SchedLiveTV(void);
180  void PruneRedundants(void);
181  void UpdateNextRecord(void);
182 
183  bool ChangeRecordingEnd(RecordingInfo *oldp, RecordingInfo *newp);
184 
185  bool CheckShutdownServer(int prerollseconds, QDateTime &idleSince,
186  bool &blockShutdown, uint logmask);
187  void ShutdownServer(int prerollseconds, QDateTime &idleSince);
188  void PutInactiveSlavesToSleep(void);
189  bool WakeUpSlave(const QString& slaveHostname, bool setWakingStatus = true);
190  void WakeUpSlaves(void);
191 
192  int FillRecordingDir(const QString &title,
193  const QString &hostname,
194  const QString &storagegroup,
195  const QDateTime &recstartts,
196  const QDateTime &recendts,
197  uint cardid,
198  QString &recording_dir,
199  const RecList &reclist);
200  void FillDirectoryInfoCache(void);
201 
202  void OldRecordedFixups(void);
203  void ResetDuplicates(uint recordid, uint findid, const QString &title,
204  const QString &subtitle, const QString &descrip,
205  const QString &programid);
206  bool HandleReschedule(void);
207  bool HandleRunSchedulerStartup(
208  int prerollseconds, int idleWaitForRecordingTime);
209  void HandleWakeSlave(RecordingInfo &ri, int prerollseconds);
210  bool HandleRecording(RecordingInfo &ri, bool &statuschanged,
211  QDateTime &nextStartTime, QDateTime &nextWakeTime,
212  int prerollseconds);
213  void HandleRecordingStatusChange(
214  RecordingInfo &ri, RecStatus::Type recStatus, const QString &details);
215  bool AssignGroupInput(RecordingInfo &ri, int prerollseconds);
216  void HandleIdleShutdown(
217  bool &blockShutdown, QDateTime &idleSince, int prerollseconds,
219  bool &statuschanged);
220 
221  void EnqueueMatch(uint recordid, uint sourceid, uint mplexid,
222  const QDateTime &maxstarttime, const QString &why)
223  { m_reschedQueue.enqueue(ScheduledRecording::BuildMatchRequest(recordid,
224  sourceid, mplexid, maxstarttime, why)); };
225  void EnqueueCheck(const RecordingInfo &recinfo, const QString &why)
226  { m_reschedQueue.enqueue(ScheduledRecording::BuildCheckRequest(recinfo,
227  why)); };
228  void EnqueuePlace(const QString &why)
229  { m_reschedQueue.enqueue(ScheduledRecording::BuildPlaceRequest(why)); };
230 
232  { return !m_reschedQueue.empty(); };
233  void ClearRequestQueue(void)
234  { m_reschedQueue.clear(); };
235 
236  bool CreateConflictLists(void);
237 
239  mutable QMutex m_schedLock;
241  QWaitCondition m_reschedWait;
245  QMap<uint, SchedInputInfo> m_sinputinfomap;
246  vector<RecList *> m_conflictlists;
247  QMap<uint, RecList> m_recordidlistmap;
248  QMap<QString, RecList> m_titlelistmap;
249 
250  QDateTime m_schedTime;
251  bool m_reclist_changed {false};
252 
254  bool m_schedulingEnabled {true};
255  QMap<int, bool> m_schedAfterStartMap;
256 
257  QMap<int, EncoderLink *> *m_tvList {nullptr};
258  AutoExpire *m_expirer {nullptr};
259 
261 
262  bool m_doRun;
263 
264  MainServer *m_mainServer {nullptr};
265 
267  bool m_resetIdleTime {false};
268 
269  bool m_isShuttingDown {false};
271 
272  QMap<QString, FileSystemInfo> m_fsInfoCache;
273 
274  int m_error {0};
275 
276  QSet<QString> m_sysEvents[4];
277 
278  // Try to avoid LiveTV sessions until this time
279  QDateTime m_livetvTime;
280 
281  QDateTime m_lastPrepareTime;
282 
284 
285  // cache IsSameProgram()
286  typedef pair<const RecordingInfo*,const RecordingInfo*> IsSameKey;
287  typedef QMap<IsSameKey,bool> IsSameCacheType;
289  int m_tmLastLog {0};
290 };
291 
292 #endif
QMap< QString, FileSystemInfo > m_fsInfoCache
Definition: scheduler.h:272
This is an generic interface to a program scheduler.
Definition: mythscheduler.h:20
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
void ClearRequestQueue(void)
Definition: scheduler.h:233
SchedSortColumn
Definition: scheduler.h:87
QMap< QString, RecList > m_titlelistmap
Definition: scheduler.h:248
QSet< uint > m_schedorder_warned
Definition: scheduler.h:260
SchedInputInfo(void)
Definition: scheduler.h:35
void AddRecording(const ProgramInfo &prog)
Definition: scheduler.h:71
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
pair< const RecordingInfo *, const RecordingInfo * > IsSameKey
Definition: scheduler.h:286
vector< uint > m_conflicting_inputs
Definition: scheduler.h:44
void EnqueuePlace(const QString &why)
Definition: scheduler.h:228
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:34
MSqlQueryInfo m_dbConn
Definition: scheduler.h:270
QMap< IsSameKey, bool > IsSameCacheType
Definition: scheduler.h:287
unsigned int uint
Definition: compat.h:140
void DisableScheduling(void)
Definition: scheduler.h:109
RecList::const_iterator RecConstIter
Definition: mythscheduler.h:16
RecList::iterator RecIter
Definition: mythscheduler.h:17
void RescheduleMatch(uint recordid, uint sourceid, uint mplexid, const QDateTime &maxstarttime, const QString &why)
Definition: scheduler.h:61
unsigned char b
Definition: ParseText.cpp:329
static QStringList BuildCheckRequest(const RecordingInfo &recinfo, const QString &why)
Holds information on recordings and videos.
Definition: programinfo.h:66
void EnableScheduling(void)
Definition: scheduler.h:110
bool m_doRun
Definition: scheduler.h:262
static QStringList BuildMatchRequest(uint recordid, uint sourceid, uint mplexid, const QDateTime &maxstarttime, const QString &why)
QDateTime m_lastPrepareTime
Definition: scheduler.h:281
void RescheduleCheck(const RecordingInfo &recinfo, const QString &why)
Definition: scheduler.h:65
QMutex m_recordmatchLock
Definition: scheduler.h:240
RecList m_reclist
Definition: scheduler.h:242
static GlobalSpinBoxSetting * idleTimeoutSecs()
QMutex m_resetIdleTime_lock
Definition: scheduler.h:266
void EnqueueMatch(uint recordid, uint sourceid, uint mplexid, const QDateTime &maxstarttime, const QString &why)
Definition: scheduler.h:221
QString m_priorityTable
Definition: scheduler.h:133
void ReschedulePlace(const QString &why)
Definition: scheduler.h:67
void PrintList(bool onlyFutureRecordings=false)
Definition: scheduler.h:99
QWaitCondition m_reschedWait
Definition: scheduler.h:241
QMap< uint, SchedInputInfo > m_sinputinfomap
Definition: scheduler.h:245
string hostname
Definition: caa.py:17
bool HaveQueuedRequests(void)
Definition: scheduler.h:231
RecList m_livetvlist
Definition: scheduler.h:244
QString m_recordTable
Definition: scheduler.h:132
QMap< int, bool > m_schedAfterStartMap
Definition: scheduler.h:255
void SetExpirer(AutoExpire *autoExpirer)
Definition: scheduler.h:58
void Wait(void)
Definition: scheduler.h:56
MSqlDatabase Info, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:90
static GlobalSpinBoxSetting * idleWaitForRecordingTime()
vector< uint > m_group_inputs
Definition: scheduler.h:43
void EnqueueCheck(const RecordingInfo &recinfo, const QString &why)
Definition: scheduler.h:225
QDateTime m_schedTime
Definition: scheduler.h:250
Used to expire recordings to make space for new recordings.
Definition: autoexpire.h:61
static QStringList BuildPlaceRequest(const QString &why)
bool m_specsched
Definition: scheduler.h:253
QMap< uint, RecList > m_recordidlistmap
Definition: scheduler.h:247
static int Reschedule(const MythUtilCommandLineParser &)
QDateTime m_livetvTime
Definition: scheduler.h:279
OpenEndType m_openEnd
Definition: scheduler.h:283
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
MythDeque< QStringList > m_reschedQueue
Definition: scheduler.h:238
QMap< int, EncoderLink * > tvList
vector< RecList * > m_conflictlists
Definition: scheduler.h:246
QMutex m_schedLock
Definition: scheduler.h:239
IsSameCacheType m_cache_is_same_program
Definition: scheduler.h:288
RecList m_worklist
Definition: scheduler.h:243
int GetError(void) const
Definition: scheduler.h:118