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 __MINGW32__
9#include <sys/select.h>
10#endif
11
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"
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
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");
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);
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 {
226 return 40 + m_lockCnt;
227 }
228 }
229 else
230 {
231 // Indicate that we are still waiting for a valid res, every 3 seconds.
232 if (!m_statusTime.isValid() || MythDate::current() > m_statusTime)
233 {
234 LOG(VB_CHANNEL, LOG_WARNING, "Waiting for valid resolution");
235 m_statusTime = MythDate::current().addSecs(3);
236 }
237 m_timer.stop();
238 }
239
240 m_width = width;
241 return 20 + m_lockCnt;
242}
Overall structure.
virtual QString GetDevice(void) const
Returns String representing device, useful for debugging.
Definition: channelbase.h:78
This class is intended to detect the presence of needed tables.
bool IsAllGood(void) const override
void RemoveFlags(uint64_t _flags) override
MPEGStreamData * GetStreamData()
Returns the MPEG stream data if it exists.
std::chrono::milliseconds elapsed(void)
Returns milliseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:91
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
void stop(void)
Stops timer, next call to isRunning() will return false and any calls to elapsed() or restart() will ...
Definition: mythtimer.cpp:78
void start(void)
starts measuring elapsed time.
Definition: mythtimer.cpp:47
void SetRange(int _min, int _max)
Sets the minimum and maximum values.
bool IsGood() const
Returns true if the value is equal to the threshold, or on the right side of the threshold (depends o...
void SetValue(int _value)
static const uint64_t kDTVSigMon_WaitForVCT
QRecursiveMutex m_statusLock
volatile bool m_exit
void SendMessageAllGood(void)
SignalMonitorValue m_signalLock
static const uint64_t kDTVSigMon_WaitForNIT
SignalMonitorValue m_scriptStatus
static const uint64_t kDVBSigMon_WaitForPos
Wait for rotor to complete turning the antenna.
static const uint64_t kDTVSigMon_WaitForSDT
virtual void Stop()
Stop signal monitoring thread.
volatile bool m_running
ChannelBase * m_channel
static const uint64_t kDTVSigMon_WaitForPMT
virtual void EmitStatus(void)
static const uint64_t kDTVSigMon_WaitForPAT
static const uint64_t kDTVSigMon_WaitForMGT
virtual void UpdateValues(void)
This should be overridden to actually do signal monitoring.
SignalMonitorValue m_signalStrength
bool HasAnyFlag(uint64_t _flags) const
bool HasError(void) const
Definition: streamhandler.h:66
virtual void RemoveListener(MPEGStreamData *data)
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, const QString &output_file=QString())
void UpdateValues(void) override
Fills in frontend stats and emits status Qt signals.
~V4L2encSignalMonitor() override
Stops signal monitoring and table monitoring threads.
std::chrono::milliseconds m_stableTime
int StableResolution(void)
Wait for a stable signal.
void Stop(void) override
Stop signal monitoring and table monitoring threads.
V4L2encStreamHandler * m_streamHandler
static void Return(V4L2encStreamHandler *&ref, int inputid)
static V4L2encStreamHandler * Get(const QString &devname, int audioinput, int inputid)
bool Open(const QString &dev_name, const QString &vbi_dev_name="")
Definition: v4l2util.cpp:34
bool GetResolution(int &width, int &height) const
Definition: v4l2util.cpp:654
bool IsOpen(void) const
Definition: v4l2util.h:31
int GetStreamType(void) const
Definition: v4l2util.cpp:799
int GetSignalStrength(void) const
Definition: v4l2util.cpp:633
Implements tuning for TV cards using the V4L driver API, both versions 1 and 2.
Definition: v4lchannel.h:32
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:15
#define LOC