MythTV  master
satiputils.cpp
Go to the documentation of this file.
1 // C++
2 #include <chrono>
3 #include <thread>
4 
5 // Qt
6 #include <QString>
7 #include <QStringList>
8 
9 // MythTV headers
11 #include "libmythbase/mythtimer.h"
12 #include "libmythupnp/ssdp.h"
13 
14 #include "cardutil.h"
15 #include "satiputils.h"
16 
17 #define LOC QString("SatIP: ")
18 
19 namespace {
20  const QString SATIP_URI = "urn:ses-com:device:SatIPServer:1";
21  constexpr std::chrono::milliseconds SEARCH_TIME_MS { 3s };
22 }
23 
24 QStringList SatIP::probeDevices(void)
25 {
26  const std::chrono::milliseconds totalSearchTime = SEARCH_TIME_MS;
27  auto seconds = duration_cast<std::chrono::seconds>(totalSearchTime);
28 
29  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Using UPNP to search for Sat>IP servers (%1 secs)")
30  .arg(seconds.count()));
31 
33 
34  MythTimer totalTime; totalTime.start();
35  MythTimer searchTime; searchTime.start();
36 
37  while (totalTime.elapsed() < totalSearchTime)
38  {
39  std::this_thread::sleep_for(25ms);
40  std::chrono::milliseconds ttl = totalSearchTime - totalTime.elapsed();
41  if (searchTime.elapsed() > 249ms && ttl > 1s)
42  {
43  auto ttl_s = duration_cast<std::chrono::seconds>(ttl);
44  LOG(VB_GENERAL, LOG_DEBUG, LOC + QString("UPNP search %1 ms")
45  .arg(ttl_s.count()));
47  searchTime.start();
48  }
49  }
50 
51  return SatIP::doUPNPsearch(true);
52 }
53 
54 QStringList SatIP::doUPNPsearch(bool loginfo)
55 {
56  QStringList result;
57 
58  SSDPCacheEntries *satipservers = SSDP::Find(SATIP_URI);
59 
60  if (!satipservers)
61  {
62  LOG(VB_GENERAL, LOG_INFO, LOC + "No UPnP Sat>IP servers found");
63  return {};
64  }
65 
66  int count = satipservers->Count();
67  if (count > 0)
68  {
69  if (loginfo)
70  {
71  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Found %1 possible Sat>IP servers").arg(count));
72  }
73  }
74  else
75  {
76  LOG(VB_GENERAL, LOG_ERR, LOC + "No UPnP Sat>IP servers found, but SSDP::Find() != NULL");
77  }
78 
79  EntryMap map;
80  satipservers->GetEntryMap(map);
81 
82  for (auto *BE : std::as_const(map))
83  {
84  QString friendlyName = BE->GetFriendlyName();
85  UPnpDeviceDesc *desc = BE->GetDeviceDesc();
86 
87  if (!desc)
88  {
89  LOG(VB_GENERAL, LOG_ERR, LOC + QString("GetDeviceDesc() failed for %1").arg(friendlyName));
90  continue;
91  }
92 
93  QString ip = desc->m_hostUrl.host();
94  QString id = desc->m_rootDevice.GetUDN();
95  QList<NameValue> extraAttribs = desc->m_rootDevice.m_lstExtra;
96 
97  for (const auto& attrib : extraAttribs)
98  {
99  if (attrib.m_sName == "satip:X_SATIPCAP")
100  {
101  QStringList caps = attrib.m_sValue.split(",");
102 
103  for (const auto& cap : std::as_const(caps))
104  {
105  QStringList tuner = cap.split("-");
106 
107  if (tuner.size() != 2)
108  continue;
109 
110  int num_tuners = tuner.at(1).toInt();
111  for (int i = 0; i < num_tuners; i++)
112  {
113  QString device = QString("%1 %2 %3 %4 %5")
114  .arg(id,
115  friendlyName.remove(" "),
116  ip,
117  QString::number(i),
118  tuner.at(0));
119  result << device;
120  if (loginfo)
121  {
122  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Found %1").arg(device));
123  }
124  }
125  }
126  }
127  }
128  BE->DecrRef();
129  }
130 
131  satipservers->DecrRef();
132  satipservers = nullptr;
133 
134  return result;
135 }
136 
137 QStringList SatIP::findServers(void)
138 {
139  QStringList devs;
140  SSDPCacheEntries *satipservers = SSDP::Find(SATIP_URI);
141  if (satipservers && satipservers->Count() > 0)
142  {
143  devs = SatIP::doUPNPsearch(false);
144  }
145  else
146  {
147  devs = SatIP::probeDevices();
148  }
149  return devs;
150 }
151 
152 QString SatIP::findDeviceIP(const QString& deviceuuid)
153 {
154  QStringList devs = SatIP::findServers();
155 
156  for (const auto& dev : std::as_const(devs))
157  {
158  QStringList devinfo = dev.split(" ");
159  const QString& id = devinfo.at(0);
160 
161  if (id.toUpper() == deviceuuid.toUpper())
162  {
163  return devinfo.at(2);
164  }
165  }
166  return nullptr;
167 }
168 
170 {
171  QStringList dev = deviceid.split(":");
172  if (dev.length() < 3)
173  {
175  }
176 
177  QString type = dev.at(2).toUpper();
178  if (type == "DVBC")
179  {
180  return CardUtil::DVBC;
181  }
182  if (type == "DVBC2")
183  {
184  return CardUtil::DVBC; // DVB-C2 is not supported yet.
185  }
186  if (type == "DVBT")
187  {
188  return CardUtil::DVBT;
189  }
190  if (type == "DVBT2")
191  {
192  return CardUtil::DVBT2;
193  }
194  if (type == "DVBS2")
195  {
196  return CardUtil::DVBS2;
197  }
199 }
200 
201 int SatIP::toTunerType(const QString& deviceid)
202 {
203  QStringList devinfo = deviceid.split(":");
204  if (devinfo.length() < 3)
205  {
207  }
208 
209  QString type = devinfo.at(2).toUpper();
210 
211  if (type.startsWith("DVBC")) // DVB-C2 is not supported yet.
212  {
214  }
215  if (type == "DVBT")
216  {
218  }
219  if (type == "DVBT2")
220  {
222  }
223  if (type == "DVBS")
224  {
226  }
227  if (type == "DVBS2")
228  {
230  }
231 
233 }
234 
236 {
238  {
239  return "5";
240  }
242  {
243  return "6";
244  }
246  {
247  return "7";
248  }
250  {
251  return "8";
252  }
254  {
255  return "10";
256  }
258  {
259  return "1.712";
260  }
262  {
263  return "auto";
264  }
265  return "auto";
266 }
267 
268 QString SatIP::freq(uint64_t freq)
269 {
270  return QString::number(freq / 1000000.0, 'f', 2);
271 }
272 
274 {
276  {
277  return "dvbs";
278  }
280  {
281  return "dvbs2";
282  }
284  {
285  return "dvbt";
286  }
288  {
289  return "dvbt2";
290  }
292  {
293  return "dvbc";
294  }
295  // Not supported yet: DVB-C2
296  return "auto";
297 }
298 
300 {
302  {
303  return "qpsk";
304  }
306  {
307  return "8psk";
308  }
310  {
311  return "16qam";
312  }
314  {
315  return "32qam";
316  }
318  {
319  return "64qam";
320  }
322  {
323  return "128qam";
324  }
326  {
327  return "256qam";
328  }
329  return "auto";
330 }
331 
333 {
335  {
336  return "2k";
337  }
339  {
340  return "4k";
341  }
343  {
344  return "8k";
345  }
347  {
348  return "1k";
349  }
351  {
352  return "16k";
353  }
355  {
356  return "32k";
357  }
358  return "auto";
359 }
360 
362 {
364  {
365  return "14";
366  }
368  {
369  return "18";
370  }
372  {
373  return "116";
374  }
376  {
377  return "132";
378  }
380  {
381  return "1128";
382  }
384  {
385  return "19128";
386  }
388  {
389  return "19256";
390  }
391  return "auto";
392 }
393 
395 {
396  if (fec == DTVCodeRate::kFEC_1_2)
397  {
398  return "12";
399  }
400  if (fec == DTVCodeRate::kFEC_2_3)
401  {
402  return "23";
403  }
404  if (fec == DTVCodeRate::kFEC_3_4)
405  {
406  return "34";
407  }
408  if (fec == DTVCodeRate::kFEC_3_5)
409  {
410  return "35";
411  }
412  if (fec == DTVCodeRate::kFEC_4_5)
413  {
414  return "45";
415  }
416  if (fec == DTVCodeRate::kFEC_5_6)
417  {
418  return "56";
419  }
420  if (fec == DTVCodeRate::kFEC_6_7)
421  {
422  return "67";
423  }
424  if (fec == DTVCodeRate::kFEC_7_8)
425  {
426  return "78";
427  }
428  if (fec == DTVCodeRate::kFEC_8_9)
429  {
430  return "89";
431  }
433  {
434  return "910";
435  }
436  return "auto";
437 }
438 
440 {
442  {
443  return "0.35";
444  }
446  {
447  return "0.20";
448  }
450  {
451  return "0.25";
452  }
454  {
455  return "auto";
456  }
457  return "auto";
458 }
459 
461 {
463  {
464  return "v";
465  }
467  {
468  return "h";
469  }
471  {
472  return "r";
473  }
475  {
476  return "l";
477  }
478  return "auto";
479 }
DTVRollOff::kRollOff_25
@ kRollOff_25
Definition: dtvconfparserhelpers.h:738
CardUtil::ERROR_UNKNOWN
@ ERROR_UNKNOWN
Definition: cardutil.h:52
MythTimer::elapsed
std::chrono::milliseconds elapsed(void)
Returns milliseconds elapsed since last start() or restart()
Definition: mythtimer.cpp:91
DTVGuardInterval::kGuardInterval_1_16
@ kGuardInterval_1_16
Definition: dtvconfparserhelpers.h:499
DTVRollOff::kRollOff_20
@ kRollOff_20
Definition: dtvconfparserhelpers.h:737
DTVBandwidth
Definition: dtvconfparserhelpers.h:217
DTVCodeRate::kFEC_1_2
@ kFEC_1_2
Definition: dtvconfparserhelpers.h:292
CardUtil::DVBS2
@ DVBS2
Definition: cardutil.h:64
DTVTunerType::kTunerTypeDVBC
static const int kTunerTypeDVBC
Definition: dtvconfparserhelpers.h:95
DTVModulation::kModulationQAM256
@ kModulationQAM256
Definition: dtvconfparserhelpers.h:363
ReferenceCounter::DecrRef
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
Definition: referencecounter.cpp:125
DTVPolarity::kPolarityLeft
@ kPolarityLeft
Definition: dtvconfparserhelpers.h:617
DTVTransmitMode::kTransmissionMode2K
@ kTransmissionMode2K
Definition: dtvconfparserhelpers.h:435
SSDP::PerformSearch
void PerformSearch(const QString &sST, std::chrono::seconds timeout=2s)
Definition: ssdp.cpp:204
MythTimer
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
DTVModulationSystem::kModulationSystem_DVBT
@ kModulationSystem_DVBT
Definition: dtvconfparserhelpers.h:659
SatIP::ro
static QString ro(DTVRollOff ro)
Definition: satiputils.cpp:439
DTVGuardInterval::kGuardInterval_1_128
@ kGuardInterval_1_128
Definition: dtvconfparserhelpers.h:503
ssdp.h
DTVModulationSystem::kModulationSystem_DVBS2
@ kModulationSystem_DVBS2
Definition: dtvconfparserhelpers.h:662
DTVTunerType::kTunerTypeDVBS1
static const int kTunerTypeDVBS1
Definition: dtvconfparserhelpers.h:93
DTVCodeRate::kFEC_7_8
@ kFEC_7_8
Definition: dtvconfparserhelpers.h:298
DTVTransmitMode::kTransmissionMode4K
@ kTransmissionMode4K
Definition: dtvconfparserhelpers.h:438
freq
static const std::array< const uint32_t, 4 > freq
Definition: element.cpp:45
SatIP::bw
static QString bw(DTVBandwidth bw)
Definition: satiputils.cpp:235
UPnpDeviceDesc
Definition: upnpdevice.h:151
DTVTransmitMode::kTransmissionMode8K
@ kTransmissionMode8K
Definition: dtvconfparserhelpers.h:436
DTVTransmitMode::kTransmissionMode32K
@ kTransmissionMode32K
Definition: dtvconfparserhelpers.h:441
DTVGuardInterval::kGuardInterval_19_128
@ kGuardInterval_19_128
Definition: dtvconfparserhelpers.h:504
SSDP::Find
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:132
DTVTransmitMode::kTransmissionMode16K
@ kTransmissionMode16K
Definition: dtvconfparserhelpers.h:440
DTVCodeRate::kFEC_3_5
@ kFEC_3_5
Definition: dtvconfparserhelpers.h:301
MythTimer::start
void start(void)
starts measuring elapsed time.
Definition: mythtimer.cpp:47
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
DTVBandwidth::kBandwidth10MHz
@ kBandwidth10MHz
Definition: dtvconfparserhelpers.h:233
SatIP::tmode
static QString tmode(DTVTransmitMode tmode)
Definition: satiputils.cpp:332
SSDPCacheEntries::GetEntryMap
void GetEntryMap(EntryMap &map)
Returns a copy of the EntryMap.
Definition: ssdpcache.cpp:85
DTVGuardInterval::kGuardInterval_19_256
@ kGuardInterval_19_256
Definition: dtvconfparserhelpers.h:505
anonymous_namespace{satiputils.cpp}::SEARCH_TIME_MS
constexpr std::chrono::milliseconds SEARCH_TIME_MS
Definition: satiputils.cpp:21
SatIP::doUPNPsearch
static QStringList doUPNPsearch(bool loginfo)
Definition: satiputils.cpp:54
DTVGuardInterval
Definition: dtvconfparserhelpers.h:487
DTVCodeRate::kFEC_2_3
@ kFEC_2_3
Definition: dtvconfparserhelpers.h:293
SatIP::findDeviceIP
static QString findDeviceIP(const QString &deviceuuid)
Definition: satiputils.cpp:152
DTVCodeRate
Definition: dtvconfparserhelpers.h:280
DTVBandwidth::kBandwidthAuto
@ kBandwidthAuto
Definition: dtvconfparserhelpers.h:231
satiputils.h
SSDP::Instance
static SSDP * Instance()
Definition: ssdp.cpp:55
DTVGuardInterval::kGuardInterval_1_8
@ kGuardInterval_1_8
Definition: dtvconfparserhelpers.h:500
UPnpDeviceDesc::m_rootDevice
UPnpDevice m_rootDevice
Definition: upnpdevice.h:155
DTVBandwidth::kBandwidth8MHz
@ kBandwidth8MHz
Definition: dtvconfparserhelpers.h:228
CardUtil::DVBT
@ DVBT
Definition: cardutil.h:56
mythlogging.h
SatIP::probeDevices
static QStringList probeDevices(void)
Definition: satiputils.cpp:24
DTVGuardInterval::kGuardInterval_1_4
@ kGuardInterval_1_4
Definition: dtvconfparserhelpers.h:501
DTVRollOff::kRollOff_35
@ kRollOff_35
Definition: dtvconfparserhelpers.h:736
DTVBandwidth::kBandwidth7MHz
@ kBandwidth7MHz
Definition: dtvconfparserhelpers.h:229
DTVModulation::kModulationQAM16
@ kModulationQAM16
Definition: dtvconfparserhelpers.h:359
UPnpDevice::m_lstExtra
NameValues m_lstExtra
Definition: upnpdevice.h:119
DTVTunerType::kTunerTypeUnknown
static const int kTunerTypeUnknown
Definition: dtvconfparserhelpers.h:103
LOC
#define LOC
Definition: satiputils.cpp:17
SatIP::toTunerType
static int toTunerType(const QString &deviceid)
Definition: satiputils.cpp:201
DTVTransmitMode
Definition: dtvconfparserhelpers.h:424
SatIP::findServers
static QStringList findServers(void)
Definition: satiputils.cpp:137
DTVModulation::kModulationQAM32
@ kModulationQAM32
Definition: dtvconfparserhelpers.h:360
EntryMap
QMap< QString, DeviceLocation * > EntryMap
Key == Unique Service Name (USN)
Definition: ssdpcache.h:29
SatIP::toDVBInputType
static CardUtil::INPUT_TYPES toDVBInputType(const QString &deviceid)
Definition: satiputils.cpp:169
UPnpDeviceDesc::m_hostUrl
QUrl m_hostUrl
Definition: upnpdevice.h:157
SatIP::freq
static QString freq(uint64_t freq)
Definition: satiputils.cpp:268
CardUtil::DVBC
@ DVBC
Definition: cardutil.h:55
SatIP::gi
static QString gi(DTVGuardInterval gi)
Definition: satiputils.cpp:361
DTVPolarity::kPolarityVertical
@ kPolarityVertical
Definition: dtvconfparserhelpers.h:614
DTVRollOff::kRollOff_Auto
@ kRollOff_Auto
Definition: dtvconfparserhelpers.h:739
SSDPCacheEntries
Definition: ssdpcache.h:35
DTVCodeRate::kFEC_9_10
@ kFEC_9_10
Definition: dtvconfparserhelpers.h:302
SatIP::pol
static QString pol(DTVPolarity pol)
Definition: satiputils.cpp:460
DTVCodeRate::kFEC_5_6
@ kFEC_5_6
Definition: dtvconfparserhelpers.h:296
DTVTunerType::kTunerTypeDVBS2
static const int kTunerTypeDVBS2
Definition: dtvconfparserhelpers.h:94
DTVModulationSystem::kModulationSystem_DVBS
@ kModulationSystem_DVBS
Definition: dtvconfparserhelpers.h:661
DTVModulation::kModulationQAM128
@ kModulationQAM128
Definition: dtvconfparserhelpers.h:362
DTVCodeRate::kFEC_3_4
@ kFEC_3_4
Definition: dtvconfparserhelpers.h:294
DTVCodeRate::kFEC_4_5
@ kFEC_4_5
Definition: dtvconfparserhelpers.h:295
anonymous_namespace{satiputils.cpp}::SATIP_URI
const QString SATIP_URI
Definition: satiputils.cpp:20
DTVModulationSystem
Definition: dtvconfparserhelpers.h:644
cardutil.h
DTVModulationSystem::kModulationSystem_DVBT2
@ kModulationSystem_DVBT2
Definition: dtvconfparserhelpers.h:672
DTVModulationSystem::kModulationSystem_DVBC_ANNEX_A
@ kModulationSystem_DVBC_ANNEX_A
Definition: dtvconfparserhelpers.h:657
DTVPolarity::kPolarityRight
@ kPolarityRight
Definition: dtvconfparserhelpers.h:616
DTVCodeRate::kFEC_8_9
@ kFEC_8_9
Definition: dtvconfparserhelpers.h:299
SatIP::fec
static QString fec(DTVCodeRate fec)
Definition: satiputils.cpp:394
mythtimer.h
DTVCodeRate::kFEC_6_7
@ kFEC_6_7
Definition: dtvconfparserhelpers.h:297
CardUtil::DVBT2
@ DVBT2
Definition: cardutil.h:71
DTVGuardInterval::kGuardInterval_1_32
@ kGuardInterval_1_32
Definition: dtvconfparserhelpers.h:498
DTVTunerType::kTunerTypeDVBT
static const int kTunerTypeDVBT
Definition: dtvconfparserhelpers.h:96
CardUtil::INPUT_TYPES
INPUT_TYPES
all the different inputs
Definition: cardutil.h:49
DTVModulation::kModulationQAM64
@ kModulationQAM64
Definition: dtvconfparserhelpers.h:361
UPnpDevice::GetUDN
QString GetUDN(void) const
Definition: upnpdevice.cpp:759
SSDPCacheEntries::Count
uint Count(void) const
Definition: ssdpcache.h:45
SatIP::msys
static QString msys(DTVModulationSystem msys)
Definition: satiputils.cpp:273
DTVTransmitMode::kTransmissionMode1K
@ kTransmissionMode1K
Definition: dtvconfparserhelpers.h:439
DTVTunerType::kTunerTypeDVBT2
static const int kTunerTypeDVBT2
Definition: dtvconfparserhelpers.h:97
SatIP::mtype
static QString mtype(DTVModulation mtype)
Definition: satiputils.cpp:299
DTVBandwidth::kBandwidth5MHz
@ kBandwidth5MHz
Definition: dtvconfparserhelpers.h:232
DTVModulation::kModulationQPSK
@ kModulationQPSK
Definition: dtvconfparserhelpers.h:358
DTVBandwidth::kBandwidth1712kHz
@ kBandwidth1712kHz
Definition: dtvconfparserhelpers.h:234
DTVModulation::kModulation8PSK
@ kModulation8PSK
Definition: dtvconfparserhelpers.h:367
DTVBandwidth::kBandwidth6MHz
@ kBandwidth6MHz
Definition: dtvconfparserhelpers.h:230
DTVPolarity
Definition: dtvconfparserhelpers.h:605
DTVModulation
Definition: dtvconfparserhelpers.h:347
DTVPolarity::kPolarityHorizontal
@ kPolarityHorizontal
Definition: dtvconfparserhelpers.h:615
DTVRollOff
Definition: dtvconfparserhelpers.h:725