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