MythTV  master
signalmonitor.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Copyright (c) 2005, Daniel Thor Kristjansson
3 
4 #ifndef SIGNALMONITOR_H
5 #define SIGNALMONITOR_H
6 
7 // C++ headers
8 #include <vector>
9 #include <algorithm>
10 
11 // Qt headers
12 #include <QWaitCondition>
13 #include <QMutex>
14 #include <QCoreApplication>
15 #if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
16 #include <QRecursiveMutex>
17 #endif
18 
19 // MythTV headers
20 #include "libmythbase/mthread.h"
21 #include "libmythbase/mythtimer.h"
22 
23 #include "cardutil.h"
24 #include "channelbase.h"
25 #include "signalmonitorlistener.h"
26 #include "signalmonitorvalue.h"
27 
28 #define DBG_SM(FUNC, MSG) LOG(VB_CHANNEL, LOG_DEBUG, \
29  QString("SM(%1)::%2: %3") .arg(channel->GetDevice()).arg(FUNC).arg(<(MSG)));
30 
31 inline QString sm_flags_to_string(uint64_t flags);
32 
33 class TVRec;
34 
35 class SignalMonitor : protected MThread
36 {
38 
39  public:
41  static inline bool IsRequired(const QString &cardtype);
42  static inline bool IsSupported(const QString &cardtype);
43  static SignalMonitor *Init(const QString& cardtype, int db_cardnum,
44  ChannelBase *channel,
45  bool release_stream);
46  ~SignalMonitor() override;
47 
48  // Prevent implicit conversion of wrongly ordered arguments
49  SignalMonitor(int, ChannelBase *, uint64_t, bool) = delete;
50 
51  // // // // // // // // // // // // // // // // // // // // // // // //
52  // Control // // // // // // // // // // // // // // // // // // // //
53 
54  virtual void Start();
55  virtual void Stop();
56 
57  // // // // // // // // // // // // // // // // // // // // // // // //
58  // Flags // // // // // // // // // // // // // // // // // // // // //
59 
60  virtual void AddFlags(uint64_t _flags);
61  virtual void RemoveFlags(uint64_t _flags);
62  bool HasFlags(uint64_t _flags) const;
63  bool HasAnyFlag(uint64_t _flags) const;
64  uint64_t GetFlags(void) const { return m_flags; }
65  virtual bool HasExtraSlowTuning(void) const { return false; }
66 
67  // // // // // // // // // // // // // // // // // // // // // // // //
68  // Gets // // // // // // // // // // // // // // // // // // // // //
69 
72  bool GetNotifyFrontend() const { return m_notifyFrontend; }
74  std::chrono::milliseconds GetUpdateRate() const { return m_updateRate; }
75  virtual QStringList GetStatusList(void) const;
77 
80  bool HasSignalLock(void) const
81  {
82  QMutexLocker locker(&m_statusLock);
84  }
85 
86  virtual bool IsAllGood(void) const { return HasSignalLock(); }
87  bool IsErrored(void) const { return !m_error.isEmpty(); }
88 
89  // // // // // // // // // // // // // // // // // // // // // // // //
90  // Sets // // // // // // // // // // // // // // // // // // // // //
91 
97  void SetNotifyFrontend(bool notify) { m_notifyFrontend = notify; }
98 
105  void SetMonitoring(TVRec * parent, bool EITscan, bool monitor)
106  { m_pParent = parent; m_eitScan = EITscan, m_tablemon = monitor; }
107 
114  void SetUpdateRate(std::chrono::milliseconds msec)
115  { m_updateRate = std::max(msec, m_minimumUpdateRate); }
116 
117  // // // // // // // // // // // // // // // // // // // // // // // //
118  // Listeners // // // // // // // // // // // // // // // // // // //
119  void AddListener(SignalMonitorListener *listener);
120  void RemoveListener(SignalMonitorListener *listener);
122  const SignalMonitorValue &val);
123  void SendMessageAllGood(void);
124  virtual void EmitStatus(void);
125 
126  protected:
127  SignalMonitor(int _inputid, ChannelBase *_channel,
128  bool _release_stream, uint64_t wait_for_mask);
129 
130  void run(void) override; // MThread
131 
133  virtual void UpdateValues(void);
134 
135  public:
138  static const uint64_t kDTVSigMon_PATSeen = 0x0000000001ULL;
141  static const uint64_t kDTVSigMon_PMTSeen = 0x0000000002ULL;
144  static const uint64_t kDTVSigMon_MGTSeen = 0x0000000004ULL;
147  static const uint64_t kDTVSigMon_VCTSeen = 0x0000000008ULL;
149  static const uint64_t kDTVSigMon_TVCTSeen = 0x0000000010ULL;
151  static const uint64_t kDTVSigMon_CVCTSeen = 0x0000000020ULL;
154  static const uint64_t kDTVSigMon_NITSeen = 0x0000000040ULL;
157  static const uint64_t kDTVSigMon_SDTSeen = 0x0000000080ULL;
159  static const uint64_t kFWSigMon_PowerSeen = 0x0000000100ULL;
161  static const uint64_t kDTVSigMon_CryptSeen = 0x0000000200ULL;
162 
164  static const uint64_t kDTVSigMon_PATMatch = 0x0000001000ULL;
166  static const uint64_t kDTVSigMon_PMTMatch = 0x0000002000ULL;
168  static const uint64_t kDTVSigMon_MGTMatch = 0x0000004000ULL;
170  static const uint64_t kDTVSigMon_VCTMatch = 0x0000008000ULL;
172  static const uint64_t kDTVSigMon_TVCTMatch = 0x0000010000ULL;
174  static const uint64_t kDTVSigMon_CVCTMatch = 0x0000020000ULL;
176  static const uint64_t kDTVSigMon_NITMatch = 0x0000040000ULL;
178  static const uint64_t kDTVSigMon_SDTMatch = 0x0000080000ULL;
180  static const uint64_t kFWSigMon_PowerMatch = 0x0000100000ULL;
182  static const uint64_t kDTVSigMon_CryptMatch = 0x0000200000ULL;
183 
184  static const uint64_t kDTVSigMon_WaitForPAT = 0x0001000000ULL;
185  static const uint64_t kDTVSigMon_WaitForPMT = 0x0002000000ULL;
186  static const uint64_t kDTVSigMon_WaitForMGT = 0x0004000000ULL;
187  static const uint64_t kDTVSigMon_WaitForVCT = 0x0008000000ULL;
188  static const uint64_t kDTVSigMon_WaitForNIT = 0x0010000000ULL;
189  static const uint64_t kDTVSigMon_WaitForSDT = 0x0020000000ULL;
190  static const uint64_t kSigMon_WaitForSig = 0x0040000000ULL;
191  static const uint64_t kFWSigMon_WaitForPower= 0x0080000000ULL;
192  static const uint64_t kDTVSigMon_WaitForCrypt=0x0100000000ULL;
193 
194  static const uint64_t kDTVSigMon_WaitForAll = 0x01FF000000ULL;
195 
197  static const uint64_t kDVBSigMon_WaitForSNR = 0x1000000000ULL;
199  static const uint64_t kDVBSigMon_WaitForBER = 0x2000000000ULL;
201  static const uint64_t kDVBSigMon_WaitForUB = 0x4000000000ULL;
203  static const uint64_t kDVBSigMon_WaitForPos = 0x8000000000ULL;
204 
205  protected:
206  ChannelBase *m_channel {nullptr};
207  TVRec *m_pParent {nullptr};
209  volatile uint64_t m_flags;
211  std::chrono::milliseconds m_updateRate {25ms};
212  std::chrono::milliseconds m_minimumUpdateRate {5ms};
213  bool m_updateDone {false};
214  bool m_notifyFrontend {true};
215  bool m_tablemon {false};
216  bool m_eitScan {false};
217 
218  // not to be confused with StreamHandler::m_bError.
219  QString m_error;
220 
224 
225  std::vector<SignalMonitorListener*> m_listeners;
226 
228  QWaitCondition m_startStopWait; // protected by startStopLock
229  volatile bool m_running {false}; // protected by startStopLock
230  volatile bool m_exit {false}; // protected by startStopLock
231 
232 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
233  mutable QMutex m_statusLock {QMutex::Recursive};
234 #else
235  mutable QRecursiveMutex m_statusLock;
236 #endif
237  mutable QMutex m_listenerLock;
238 };
239 
240 inline QString sm_flags_to_string(uint64_t flags)
241 {
242  QString str("Seen(");
244  str += "PAT,";
246  str += "PMT,";
248  str += "MGT,";
250  str += "VCT,";
252  str += "TVCT,";
254  str += "CVCT,";
256  str += "NIT,";
258  str += "SDT,";
260  str += "STB,";
262  str += "Crypt,";
263 
264  str += ") Match(";
266  str += "PAT,";
268  str += "PMT,";
270  str += "MGT,";
272  str += "VCT,";
274  str += "TVCT,";
276  str += "CVCT,";
278  str += "NIT,";
280  str += "SDT,";
282  str += "STB,";
284  str += "Crypt,";
285 
286  str += ") Wait(";
288  str += "PAT,";
290  str += "PMT,";
292  str += "MGT,";
294  str += "VCT,";
296  str += "NIT,";
298  str += "SDT,";
300  str += "Sig,";
302  str += "STB,";
304  str += "Crypt,";
305 
307  str += "SNR,";
309  str += "BER,";
311  str += "UB,";
313  str += "Pos,";
314 
315  str += ")";
316  return str;
317 }
318 
319 inline bool SignalMonitor::IsRequired(const QString &cardtype)
320 {
321  return (cardtype != "IMPORT" && cardtype != "DEMO");
322 }
323 
324 inline bool SignalMonitor::IsSupported(const QString &cardtype)
325 {
326  return IsRequired(cardtype);
327 }
328 
329 
330 #endif // SIGNALMONITOR_H
SignalMonitor::m_flags
volatile uint64_t m_flags
Definition: signalmonitor.h:209
SignalMonitor::kDTVSigMon_WaitForAll
static const uint64_t kDTVSigMon_WaitForAll
Definition: signalmonitor.h:194
SignalMonitor::m_minimumUpdateRate
std::chrono::milliseconds m_minimumUpdateRate
Definition: signalmonitor.h:212
SignalMonitor::kFWSigMon_WaitForPower
static const uint64_t kFWSigMon_WaitForPower
Definition: signalmonitor.h:191
SignalMonitor::IsAllGood
virtual bool IsAllGood(void) const
Definition: signalmonitor.h:86
SignalMonitor::kDTVSigMon_NITMatch
static const uint64_t kDTVSigMon_NITMatch
We've seen an NIT matching our requirements.
Definition: signalmonitor.h:176
SignalMonitor::IsSupported
static bool IsSupported(const QString &cardtype)
Definition: signalmonitor.h:324
SignalMonitor::kDTVSigMon_VCTMatch
static const uint64_t kDTVSigMon_VCTMatch
We've seen a VCT matching our requirements.
Definition: signalmonitor.h:170
SignalMonitor::m_channel
ChannelBase * m_channel
Definition: signalmonitor.h:206
SignalMonitorValue::GetNormalizedValue
int GetNormalizedValue(int newmin, int newmax) const
Returns the value normalized to the [newmin, newmax] range.
Definition: signalmonitorvalue.h:63
SignalMonitor::kDTVSigMon_WaitForMGT
static const uint64_t kDTVSigMon_WaitForMGT
Definition: signalmonitor.h:186
SignalMonitor::kDVBSigMon_WaitForSNR
static const uint64_t kDVBSigMon_WaitForSNR
Wait for the Signal to Noise Ratio to rise above a threshold.
Definition: signalmonitor.h:197
SignalMonitor::SendMessage
void SendMessage(SignalMonitorMessageType type, const SignalMonitorValue &val)
Definition: signalmonitor.cpp:413
SignalMonitor::m_exit
volatile bool m_exit
Definition: signalmonitor.h:230
SignalMonitor::kDTVSigMon_CVCTSeen
static const uint64_t kDTVSigMon_CVCTSeen
We've seen a CVCT, the cable version of the VCT.
Definition: signalmonitor.h:151
SignalMonitor::GetSignalStrength
int GetSignalStrength(void)
Definition: signalmonitor.h:76
signalmonitorvalue.h
SignalMonitor::GetUpdateRate
std::chrono::milliseconds GetUpdateRate() const
Returns milliseconds between signal monitoring events.
Definition: signalmonitor.h:74
SignalMonitor::kDTVSigMon_SDTMatch
static const uint64_t kDTVSigMon_SDTMatch
We've seen an SDT matching our requirements.
Definition: signalmonitor.h:178
channelbase.h
SignalMonitor::m_running
volatile bool m_running
Definition: signalmonitor.h:229
SignalMonitor::kDVBSigMon_WaitForBER
static const uint64_t kDVBSigMon_WaitForBER
Wait for the Bit Error Rate to fall below a threshold.
Definition: signalmonitor.h:199
SignalMonitorListener
Definition: signalmonitorlistener.h:21
SignalMonitor::kDTVSigMon_PMTMatch
static const uint64_t kDTVSigMon_PMTMatch
We've seen a PMT matching our requirements.
Definition: signalmonitor.h:166
SignalMonitor::HasAnyFlag
bool HasAnyFlag(uint64_t _flags) const
Definition: signalmonitor.cpp:283
SignalMonitor::AddFlags
virtual void AddFlags(uint64_t _flags)
Definition: signalmonitor.cpp:266
SignalMonitor::kSigMon_WaitForSig
static const uint64_t kSigMon_WaitForSig
Definition: signalmonitor.h:190
SignalMonitor::m_error
QString m_error
Definition: signalmonitor.h:219
SignalMonitor::m_startStopLock
QMutex m_startStopLock
Definition: signalmonitor.h:227
SignalMonitor::m_statusLock
QRecursiveMutex m_statusLock
Definition: signalmonitor.h:235
SignalMonitor::m_scriptStatus
SignalMonitorValue m_scriptStatus
Definition: signalmonitor.h:223
SignalMonitor::IsRequired
static bool IsRequired(const QString &cardtype)
Returns true iff the card type supports signal monitoring.
Definition: signalmonitor.h:319
SignalMonitor::kDVBSigMon_WaitForUB
static const uint64_t kDVBSigMon_WaitForUB
Wait for uncorrected FEC blocks to fall below a threshold.
Definition: signalmonitor.h:201
SignalMonitor::RemoveFlags
virtual void RemoveFlags(uint64_t _flags)
Definition: signalmonitor.cpp:272
SignalMonitor::m_signalStrength
SignalMonitorValue m_signalStrength
Definition: signalmonitor.h:222
SignalMonitor::m_inputid
int m_inputid
Definition: signalmonitor.h:208
SignalMonitor::kDVBSigMon_WaitForPos
static const uint64_t kDVBSigMon_WaitForPos
Wait for rotor to complete turning the antenna.
Definition: signalmonitor.h:203
signalmonitorlistener.h
ChannelBase
Abstract class providing a generic interface to tuning hardware.
Definition: channelbase.h:31
SignalMonitor::m_notifyFrontend
bool m_notifyFrontend
Definition: signalmonitor.h:214
SignalMonitor::kFWSigMon_PowerSeen
static const uint64_t kFWSigMon_PowerSeen
We've seen the FireWire STB power state.
Definition: signalmonitor.h:159
sm_flags_to_string
QString sm_flags_to_string(uint64_t flags)
Definition: signalmonitor.h:240
SignalMonitor::m_releaseStream
bool m_releaseStream
Definition: signalmonitor.h:210
SignalMonitor::m_signalLock
SignalMonitorValue m_signalLock
Definition: signalmonitor.h:221
SignalMonitor::m_updateRate
std::chrono::milliseconds m_updateRate
Definition: signalmonitor.h:211
SignalMonitor::run
void run(void) override
Basic signal monitoring loop.
Definition: signalmonitor.cpp:345
SignalMonitor::SendMessageAllGood
void SendMessageAllGood(void)
Definition: signalmonitor.cpp:468
SignalMonitor::kDTVSigMon_CVCTMatch
static const uint64_t kDTVSigMon_CVCTMatch
We've seen a CVCT matching our requirements.
Definition: signalmonitor.h:174
SignalMonitor::~SignalMonitor
~SignalMonitor() override
Stops monitoring thread.
Definition: signalmonitor.cpp:260
SignalMonitor::kDTVSigMon_WaitForPMT
static const uint64_t kDTVSigMon_WaitForPMT
Definition: signalmonitor.h:185
SignalMonitor::kDTVSigMon_WaitForPAT
static const uint64_t kDTVSigMon_WaitForPAT
Definition: signalmonitor.h:184
SignalMonitor::m_updateDone
bool m_updateDone
Definition: signalmonitor.h:213
SignalMonitor::kDTVSigMon_WaitForNIT
static const uint64_t kDTVSigMon_WaitForNIT
Definition: signalmonitor.h:188
SignalMonitor::EmitStatus
virtual void EmitStatus(void)
Definition: signalmonitor.cpp:475
SignalMonitor::kDTVSigMon_WaitForSDT
static const uint64_t kDTVSigMon_WaitForSDT
Definition: signalmonitor.h:189
SignalMonitor::UpdateValues
virtual void UpdateValues(void)
This should be overridden to actually do signal monitoring.
Definition: signalmonitor.cpp:459
SignalMonitor::kDTVSigMon_SDTSeen
static const uint64_t kDTVSigMon_SDTSeen
We've seen a SDT, which maps DVB Channels to MPEG program numbers, and provides additional data.
Definition: signalmonitor.h:157
SignalMonitor::kFWSigMon_PowerMatch
static const uint64_t kFWSigMon_PowerMatch
We've seen a FireWire STB power state matching our requirements.
Definition: signalmonitor.h:180
SignalMonitor::kDTVSigMon_NITSeen
static const uint64_t kDTVSigMon_NITSeen
We've seen a NIT, which tells us where to find SDT and other DVB tables.
Definition: signalmonitor.h:154
SignalMonitorValue
Definition: signalmonitorvalue.h:13
SignalMonitor::kDTVSigMon_TVCTSeen
static const uint64_t kDTVSigMon_TVCTSeen
We've seen a TVCT, the terrestrial version of the VCT.
Definition: signalmonitor.h:149
SignalMonitor::kDTVSigMon_PATSeen
static const uint64_t kDTVSigMon_PATSeen
We've seen a PAT, which maps MPEG program numbers to pids where we find PMTs.
Definition: signalmonitor.h:138
SignalMonitor::kDTVSigMon_PMTSeen
static const uint64_t kDTVSigMon_PMTSeen
We've seen a PMT, which maps program to audio, video and other stream PIDs.
Definition: signalmonitor.h:141
SignalMonitor::m_startStopWait
QWaitCondition m_startStopWait
Definition: signalmonitor.h:228
SignalMonitor
Signal monitoring base class.
Definition: signalmonitor.h:35
SignalMonitorValue::IsGood
bool IsGood() const
Returns true if the value is equal to the threshold, or on the right side of the threshold (depends o...
Definition: signalmonitorvalue.h:56
SignalMonitor::Init
static SignalMonitor * Init(const QString &cardtype, int db_cardnum, ChannelBase *channel, bool release_stream)
Definition: signalmonitor.cpp:91
SignalMonitor::kDTVSigMon_VCTSeen
static const uint64_t kDTVSigMon_VCTSeen
We've seen a VCT, which maps ATSC Channels to MPEG program numbers, and provides additional data.
Definition: signalmonitor.h:147
SignalMonitor::HasExtraSlowTuning
virtual bool HasExtraSlowTuning(void) const
Definition: signalmonitor.h:65
SignalMonitor::kDTVSigMon_CryptSeen
static const uint64_t kDTVSigMon_CryptSeen
We've seen something indicating whether the data stream is encrypted.
Definition: signalmonitor.h:161
SignalMonitor::SetUpdateRate
void SetUpdateRate(std::chrono::milliseconds msec)
Sets the number of milliseconds between signal monitoring attempts in the signal monitoring thread.
Definition: signalmonitor.h:114
SignalMonitor::kDTVSigMon_MGTMatch
static const uint64_t kDTVSigMon_MGTMatch
We've seen an MGT matching our requirements.
Definition: signalmonitor.h:168
SignalMonitor::GetNotifyFrontend
bool GetNotifyFrontend() const
Returns whether or not a SIGNAL MythEvent is being sent regularly to the frontend.
Definition: signalmonitor.h:72
SignalMonitor::HasFlags
bool HasFlags(uint64_t _flags) const
Definition: signalmonitor.cpp:278
SignalMonitor::AddListener
void AddListener(SignalMonitorListener *listener)
Definition: signalmonitor.cpp:388
SignalMonitor::m_eitScan
bool m_eitScan
Definition: signalmonitor.h:216
SignalMonitor::kDTVSigMon_MGTSeen
static const uint64_t kDTVSigMon_MGTSeen
We've seen a MGT, which tells us on which PIDs to find VCT and other ATSC tables.
Definition: signalmonitor.h:144
SignalMonitor::SetMonitoring
void SetMonitoring(TVRec *parent, bool EITscan, bool monitor)
Indicate if table monitoring is needed.
Definition: signalmonitor.h:105
cardutil.h
SignalMonitor::RemoveListener
void RemoveListener(SignalMonitorListener *listener)
Definition: signalmonitor.cpp:399
SignalMonitorMessageType
SignalMonitorMessageType
Definition: signalmonitorlistener.h:10
SignalMonitor::kDTVSigMon_PATMatch
static const uint64_t kDTVSigMon_PATMatch
We've seen a PAT matching our requirements.
Definition: signalmonitor.h:164
TVRec
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:144
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mthread.h
SignalMonitor::kDTVSigMon_TVCTMatch
static const uint64_t kDTVSigMon_TVCTMatch
We've seen a TVCT matching our requirements.
Definition: signalmonitor.h:172
SignalMonitor::Start
virtual void Start()
Start signal monitoring thread.
Definition: signalmonitor.cpp:291
SignalMonitor::m_tablemon
bool m_tablemon
Definition: signalmonitor.h:215
SignalMonitor::SetNotifyFrontend
void SetNotifyFrontend(bool notify)
Enables or disables frontend notification of the current signal value.
Definition: signalmonitor.h:97
mythtimer.h
SignalMonitor::m_listeners
std::vector< SignalMonitorListener * > m_listeners
Definition: signalmonitor.h:225
SignalMonitor::GetStatusList
virtual QStringList GetStatusList(void) const
Returns QStringList containing all signals and their current values.
Definition: signalmonitor.cpp:331
SignalMonitor::IsErrored
bool IsErrored(void) const
Definition: signalmonitor.h:87
SignalMonitor::m_listenerLock
QMutex m_listenerLock
Definition: signalmonitor.h:237
SignalMonitor::GetFlags
uint64_t GetFlags(void) const
Definition: signalmonitor.h:64
SignalMonitor::HasSignalLock
bool HasSignalLock(void) const
Returns true iff scriptStatus.IsGood() and signalLock.IsGood() return true.
Definition: signalmonitor.h:80
SignalMonitor::kDTVSigMon_CryptMatch
static const uint64_t kDTVSigMon_CryptMatch
We've seen unencrypted data in data stream.
Definition: signalmonitor.h:182
SignalMonitor::Stop
virtual void Stop()
Stop signal monitoring thread.
Definition: signalmonitor.cpp:307
SignalMonitor::m_pParent
TVRec * m_pParent
Definition: signalmonitor.h:207
SignalMonitor::Q_DECLARE_TR_FUNCTIONS
Q_DECLARE_TR_FUNCTIONS(SignalMonitor)
SignalMonitor::kDTVSigMon_WaitForCrypt
static const uint64_t kDTVSigMon_WaitForCrypt
Definition: signalmonitor.h:192
SignalMonitor::SignalMonitor
SignalMonitor(int, ChannelBase *, uint64_t, bool)=delete
SignalMonitor::kDTVSigMon_WaitForVCT
static const uint64_t kDTVSigMon_WaitForVCT
Definition: signalmonitor.h:187