MythTV  master
externrecscanner.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 // Std C headers
4 #include <cmath>
5 #include <unistd.h>
6 #include <utility>
7 
8 // Qt headers
9 #include <QFile>
10 #include <QRegularExpression>
11 #include <QTextStream>
12 
13 // MythTV headers
14 #include "libmyth/mythcontext.h"
16 
17 #include "cardutil.h"
18 #include "channelutil.h"
19 #include "externrecscanner.h"
21 #include "scanmonitor.h"
22 
23 #define LOC QString("ExternRecChanFetch: ")
24 
26  QString inputname,
27  uint sourceid,
28  ScanMonitor *monitor)
29  : m_scanMonitor(monitor)
30  , m_cardId(cardid)
31  , m_inputName(std::move(inputname))
32  , m_sourceId(sourceid)
33  , m_thread(new MThread("ExternRecChannelScanner", this))
34 {
35  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Has ScanMonitor %1")
36  .arg(monitor ? "true" : "false"));
37 }
38 
40 {
41  Stop();
42  delete m_thread;
43  m_thread = nullptr;
44 }
45 
50 {
51  m_lock.lock();
52 
53  while (m_threadRunning)
54  {
55  m_stopNow = true;
56  m_lock.unlock();
57  m_thread->wait(5ms);
58  m_lock.lock();
59  }
60 
61  m_lock.unlock();
62 
63  m_thread->wait();
64 }
65 
68 {
69  Stop();
70  m_stopNow = false;
71  m_thread->start();
72 }
73 
75 {
76  m_lock.lock();
77  m_threadRunning = true;
78  m_lock.unlock();
79 
80 
81  // Step 1/4 : Get info from DB
82  QString cmd = CardUtil::GetVideoDevice(m_cardId);
83 
84  if (m_stopNow || cmd.isEmpty())
85  {
86  LOG(VB_CHANNEL, LOG_INFO, LOC + "Invalid external command");
87  QMutexLocker locker(&m_lock);
88  m_threadRunning = false;
89  m_stopNow = true;
90  return;
91  }
92 
93  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("External Command: '%1'").arg(cmd));
94 
95 
96  // Step 2/4 : Download
97  if (m_scanMonitor)
98  {
100  m_scanMonitor->ScanAppendTextToLog(tr("Creating channel list"));
101  }
102 
104 
105  if ((m_channelTotal = fetch.LoadChannels()) < 1)
106  {
107  LOG(VB_CHANNEL, LOG_ERR, LOC + "Failed to load channels");
108  QMutexLocker locker(&m_lock);
109  m_threadRunning = false;
110  m_stopNow = true;
111  return;
112  }
113 
114  std::vector<uint> existing = ChannelUtil::GetChanIDs(m_sourceId);
115  std::vector<uint>::iterator Iold;
116 
117  // Step 3/4 : Process
118  if (m_scanMonitor)
119  m_scanMonitor->ScanAppendTextToLog(tr("Processing channels"));
120 
121  QString channum;
122  QString name;
123  QString callsign;
124  QString xmltvid;
125  QString icon;
126  int atsc_major = 0;
127  int atsc_minor = 0;
128  int cnt = 0;
129 
130  if (!fetch.FirstChannel(channum, name, callsign, xmltvid, icon))
131  {
132  LOG(VB_CHANNEL, LOG_WARNING, LOC + "No channels found.");
133  QMutexLocker locker(&m_lock);
134  m_threadRunning = false;
135  m_stopNow = true;
136  return;
137  }
138 
139  if (m_scanMonitor)
140  m_scanMonitor->ScanAppendTextToLog(tr("Adding Channels"));
141 
142  uint idx = 0;
143  for (;;)
144  {
145  QString msg = tr("Channel #%1 : %2").arg(channum, name);
146  QStringList digits = channum.split(QRegularExpression("\\D+"));
147 
148  if (digits.size() > 1)
149  {
150  atsc_major = digits.at(0).toInt();
151  atsc_minor = digits.at(1).toInt();
152  LOG(VB_CHANNEL, LOG_DEBUG, LOC +
153  QString("ATSC: %1.%2").arg(atsc_major).arg(atsc_minor));
154  }
155  else
156  {
157  atsc_major = atsc_minor = 0;
158  }
159 
160  LOG(VB_CHANNEL, LOG_INFO, QString("Handling channel %1 %2")
161  .arg(channum, name));
162 
163  int chanid = ChannelUtil::GetChanID(m_sourceId, channum);
164 
165  if (m_scanMonitor)
167 
168  if (chanid <= 0)
169  {
170  if (m_scanMonitor)
171  m_scanMonitor->ScanAppendTextToLog(tr("Adding %1").arg(msg));
172 
173  chanid = ChannelUtil::CreateChanID(m_sourceId, channum);
174  ChannelUtil::CreateChannel(0, m_sourceId, chanid, callsign, name,
175  channum, 1, atsc_major, atsc_minor,
176  false, kChannelVisible, QString(),
177  icon, "Default", xmltvid);
178  }
179  else
180  {
181  if (m_scanMonitor)
182  m_scanMonitor->ScanAppendTextToLog(tr("Updating %1").arg(msg));
183 
184  ChannelUtil::UpdateChannel(0, m_sourceId, chanid, callsign, name,
185  channum, 1, atsc_major, atsc_minor,
186  false, kChannelVisible, QString(),
187  icon, "Default", xmltvid);
188  }
189 
191 
192  if ((Iold = std::find(existing.begin(), existing.end(), chanid)) !=
193  existing.end())
194  {
195  existing.erase(Iold);
196  }
197 
198  if (++idx < m_channelTotal)
199  fetch.NextChannel(channum, name, callsign, xmltvid, icon);
200  else
201  break;
202  }
203 
204  // Remove any channels which are no longer valid
205  for (Iold = existing.begin(); Iold != existing.end(); ++Iold)
206  {
207  channum = ChannelUtil::GetChanNum(*Iold);
208 
209  if (m_scanMonitor)
211  (tr("Removing unused Channel #%1").arg(channum));
212 
214  }
215 
216  // Step 4/4 : Finish up
217  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Found %1 channels").arg(cnt));
218  if (m_scanMonitor)
220  (tr("Found %1 channels.").arg(cnt));
221 
222  if (m_scanMonitor)
223  {
224  m_scanMonitor->ScanAppendTextToLog(tr("Done"));
228  }
229 
230  QMutexLocker locker(&m_lock);
231  m_threadRunning = false;
232  m_stopNow = true;
233 }
234 
236 {
237  uint minval = 35;
238  uint range = 70 - minval;
239  uint pct = minval + (uint) truncf((((float)val) / m_channelCnt) * range);
240  if (m_scanMonitor)
242 }
243 
245 {
246  uint minval = 70;
247  uint range = 100 - minval;
248  uint pct = minval + (uint) truncf((((float)val) / m_channelCnt) * range);
249  if (m_scanMonitor)
251 }
252 
253 void ExternRecChannelScanner::SetMessage(const QString &status)
254 {
255  if (m_scanMonitor)
257 }
258 
259 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:283
LOC
#define LOC
Definition: externrecscanner.cpp:23
ExternRecChannelScanner::~ExternRecChannelScanner
~ExternRecChannelScanner() override
Definition: externrecscanner.cpp:39
ExternRecChannelScanner::Stop
void Stop(void)
Stops the scanning thread running.
Definition: externrecscanner.cpp:49
MThread::wait
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:300
ChannelUtil::DeleteChannel
static bool DeleteChannel(uint channel_id)
Definition: channelutil.cpp:1786
ExternRecChannelScanner::m_lock
QMutex m_lock
Definition: externrecscanner.h:52
ScanMonitor::ScanPercentComplete
void ScanPercentComplete(int pct)
Definition: scanmonitor.cpp:103
ExternRecChannelScanner::m_channelTotal
uint m_channelTotal
Definition: externrecscanner.h:47
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
ExternRecChannelScanner::m_sourceId
uint m_sourceId
Definition: externrecscanner.h:46
ExternRecChannelScanner::m_thread
MThread * m_thread
Definition: externrecscanner.h:51
ExternRecChannelScanner::m_stopNow
bool m_stopNow
Definition: externrecscanner.h:50
ExternRecChannelScanner::m_scanMonitor
ScanMonitor * m_scanMonitor
Definition: externrecscanner.h:43
scanmonitor.h
ChannelUtil::GetChanID
static int GetChanID(int db_mplexid, int service_transport_id, int major_channel, int minor_channel, int program_number)
Definition: channelutil.cpp:1311
mythlogging.h
ExternRecChannelScanner::SetNumChannelsParsed
void SetNumChannelsParsed(uint val)
Definition: externrecscanner.cpp:235
ExternRecChannelScanner::m_channelCnt
uint m_channelCnt
Definition: externrecscanner.h:48
ChannelUtil::UpdateChannel
static bool UpdateChannel(uint db_mplexid, uint source_id, uint channel_id, const QString &callsign, const QString &service_name, const QString &chan_num, uint service_id, uint atsc_major_channel, uint atsc_minor_channel, bool use_on_air_guide, ChannelVisibleType visible, const QString &freqid=QString(), const QString &icon=QString(), QString format=QString(), const QString &xmltvid=QString(), const QString &default_authority=QString(), uint service_type=0, int recpriority=INT_MIN, int tmOffset=INT_MIN, int commMethod=INT_MIN)
Definition: channelutil.cpp:1572
ChannelUtil::CreateChanID
static int CreateChanID(uint sourceid, const QString &chan_num)
Creates a unique channel ID for database use.
Definition: channelutil.cpp:1447
ExternRecChannelScanner::ExternRecChannelScanner
ExternRecChannelScanner(uint cardid, QString inputname, uint sourceid, ScanMonitor *monitor=nullptr)
Definition: externrecscanner.cpp:25
uint
unsigned int uint
Definition: compat.h:81
ExternRecChannelScanner::Scan
void Scan(void)
Scans the list.
Definition: externrecscanner.cpp:67
ScanMonitor::ScanAppendTextToLog
void ScanAppendTextToLog(const QString &status)
Definition: scanmonitor.cpp:109
ExternalRecChannelFetcher::LoadChannels
int LoadChannels(void)
Definition: ExternalRecChannelFetcher.cpp:126
ChannelUtil::CreateChannel
static bool CreateChannel(uint db_mplexid, uint db_sourceid, uint new_channel_id, const QString &callsign, const QString &service_name, const QString &chan_num, uint service_id, uint atsc_major_channel, uint atsc_minor_channel, bool use_on_air_guide, ChannelVisibleType visible, const QString &freqid, const QString &icon=QString(), QString format="Default", const QString &xmltvid=QString(), const QString &default_authority=QString(), uint service_type=0, int recpriority=0, int tmOffset=0, int commMethod=-1)
Definition: channelutil.cpp:1484
ExternRecChannelScanner::m_cardId
uint m_cardId
Definition: externrecscanner.h:44
channelutil.h
ExternalRecChannelFetcher
Definition: ExternalRecChannelFetcher.h:26
ExternRecChannelScanner::SetMessage
void SetMessage(const QString &status)
Definition: externrecscanner.cpp:253
ExternalRecChannelFetcher.h
ExternalRecChannelFetcher::NextChannel
bool NextChannel(QString &channum, QString &name, QString &callsign, QString &xmltvid, QString &icon)
Definition: ExternalRecChannelFetcher.h:44
kChannelVisible
@ kChannelVisible
Definition: channelinfo.h:23
cardutil.h
ExternalRecChannelFetcher::FirstChannel
bool FirstChannel(QString &channum, QString &name, QString &callsign, QString &xmltvid, QString &icon)
Definition: ExternalRecChannelFetcher.h:35
ScanMonitor::ScanComplete
void ScanComplete(void)
Definition: scanmonitor.cpp:98
std
Definition: mythchrono.h:23
mythcontext.h
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
ExternRecChannelScanner::run
void run(void) override
Definition: externrecscanner.cpp:74
ExternRecChannelScanner::SetNumChannelsInserted
void SetNumChannelsInserted(uint val)
Definition: externrecscanner.cpp:244
externrecscanner.h
ChannelUtil::GetChanNum
static QString GetChanNum(int chan_id)
Returns the channel-number string of the given channel.
Definition: channelutil.cpp:776
ChannelUtil::GetChanIDs
static std::vector< uint > GetChanIDs(int sourceid=-1, bool onlyVisible=false)
Definition: channelutil.cpp:2178
ExternRecChannelScanner::m_threadRunning
bool m_threadRunning
Definition: externrecscanner.h:49
ScanMonitor
Definition: scanmonitor.h:44
find
static pid_list_t::iterator find(const PIDInfoMap &map, pid_list_t &list, pid_list_t::iterator begin, pid_list_t::iterator end, bool find_open)
Definition: dvbstreamhandler.cpp:363
CardUtil::GetVideoDevice
static QString GetVideoDevice(uint inputid)
Definition: cardutil.h:294