MythTV  master
ExternalSignalMonitor.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #include <cerrno>
4 #include <cstring>
5 
6 #include <fcntl.h>
7 #include <unistd.h>
8 #if !defined(USING_MINGW) && !defined(_WIN32)
9 #include <sys/select.h>
10 #endif
11 
12 #include "mythlogging.h"
13 #include "mythdbcon.h"
14 #include "ExternalSignalMonitor.h"
15 #include "atscstreamdata.h"
16 #include "mpegtables.h"
17 #include "atsctables.h"
18 
19 #include "ExternalChannel.h"
20 #include "ExternalRecorder.h"
21 #include "ExternalStreamHandler.h"
22 
23 #define LOC QString("ExternSigMon[%1](%2): ").arg(m_inputid).arg(m_loc)
24 
40  ExternalChannel *_channel,
41  bool _release_stream,
42  uint64_t _flags)
43  : DTVSignalMonitor(db_cardnum, _channel, _release_stream, _flags)
44 {
45  LOG(VB_CHANNEL, LOG_INFO, LOC + "ctor");
50  {
51  LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
52  if (m_streamHandler)
54  }
55  else
57 
59  if (channel && channel->IsBackgroundTuning())
61 }
62 
67 {
68  LOG(VB_CHANNEL, LOG_INFO, LOC + "dtor");
70  if (m_streamHandler)
72 }
73 
78 {
79  LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- begin");
80 
82  if (GetStreamData())
83  {
86  }
87  m_streamHandlerStarted = false;
88 
89  LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- end");
90 }
91 
99 {
100  if (!m_running || m_exit)
101  return;
102 
103  ExternalChannel *channel = GetExternalChannel();
104  if (channel == nullptr)
105  return;
106 
107  if (channel->IsExternalChannelChangeInUse())
108  {
110 
111  QMutexLocker locker(&m_statusLock);
112  if (!m_scriptStatus.IsGood())
113  return;
114  }
115 
116  if (channel->IsBackgroundTuning())
117  {
118  QMutexLocker locker(&m_statusLock);
119  if (m_scriptStatus.GetValue() < 2)
121 
122  if (!m_scriptStatus.IsGood())
123  return;
124  }
125 
127  {
128  if (!m_streamHandler->IsRunning())
129  {
130  m_error = QObject::tr("Error: stream handler died");
131  LOG(VB_CHANNEL, LOG_ERR, LOC + m_error);
132  m_updateDone = true;
133  return;
134  }
135 
136  EmitStatus();
137  if (IsAllGood())
139 
140  m_updateDone = true;
141  return;
142  }
143 
145 
146  int strength = GetSignalStrengthPercent();
147  bool is_locked = HasLock();
148 
149  // Set SignalMonitorValues
150  {
151  QMutexLocker locker(&m_statusLock);
152  m_signalStrength.SetValue(strength);
153  m_signalLock.SetValue(static_cast<int>(is_locked));
154  }
155 
156  EmitStatus();
157  if (IsAllGood())
159 
160  // Start table monitoring if we are waiting on any table
161  // and we have a lock.
162  if (is_locked && GetStreamData() &&
166  {
168  {
171  m_streamHandlerStarted = true;
172  }
173  }
174 
175  m_updateDone = true;
176 }
177 
179 {
180  QString result;
181 
182  m_streamHandler->ProcessCommand("HasLock?", result);
183  if (result.startsWith("OK:"))
184  {
185  return result.mid(3, 3) == "Yes";
186  }
187  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
188  ("HasLock: invalid response '%1'").arg(result));
189  if (!result.startsWith("WARN"))
190  m_error = QString("HasLock: invalid response '%1'").arg(result);
191  return false;
192 }
193 
195 {
196  QString result;
197 
198  m_streamHandler->ProcessCommand("SignalStrengthPercent?", result);
199  if (result.startsWith("OK:"))
200  {
201  bool ok = false;
202  int percent = result.mid(3).toInt(&ok);
203  if (!ok)
204  {
205  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
206  ("GetSignalStrengthPercent: invalid response '%1'")
207  .arg(result));
208  return -1;
209  }
210  return percent;
211  }
212  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
213  ("GetSignalStrengthPercent: invalid response '%1'").arg(result));
214  if (!result.startsWith("WARN"))
215  m_error = QString("GetSignalStrengthPercent: invalid response '%1'")
216  .arg(result);
217  return -1;
218 }
219 
220 std::chrono::seconds ExternalSignalMonitor::GetLockTimeout(void)
221 {
222  QString result;
223 
224  m_streamHandler->ProcessCommand("LockTimeout?", result, 10s);
225  if (result.startsWith("OK:"))
226  {
227  bool ok = false;
228  auto timeout = std::chrono::seconds(result.mid(3).toInt(&ok));
229  if (!ok)
230  {
231  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
232  ("GetLockTimeout: invalid response '%1'")
233  .arg(result));
234  return -1s;
235  }
236  return timeout;
237  }
238  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
239  ("GetLockTimeout: invalid response '%1'").arg(result));
240  if (!result.startsWith("WARN"))
241  m_error = QString("GetLockTimeout: invalid response '%1'").arg(result);
242  return -1s;
243 }
DTVSignalMonitor::AddFlags
void AddFlags(uint64_t _flags) override
Definition: dtvsignalmonitor.cpp:140
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
ExternalRecorder.h
SignalMonitorValue::SetValue
void SetValue(int _value)
Definition: signalmonitorvalue.h:74
StreamHandler::HasError
bool HasError(void) const
Definition: streamhandler.h:66
SignalMonitor::m_channel
ChannelBase * m_channel
Definition: signalmonitor.h:205
SignalMonitor::kDTVSigMon_WaitForMGT
static const uint64_t kDTVSigMon_WaitForMGT
Definition: signalmonitor.h:185
ExternalSignalMonitor::m_lockTimeout
std::chrono::milliseconds m_lockTimeout
Definition: ExternalSignalMonitor.h:42
SignalMonitor::m_exit
volatile bool m_exit
Definition: signalmonitor.h:229
mythdbcon.h
ExternalStreamHandler::StopStreaming
bool StopStreaming(void)
Definition: ExternalStreamHandler.cpp:1172
DTVSignalMonitor::GetStreamData
MPEGStreamData * GetStreamData()
Returns the MPEG stream data if it exists.
Definition: dtvsignalmonitor.h:59
atscstreamdata.h
ExternalChannel::IsBackgroundTuning
bool IsBackgroundTuning(void) const
Definition: ExternalChannel.h:48
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
SignalMonitor::m_running
volatile bool m_running
Definition: signalmonitor.h:228
ExternalSignalMonitor.h
ExternalStreamHandler.h
SignalMonitor::HasAnyFlag
bool HasAnyFlag(uint64_t _flags) const
Definition: signalmonitor.cpp:282
SignalMonitor::kSigMon_WaitForSig
static const uint64_t kSigMon_WaitForSig
Definition: signalmonitor.h:189
SignalMonitor::m_error
QString m_error
Definition: signalmonitor.h:218
ExternalSignalMonitor::GetSignalStrengthPercent
int GetSignalStrengthPercent(void)
Definition: ExternalSignalMonitor.cpp:194
StreamHandler::RemoveListener
virtual void RemoveListener(MPEGStreamData *data)
Definition: streamhandler.cpp:78
SignalMonitor::m_statusLock
QRecursiveMutex m_statusLock
Definition: signalmonitor.h:234
SignalMonitor::m_scriptStatus
SignalMonitorValue m_scriptStatus
Definition: signalmonitor.h:222
SignalMonitorValue::GetValue
int GetValue() const
Returns the value.
Definition: signalmonitorvalue.h:40
ExternalSignalMonitor::~ExternalSignalMonitor
~ExternalSignalMonitor() override
Stops signal monitoring and table monitoring threads.
Definition: ExternalSignalMonitor.cpp:66
SignalMonitor::m_signalStrength
SignalMonitorValue m_signalStrength
Definition: signalmonitor.h:221
mythlogging.h
SignalMonitor::m_inputid
int m_inputid
Definition: signalmonitor.h:207
LOC
#define LOC
Definition: ExternalSignalMonitor.cpp:23
ExternalSignalMonitor::GetLockTimeout
std::chrono::seconds GetLockTimeout(void)
Definition: ExternalSignalMonitor.cpp:220
SignalMonitor::m_signalLock
SignalMonitorValue m_signalLock
Definition: signalmonitor.h:220
SignalMonitor::SendMessageAllGood
void SendMessageAllGood(void)
Definition: signalmonitor.cpp:467
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
ChannelBase::IsExternalChannelChangeInUse
virtual bool IsExternalChannelChangeInUse(void)
Definition: channelbase.cpp:836
ExternalStreamHandler::Get
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:455
mpegtables.h
ExternalChannel
-*- Mode: c++ -*-
Definition: ExternalChannel.h:17
ExternalStreamHandler::StartStreaming
bool StartStreaming(void)
Definition: ExternalStreamHandler.cpp:1124
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
ExternalSignalMonitor::HasLock
bool HasLock(void)
Definition: ExternalSignalMonitor.cpp:178
ExternalChannel::GetTuneStatus
uint GetTuneStatus(void)
Definition: ExternalChannel.cpp:144
ExternalChannel.h
ExternalSignalMonitor::GetExternalChannel
ExternalChannel * GetExternalChannel(void)
Definition: ExternalSignalMonitor.h:29
ExternalStreamHandler::ProcessCommand
bool ProcessCommand(const QString &cmd, QString &result, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1231
DTVSignalMonitor
This class is intended to detect the presence of needed tables.
Definition: dtvsignalmonitor.h:14
ChannelBase::GetInputID
virtual int GetInputID(void) const
Definition: channelbase.h:67
ExternalSignalMonitor::UpdateValues
void UpdateValues(void) override
Fills in frontend stats and emits status Qt signals.
Definition: ExternalSignalMonitor.cpp:98
atsctables.h
ChannelBase::GetDevice
virtual QString GetDevice(void) const
Returns String representing device, useful for debugging.
Definition: channelbase.h:78
ExternalSignalMonitor::m_streamHandlerStarted
bool m_streamHandlerStarted
Definition: ExternalSignalMonitor.h:41
StreamHandler::AddListener
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, const QString &output_file=QString())
Definition: streamhandler.cpp:33
ExternalSignalMonitor::m_streamHandler
ExternalStreamHandler * m_streamHandler
Definition: ExternalSignalMonitor.h:40
ExternalSignalMonitor::Stop
void Stop(void) override
Stop signal monitoring and table monitoring threads.
Definition: ExternalSignalMonitor.cpp:77
StreamHandler::IsRunning
bool IsRunning(void) const
Definition: streamhandler.cpp:161
SignalMonitor::Stop
virtual void Stop()
Stop signal monitoring thread.
Definition: signalmonitor.cpp:306
ChannelBase::GetMajorID
int GetMajorID(void)
Definition: channelbase.cpp:852
DTVSignalMonitor::IsAllGood
bool IsAllGood(void) const override
Definition: dtvsignalmonitor.cpp:580
ExternalStreamHandler::Return
static void Return(ExternalStreamHandler *&ref, int inputid)
Definition: ExternalStreamHandler.cpp:486
ExternalSignalMonitor::ExternalSignalMonitor
ExternalSignalMonitor(void)
SignalMonitor::kDTVSigMon_WaitForVCT
static const uint64_t kDTVSigMon_WaitForVCT
Definition: signalmonitor.h:186