MythTV  master
tv_rec.h
Go to the documentation of this file.
1 #ifndef TVREC_H
2 #define TVREC_H
3 
4 // Qt headers
5 #include <QWaitCondition>
6 #include <QStringList>
7 #include <QDateTime>
8 #include <QRunnable>
9 #include <QString>
10 #include <QMap>
11 #include <QMutex> // for QMutex
12 #include <QReadWriteLock>
13 #include <QHash> // for QHash
14 
15 // C++ headers
16 #include <vector> // for vector
17 
18 // MythTV headers
19 #include "mythtimer.h"
20 #include "mthread.h"
21 #include "inputinfo.h"
22 #include "mythdeque.h"
23 
24 #include "recordinginfo.h"
25 #include "tv.h"
26 #include "signalmonitorlistener.h"
27 #include "mythtvexp.h" // for MTV_PUBLIC
28 #include "programtypes.h" // for RecStatus, RecStatus::Type, etc
29 #include "videoouttypes.h" // for PictureAttribute
30 
31 #include "mythconfig.h"
32 
33 // locking order
34 // setChannelLock -> stateChangeLock -> triggerEventLoopLock
35 // -> pendingRecLock
36 
37 class RingBuffer;
38 class EITScanner;
39 class RecordingProfile;
40 class LiveTVChain;
41 
42 class RecorderBase;
43 class DTVRecorder;
44 class DVBRecorder;
45 class HDHRRecorder;
46 class ASIRecorder;
47 class CetonRecorder;
48 
49 class SignalMonitor;
50 class DTVSignalMonitor;
51 
52 class ChannelBase;
53 class DTVChannel;
54 class DVBChannel;
55 class FirewireChannel;
56 class V4LChannel;
57 class HDHRChannel;
58 class CetonChannel;
59 
60 class MPEGStreamData;
61 class ProgramMapTable;
62 class RecordingQuality;
63 
65 {
66  public:
68  videodev(""), vbidev(""),
69  audiodev(""),
70  inputtype("V4L"),
72  signal_timeout(1000), channel_timeout(3000),
74 
75  QString videodev;
76  QString vbidev;
77  QString audiodev;
78  QString inputtype;
84 };
85 
87 {
88  public:
93 };
94 
96 {
97  public:
98  FireWireDBOptions() : speed(-1), connection(-1), model("") {;}
99 
100  int speed;
102  QString model;
103 };
104 
106 {
107  public:
108  explicit TuningRequest(uint f) :
109  flags(f), program(nullptr),
110  majorChan(0), minorChan(0), progNum(-1) {;}
112  flags(f), program(p),
113  majorChan(0), minorChan(0), progNum(-1) {;}
114  TuningRequest(uint f, const QString& ch, const QString& in = QString()) :
115  flags(f), program(nullptr), channel(ch),
116  input(in), majorChan(0), minorChan(0), progNum(-1) {;}
117 
118  QString toString(void) const;
119 
120  bool IsOnSameMultiplex(void) const { return minorChan || (progNum >= 0); }
121 
122  public:
125  QString channel;
126  QString input;
129  int progNum;
130 };
133 
135 {
136  public:
138  info(nullptr), hasLaterShowing(false), canceled(false),
139  ask(false), doNotAsk(false) { }
141  QDateTime recordingStart;
143  bool canceled;
144  bool ask;
145  bool doNotAsk;
146  vector<uint> possibleConflicts;
147 };
148 typedef QMap<uint,PendingInfo> PendingMap;
149 
150 class MTV_PUBLIC TVRec : public SignalMonitorListener, public QRunnable
151 {
152  Q_DECLARE_TR_FUNCTIONS(TVRec)
153 
154  friend class TuningRequest;
155  friend class TVRecRecordThread;
156 
157  public:
158  explicit TVRec(int _inputid);
159  ~TVRec(void);
160 
161  bool Init(void);
162 
163  void RecordPending(const ProgramInfo *rcinfo, int secsleft, bool hasLater);
164  RecStatus::Type StartRecording(ProgramInfo *pginfo);
165  RecStatus::Type GetRecordingStatus(void) const;
166 
167  void StopRecording(bool killFile = false);
169  void FinishRecording(void) { SetFlags(kFlagFinishRecording,
170  __FILE__, __LINE__); }
172  void FrontendReady(void) { SetFlags(kFlagFrontendReady,
173  __FILE__, __LINE__); }
174  void CancelNextRecording(bool cancel);
175  ProgramInfo *GetRecording(void);
176 
178  bool IsRunning(void) const { return HasFlags(kFlagRunMainLoop); }
180  void Stop(void) { ClearFlags(kFlagRunMainLoop, __FILE__, __LINE__); }
181 
182  TVState GetState(void) const;
184  bool IsPlaying(void) { return StateIsPlaying(m_internalState); }
187  bool IsRecording(void) { return StateIsRecording(m_internalState); }
188 
189  bool SetVideoFiltersForChannel(uint sourceid, const QString &channum);
190 
191  bool IsBusy(InputInfo *busy_input = nullptr, int time_buffer = 5) const;
192  bool IsReallyRecording(void);
193 
194  float GetFramerate(void);
195  long long GetFramesWritten(void);
196  long long GetFilePosition(void);
197  long long GetMaxBitrate(void) const;
198  int64_t GetKeyframePosition(uint64_t desired) const;
199  bool GetKeyframePositions(int64_t start, int64_t end, frm_pos_map_t&) const;
200  bool GetKeyframeDurations(int64_t start, int64_t end, frm_pos_map_t&) const;
201  void SpawnLiveTV(LiveTVChain *newchain, bool pip, QString startchan);
202  QString GetChainID(void);
203  void StopLiveTV(void);
204  void PauseRecorder(void);
205  void ToggleChannelFavorite(const QString&);
206 
207  void SetLiveRecording(int recording);
208 
209  QString GetInput(void) const;
210  uint GetSourceID(void) const;
211  QString SetInput(QString input);
212 
215  { SetChannel(QString("NextChannel %1").arg((int)dir)); }
216  void SetChannel(const QString& name, uint requestType = kFlagDetect);
217  bool QueueEITChannelChange(const QString &name);
218 
219  int SetSignalMonitoringRate(int rate, int notifyFrontend = 1);
220  int GetPictureAttribute(PictureAttribute attr);
221  int ChangePictureAttribute(PictureAdjustType type, PictureAttribute attr,
222  bool direction);
223  bool CheckChannel(const QString& name) const;
224  bool ShouldSwitchToAnotherInput(const QString& chanid);
225  bool CheckChannelPrefix(const QString&,uint&,bool&,QString&);
226  void GetNextProgram(BrowseDirection direction,
227  QString &title, QString &subtitle,
228  QString &desc, QString &category,
229  QString &starttime, QString &endtime,
230  QString &callsign, QString &iconpath,
231  QString &channum, uint &chanid,
232  QString &seriesid, QString &programid);
233  bool GetChannelInfo(uint &chanid, uint &sourceid,
234  QString &callsign, QString &channum,
235  QString &channame, QString &xmltvid) const;
236  bool SetChannelInfo(uint chanid, uint sourceid, const QString& oldchannum,
237  const QString& callsign, const QString& channum,
238  const QString& channame, const QString& xmltvid);
239 
241  uint GetInputId(void) { return m_inputid; }
242  uint GetParentId(void) { return m_parentid; }
243  uint GetMajorId(void) { return m_parentid ? m_parentid : m_inputid; }
245  bool IsErrored(void) const { return HasFlags(kFlagErrored); }
246 
247  void RingBufferChanged(RingBuffer*, RecordingInfo*, RecordingQuality*);
248  void RecorderPaused(void);
249 
250  void SetNextLiveTVDir(QString dir);
251 
252  uint GetFlags(void) const { return m_stateFlags; }
253 
254  static TVRec *GetTVRec(uint inputid);
255 
256  void AllGood(void) override { WakeEventLoop(); } // SignalMonitorListener
257  void StatusChannelTuned(const SignalMonitorValue&) override { } // SignalMonitorListener
258  void StatusSignalLock(const SignalMonitorValue&) override { } // SignalMonitorListener
259  void StatusSignalStrength(const SignalMonitorValue&) override { } // SignalMonitorListener
260 
261  protected:
262  void run(void) override; // QRunnable
263  bool WaitForEventThreadSleep(bool wake = true, ulong time = ULONG_MAX);
264 
265  private:
266  void SetRingBuffer(RingBuffer *);
267  void SetPseudoLiveTVRecording(RecordingInfo*);
268  void TeardownAll(void);
269  void WakeEventLoop(void);
270 
271  static bool GetDevices(uint inputid,
272  uint &parentid,
273  GeneralDBOptions &gen_opts,
274  DVBDBOptions &dvb_opts,
275  FireWireDBOptions &firewire_opts);
276 
277  static QString GetStartChannel(uint inputid);
278 
279  void TeardownRecorder(uint request_flags);
280  DTVRecorder *GetDTVRecorder(void);
281 
282  bool CreateChannel(const QString &startchannel,
283  bool enter_power_save_mode);
284  void CloseChannel(void);
285  DTVChannel *GetDTVChannel(void);
286  V4LChannel *GetV4LChannel(void);
287 
288  bool SetupSignalMonitor(
289  bool tablemon, bool EITscan, bool notify);
290  bool SetupDTVSignalMonitor(bool EITscan);
291  void TeardownSignalMonitor(void);
292  DTVSignalMonitor *GetDTVSignalMonitor(void);
293 
294  bool HasFlags(uint f) const { return (m_stateFlags & f) == f; }
295  void SetFlags(uint f, const QString & file, int line);
296  void ClearFlags(uint f, const QString & file, int line);
297  static QString FlagToString(uint);
298 
299  void HandleTuning(void);
300  void TuningShutdowns(const TuningRequest&);
301  void TuningFrequency(const TuningRequest&);
302  MPEGStreamData *TuningSignalCheck(void);
303 
304  void TuningNewRecorder(MPEGStreamData*);
305  void TuningRestartRecorder(void);
306  QString TuningGetChanNum(const TuningRequest&, QString &input) const;
307  bool TuningOnSameMultiplex(TuningRequest &request);
308 
309  void HandleStateChange(void);
310  void ChangeState(TVState nextState);
311  bool StateIsRecording(TVState state);
312  bool StateIsPlaying(TVState state);
313  TVState RemovePlaying(TVState state);
314  TVState RemoveRecording(TVState state);
315 
316  void HandlePendingRecordings(void);
317 
318  bool WaitForNextLiveTVDir(void);
319  bool GetProgramRingBufferForLiveTV(RecordingInfo **pginfo, RingBuffer **rb,
320  const QString &channum);
321  bool CreateLiveTVRingBuffer(const QString & channum);
322  bool SwitchLiveTVRingBuffer(const QString & channum,
323  bool discont, bool set_rec);
324 
325  RecordingInfo *SwitchRecordingRingBuffer(const RecordingInfo &rcinfo);
326 
327  void StartedRecording(RecordingInfo*);
328  void FinishedRecording(RecordingInfo*, RecordingQuality*);
329  QDateTime GetRecordEndTime(const ProgramInfo*) const;
330  void CheckForRecGroupChange(void);
331  void NotifySchedulerOfRecording(RecordingInfo*);
332  typedef enum { kAutoRunProfile, kAutoRunNone, } AutoRunInitType;
333  void InitAutoRunJobs(RecordingInfo*, AutoRunInitType,
334  RecordingProfile *, int line);
335 
336  void SetRecordingStatus(
337  RecStatus::Type new_status, int line, bool have_lock = false);
338 
339  QString LoadProfile(void*, RecordingInfo*,
341 
342  // Various components TVRec coordinates
343  RecorderBase *m_recorder {nullptr};
344  ChannelBase *m_channel {nullptr};
345  SignalMonitor *m_signalMonitor {nullptr};
346  EITScanner *m_scanner {nullptr};
347 
349  bool m_signalEventCmdSent {false};
350 
353  uint m_signalMonitorCheckCnt {0};
354  bool m_reachedRecordingDeadline {false};
355  QDateTime m_preFailDeadline;
356  bool m_reachedPreFail {false};
357 
358  // Various threads
360  MThread *m_eventThread {nullptr};
362  MThread *m_recorderThread {nullptr};
363 
364  // Configuration variables from database
365  bool m_transcodeFirst {false};
366  bool m_earlyCommFlag {false};
367  bool m_runJobOnHostOnly {false};
368  int m_eitCrawlIdleStart {60};
369  int m_eitTransportTimeout {5*60};
370  int m_audioSampleRateDB {0};
371  int m_overRecordSecNrml {0};
372  int m_overRecordSecCat {0};
374 
375  // Configuration variables from setup routines
377  uint m_parentid {0};
378  bool m_ispip {false};
379 
380  // Configuration variables from database, based on inputid
384 
386 
387  // State variables
388  mutable QMutex m_setChannelLock;
389  mutable QMutex m_stateChangeLock {QMutex::Recursive};
390  mutable QMutex m_pendingRecLock {QMutex::Recursive};
391  TVState m_internalState {kState_None};
392  TVState m_desiredNextState {kState_None};
393  bool m_changeState {false};
394  bool m_pauseNotify {true};
395  uint m_stateFlags {0};
397  TuningRequest m_lastTuningRequest {0};
399  mutable QMutex m_triggerEventLoopLock {QMutex::NonRecursive};
400  QWaitCondition m_triggerEventLoopWait;
401  bool m_triggerEventLoopSignal {false};
402  mutable QMutex m_triggerEventSleepLock {QMutex::NonRecursive};
403  QWaitCondition m_triggerEventSleepWait;
404  bool m_triggerEventSleepSignal {false};
405  volatile bool m_switchingBuffer {false};
407 
408  // Current recording info
409  RecordingInfo *m_curRecording {nullptr};
410  QDateTime m_recordEndTime;
411  // RecordingInfo::MakeUniqueKey()->autoRun
412  QHash<QString,int> m_autoRunJobs;
413  int m_overrecordseconds {0};
414 
415  // Pending recording info
417 
418  // Pseudo LiveTV recording
419  RecordingInfo *m_pseudoLiveTVRecording {nullptr};
422  QWaitCondition m_triggerLiveTVDir;
424 
425  // LiveTV file chain
426  LiveTVChain *m_tvChain {nullptr};
427 
428  // RingBuffer info
429  RingBuffer *m_ringBuffer {nullptr};
430  QString m_rbFileExt {"ts"};
431 
432  public:
433  static QReadWriteLock s_inputsLock;
434  static QMap<uint,TVRec*> s_inputs;
435 
436  public:
438 
439  // General State flags
440  static const uint kFlagFrontendReady = 0x00000001;
441  static const uint kFlagRunMainLoop = 0x00000002;
442  static const uint kFlagExitPlayer = 0x00000004;
443  static const uint kFlagFinishRecording = 0x00000008;
444  static const uint kFlagErrored = 0x00000010;
445  static const uint kFlagCancelNextRecording = 0x00000020;
446 
447  // Tuning flags
449  static const uint kFlagLiveTV = 0x00000100;
451  static const uint kFlagRecording = 0x00000200;
453  static const uint kFlagAntennaAdjust = 0x00000400;
454  static const uint kFlagRec = 0x00000F00;
455 
456  // Non-recording Commands
458  static const uint kFlagEITScan = 0x00001000;
460  static const uint kFlagCloseRec = 0x00002000;
462  static const uint kFlagKillRec = 0x00004000;
463 
464  static const uint kFlagNoRec = 0x0000F000;
465  static const uint kFlagKillRingBuffer = 0x00010000;
466 
467  // Waiting stuff
468  static const uint kFlagWaitingForRecPause = 0x00100000;
469  static const uint kFlagWaitingForSignal = 0x00200000;
470  static const uint kFlagNeedToStartRecorder = 0x00800000;
471  static const uint kFlagPendingActions = 0x00F00000;
472 
473  // Running stuff
474  static const uint kFlagSignalMonitorRunning = 0x01000000;
475  static const uint kFlagEITScannerRunning = 0x04000000;
476 
477  static const uint kFlagDummyRecorderRunning = 0x10000000;
478  static const uint kFlagRecorderRunning = 0x20000000;
479  static const uint kFlagAnyRecRunning = 0x30000000;
480  static const uint kFlagAnyRunning = 0x3F000000;
481 
482  // Tuning state
483  static const uint kFlagRingBufferReady = 0x40000000;
484  static const uint kFlagDetect = 0x80000000;
485 };
486 
487 #endif
uint flags
Definition: tv_rec.h:123
uint dvb_tuning_delay
Definition: tv_rec.h:91
bool IsPlaying(void)
Returns "state == kState_RecordingPreRecorded".
Definition: tv_rec.h:184
QWaitCondition m_triggerLiveTVDir
Definition: tv_rec.h:422
int audiosamplerate
Definition: tv_rec.h:79
ProgramInfo * info
Definition: tv_rec.h:140
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
This is a specialization of RecorderBase used to handle MPEG-2, MPEG-4, MPEG-4 AVC,...
Definition: dtvrecorder.h:28
uint GetInputId(void)
Returns the inputid.
Definition: tv_rec.h:241
ChannelChangeDirection
ChannelChangeDirection is an enumeration of possible channel changing directions.
Definition: tv.h:28
VERBOSE_PREAMBLE Most true
Definition: verbosedefs.h:91
QString model
Definition: tv_rec.h:102
MythDeque< TuningRequest > TuningQueue
Definition: tv_rec.h:131
This is a specialization of DTVRecorder used to handle streams from DVB drivers.
Definition: dvbrecorder.h:21
QMap< uint, PendingInfo > PendingMap
Definition: tv_rec.h:148
QString m_recProfileName
Definition: tv_rec.h:385
uint minorChan
Definition: tv_rec.h:128
TuningRequest(uint f)
Definition: tv_rec.h:108
void AllGood(void) override
Signal to be sent when you have a lock on all values.
Definition: tv_rec.h:256
uint channel_timeout
Definition: tv_rec.h:82
QString vbidev
Definition: tv_rec.h:76
GeneralDBOptions()
Definition: tv_rec.h:67
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:34
bool dvb_eitscan
Definition: tv_rec.h:92
QString channel
Definition: tv_rec.h:125
uint GetMajorId(void)
Definition: tv_rec.h:243
QString m_overRecordCategory
Definition: tv_rec.h:373
unsigned int uint
Definition: compat.h:140
Provides interface to the tuning hardware when using DVB drivers.
Definition: dvbchannel.h:29
uint signal_timeout
Definition: tv_rec.h:81
void ChangeChannel(ChannelChangeDirection dir)
Changes to a channel in the 'dir' channel change direction.
Definition: tv_rec.h:214
void StatusChannelTuned(const SignalMonitorValue &) override
Signal to be sent with change change status.
Definition: tv_rec.h:257
void FrontendReady(void)
Tells TVRec that the frontend's TV class is ready for messages.
Definition: tv_rec.h:172
QDateTime m_recordEndTime
Definition: tv_rec.h:410
bool IsOnSameMultiplex(void) const
Definition: tv_rec.h:120
TVState
TVState is an enumeration of the states used by TV and TVRec.
Definition: tv.h:50
PendingMap m_pendingRecordings
Definition: tv_rec.h:416
void StatusSignalLock(const SignalMonitorValue &) override
Signal to be sent as true when it is safe to begin or continue recording, and false if it may not be ...
Definition: tv_rec.h:258
bool dvb_on_demand
Definition: tv_rec.h:90
uint GetParentId(void)
Definition: tv_rec.h:242
DVBDBOptions()
Definition: tv_rec.h:89
bool wait_for_seqstart
Definition: tv_rec.h:83
uint m_inputid
Definition: tv_rec.h:376
RecordingInfo * program
Definition: tv_rec.h:124
enum BrowseDirections BrowseDirection
Used to request ProgramInfo for channel browsing.
Holds information on recordings and videos.
Definition: programinfo.h:66
QDateTime m_eitScanStartTime
Definition: tv_rec.h:398
QMutex m_setChannelLock
Definition: tv_rec.h:388
static QReadWriteLock s_inputsLock
Definition: tv_rec.h:433
int progNum
Definition: tv_rec.h:129
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
bool skip_btaudio
Definition: tv_rec.h:80
QString m_nextLiveTVDir
Definition: tv_rec.h:420
QHash< QString, int > m_autoRunJobs
Definition: tv_rec.h:412
QWaitCondition m_triggerEventSleepWait
Definition: tv_rec.h:403
Class providing a generic interface to digital tuning hardware.
Definition: dtvchannel.h:34
#define MTV_PUBLIC
Definition: mythtvexp.h:15
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:150
bool hasLaterShowing
Definition: tv_rec.h:142
TuningRequest myth_deque_init(const TuningRequest *)
Definition: tv_rec.h:132
#define GetState(a, b)
FirewireChannel Copyright (c) 2005 by Jim Westfall and Dave Abrahams Distributed as part of MythTV un...
void Stop(void)
Tells TVRec to stop event loop.
Definition: tv_rec.h:180
bool canceled
Definition: tv_rec.h:143
QMutex m_nextLiveTVDirLock
Definition: tv_rec.h:421
bool ask
Definition: tv_rec.h:144
QDateTime m_signalEventCmdTimeout
Definition: tv_rec.h:348
Acts as glue between ChannelBase, EITSource, and EITHelper.
Definition: eitscanner.h:30
static const uint kSignalMonitoringRate
How many milliseconds the signal monitor should wait between checks.
Definition: tv_rec.h:437
PictureAttribute
Definition: videoouttypes.h:89
const char * name
Definition: ParseText.cpp:328
Implements tuning for TV cards using the V4L driver API, both versions 1 and 2.
Definition: v4lchannel.h:32
QString m_liveTVStartChannel
Definition: tv_rec.h:423
This is the abstract base class for supporting recorder hardware.
Definition: recorderbase.h:66
void StatusSignalStrength(const SignalMonitorValue &) override
Signal to be sent with an actual signal value.
Definition: tv_rec.h:259
TuningRequest(uint f, const QString &ch, const QString &in=QString())
Definition: tv_rec.h:114
QDateTime m_signalMonitorDeadline
Definition: tv_rec.h:352
This is a specialization of DTVRecorder used to handle streams from ASI drivers.
Definition: asirecorder.h:55
None State, this is the initial state in both TV and TVRec, it indicates that we are ready to change ...
Definition: tv.h:58
This class is intended to detect the presence of needed tables.
GeneralDBOptions m_genOpt
Definition: tv_rec.h:381
PendingInfo()
Definition: tv_rec.h:137
bool IsRunning(void) const
Returns true if event loop has not been told to shut down.
Definition: tv_rec.h:178
Signal monitoring base class.
Definition: signalmonitor.h:32
QString audiodev
Definition: tv_rec.h:77
uint GetFlags(void) const
Definition: tv_rec.h:252
Abstract class providing a generic interface to tuning hardware.
Definition: channelbase.h:31
PictureAdjustType
Definition: tv.h:120
FireWireDBOptions m_fwOpt
Definition: tv_rec.h:383
bool HasFlags(uint f) const
Definition: tv_rec.h:294
QString inputtype
Definition: tv_rec.h:78
QDateTime recordingStart
Definition: tv_rec.h:141
QWaitCondition m_triggerEventLoopWait
Definition: tv_rec.h:400
bool IsErrored(void) const
Returns true is "errored" is true, false otherwise.
Definition: tv_rec.h:245
vector< uint > possibleConflicts
Definition: tv_rec.h:146
QString input
Definition: tv_rec.h:126
bool IsRecording(void)
Returns "state == kState_RecordingRecordedOnly".
Definition: tv_rec.h:187
Implements a file/stream reader/writer.
QMap< long long, long long > frm_pos_map_t
Frame # -> File offset map.
Definition: programtypes.h:46
DVBDBOptions m_dvbOpt
Definition: tv_rec.h:382
QString toString(void) const
Definition: tv_rec.cpp:4821
A PMT table maps a program described in the ProgramAssociationTable to various PID's which describe t...
Definition: mpegtables.h:656
Keeps track of recordings in a current LiveTV instance.
Definition: livetvchain.h:31
uint majorChan
Definition: tv_rec.h:127
static QMap< uint, TVRec * > s_inputs
Definition: tv_rec.h:434
QString videodev
Definition: tv_rec.h:75
QDateTime m_preFailDeadline
Definition: tv_rec.h:355
Encapsulates data about MPEG stream and emits events for each table.
TuningQueue m_tuningRequests
Definition: tv_rec.h:396
TuningRequest(uint f, RecordingInfo *p)
Definition: tv_rec.h:111
bool doNotAsk
Definition: tv_rec.h:145
QDateTime m_startRecordingDeadline
Definition: tv_rec.h:351