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 <QTextStream>
11 
12 // MythTV headers
13 #include "libmyth/mythcontext.h"
15 
16 #include "cardutil.h"
17 #include "channelutil.h"
18 #include "externrecscanner.h"
20 #include "scanmonitor.h"
21 
22 #define LOC QString("ExternRecChanFetch: ")
23 
25  QString inputname,
26  uint sourceid,
27  ScanMonitor *monitor)
28  : m_scanMonitor(monitor)
29  , m_cardId(cardid)
30  , m_inputName(std::move(inputname))
31  , m_sourceId(sourceid)
32  , m_thread(new MThread("ExternRecChannelScanner", this))
33 {
34  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Has ScanMonitor %1")
35  .arg(monitor ? "true" : "false"));
36 }
37 
39 {
40  Stop();
41  delete m_thread;
42  m_thread = nullptr;
43 }
44 
49 {
50  m_lock.lock();
51 
52  while (m_threadRunning)
53  {
54  m_stopNow = true;
55  m_lock.unlock();
56  m_thread->wait(5ms);
57  m_lock.lock();
58  }
59 
60  m_lock.unlock();
61 
62  m_thread->wait();
63 }
64 
67 {
68  Stop();
69  m_stopNow = false;
70  m_thread->start();
71 }
72 
74 {
75  m_lock.lock();
76  m_threadRunning = true;
77  m_lock.unlock();
78 
79 
80  // Step 1/4 : Get info from DB
81  QString cmd = CardUtil::GetVideoDevice(m_cardId);
82 
83  if (m_stopNow || cmd.isEmpty())
84  {
85  LOG(VB_CHANNEL, LOG_INFO, LOC + "Invalid external command");
86  QMutexLocker locker(&m_lock);
87  m_threadRunning = false;
88  m_stopNow = true;
89  return;
90  }
91 
92  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("External Command: '%1'").arg(cmd));
93 
94 
95  // Step 2/4 : Download
96  if (m_scanMonitor)
97  {
99  m_scanMonitor->ScanAppendTextToLog(tr("Creating channel list"));
100  }
101 
103 
104  if ((m_channelTotal = fetch.LoadChannels()) < 1)
105  {
106  LOG(VB_CHANNEL, LOG_ERR, LOC + "Failed to load channels");
107  QMutexLocker locker(&m_lock);
108  m_threadRunning = false;
109  m_stopNow = true;
110  return;
111  }
112 
113  std::vector<uint> existing = ChannelUtil::GetChanIDs(m_sourceId);
114  std::vector<uint>::iterator Iold;
115 
116  // Step 3/4 : Process
117  if (m_scanMonitor)
118  m_scanMonitor->ScanAppendTextToLog(tr("Processing channels"));
119 
120  QString channum;
121  QString name;
122  QString callsign;
123  QString xmltvid;
124  QString icon;
125  int cnt = 0;
126 
127  if (!fetch.FirstChannel(channum, name, callsign, xmltvid, icon))
128  {
129  LOG(VB_CHANNEL, LOG_WARNING, LOC + "No channels found.");
130  QMutexLocker locker(&m_lock);
131  m_threadRunning = false;
132  m_stopNow = true;
133  return;
134  }
135 
136  if (m_scanMonitor)
137  m_scanMonitor->ScanAppendTextToLog(tr("Adding Channels"));
138 
139  uint idx = 0;
140  for (;;)
141  {
142  QString msg = tr("Channel #%1 : %2").arg(channum, name);
143 
144  LOG(VB_CHANNEL, LOG_INFO, QString("Handling channel %1 %2")
145  .arg(channum, name));
146 
147  int chanid = ChannelUtil::GetChanID(m_sourceId, channum);
148 
149  if (m_scanMonitor)
151 
152  if (chanid <= 0)
153  {
154  if (m_scanMonitor)
155  m_scanMonitor->ScanAppendTextToLog(tr("Adding %1").arg(msg));
156 
157  chanid = ChannelUtil::CreateChanID(m_sourceId, channum);
158  ChannelUtil::CreateChannel(0, m_sourceId, chanid, callsign, name,
159  channum, 1, 0, 0,
160  false, kChannelVisible, QString(),
161  icon, "Default", xmltvid);
162  }
163  else
164  {
165  if (m_scanMonitor)
166  m_scanMonitor->ScanAppendTextToLog(tr("Updating %1").arg(msg));
167 
168  ChannelUtil::UpdateChannel(0, m_sourceId, chanid, callsign, name,
169  channum, 1, 0, 0,
170  false, kChannelVisible, QString(),
171  icon, "Default", xmltvid);
172  }
173 
175 
176  if ((Iold = std::find(existing.begin(), existing.end(), chanid)) !=
177  existing.end())
178  {
179  existing.erase(Iold);
180  }
181 
182  if (++idx < m_channelTotal)
183  fetch.NextChannel(channum, name, callsign, xmltvid, icon);
184  else
185  break;
186  }
187 
188  // Remove any channels which are no longer valid
189  for (Iold = existing.begin(); Iold != existing.end(); ++Iold)
190  {
191  channum = ChannelUtil::GetChanNum(*Iold);
192 
193  if (m_scanMonitor)
195  (tr("Removing unused Channel #%1").arg(channum));
196 
198  }
199 
200  // Step 4/4 : Finish up
201  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Found %1 channels").arg(cnt));
202  if (m_scanMonitor)
204  (tr("Found %1 channels.").arg(cnt));
205 
206  if (m_scanMonitor)
207  {
208  m_scanMonitor->ScanAppendTextToLog(tr("Done"));
212  }
213 
214  QMutexLocker locker(&m_lock);
215  m_threadRunning = false;
216  m_stopNow = true;
217 }
218 
220 {
221  uint minval = 35;
222  uint range = 70 - minval;
223  uint pct = minval + (uint) truncf((((float)val) / m_channelCnt) * range);
224  if (m_scanMonitor)
226 }
227 
229 {
230  uint minval = 70;
231  uint range = 100 - minval;
232  uint pct = minval + (uint) truncf((((float)val) / m_channelCnt) * range);
233  if (m_scanMonitor)
235 }
236 
237 void ExternRecChannelScanner::SetMessage(const QString &status)
238 {
239  if (m_scanMonitor)
241 }
242 
243 /* 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:286
LOC
#define LOC
Definition: externrecscanner.cpp:22
ExternRecChannelScanner::~ExternRecChannelScanner
~ExternRecChannelScanner() override
Definition: externrecscanner.cpp:38
ExternRecChannelScanner::Stop
void Stop(void)
Stops the scanning thread running.
Definition: externrecscanner.cpp:48
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:303
ChannelUtil::DeleteChannel
static bool DeleteChannel(uint channel_id)
Definition: channelutil.cpp:1765
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:23
ExternRecChannelScanner::m_sourceId
uint m_sourceId
Definition: externrecscanner.h:46
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)
Definition: channelutil.cpp:1481
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:1309
mythlogging.h
ExternRecChannelScanner::SetNumChannelsParsed
void SetNumChannelsParsed(uint val)
Definition: externrecscanner.cpp:219
ExternRecChannelScanner::m_channelCnt
uint m_channelCnt
Definition: externrecscanner.h:48
ChannelUtil::CreateChanID
static int CreateChanID(uint sourceid, const QString &chan_num)
Creates a unique channel ID for database use.
Definition: channelutil.cpp:1445
ExternRecChannelScanner::ExternRecChannelScanner
ExternRecChannelScanner(uint cardid, QString inputname, uint sourceid, ScanMonitor *monitor=nullptr)
Definition: externrecscanner.cpp:24
uint
unsigned int uint
Definition: compat.h:79
ExternRecChannelScanner::Scan
void Scan(void)
Scans the list.
Definition: externrecscanner.cpp:66
ScanMonitor::ScanAppendTextToLog
void ScanAppendTextToLog(const QString &status)
Definition: scanmonitor.cpp:109
ExternalRecChannelFetcher::LoadChannels
int LoadChannels(void)
Definition: ExternalRecChannelFetcher.cpp:126
ExternRecChannelScanner::m_cardId
uint m_cardId
Definition: externrecscanner.h:44
channelutil.h
ExternalRecChannelFetcher
Definition: ExternalRecChannelFetcher.h:26
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)
Definition: channelutil.cpp:1561
ExternRecChannelScanner::SetMessage
void SetMessage(const QString &status)
Definition: externrecscanner.cpp:237
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:73
ExternRecChannelScanner::SetNumChannelsInserted
void SetNumChannelsInserted(uint val)
Definition: externrecscanner.cpp:228
externrecscanner.h
ChannelUtil::GetChanNum
static QString GetChanNum(int chan_id)
Returns the channel-number string of the given channel.
Definition: channelutil.cpp:775
ChannelUtil::GetChanIDs
static std::vector< uint > GetChanIDs(int sourceid=-1, bool onlyVisible=false)
Definition: channelutil.cpp:2133
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:357
CardUtil::GetVideoDevice
static QString GetVideoDevice(uint inputid)
Definition: cardutil.h:289