MythTV master
v4l2encsignalmonitor.cpp
Go to the documentation of this file.
1// -*- Mode: c++ -*-
3
6
8#include "mpeg/atsctables.h"
9#include "mpeg/mpegtables.h"
10
11#include "v4lchannel.h"
12#include "v4l2encrecorder.h"
14
15#define LOC QString("V4L2SigMon[%1](%2): ").arg(m_inputid).arg(m_channel->GetDevice())
16
31 V4LChannel *_channel,
32 bool _release_stream,
33 uint64_t _flags)
34 : DTVSignalMonitor(db_cardnum, _channel, _release_stream, _flags)
35{
36 LOG(VB_CHANNEL, LOG_INFO, LOC + "ctor");
37
39 if (!m_v4l2.IsOpen())
40 {
41 LOG(VB_GENERAL, LOG_ERR, LOC + "ctor -- Open failed");
42 return;
43 }
44
45 m_isTS = (m_v4l2.GetStreamType() == V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
46
47
49 LOG(VB_CHANNEL, LOG_INFO, LOC + QString("%1 stream.")
50 .arg(m_isTS ? "Transport" : "Program"));
51}
52
57{
58 LOG(VB_CHANNEL, LOG_INFO, LOC + "dtor");
62}
63
68{
69 LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- begin");
70
74
75 LOG(VB_CHANNEL, LOG_INFO, LOC + "Stop() -- end");
76}
77
85{
86 if (!m_running || m_exit)
87 return;
88
89 if (!m_isTS)
90 {
94
96
97 {
98 QMutexLocker locker(&m_statusLock);
100 return;
101 }
102 }
103
104 if (m_streamHandler)
105 {
106 EmitStatus();
107 if (IsAllGood())
109
110 m_updateDone = true;
111 return;
112 }
113
114 bool isLocked = HasLock();
115
116 LOG(VB_CHANNEL, LOG_INFO, LOC + QString("isLocked: %1").arg(isLocked));
117
118 {
119 QMutexLocker locker(&m_statusLock);
121 m_signalLock.SetValue(isLocked ? 1 : 0);
122 }
123
124 EmitStatus();
125 if (IsAllGood())
127
128 // Start table monitoring if we are waiting on any table
129 // and we have a lock.
130 if (isLocked && !m_streamHandler && GetStreamData() &&
134 {
135 auto* chn = reinterpret_cast<V4LChannel*>(m_channel);
137 chn->GetAudioDevice().toInt(), m_inputid);
139 {
140 LOG(VB_GENERAL, LOG_ERR, LOC +
141 "V4L2encSignalMonitor -- failed to start StreamHandler.");
142 }
143 else
144 {
147 }
148 }
149
150 m_updateDone = true;
151}
152
154{
155 if (!m_v4l2.IsOpen())
156 {
157 LOG(VB_CHANNEL, LOG_INFO, LOC +
158 "GetSignalStrengthPercent() -- v4l2 device not open");
159 return false;
160 }
161
162 if (m_strength >= 0)
164 if (m_strength < 0)
165 return (true /* || StableResolution() == 100 */);
166
167 return m_strength > 50;
168}
169
180{
181 int width = 0;
182 int height = 0;
183
184 if (m_v4l2.GetResolution(width, height))
185 {
186 m_timer.stop();
187 if (width > 0)
188 {
189 LOG(VB_CHANNEL, LOG_INFO, LOC +
190 QString("Resolution already stable at %1 x %2")
191 .arg(width).arg(height));
192 return 100;
193 }
194 LOG(VB_GENERAL, LOG_ERR, LOC + "Device wedged?");
195 return 0;
196 }
197
198 if (m_width == width)
199 {
200 if (!m_timer.isRunning())
201 {
202 LOG(VB_CHANNEL, LOG_INFO, QString("Resolution %1 x %2")
203 .arg(width).arg(height));
204 if (++m_lockCnt > 9)
205 m_lockCnt = 1;
206 m_timer.start();
207 }
208 else if (m_timer.elapsed() > m_stableTime)
209 {
210 LOG(VB_CHANNEL, LOG_INFO, QString("Resolution stable at %1 x %2")
211 .arg(width).arg(height));
212 m_timer.stop();
213 return 100;
214 }
215 else
216 {
217 return 40 + m_lockCnt;
218 }
219 }
220 else
221 {
222 // Indicate that we are still waiting for a valid res, every 3 seconds.
223 if (!m_statusTime.isValid() || MythDate::current() > m_statusTime)
224 {
225 LOG(VB_CHANNEL, LOG_WARNING, "Waiting for valid resolution");
226 m_statusTime = MythDate::current().addSecs(3);
227 }
228 m_timer.stop();
229 }
230
231 m_width = width;
232 return 20 + m_lockCnt;
233}
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:655
bool IsOpen(void) const
Definition: v4l2util.h:31
int GetStreamType(void) const
Definition: v4l2util.cpp:800
int GetSignalStrength(void) const
Definition: v4l2util.cpp:634
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