MythTV  master
channelutil.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #include <algorithm>
4 #include <cstdint>
5 #include <set>
6 #include <utility>
7 
8 #include <QFile>
9 #include <QHash>
10 #include <QImage>
11 #include <QReadWriteLock>
12 #include <QRegularExpression>
13 
14 #include "libmythbase/mythdb.h"
15 #include "libmythbase/stringutil.h"
16 
17 #include "channelutil.h"
18 #include "mpeg/dvbtables.h"
20 #include "sourceutil.h"
21 
22 #define LOC QString("ChanUtil: ")
23 
24 const QString ChannelUtil::kATSCSeparators = "(_|-|#|\\.)";
25 
26 static uint get_dtv_multiplex(uint db_source_id, const QString& sistandard,
27  uint64_t frequency,
28  // DVB specific
29  uint transport_id,
30  // tsid exists with other sistandards,
31  // but we only trust it in dvb-land.
32  uint network_id,
33  // must check polarity for dvb-s
34  signed char polarity)
35 {
36  QString qstr =
37  "SELECT mplexid "
38  "FROM dtv_multiplex "
39  "WHERE sourceid = :SOURCEID "
40  " AND sistandard = :SISTANDARD ";
41 
42  if (sistandard.toLower() != "dvb")
43  qstr += "AND frequency = :FREQUENCY ";
44  else
45  {
46  qstr += "AND transportid = :TRANSPORTID ";
47  qstr += "AND networkid = :NETWORKID ";
48  qstr += "AND polarity = :POLARITY ";
49  }
50 
51 
53  query.prepare(qstr);
54 
55  query.bindValue(":SOURCEID", db_source_id);
56  query.bindValue(":SISTANDARD", sistandard);
57 
58  if (sistandard.toLower() != "dvb")
59  query.bindValue(":FREQUENCY", QString::number(frequency));
60  else
61  {
62  query.bindValue(":TRANSPORTID", transport_id);
63  query.bindValue(":NETWORKID", network_id);
64  query.bindValue(":POLARITY", QChar(polarity));
65  }
66 
67  if (!query.exec() || !query.isActive())
68  {
69  MythDB::DBError("get_dtv_multiplex", query);
70  return 0;
71  }
72 
73  if (query.next())
74  return query.value(0).toUInt();
75 
76  return 0;
77 }
78 
80  int db_source_id, const QString& sistandard,
81  uint64_t frequency, const QString& modulation,
82  // DVB specific
83  int transport_id, int network_id,
84  int symbol_rate, signed char bandwidth,
85  signed char polarity, signed char inversion,
86  signed char trans_mode,
87  const QString& inner_FEC, const QString& constellation,
88  signed char hierarchy, const QString& hp_code_rate,
89  const QString& lp_code_rate, const QString& guard_interval,
90  const QString& mod_sys, const QString& rolloff)
91 {
93 
94  // If transport is already present, skip insert
95  uint mplex = get_dtv_multiplex(
96  db_source_id, sistandard, frequency,
97  // DVB specific
98  transport_id, network_id, polarity);
99 
100  LOG(VB_CHANSCAN, LOG_INFO, "insert_dtv_multiplex(" +
101  QString("dbid:%1 std:'%2' ").arg(db_source_id).arg(sistandard) +
102  QString("freq:%1 mod:%2 ").arg(frequency).arg(modulation) +
103  QString("tid:%1 nid:%2 ").arg(transport_id).arg(network_id) +
104  QString("pol:%1 msys:%2 ...)").arg(QChar(polarity)).arg(mod_sys) +
105  QString("mplexid:%1").arg(mplex));
106 
107  bool isDVB = (sistandard.toLower() == "dvb");
108 
109  QString updateStr =
110  "UPDATE dtv_multiplex "
111  "SET frequency = :FREQUENCY1, ";
112 
113  updateStr += (!modulation.isNull()) ?
114  "modulation = :MODULATION, " : "";
115  updateStr += (symbol_rate >= 0) ?
116  "symbolrate = :SYMBOLRATE, " : "";
117  updateStr += (bandwidth >= 0) ?
118  "bandwidth = :BANDWIDTH, " : "";
119  updateStr += (polarity >= 0) ?
120  "polarity = :POLARITY, " : "";
121  updateStr += (inversion >= 0) ?
122  "inversion = :INVERSION, " : "";
123  updateStr += (trans_mode >= 0) ?
124  "transmission_mode= :TRANS_MODE, " : "";
125  updateStr += (!inner_FEC.isNull()) ?
126  "fec = :INNER_FEC, " : "";
127  updateStr += (!constellation.isNull()) ?
128  "constellation = :CONSTELLATION, " : "";
129  updateStr += (hierarchy >= 0) ?
130  "hierarchy = :HIERARCHY, " : "";
131  updateStr += (!hp_code_rate.isNull()) ?
132  "hp_code_rate = :HP_CODE_RATE, " : "";
133  updateStr += (!lp_code_rate.isNull()) ?
134  "lp_code_rate = :LP_CODE_RATE, " : "";
135  updateStr += (!guard_interval.isNull()) ?
136  "guard_interval = :GUARD_INTERVAL, " : "";
137  updateStr += (!mod_sys.isNull()) ?
138  "mod_sys = :MOD_SYS, " : "";
139  updateStr += (symbol_rate >= 0) ?
140  "rolloff = :ROLLOFF, " : "";
141  updateStr += (transport_id && !isDVB) ?
142  "transportid = :TRANSPORTID, " : "";
143 
144  updateStr = updateStr.left(updateStr.length()-2) + " ";
145 
146  updateStr +=
147  "WHERE sourceid = :SOURCEID AND "
148  " sistandard = :SISTANDARD AND ";
149 
150  updateStr += (isDVB) ?
151  " polarity = :WHEREPOLARITY AND "
152  " transportid = :TRANSPORTID AND networkid = :NETWORKID " :
153  " frequency = :FREQUENCY2 ";
154 
155  QString insertStr =
156  "INSERT INTO dtv_multiplex "
157  " (sourceid, sistandard, frequency, ";
158 
159  insertStr += (!modulation.isNull()) ? "modulation, " : "";
160  insertStr += (transport_id || isDVB) ? "transportid, " : "";
161  insertStr += (isDVB) ? "networkid, " : "";
162  insertStr += (symbol_rate >= 0) ? "symbolrate, " : "";
163  insertStr += (bandwidth >= 0) ? "bandwidth, " : "";
164  insertStr += (polarity >= 0) ? "polarity, " : "";
165  insertStr += (inversion >= 0) ? "inversion, " : "";
166  insertStr += (trans_mode >= 0) ? "transmission_mode, " : "";
167  insertStr += (!inner_FEC.isNull()) ? "fec, " : "";
168  insertStr += (!constellation.isNull()) ? "constellation, " : "";
169  insertStr += (hierarchy >= 0) ? "hierarchy, " : "";
170  insertStr += (!hp_code_rate.isNull()) ? "hp_code_rate, " : "";
171  insertStr += (!lp_code_rate.isNull()) ? "lp_code_rate, " : "";
172  insertStr += (!guard_interval.isNull()) ? "guard_interval, " : "";
173  insertStr += (!mod_sys.isNull()) ? "mod_sys, " : "";
174  insertStr += (!rolloff.isNull()) ? "rolloff, " : "";
175  insertStr = insertStr.left(insertStr.length()-2) + ") ";
176 
177  insertStr +=
178  "VALUES "
179  " (:SOURCEID, :SISTANDARD, :FREQUENCY1, ";
180  insertStr += (!modulation.isNull()) ? ":MODULATION, " : "";
181  insertStr += (transport_id || isDVB) ? ":TRANSPORTID, " : "";
182  insertStr += (isDVB) ? ":NETWORKID, " : "";
183  insertStr += (symbol_rate >= 0) ? ":SYMBOLRATE, " : "";
184  insertStr += (bandwidth >= 0) ? ":BANDWIDTH, " : "";
185  insertStr += (polarity >= 0) ? ":POLARITY, " : "";
186  insertStr += (inversion >= 0) ? ":INVERSION, " : "";
187  insertStr += (trans_mode >= 0) ? ":TRANS_MODE, " : "";
188  insertStr += (!inner_FEC.isNull()) ? ":INNER_FEC, " : "";
189  insertStr += (!constellation.isNull()) ? ":CONSTELLATION, " : "";
190  insertStr += (hierarchy >= 0) ? ":HIERARCHY, " : "";
191  insertStr += (!hp_code_rate.isNull()) ? ":HP_CODE_RATE, " : "";
192  insertStr += (!lp_code_rate.isNull()) ? ":LP_CODE_RATE, " : "";
193  insertStr += (!guard_interval.isNull()) ? ":GUARD_INTERVAL, " : "";
194  insertStr += (!mod_sys.isNull()) ? ":MOD_SYS, " : "";
195  insertStr += (!rolloff.isNull()) ? ":ROLLOFF, " : "";
196  insertStr = insertStr.left(insertStr.length()-2) + ");";
197 
198  query.prepare((mplex) ? updateStr : insertStr);
199 
200  query.bindValue(":SOURCEID", db_source_id);
201  query.bindValue(":SISTANDARD", sistandard);
202  query.bindValue(":FREQUENCY1", QString::number(frequency));
203 
204  if (mplex)
205  {
206  if (isDVB)
207  {
208  query.bindValue(":TRANSPORTID", transport_id);
209  query.bindValue(":NETWORKID", network_id);
210  query.bindValue(":WHEREPOLARITY", QChar(polarity));
211  }
212  else
213  {
214  query.bindValue(":FREQUENCY2", QString::number(frequency));
215  if (transport_id)
216  query.bindValue(":TRANSPORTID", transport_id);
217  }
218  }
219  else
220  {
221  if (transport_id || isDVB)
222  query.bindValue(":TRANSPORTID", transport_id);
223  if (isDVB)
224  query.bindValue(":NETWORKID", network_id);
225  }
226 
227  if (!modulation.isNull())
228  query.bindValue(":MODULATION", modulation);
229 
230  if (symbol_rate >= 0)
231  query.bindValue(":SYMBOLRATE", symbol_rate);
232  if (bandwidth >= 0)
233  query.bindValue(":BANDWIDTH", QString("%1").arg((char)bandwidth));
234  if (polarity >= 0)
235  query.bindValue(":POLARITY", QString("%1").arg((char)polarity));
236  if (inversion >= 0)
237  query.bindValue(":INVERSION", QString("%1").arg((char)inversion));
238  if (trans_mode >= 0)
239  query.bindValue(":TRANS_MODE", QString("%1").arg((char)trans_mode));
240 
241  if (!inner_FEC.isNull())
242  query.bindValue(":INNER_FEC", inner_FEC);
243  if (!constellation.isNull())
244  query.bindValue(":CONSTELLATION", constellation);
245  if (hierarchy >= 0)
246  query.bindValue(":HIERARCHY", QString("%1").arg((char)hierarchy));
247  if (!hp_code_rate.isNull())
248  query.bindValue(":HP_CODE_RATE", hp_code_rate);
249  if (!lp_code_rate.isNull())
250  query.bindValue(":LP_CODE_RATE", lp_code_rate);
251  if (!guard_interval.isNull())
252  query.bindValue(":GUARD_INTERVAL",guard_interval);
253  if (!mod_sys.isNull())
254  query.bindValue(":MOD_SYS", mod_sys);
255  if (!rolloff.isNull())
256  query.bindValue(":ROLLOFF", rolloff);
257 
258  if (!query.exec() || !query.isActive())
259  {
260  MythDB::DBError("Adding transport to Database.", query);
261  return 0;
262  }
263 
264  if (mplex)
265  return mplex;
266 
267  mplex = get_dtv_multiplex(
268  db_source_id, sistandard, frequency,
269  // DVB specific
270  transport_id, network_id, polarity);
271 
272  LOG(VB_CHANSCAN, LOG_INFO, QString("insert_dtv_multiplex -- ") +
273  QString("inserted mplexid %1").arg(mplex));
274 
275  return mplex;
276 }
277 
278 static void handle_transport_desc(std::vector<uint> &muxes,
279  const MPEGDescriptor &desc,
280  uint sourceid, uint tsid, uint netid)
281 {
282  uint tag = desc.DescriptorTag();
283 
285  {
287  uint64_t freq = cd.FrequencyHz();
288 
289  // Use the frequency we already have for this mplex
290  // as it may be one of the other_frequencies for this mplex
291  int mux = ChannelUtil::GetMplexID(sourceid, tsid, netid);
292  if (mux > 0)
293  {
294  QString dummy_mod;
295  QString dummy_sistd;
296  uint dummy_tsid = 0;
297  uint dummy_netid = 0;
298  ChannelUtil::GetTuningParams(mux, dummy_mod, freq,
299  dummy_tsid, dummy_netid, dummy_sistd);
300  }
301 
303  (int)sourceid, "dvb",
304  freq, QString(),
305  // DVB specific
306  (int)tsid, (int)netid,
307  -1, cd.BandwidthString().at(0).toLatin1(),
308  -1, 'a',
309  cd.TransmissionModeString().at(0).toLatin1(),
310  QString(), cd.ConstellationString(),
311  cd.HierarchyString().at(0).toLatin1(), cd.CodeRateHPString(),
313  QString(), QString());
314 
315  if (mux)
316  muxes.push_back(mux);
317 
318  /* unused
319  HighPriority()
320  IsTimeSlicingIndicatorUsed()
321  IsMPE_FECUsed()
322  NativeInterleaver()
323  Alpha()
324  */
325  }
327  {
328  const SatelliteDeliverySystemDescriptor cd(desc);
329 
331  sourceid, "dvb",
332  cd.FrequencykHz(), cd.ModulationString(),
333  // DVB specific
334  tsid, netid,
335  cd.SymbolRateHz(), -1,
336  cd.PolarizationString().at(0).toLatin1(), 'a',
337  -1,
338  cd.FECInnerString(), QString(),
339  -1, QString(),
340  QString(), QString(),
342 
343  if (mux)
344  muxes.push_back(mux);
345 
346  /* unused
347  OrbitalPositionString() == OrbitalLocation
348  */
349  }
350  else if (tag == DescriptorID::cable_delivery_system)
351  {
352  const CableDeliverySystemDescriptor cd(desc);
353 
355  sourceid, "dvb",
356  cd.FrequencyHz(), cd.ModulationString(),
357  // DVB specific
358  tsid, netid,
359  cd.SymbolRateHz(), -1,
360  -1, 'a',
361  -1,
362  cd.FECInnerString(), QString(),
363  -1, QString(),
364  QString(), QString(),
365  QString(), QString());
366 
367  if (mux)
368  muxes.push_back(mux);
369  }
370 }
371 
372 uint ChannelUtil::CreateMultiplex(int sourceid, const QString& sistandard,
373  uint64_t frequency, const QString& modulation,
374  int transport_id, int network_id)
375 {
376  return CreateMultiplex(
377  sourceid, sistandard,
378  frequency, modulation,
379  transport_id, network_id,
380  -1, -1,
381  -1, -1,
382  -1,
383  QString(), QString(),
384  -1, QString(),
385  QString(), QString(),
386  QString(), QString());
387 }
388 
390  int sourceid, const QString& sistandard,
391  uint64_t freq, const QString& modulation,
392  // DVB specific
393  int transport_id, int network_id,
394  int symbol_rate, signed char bandwidth,
395  signed char polarity, signed char inversion,
396  signed char trans_mode,
397  const QString& inner_FEC, const QString& constellation,
398  signed char hierarchy, const QString& hp_code_rate,
399  const QString& lp_code_rate, const QString& guard_interval,
400  const QString& mod_sys, const QString& rolloff)
401 {
402  return insert_dtv_multiplex(
403  sourceid, sistandard,
404  freq, modulation,
405  // DVB specific
406  transport_id, network_id,
407  symbol_rate, bandwidth,
408  polarity, inversion,
409  trans_mode,
410  inner_FEC, constellation,
411  hierarchy, hp_code_rate,
412  lp_code_rate, guard_interval,
413  mod_sys, rolloff);
414 }
415 
417  int transport_id, int network_id)
418 {
419  return insert_dtv_multiplex(
420  sourceid, mux.m_sistandard,
421  mux.m_frequency, mux.m_modulation.toString(),
422  // DVB specific
423  transport_id, network_id,
424  mux.m_symbolRate, mux.m_bandwidth.toChar().toLatin1(),
425  mux.m_polarity.toChar().toLatin1(), mux.m_inversion.toChar().toLatin1(),
426  mux.m_transMode.toChar().toLatin1(),
427  mux.m_fec.toString(), mux.m_modulation.toString(),
428  mux.m_hierarchy.toChar().toLatin1(), mux.m_hpCodeRate.toString(),
430  mux.m_modSys.toString(), mux.m_rolloff.toString());
431 }
432 
433 
438  int sourceid, const NetworkInformationTable *nit)
439 {
440  std::vector<uint> muxes;
441 
442  if (sourceid <= 0)
443  return muxes;
444 
445  for (uint i = 0; i < nit->TransportStreamCount(); ++i)
446  {
447  const desc_list_t& list =
450 
451  uint tsid = nit->TSID(i);
452  uint netid = nit->OriginalNetworkID(i);
453  for (const auto *j : list)
454  {
455  const MPEGDescriptor desc(j);
456  handle_transport_desc(muxes, desc, sourceid, tsid, netid);
457  }
458  }
459  return muxes;
460 }
461 
462 uint ChannelUtil::GetMplexID(uint sourceid, const QString &channum)
463 {
464  MSqlQuery query(MSqlQuery::InitCon());
465  /* See if mplexid is already in the database */
466  query.prepare(
467  "SELECT mplexid "
468  "FROM channel "
469  "WHERE deleted IS NULL AND "
470  " sourceid = :SOURCEID AND "
471  " channum = :CHANNUM");
472 
473  query.bindValue(":SOURCEID", sourceid);
474  query.bindValue(":CHANNUM", channum);
475 
476  if (!query.exec() || !query.isActive())
477  MythDB::DBError("GetMplexID 0", query);
478  else if (query.next())
479  return query.value(0).toInt();
480 
481  return 0;
482 }
483 
484 int ChannelUtil::GetMplexID(uint sourceid, uint64_t frequency)
485 {
486  MSqlQuery query(MSqlQuery::InitCon());
487  /* See if mplexid is already in the database */
488  query.prepare(
489  "SELECT mplexid "
490  "FROM dtv_multiplex "
491  "WHERE sourceid = :SOURCEID AND "
492  " frequency = :FREQUENCY");
493 
494  query.bindValue(":SOURCEID", sourceid);
495  query.bindValue(":FREQUENCY", QString::number(frequency));
496 
497  if (!query.exec() || !query.isActive())
498  {
499  MythDB::DBError("GetMplexID 1", query);
500  return -1;
501  }
502 
503  if (query.next())
504  return query.value(0).toInt();
505 
506  return -1;
507 }
508 
509 int ChannelUtil::GetMplexID(uint sourceid, uint64_t frequency,
510  uint transport_id, uint network_id)
511 {
512  MSqlQuery query(MSqlQuery::InitCon());
513  // See if transport already in database
514  query.prepare(
515  "SELECT mplexid "
516  "FROM dtv_multiplex "
517  "WHERE networkid = :NETWORKID AND "
518  " transportid = :TRANSPORTID AND "
519  " frequency = :FREQUENCY AND "
520  " sourceid = :SOURCEID");
521 
522  query.bindValue(":SOURCEID", sourceid);
523  query.bindValue(":NETWORKID", network_id);
524  query.bindValue(":TRANSPORTID", transport_id);
525  query.bindValue(":FREQUENCY", QString::number(frequency));
526 
527  if (!query.exec() || !query.isActive())
528  {
529  MythDB::DBError("GetMplexID 2", query);
530  return -1;
531  }
532 
533  if (query.next())
534  return query.value(0).toInt();
535 
536  return -1;
537 }
538 
540  uint transport_id, uint network_id)
541 {
542  MSqlQuery query(MSqlQuery::InitCon());
543  // See if transport already in database
544  query.prepare(
545  "SELECT mplexid "
546  "FROM dtv_multiplex "
547  "WHERE networkid = :NETWORKID AND "
548  " transportid = :TRANSPORTID AND "
549  " sourceid = :SOURCEID");
550 
551  query.bindValue(":SOURCEID", sourceid);
552  query.bindValue(":NETWORKID", network_id);
553  query.bindValue(":TRANSPORTID", transport_id);
554 
555  if (!query.exec() || !query.isActive())
556  {
557  MythDB::DBError("GetMplexID 3", query);
558  return -1;
559  }
560 
561  if (query.next())
562  return query.value(0).toInt();
563 
564  return -1;
565 }
566 
568 {
569  MSqlQuery query(MSqlQuery::InitCon());
570  /* See if mplexid is already in the database */
571  query.prepare(
572  "SELECT mplexid "
573  "FROM channel "
574  "WHERE chanid = :CHANID");
575 
576  query.bindValue(":CHANID", chanid);
577 
578  if (!query.exec())
579  MythDB::DBError("GetMplexID 4", query);
580  else if (query.next())
581  return query.value(0).toInt();
582 
583  return 0;
584 }
585 
608 // current_mplexid always exists in scanner, see ScanTransport()
609 //
610 int ChannelUtil::GetBetterMplexID(int current_mplexid,
611  int transport_id,
612  int network_id)
613 {
614  LOG(VB_CHANSCAN, LOG_INFO,
615  QString("GetBetterMplexID(mplexId %1, tId %2, netId %3)")
616  .arg(current_mplexid).arg(transport_id).arg(network_id));
617 
618  int q_networkid = 0;
619  int q_transportid = 0;
620  MSqlQuery query(MSqlQuery::InitCon());
621 
622  query.prepare("SELECT networkid, transportid "
623  "FROM dtv_multiplex "
624  "WHERE mplexid = :MPLEX_ID");
625 
626  query.bindValue(":MPLEX_ID", current_mplexid);
627 
628  if (!query.exec())
629  MythDB::DBError("Getting mplexid global search", query);
630  else if (query.next())
631  {
632  q_networkid = query.value(0).toInt();
633  q_transportid = query.value(1).toInt();
634  }
635 
636  // Got a match, return it.
637  if ((q_networkid == network_id) && (q_transportid == transport_id))
638  {
639  LOG(VB_CHANSCAN, LOG_INFO,
640  QString("GetBetterMplexID(): Returning perfect match %1")
641  .arg(current_mplexid));
642  return current_mplexid;
643  }
644 
645  // Not in DB at all, insert it
646  if (!q_networkid && !q_transportid)
647  {
648  int qsize = query.size();
649  query.prepare("UPDATE dtv_multiplex "
650  "SET networkid = :NETWORK_ID, "
651  " transportid = :TRANSPORT_ID "
652  "WHERE mplexid = :MPLEX_ID");
653 
654  query.bindValue(":NETWORK_ID", network_id);
655  query.bindValue(":TRANSPORT_ID", transport_id);
656  query.bindValue(":MPLEX_ID", current_mplexid);
657 
658  if (!query.exec())
659  MythDB::DBError("Getting mplexid global search", query);
660 
661  LOG(VB_CHANSCAN, LOG_INFO,
662  QString("GetBetterMplexID(): net id and transport id "
663  "are null, qsize(%1), Returning %2")
664  .arg(qsize).arg(current_mplexid));
665  return current_mplexid;
666  }
667 
668  // We have a partial match, so we try to do better...
669  std::array<QString,2> theQueries
670  {
671  QString("SELECT a.mplexid "
672  "FROM dtv_multiplex a, dtv_multiplex b "
673  "WHERE a.networkid = :NETWORK_ID AND "
674  " a.transportid = :TRANSPORT_ID AND "
675  " a.sourceid = b.sourceid AND "
676  " b.mplexid = :MPLEX_ID"),
677 
678  QString("SELECT mplexid "
679  "FROM dtv_multiplex "
680  "WHERE networkid = :NETWORK_ID AND "
681  " transportid = :TRANSPORT_ID"),
682  };
683 
684  for (uint i=0; i<2; i++)
685  {
686  query.prepare(theQueries[i]);
687 
688  query.bindValue(":NETWORK_ID", network_id);
689  query.bindValue(":TRANSPORT_ID", transport_id);
690  if (i == 0)
691  query.bindValue(":MPLEX_ID", current_mplexid);
692 
693  if (!query.exec() || !query.isActive())
694  MythDB::DBError("Finding matching mplexid", query);
695 
696  if (query.size() == 1 && query.next())
697  {
698  LOG(VB_CHANSCAN, LOG_INFO,
699  QString("GetBetterMplexID(): query#%1 qsize(%2) "
700  "Returning %3")
701  .arg(i).arg(query.size()).arg(current_mplexid));
702  return query.value(0).toInt();
703  }
704 
705  if (query.next())
706  {
707  int ret = (i==0) ? current_mplexid : query.value(0).toInt();
708  LOG(VB_CHANSCAN, LOG_INFO,
709  QString("GetBetterMplexID(): query#%1 qsize(%2) "
710  "Returning %3")
711  .arg(i).arg(query.size()).arg(ret));
712  return ret;
713  }
714  }
715 
716  // If you still didn't find this combo return -1 (failure)
717  LOG(VB_CHANSCAN, LOG_INFO, "GetBetterMplexID(): Returning -1");
718  return -1;
719 }
720 
722  QString &modulation,
723  uint64_t &frequency,
724  uint &dvb_transportid,
725  uint &dvb_networkid,
726  QString &si_std)
727 {
728  if (!mplexid || (mplexid == 32767)) /* 32767 deals with old lineups */
729  return false;
730 
731  MSqlQuery query(MSqlQuery::InitCon());
732  query.prepare(
733  "SELECT transportid, networkid, frequency, modulation, sistandard "
734  "FROM dtv_multiplex "
735  "WHERE mplexid = :MPLEXID");
736  query.bindValue(":MPLEXID", mplexid);
737 
738  if (!query.exec())
739  {
740  MythDB::DBError("GetTuningParams failed ", query);
741  return false;
742  }
743 
744  if (!query.next())
745  return false;
746 
747  dvb_transportid = query.value(0).toUInt();
748  dvb_networkid = query.value(1).toUInt();
749  frequency = query.value(2).toULongLong();
750  modulation = query.value(3).toString();
751  si_std = query.value(4).toString();
752 
753  return true;
754 }
755 
756 QString ChannelUtil::GetChannelStringField(int chan_id, const QString &field)
757 {
758  if (chan_id < 0)
759  return {};
760 
761  MSqlQuery query(MSqlQuery::InitCon());
762  query.prepare(QString("SELECT %1 FROM channel "
763  "WHERE chanid = :CHANID").arg(field));
764  query.bindValue(":CHANID", chan_id);
765  if (!query.exec())
766  {
767  MythDB::DBError("Selecting channel/dtv_multiplex 1", query);
768  return {};
769  }
770 
771  if (!query.next())
772  return {};
773 
774  return query.value(0).toString();
775 }
776 
777 QString ChannelUtil::GetChanNum(int chan_id)
778 {
779  return GetChannelStringField(chan_id, QString("channum"));
780 }
781 
782 std::chrono::minutes ChannelUtil::GetTimeOffset(int chan_id)
783 {
784  return std::chrono::minutes(GetChannelStringField(chan_id, QString("tmoffset")).toInt());
785 }
786 
787 int ChannelUtil::GetSourceID(int db_mplexid)
788 {
789  MSqlQuery query(MSqlQuery::InitCon());
790 
791  query.prepare("SELECT sourceid "
792  "FROM dtv_multiplex "
793  "WHERE mplexid = :MPLEXID");
794  query.bindValue(":MPLEXID", db_mplexid);
795  if (!query.exec())
796  {
797  MythDB::DBError("Selecting channel/dtv_multiplex", query);
798  return -1;
799  }
800 
801  if (query.next())
802  return query.value(0).toInt();
803 
804  return -1;
805 }
806 
808 {
809  MSqlQuery query(MSqlQuery::InitCon());
810 
811  query.prepare(
812  "SELECT sourceid "
813  "FROM channel "
814  "WHERE chanid = :CHANID");
815  query.bindValue(":CHANID", chanid);
816 
817  if (!query.exec())
818  MythDB::DBError("Selecting channel/dtv_multiplex", query);
819  else if (query.next())
820  return query.value(0).toUInt();
821 
822  return 0;
823 }
824 
825 QStringList ChannelUtil::GetInputTypes(uint chanid)
826 {
827  MSqlQuery query(MSqlQuery::InitCon());
828  query.prepare("SELECT cardtype "
829  "FROM capturecard, channel "
830  "WHERE channel.chanid = :CHANID AND "
831  " channel.sourceid = capturecard.sourceid "
832  "GROUP BY cardtype");
833  query.bindValue(":CHANID", chanid);
834 
835  QStringList list;
836  if (!query.exec())
837  {
838  MythDB::DBError("ChannelUtil::GetInputTypes", query);
839  return list;
840  }
841  while (query.next())
842  list.push_back(query.value(0).toString());
843  return list;
844 }
845 
846 static bool lt_pidcache(
847  const pid_cache_item_t a, const pid_cache_item_t b)
848 {
849  return a.GetPID() < b.GetPID();
850 }
851 
859  pid_cache_t &pid_cache)
860 {
861  MSqlQuery query(MSqlQuery::InitCon());
862  query.prepare("SELECT pid, tableid FROM pidcache "
863  "WHERE chanid = :CHANID");
864 
865  query.bindValue(":CHANID", chanid);
866 
867  if (!query.exec())
868  {
869  MythDB::DBError("GetCachedPids: fetching pids", query);
870  return false;
871  }
872 
873  while (query.next())
874  {
875  int pid = query.value(0).toInt();
876  int tid = query.value(1).toInt();
877  if ((pid >= 0) && (tid >= 0))
878  pid_cache.emplace_back(pid, tid);
879  }
880  stable_sort(pid_cache.begin(), pid_cache.end(), lt_pidcache);
881 
882  return true;
883 }
884 
892  const pid_cache_t &_pid_cache,
893  bool delete_all)
894 {
895  MSqlQuery query(MSqlQuery::InitCon());
896 
898  if (delete_all)
899  query.prepare("DELETE FROM pidcache WHERE chanid = :CHANID");
900  else
901  {
902  query.prepare(
903  "DELETE FROM pidcache "
904  "WHERE chanid = :CHANID AND tableid < 65536");
905  }
906 
907  query.bindValue(":CHANID", chanid);
908 
909  if (!query.exec())
910  {
911  MythDB::DBError("GetCachedPids -- delete", query);
912  return false;
913  }
914 
915  pid_cache_t old_cache;
916  GetCachedPids(chanid, old_cache);
917  pid_cache_t pid_cache = _pid_cache;
918  stable_sort(pid_cache.begin(), pid_cache.end(), lt_pidcache);
919 
921  query.prepare(
922  "INSERT INTO pidcache "
923  "SET chanid = :CHANID, pid = :PID, tableid = :TABLEID");
924  query.bindValue(":CHANID", chanid);
925 
926  bool ok = true;
927  auto ito = old_cache.begin();
928  for (const auto& itn : pid_cache)
929  {
930  // if old pid smaller than current new pid, skip this old pid
931  for (; ito != old_cache.end() && ito->GetPID() < itn.GetPID(); ++ito);
932 
933  // if already in DB, skip DB insert
934  if (ito != old_cache.end() && ito->GetPID() == itn.GetPID())
935  continue;
936 
937  query.bindValue(":PID", itn.GetPID());
938  query.bindValue(":TABLEID", itn.GetComposite());
939 
940  if (!query.exec())
941  {
942  MythDB::DBError("GetCachedPids -- insert", query);
943  ok = false;
944  }
945  }
946 
947  return ok;
948 }
949 
950 QString ChannelUtil::GetChannelValueStr(const QString &channel_field,
951  uint sourceid,
952  const QString &channum)
953 {
954  QString retval;
955 
956  MSqlQuery query(MSqlQuery::InitCon());
957 
958  query.prepare(
959  QString(
960  "SELECT channel.%1 "
961  "FROM channel "
962  "WHERE deleted IS NULL AND "
963  " channum = :CHANNUM AND "
964  " sourceid = :SOURCEID")
965  .arg(channel_field));
966 
967  query.bindValue(":SOURCEID", sourceid);
968  query.bindValue(":CHANNUM", channum);
969 
970  if (!query.exec() || !query.isActive())
971  MythDB::DBError("getchannelvalue", query);
972  else if (query.next())
973  retval = query.value(0).toString();
974 
975  return retval;
976 }
977 
978 int ChannelUtil::GetChannelValueInt(const QString &channel_field,
979  uint sourceid,
980  const QString &channum)
981 {
982  QString val = GetChannelValueStr(channel_field, sourceid, channum);
983 
984  int retval = 0;
985  if (!val.isEmpty())
986  retval = val.toInt();
987 
988  return (retval) ? retval : -1;
989 }
990 
992  const QString &new_channum,
993  const QString &old_channum)
994 {
995  if (new_channum.isEmpty() || old_channum.isEmpty())
996  return false;
997 
998  if (new_channum == old_channum)
999  return true;
1000 
1001  uint old_mplexid = GetMplexID(srcid, old_channum);
1002  if (!old_mplexid)
1003  return false;
1004 
1005  uint new_mplexid = GetMplexID(srcid, new_channum);
1006  if (!new_mplexid)
1007  return false;
1008 
1009  LOG(VB_CHANNEL, LOG_INFO, QString("IsOnSameMultiplex? %1==%2 -> %3")
1010  .arg(old_mplexid).arg(new_mplexid)
1011  .arg(old_mplexid == new_mplexid));
1012 
1013  return old_mplexid == new_mplexid;
1014 }
1015 
1021 static QStringList get_valid_recorder_list(uint chanid)
1022 {
1023  QStringList reclist;
1024 
1025  // Query the database to determine which source is being used currently.
1026  // set the EPG so that it only displays the channels of the current source
1027  MSqlQuery query(MSqlQuery::InitCon());
1028  // We want to get the current source id for this recorder
1029  query.prepare(
1030  "SELECT capturecard.cardid "
1031  "FROM channel "
1032  "LEFT JOIN capturecard ON channel.sourceid = capturecard.sourceid "
1033  "WHERE channel.chanid = :CHANID AND "
1034  " capturecard.livetvorder > 0 "
1035  "ORDER BY capturecard.livetvorder, capturecard.cardid");
1036  query.bindValue(":CHANID", chanid);
1037 
1038  if (!query.exec() || !query.isActive())
1039  {
1040  MythDB::DBError("get_valid_recorder_list ChanID", query);
1041  return reclist;
1042  }
1043 
1044  while (query.next())
1045  reclist << query.value(0).toString();
1046 
1047  return reclist;
1048 }
1049 
1055 static QStringList get_valid_recorder_list(const QString &channum)
1056 {
1057  QStringList reclist;
1058 
1059  // Query the database to determine which source is being used currently.
1060  // set the EPG so that it only displays the channels of the current source
1061  MSqlQuery query(MSqlQuery::InitCon());
1062  // We want to get the current source id for this recorder
1063  query.prepare(
1064  "SELECT capturecard.cardid "
1065  "FROM channel "
1066  "LEFT JOIN capturecard ON channel.sourceid = capturecard.sourceid "
1067  "WHERE channel.deleted IS NULL AND "
1068  " channel.channum = :CHANNUM AND "
1069  " capturecard.livetvorder > 0 "
1070  "ORDER BY capturecard.livetvorder, capturecard.cardid");
1071  query.bindValue(":CHANNUM", channum);
1072 
1073  if (!query.exec() || !query.isActive())
1074  {
1075  MythDB::DBError("get_valid_recorder_list ChanNum", query);
1076  return reclist;
1077  }
1078 
1079  while (query.next())
1080  reclist << query.value(0).toString();
1081 
1082  return reclist;
1083 }
1084 
1093  uint chanid, const QString &channum)
1094 {
1095  if (chanid)
1096  return get_valid_recorder_list(chanid);
1097  if (!channum.isEmpty())
1098  return get_valid_recorder_list(channum);
1099  return {};
1100 }
1101 
1102 
1103 std::vector<uint> ChannelUtil::GetConflicting(const QString &channum, uint sourceid)
1104 {
1105  MSqlQuery query(MSqlQuery::InitCon());
1106  std::vector<uint> conflicting;
1107 
1108  if (sourceid)
1109  {
1110  query.prepare(
1111  "SELECT chanid from channel "
1112  "WHERE deleted IS NULL AND "
1113  " sourceid = :SOURCEID AND "
1114  " channum = :CHANNUM");
1115  query.bindValue(":SOURCEID", sourceid);
1116  }
1117  else
1118  {
1119  query.prepare(
1120  "SELECT chanid from channel "
1121  "WHERE deleted IS NULL AND "
1122  " channum = :CHANNUM");
1123  }
1124 
1125  query.bindValue(":CHANNUM", channum);
1126  if (!query.exec())
1127  {
1128  MythDB::DBError("IsConflicting", query);
1129  conflicting.push_back(0);
1130  return conflicting;
1131  }
1132 
1133  while (query.next())
1134  conflicting.push_back(query.value(0).toUInt());
1135 
1136  return conflicting;
1137 }
1138 
1139 bool ChannelUtil::SetChannelValue(const QString &field_name,
1140  const QString& value,
1141  uint sourceid,
1142  const QString &channum)
1143 {
1144  MSqlQuery query(MSqlQuery::InitCon());
1145 
1146  query.prepare(
1147  QString("UPDATE channel SET channel.%1=:VALUE "
1148  "WHERE channel.channum = :CHANNUM AND "
1149  " channel.sourceid = :SOURCEID").arg(field_name));
1150 
1151  query.bindValue(":VALUE", value);
1152  query.bindValue(":CHANNUM", channum);
1153  query.bindValue(":SOURCEID", sourceid);
1154 
1155  return query.exec();
1156 }
1157 
1158 bool ChannelUtil::SetChannelValue(const QString &field_name,
1159  const QString& value,
1160  int chanid)
1161 {
1162  MSqlQuery query(MSqlQuery::InitCon());
1163 
1164  query.prepare(
1165  QString("UPDATE channel SET channel.%1=:VALUE "
1166  "WHERE channel.chanid = :CHANID").arg(field_name));
1167 
1168  query.bindValue(":VALUE", value);
1169  query.bindValue(":CHANID", chanid);
1170 
1171  return query.exec();
1172 }
1173 
1177 
1180 {
1181  s_channelDefaultAuthorityMapLock.lockForRead();
1182 
1184  {
1186  s_channelDefaultAuthorityMapLock.lockForWrite();
1187  // cppcheck-suppress knownConditionTrueFalse
1189  {
1190  MSqlQuery query(MSqlQuery::InitCon());
1191  query.prepare(
1192  "SELECT chanid, m.default_authority "
1193  "FROM channel c "
1194  "LEFT JOIN dtv_multiplex m "
1195  "ON (c.mplexid = m.mplexid) "
1196  "WHERE deleted IS NULL");
1197  if (query.exec())
1198  {
1199  while (query.next())
1200  {
1201  if (!query.value(1).toString().isEmpty())
1202  {
1203  s_channelDefaultAuthorityMap[query.value(0).toUInt()] =
1204  query.value(1).toString();
1205  }
1206  }
1208  }
1209  else
1210  {
1211  MythDB::DBError("GetDefaultAuthority 1", query);
1212  }
1213 
1214  query.prepare(
1215  "SELECT chanid, default_authority "
1216  "FROM channel "
1217  "WHERE deleted IS NULL");
1218  if (query.exec())
1219  {
1220  while (query.next())
1221  {
1222  if (!query.value(1).toString().isEmpty())
1223  {
1224  s_channelDefaultAuthorityMap[query.value(0).toUInt()] =
1225  query.value(1).toString();
1226  }
1227  }
1229  }
1230  else
1231  {
1232  MythDB::DBError("GetDefaultAuthority 2", query);
1233  }
1234 
1235  }
1236  }
1237 
1238  QMap<uint,QString>::iterator it = s_channelDefaultAuthorityMap.find(chanid);
1239  QString ret;
1240  if (it != s_channelDefaultAuthorityMap.end())
1241  ret = *it;
1243 
1244  return ret;
1245 }
1246 
1248 {
1249  static QReadWriteLock s_channelIconMapLock;
1250  static QHash<uint,QString> s_channelIconMap;
1251  static bool s_runInit = true;
1252 
1253  s_channelIconMapLock.lockForRead();
1254 
1255  QString ret(s_channelIconMap.value(chanid, "_cold_"));
1256 
1257  s_channelIconMapLock.unlock();
1258 
1259  if (ret != "_cold_")
1260  return ret;
1261 
1262  s_channelIconMapLock.lockForWrite();
1263 
1264  MSqlQuery query(MSqlQuery::InitCon());
1265  QString iconquery = "SELECT chanid, icon FROM channel";
1266 
1267  if (s_runInit)
1268  iconquery += " WHERE visible > 0";
1269  else
1270  iconquery += " WHERE chanid = :CHANID";
1271 
1272  query.prepare(iconquery);
1273 
1274  if (!s_runInit)
1275  query.bindValue(":CHANID", chanid);
1276 
1277  if (query.exec())
1278  {
1279  if (s_runInit)
1280  {
1281  s_channelIconMap.reserve(query.size());
1282  while (query.next())
1283  {
1284  s_channelIconMap[query.value(0).toUInt()] =
1285  query.value(1).toString();
1286  }
1287  s_runInit = false;
1288  }
1289  else
1290  {
1291  s_channelIconMap[chanid] = (query.next()) ?
1292  query.value(1).toString() : "";
1293  }
1294  }
1295  else
1296  {
1297  MythDB::DBError("GetIcon", query);
1298  }
1299 
1300  ret = s_channelIconMap.value(chanid, "");
1301 
1302  s_channelIconMapLock.unlock();
1303 
1304  return ret;
1305 }
1306 
1308 {
1309  return tr("UNKNOWN", "Synthesized callsign");
1310 }
1311 
1312 int ChannelUtil::GetChanID(int mplexid, int service_transport_id,
1313  int major_channel, int minor_channel,
1314  int program_number)
1315 {
1316  MSqlQuery query(MSqlQuery::InitCon());
1317 
1318  // find source id, so we can find manually inserted ATSC channels
1319  query.prepare("SELECT sourceid "
1320  "FROM dtv_multiplex "
1321  "WHERE mplexid = :MPLEXID");
1322  query.bindValue(":MPLEXID", mplexid);
1323  if (!query.exec())
1324  {
1325  MythDB::DBError("Selecting channel/dtv_multiplex 2", query);
1326  return -1;
1327  }
1328  if (!query.next())
1329  return -1;
1330 
1331  int source_id = query.value(0).toInt();
1332 
1333  // find a proper ATSC channel
1334  query.prepare("SELECT chanid FROM channel,dtv_multiplex "
1335  "WHERE channel.deleted IS NULL AND "
1336  " channel.sourceid = :SOURCEID AND "
1337  " atsc_major_chan = :MAJORCHAN AND "
1338  " atsc_minor_chan = :MINORCHAN AND "
1339  " dtv_multiplex.transportid = :TRANSPORTID AND "
1340  " dtv_multiplex.mplexid = :MPLEXID AND "
1341  " dtv_multiplex.sourceid = channel.sourceid AND "
1342  " dtv_multiplex.mplexid = channel.mplexid");
1343 
1344  query.bindValue(":SOURCEID", source_id);
1345  query.bindValue(":MAJORCHAN", major_channel);
1346  query.bindValue(":MINORCHAN", minor_channel);
1347  query.bindValue(":TRANSPORTID", service_transport_id);
1348  query.bindValue(":MPLEXID", mplexid);
1349 
1350  if (query.exec() && query.next())
1351  return query.value(0).toInt();
1352 
1353  // Find manually inserted/edited channels in order of scariness.
1354  // find renamed channel, where atsc is valid
1355  query.prepare("SELECT chanid FROM channel "
1356  "WHERE deleted IS NULL AND "
1357  "sourceid = :SOURCEID AND "
1358  "atsc_major_chan = :MAJORCHAN AND "
1359  "atsc_minor_chan = :MINORCHAN");
1360 
1361  query.bindValue(":SOURCEID", source_id);
1362  query.bindValue(":MAJORCHAN", major_channel);
1363  query.bindValue(":MINORCHAN", minor_channel);
1364 
1365  if (query.exec() && query.next())
1366  return query.value(0).toInt();
1367 
1368  // find based on mpeg program number and mplexid alone
1369  query.prepare("SELECT chanid FROM channel "
1370  "WHERE deleted IS NULL AND "
1371  "sourceid = :SOURCEID AND "
1372  "serviceID = :SERVICEID AND "
1373  "mplexid = :MPLEXID");
1374 
1375  query.bindValue(":SOURCEID", source_id);
1376  query.bindValue(":SERVICEID", program_number);
1377  query.bindValue(":MPLEXID", mplexid);
1378 
1379  if (query.exec() && query.next())
1380  return query.value(0).toInt();
1381 
1382  return -1;
1383 }
1384 
1385 uint ChannelUtil::FindChannel(uint sourceid, const QString &freqid)
1386 {
1387  MSqlQuery query(MSqlQuery::InitCon());
1388  query.prepare("SELECT chanid "
1389  "FROM channel "
1390  "WHERE deleted IS NULL AND "
1391  " sourceid = :SOURCEID AND "
1392  " freqid = :FREQID");
1393 
1394  query.bindValue(":SOURCEID", sourceid);
1395  query.bindValue(":FREQID", freqid);
1396 
1397  if (!query.exec() || !query.isActive())
1398  MythDB::DBError("FindChannel", query);
1399  else if (query.next())
1400  return query.value(0).toUInt();
1401 
1402  return 0;
1403 }
1404 
1405 
1406 static uint get_max_chanid(uint sourceid)
1407 {
1408  QString qstr = "SELECT MAX(chanid) FROM channel ";
1409  qstr += (sourceid) ? "WHERE sourceid = :SOURCEID" : "";
1410 
1412  query.prepare(qstr);
1413 
1414  if (sourceid)
1415  query.bindValue(":SOURCEID", sourceid);
1416 
1417  if (!query.exec() || !query.isActive())
1418  MythDB::DBError("Getting chanid for new channel (2)", query);
1419  else if (!query.next())
1420  LOG(VB_GENERAL, LOG_ERR, "Error getting chanid for new channel.");
1421  else
1422  return query.value(0).toUInt();
1423 
1424  return 0;
1425 }
1426 
1427 static bool chanid_available(uint chanid)
1428 {
1430  query.prepare(
1431  "SELECT chanid "
1432  "FROM channel "
1433  "WHERE chanid = :CHANID");
1434  query.bindValue(":CHANID", chanid);
1435 
1436  if (!query.exec() || !query.isActive())
1437  MythDB::DBError("is_chan_id_available", query);
1438  else if (query.size() == 0)
1439  return true;
1440 
1441  return false;
1442 }
1443 
1448 int ChannelUtil::CreateChanID(uint sourceid, const QString &chan_num)
1449 {
1450  // first try to base it on the channel number for human readability
1451  static const QRegularExpression kNonDigitRE { R"(\D)" };
1452  uint chanid = 0;
1453  int chansep = chan_num.indexOf(kNonDigitRE);
1454  if (chansep > 0)
1455  {
1456  chanid =
1457  sourceid * 10000 +
1458  chan_num.left(chansep).toInt() * 100 +
1459  chan_num.right(chan_num.length() - chansep - 1).toInt();
1460  }
1461  else
1462  {
1463  chanid = sourceid * 10000 + chan_num.toInt();
1464  }
1465 
1466  if ((chanid > sourceid * 10000) && (chanid_available(chanid)))
1467  return chanid;
1468 
1469  // try to at least base it on the sourceid for human readability
1470  chanid = std::max(get_max_chanid(sourceid) + 1, sourceid * 10000);
1471 
1472  if (chanid_available(chanid))
1473  return chanid;
1474 
1475  // just get a chanid we know should work
1476  chanid = get_max_chanid(0) + 1;
1477 
1478  if (chanid_available(chanid))
1479  return chanid;
1480 
1481  // failure
1482  return -1;
1483 }
1484 
1486  uint db_sourceid,
1487  uint new_channel_id,
1488  const QString &callsign,
1489  const QString &service_name,
1490  const QString &chan_num,
1491  uint service_id,
1492  uint atsc_major_channel,
1493  uint atsc_minor_channel,
1494  bool use_on_air_guide,
1495  ChannelVisibleType visible,
1496  const QString &freqid,
1497  const QString& icon,
1498  QString format,
1499  const QString& xmltvid,
1500  const QString& default_authority,
1501  uint service_type,
1502  int recpriority,
1503  int tmOffset,
1504  int commMethod )
1505 {
1506  MSqlQuery query(MSqlQuery::InitCon());
1507 
1508  QString chanNum = (chan_num == "-1") ?
1509  QString::number(service_id) : chan_num;
1510 
1511  QString qstr =
1512  "INSERT INTO channel "
1513  " (chanid, channum, sourceid, "
1514  " callsign, name, serviceid, ";
1515  qstr += (db_mplexid > 0) ? "mplexid, " : "";
1516  qstr += (!freqid.isEmpty()) ? "freqid, " : "";
1517  qstr +=
1518  " atsc_major_chan, atsc_minor_chan, "
1519  " useonairguide, visible, tvformat, "
1520  " icon, xmltvid, default_authority, "
1521  " service_type, recpriority, tmoffset, "
1522  " commmethod ) "
1523  "VALUES "
1524  " (:CHANID, :CHANNUM, :SOURCEID, "
1525  " :CALLSIGN, :NAME, :SERVICEID, ";
1526  qstr += (db_mplexid > 0) ? ":MPLEXID, " : "";
1527  qstr += (!freqid.isEmpty()) ? ":FREQID, " : "";
1528  qstr +=
1529  " :MAJORCHAN, :MINORCHAN, "
1530  " :USEOAG, :VISIBLE, :TVFORMAT, "
1531  " :ICON, :XMLTVID, :AUTHORITY, "
1532  " :SERVICETYPE, :RECPRIORITY, :TMOFFSET, "
1533  " :COMMMETHOD ) ";
1534 
1535  query.prepare(qstr);
1536 
1537  query.bindValue (":CHANID", new_channel_id);
1538  query.bindValueNoNull(":CHANNUM", chanNum);
1539  query.bindValue (":SOURCEID", db_sourceid);
1540  query.bindValueNoNull(":CALLSIGN", callsign);
1541  query.bindValueNoNull(":NAME", service_name);
1542 
1543  if (db_mplexid > 0)
1544  query.bindValue(":MPLEXID", db_mplexid);
1545 
1546  query.bindValue(":SERVICEID", service_id);
1547  query.bindValue(":MAJORCHAN", atsc_major_channel);
1548  query.bindValue(":MINORCHAN", atsc_minor_channel);
1549  query.bindValue(":USEOAG", use_on_air_guide);
1550  query.bindValue(":VISIBLE", visible);
1551 
1552  if (!freqid.isEmpty())
1553  query.bindValue(":FREQID", freqid);
1554 
1555  QString tvformat = (atsc_minor_channel > 0) ? "ATSC" : std::move(format);
1556  query.bindValueNoNull(":TVFORMAT", tvformat);
1557  query.bindValueNoNull(":ICON", icon);
1558  query.bindValueNoNull(":XMLTVID", xmltvid);
1559  query.bindValueNoNull(":AUTHORITY", default_authority);
1560  query.bindValue (":SERVICETYPE", service_type);
1561  query.bindValue (":RECPRIORITY", recpriority);
1562  query.bindValue (":TMOFFSET", tmOffset);
1563  query.bindValue (":COMMMETHOD", commMethod);
1564 
1565  if (!query.exec() || !query.isActive())
1566  {
1567  MythDB::DBError("Adding Service", query);
1568  return false;
1569  }
1570  return true;
1571 }
1572 
1574  uint source_id,
1575  uint channel_id,
1576  const QString &callsign,
1577  const QString &service_name,
1578  const QString &chan_num,
1579  uint service_id,
1580  uint atsc_major_channel,
1581  uint atsc_minor_channel,
1582  bool use_on_air_guide,
1583  ChannelVisibleType visible,
1584  const QString& freqid,
1585  const QString& icon,
1586  QString format,
1587  const QString& xmltvid,
1588  const QString& default_authority,
1589  uint service_type,
1590  int recpriority,
1591  int tmOffset,
1592  int commMethod )
1593 {
1594  if (!channel_id)
1595  return false;
1596 
1597  QString tvformat = (atsc_minor_channel > 0) ? "ATSC" : std::move(format);
1598  bool set_channum = !chan_num.isEmpty() && chan_num != "-1";
1599  QString qstr = QString(
1600  "UPDATE channel "
1601  "SET %1 %2 %3 %4 %5 %6 %7 %8 %9 "
1602  " mplexid = :MPLEXID, serviceid = :SERVICEID, "
1603  " atsc_major_chan = :MAJORCHAN, atsc_minor_chan = :MINORCHAN, "
1604  " callsign = :CALLSIGN, name = :NAME, "
1605  " sourceid = :SOURCEID, useonairguide = :USEOAG, "
1606  " visible = :VISIBLE, service_type = :SERVICETYPE "
1607  "WHERE chanid=:CHANID")
1608  .arg((!set_channum) ? "" : "channum = :CHANNUM, ",
1609  (freqid.isNull()) ? "" : "freqid = :FREQID, ",
1610  (icon.isNull()) ? "" : "icon = :ICON, ",
1611  (tvformat.isNull()) ? "" : "tvformat = :TVFORMAT, ",
1612  (xmltvid.isNull()) ? "" : "xmltvid = :XMLTVID, ",
1613  (default_authority.isNull()) ?
1614  "" : "default_authority = :AUTHORITY,",
1615  (recpriority == INT_MIN) ? "" : "recpriority = :RECPRIORITY, ",
1616  (tmOffset == INT_MIN) ? "" : "tmOffset = :TMOFFSET, ",
1617  (commMethod == INT_MIN) ? "" : "commmethod = :COMMMETHOD, ");
1618 
1619  MSqlQuery query(MSqlQuery::InitCon());
1620  query.prepare(qstr);
1621 
1622  query.bindValue(":CHANID", channel_id);
1623 
1624  if (set_channum)
1625  query.bindValue(":CHANNUM", chan_num);
1626 
1627  query.bindValue (":SOURCEID", source_id);
1628  query.bindValueNoNull(":CALLSIGN", callsign);
1629  query.bindValueNoNull(":NAME", service_name);
1630 
1631  query.bindValue(":MPLEXID", db_mplexid);
1632  query.bindValue(":SERVICEID", service_id);
1633  query.bindValue(":MAJORCHAN", atsc_major_channel);
1634  query.bindValue(":MINORCHAN", atsc_minor_channel);
1635  query.bindValue(":USEOAG", use_on_air_guide);
1636  query.bindValue(":VISIBLE", visible);
1637  query.bindValue(":SERVICETYPE", service_type);
1638 
1639  if (!freqid.isNull())
1640  query.bindValue(":FREQID", freqid);
1641  if (!tvformat.isNull())
1642  query.bindValue(":TVFORMAT", tvformat);
1643  if (!icon.isNull())
1644  query.bindValue(":ICON", icon);
1645  if (!xmltvid.isNull())
1646  query.bindValue(":XMLTVID", xmltvid);
1647  if (!default_authority.isNull())
1648  query.bindValue(":AUTHORITY", default_authority);
1649  if (recpriority != INT_MIN)
1650  query.bindValue(":RECPRIORITY", recpriority);
1651  if (tmOffset != INT_MIN)
1652  query.bindValue(":TMOFFSET", tmOffset);
1653  if (commMethod != INT_MIN)
1654  query.bindValue(":COMMMETHOD", commMethod);
1655 
1656  if (!query.exec())
1657  {
1658  MythDB::DBError("Updating Service", query);
1659  return false;
1660  }
1661  return true;
1662 }
1663 
1665 {
1666  MSqlQuery query(MSqlQuery::InitCon());
1667  query.prepare(
1668  "SELECT channum "
1669  "FROM channel "
1670  "WHERE chanid = :ID");
1671  query.bindValue(":ID", chan.m_channelId);
1672 
1673  if (!query.exec())
1674  {
1675  MythDB::DBError("UpdateChannelNumberFromDB", query);
1676  return;
1677  }
1678 
1679  if (query.next())
1680  {
1681  QString channum = query.value(0).toString();
1682 
1683  if (!channum.isEmpty())
1684  {
1685  chan.m_chanNum = channum;
1686  }
1687  }
1688 }
1689 
1691 {
1692  MSqlQuery query(MSqlQuery::InitCon());
1693  query.prepare(
1694  "SELECT xmltvid, useonairguide, visible "
1695  "FROM channel "
1696  "WHERE chanid = :ID");
1697  query.bindValue(":ID", chan.m_channelId);
1698 
1699  if (!query.exec())
1700  {
1701  MythDB::DBError("UpdateInsertInfoFromDB", query);
1702  return;
1703  }
1704 
1705  if (query.next())
1706  {
1707  QString xmltvid = query.value(0).toString();
1708  bool useeit = query.value(1).toBool();
1709  ChannelVisibleType visible =
1710  static_cast<ChannelVisibleType>(query.value(2).toInt());
1711 
1712  if (!xmltvid.isEmpty())
1713  {
1714  if (useeit)
1715  {
1716  LOG(VB_GENERAL, LOG_ERR,
1717  "Using EIT and xmltv for the same channel "
1718  "is an unsupported configuration.");
1719  }
1720  chan.m_xmltvId = xmltvid;
1721  }
1722  chan.m_useOnAirGuide = useeit;
1723  chan.m_hidden = (visible == kChannelNotVisible ||
1724  visible == kChannelNeverVisible);
1725  chan.m_visible = visible;
1726  }
1727 }
1728 
1730  uint channel_id, const IPTVTuningData &tuning)
1731 {
1732  MSqlQuery query(MSqlQuery::InitCon());
1733 
1734  query.prepare(
1735  "DELETE FROM iptv_channel "
1736  "WHERE chanid=:CHANID");
1737  query.bindValue(":CHANID", channel_id);
1738 
1739  if (!query.exec())
1740  {
1741  MythDB::DBError("UpdateIPTVTuningData -- delete", query);
1742  return false;
1743  }
1744 
1745  query.prepare(
1746  "INSERT INTO iptv_channel (chanid, url, type, bitrate) "
1747  "VALUES (:CHANID, :URL, :TYPE, :BITRATE)");
1748  query.bindValue(":CHANID", channel_id);
1749 
1750  query.bindValue(":URL", tuning.GetDataURL().toString());
1751  query.bindValue(":TYPE", tuning.GetFECTypeString(0));
1752  query.bindValue(":BITRATE", tuning.GetBitrate(0));
1753 
1754  if (!query.exec())
1755  {
1756  MythDB::DBError("UpdateIPTVTuningData -- data", query);
1757  return false;
1758  }
1759 
1760  if (tuning.GetFECURL0().port() >= 0)
1761  {
1762  query.bindValue(":URL", tuning.GetFECURL0().toString());
1763  query.bindValue(":TYPE", tuning.GetFECTypeString(1));
1764  query.bindValue(":BITRATE", tuning.GetBitrate(1));
1765  if (!query.exec())
1766  {
1767  MythDB::DBError("UpdateIPTVTuningData -- fec 0", query);
1768  return false;
1769  }
1770  }
1771 
1772  if (tuning.GetFECURL1().port() >= 0)
1773  {
1774  query.bindValue(":URL", tuning.GetFECURL1().toString());
1775  query.bindValue(":TYPE", tuning.GetFECTypeString(2));
1776  query.bindValue(":BITRATE", tuning.GetBitrate(2));
1777  if (!query.exec())
1778  {
1779  MythDB::DBError("UpdateIPTVTuningData -- fec 1", query);
1780  return false;
1781  }
1782  }
1783 
1784  return true;
1785 }
1786 
1788 {
1789  MSqlQuery query(MSqlQuery::InitCon());
1790  query.prepare(
1791  "UPDATE channel "
1792  "SET deleted = NOW() "
1793  "WHERE chanid = :ID");
1794  query.bindValue(":ID", channel_id);
1795 
1796  if (!query.exec())
1797  {
1798  MythDB::DBError("Delete Channel", query);
1799  return false;
1800  }
1801 
1802  query.prepare(
1803  "DELETE FROM iptv_channel "
1804  "WHERE chanid = :ID");
1805  query.bindValue(":ID", channel_id);
1806 
1807  if (!query.exec())
1808  {
1809  MythDB::DBError("Delete Channel 2", query);
1810  return false;
1811  }
1812 
1813  return true;
1814 }
1815 
1817 {
1818  MSqlQuery query(MSqlQuery::InitCon());
1819  query.prepare(
1820  "UPDATE channel "
1821  "SET visible = :VISIBLE "
1822  "WHERE chanid = :ID");
1823  query.bindValue(":ID", channel_id);
1824  query.bindValue(":VISIBLE", visible);
1825 
1826  if (!query.exec())
1827  {
1828  MythDB::DBError("ChannelUtil::SetVisible", query);
1829  return false;
1830  }
1831 
1832  return true;
1833 }
1834 
1836 {
1837  MSqlQuery query(MSqlQuery::InitCon());
1838 
1839  query.prepare("UPDATE dtv_multiplex "
1840  "SET serviceversion = :VERSION "
1841  "WHERE mplexid = :MPLEXID");
1842 
1843  query.bindValue(":VERSION", version);
1844  query.bindValue(":MPLEXID", mplexid);
1845 
1846  if (!query.exec())
1847  {
1848  MythDB::DBError("Selecting channel/dtv_multiplex", query);
1849  return false;
1850  }
1851  return true;
1852 }
1853 
1855 {
1856  MSqlQuery query(MSqlQuery::InitCon());
1857 
1858  query.prepare("SELECT serviceversion "
1859  "FROM dtv_multiplex "
1860  "WHERE mplexid = :MPLEXID");
1861 
1862  query.bindValue(":MPLEXID", mplexid);
1863 
1864  if (!query.exec())
1865  {
1866  MythDB::DBError("Selecting channel/dtv_multiplex", query);
1867  return 0;
1868  }
1869 
1870  if (query.next())
1871  return query.value(0).toInt();
1872 
1873  return -1;
1874 }
1875 
1876 bool ChannelUtil::GetATSCChannel(uint sourceid, const QString &channum,
1877  uint &major, uint &minor)
1878 {
1879  major = minor = 0;
1880 
1881  MSqlQuery query(MSqlQuery::InitCon());
1882  query.prepare(
1883  "SELECT atsc_major_chan, atsc_minor_chan "
1884  "FROM channel "
1885  "WHERE deleted IS NULL AND "
1886  " channum = :CHANNUM AND "
1887  " sourceid = :SOURCEID");
1888 
1889  query.bindValue(":SOURCEID", sourceid);
1890  query.bindValue(":CHANNUM", channum);
1891 
1892  if (!query.exec() || !query.isActive())
1893  MythDB::DBError("getatscchannel", query);
1894  else if (query.next())
1895  {
1896  major = query.value(0).toUInt();
1897  minor = query.value(1).toUInt();
1898  return true;
1899  }
1900 
1901  return false;
1902 }
1903 
1905  uint sourceid,
1906  uint &chanid, const QString &channum,
1907  QString &tvformat, QString &modulation,
1908  QString &freqtable, QString &freqid,
1909  int &finetune, uint64_t &frequency,
1910  QString &dtv_si_std, int &mpeg_prog_num,
1911  uint &atsc_major, uint &atsc_minor,
1912  uint &dvb_transportid, uint &dvb_networkid,
1913  uint &mplexid,
1914  bool &commfree)
1915 {
1916  chanid = 0;
1917  tvformat.clear();
1918  modulation.clear();
1919  freqtable.clear();;
1920  freqid.clear();
1921  dtv_si_std.clear();
1922  finetune = 0;
1923  frequency = 0;
1924  mpeg_prog_num = -1;
1925  atsc_major = atsc_minor = mplexid = 0;
1926  dvb_networkid = dvb_transportid = 0;
1927  commfree = false;
1928 
1929  int found = 0;
1930  MSqlQuery query(MSqlQuery::InitCon());
1931  query.prepare(
1932  "SELECT finetune, freqid, tvformat, freqtable, "
1933  " commmethod, mplexid, "
1934  " atsc_major_chan, atsc_minor_chan, serviceid, "
1935  " chanid, visible "
1936  "FROM channel, videosource "
1937  "WHERE channel.deleted IS NULL AND "
1938  " videosource.sourceid = channel.sourceid AND "
1939  " channum = :CHANNUM AND "
1940  " channel.sourceid = :SOURCEID "
1941  "ORDER BY channel.visible > 0 DESC, channel.chanid ");
1942  query.bindValue(":CHANNUM", channum);
1943  query.bindValue(":SOURCEID", sourceid);
1944 
1945  if (!query.exec() || !query.isActive())
1946  {
1947  MythDB::DBError("GetChannelData", query);
1948  return false;
1949  }
1950 
1951  if (query.next())
1952  {
1953  finetune = query.value(0).toInt();
1954  freqid = query.value(1).toString();
1955  tvformat = query.value(2).toString();
1956  freqtable = query.value(3).toString();
1957  commfree = (query.value(4).toInt() == -2);
1958  mplexid = query.value(5).toUInt();
1959  atsc_major = query.value(6).toUInt();
1960  atsc_minor = query.value(7).toUInt();
1961  mpeg_prog_num = (query.value(8).isNull()) ? -1
1962  : query.value(8).toInt();
1963  chanid = query.value(9).toUInt();
1964 
1965  if (query.value(10).toInt() > kChannelNotVisible)
1966  found++;
1967  }
1968 
1969  while (query.next())
1970  if (query.value(10).toInt() > kChannelNotVisible)
1971  found++;
1972 
1973  if (found == 0 && chanid)
1974  {
1975  LOG(VB_GENERAL, LOG_WARNING,
1976  QString("No visible channels for %1, using invisble chanid %2")
1977  .arg(channum).arg(chanid));
1978  }
1979 
1980  if (found > 1)
1981  {
1982  LOG(VB_GENERAL, LOG_WARNING,
1983  QString("Found multiple visible channels for %1, using chanid %2")
1984  .arg(channum).arg(chanid));
1985  }
1986 
1987  if (!chanid)
1988  {
1989  LOG(VB_GENERAL, LOG_ERR,
1990  QString("Could not find channel '%1' in DB for source %2 '%3'.")
1991  .arg(channum).arg(sourceid).arg(SourceUtil::GetSourceName(sourceid)));
1992  return false;
1993  }
1994 
1995  if (!mplexid || (mplexid == 32767)) /* 32767 deals with old lineups */
1996  return true;
1997 
1998  return GetTuningParams(mplexid, modulation, frequency,
1999  dvb_transportid, dvb_networkid, dtv_si_std);
2000 }
2001 
2003 {
2004  MSqlQuery query(MSqlQuery::InitCon());
2005  query.prepare(
2006  "SELECT type+0, url, bitrate "
2007  "FROM iptv_channel "
2008  "WHERE chanid = :CHANID "
2009  "ORDER BY type+0");
2010  query.bindValue(":CHANID", chanid);
2011 
2012  if (!query.exec())
2013  {
2014  MythDB::DBError("GetChannelData -- iptv", query);
2015  return {};
2016  }
2017 
2018  QString data_url;
2019  QString fec_url0;
2020  QString fec_url1;
2022  std::array<uint,3> bitrate { 0, 0, 0, };
2023  while (query.next())
2024  {
2026  query.value(0).toUInt();
2027  switch (type)
2028  {
2029  case IPTVTuningData::kData:
2030  data_url = query.value(1).toString();
2031  bitrate[0] = query.value(2).toUInt();
2032  break;
2036  fec_url0 = query.value(1).toString();
2037  bitrate[1] = query.value(2).toUInt();
2038  break;
2042  fec_url1 = query.value(1).toString();
2043  bitrate[2] = query.value(2).toUInt();
2044  break;
2045  }
2046  switch (type)
2047  {
2048  case IPTVTuningData::kData:
2049  break;
2051  fec_type = IPTVTuningData::kRFC2733;
2052  break;
2054  fec_type = IPTVTuningData::kRFC5109;
2055  break;
2057  fec_type = IPTVTuningData::kSMPTE2022;
2058  break;
2062  break; // will be handled by type of first FEC stream
2063  }
2064  }
2065 
2066  IPTVTuningData tuning(data_url, bitrate[0], fec_type,
2067  fec_url0, bitrate[1], fec_url1, bitrate[2]);
2068  LOG(VB_GENERAL, LOG_INFO, QString("Loaded %1 for %2")
2069  .arg(tuning.GetDeviceName()).arg(chanid));
2070  return tuning;
2071 }
2072 
2073 // TODO This should be modified to load a complete channelinfo object including
2074 // all fields from the database
2079  uint sourceid, bool visible_only, bool include_disconnected,
2080  const QString &group_by, uint channel_groupid)
2081 {
2082  ChannelInfoList list;
2083 
2084  MSqlQuery query(MSqlQuery::InitCon());
2085 
2086  QString qstr = QString(
2087  "SELECT videosource.sourceid, GROUP_CONCAT(capturecard.cardid) "
2088  "FROM videosource "
2089  "%1 JOIN capturecard ON capturecard.sourceid = videosource.sourceid "
2090  "GROUP BY videosource.sourceid")
2091  .arg((include_disconnected) ? "LEFT" : "");
2092 
2093  query.prepare(qstr);
2094  if (!query.exec())
2095  {
2096  MythDB::DBError("ChannelUtil::GetChannels()", query);
2097  return list;
2098  }
2099 
2100  QMap<uint, QList<uint>> inputIdLists;
2101  while (query.next())
2102  {
2103  uint qSourceId = query.value(0).toUInt();
2104  QList<uint> &inputIdList = inputIdLists[qSourceId];
2105  QStringList inputIds = query.value(1).toString().split(",");
2106  while (!inputIds.isEmpty())
2107  inputIdList.append(inputIds.takeFirst().toUInt());
2108  }
2109 
2110  qstr = QString(
2111  "SELECT channum, callsign, channel.chanid, "
2112  " atsc_major_chan, atsc_minor_chan, "
2113  " name, icon, mplexid, visible, "
2114  " channel.sourceid, "
2115  " GROUP_CONCAT(DISTINCT channelgroup.grpid), "
2116  " xmltvid "
2117  "FROM channel "
2118  "LEFT JOIN channelgroup ON channel.chanid = channelgroup.chanid ");
2119 
2120  qstr += "WHERE deleted IS NULL ";
2121 
2122  if (sourceid)
2123  qstr += QString("AND channel.sourceid='%1' ").arg(sourceid);
2124 
2125  // Select only channels from the specified channel group
2126  if (channel_groupid > 0)
2127  qstr += QString("AND channelgroup.grpid = '%1' ").arg(channel_groupid);
2128 
2129  if (visible_only)
2130  qstr += QString("AND visible > 0 ");
2131 
2132  qstr += " GROUP BY chanid";
2133 
2134  if (!group_by.isEmpty())
2135  qstr += QString(", %1").arg(group_by);
2136 
2137  query.prepare(qstr);
2138  if (!query.exec())
2139  {
2140  MythDB::DBError("ChannelUtil::GetChannels()", query);
2141  return list;
2142  }
2143 
2144  while (query.next())
2145  {
2146  if (query.value(0).toString().isEmpty() || !query.value(2).toBool())
2147  continue; // skip if channum blank, or chanid empty
2148 
2149  uint qSourceID = query.value(9).toUInt();
2150  ChannelInfo chan(
2151  query.value(0).toString(), /* channum */
2152  query.value(1).toString(), /* callsign */
2153  query.value(2).toUInt(), /* chanid */
2154  query.value(3).toUInt(), /* ATSC major */
2155  query.value(4).toUInt(), /* ATSC minor */
2156  query.value(7).toUInt(), /* mplexid */
2157  static_cast<ChannelVisibleType>(query.value(8).toInt()),
2158  /* visible */
2159  query.value(5).toString(), /* name */
2160  query.value(6).toString(), /* icon */
2161  qSourceID); /* sourceid */
2162 
2163  chan.m_xmltvId = query.value(11).toString(); /* xmltvid */
2164 
2165  for (auto inputId : std::as_const(inputIdLists[qSourceID]))
2166  chan.AddInputId(inputId);
2167 
2168  QStringList groupIDs = query.value(10).toString().split(",");
2169  while (!groupIDs.isEmpty())
2170  chan.AddGroupId(groupIDs.takeFirst().toUInt());
2171 
2172  list.push_back(chan);
2173 
2174  }
2175 
2176  return list;
2177 }
2178 
2179 std::vector<uint> ChannelUtil::GetChanIDs(int sourceid, bool onlyVisible)
2180 {
2181  MSqlQuery query(MSqlQuery::InitCon());
2182 
2183  QString select = "SELECT chanid FROM channel WHERE deleted IS NULL ";
2184  // Yes, this a little ugly
2185  if (onlyVisible || sourceid > 0)
2186  {
2187  if (onlyVisible)
2188  select += "AND visible > 0 ";
2189  if (sourceid > 0)
2190  select += "AND sourceid=" + QString::number(sourceid);
2191  }
2192 
2193  std::vector<uint> list;
2194  query.prepare(select);
2195  if (!query.exec())
2196  {
2197  MythDB::DBError("SourceUtil::GetChanIDs()", query);
2198  return list;
2199  }
2200 
2201  while (query.next())
2202  list.push_back(query.value(0).toUInt());
2203 
2204  return list;
2205 }
2206 
2207 inline bool lt_callsign(const ChannelInfo &a, const ChannelInfo &b)
2208 {
2210 }
2211 
2212 inline bool lt_smart(const ChannelInfo &a, const ChannelInfo &b)
2213 {
2214  static QMutex s_sepExprLock;
2215  static const QRegularExpression kSepExpr(ChannelUtil::kATSCSeparators);
2216 
2217  bool isIntA = false;
2218  bool isIntB = false;
2219  int a_int = a.m_chanNum.toUInt(&isIntA);
2220  int b_int = b.m_chanNum.toUInt(&isIntB);
2221  int a_major = a.m_atscMajorChan;
2222  int b_major = b.m_atscMajorChan;
2223  int a_minor = a.m_atscMinorChan;
2224  int b_minor = b.m_atscMinorChan;
2225 
2226  // Extract minor and major numbers from channum..
2227  int idxA = 0;
2228  int idxB = 0;
2229  {
2230  QMutexLocker locker(&s_sepExprLock);
2231  idxA = a.m_chanNum.indexOf(kSepExpr);
2232  idxB = b.m_chanNum.indexOf(kSepExpr);
2233  }
2234  if (idxA >= 0)
2235  {
2236  bool tmp1 = false;
2237  bool tmp2 = false;
2238  int major = a.m_chanNum.left(idxA).toUInt(&tmp1);
2239  int minor = a.m_chanNum.mid(idxA+1).toUInt(&tmp2);
2240  if (tmp1 && tmp2)
2241  (a_major = major), (a_minor = minor), (isIntA = false);
2242  }
2243 
2244  if (idxB >= 0)
2245  {
2246  bool tmp1 = false;
2247  bool tmp2 = false;
2248  int major = b.m_chanNum.left(idxB).toUInt(&tmp1);
2249  int minor = b.m_chanNum.mid(idxB+1).toUInt(&tmp2);
2250  if (tmp1 && tmp2)
2251  (b_major = major), (b_minor = minor), (isIntB = false);
2252  }
2253 
2254  // If ATSC channel has been renumbered, sort by new channel number
2255  if ((a_minor > 0) && isIntA)
2256  {
2257  int atsc_int = (QString("%1%2").arg(a_major).arg(a_minor)).toInt();
2258  a_minor = (atsc_int == a_int) ? a_minor : 0;
2259  }
2260 
2261  if ((b_minor > 0) && isIntB)
2262  {
2263  int atsc_int = (QString("%1%2").arg(b_major).arg(b_minor)).toInt();
2264  b_minor = (atsc_int == b_int) ? b_minor : 0;
2265  }
2266 
2267  // one of the channels is an ATSC channel, and the other
2268  // is either ATSC or is numeric.
2269  if ((a_minor || b_minor) &&
2270  (a_minor || isIntA) && (b_minor || isIntB))
2271  {
2272  int a_maj = (!a_minor && isIntA) ? a_int : a_major;
2273  int b_maj = (!b_minor && isIntB) ? b_int : b_major;
2274  int cmp = a_maj - b_maj;
2275  if (cmp != 0)
2276  return cmp < 0;
2277 
2278  cmp = a_minor - b_minor;
2279  if (cmp != 0)
2280  return cmp < 0;
2281  }
2282 
2283  if (isIntA && isIntB)
2284  {
2285  // both channels have a numeric channum
2286  int cmp = a_int - b_int;
2287  if (cmp)
2288  return cmp < 0;
2289  }
2290  else if (isIntA ^ isIntB)
2291  {
2292  // if only one is channel numeric always consider it less than
2293  return isIntA;
2294  }
2295  else
2296  {
2297  // neither of channels have a numeric channum
2299  if (cmp)
2300  return cmp < 0;
2301  }
2302 
2303  return lt_callsign(a,b);
2304 }
2305 
2307 {
2308  MSqlQuery query(MSqlQuery::InitCon());
2309  QString select;
2310 
2311 
2312  select = "SELECT chanid FROM channel WHERE deleted IS NULL ";
2313  if (sourceid >= 0)
2314  select += "AND sourceid=" + QString::number(sourceid);
2315  select += ';';
2316 
2317  query.prepare(select);
2318 
2319  if (!query.exec() || !query.isActive())
2320  return 0;
2321 
2322  return query.size();
2323 }
2324 
2325 void ChannelUtil::SortChannels(ChannelInfoList &list, const QString &order,
2326  bool eliminate_duplicates)
2327 {
2328  bool cs = order.toLower() == "callsign";
2329  if (cs)
2330  stable_sort(list.begin(), list.end(), lt_callsign);
2331  else /* if (sortorder == "channum") */
2332  stable_sort(list.begin(), list.end(), lt_smart);
2333 
2334  if (eliminate_duplicates && !list.empty())
2335  {
2337  tmp.push_back(list[0]);
2338  for (size_t i = 1; i < list.size(); i++)
2339  {
2340  if ((cs && lt_callsign(tmp.back(), list[i])) ||
2341  (!cs && lt_smart(tmp.back(), list[i])))
2342  {
2343  tmp.push_back(list[i]);
2344  }
2345  }
2346 
2347  list = tmp;
2348  }
2349 }
2350 
2351 // Return the array index of the best matching channel. An exact
2352 // match is the best match. Otherwise, find the closest numerical
2353 // value greater than channum. E.g., if the channel list is {2_1,
2354 // 2_2, 4_1, 4_2, 300} then input 3 returns 2_2, input 4 returns 2_2,
2355 // and input 5 returns 4_2.
2356 //
2357 // The list does not need to be sorted.
2359  const QString &channum)
2360 {
2361  ChannelInfo target;
2362  target.m_chanNum = channum;
2363  int b = -1; // index of best seen so far
2364  for (int i = 0; i < (int)list.size(); ++i)
2365  {
2366  // Index i is a better result if any of the following hold:
2367  // i is the first element seen
2368  // i < target < best (i.e., i is the first one less than the target)
2369  // best < i < target
2370  // target < i < best
2371  if ((b < 0) ||
2372  (lt_smart(list[i], target) && lt_smart(target, list[b])) ||
2373  (lt_smart(list[b], list[i]) && lt_smart(list[i], target)) ||
2374  (lt_smart(target, list[i]) && lt_smart(list[i], list[b])))
2375  {
2376  b = i;
2377  }
2378  }
2379  return b;
2380 }
2381 
2383  const ChannelInfoList &sorted,
2384  uint old_chanid,
2385  uint mplexid_restriction,
2386  uint chanid_restriction,
2387  ChannelChangeDirection direction,
2388  bool skip_non_visible,
2389  bool skip_same_channum_and_callsign,
2390  bool skip_other_sources)
2391 {
2392  auto it = find(sorted.cbegin(), sorted.cend(), old_chanid);
2393 
2394  if (it == sorted.end())
2395  it = sorted.begin(); // not in list, pretend we are on first channel
2396 
2397  if (it == sorted.end())
2398  return 0; // no channels..
2399 
2400  auto start = it;
2401 
2402  if (CHANNEL_DIRECTION_DOWN == direction)
2403  {
2404  do
2405  {
2406  if (it == sorted.begin())
2407  {
2408  it = find(sorted.begin(), sorted.end(),
2409  sorted.rbegin()->m_chanId);
2410  if (it == sorted.end())
2411  {
2412  --it;
2413  }
2414  }
2415  else
2416  --it;
2417  }
2418  while ((it != start) &&
2419  ((skip_non_visible && it->m_visible < kChannelVisible) ||
2420  (skip_other_sources &&
2421  it->m_sourceId != start->m_sourceId) ||
2422  (skip_same_channum_and_callsign &&
2423  it->m_chanNum == start->m_chanNum &&
2424  it->m_callSign == start->m_callSign) ||
2425  ((mplexid_restriction != 0U) &&
2426  (mplexid_restriction != it->m_mplexId)) ||
2427  ((chanid_restriction != 0U) &&
2428  (chanid_restriction != it->m_chanId))));
2429  }
2430  else if ((CHANNEL_DIRECTION_UP == direction) ||
2431  (CHANNEL_DIRECTION_FAVORITE == direction))
2432  {
2433  do
2434  {
2435  ++it;
2436  if (it == sorted.end())
2437  it = sorted.begin();
2438  }
2439  while ((it != start) &&
2440  ((skip_non_visible && it->m_visible < kChannelVisible) ||
2441  (skip_other_sources &&
2442  it->m_sourceId != start->m_sourceId) ||
2443  (skip_same_channum_and_callsign &&
2444  it->m_chanNum == start->m_chanNum &&
2445  it->m_callSign == start->m_callSign) ||
2446  ((mplexid_restriction != 0U) &&
2447  (mplexid_restriction != it->m_mplexId)) ||
2448  ((chanid_restriction != 0U) &&
2449  (chanid_restriction != it->m_chanId))));
2450  }
2451 
2452  return it->m_chanId;
2453 }
2454 
2456  uint &totalAvailable,
2457  bool ignoreHidden,
2458  ChannelUtil::OrderBy orderBy,
2459  ChannelUtil::GroupBy groupBy,
2460  uint sourceID,
2461  uint channelGroupID,
2462  bool liveTVOnly,
2463  const QString& callsign,
2464  const QString& channum,
2465  bool ignoreUntunable)
2466 {
2467  ChannelInfoList channelList;
2468 
2469  MSqlQuery query(MSqlQuery::InitCon());
2470 
2471  QString sql = QString(
2472  "SELECT parentid, GROUP_CONCAT(cardid ORDER BY cardid) "
2473  "FROM capturecard "
2474  "WHERE parentid <> 0 "
2475  "GROUP BY parentid ");
2476 
2477  query.prepare(sql);
2478  if (!query.exec())
2479  {
2480  MythDB::DBError("ChannelUtil::GetChannels()", query);
2481  return channelList;
2482  }
2483 
2484  QMap<uint, QList<uint>> childIdLists;
2485  while (query.next())
2486  {
2487  auto parentId = query.value(0).toUInt();
2488  auto &childIdList = childIdLists[parentId];
2489  auto childIds = query.value(1).toString().split(",");
2490  while (!childIds.isEmpty())
2491  childIdList.append(childIds.takeFirst().toUInt());
2492  }
2493 
2494  sql = "SELECT %1 channum, freqid, channel.sourceid, "
2495  "callsign, name, icon, finetune, videofilters, xmltvid, "
2496  "channel.recpriority, channel.contrast, channel.brightness, "
2497  "channel.colour, channel.hue, tvformat, "
2498  "visible, outputfilters, useonairguide, mplexid, "
2499  "serviceid, atsc_major_chan, atsc_minor_chan, last_record, "
2500  "default_authority, commmethod, tmoffset, iptvid, "
2501  "channel.chanid, "
2502  "GROUP_CONCAT(DISTINCT `groups`.`groupids`), " // Creates a CSV list of channel groupids for this channel
2503  "GROUP_CONCAT(DISTINCT capturecard.cardid "
2504  " ORDER BY livetvorder), " // Creates a CSV list of inputids for this channel
2505  "MIN(livetvorder) livetvorder "
2506  "FROM channel ";
2507  if (!channelGroupID)
2508  sql += "LEFT ";
2509  sql += "JOIN ( "
2510  " SELECT chanid ,"
2511  " GROUP_CONCAT(grpid ORDER BY grpid) groupids "
2512  " FROM channelgroup ";
2513  if (channelGroupID)
2514  sql += " WHERE grpid = :CHANGROUPID ";
2515  sql += " GROUP BY chanid "
2516  ") `groups` "
2517  " ON channel.chanid = `groups`.`chanid` ";
2518  if (!ignoreUntunable && !liveTVOnly)
2519  sql += "LEFT ";
2520  sql += "JOIN capturecard "
2521  " ON capturecard.sourceid = channel.sourceid "
2522  " AND capturecard.parentid = 0 ";
2523  if (liveTVOnly)
2524  sql += " AND capturecard.livetvorder > 0 ";
2525 
2526  sql += "WHERE channel.deleted IS NULL ";
2527  if (ignoreHidden)
2528  sql += "AND channel.visible > 0 ";
2529 
2530  if (sourceID > 0)
2531  sql += "AND channel.sourceid = :SOURCEID ";
2532 
2533  if (groupBy == kChanGroupByCallsign)
2534  sql += "GROUP BY channel.callsign ";
2535  else if (groupBy == kChanGroupByCallsignAndChannum)
2536  sql += "GROUP BY channel.callsign, channel.channum ";
2537  else
2538  sql += "GROUP BY channel.chanid "; // We must always group for this query
2539 
2540  if (orderBy == kChanOrderByName)
2541  sql += "ORDER BY channel.name ";
2542  else if (orderBy == kChanOrderByChanNum)
2543  {
2544  // Natural sorting including subchannels e.g. 2_4, 1.3
2545  sql += "ORDER BY LPAD(CAST(channel.channum AS UNSIGNED), 10, 0), "
2546  " LPAD(channel.channum, 10, 0) ";
2547  }
2548  else // kChanOrderByLiveTV
2549  {
2550  sql += "ORDER BY callsign = :CALLSIGN1 AND channum = :CHANNUM DESC, "
2551  " callsign = :CALLSIGN2 DESC, "
2552  " livetvorder, "
2553  " channel.recpriority DESC, "
2554  " chanid ";
2555  }
2556 
2557  if (count > 0)
2558  sql += "LIMIT :LIMIT ";
2559 
2560  if (startIndex > 0)
2561  sql += "OFFSET :STARTINDEX ";
2562 
2563 
2564  if (startIndex > 0 || count > 0)
2565  sql = sql.arg("SQL_CALC_FOUND_ROWS");
2566  else
2567  sql = sql.arg(""); // remove place holder
2568 
2569  query.prepare(sql);
2570 
2571  if (channelGroupID > 0)
2572  query.bindValue(":CHANGROUPID", channelGroupID);
2573 
2574  if (sourceID > 0)
2575  query.bindValue(":SOURCEID", sourceID);
2576 
2577  if (count > 0)
2578  query.bindValue(":LIMIT", count);
2579 
2580  if (startIndex > 0)
2581  query.bindValue(":STARTINDEX", startIndex);
2582 
2583  if (orderBy == kChanOrderByLiveTV)
2584  {
2585  query.bindValue(":CALLSIGN1", callsign);
2586  query.bindValue(":CHANNUM", channum);
2587  query.bindValue(":CALLSIGN2", callsign);
2588  }
2589 
2590  if (!query.exec())
2591  {
2592  MythDB::DBError("ChannelInfo::Load()", query);
2593  return channelList;
2594  }
2595 
2596  QList<uint> groupIdList;
2597  while (query.next())
2598  {
2599  ChannelInfo channelInfo;
2600  channelInfo.m_chanNum = query.value(0).toString();
2601  channelInfo.m_freqId = query.value(1).toString();
2602  channelInfo.m_sourceId = query.value(2).toUInt();
2603  channelInfo.m_callSign = query.value(3).toString();
2604  channelInfo.m_name = query.value(4).toString();
2605  channelInfo.m_icon = query.value(5).toString();
2606  channelInfo.m_fineTune = query.value(6).toInt();
2607  channelInfo.m_videoFilters = query.value(7).toString();
2608  channelInfo.m_xmltvId = query.value(8).toString();
2609  channelInfo.m_recPriority = query.value(9).toInt();
2610  channelInfo.m_contrast = query.value(10).toUInt();
2611  channelInfo.m_brightness = query.value(11).toUInt();
2612  channelInfo.m_colour = query.value(12).toUInt();
2613  channelInfo.m_hue = query.value(13).toUInt();
2614  channelInfo.m_tvFormat = query.value(14).toString();
2615  channelInfo.m_visible =
2616  static_cast<ChannelVisibleType>(query.value(15).toInt());
2617  channelInfo.m_outputFilters = query.value(16).toString();
2618  channelInfo.m_useOnAirGuide = query.value(17).toBool();
2619  channelInfo.m_mplexId = query.value(18).toUInt();
2620  channelInfo.m_serviceId = query.value(19).toUInt();
2621  channelInfo.m_atscMajorChan = query.value(20).toUInt();
2622  channelInfo.m_atscMinorChan = query.value(21).toUInt();
2623  channelInfo.m_lastRecord = query.value(22).toDateTime();
2624  channelInfo.m_defaultAuthority = query.value(23).toString();
2625  channelInfo.m_commMethod = query.value(24).toUInt();
2626  channelInfo.m_tmOffset = query.value(25).toUInt();
2627  channelInfo.m_iptvId = query.value(26).toUInt();
2628  channelInfo.m_chanId = query.value(27).toUInt();
2629 
2630  QStringList groupIDs = query.value(28).toString().split(",");
2631  groupIdList.clear();
2632  while (!groupIDs.isEmpty())
2633  groupIdList.push_back(groupIDs.takeFirst().toUInt());
2634  std::sort(groupIdList.begin(), groupIdList.end());
2635  for (auto groupId : groupIdList)
2636  channelInfo.AddGroupId(groupId);
2637 
2638  QStringList parentIDs = query.value(29).toString().split(",");
2639  while (!parentIDs.isEmpty())
2640  {
2641  auto parentId = parentIDs.takeFirst().toUInt();
2642  channelInfo.AddInputId(parentId);
2643  auto childIdList = childIdLists[parentId];
2644  for (auto childId : childIdList)
2645  channelInfo.AddInputId(childId);
2646  }
2647 
2648  channelList.push_back(channelInfo);
2649  }
2650 
2651  if ((startIndex > 0 || count > 0) &&
2652  query.exec("SELECT FOUND_ROWS()") && query.next())
2653  totalAvailable = query.value(0).toUInt();
2654  else
2655  totalAvailable = query.size();
2656 
2657  return channelList;
2658 }
2659 
2660 /* vim: set expandtab tabstop=4 shiftwidth=4: */
ChannelInfo
Definition: channelinfo.h:31
DTVModulation::toString
QString toString() const
Definition: dtvconfparserhelpers.h:412
NetworkInformationTable::TransportDescriptorsLength
uint TransportDescriptorsLength(uint i) const
trans_desc_length 12 4.4+p
Definition: dvbtables.h:83
DTVMultiplex::m_frequency
uint64_t m_frequency
Definition: dtvmultiplex.h:94
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:215
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:813
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:127
DTVMultiplex
Definition: dtvmultiplex.h:24
MSqlQuery::bindValueNoNull
void bindValueNoNull(const QString &placeholder, const QVariant &val)
Add a single binding, taking care not to set a NULL value.
Definition: mythdbcon.cpp:903
MSqlQuery::size
int size(void) const
Definition: mythdbcon.h:214
TerrestrialDeliverySystemDescriptor::HierarchyString
QString HierarchyString(void) const
Definition: dvbdescriptors.h:1009
insert_dtv_multiplex
static uint insert_dtv_multiplex(int db_source_id, const QString &sistandard, uint64_t frequency, const QString &modulation, int transport_id, int network_id, int symbol_rate, signed char bandwidth, signed char polarity, signed char inversion, signed char trans_mode, const QString &inner_FEC, const QString &constellation, signed char hierarchy, const QString &hp_code_rate, const QString &lp_code_rate, const QString &guard_interval, const QString &mod_sys, const QString &rolloff)
Definition: channelutil.cpp:79
ChannelInsertInfo::m_useOnAirGuide
bool m_useOnAirGuide
Definition: channelinfo.h:223
MPEGDescriptor::DescriptorTag
uint DescriptorTag(void) const
Definition: mpegdescriptors.h:345
TerrestrialDeliverySystemDescriptor::GuardIntervalString
QString GuardIntervalString(void) const
Definition: dvbdescriptors.h:1055
ChannelInfo::m_outputFilters
QString m_outputFilters
Definition: channelinfo.h:107
DTVTransmitMode::toChar
QChar toChar() const
Definition: dtvconfparserhelpers.h:476
NetworkInformationTable::TSID
uint TSID(uint i) const
transport_stream_id 16 0.0+p
Definition: dvbtables.h:77
ChannelInsertInfo::m_chanNum
QString m_chanNum
Definition: channelinfo.h:218
DTVMultiplex::m_rolloff
DTVRollOff m_rolloff
Definition: dtvmultiplex.h:107
CHANNEL_DIRECTION_DOWN
@ CHANNEL_DIRECTION_DOWN
Definition: tv.h:31
DTVInversion::toChar
QChar toChar() const
Definition: dtvconfparserhelpers.h:206
DTVPolarity::toChar
QChar toChar() const
Definition: dtvconfparserhelpers.h:633
ChannelChangeDirection
ChannelChangeDirection
ChannelChangeDirection is an enumeration of possible channel changing directions.
Definition: tv.h:28
mythdb.h
SourceUtil::GetSourceName
static QString GetSourceName(uint sourceid)
Definition: sourceutil.cpp:47
CHANNEL_DIRECTION_UP
@ CHANNEL_DIRECTION_UP
Definition: tv.h:30
ChannelInfo::m_chanId
uint m_chanId
Definition: channelinfo.h:85
ChannelUtil::LoadChannels
static ChannelInfoList LoadChannels(uint startIndex, uint count, uint &totalAvailable, bool ignoreHidden=true, OrderBy orderBy=kChanOrderByChanNum, GroupBy groupBy=kChanGroupByChanid, uint sourceID=0, uint channelGroupID=0, bool liveTVOnly=false, const QString &callsign="", const QString &channum="", bool ignoreUntunable=true)
Load channels from database into a list of ChannelInfo objects.
Definition: channelutil.cpp:2455
kChannelNeverVisible
@ kChannelNeverVisible
Definition: channelinfo.h:25
ChannelUtil::DeleteChannel
static bool DeleteChannel(uint channel_id)
Definition: channelutil.cpp:1787
ChannelInfo::m_freqId
QString m_freqId
Definition: channelinfo.h:87
pid_cache_item_t
Definition: channelutil.h:24
NetworkInformationTable::TransportDescriptors
const unsigned char * TransportDescriptors(uint i) const
for(j=0;j<N;j++) x 6.0+p { descriptor() }
Definition: dvbtables.h:87
ChannelUtil::OrderBy
OrderBy
Definition: channelutil.h:206
IPTVTuningData::kNone
@ kNone
Definition: iptvtuningdata.h:26
freq
static const std::array< const uint32_t, 4 > freq
Definition: element.cpp:45
ChannelUtil::GetBetterMplexID
static int GetBetterMplexID(int current_mplexid, int transport_id, int network_id)
Returns best match multiplex ID, creating one if needed.
Definition: channelutil.cpp:610
ChannelInfo::m_fineTune
int m_fineTune
Definition: channelinfo.h:95
ChannelInfo::m_iptvId
uint m_iptvId
Definition: channelinfo.h:121
ChannelUtil::SetChannelValue
static bool SetChannelValue(const QString &field_name, const QString &value, uint sourceid, const QString &channum)
Definition: channelutil.cpp:1139
ChannelUtil::GetATSCChannel
static bool GetATSCChannel(uint sourceid, const QString &channum, uint &major, uint &minor)
Definition: channelutil.cpp:1876
ChannelInfo::AddInputId
void AddInputId(uint linputid)
Definition: channelinfo.h:71
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:204
DTVMultiplex::m_hierarchy
DTVHierarchy m_hierarchy
Definition: dtvmultiplex.h:103
CableDeliverySystemDescriptor
Definition: dvbdescriptors.h:751
ChannelInfo::m_atscMajorChan
uint m_atscMajorChan
Definition: channelinfo.h:113
IPTVTuningData::GetDataURL
QUrl GetDataURL(void) const
Definition: iptvtuningdata.h:139
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:619
IPTVTuningData::GetFECURL0
QUrl GetFECURL0(void) const
Definition: iptvtuningdata.h:140
ChannelInfo::m_name
QString m_name
Definition: channelinfo.h:92
ChannelUtil::GetCachedPids
static bool GetCachedPids(uint chanid, pid_cache_t &pid_cache)
Returns cached MPEG PIDs when given a Channel ID.
Definition: channelutil.cpp:858
ChannelUtil::SetVisible
static bool SetVisible(uint channel_id, ChannelVisibleType visible)
Definition: channelutil.cpp:1816
ChannelInsertInfo::m_hidden
bool m_hidden
Definition: channelinfo.h:224
ChannelUtil::kChanOrderByLiveTV
@ kChanOrderByLiveTV
Definition: channelutil.h:210
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
IPTVTuningData::kSMPTE2022_1
@ kSMPTE2022_1
Definition: iptvtuningdata.h:39
ChannelUtil::kChanOrderByName
@ kChanOrderByName
Definition: channelutil.h:209
ChannelUtil::GetMplexID
static uint GetMplexID(uint sourceid, const QString &channum)
Definition: channelutil.cpp:462
DTVMultiplex::m_bandwidth
DTVBandwidth m_bandwidth
Definition: dtvmultiplex.h:97
CableDeliverySystemDescriptor::FrequencyHz
unsigned long long FrequencyHz(void) const
Definition: dvbdescriptors.h:766
ChannelUtil::GetIPTVTuningData
static IPTVTuningData GetIPTVTuningData(uint chanid)
Definition: channelutil.cpp:2002
DTVCodeRate::toString
QString toString() const
Definition: dtvconfparserhelpers.h:341
ChannelUtil::GetChannelStringField
static QString GetChannelStringField(int chan_id, const QString &field)
Definition: channelutil.cpp:756
ChannelUtil::GetNextChannel
static uint GetNextChannel(const ChannelInfoList &sorted, uint old_chanid, uint mplexid_restriction, uint chanid_restriction, ChannelChangeDirection direction, bool skip_non_visible=true, bool skip_same_channum_and_callsign=false, bool skip_other_sources=false)
Definition: channelutil.cpp:2382
DescriptorID::satellite_delivery_system
@ satellite_delivery_system
Definition: mpegdescriptors.h:75
CableDeliverySystemDescriptor::FECInnerString
QString FECInnerString(void) const
Definition: dvbdescriptors.h:825
ChannelUtil::GetUnknownCallsign
static QString GetUnknownCallsign(void)
Definition: channelutil.cpp:1307
ChannelInfo::m_icon
QString m_icon
Definition: channelinfo.h:93
TerrestrialDeliverySystemDescriptor
Definition: dvbdescriptors.h:939
tmp
static guint32 * tmp
Definition: goom_core.cpp:26
ChannelInsertInfo
Definition: channelinfo.h:133
ChannelUtil::GetInputTypes
static QStringList GetInputTypes(uint chanid)
Definition: channelutil.cpp:825
ChannelUtil::SortChannels
static void SortChannels(ChannelInfoList &list, const QString &order, bool eliminate_duplicates=false)
Definition: channelutil.cpp:2325
get_max_chanid
static uint get_max_chanid(uint sourceid)
Definition: channelutil.cpp:1406
ChannelUtil::GetSourceID
static int GetSourceID(int mplexid)
Definition: channelutil.cpp:787
ChannelInfo::m_commMethod
int m_commMethod
Definition: channelinfo.h:119
DTVMultiplex::m_inversion
DTVInversion m_inversion
Definition: dtvmultiplex.h:96
ChannelUtil::s_channelDefaultAuthorityMap
static QMap< uint, QString > s_channelDefaultAuthorityMap
Definition: channelutil.h:354
ChannelInfo::m_useOnAirGuide
bool m_useOnAirGuide
Definition: channelinfo.h:108
sourceutil.h
chanid_available
static bool chanid_available(uint chanid)
Definition: channelutil.cpp:1427
minor
#define minor(X)
Definition: compat.h:78
ChannelUtil::GetChanID
static int GetChanID(int db_mplexid, int service_transport_id, int major_channel, int minor_channel, int program_number)
Definition: channelutil.cpp:1312
TerrestrialDeliverySystemDescriptor::BandwidthString
QString BandwidthString(void) const
Definition: dvbdescriptors.h:967
DTVRollOff::toString
QString toString() const
Definition: dtvconfparserhelpers.h:768
MPEGDescriptor::Parse
static desc_list_t Parse(const unsigned char *data, uint len)
Definition: mpegdescriptors.cpp:17
ChannelUtil::FindChannel
static uint FindChannel(uint sourceid, const QString &freqid)
Definition: channelutil.cpp:1385
ChannelUtil::GetChannelsInternal
static ChannelInfoList GetChannelsInternal(uint sourceid, bool visible_only, bool include_disconnected, const QString &group_by, uint channel_groupid)
Definition: channelutil.cpp:2078
DTVMultiplex::m_guardInterval
DTVGuardInterval m_guardInterval
Definition: dtvmultiplex.h:102
ChannelInfo::m_atscMinorChan
uint m_atscMinorChan
Definition: channelinfo.h:114
MPEGDescriptor
Definition: mpegdescriptors.h:302
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:551
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:1573
SatelliteDeliverySystemDescriptor::RollOffString
QString RollOffString(void) const
Definition: dvbdescriptors.h:884
pid_cache_t
std::vector< pid_cache_item_t > pid_cache_t
Definition: channelutil.h:43
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:226
IPTVTuningData::GetDeviceName
QString GetDeviceName(void) const
Definition: iptvtuningdata.h:113
lt_smart
bool lt_smart(const ChannelInfo &a, const ChannelInfo &b)
Definition: channelutil.cpp:2212
IPTVTuningData::GetFECURL1
QUrl GetFECURL1(void) const
Definition: iptvtuningdata.h:141
StringUtil::naturalCompare
MBASE_PUBLIC int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
This method chops the input a and b into pieces of digits and non-digits (a1.05 becomes a | 1 | .
Definition: stringutil.cpp:160
desc_list_t
std::vector< const unsigned char * > desc_list_t
Definition: mpegdescriptors.h:18
ChannelInfo::m_chanNum
QString m_chanNum
Definition: channelinfo.h:86
TerrestrialDeliverySystemDescriptor::ConstellationString
QString ConstellationString(void) const
Definition: dvbdescriptors.h:988
stringutil.h
ChannelInsertInfo::m_channelId
uint m_channelId
Definition: channelinfo.h:215
DTVMultiplex::m_hpCodeRate
DTVCodeRate m_hpCodeRate
Definition: dtvmultiplex.h:98
ChannelUtil::s_channelDefaultAuthorityMapLock
static QReadWriteLock s_channelDefaultAuthorityMapLock
Definition: channelutil.h:353
lt_pidcache
static bool lt_pidcache(const pid_cache_item_t a, const pid_cache_item_t b)
Definition: channelutil.cpp:846
SatelliteDeliverySystemDescriptor::FECInnerString
QString FECInnerString(void) const
Definition: dvbdescriptors.h:933
IPTVTuningData
Definition: iptvtuningdata.h:21
IPTVTuningData::kRFC2733
@ kRFC2733
Definition: iptvtuningdata.h:27
ChannelUtil::GetChannelValueStr
static QString GetChannelValueStr(const QString &channel_field, uint sourceid, const QString &channum)
Definition: channelutil.cpp:950
DTVHierarchy::toChar
QChar toChar() const
Definition: dtvconfparserhelpers.h:594
dvbtables.h
ChannelUtil::CreateChanID
static int CreateChanID(uint sourceid, const QString &chan_num)
Creates a unique channel ID for database use.
Definition: channelutil.cpp:1448
DTVMultiplex::m_modSys
DTVModulationSystem m_modSys
Definition: dtvmultiplex.h:106
IPTVTuningData::kRFC5109
@ kRFC5109
Definition: iptvtuningdata.h:28
ChannelInfo::m_tmOffset
int m_tmOffset
Definition: channelinfo.h:120
DTVMultiplex::m_fec
DTVCodeRate m_fec
Definition: dtvmultiplex.h:105
ChannelInfo::m_hue
uint m_hue
Definition: channelinfo.h:103
ChannelVisibleType
ChannelVisibleType
Definition: channelinfo.h:20
ChannelInfo::m_contrast
uint m_contrast
Definition: channelinfo.h:100
ChannelUtil::s_channelDefaultAuthority_runInit
static bool s_channelDefaultAuthority_runInit
Definition: channelutil.h:355
CHANNEL_DIRECTION_FAVORITE
@ CHANNEL_DIRECTION_FAVORITE
Definition: tv.h:32
NetworkInformationTable::OriginalNetworkID
uint OriginalNetworkID(uint i) const
original_network_id 16 2.0+p
Definition: dvbtables.h:79
uint
unsigned int uint
Definition: compat.h:81
IPTVTuningData::FECType
FECType
Definition: iptvtuningdata.h:24
IPTVTuningData::IPTVType
IPTVType
Definition: iptvtuningdata.h:32
ChannelInfo::AddGroupId
void AddGroupId(uint lgroupid)
Definition: channelinfo.h:59
IPTVTuningData::kRFC5109_1
@ kRFC5109_1
Definition: iptvtuningdata.h:37
DescriptorID::terrestrial_delivery_system
@ terrestrial_delivery_system
Definition: mpegdescriptors.h:99
handle_transport_desc
static void handle_transport_desc(std::vector< uint > &muxes, const MPEGDescriptor &desc, uint sourceid, uint tsid, uint netid)
Definition: channelutil.cpp:278
ChannelUtil::GetChannelValueInt
static int GetChannelValueInt(const QString &channel_field, uint sourceid, const QString &channum)
Definition: channelutil.cpp:978
MSqlQuery::ChannelCon
static MSqlQueryInfo ChannelCon()
Returns dedicated connection. (Required for using temporary SQL tables.)
Definition: mythdbcon.cpp:600
ChannelUtil::UpdateInsertInfoFromDB
static void UpdateInsertInfoFromDB(ChannelInsertInfo &chan)
Definition: channelutil.cpp:1690
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:1485
lt_callsign
bool lt_callsign(const ChannelInfo &a, const ChannelInfo &b)
Definition: channelutil.cpp:2207
channelutil.h
DTVMultiplex::m_symbolRate
uint64_t m_symbolRate
Definition: dtvmultiplex.h:95
DTVModulationSystem::toString
QString toString() const
Definition: dtvconfparserhelpers.h:719
IPTVTuningData::kSMPTE2022
@ kSMPTE2022
Definition: iptvtuningdata.h:29
ChannelUtil::GetTimeOffset
static std::chrono::minutes GetTimeOffset(int chan_id)
Returns the listings time offset in minutes for given channel.
Definition: channelutil.cpp:782
ChannelUtil::GetChannelData
static bool GetChannelData(uint sourceid, uint &chanid, const QString &channum, QString &tvformat, QString &modulation, QString &freqtable, QString &freqid, int &finetune, uint64_t &frequency, QString &dtv_si_std, int &mpeg_prog_num, uint &atsc_major, uint &atsc_minor, uint &dvb_transportid, uint &dvb_networkid, uint &mplexid, bool &commfree)
Definition: channelutil.cpp:1904
ChannelUtil::CreateMultiplexes
static std::vector< uint > CreateMultiplexes(int sourceid, const NetworkInformationTable *nit)
Definition: channelutil.cpp:437
TerrestrialDeliverySystemDescriptor::CodeRateLPString
QString CodeRateLPString(void) const
Definition: dvbdescriptors.h:1039
ChannelUtil::SetServiceVersion
static bool SetServiceVersion(int mplexid, int version)
Definition: channelutil.cpp:1835
ChannelInfo::m_serviceId
uint m_serviceId
Definition: channelinfo.h:111
ChannelUtil::GetIcon
static QString GetIcon(uint chanid)
Definition: channelutil.cpp:1247
pid_cache_item_t::GetPID
uint GetPID(void) const
Definition: channelutil.h:30
kChannelVisible
@ kChannelVisible
Definition: channelinfo.h:23
IPTVTuningData::kSMPTE2022_2
@ kSMPTE2022_2
Definition: iptvtuningdata.h:40
IPTVTuningData::kRFC2733_1
@ kRFC2733_1
Definition: iptvtuningdata.h:35
IPTVTuningData::kRFC5109_2
@ kRFC5109_2
Definition: iptvtuningdata.h:38
get_valid_recorder_list
static QStringList get_valid_recorder_list(uint chanid)
Returns list of the recorders that have chanid in their sources.
Definition: channelutil.cpp:1021
ChannelUtil::GroupBy
GroupBy
Definition: channelutil.h:213
SatelliteDeliverySystemDescriptor::ModulationSystemString
QString ModulationSystemString(void) const
Definition: dvbdescriptors.h:891
ChannelUtil::CreateMultiplex
static uint CreateMultiplex(int sourceid, const QString &sistandard, uint64_t frequency, const QString &modulation, int transport_id=-1, int network_id=-1)
Definition: channelutil.cpp:372
CableDeliverySystemDescriptor::ModulationString
QString ModulationString(void) const
Definition: dvbdescriptors.h:795
SatelliteDeliverySystemDescriptor::SymbolRateHz
uint SymbolRateHz(void) const
Definition: dvbdescriptors.h:916
DTVBandwidth::toChar
QChar toChar() const
Definition: dtvconfparserhelpers.h:269
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:889
ChannelUtil::GetSourceIDForChannel
static uint GetSourceIDForChannel(uint chanid)
Definition: channelutil.cpp:807
ChannelUtil::GetServiceVersion
static int GetServiceVersion(int mplexid)
Definition: channelutil.cpp:1854
ChannelUtil::UpdateChannelNumberFromDB
static void UpdateChannelNumberFromDB(ChannelInsertInfo &chan)
Definition: channelutil.cpp:1664
TerrestrialDeliverySystemDescriptor::FrequencyHz
uint64_t FrequencyHz(void) const
Definition: dvbdescriptors.h:955
ChannelUtil::kChanGroupByCallsignAndChannum
@ kChanGroupByCallsignAndChannum
Definition: channelutil.h:216
ChannelInfo::m_sourceId
uint m_sourceId
Definition: channelinfo.h:89
IPTVTuningData::GetFECTypeString
QString GetFECTypeString(uint i) const
Definition: iptvtuningdata.h:156
DTVMultiplex::m_sistandard
QString m_sistandard
Definition: dtvmultiplex.h:111
TerrestrialDeliverySystemDescriptor::TransmissionModeString
QString TransmissionModeString(void) const
Definition: dvbdescriptors.h:1068
ChannelInfo::m_tvFormat
QString m_tvFormat
Definition: channelinfo.h:105
SatelliteDeliverySystemDescriptor::PolarizationString
QString PolarizationString() const
Definition: dvbdescriptors.h:866
ChannelInfo::m_recPriority
int m_recPriority
Definition: channelinfo.h:98
SatelliteDeliverySystemDescriptor
Definition: dvbdescriptors.h:830
ChannelUtil::GetConflicting
static std::vector< uint > GetConflicting(const QString &channum, uint sourceid=0)
Definition: channelutil.cpp:1103
TerrestrialDeliverySystemDescriptor::CodeRateHPString
QString CodeRateHPString(void) const
Definition: dvbdescriptors.h:1030
ChannelInfo::m_xmltvId
QString m_xmltvId
Definition: channelinfo.h:97
CableDeliverySystemDescriptor::SymbolRateHz
uint SymbolRateHz(void) const
Definition: dvbdescriptors.h:808
DTVMultiplex::m_modulation
DTVModulation m_modulation
Definition: dtvmultiplex.h:100
ChannelInfo::m_videoFilters
QString m_videoFilters
Definition: channelinfo.h:96
DTVMultiplex::m_polarity
DTVPolarity m_polarity
Definition: dtvmultiplex.h:104
ChannelUtil::GetValidRecorderList
static QStringList GetValidRecorderList(uint chanid, const QString &channum)
Returns list of the recorders that have chanid or channum in their sources.
Definition: channelutil.cpp:1092
ChannelUtil::SaveCachedPids
static bool SaveCachedPids(uint chanid, const pid_cache_t &_pid_cache, bool delete_all=false)
Saves PIDs for PSIP tables to database.
Definition: channelutil.cpp:891
ChannelUtil::GetChannelCount
static uint GetChannelCount(int sourceid=-1)
Definition: channelutil.cpp:2306
ChannelUtil::GetChanNum
static QString GetChanNum(int chan_id)
Returns the channel-number string of the given channel.
Definition: channelutil.cpp:777
ChannelInsertInfo::m_xmltvId
QString m_xmltvId
Definition: channelinfo.h:230
ChannelUtil::GetNearestChannel
static int GetNearestChannel(const ChannelInfoList &list, const QString &channum)
Definition: channelutil.cpp:2358
NetworkInformationTable::TransportStreamCount
uint TransportStreamCount(void) const
Definition: dvbtables.h:73
ChannelInfo::m_lastRecord
QDateTime m_lastRecord
Definition: channelinfo.h:116
ChannelUtil::GetChanIDs
static std::vector< uint > GetChanIDs(int sourceid=-1, bool onlyVisible=false)
Definition: channelutil.cpp:2179
DTVGuardInterval::toString
QString toString() const
Definition: dtvconfparserhelpers.h:540
IPTVTuningData::kData
@ kData
Definition: iptvtuningdata.h:34
ChannelInfo::m_mplexId
uint m_mplexId
Definition: channelinfo.h:110
ChannelUtil::GetDefaultAuthority
static QString GetDefaultAuthority(uint chanid)
Returns the DVB default authority for the chanid given.
Definition: channelutil.cpp:1179
ChannelInfo::m_brightness
uint m_brightness
Definition: channelinfo.h:101
ChannelInfo::m_colour
uint m_colour
Definition: channelinfo.h:102
IPTVTuningData::kRFC2733_2
@ kRFC2733_2
Definition: iptvtuningdata.h:36
ChannelUtil::GetTuningParams
static bool GetTuningParams(uint mplexid, QString &modulation, uint64_t &frequency, uint &dvb_transportid, uint &dvb_networkid, QString &si_std)
Definition: channelutil.cpp:721
SatelliteDeliverySystemDescriptor::FrequencykHz
uint64_t FrequencykHz(void) const
Definition: dvbdescriptors.h:846
ChannelInfo::m_visible
ChannelVisibleType m_visible
Definition: channelinfo.h:106
IPTVTuningData::GetBitrate
uint GetBitrate(uint i) const
Definition: iptvtuningdata.h:152
nv_python_libs.bbciplayer.bbciplayer_api.version
string version
Definition: bbciplayer_api.py:77
ChannelUtil::IsOnSameMultiplex
static bool IsOnSameMultiplex(uint srcid, const QString &new_channum, const QString &old_channum)
Definition: channelutil.cpp:991
DTVMultiplex::m_transMode
DTVTransmitMode m_transMode
Definition: dtvmultiplex.h:101
ChannelInfo::m_defaultAuthority
QString m_defaultAuthority
Definition: channelinfo.h:118
ChannelInfo::m_callSign
QString m_callSign
Definition: channelinfo.h:91
ChannelUtil::kATSCSeparators
static const QString kATSCSeparators
Definition: channelutil.h:342
SatelliteDeliverySystemDescriptor::ModulationString
QString ModulationString(void) const
Definition: dvbdescriptors.h:905
ChannelUtil::kChanOrderByChanNum
@ kChanOrderByChanNum
Definition: channelutil.h:208
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
ChannelUtil::kChanGroupByCallsign
@ kChanGroupByCallsign
Definition: channelutil.h:215
kChannelNotVisible
@ kChannelNotVisible
Definition: channelinfo.h:24
ChannelInsertInfo::m_visible
ChannelVisibleType m_visible
Definition: channelinfo.h:226
ChannelUtil::UpdateIPTVTuningData
static bool UpdateIPTVTuningData(uint channel_id, const IPTVTuningData &tuning)
Definition: channelutil.cpp:1729
get_dtv_multiplex
static uint get_dtv_multiplex(uint db_source_id, const QString &sistandard, uint64_t frequency, uint transport_id, uint network_id, signed char polarity)
Definition: channelutil.cpp:26
DescriptorID::cable_delivery_system
@ cable_delivery_system
Definition: mpegdescriptors.h:76
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:838
DTVMultiplex::m_lpCodeRate
DTVCodeRate m_lpCodeRate
Definition: dtvmultiplex.h:99
NetworkInformationTable
This table tells the decoder on which PIDs to find other tables.
Definition: dvbtables.h:28
HLSReader.h
ChannelInfoList
std::vector< ChannelInfo > ChannelInfoList
Definition: channelinfo.h:131