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 "libmythbase/mythdbcon.h"
14 
15 #include "mpeg/atscstreamdata.h"
16 #include "mpeg/atsctables.h"
17 #include "mpeg/mpegtables.h"
18 
19 #include "v4lchannel.h"
20 #include "v4l2encrecorder.h"
21 #include "v4l2encstreamhandler.h"
22 #include "v4l2encsignalmonitor.h"
23 
24 #define LOC QString("V4L2SigMon[%1](%2): ").arg(m_inputid).arg(m_channel->GetDevice())
25 
40  V4LChannel *_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");
46 
48  if (!m_v4l2.IsOpen())
49  {
50  LOG(VB_GENERAL, LOG_ERR, LOC + "ctor -- Open failed");
51  return;
52  }
53 
54  m_isTS = (m_v4l2.GetStreamType() == V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
55 
56 
57  m_signalStrength.SetRange(0, 100);
58  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("%1 stream.")
59  .arg(m_isTS ? "Transport" : "Program"));
60 }
61 
66 {
67  LOG(VB_CHANNEL, LOG_INFO, LOC + "dtor");
69  if (m_streamHandler)
71 }
72 
77 {
78  LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- begin");
79 
83 
84  LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- end");
85 }
86 
94 {
95  if (!m_running || m_exit)
96  return;
97 
98  if (!m_isTS)
99  {
103 
105 
106  {
107  QMutexLocker locker(&m_statusLock);
108  if (!m_scriptStatus.IsGood())
109  return;
110  }
111  }
112 
113  if (m_streamHandler)
114  {
115  EmitStatus();
116  if (IsAllGood())
118 
119  m_updateDone = true;
120  return;
121  }
122 
123  bool isLocked = HasLock();
124 
125  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("isLocked: %1").arg(isLocked));
126 
127  {
128  QMutexLocker locker(&m_statusLock);
130  m_signalLock.SetValue(isLocked ? 1 : 0);
131  }
132 
133  EmitStatus();
134  if (IsAllGood())
136 
137  // Start table monitoring if we are waiting on any table
138  // and we have a lock.
139  if (isLocked && !m_streamHandler && GetStreamData() &&
143  {
144  auto* chn = reinterpret_cast<V4LChannel*>(m_channel);
145  m_streamHandler = V4L2encStreamHandler::Get(chn->GetDevice(),
146  chn->GetAudioDevice().toInt(), m_inputid);
148  {
149  LOG(VB_GENERAL, LOG_ERR, LOC +
150  "V4L2encSignalMonitor -- failed to start StreamHandler.");
151  }
152  else
153  {
156  }
157  }
158 
159  m_updateDone = true;
160 }
161 
163 {
164  if (!m_v4l2.IsOpen())
165  {
166  LOG(VB_CHANNEL, LOG_INFO, LOC +
167  "GetSignalStrengthPercent() -- v4l2 device not open");
168  return false;
169  }
170 
171  if (m_strength >= 0)
173  if (m_strength < 0)
174  return (true /* || StableResolution() == 100 */);
175 
176  return m_strength > 50;
177 }
178 
189 {
190  int width = 0;
191  int height = 0;
192 
193  if (m_v4l2.GetResolution(width, height))
194  {
195  m_timer.stop();
196  if (width > 0)
197  {
198  LOG(VB_CHANNEL, LOG_INFO, LOC +
199  QString("Resolution already stable at %1 x %2")
200  .arg(width).arg(height));
201  return 100;
202  }
203  LOG(VB_GENERAL, LOG_ERR, LOC + "Device wedged?");
204  return 0;
205  }
206 
207  if (m_width == width)
208  {
209  if (!m_timer.isRunning())
210  {
211  LOG(VB_CHANNEL, LOG_INFO, QString("Resolution %1 x %2")
212  .arg(width).arg(height));
213  if (++m_lockCnt > 9)
214  m_lockCnt = 1;
215  m_timer.start();
216  }
217  else if (m_timer.elapsed() > m_stableTime)
218  {
219  LOG(VB_CHANNEL, LOG_INFO, QString("Resolution stable at %1 x %2")
220  .arg(width).arg(height));
221  m_timer.stop();
222  return 100;
223  }
224  else
225  return 40 + m_lockCnt;
226  }
227  else
228  {
229  // Indicate that we are still waiting for a valid res, every 3 seconds.
230  if (!m_statusTime.isValid() || MythDate::current() > m_statusTime)
231  {
232  LOG(VB_CHANNEL, LOG_WARNING, "Waiting for valid resolution");
233  m_statusTime = MythDate::current().addSecs(3);
234  }
235  m_timer.stop();
236  }
237 
238  m_width = width;
239  return 20 + m_lockCnt;
240 }
V4L2encStreamHandler::Return
static void Return(V4L2encStreamHandler *&ref, int inputid)
Definition: v4l2encstreamhandler.cpp:95
MythTimer::elapsed
std::chrono::milliseconds elapsed(void)
Returns milliseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:91
V4L2encSignalMonitor::HasLock
bool HasLock(void)
Definition: v4l2encsignalmonitor.cpp:162
LOC
#define LOC
Definition: v4l2encsignalmonitor.cpp:24
DTVSignalMonitor::RemoveFlags
void RemoveFlags(uint64_t _flags) override
Definition: dtvsignalmonitor.cpp:146
V4L2util::GetResolution
bool GetResolution(int &width, int &height) const
Definition: v4l2util.cpp:650
SignalMonitorValue::SetValue
void SetValue(int _value)
Definition: signalmonitorvalue.h:74
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:67
SignalMonitor::m_channel
ChannelBase * m_channel
Definition: signalmonitor.h:206
SignalMonitor::kDTVSigMon_WaitForMGT
static const uint64_t kDTVSigMon_WaitForMGT
Definition: signalmonitor.h:186
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:78
SignalMonitor::m_exit
volatile bool m_exit
Definition: signalmonitor.h:230
mythdbcon.h
V4L2encSignalMonitor::~V4L2encSignalMonitor
~V4L2encSignalMonitor() override
Stops signal monitoring and table monitoring threads.
Definition: v4l2encsignalmonitor.cpp:65
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:135
DTVSignalMonitor::GetStreamData
MPEGStreamData * GetStreamData()
Returns the MPEG stream data if it exists.
Definition: dtvsignalmonitor.h:59
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:229
V4L2encSignalMonitor::m_v4l2
V4L2util m_v4l2
Definition: v4l2encsignalmonitor.h:37
v4l2encstreamhandler.h
SignalMonitor::HasAnyFlag
bool HasAnyFlag(uint64_t _flags) const
Definition: signalmonitor.cpp:283
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:15
StreamHandler::RemoveListener
virtual void RemoveListener(MPEGStreamData *data)
Definition: streamhandler.cpp:80
SignalMonitor::m_statusLock
QRecursiveMutex m_statusLock
Definition: signalmonitor.h:235
SignalMonitor::m_scriptStatus
SignalMonitorValue m_scriptStatus
Definition: signalmonitor.h:223
v4l2encsignalmonitor.h
SignalMonitor::m_signalStrength
SignalMonitorValue m_signalStrength
Definition: signalmonitor.h:222
mythlogging.h
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
V4L2encSignalMonitor::StableResolution
int StableResolution(void)
Wait for a stable signal.
Definition: v4l2encsignalmonitor.cpp:188
SignalMonitor::m_signalLock
SignalMonitorValue m_signalLock
Definition: signalmonitor.h:221
SignalMonitor::SendMessageAllGood
void SendMessageAllGood(void)
Definition: signalmonitor.cpp:468
v4lchannel.h
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
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: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
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:501
v4l2encrecorder.h
mpegtables.h
SignalMonitorValue::SetRange
void SetRange(int _min, int _max)
Sets the minimum and maximum values.
Definition: signalmonitorvalue.h:93
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
V4L2encSignalMonitor::V4L2encSignalMonitor
V4L2encSignalMonitor(void)
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:76
DTVSignalMonitor
This class is intended to detect the presence of needed tables.
Definition: dtvsignalmonitor.h:14
V4L2encSignalMonitor::m_strength
int m_strength
Definition: v4l2encsignalmonitor.h:42
atsctables.h
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
StreamHandler::AddListener
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, const QString &output_file=QString())
Definition: streamhandler.cpp:35
V4L2encStreamHandler::Get
static V4L2encStreamHandler * Get(const QString &devname, int audioinput, int inputid)
Definition: v4l2encstreamhandler.cpp:63
V4L2util::GetStreamType
int GetStreamType(void) const
Definition: v4l2util.cpp:795
SignalMonitor::Stop
virtual void Stop()
Stop signal monitoring thread.
Definition: signalmonitor.cpp:307
V4L2encSignalMonitor::UpdateValues
void UpdateValues(void) override
Fills in frontend stats and emits status Qt signals.
Definition: v4l2encsignalmonitor.cpp:93
V4L2encSignalMonitor::m_stableTime
std::chrono::milliseconds m_stableTime
Definition: v4l2encsignalmonitor.h:43
V4LChannel
Implements tuning for TV cards using the V4L driver API, both versions 1 and 2.
Definition: v4lchannel.h:30
DTVSignalMonitor::IsAllGood
bool IsAllGood(void) const override
Definition: dtvsignalmonitor.cpp:587
V4L2encSignalMonitor::m_width
int m_width
Definition: v4l2encsignalmonitor.h:44
SignalMonitor::kDTVSigMon_WaitForVCT
static const uint64_t kDTVSigMon_WaitForVCT
Definition: signalmonitor.h:187
V4L2util::GetSignalStrength
int GetSignalStrength(void) const
Definition: v4l2util.cpp:629