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 
7 #include <QFile>
8 #include <QHash>
9 #include <QImage>
10 #include <QReadWriteLock>
11 #include <QRegExp>
12 #include <utility>
13 
14 #include "channelutil.h"
15 #include "mythdb.h"
16 #include "dvbtables.h"
17 #include "mythmiscutil.h"
18 #include "HLSReader.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", QString(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", QString(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 {
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 {
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 {
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 {
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 {
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;
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 
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 QString();
758 
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 QString();
767  }
768 
769  if (!query.next())
770  return QString();
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 
781 {
782  return GetChannelStringField(chan_id, QString("tmoffset")).toInt();
783 }
784 
785 int ChannelUtil::GetSourceID(int db_mplexid)
786 {
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 {
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 {
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 {
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 {
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 
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
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
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 QStringList();
1098 }
1099 
1100 
1101 std::vector<uint> ChannelUtil::GetConflicting(const QString &channum, uint sourceid)
1102 {
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 {
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 {
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 
1174 {
1175  static QReadWriteLock s_channelDefaultAuthorityMapLock;
1176  static QMap<uint,QString> s_channelDefaultAuthorityMap;
1177  static bool s_runInit = true;
1178 
1179  s_channelDefaultAuthorityMapLock.lockForRead();
1180 
1181  if (s_runInit)
1182  {
1183  s_channelDefaultAuthorityMapLock.unlock();
1184  s_channelDefaultAuthorityMapLock.lockForWrite();
1185  // cppcheck-suppress identicalInnerCondition
1186  if (s_runInit)
1187  {
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  }
1205  s_runInit = false;
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  }
1226  s_runInit = false;
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;
1240  s_channelDefaultAuthorityMapLock.unlock();
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 
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 {
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 {
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  uint chanid = 0;
1450  int chansep = chan_num.indexOf(QRegularExpression(R"(\D)"));
1451  if (chansep > 0)
1452  {
1453  chanid =
1454  sourceid * 10000 +
1455  chan_num.leftRef(chansep).toInt() * 100 +
1456  chan_num.rightRef(chan_num.length() - chansep - 1).toInt();
1457  }
1458  else
1459  {
1460  chanid = sourceid * 10000 + chan_num.toInt();
1461  }
1462 
1463  if ((chanid > sourceid * 10000) && (chanid_available(chanid)))
1464  return chanid;
1465 
1466  // try to at least base it on the sourceid for human readability
1467  chanid = std::max(get_max_chanid(sourceid) + 1, sourceid * 10000);
1468 
1469  if (chanid_available(chanid))
1470  return chanid;
1471 
1472  // just get a chanid we know should work
1473  chanid = get_max_chanid(0) + 1;
1474 
1475  if (chanid_available(chanid))
1476  return chanid;
1477 
1478  // failure
1479  return -1;
1480 }
1481 
1483  uint db_sourceid,
1484  uint new_channel_id,
1485  const QString &callsign,
1486  const QString &service_name,
1487  const QString &chan_num,
1488  uint service_id,
1489  uint atsc_major_channel,
1490  uint atsc_minor_channel,
1491  bool use_on_air_guide,
1492  ChannelVisibleType visible,
1493  const QString &freqid,
1494  const QString& icon,
1495  QString format,
1496  const QString& xmltvid,
1497  const QString& default_authority,
1498  uint service_type)
1499 {
1501 
1502  QString chanNum = (chan_num == "-1") ?
1503  QString::number(service_id) : chan_num;
1504 
1505  QString qstr =
1506  "INSERT INTO channel "
1507  " (chanid, channum, sourceid, "
1508  " callsign, name, serviceid, ";
1509  qstr += (db_mplexid > 0) ? "mplexid, " : "";
1510  qstr += (!freqid.isEmpty()) ? "freqid, " : "";
1511  qstr +=
1512  " atsc_major_chan, atsc_minor_chan, "
1513  " useonairguide, visible, tvformat, "
1514  " icon, xmltvid, default_authority, "
1515  " service_type) "
1516  "VALUES "
1517  " (:CHANID, :CHANNUM, :SOURCEID, "
1518  " :CALLSIGN, :NAME, :SERVICEID, ";
1519  qstr += (db_mplexid > 0) ? ":MPLEXID, " : "";
1520  qstr += (!freqid.isEmpty()) ? ":FREQID, " : "";
1521  qstr +=
1522  " :MAJORCHAN, :MINORCHAN, "
1523  " :USEOAG, :VISIBLE, :TVFORMAT, "
1524  " :ICON, :XMLTVID, :AUTHORITY, "
1525  " :SERVICETYPE ) ";
1526 
1527  query.prepare(qstr);
1528 
1529  query.bindValue (":CHANID", new_channel_id);
1530  query.bindValueNoNull(":CHANNUM", chanNum);
1531  query.bindValue (":SOURCEID", db_sourceid);
1532  query.bindValueNoNull(":CALLSIGN", callsign);
1533  query.bindValueNoNull(":NAME", service_name);
1534 
1535  if (db_mplexid > 0)
1536  query.bindValue(":MPLEXID", db_mplexid);
1537 
1538  query.bindValue(":SERVICEID", service_id);
1539  query.bindValue(":MAJORCHAN", atsc_major_channel);
1540  query.bindValue(":MINORCHAN", atsc_minor_channel);
1541  query.bindValue(":USEOAG", use_on_air_guide);
1542  query.bindValue(":VISIBLE", visible);
1543 
1544  if (!freqid.isEmpty())
1545  query.bindValue(":FREQID", freqid);
1546 
1547  QString tvformat = (atsc_minor_channel > 0) ? "ATSC" : std::move(format);
1548  query.bindValueNoNull(":TVFORMAT", tvformat);
1549  query.bindValueNoNull(":ICON", icon);
1550  query.bindValueNoNull(":XMLTVID", xmltvid);
1551  query.bindValueNoNull(":AUTHORITY", default_authority);
1552  query.bindValue (":SERVICETYPE", service_type);
1553 
1554  if (!query.exec() || !query.isActive())
1555  {
1556  MythDB::DBError("Adding Service", query);
1557  return false;
1558  }
1559  return true;
1560 }
1561 
1563  uint source_id,
1564  uint channel_id,
1565  const QString &callsign,
1566  const QString &service_name,
1567  const QString &chan_num,
1568  uint service_id,
1569  uint atsc_major_channel,
1570  uint atsc_minor_channel,
1571  bool use_on_air_guide,
1572  ChannelVisibleType visible,
1573  const QString& freqid,
1574  const QString& icon,
1575  QString format,
1576  const QString& xmltvid,
1577  const QString& default_authority,
1578  uint service_type)
1579 {
1580  if (!channel_id)
1581  return false;
1582 
1583  QString tvformat = (atsc_minor_channel > 0) ? "ATSC" : std::move(format);
1584  bool set_channum = !chan_num.isEmpty() && chan_num != "-1";
1585  QString qstr = QString(
1586  "UPDATE channel "
1587  "SET %1 %2 %3 %4 %5 %6"
1588  " mplexid = :MPLEXID, serviceid = :SERVICEID, "
1589  " atsc_major_chan = :MAJORCHAN, atsc_minor_chan = :MINORCHAN, "
1590  " callsign = :CALLSIGN, name = :NAME, "
1591  " sourceid = :SOURCEID, useonairguide = :USEOAG, "
1592  " visible = :VISIBLE, service_type = :SERVICETYPE "
1593  "WHERE chanid=:CHANID")
1594  .arg((!set_channum) ? "" : "channum = :CHANNUM, ")
1595  .arg((freqid.isEmpty()) ? "" : "freqid = :FREQID, ")
1596  .arg((icon.isEmpty()) ? "" : "icon = :ICON, ")
1597  .arg((tvformat.isEmpty()) ? "" : "tvformat = :TVFORMAT, ")
1598  .arg((xmltvid.isEmpty()) ? "" : "xmltvid = :XMLTVID, ")
1599  .arg((default_authority.isEmpty()) ?
1600  "" : "default_authority = :AUTHORITY,");
1601 
1603  query.prepare(qstr);
1604 
1605  query.bindValue(":CHANID", channel_id);
1606 
1607  if (set_channum)
1608  query.bindValue(":CHANNUM", chan_num);
1609 
1610  query.bindValue (":SOURCEID", source_id);
1611  query.bindValueNoNull(":CALLSIGN", callsign);
1612  query.bindValueNoNull(":NAME", service_name);
1613 
1614  query.bindValue(":MPLEXID", db_mplexid);
1615  query.bindValue(":SERVICEID", service_id);
1616  query.bindValue(":MAJORCHAN", atsc_major_channel);
1617  query.bindValue(":MINORCHAN", atsc_minor_channel);
1618  query.bindValue(":USEOAG", use_on_air_guide);
1619  query.bindValue(":VISIBLE", visible);
1620  query.bindValue(":SERVICETYPE", service_type);
1621 
1622  if (!freqid.isEmpty())
1623  query.bindValue(":FREQID", freqid);
1624 
1625  if (!tvformat.isEmpty())
1626  query.bindValue(":TVFORMAT", tvformat);
1627 
1628  if (!icon.isEmpty())
1629  query.bindValue(":ICON", icon);
1630  if (!xmltvid.isEmpty())
1631  query.bindValue(":XMLTVID", xmltvid);
1632  if (!default_authority.isEmpty())
1633  query.bindValue(":AUTHORITY", default_authority);
1634 
1635  if (!query.exec())
1636  {
1637  MythDB::DBError("Updating Service", query);
1638  return false;
1639  }
1640  return true;
1641 }
1642 
1644 {
1646  query.prepare(
1647  "SELECT channum "
1648  "FROM channel "
1649  "WHERE chanid = :ID");
1650  query.bindValue(":ID", chan.m_channelId);
1651 
1652  if (!query.exec())
1653  {
1654  MythDB::DBError("UpdateChannelNumberFromDB", query);
1655  return;
1656  }
1657 
1658  if (query.next())
1659  {
1660  QString channum = query.value(0).toString();
1661 
1662  if (!channum.isEmpty())
1663  {
1664  chan.m_chanNum = channum;
1665  }
1666  }
1667 }
1668 
1670 {
1672  query.prepare(
1673  "SELECT xmltvid, useonairguide, visible "
1674  "FROM channel "
1675  "WHERE chanid = :ID");
1676  query.bindValue(":ID", chan.m_channelId);
1677 
1678  if (!query.exec())
1679  {
1680  MythDB::DBError("UpdateInsertInfoFromDB", query);
1681  return;
1682  }
1683 
1684  if (query.next())
1685  {
1686  QString xmltvid = query.value(0).toString();
1687  bool useeit = query.value(1).toBool();
1688  ChannelVisibleType visible =
1689  static_cast<ChannelVisibleType>(query.value(2).toInt());
1690 
1691  if (!xmltvid.isEmpty())
1692  {
1693  if (useeit)
1694  {
1695  LOG(VB_GENERAL, LOG_ERR,
1696  "Using EIT and xmltv for the same channel "
1697  "is an unsupported configuration.");
1698  }
1699  chan.m_xmltvId = xmltvid;
1700  }
1701  chan.m_useOnAirGuide = useeit;
1702  chan.m_hidden = (visible == kChannelNotVisible ||
1703  visible == kChannelNeverVisible);
1704  chan.m_visible = visible;
1705  }
1706 }
1707 
1709  uint channel_id, const IPTVTuningData &tuning)
1710 {
1712 
1713  query.prepare(
1714  "DELETE FROM iptv_channel "
1715  "WHERE chanid=:CHANID");
1716  query.bindValue(":CHANID", channel_id);
1717 
1718  if (!query.exec())
1719  {
1720  MythDB::DBError("UpdateIPTVTuningData -- delete", query);
1721  return false;
1722  }
1723 
1724  query.prepare(
1725  "INSERT INTO iptv_channel (chanid, url, type, bitrate) "
1726  "VALUES (:CHANID, :URL, :TYPE, :BITRATE)");
1727  query.bindValue(":CHANID", channel_id);
1728 
1729  query.bindValue(":URL", tuning.GetDataURL().toString());
1730  query.bindValue(":TYPE", tuning.GetFECTypeString(0));
1731  query.bindValue(":BITRATE", tuning.GetBitrate(0));
1732 
1733  if (!query.exec())
1734  {
1735  MythDB::DBError("UpdateIPTVTuningData -- data", query);
1736  return false;
1737  }
1738 
1739  if (tuning.GetFECURL0().port() >= 0)
1740  {
1741  query.bindValue(":URL", tuning.GetFECURL0().toString());
1742  query.bindValue(":TYPE", tuning.GetFECTypeString(1));
1743  query.bindValue(":BITRATE", tuning.GetBitrate(1));
1744  if (!query.exec())
1745  {
1746  MythDB::DBError("UpdateIPTVTuningData -- fec 0", query);
1747  return false;
1748  }
1749  }
1750 
1751  if (tuning.GetFECURL1().port() >= 0)
1752  {
1753  query.bindValue(":URL", tuning.GetFECURL1().toString());
1754  query.bindValue(":TYPE", tuning.GetFECTypeString(2));
1755  query.bindValue(":BITRATE", tuning.GetBitrate(2));
1756  if (!query.exec())
1757  {
1758  MythDB::DBError("UpdateIPTVTuningData -- fec 1", query);
1759  return false;
1760  }
1761  }
1762 
1763  return true;
1764 }
1765 
1767 {
1769  query.prepare(
1770  "UPDATE channel "
1771  "SET deleted = NOW() "
1772  "WHERE chanid = :ID");
1773  query.bindValue(":ID", channel_id);
1774 
1775  if (!query.exec())
1776  {
1777  MythDB::DBError("Delete Channel", query);
1778  return false;
1779  }
1780 
1781  query.prepare(
1782  "DELETE FROM iptv_channel "
1783  "WHERE chanid = :ID");
1784  query.bindValue(":ID", channel_id);
1785 
1786  if (!query.exec())
1787  {
1788  MythDB::DBError("Delete Channel 2", query);
1789  return false;
1790  }
1791 
1792  return true;
1793 }
1794 
1796 {
1798  query.prepare(
1799  "UPDATE channel "
1800  "SET visible = :VISIBLE "
1801  "WHERE chanid = :ID");
1802  query.bindValue(":ID", channel_id);
1803  query.bindValue(":VISIBLE", visible);
1804 
1805  if (!query.exec())
1806  {
1807  MythDB::DBError("ChannelUtil::SetVisible", query);
1808  return false;
1809  }
1810 
1811  return true;
1812 }
1813 
1815 {
1817 
1818  query.prepare("UPDATE dtv_multiplex "
1819  "SET serviceversion = :VERSION "
1820  "WHERE mplexid = :MPLEXID");
1821 
1822  query.bindValue(":VERSION", version);
1823  query.bindValue(":MPLEXID", mplexid);
1824 
1825  if (!query.exec())
1826  {
1827  MythDB::DBError("Selecting channel/dtv_multiplex", query);
1828  return false;
1829  }
1830  return true;
1831 }
1832 
1834 {
1836 
1837  query.prepare("SELECT serviceversion "
1838  "FROM dtv_multiplex "
1839  "WHERE mplexid = :MPLEXID");
1840 
1841  query.bindValue(":MPLEXID", mplexid);
1842 
1843  if (!query.exec())
1844  {
1845  MythDB::DBError("Selecting channel/dtv_multiplex", query);
1846  return 0;
1847  }
1848 
1849  if (query.next())
1850  return query.value(0).toInt();
1851 
1852  return -1;
1853 }
1854 
1855 bool ChannelUtil::GetATSCChannel(uint sourceid, const QString &channum,
1856  uint &major, uint &minor)
1857 {
1858  major = minor = 0;
1859 
1861  query.prepare(
1862  "SELECT atsc_major_chan, atsc_minor_chan "
1863  "FROM channel "
1864  "WHERE deleted IS NULL AND "
1865  " channum = :CHANNUM AND "
1866  " sourceid = :SOURCEID");
1867 
1868  query.bindValue(":SOURCEID", sourceid);
1869  query.bindValue(":CHANNUM", channum);
1870 
1871  if (!query.exec() || !query.isActive())
1872  MythDB::DBError("getatscchannel", query);
1873  else if (query.next())
1874  {
1875  major = query.value(0).toUInt();
1876  minor = query.value(1).toUInt();
1877  return true;
1878  }
1879 
1880  return false;
1881 }
1882 
1884  uint sourceid,
1885  uint &chanid, const QString &channum,
1886  QString &tvformat, QString &modulation,
1887  QString &freqtable, QString &freqid,
1888  int &finetune, uint64_t &frequency,
1889  QString &dtv_si_std, int &mpeg_prog_num,
1890  uint &atsc_major, uint &atsc_minor,
1891  uint &dvb_transportid, uint &dvb_networkid,
1892  uint &mplexid,
1893  bool &commfree)
1894 {
1895  chanid = 0;
1896  tvformat.clear();
1897  modulation.clear();
1898  freqtable.clear();;
1899  freqid.clear();
1900  dtv_si_std.clear();
1901  finetune = 0;
1902  frequency = 0;
1903  mpeg_prog_num = -1;
1904  atsc_major = atsc_minor = mplexid = 0;
1905  dvb_networkid = dvb_transportid = 0;
1906  commfree = false;
1907 
1908  int found = 0;
1910  query.prepare(
1911  "SELECT finetune, freqid, tvformat, freqtable, "
1912  " commmethod, mplexid, "
1913  " atsc_major_chan, atsc_minor_chan, serviceid, "
1914  " chanid, visible "
1915  "FROM channel, videosource "
1916  "WHERE channel.deleted IS NULL AND "
1917  " videosource.sourceid = channel.sourceid AND "
1918  " channum = :CHANNUM AND "
1919  " channel.sourceid = :SOURCEID "
1920  "ORDER BY channel.visible > 0 DESC, channel.chanid ");
1921  query.bindValue(":CHANNUM", channum);
1922  query.bindValue(":SOURCEID", sourceid);
1923 
1924  if (!query.exec() || !query.isActive())
1925  {
1926  MythDB::DBError("GetChannelData", query);
1927  return false;
1928  }
1929 
1930  if (query.next())
1931  {
1932  finetune = query.value(0).toInt();
1933  freqid = query.value(1).toString();
1934  tvformat = query.value(2).toString();
1935  freqtable = query.value(3).toString();
1936  commfree = (query.value(4).toInt() == -2);
1937  mplexid = query.value(5).toUInt();
1938  atsc_major = query.value(6).toUInt();
1939  atsc_minor = query.value(7).toUInt();
1940  mpeg_prog_num = (query.value(8).isNull()) ? -1
1941  : query.value(8).toInt();
1942  chanid = query.value(9).toUInt();
1943 
1944  found += query.value(10).toInt();
1945  }
1946 
1947  while (query.next())
1948  found += query.value(10).toInt();
1949 
1950  if (found == 0 && chanid)
1951  {
1952  LOG(VB_GENERAL, LOG_WARNING,
1953  QString("No visible channels for %1, using invisble chanid %2")
1954  .arg(channum).arg(chanid));
1955  }
1956 
1957  if (found > 1)
1958  {
1959  LOG(VB_GENERAL, LOG_WARNING,
1960  QString("Found multiple visible channels for %1, using chanid %2")
1961  .arg(channum).arg(chanid));
1962  }
1963 
1964  if (!chanid)
1965  {
1966  LOG(VB_GENERAL, LOG_ERR,
1967  QString("GetChannelData() failed because it could not\n"
1968  "\t\t\tfind channel number '%1' in DB for source '%2'.")
1969  .arg(channum).arg(sourceid));
1970  return false;
1971  }
1972 
1973  if (!mplexid || (mplexid == 32767)) /* 32767 deals with old lineups */
1974  return true;
1975 
1976  return GetTuningParams(mplexid, modulation, frequency,
1977  dvb_transportid, dvb_networkid, dtv_si_std);
1978 }
1979 
1981 {
1983  query.prepare(
1984  "SELECT type+0, url, bitrate "
1985  "FROM iptv_channel "
1986  "WHERE chanid = :CHANID "
1987  "ORDER BY type+0");
1988  query.bindValue(":CHANID", chanid);
1989 
1990  if (!query.exec())
1991  {
1992  MythDB::DBError("GetChannelData -- iptv", query);
1993  return IPTVTuningData();
1994  }
1995 
1996  QString data_url;
1997  QString fec_url0;
1998  QString fec_url1;
2000  std::array<uint,3> bitrate { 0, 0, 0, };
2001  while (query.next())
2002  {
2004  query.value(0).toUInt();
2005  switch (type)
2006  {
2007  case IPTVTuningData::kData:
2008  data_url = query.value(1).toString();
2009  bitrate[0] = query.value(2).toUInt();
2010  break;
2014  fec_url0 = query.value(1).toString();
2015  bitrate[1] = query.value(2).toUInt();
2016  break;
2020  fec_url1 = query.value(1).toString();
2021  bitrate[2] = query.value(2).toUInt();
2022  break;
2023  }
2024  switch (type)
2025  {
2026  case IPTVTuningData::kData:
2027  break;
2029  fec_type = IPTVTuningData::kRFC2733;
2030  break;
2032  fec_type = IPTVTuningData::kRFC5109;
2033  break;
2035  fec_type = IPTVTuningData::kSMPTE2022;
2036  break;
2040  break; // will be handled by type of first FEC stream
2041  }
2042  }
2043 
2044  IPTVTuningData tuning(data_url, bitrate[0], fec_type,
2045  fec_url0, bitrate[1], fec_url1, bitrate[2]);
2046  LOG(VB_GENERAL, LOG_INFO, QString("Loaded %1 for %2")
2047  .arg(tuning.GetDeviceName()).arg(chanid));
2048  return tuning;
2049 }
2050 
2051 // TODO This should be modified to load a complete channelinfo object including
2052 // all fields from the database
2057  uint sourceid, bool visible_only, bool include_disconnected,
2058  const QString &group_by, uint channel_groupid)
2059 {
2060  ChannelInfoList list;
2061 
2063 
2064  QString qstr = QString(
2065  "SELECT channum, callsign, channel.chanid, "
2066  " atsc_major_chan, atsc_minor_chan, "
2067  " name, icon, mplexid, visible, "
2068  " channel.sourceid, GROUP_CONCAT(DISTINCT capturecard.cardid), "
2069  " GROUP_CONCAT(DISTINCT channelgroup.grpid), "
2070  " xmltvid "
2071  "FROM channel "
2072  "LEFT JOIN channelgroup ON channel.chanid = channelgroup.chanid "
2073  " %1 JOIN capturecard ON capturecard.sourceid = channel.sourceid ")
2074  .arg((include_disconnected) ? "LEFT" : "");
2075 
2076  qstr += "WHERE deleted IS NULL ";
2077 
2078  if (sourceid)
2079  qstr += QString("AND channel.sourceid='%1' ").arg(sourceid);
2080 
2081  // Select only channels from the specified channel group
2082  if (channel_groupid > 0)
2083  qstr += QString("AND channelgroup.grpid = '%1' ").arg(channel_groupid);
2084 
2085  if (visible_only)
2086  qstr += QString("AND visible > 0 ");
2087 
2088  qstr += " GROUP BY chanid";
2089 
2090  if (!group_by.isEmpty())
2091  qstr += QString(", %1").arg(group_by);
2092 
2093  query.prepare(qstr);
2094  if (!query.exec())
2095  {
2096  MythDB::DBError("ChannelUtil::GetChannels()", query);
2097  return list;
2098  }
2099 
2100  while (query.next())
2101  {
2102  if (query.value(0).toString().isEmpty() || !query.value(2).toBool())
2103  continue; // skip if channum blank, or chanid empty
2104 
2105  ChannelInfo chan(
2106  query.value(0).toString(), /* channum */
2107  query.value(1).toString(), /* callsign */
2108  query.value(2).toUInt(), /* chanid */
2109  query.value(3).toUInt(), /* ATSC major */
2110  query.value(4).toUInt(), /* ATSC minor */
2111  query.value(7).toUInt(), /* mplexid */
2112  static_cast<ChannelVisibleType>(query.value(8).toInt()),
2113  /* visible */
2114  query.value(5).toString(), /* name */
2115  query.value(6).toString(), /* icon */
2116  query.value(9).toUInt()); /* sourceid */
2117 
2118  chan.m_xmltvId = query.value(12).toString(); /* xmltvid */
2119 
2120  QStringList inputIDs = query.value(11).toString().split(",");
2121  while (!inputIDs.isEmpty())
2122  chan.AddInputId(inputIDs.takeFirst().toUInt());
2123 
2124  QStringList groupIDs = query.value(10).toString().split(",");
2125  while (!groupIDs.isEmpty())
2126  chan.AddGroupId(groupIDs.takeFirst().toUInt());
2127 
2128  list.push_back(chan);
2129 
2130  }
2131 
2132  return list;
2133 }
2134 
2135 std::vector<uint> ChannelUtil::GetChanIDs(int sourceid, bool onlyVisible)
2136 {
2138 
2139  QString select = "SELECT chanid FROM channel WHERE deleted IS NULL ";
2140  // Yes, this a little ugly
2141  if (onlyVisible || sourceid > 0)
2142  {
2143  if (onlyVisible)
2144  select += "AND visible > 0 ";
2145  if (sourceid > 0)
2146  select += "AND sourceid=" + QString::number(sourceid);
2147  }
2148 
2149  std::vector<uint> list;
2150  query.prepare(select);
2151  if (!query.exec())
2152  {
2153  MythDB::DBError("SourceUtil::GetChanIDs()", query);
2154  return list;
2155  }
2156 
2157  while (query.next())
2158  list.push_back(query.value(0).toUInt());
2159 
2160  return list;
2161 }
2162 
2163 inline bool lt_callsign(const ChannelInfo &a, const ChannelInfo &b)
2164 {
2165  return naturalCompare(a.m_callSign, b.m_callSign) < 0;
2166 }
2167 
2168 inline bool lt_smart(const ChannelInfo &a, const ChannelInfo &b)
2169 {
2170  static QMutex s_sepExprLock;
2171  static const QRegularExpression kSepExpr(ChannelUtil::kATSCSeparators);
2172 
2173  bool isIntA = false;
2174  bool isIntB = false;
2175  int a_int = a.m_chanNum.toUInt(&isIntA);
2176  int b_int = b.m_chanNum.toUInt(&isIntB);
2177  int a_major = a.m_atscMajorChan;
2178  int b_major = b.m_atscMajorChan;
2179  int a_minor = a.m_atscMinorChan;
2180  int b_minor = b.m_atscMinorChan;
2181 
2182  // Extract minor and major numbers from channum..
2183  int idxA = 0;
2184  int idxB = 0;
2185  {
2186  QMutexLocker locker(&s_sepExprLock);
2187  idxA = a.m_chanNum.indexOf(kSepExpr);
2188  idxB = b.m_chanNum.indexOf(kSepExpr);
2189  }
2190  if (idxA >= 0)
2191  {
2192  bool tmp1 = false;
2193  bool tmp2 = false;
2194  int major = a.m_chanNum.leftRef(idxA).toUInt(&tmp1);
2195  int minor = a.m_chanNum.midRef(idxA+1).toUInt(&tmp2);
2196  if (tmp1 && tmp2)
2197  (a_major = major), (a_minor = minor), (isIntA = false);
2198  }
2199 
2200  if (idxB >= 0)
2201  {
2202  bool tmp1 = false;
2203  bool tmp2 = false;
2204  int major = b.m_chanNum.leftRef(idxB).toUInt(&tmp1);
2205  int minor = b.m_chanNum.midRef(idxB+1).toUInt(&tmp2);
2206  if (tmp1 && tmp2)
2207  (b_major = major), (b_minor = minor), (isIntB = false);
2208  }
2209 
2210  // If ATSC channel has been renumbered, sort by new channel number
2211  if ((a_minor > 0) && isIntA)
2212  {
2213  int atsc_int = (QString("%1%2").arg(a_major).arg(a_minor)).toInt();
2214  a_minor = (atsc_int == a_int) ? a_minor : 0;
2215  }
2216 
2217  if ((b_minor > 0) && isIntB)
2218  {
2219  int atsc_int = (QString("%1%2").arg(b_major).arg(b_minor)).toInt();
2220  b_minor = (atsc_int == b_int) ? b_minor : 0;
2221  }
2222 
2223  // one of the channels is an ATSC channel, and the other
2224  // is either ATSC or is numeric.
2225  if ((a_minor || b_minor) &&
2226  (a_minor || isIntA) && (b_minor || isIntB))
2227  {
2228  int a_maj = (!a_minor && isIntA) ? a_int : a_major;
2229  int b_maj = (!b_minor && isIntB) ? b_int : b_major;
2230  int cmp = 0;
2231  if ((cmp = a_maj - b_maj))
2232  return cmp < 0;
2233 
2234  if ((cmp = a_minor - b_minor))
2235  return cmp < 0;
2236  }
2237 
2238  if (isIntA && isIntB)
2239  {
2240  // both channels have a numeric channum
2241  int cmp = a_int - b_int;
2242  if (cmp)
2243  return cmp < 0;
2244  }
2245  else if (isIntA ^ isIntB)
2246  {
2247  // if only one is channel numeric always consider it less than
2248  return isIntA;
2249  }
2250  else
2251  {
2252  // neither of channels have a numeric channum
2253  int cmp = naturalCompare(a.m_chanNum, b.m_chanNum);
2254  if (cmp)
2255  return cmp < 0;
2256  }
2257 
2258  return lt_callsign(a,b);
2259 }
2260 
2262 {
2264  QString select;
2265 
2266 
2267  select = "SELECT chanid FROM channel WHERE deleted IS NULL ";
2268  if (sourceid >= 0)
2269  select += "AND sourceid=" + QString::number(sourceid);
2270  select += ';';
2271 
2272  query.prepare(select);
2273 
2274  if (!query.exec() || !query.isActive())
2275  return 0;
2276 
2277  return query.size();
2278 }
2279 
2280 void ChannelUtil::SortChannels(ChannelInfoList &list, const QString &order,
2281  bool eliminate_duplicates)
2282 {
2283  bool cs = order.toLower() == "callsign";
2284  if (cs)
2285  stable_sort(list.begin(), list.end(), lt_callsign);
2286  else /* if (sortorder == "channum") */
2287  stable_sort(list.begin(), list.end(), lt_smart);
2288 
2289  if (eliminate_duplicates && !list.empty())
2290  {
2292  tmp.push_back(list[0]);
2293  for (size_t i = 1; i < list.size(); i++)
2294  {
2295  if ((cs && lt_callsign(tmp.back(), list[i])) ||
2296  (!cs && lt_smart(tmp.back(), list[i])))
2297  {
2298  tmp.push_back(list[i]);
2299  }
2300  }
2301 
2302  list = tmp;
2303  }
2304 }
2305 
2306 // Return the array index of the best matching channel. An exact
2307 // match is the best match. Otherwise, find the closest numerical
2308 // value greater than channum. E.g., if the channel list is {2_1,
2309 // 2_2, 4_1, 4_2, 300} then input 3 returns 2_2, input 4 returns 2_2,
2310 // and input 5 returns 4_2.
2311 //
2312 // The list does not need to be sorted.
2314  const QString &channum)
2315 {
2316  ChannelInfo target;
2317  target.m_chanNum = channum;
2318  int b = -1; // index of best seen so far
2319  for (int i = 0; i < (int)list.size(); ++i)
2320  {
2321  // Index i is a better result if any of the following hold:
2322  // i is the first element seen
2323  // i < target < best (i.e., i is the first one less than the target)
2324  // best < i < target
2325  // target < i < best
2326  if ((b < 0) ||
2327  (lt_smart(list[i], target) && lt_smart(target, list[b])) ||
2328  (lt_smart(list[b], list[i]) && lt_smart(list[i], target)) ||
2329  (lt_smart(target, list[i]) && lt_smart(list[i], list[b])))
2330  {
2331  b = i;
2332  }
2333  }
2334  return b;
2335 }
2336 
2338  const ChannelInfoList &sorted,
2339  uint old_chanid,
2340  uint mplexid_restriction,
2341  uint chanid_restriction,
2342  ChannelChangeDirection direction,
2343  bool skip_non_visible,
2344  bool skip_same_channum_and_callsign)
2345 {
2346  auto it = find(sorted.cbegin(), sorted.cend(), old_chanid);
2347 
2348  if (it == sorted.end())
2349  it = sorted.begin(); // not in list, pretend we are on first channel
2350 
2351  if (it == sorted.end())
2352  return 0; // no channels..
2353 
2354  auto start = it;
2355 
2356  if (CHANNEL_DIRECTION_DOWN == direction)
2357  {
2358  do
2359  {
2360  if (it == sorted.begin())
2361  {
2362  it = find(sorted.begin(), sorted.end(),
2363  sorted.rbegin()->m_chanId);
2364  if (it == sorted.end())
2365  {
2366  --it;
2367  }
2368  }
2369  else
2370  --it;
2371  }
2372  while ((it != start) &&
2373  ((skip_non_visible && it->m_visible < kChannelVisible) ||
2374  (skip_same_channum_and_callsign &&
2375  it->m_chanNum == start->m_chanNum &&
2376  it->m_callSign == start->m_callSign) ||
2377  ((mplexid_restriction != 0U) &&
2378  (mplexid_restriction != it->m_mplexId)) ||
2379  ((chanid_restriction != 0U) &&
2380  (chanid_restriction != it->m_chanId))));
2381  }
2382  else if ((CHANNEL_DIRECTION_UP == direction) ||
2383  (CHANNEL_DIRECTION_FAVORITE == direction))
2384  {
2385  do
2386  {
2387  ++it;
2388  if (it == sorted.end())
2389  it = sorted.begin();
2390  }
2391  while ((it != start) &&
2392  ((skip_non_visible && it->m_visible < kChannelVisible) ||
2393  (skip_same_channum_and_callsign &&
2394  it->m_chanNum == start->m_chanNum &&
2395  it->m_callSign == start->m_callSign) ||
2396  ((mplexid_restriction != 0U) &&
2397  (mplexid_restriction != it->m_mplexId)) ||
2398  ((chanid_restriction != 0U) &&
2399  (chanid_restriction != it->m_chanId))));
2400  }
2401 
2402  return it->m_chanId;
2403 }
2404 
2406  uint &totalAvailable,
2407  bool ignoreHidden,
2408  ChannelUtil::OrderBy orderBy,
2409  ChannelUtil::GroupBy groupBy,
2410  uint sourceID,
2411  uint channelGroupID,
2412  bool liveTVOnly,
2413  const QString& callsign,
2414  const QString& channum,
2415  bool ignoreUntunable)
2416 {
2417  ChannelInfoList channelList;
2418 
2420  QString sql = "SELECT %1 channum, freqid, channel.sourceid, "
2421  "callsign, name, icon, finetune, videofilters, xmltvid, "
2422  "channel.recpriority, channel.contrast, channel.brightness, "
2423  "channel.colour, channel.hue, tvformat, "
2424  "visible, outputfilters, useonairguide, mplexid, "
2425  "serviceid, atsc_major_chan, atsc_minor_chan, last_record, "
2426  "default_authority, commmethod, tmoffset, iptvid, "
2427  "channel.chanid, "
2428  "GROUP_CONCAT(DISTINCT channelgroup.grpid "
2429  " ORDER BY channelgroup.grpid), " // Creates a CSV list of channel groupids for this channel
2430  "GROUP_CONCAT(DISTINCT capturecard.cardid "
2431  " ORDER BY livetvorder), " // Creates a CSV list of inputids for this channel
2432  "MIN(livetvorder) livetvorder "
2433  "FROM channel "
2434  "LEFT JOIN channelgroup ON channel.chanid = channelgroup.chanid ";
2435 
2436  sql += QString("%1 JOIN capturecard ON capturecard.sourceid = channel.sourceid ")
2437  .arg(ignoreUntunable ? "INNER" : "LEFT");
2438 
2439  sql += "WHERE channel.deleted IS NULL ";
2440  if (ignoreHidden)
2441  sql += "AND channel.visible > 0 ";
2442 
2443  if (channelGroupID > 0)
2444  sql += "AND channelgroup.grpid = :CHANGROUPID ";
2445 
2446  if (sourceID > 0)
2447  sql += "AND channel.sourceid = :SOURCEID ";
2448 
2449  if (liveTVOnly)
2450  sql += "AND capturecard.livetvorder > 0 ";
2451 
2452  if (groupBy == kChanGroupByCallsign)
2453  sql += "GROUP BY channel.callsign ";
2454  else if (groupBy == kChanGroupByCallsignAndChannum)
2455  sql += "GROUP BY channel.callsign, channel.channum ";
2456  else
2457  sql += "GROUP BY channel.chanid "; // We must always group for this query
2458 
2459  if (orderBy == kChanOrderByName)
2460  sql += "ORDER BY channel.name ";
2461  else if (orderBy == kChanOrderByChanNum)
2462  {
2463  // Natural sorting including subchannels e.g. 2_4, 1.3
2464  sql += "ORDER BY LPAD(CAST(channel.channum AS UNSIGNED), 10, 0), "
2465  " LPAD(channel.channum, 10, 0) ";
2466  }
2467  else // kChanOrderByLiveTV
2468  {
2469  sql += "ORDER BY callsign = :CALLSIGN1 AND channum = :CHANNUM DESC, "
2470  " callsign = :CALLSIGN2 DESC, "
2471  " livetvorder, "
2472  " channel.recpriority DESC, "
2473  " chanid ";
2474  }
2475 
2476  if (count > 0)
2477  sql += "LIMIT :LIMIT ";
2478 
2479  if (startIndex > 0)
2480  sql += "OFFSET :STARTINDEX ";
2481 
2482 
2483  if (startIndex > 0 || count > 0)
2484  sql = sql.arg("SQL_CALC_FOUND_ROWS");
2485  else
2486  sql = sql.arg(""); // remove place holder
2487 
2488  query.prepare(sql);
2489 
2490  if (channelGroupID > 0)
2491  query.bindValue(":CHANGROUPID", channelGroupID);
2492 
2493  if (sourceID > 0)
2494  query.bindValue(":SOURCEID", sourceID);
2495 
2496  if (count > 0)
2497  query.bindValue(":LIMIT", count);
2498 
2499  if (startIndex > 0)
2500  query.bindValue(":STARTINDEX", startIndex);
2501 
2502  if (orderBy == kChanOrderByLiveTV)
2503  {
2504  query.bindValue(":CALLSIGN1", callsign);
2505  query.bindValue(":CHANNUM", channum);
2506  query.bindValue(":CALLSIGN2", callsign);
2507  }
2508 
2509  if (!query.exec())
2510  {
2511  MythDB::DBError("ChannelInfo::Load()", query);
2512  return channelList;
2513  }
2514 
2515  while (query.next())
2516  {
2517  ChannelInfo channelInfo;
2518  channelInfo.m_chanNum = query.value(0).toString();
2519  channelInfo.m_freqId = query.value(1).toString();
2520  channelInfo.m_sourceId = query.value(2).toUInt();
2521  channelInfo.m_callSign = query.value(3).toString();
2522  channelInfo.m_name = query.value(4).toString();
2523  channelInfo.m_icon = query.value(5).toString();
2524  channelInfo.m_fineTune = query.value(6).toInt();
2525  channelInfo.m_videoFilters = query.value(7).toString();
2526  channelInfo.m_xmltvId = query.value(8).toString();
2527  channelInfo.m_recPriority = query.value(9).toInt();
2528  channelInfo.m_contrast = query.value(10).toUInt();
2529  channelInfo.m_brightness = query.value(11).toUInt();
2530  channelInfo.m_colour = query.value(12).toUInt();
2531  channelInfo.m_hue = query.value(13).toUInt();
2532  channelInfo.m_tvFormat = query.value(14).toString();
2533  channelInfo.m_visible =
2534  static_cast<ChannelVisibleType>(query.value(15).toInt());
2535  channelInfo.m_outputFilters = query.value(16).toString();
2536  channelInfo.m_useOnAirGuide = query.value(17).toBool();
2537  channelInfo.m_mplexId = query.value(18).toUInt();
2538  channelInfo.m_serviceId = query.value(19).toUInt();
2539  channelInfo.m_atscMajorChan = query.value(20).toUInt();
2540  channelInfo.m_atscMinorChan = query.value(21).toUInt();
2541  channelInfo.m_lastRecord = query.value(22).toDateTime();
2542  channelInfo.m_defaultAuthority = query.value(23).toString();
2543  channelInfo.m_commMethod = query.value(24).toUInt();
2544  channelInfo.m_tmOffset = query.value(25).toUInt();
2545  channelInfo.m_iptvId = query.value(26).toUInt();
2546  channelInfo.m_chanId = query.value(27).toUInt();
2547 
2548  QStringList groupIDs = query.value(28).toString().split(",");
2549  while (!groupIDs.isEmpty())
2550  channelInfo.AddGroupId(groupIDs.takeFirst().toUInt());
2551 
2552  QStringList inputIDs = query.value(29).toString().split(",");
2553  while (!inputIDs.isEmpty())
2554  channelInfo.AddInputId(inputIDs.takeFirst().toUInt());
2555 
2556  channelList.push_back(channelInfo);
2557  }
2558 
2559  if ((startIndex > 0 || count > 0) &&
2560  query.exec("SELECT FOUND_ROWS()") && query.next())
2561  totalAvailable = query.value(0).toUInt();
2562  else
2563  totalAvailable = query.size();
2564 
2565  return channelList;
2566 }
2567 
2568 /* vim: set expandtab tabstop=4 shiftwidth=4: */
ChannelInfo
Definition: channelinfo.h:32
DTVModulation::toString
QString toString() const
Definition: dtvconfparserhelpers.h:410
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:204
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:126
DTVMultiplex
Definition: dtvmultiplex.h:25
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:869
MSqlQuery::size
int size(void) const
Definition: mythdbcon.h:203
TerrestrialDeliverySystemDescriptor::HierarchyString
QString HierarchyString(void) const
Definition: dvbdescriptors.h:974
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:343
TerrestrialDeliverySystemDescriptor::GuardIntervalString
QString GuardIntervalString(void) const
Definition: dvbdescriptors.h:1020
ChannelInfo::m_outputFilters
QString m_outputFilters
Definition: channelinfo.h:107
DTVTransmitMode::toChar
QChar toChar() const
Definition: dtvconfparserhelpers.h:474
ChannelUtil::GetTimeOffset
static int GetTimeOffset(int chan_id)
Returns the listings time offset in minutes for given channel.
Definition: channelutil.cpp:780
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:204
DTVPolarity::toChar
QChar toChar() const
Definition: dtvconfparserhelpers.h:631
ChannelChangeDirection
ChannelChangeDirection
ChannelChangeDirection is an enumeration of possible channel changing directions.
Definition: tv.h:29
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:2405
kChannelNeverVisible
@ kChannelNeverVisible
Definition: channelinfo.h:25
ChannelUtil::DeleteChannel
static bool DeleteChannel(uint channel_id)
Definition: channelutil.cpp:1766
ChannelInfo::m_freqId
QString m_freqId
Definition: channelinfo.h:87
pid_cache_item_t
Definition: channelutil.h:24
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)
Definition: channelutil.cpp:2337
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:196
IPTVTuningData::kNone
@ kNone
Definition: iptvtuningdata.h:21
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:1855
ChannelInfo::AddInputId
void AddInputId(uint linputid)
Definition: channelinfo.h:71
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:198
arg
arg(title).arg(filename).arg(doDelete))
DTVMultiplex::m_hierarchy
DTVHierarchy m_hierarchy
Definition: dtvmultiplex.h:103
CableDeliverySystemDescriptor
Definition: dvbdescriptors.h:717
ChannelInfo::m_atscMajorChan
uint m_atscMajorChan
Definition: channelinfo.h:113
IPTVTuningData::GetDataURL
QUrl GetDataURL(void) const
Definition: iptvtuningdata.h:134
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
IPTVTuningData::GetFECURL0
QUrl GetFECURL0(void) const
Definition: iptvtuningdata.h:135
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:1795
ChannelInsertInfo::m_hidden
bool m_hidden
Definition: channelinfo.h:224
ChannelUtil::kChanOrderByLiveTV
@ kChanOrderByLiveTV
Definition: channelutil.h:199
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
IPTVTuningData::kSMPTE2022_1
@ kSMPTE2022_1
Definition: iptvtuningdata.h:34
ChannelUtil::kChanOrderByName
@ kChanOrderByName
Definition: channelutil.h:198
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:731
ChannelUtil::GetIPTVTuningData
static IPTVTuningData GetIPTVTuningData(uint chanid)
Definition: channelutil.cpp:1980
DTVCodeRate::toString
QString toString() const
Definition: dtvconfparserhelpers.h:339
ChannelUtil::GetChannelStringField
static QString GetChannelStringField(int chan_id, const QString &field)
Definition: channelutil.cpp:754
CableDeliverySystemDescriptor::FECInnerString
QString FECInnerString(void) const
Definition: dvbdescriptors.h:790
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:1482
ChannelUtil::GetUnknownCallsign
static QString GetUnknownCallsign(void)
Definition: channelutil.cpp:1305
ChannelInfo::m_icon
QString m_icon
Definition: channelinfo.h:93
TerrestrialDeliverySystemDescriptor
Definition: dvbdescriptors.h:905
DescriptorID::cable_delivery_system
@ cable_delivery_system
Definition: mpegdescriptors.h:76
tmp
static guint32 * tmp
Definition: goom_core.cpp:31
ChannelInsertInfo
Definition: channelinfo.h:134
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:2280
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
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:139
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:932
DTVRollOff::toString
QString toString() const
Definition: dtvconfparserhelpers.h:766
MPEGDescriptor::Parse
static desc_list_t Parse(const unsigned char *data, uint len)
Definition: mpegdescriptors.cpp:15
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:2056
DTVMultiplex::m_guardInterval
DTVGuardInterval m_guardInterval
Definition: dtvmultiplex.h:102
ChannelInfo::m_atscMinorChan
uint m_atscMinorChan
Definition: channelinfo.h:114
MPEGDescriptor
Definition: mpegdescriptors.h:301
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
SatelliteDeliverySystemDescriptor::RollOffString
QString RollOffString(void) const
Definition: dvbdescriptors.h:849
pid_cache_t
std::vector< pid_cache_item_t > pid_cache_t
Definition: channelutil.h:42
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:178
IPTVTuningData::GetDeviceName
QString GetDeviceName(void) const
Definition: iptvtuningdata.h:108
lt_smart
bool lt_smart(const ChannelInfo &a, const ChannelInfo &b)
Definition: channelutil.cpp:2168
IPTVTuningData::GetFECURL1
QUrl GetFECURL1(void) const
Definition: iptvtuningdata.h:136
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:953
ChannelInsertInfo::m_channelId
uint m_channelId
Definition: channelinfo.h:215
DTVMultiplex::m_hpCodeRate
DTVCodeRate m_hpCodeRate
Definition: dtvmultiplex.h:98
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:898
IPTVTuningData
Definition: iptvtuningdata.h:17
IPTVTuningData::kRFC2733
@ kRFC2733
Definition: iptvtuningdata.h:22
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:592
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:23
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:21
naturalCompare
int naturalCompare(const QString &_a, const QString &_b, Qt::CaseSensitivity caseSensitivity)
Definition: mythmiscutil.cpp:1033
ChannelInfo::m_contrast
uint m_contrast
Definition: channelinfo.h:100
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:141
IPTVTuningData::FECType
FECType
Definition: iptvtuningdata.h:20
IPTVTuningData::IPTVType
IPTVType
Definition: iptvtuningdata.h:28
ChannelInfo::AddGroupId
void AddGroupId(uint lgroupid)
Definition: channelinfo.h:59
IPTVTuningData::kRFC5109_1
@ kRFC5109_1
Definition: iptvtuningdata.h:32
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
SatelliteDeliverySystemDescriptor::FrequencykHz
unsigned long long FrequencykHz(void) const
Definition: dvbdescriptors.h:811
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:584
ChannelUtil::UpdateInsertInfoFromDB
static void UpdateInsertInfoFromDB(ChannelInsertInfo &chan)
Definition: channelutil.cpp:1669
lt_callsign
bool lt_callsign(const ChannelInfo &a, const ChannelInfo &b)
Definition: channelutil.cpp:2163
channelutil.h
DTVMultiplex::m_symbolRate
uint64_t m_symbolRate
Definition: dtvmultiplex.h:95
DTVModulationSystem::toString
QString toString() const
Definition: dtvconfparserhelpers.h:717
IPTVTuningData::kSMPTE2022
@ kSMPTE2022
Definition: iptvtuningdata.h:24
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:1562
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:1883
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:1004
ChannelUtil::SetServiceVersion
static bool SetServiceVersion(int mplexid, int version)
Definition: channelutil.cpp:1814
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:29
mythmiscutil.h
kChannelVisible
@ kChannelVisible
Definition: channelinfo.h:23
IPTVTuningData::kSMPTE2022_2
@ kSMPTE2022_2
Definition: iptvtuningdata.h:35
IPTVTuningData::kRFC2733_1
@ kRFC2733_1
Definition: iptvtuningdata.h:30
IPTVTuningData::kRFC5109_2
@ kRFC5109_2
Definition: iptvtuningdata.h:33
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:203
SatelliteDeliverySystemDescriptor::ModulationSystemString
QString ModulationSystemString(void) const
Definition: dvbdescriptors.h:856
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:760
SatelliteDeliverySystemDescriptor::SymbolRateHz
uint SymbolRateHz(void) const
Definition: dvbdescriptors.h:881
DTVBandwidth::toChar
QChar toChar() const
Definition: dtvconfparserhelpers.h:267
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
ChannelUtil::GetSourceIDForChannel
static uint GetSourceIDForChannel(uint chanid)
Definition: channelutil.cpp:805
ChannelUtil::GetServiceVersion
static int GetServiceVersion(int mplexid)
Definition: channelutil.cpp:1833
ChannelUtil::UpdateChannelNumberFromDB
static void UpdateChannelNumberFromDB(ChannelInsertInfo &chan)
Definition: channelutil.cpp:1643
TerrestrialDeliverySystemDescriptor::FrequencyHz
uint64_t FrequencyHz(void) const
Definition: dvbdescriptors.h:920
ChannelUtil::kChanGroupByCallsignAndChannum
@ kChanGroupByCallsignAndChannum
Definition: channelutil.h:205
ChannelInfo::m_sourceId
uint m_sourceId
Definition: channelinfo.h:89
IPTVTuningData::GetFECTypeString
QString GetFECTypeString(uint i) const
Definition: iptvtuningdata.h:151
DTVMultiplex::m_sistandard
QString m_sistandard
Definition: dtvmultiplex.h:111
TerrestrialDeliverySystemDescriptor::TransmissionModeString
QString TransmissionModeString(void) const
Definition: dvbdescriptors.h:1033
ChannelInfo::m_tvFormat
QString m_tvFormat
Definition: channelinfo.h:105
SatelliteDeliverySystemDescriptor::PolarizationString
QString PolarizationString() const
Definition: dvbdescriptors.h:831
ChannelInfo::m_recPriority
int m_recPriority
Definition: channelinfo.h:98
SatelliteDeliverySystemDescriptor
Definition: dvbdescriptors.h:796
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:995
ChannelInfo::m_xmltvId
QString m_xmltvId
Definition: channelinfo.h:97
CableDeliverySystemDescriptor::SymbolRateHz
uint SymbolRateHz(void) const
Definition: dvbdescriptors.h:773
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:2261
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:2313
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:2135
DTVGuardInterval::toString
QString toString() const
Definition: dtvconfparserhelpers.h:538
IPTVTuningData::kData
@ kData
Definition: iptvtuningdata.h:29
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:1173
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:31
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
ChannelInfo::m_visible
ChannelVisibleType m_visible
Definition: channelinfo.h:106
query
MSqlQuery query(MSqlQuery::InitCon())
IPTVTuningData::GetBitrate
uint GetBitrate(uint i) const
Definition: iptvtuningdata.h:147
nv_python_libs.bbciplayer.bbciplayer_api.version
string version
Definition: bbciplayer_api.py:81
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:330
SatelliteDeliverySystemDescriptor::ModulationString
QString ModulationString(void) const
Definition: dvbdescriptors.h:870
ChannelUtil::kChanOrderByChanNum
@ kChanOrderByChanNum
Definition: channelutil.h:197
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:356
ChannelUtil::kChanGroupByCallsign
@ kChanGroupByCallsign
Definition: channelutil.h:204
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:1708
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:808
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:31
HLSReader.h
ChannelInfoList
std::vector< ChannelInfo > ChannelInfoList
Definition: channelinfo.h:131