MythTV  master
v4l2encsignalmonitor.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 #ifndef USING_MINGW
9 #include <sys/select.h>
10 #endif
11 
12 #include "mythlogging.h"
13 #include "mythdbcon.h"
14 #include "atscstreamdata.h"
15 #include "mpegtables.h"
16 #include "atsctables.h"
17 
18 #include "v4lchannel.h"
19 #include "v4l2encrecorder.h"
20 #include "v4l2encstreamhandler.h"
21 #include "v4l2encsignalmonitor.h"
22 
23 #define LOC QString("V4L2SigMon[%1](%2): ").arg(m_inputid).arg(m_channel->GetDevice())
24 
39  V4LChannel *_channel,
40  bool _release_stream,
41  uint64_t _flags)
42  : DTVSignalMonitor(db_cardnum, _channel, _release_stream, _flags)
43 {
44  LOG(VB_CHANNEL, LOG_INFO, LOC + "ctor");
45 
47  if (!m_v4l2.IsOpen())
48  {
49  LOG(VB_GENERAL, LOG_ERR, LOC + "ctor -- Open failed");
50  return;
51  }
52 
53  m_isTS = (m_v4l2.GetStreamType() == V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
54 
55 
56  m_signalStrength.SetRange(0, 100);
57  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("%1 stream.")
58  .arg(m_isTS ? "Transport" : "Program"));
59 }
60 
65 {
66  LOG(VB_CHANNEL, LOG_INFO, LOC + "dtor");
68  if (m_streamHandler)
70 }
71 
76 {
77  LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- begin");
78 
82 
83  LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- end");
84 }
85 
93 {
94  if (!m_running || m_exit)
95  return;
96 
97  if (!m_isTS)
98  {
102 
104 
105  {
106  QMutexLocker locker(&m_statusLock);
107  if (!m_scriptStatus.IsGood())
108  return;
109  }
110  }
111 
112  if (m_streamHandler)
113  {
114  EmitStatus();
115  if (IsAllGood())
117 
118  m_updateDone = true;
119  return;
120  }
121 
122  bool isLocked = HasLock();
123 
124  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("isLocked: %1").arg(isLocked));
125 
126  {
127  QMutexLocker locker(&m_statusLock);
129  m_signalLock.SetValue(isLocked ? 1 : 0);
130  }
131 
132  EmitStatus();
133  if (IsAllGood())
135 
136  // Start table monitoring if we are waiting on any table
137  // and we have a lock.
138  if (isLocked && !m_streamHandler && GetStreamData() &&
142  {
143  auto* chn = reinterpret_cast<V4LChannel*>(m_channel);
144  m_streamHandler = V4L2encStreamHandler::Get(chn->GetDevice(),
145  chn->GetAudioDevice().toInt(), m_inputid);
147  {
148  LOG(VB_GENERAL, LOG_ERR, LOC +
149  "V4L2encSignalMonitor -- failed to start StreamHandler.");
150  }
151  else
152  {
155  }
156  }
157 
158  m_updateDone = true;
159 }
160 
162 {
163  if (!m_v4l2.IsOpen())
164  {
165  LOG(VB_CHANNEL, LOG_INFO, LOC +
166  "GetSignalStrengthPercent() -- v4l2 device not open");
167  return false;
168  }
169 
170  if (m_strength >= 0)
172  if (m_strength < 0)
173  return (true /* || StableResolution() == 100 */);
174 
175  return m_strength > 50;
176 }
177 
188 {
189  int width = 0;
190  int height = 0;
191 
192  if (m_v4l2.GetResolution(width, height))
193  {
194  m_timer.stop();
195  if (width > 0)
196  {
197  LOG(VB_CHANNEL, LOG_INFO, LOC +
198  QString("Resolution already stable at %1 x %2")
199  .arg(width).arg(height));
200  return 100;
201  }
202  LOG(VB_GENERAL, LOG_ERR, LOC + "Device wedged?");
203  return 0;
204  }
205 
206  if (m_width == width)
207  {
208  if (!m_timer.isRunning())
209  {
210  LOG(VB_CHANNEL, LOG_INFO, QString("Resolution %1 x %2")
211  .arg(width).arg(height));
212  if (++m_lockCnt > 9)
213  m_lockCnt = 1;
214  m_timer.start();
215  }
216  else if (m_timer.elapsed() > m_stableTime)
217  {
218  LOG(VB_CHANNEL, LOG_INFO, QString("Resolution stable at %1 x %2")
219  .arg(width).arg(height));
220  m_timer.stop();
221  return 100;
222  }
223  else
224  return 40 + m_lockCnt;
225  }
226  else
227  {
228  // Indicate that we are still waiting for a valid res, every 3 seconds.
229  if (!m_statusTime.isValid() || MythDate::current() > m_statusTime)
230  {
231  LOG(VB_CHANNEL, LOG_WARNING, "Waiting for valid resolution");
232  m_statusTime = MythDate::current().addSecs(3);
233  }
234  m_timer.stop();
235  }
236 
237  m_width = width;
238  return 20 + m_lockCnt;
239 }
V4L2encStreamHandler::Return
static void Return(V4L2encStreamHandler *&ref, int inputid)
Definition: v4l2encstreamhandler.cpp:94
V4L2encSignalMonitor::HasLock
bool HasLock(void)
Definition: v4l2encsignalmonitor.cpp:161
LOC
#define LOC
Definition: v4l2encsignalmonitor.cpp:23
DTVSignalMonitor::RemoveFlags
void RemoveFlags(uint64_t _flags) override
Definition: dtvsignalmonitor.cpp:149
V4L2util::GetResolution
bool GetResolution(int &width, int &height) const
Definition: v4l2util.cpp:657
SignalMonitorValue::SetValue
void SetValue(int _value)
Definition: signalmonitorvalue.h:75
V4L2encSignalMonitor::m_timer
MythTimer m_timer
Definition: v4l2encsignalmonitor.h:47
V4L2encSignalMonitor::m_statusTime
QDateTime m_statusTime
Definition: v4l2encsignalmonitor.h:48
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
MythTimer::stop
void stop(void)
Stops timer, next call to isRunning() will return false and any calls to elapsed() or restart() will ...
Definition: mythtimer.cpp:77
SignalMonitor::m_exit
volatile bool m_exit
Definition: signalmonitor.h:227
arg
arg(title).arg(filename).arg(doDelete))
mythdbcon.h
V4L2encSignalMonitor::~V4L2encSignalMonitor
~V4L2encSignalMonitor() override
Stops signal monitoring and table monitoring threads.
Definition: v4l2encsignalmonitor.cpp:64
MythTimer::isRunning
bool isRunning(void) const
Returns true if start() or restart() has been called at least once since construction and since any c...
Definition: mythtimer.cpp:134
DTVSignalMonitor::GetStreamData
MPEGStreamData * GetStreamData()
Returns the MPEG stream data if it exists.
Definition: dtvsignalmonitor.h:60
atscstreamdata.h
MythTimer::start
void start(void)
starts measuring elapsed time.
Definition: mythtimer.cpp:47
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
SignalMonitor::m_running
volatile bool m_running
Definition: signalmonitor.h:226
V4L2encSignalMonitor::m_v4l2
V4L2util m_v4l2
Definition: v4l2encsignalmonitor.h:37
v4l2encstreamhandler.h
SignalMonitor::HasAnyFlag
bool HasAnyFlag(uint64_t _flags) const
Definition: signalmonitor.cpp:282
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
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
v4l2encsignalmonitor.h
SignalMonitor::m_signalStrength
SignalMonitorValue m_signalStrength
Definition: signalmonitor.h:219
mythlogging.h
SignalMonitor::m_inputid
int m_inputid
Definition: signalmonitor.h:205
SignalMonitor::kDVBSigMon_WaitForPos
static const uint64_t kDVBSigMon_WaitForPos
Wait for rotor to complete turning the antenna.
Definition: signalmonitor.h:200
V4L2encSignalMonitor::StableResolution
int StableResolution(void)
Wait for a stable signal.
Definition: v4l2encsignalmonitor.cpp:187
SignalMonitor::m_signalLock
SignalMonitorValue m_signalLock
Definition: signalmonitor.h:218
SignalMonitor::SendMessageAllGood
void SendMessageAllGood(void)
Definition: signalmonitor.cpp:467
v4lchannel.h
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
V4L2util::Open
bool Open(const QString &dev_name, const QString &vbi_dev_name="")
Definition: v4l2util.cpp:32
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
V4L2encSignalMonitor::m_streamHandler
V4L2encStreamHandler * m_streamHandler
Definition: v4l2encsignalmonitor.h:38
V4L2encSignalMonitor::m_isTS
bool m_isTS
Definition: v4l2encsignalmonitor.h:39
V4L2encStreamHandler::StartEncoding
bool StartEncoding(void)
Definition: v4l2encstreamhandler.cpp:500
v4l2encrecorder.h
mpegtables.h
V4L2encSignalMonitor::m_stableTime
int m_stableTime
Definition: v4l2encsignalmonitor.h:43
SignalMonitorValue::SetRange
void SetRange(int _min, int _max)
Sets the minimum and maximum values.
Definition: signalmonitorvalue.h:94
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
V4L2encSignalMonitor::V4L2encSignalMonitor
V4L2encSignalMonitor(void)
MythTimer::elapsed
int elapsed(void)
Returns milliseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:90
V4L2util::IsOpen
bool IsOpen(void) const
Definition: v4l2util.h:29
V4L2encSignalMonitor::Stop
void Stop(void) override
Stop signal monitoring and table monitoring threads.
Definition: v4l2encsignalmonitor.cpp:75
SignalMonitor::m_statusLock
QMutex m_statusLock
Definition: signalmonitor.h:229
DTVSignalMonitor
This class is intended to detect the presence of needed tables.
Definition: dtvsignalmonitor.h:20
V4L2encSignalMonitor::m_strength
int m_strength
Definition: v4l2encsignalmonitor.h:42
atsctables.h
Overall structure.
ChannelBase::GetDevice
virtual QString GetDevice(void) const
Returns String representing device, useful for debugging.
Definition: channelbase.h:78
V4L2encSignalMonitor::m_lockCnt
uint m_lockCnt
Definition: v4l2encsignalmonitor.h:46
V4L2encStreamHandler::Get
static V4L2encStreamHandler * Get(const QString &devname, int audioinput, int inputid)
Definition: v4l2encstreamhandler.cpp:62
V4L2util::GetStreamType
int GetStreamType(void) const
Definition: v4l2util.cpp:802
SignalMonitor::Stop
virtual void Stop()
Stop signal monitoring thread.
Definition: signalmonitor.cpp:306
V4L2encSignalMonitor::UpdateValues
void UpdateValues(void) override
Fills in frontend stats and emits status Qt signals.
Definition: v4l2encsignalmonitor.cpp:92
V4LChannel
Implements tuning for TV cards using the V4L driver API, both versions 1 and 2.
Definition: v4lchannel.h:33
DTVSignalMonitor::IsAllGood
bool IsAllGood(void) const override
Definition: dtvsignalmonitor.cpp:567
V4L2encSignalMonitor::m_width
int m_width
Definition: v4l2encsignalmonitor.h:44
SignalMonitor::kDTVSigMon_WaitForVCT
static const uint64_t kDTVSigMon_WaitForVCT
Definition: signalmonitor.h:184
V4L2util::GetSignalStrength
int GetSignalStrength(void) const
Definition: v4l2util.cpp:636