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