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  QString result;
46 
47  LOG(VB_CHANNEL, LOG_INFO, LOC + "ctor");
52  LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
53  else
54  m_lockTimeout = GetLockTimeout() * 1000;
55 
56  if (GetExternalChannel()->IsBackgroundTuning())
58 }
59 
64 {
65  LOG(VB_CHANNEL, LOG_INFO, LOC + "dtor");
68 }
69 
74 {
75  QString result;
76 
77  LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- begin");
78 
80  if (GetStreamData())
81  {
84  }
85  m_streamHandlerStarted = false;
86 
87  LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- end");
88 }
89 
97 {
98  if (!m_running || m_exit)
99  return;
100 
101  if (GetExternalChannel()->IsExternalChannelChangeInUse())
102  {
104 
105  QMutexLocker locker(&m_statusLock);
106  if (!m_scriptStatus.IsGood())
107  return;
108  }
109 
110  if (GetExternalChannel()->IsBackgroundTuning())
111  {
112  QMutexLocker locker(&m_statusLock);
113  if (m_scriptStatus.GetValue() < 2)
114  m_scriptStatus.SetValue(GetExternalChannel()->GetTuneStatus());
115 
116  if (!m_scriptStatus.IsGood())
117  return;
118  }
119 
121  {
122  if (!m_streamHandler->IsRunning())
123  {
124  m_error = QObject::tr("Error: stream handler died");
125  LOG(VB_CHANNEL, LOG_ERR, LOC + m_error);
126  m_updateDone = true;
127  return;
128  }
129 
130  EmitStatus();
131  if (IsAllGood())
133 
134  m_updateDone = true;
135  return;
136  }
137 
139 
140  int strength = GetSignalStrengthPercent();
141  bool is_locked = HasLock();
142 
143  // Set SignalMonitorValues
144  {
145  QMutexLocker locker(&m_statusLock);
146  m_signalStrength.SetValue(strength);
147  m_signalLock.SetValue(static_cast<int>(is_locked));
148  }
149 
150  EmitStatus();
151  if (IsAllGood())
153 
154  // Start table monitoring if we are waiting on any table
155  // and we have a lock.
156  if (is_locked && GetStreamData() &&
160  {
162  {
165  m_streamHandlerStarted = true;
166  }
167  }
168 
169  m_updateDone = true;
170 }
171 
173 {
174  QString result;
175 
176  m_streamHandler->ProcessCommand("HasLock?", result);
177  if (result.startsWith("OK:"))
178  {
179  return result.mid(3, 3) == "Yes";
180  }
181  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
182  ("HasLock: invalid response '%1'").arg(result));
183  if (!result.startsWith("WARN"))
184  m_error = QString("HasLock: invalid response '%1'").arg(result);
185  return false;
186 }
187 
189 {
190  QString result;
191 
192  m_streamHandler->ProcessCommand("SignalStrengthPercent?", result);
193  if (result.startsWith("OK:"))
194  {
195  bool ok = false;
196  int percent = result.mid(3).toInt(&ok);
197  if (!ok)
198  {
199  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
200  ("GetSignalStrengthPercent: invalid response '%1'")
201  .arg(result));
202  return -1;
203  }
204  return percent;
205  }
206  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
207  ("GetSignalStrengthPercent: invalid response '%1'").arg(result));
208  if (!result.startsWith("WARN"))
209  m_error = QString("GetSignalStrengthPercent: invalid response '%1'")
210  .arg(result);
211  return -1;
212 }
213 
215 {
216  QString result;
217 
218  m_streamHandler->ProcessCommand("LockTimeout?", result, 10000);
219  if (result.startsWith("OK:"))
220  {
221  bool ok = false;
222  int timeout = result.mid(3).toInt(&ok);
223  if (!ok)
224  {
225  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
226  ("GetLockTimeout: invalid response '%1'")
227  .arg(result));
228  return -1;
229  }
230  return timeout;
231  }
232  LOG(VB_CHANNEL, LOG_ERR, LOC + QString
233  ("GetLockTimeout: invalid response '%1'").arg(result));
234  if (!result.startsWith("WARN"))
235  m_error = QString("GetLockTimeout: invalid response '%1'").arg(result);
236  return -1;
237 }
DTVSignalMonitor::AddFlags
void AddFlags(uint64_t _flags) override
Definition: dtvsignalmonitor.cpp:143
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
ExternalRecorder.h
ExternalStreamHandler::ProcessCommand
bool ProcessCommand(const QString &cmd, QString &result, int timeout=4000, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1227
SignalMonitorValue::SetValue
void SetValue(int _value)
Definition: signalmonitorvalue.h:75
StreamHandler::HasError
bool HasError(void) const
Definition: streamhandler.h:64
SignalMonitor::m_channel
ChannelBase * m_channel
Definition: signalmonitor.h:203
SignalMonitor::kDTVSigMon_WaitForMGT
static const uint64_t kDTVSigMon_WaitForMGT
Definition: signalmonitor.h:183
arg
arg(title).arg(filename).arg(doDelete))
SignalMonitor::m_exit
volatile bool m_exit
Definition: signalmonitor.h:227
mythdbcon.h
ExternalStreamHandler::StopStreaming
bool StopStreaming(void)
Definition: ExternalStreamHandler.cpp:1168
DTVSignalMonitor::GetStreamData
MPEGStreamData * GetStreamData()
Returns the MPEG stream data if it exists.
Definition: dtvsignalmonitor.h:60
atscstreamdata.h
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
ExternalSignalMonitor::m_lockTimeout
int m_lockTimeout
Definition: ExternalSignalMonitor.h:42
SignalMonitor::m_running
volatile bool m_running
Definition: signalmonitor.h:226
ExternalSignalMonitor.h
ExternalStreamHandler.h
ExternalSignalMonitor::GetLockTimeout
int GetLockTimeout(void)
Definition: ExternalSignalMonitor.cpp:214
SignalMonitor::HasAnyFlag
bool HasAnyFlag(uint64_t _flags) const
Definition: signalmonitor.cpp:282
SignalMonitor::kSigMon_WaitForSig
static const uint64_t kSigMon_WaitForSig
Definition: signalmonitor.h:187
SignalMonitor::m_error
QString m_error
Definition: signalmonitor.h:216
ExternalSignalMonitor::GetSignalStrengthPercent
int GetSignalStrengthPercent(void)
Definition: ExternalSignalMonitor.cpp:188
StreamHandler::RemoveListener
virtual void RemoveListener(MPEGStreamData *data)
Definition: streamhandler.cpp:78
StreamHandler::AddListener
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, QString output_file=QString())
Definition: streamhandler.cpp:33
SignalMonitor::m_scriptStatus
SignalMonitorValue m_scriptStatus
Definition: signalmonitor.h:220
SignalMonitorValue::GetValue
int GetValue() const
Returns the value.
Definition: signalmonitorvalue.h:41
ExternalSignalMonitor::~ExternalSignalMonitor
~ExternalSignalMonitor() override
Stops signal monitoring and table monitoring threads.
Definition: ExternalSignalMonitor.cpp:63
SignalMonitor::m_signalStrength
SignalMonitorValue m_signalStrength
Definition: signalmonitor.h:219
mythlogging.h
SignalMonitor::m_inputid
int m_inputid
Definition: signalmonitor.h:205
LOC
#define LOC
Definition: ExternalSignalMonitor.cpp:23
SignalMonitor::m_signalLock
SignalMonitorValue m_signalLock
Definition: signalmonitor.h:218
SignalMonitor::SendMessageAllGood
void SendMessageAllGood(void)
Definition: signalmonitor.cpp:467
SignalMonitor::kDTVSigMon_WaitForPMT
static const uint64_t kDTVSigMon_WaitForPMT
Definition: signalmonitor.h:182
SignalMonitor::kDTVSigMon_WaitForPAT
static const uint64_t kDTVSigMon_WaitForPAT
Definition: signalmonitor.h:181
SignalMonitor::m_updateDone
bool m_updateDone
Definition: signalmonitor.h:210
SignalMonitor::kDTVSigMon_WaitForNIT
static const uint64_t kDTVSigMon_WaitForNIT
Definition: signalmonitor.h:185
SignalMonitor::EmitStatus
virtual void EmitStatus(void)
Definition: signalmonitor.cpp:474
SignalMonitor::kDTVSigMon_WaitForSDT
static const uint64_t kDTVSigMon_WaitForSDT
Definition: signalmonitor.h:186
SignalMonitor::UpdateValues
virtual void UpdateValues(void)
This should be overridden to actually do signal monitoring.
Definition: signalmonitor.cpp:458
ExternalStreamHandler::Get
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:455
mpegtables.h
ExternalChannel
Definition: ExternalChannel.h:19
ExternalStreamHandler::StartStreaming
bool StartStreaming(void)
Definition: ExternalStreamHandler.cpp:1120
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:57
ExternalSignalMonitor::HasLock
bool HasLock(void)
Definition: ExternalSignalMonitor.cpp:172
ExternalChannel.h
SignalMonitor::m_statusLock
QMutex m_statusLock
Definition: signalmonitor.h:229
ExternalSignalMonitor::GetExternalChannel
ExternalChannel * GetExternalChannel(void)
Definition: ExternalSignalMonitor.h:29
DTVSignalMonitor
This class is intended to detect the presence of needed tables.
Definition: dtvsignalmonitor.h:20
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:96
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
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:73
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:857
DTVSignalMonitor::IsAllGood
bool IsAllGood(void) const override
Definition: dtvsignalmonitor.cpp:566
ExternalStreamHandler::Return
static void Return(ExternalStreamHandler *&ref, int inputid)
Definition: ExternalStreamHandler.cpp:484
ExternalSignalMonitor::ExternalSignalMonitor
ExternalSignalMonitor(void)
SignalMonitor::kDTVSigMon_WaitForVCT
static const uint64_t kDTVSigMon_WaitForVCT
Definition: signalmonitor.h:184