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 "signalmonitorlistener.h"
21 #include "signalmonitorvalue.h"
22 #include "channelbase.h"
23 #include "mythtimer.h"
24 #include "cardutil.h"
25 #include "mthread.h"
26 
27 #define DBG_SM(FUNC, MSG) LOG(VB_CHANNEL, LOG_DEBUG, \
28  QString("SM(%1)::%2: %3") .arg(channel->GetDevice()).arg(FUNC).arg(<(MSG)));
29 
30 inline QString sm_flags_to_string(uint64_t flags);
31 
32 class TVRec;
33 
34 class SignalMonitor : protected MThread
35 {
37 
38  public:
40  static inline bool IsRequired(const QString &cardtype);
41  static inline bool IsSupported(const QString &cardtype);
42  static SignalMonitor *Init(const QString& cardtype, int db_cardnum,
43  ChannelBase *channel,
44  bool release_stream);
45  ~SignalMonitor() override;
46 
47  // Prevent implicit conversion of wrongly ordered arguments
48  SignalMonitor(int, ChannelBase *, uint64_t, bool) = delete;
49 
50  // // // // // // // // // // // // // // // // // // // // // // // //
51  // Control // // // // // // // // // // // // // // // // // // // //
52 
53  virtual void Start();
54  virtual void Stop();
55 
56  // // // // // // // // // // // // // // // // // // // // // // // //
57  // Flags // // // // // // // // // // // // // // // // // // // // //
58 
59  virtual void AddFlags(uint64_t _flags);
60  virtual void RemoveFlags(uint64_t _flags);
61  bool HasFlags(uint64_t _flags) const;
62  bool HasAnyFlag(uint64_t _flags) const;
63  uint64_t GetFlags(void) const { return m_flags; }
64  virtual bool HasExtraSlowTuning(void) const { return false; }
65 
66  // // // // // // // // // // // // // // // // // // // // // // // //
67  // Gets // // // // // // // // // // // // // // // // // // // // //
68 
71  bool GetNotifyFrontend() const { return m_notifyFrontend; }
73  std::chrono::milliseconds GetUpdateRate() const { return m_updateRate; }
74  virtual QStringList GetStatusList(void) const;
76 
79  bool HasSignalLock(void) const
80  {
81  QMutexLocker locker(&m_statusLock);
83  }
84 
85  virtual bool IsAllGood(void) const { return HasSignalLock(); }
86  bool IsErrored(void) const { return !m_error.isEmpty(); }
87 
88  // // // // // // // // // // // // // // // // // // // // // // // //
89  // Sets // // // // // // // // // // // // // // // // // // // // //
90 
96  void SetNotifyFrontend(bool notify) { m_notifyFrontend = notify; }
97 
104  void SetMonitoring(TVRec * parent, bool EITscan, bool monitor)
105  { m_pParent = parent; m_eitScan = EITscan, m_tablemon = monitor; }
106 
113  void SetUpdateRate(std::chrono::milliseconds msec)
114  { m_updateRate = std::max(msec, m_minimumUpdateRate); }
115 
116  // // // // // // // // // // // // // // // // // // // // // // // //
117  // Listeners // // // // // // // // // // // // // // // // // // //
118  void AddListener(SignalMonitorListener *listener);
119  void RemoveListener(SignalMonitorListener *listener);
121  const SignalMonitorValue &val);
122  void SendMessageAllGood(void);
123  virtual void EmitStatus(void);
124 
125  protected:
126  SignalMonitor(int _inputid, ChannelBase *_channel,
127  bool _release_stream, uint64_t wait_for_mask);
128 
129  void run(void) override; // MThread
130 
132  virtual void UpdateValues(void);
133 
134  public:
137  static const uint64_t kDTVSigMon_PATSeen = 0x0000000001ULL;
140  static const uint64_t kDTVSigMon_PMTSeen = 0x0000000002ULL;
143  static const uint64_t kDTVSigMon_MGTSeen = 0x0000000004ULL;
146  static const uint64_t kDTVSigMon_VCTSeen = 0x0000000008ULL;
148  static const uint64_t kDTVSigMon_TVCTSeen = 0x0000000010ULL;
150  static const uint64_t kDTVSigMon_CVCTSeen = 0x0000000020ULL;
153  static const uint64_t kDTVSigMon_NITSeen = 0x0000000040ULL;
156  static const uint64_t kDTVSigMon_SDTSeen = 0x0000000080ULL;
158  static const uint64_t kFWSigMon_PowerSeen = 0x0000000100ULL;
160  static const uint64_t kDTVSigMon_CryptSeen = 0x0000000200ULL;
161 
163  static const uint64_t kDTVSigMon_PATMatch = 0x0000001000ULL;
165  static const uint64_t kDTVSigMon_PMTMatch = 0x0000002000ULL;
167  static const uint64_t kDTVSigMon_MGTMatch = 0x0000004000ULL;
169  static const uint64_t kDTVSigMon_VCTMatch = 0x0000008000ULL;
171  static const uint64_t kDTVSigMon_TVCTMatch = 0x0000010000ULL;
173  static const uint64_t kDTVSigMon_CVCTMatch = 0x0000020000ULL;
175  static const uint64_t kDTVSigMon_NITMatch = 0x0000040000ULL;
177  static const uint64_t kDTVSigMon_SDTMatch = 0x0000080000ULL;
179  static const uint64_t kFWSigMon_PowerMatch = 0x0000100000ULL;
181  static const uint64_t kDTVSigMon_CryptMatch = 0x0000200000ULL;
182 
183  static const uint64_t kDTVSigMon_WaitForPAT = 0x0001000000ULL;
184  static const uint64_t kDTVSigMon_WaitForPMT = 0x0002000000ULL;
185  static const uint64_t kDTVSigMon_WaitForMGT = 0x0004000000ULL;
186  static const uint64_t kDTVSigMon_WaitForVCT = 0x0008000000ULL;
187  static const uint64_t kDTVSigMon_WaitForNIT = 0x0010000000ULL;
188  static const uint64_t kDTVSigMon_WaitForSDT = 0x0020000000ULL;
189  static const uint64_t kSigMon_WaitForSig = 0x0040000000ULL;
190  static const uint64_t kFWSigMon_WaitForPower= 0x0080000000ULL;
191  static const uint64_t kDTVSigMon_WaitForCrypt=0x0100000000ULL;
192 
193  static const uint64_t kDTVSigMon_WaitForAll = 0x01FF000000ULL;
194 
196  static const uint64_t kDVBSigMon_WaitForSNR = 0x1000000000ULL;
198  static const uint64_t kDVBSigMon_WaitForBER = 0x2000000000ULL;
200  static const uint64_t kDVBSigMon_WaitForUB = 0x4000000000ULL;
202  static const uint64_t kDVBSigMon_WaitForPos = 0x8000000000ULL;
203 
204  protected:
205  ChannelBase *m_channel {nullptr};
206  TVRec *m_pParent {nullptr};
208  volatile uint64_t m_flags;
210  std::chrono::milliseconds m_updateRate {25ms};
211  std::chrono::milliseconds m_minimumUpdateRate {5ms};
212  bool m_updateDone {false};
213  bool m_notifyFrontend {true};
214  bool m_tablemon {false};
215  bool m_eitScan {false};
216 
217  // not to be confused with StreamHandler::m_bError.
218  QString m_error;
219 
223 
224  std::vector<SignalMonitorListener*> m_listeners;
225 
227  QWaitCondition m_startStopWait; // protected by startStopLock
228  volatile bool m_running {false}; // protected by startStopLock
229  volatile bool m_exit {false}; // protected by startStopLock
230 
231 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
232  mutable QMutex m_statusLock {QMutex::Recursive};
233 #else
234  mutable QRecursiveMutex m_statusLock;
235 #endif
236  mutable QMutex m_listenerLock;
237 };
238 
239 inline QString sm_flags_to_string(uint64_t flags)
240 {
241  QString str("Seen(");
243  str += "PAT,";
245  str += "PMT,";
247  str += "MGT,";
249  str += "VCT,";
251  str += "TVCT,";
253  str += "CVCT,";
255  str += "NIT,";
257  str += "SDT,";
259  str += "STB,";
261  str += "Crypt,";
262 
263  str += ") Match(";
265  str += "PAT,";
267  str += "PMT,";
269  str += "MGT,";
271  str += "VCT,";
273  str += "TVCT,";
275  str += "CVCT,";
277  str += "NIT,";
279  str += "SDT,";
281  str += "STB,";
283  str += "Crypt,";
284 
285  str += ") Wait(";
287  str += "PAT,";
289  str += "PMT,";
291  str += "MGT,";
293  str += "VCT,";
295  str += "NIT,";
297  str += "SDT,";
299  str += "Sig,";
301  str += "STB,";
303  str += "Crypt,";
304 
306  str += "SNR,";
308  str += "BER,";
310  str += "UB,";
312  str += "Pos,";
313 
314  str += ")";
315  return str;
316 }
317 
318 inline bool SignalMonitor::IsRequired(const QString &cardtype)
319 {
320  return (cardtype != "IMPORT" && cardtype != "DEMO");
321 }
322 
323 inline bool SignalMonitor::IsSupported(const QString &cardtype)
324 {
325  return IsRequired(cardtype);
326 }
327 
328 
329 #endif // SIGNALMONITOR_H
SignalMonitor::m_flags
volatile uint64_t m_flags
Definition: signalmonitor.h:208
SignalMonitor::kDTVSigMon_WaitForAll
static const uint64_t kDTVSigMon_WaitForAll
Definition: signalmonitor.h:193
SignalMonitor::m_minimumUpdateRate
std::chrono::milliseconds m_minimumUpdateRate
Definition: signalmonitor.h:211
SignalMonitor::kFWSigMon_WaitForPower
static const uint64_t kFWSigMon_WaitForPower
Definition: signalmonitor.h:190
SignalMonitor::IsAllGood
virtual bool IsAllGood(void) const
Definition: signalmonitor.h:85
SignalMonitor::kDTVSigMon_NITMatch
static const uint64_t kDTVSigMon_NITMatch
We've seen an NIT matching our requirements.
Definition: signalmonitor.h:175
SignalMonitor::IsSupported
static bool IsSupported(const QString &cardtype)
Definition: signalmonitor.h:323
SignalMonitor::kDTVSigMon_VCTMatch
static const uint64_t kDTVSigMon_VCTMatch
We've seen a VCT matching our requirements.
Definition: signalmonitor.h:169
SignalMonitor::m_channel
ChannelBase * m_channel
Definition: signalmonitor.h:205
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:185
SignalMonitor::kDVBSigMon_WaitForSNR
static const uint64_t kDVBSigMon_WaitForSNR
Wait for the Signal to Noise Ratio to rise above a threshold.
Definition: signalmonitor.h:196
SignalMonitor::SendMessage
void SendMessage(SignalMonitorMessageType type, const SignalMonitorValue &val)
Definition: signalmonitor.cpp:412
SignalMonitor::m_exit
volatile bool m_exit
Definition: signalmonitor.h:229
SignalMonitor::kDTVSigMon_CVCTSeen
static const uint64_t kDTVSigMon_CVCTSeen
We've seen a CVCT, the cable version of the VCT.
Definition: signalmonitor.h:150
SignalMonitor::GetSignalStrength
int GetSignalStrength(void)
Definition: signalmonitor.h:75
signalmonitorvalue.h
SignalMonitor::GetUpdateRate
std::chrono::milliseconds GetUpdateRate() const
Returns milliseconds between signal monitoring events.
Definition: signalmonitor.h:73
SignalMonitor::kDTVSigMon_SDTMatch
static const uint64_t kDTVSigMon_SDTMatch
We've seen an SDT matching our requirements.
Definition: signalmonitor.h:177
channelbase.h
SignalMonitor::m_running
volatile bool m_running
Definition: signalmonitor.h:228
SignalMonitor::kDVBSigMon_WaitForBER
static const uint64_t kDVBSigMon_WaitForBER
Wait for the Bit Error Rate to fall below a threshold.
Definition: signalmonitor.h:198
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:165
SignalMonitor::HasAnyFlag
bool HasAnyFlag(uint64_t _flags) const
Definition: signalmonitor.cpp:282
SignalMonitor::AddFlags
virtual void AddFlags(uint64_t _flags)
Definition: signalmonitor.cpp:265
SignalMonitor::kSigMon_WaitForSig
static const uint64_t kSigMon_WaitForSig
Definition: signalmonitor.h:189
SignalMonitor::m_error
QString m_error
Definition: signalmonitor.h:218
SignalMonitor::m_startStopLock
QMutex m_startStopLock
Definition: signalmonitor.h:226
SignalMonitor::m_statusLock
QRecursiveMutex m_statusLock
Definition: signalmonitor.h:234
SignalMonitor::m_scriptStatus
SignalMonitorValue m_scriptStatus
Definition: signalmonitor.h:222
SignalMonitor::IsRequired
static bool IsRequired(const QString &cardtype)
Returns true iff the card type supports signal monitoring.
Definition: signalmonitor.h:318
SignalMonitor::kDVBSigMon_WaitForUB
static const uint64_t kDVBSigMon_WaitForUB
Wait for uncorrected FEC blocks to fall below a threshold.
Definition: signalmonitor.h:200
SignalMonitor::RemoveFlags
virtual void RemoveFlags(uint64_t _flags)
Definition: signalmonitor.cpp:271
SignalMonitor::m_signalStrength
SignalMonitorValue m_signalStrength
Definition: signalmonitor.h:221
SignalMonitor::m_inputid
int m_inputid
Definition: signalmonitor.h:207
SignalMonitor::kDVBSigMon_WaitForPos
static const uint64_t kDVBSigMon_WaitForPos
Wait for rotor to complete turning the antenna.
Definition: signalmonitor.h:202
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:213
SignalMonitor::kFWSigMon_PowerSeen
static const uint64_t kFWSigMon_PowerSeen
We've seen the FireWire STB power state.
Definition: signalmonitor.h:158
sm_flags_to_string
QString sm_flags_to_string(uint64_t flags)
Definition: signalmonitor.h:239
SignalMonitor::m_releaseStream
bool m_releaseStream
Definition: signalmonitor.h:209
SignalMonitor::m_signalLock
SignalMonitorValue m_signalLock
Definition: signalmonitor.h:220
SignalMonitor::m_updateRate
std::chrono::milliseconds m_updateRate
Definition: signalmonitor.h:210
SignalMonitor::run
void run(void) override
Basic signal monitoring loop.
Definition: signalmonitor.cpp:344
SignalMonitor::SendMessageAllGood
void SendMessageAllGood(void)
Definition: signalmonitor.cpp:467
SignalMonitor::kDTVSigMon_CVCTMatch
static const uint64_t kDTVSigMon_CVCTMatch
We've seen a CVCT matching our requirements.
Definition: signalmonitor.h:173
SignalMonitor::~SignalMonitor
~SignalMonitor() override
Stops monitoring thread.
Definition: signalmonitor.cpp:259
SignalMonitor::kDTVSigMon_WaitForPMT
static const uint64_t kDTVSigMon_WaitForPMT
Definition: signalmonitor.h:184
SignalMonitor::kDTVSigMon_WaitForPAT
static const uint64_t kDTVSigMon_WaitForPAT
Definition: signalmonitor.h:183
SignalMonitor::m_updateDone
bool m_updateDone
Definition: signalmonitor.h:212
SignalMonitor::kDTVSigMon_WaitForNIT
static const uint64_t kDTVSigMon_WaitForNIT
Definition: signalmonitor.h:187
SignalMonitor::EmitStatus
virtual void EmitStatus(void)
Definition: signalmonitor.cpp:474
SignalMonitor::kDTVSigMon_WaitForSDT
static const uint64_t kDTVSigMon_WaitForSDT
Definition: signalmonitor.h:188
SignalMonitor::UpdateValues
virtual void UpdateValues(void)
This should be overridden to actually do signal monitoring.
Definition: signalmonitor.cpp:458
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:156
SignalMonitor::kFWSigMon_PowerMatch
static const uint64_t kFWSigMon_PowerMatch
We've seen a FireWire STB power state matching our requirements.
Definition: signalmonitor.h:179
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:153
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:148
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:137
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:140
SignalMonitor::m_startStopWait
QWaitCondition m_startStopWait
Definition: signalmonitor.h:227
SignalMonitor
Signal monitoring base class.
Definition: signalmonitor.h:34
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:90
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:146
SignalMonitor::HasExtraSlowTuning
virtual bool HasExtraSlowTuning(void) const
Definition: signalmonitor.h:64
SignalMonitor::kDTVSigMon_CryptSeen
static const uint64_t kDTVSigMon_CryptSeen
We've seen something indicating whether the data stream is encrypted.
Definition: signalmonitor.h:160
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:113
SignalMonitor::kDTVSigMon_MGTMatch
static const uint64_t kDTVSigMon_MGTMatch
We've seen an MGT matching our requirements.
Definition: signalmonitor.h:167
SignalMonitor::GetNotifyFrontend
bool GetNotifyFrontend() const
Returns whether or not a SIGNAL MythEvent is being sent regularly to the frontend.
Definition: signalmonitor.h:71
SignalMonitor::HasFlags
bool HasFlags(uint64_t _flags) const
Definition: signalmonitor.cpp:277
SignalMonitor::AddListener
void AddListener(SignalMonitorListener *listener)
Definition: signalmonitor.cpp:387
SignalMonitor::m_eitScan
bool m_eitScan
Definition: signalmonitor.h:215
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:143
SignalMonitor::SetMonitoring
void SetMonitoring(TVRec *parent, bool EITscan, bool monitor)
Indicate if table monitoring is needed.
Definition: signalmonitor.h:104
cardutil.h
SignalMonitor::RemoveListener
void RemoveListener(SignalMonitorListener *listener)
Definition: signalmonitor.cpp:398
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:163
TVRec
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:145
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:171
SignalMonitor::Start
virtual void Start()
Start signal monitoring thread.
Definition: signalmonitor.cpp:290
SignalMonitor::m_tablemon
bool m_tablemon
Definition: signalmonitor.h:214
SignalMonitor::SetNotifyFrontend
void SetNotifyFrontend(bool notify)
Enables or disables frontend notification of the current signal value.
Definition: signalmonitor.h:96
mythtimer.h
SignalMonitor::m_listeners
std::vector< SignalMonitorListener * > m_listeners
Definition: signalmonitor.h:224
SignalMonitor::GetStatusList
virtual QStringList GetStatusList(void) const
Returns QStringList containing all signals and their current values.
Definition: signalmonitor.cpp:330
SignalMonitor::IsErrored
bool IsErrored(void) const
Definition: signalmonitor.h:86
SignalMonitor::m_listenerLock
QMutex m_listenerLock
Definition: signalmonitor.h:236
SignalMonitor::GetFlags
uint64_t GetFlags(void) const
Definition: signalmonitor.h:63
SignalMonitor::HasSignalLock
bool HasSignalLock(void) const
Returns true iff scriptStatus.IsGood() and signalLock.IsGood() return true.
Definition: signalmonitor.h:79
SignalMonitor::kDTVSigMon_CryptMatch
static const uint64_t kDTVSigMon_CryptMatch
We've seen unencrypted data in data stream.
Definition: signalmonitor.h:181
SignalMonitor::Stop
virtual void Stop()
Stop signal monitoring thread.
Definition: signalmonitor.cpp:306
SignalMonitor::m_pParent
TVRec * m_pParent
Definition: signalmonitor.h:206
SignalMonitor::Q_DECLARE_TR_FUNCTIONS
Q_DECLARE_TR_FUNCTIONS(SignalMonitor)
SignalMonitor::kDTVSigMon_WaitForCrypt
static const uint64_t kDTVSigMon_WaitForCrypt
Definition: signalmonitor.h:191
SignalMonitor::SignalMonitor
SignalMonitor(int, ChannelBase *, uint64_t, bool)=delete
SignalMonitor::kDTVSigMon_WaitForVCT
static const uint64_t kDTVSigMon_WaitForVCT
Definition: signalmonitor.h:186