MythTV  master
upnpscanner.cpp
Go to the documentation of this file.
1 #include <QCoreApplication>
2 #include <QTextCodec>
3 
4 #include "mythcorecontext.h"
5 #include "mythlogging.h"
6 #include "ssdp.h"
7 #include "upnpscanner.h"
8 
9 #include <chrono> // for milliseconds
10 #include <thread> // for sleep_for
11 #include <utility>
12 
13 #define LOC QString("UPnPScan: ")
14 #define ERR QString("UPnPScan error: ")
15 
16 #define MAX_ATTEMPTS 5
17 #define MAX_REQUESTS 1
18 
20 {
21  // items don't need scanning
22  if (!m_url.isEmpty())
23  return QString();
24 
25  // scan this container
26  if (!m_scanned)
27  return m_id;
28 
29  // scan children
30  QMutableMapIterator<QString,MediaServerItem> it(m_children);
31  while (it.hasNext())
32  {
33  it.next();
34  QString result = it.value().NextUnbrowsed();
35  if (!result.isEmpty())
36  return result;
37  }
38 
39  return QString();
40 }
41 
43 {
44  if (m_id == id)
45  return this;
46 
47  QMutableMapIterator<QString,MediaServerItem> it(m_children);
48  while (it.hasNext())
49  {
50  it.next();
51  MediaServerItem* result = it.value().Find(id);
52  if (result)
53  return result;
54  }
55 
56  return nullptr;
57 }
58 
60 {
61  if (m_id == item.m_parentid)
62  {
63  m_children.insert(item.m_id, item);
64  return true;
65  }
66  return false;
67 }
68 
70 {
71  m_children.clear();
72  m_scanned = false;
73 }
74 
79 class MediaServer : public MediaServerItem
80 {
81  public:
83  : MediaServerItem(QString("0"), QString(), QString(), QString()),
84  m_controlURL(QUrl()),
85  m_eventSubURL(QUrl()), m_eventSubPath(QString()),
86  m_friendlyName(QString("Unknown"))
87  {
88  }
89  explicit MediaServer(QUrl URL)
90  : MediaServerItem(QString("0"), QString(), QString(), QString()),
91  m_url(std::move(URL)), m_controlURL(QUrl()),
92  m_eventSubURL(QUrl()), m_eventSubPath(QString()),
93  m_friendlyName(QString("Unknown"))
94  {
95  }
96 
97  bool ResetContent(int new_id)
98  {
99  bool result = true;
100  if (m_systemUpdateID != -1)
101  {
102  result = false;
103  Reset();
104  }
105  m_systemUpdateID = new_id;
106  return result;
107  }
108 
109  QUrl m_url;
113  QString m_eventSubPath;
114  QString m_friendlyName;
115  bool m_subscribed {false};
118 };
119 
123 QMutex* UPNPScanner::gUPNPScannerLock = new QMutex(QMutex::Recursive);
124 
136 {
137  Stop();
138 }
139 
144 void UPNPScanner::Enable(bool enable, UPNPSubscription *sub)
145 {
146  QMutexLocker locker(gUPNPScannerLock);
147  gUPNPScannerEnabled = enable;
148  Instance(sub);
149 }
150 
157 {
158  QMutexLocker locker(gUPNPScannerLock);
159  if (!gUPNPScannerEnabled)
160  {
161  if (gUPNPScannerThread)
162  {
165  }
166  delete gUPNPScannerThread;
167  gUPNPScannerThread = nullptr;
168  delete gUPNPScanner;
169  gUPNPScanner = nullptr;
170  return nullptr;
171  }
172 
173  if (!gUPNPScannerThread)
174  gUPNPScannerThread = new MThread("UPnPScanner");
175  if (!gUPNPScanner)
176  gUPNPScanner = new UPNPScanner(sub);
177 
179  {
180  gUPNPScanner->moveToThread(gUPNPScannerThread->qthread());
181  QObject::connect(
182  gUPNPScannerThread->qthread(), SIGNAL(started()),
183  gUPNPScanner, SLOT(Start()));
184  gUPNPScannerThread->start(QThread::LowestPriority);
185  }
186 
187  return gUPNPScanner;
188 }
195 {
196  m_fullscan = true;
197  auto *me = new MythEvent(QString("UPNP_STARTSCAN"));
198  qApp->postEvent(this, me);
199 }
200 
208  meta_dir_node *node)
209 {
210  // nothing to see..
211  QMap<QString,QString> servers = ServerList();
212  if (servers.isEmpty())
213  return;
214 
215  // Add MediaServers
216  LOG(VB_GENERAL, LOG_INFO, QString("Adding MediaServer metadata."));
217 
218  smart_dir_node mediaservers = node->addSubDir(tr("Media Servers"));
219  mediaservers->setPathRoot();
220 
221  m_lock.lock();
222  QMutableHashIterator<QString,MediaServer*> it(m_servers);
223  while (it.hasNext())
224  {
225  it.next();
226  if (!it.value()->m_subscribed)
227  continue;
228 
229  QString usn = it.key();
230  GetServerContent(usn, it.value(), list, mediaservers.get());
231  }
232  m_lock.unlock();
233 }
234 
240  meta_dir_node *node)
241 {
242  // nothing to see..
243  QMap<QString,QString> servers = ServerList();
244  if (servers.isEmpty())
245  return;
246 
247  // Start scanning if it isn't already running
248  StartFullScan();
249 
250  // wait for the scanner to complete - with a 30 second timeout
251  LOG(VB_GENERAL, LOG_INFO, LOC + "Waiting for scan to complete.");
252 
253  int count = 0;
254  while (!m_scanComplete && (count++ < 300))
255  std::this_thread::sleep_for(std::chrono::milliseconds(100));
256 
257  // some scans may just take too long (PlayOn)
258  if (!m_scanComplete)
259  LOG(VB_GENERAL, LOG_ERR, LOC + "MediaServer scan is incomplete.");
260  else
261  LOG(VB_GENERAL, LOG_INFO, LOC + "MediaServer scanning finished.");
262 
263 
264  smart_dir_node mediaservers = node->addSubDir(tr("Media Servers"));
265  mediaservers->setPathRoot();
266 
267  m_lock.lock();
268  QMutableHashIterator<QString,MediaServer*> it(m_servers);
269  while (it.hasNext())
270  {
271  it.next();
272  if (!it.value()->m_subscribed)
273  continue;
274 
275  QString usn = it.key();
276  GetServerContent(usn, it.value(), list, mediaservers.get());
277  }
278  m_lock.unlock();
279 }
280 
281 bool UPNPScanner::GetMetadata(QVariant &data)
282 {
283  // we need a USN and objectID
284  if (!data.canConvert(QVariant::StringList))
285  return false;
286 
287  QStringList list = data.toStringList();
288  if (list.size() != 2)
289  return false;
290 
291  QString usn = list[0];
292  QString object = list[1];
293 
294  m_lock.lock();
295  bool valid = m_servers.contains(usn);
296  if (valid)
297  {
298  MediaServerItem* item = m_servers[usn]->Find(object);
299  valid = item ? !item->m_scanned : false;
300  }
301  m_lock.unlock();
302  if (!valid)
303  return false;
304 
305  auto *me = new MythEvent("UPNP_BROWSEOBJECT", list);
306  qApp->postEvent(this, me);
307 
308  int count = 0;
309  bool found = false;
310  LOG(VB_GENERAL, LOG_INFO, "START");
311  while (!found && (count++ < 100)) // 10 seconds
312  {
313  std::this_thread::sleep_for(std::chrono::milliseconds(100));
314  m_lock.lock();
315  if (m_servers.contains(usn))
316  {
317  MediaServerItem *item = m_servers[usn]->Find(object);
318  if (item)
319  {
320  found = item->m_scanned;
321  }
322  else
323  {
324  LOG(VB_GENERAL, LOG_INFO, QString("Item went away..."));
325  found = true;
326  }
327  }
328  else
329  {
330  LOG(VB_GENERAL, LOG_INFO,
331  QString("Server went away while browsing."));
332  found = true;
333  }
334  m_lock.unlock();
335  }
336  LOG(VB_GENERAL, LOG_INFO, "END");
337  return true;
338 }
339 
348  meta_dir_node *node)
349 {
350  if (!content->m_scanned)
351  {
352  smart_dir_node subnode = node->addSubDir(content->m_name);
353 
354  QStringList data;
355  data << usn;
356  data << content->m_id;
357  subnode->SetData(data);
358 
360  item->SetTitle(QString("Dummy"));
361  list->push_back(item);
362  subnode->addEntry(smart_meta_node(new meta_data_node(item.get())));
363  return;
364  }
365 
366  node->SetData(QVariant());
367 
368  if (content->m_url.isEmpty())
369  {
370  smart_dir_node container = node->addSubDir(content->m_name);
371  QMutableMapIterator<QString,MediaServerItem> it(content->m_children);
372  while (it.hasNext())
373  {
374  it.next();
375  GetServerContent(usn, &it.value(), list, container.get());
376  }
377  return;
378  }
379 
381  item->SetTitle(content->m_name);
382  list->push_back(item);
383  node->addEntry(smart_meta_node(new meta_data_node(item.get())));
384 }
385 
391 QMap<QString,QString> UPNPScanner::ServerList(void)
392 {
393  QMap<QString,QString> servers;
394  m_lock.lock();
395  QHashIterator<QString,MediaServer*> it(m_servers);
396  while (it.hasNext())
397  {
398  it.next();
399  servers.insert(it.key(), it.value()->m_friendlyName);
400  }
401  m_lock.unlock();
402  return servers;
403 }
404 
411 {
412  m_lock.lock();
413 
414  // create our network handler
415  m_network = new QNetworkAccessManager();
416  connect(m_network, SIGNAL(finished(QNetworkReply*)),
417  this, SLOT(replyFinished(QNetworkReply*)));
418 
419  // listen for SSDP updates
420  SSDP::AddListener(this);
421 
422  // listen for subscriptions and events
423  if (m_subscription)
425 
426  // create our update timer (driven by AddServer and ParseDescription)
427  m_updateTimer = new QTimer(this);
428  m_updateTimer->setSingleShot(true);
429  connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(Update()));
430 
431  // create our watchdog timer (checks for stale servers)
432  m_watchdogTimer = new QTimer(this);
433  connect(m_watchdogTimer, SIGNAL(timeout()), this, SLOT(CheckStatus()));
434  m_watchdogTimer->start(1000 * 10); // every 10s
435 
436  // avoid connecting to the master backend
439 
440  m_lock.unlock();
441  LOG(VB_GENERAL, LOG_INFO, LOC + "Started");
442 }
443 
449 {
450  m_lock.lock();
451 
452  // stop listening
453  SSDP::RemoveListener(this);
454  if (m_subscription)
456 
457  // disable updates
458  if (m_updateTimer)
459  m_updateTimer->stop();
460  if (m_watchdogTimer)
461  m_watchdogTimer->stop();
462 
463  // cleanup our servers and subscriptions
464  QHashIterator<QString,MediaServer*> it(m_servers);
465  while (it.hasNext())
466  {
467  it.next();
468  if (m_subscription && it.value()->m_subscribed)
469  m_subscription->Unsubscribe(it.key());
470  if (it.value()->m_renewalTimerId)
471  killTimer(it.value()->m_renewalTimerId);
472  delete it.value();
473  }
474  m_servers.clear();
475 
476  // cleanup the network
477  foreach (QNetworkReply *reply, m_descriptionRequests)
478  {
479  reply->abort();
480  delete reply;
481  }
482  m_descriptionRequests.clear();
483  foreach (QNetworkReply *reply, m_browseRequests)
484  {
485  reply->abort();
486  delete reply;
487  }
488  m_browseRequests.clear();
489  delete m_network;
490  m_network = nullptr;
491 
492  // delete the timers
493  delete m_updateTimer;
494  delete m_watchdogTimer;
495  m_updateTimer = nullptr;
496  m_watchdogTimer = nullptr;
497 
498  m_lock.unlock();
499  LOG(VB_GENERAL, LOG_INFO, LOC + "Finished");
500 }
501 
508 {
509  // decide which servers still need to be checked
510  m_lock.lock();
511  if (m_servers.isEmpty())
512  {
513  m_lock.unlock();
514  return;
515  }
516 
517  // if our network queue is full, then we may need to come back later
518  bool reschedule = false;
519 
520  QHashIterator<QString,MediaServer*> it(m_servers);
521  while (it.hasNext())
522  {
523  it.next();
524  if ((it.value()->m_connectionAttempts < MAX_ATTEMPTS) &&
525  (it.value()->m_controlURL.isEmpty()))
526  {
527  bool sent = false;
528  QUrl url = it.value()->m_url;
529  if (!m_descriptionRequests.contains(url) &&
530  (m_descriptionRequests.empty()) &&
531  url.isValid())
532  {
533  QNetworkReply *reply = m_network->get(QNetworkRequest(url));
534  if (reply)
535  {
536  sent = true;
537  m_descriptionRequests.insert(url, reply);
538  it.value()->m_connectionAttempts++;
539  }
540  }
541  if (!sent)
542  reschedule = true;
543  }
544  }
545 
546  if (reschedule)
547  ScheduleUpdate();
548  m_lock.unlock();
549 }
550 
556 {
557  // FIXME
558  // Remove stale servers - the SSDP cache code does not send out removal
559  // notifications for expired (rather than explicitly closed) connections
560  m_lock.lock();
561  QMutableHashIterator<QString,MediaServer*> it(m_servers);
562  while (it.hasNext())
563  {
564  it.next();
565  // FIXME UPNP version comparision done wrong, we are using urn:schemas-upnp-org:device:MediaServer:4 ourselves
566  if (!SSDP::Find("urn:schemas-upnp-org:device:MediaServer:1", it.key()))
567  {
568  LOG(VB_UPNP, LOG_INFO, LOC +
569  QString("%1 no longer in SSDP cache. Removing")
570  .arg(it.value()->m_url.toString()));
571  MediaServer* last = it.value();
572  it.remove();
573  delete last;
574  }
575  }
576  m_lock.unlock();
577 }
578 
584 void UPNPScanner::replyFinished(QNetworkReply *reply)
585 {
586  if (!reply)
587  return;
588 
589  QUrl url = reply->url();
590  bool valid = reply->error() == QNetworkReply::NoError;
591 
592  if (!valid)
593  {
594  LOG(VB_UPNP, LOG_ERR, LOC +
595  QString("Network request for '%1' returned error '%2'")
596  .arg(url.toString()).arg(reply->errorString()));
597  }
598 
599  bool description = false;
600  bool browse = false;
601 
602  m_lock.lock();
603  if (m_descriptionRequests.contains(url, reply))
604  {
605  m_descriptionRequests.remove(url, reply);
606  description = true;
607  }
608  else if (m_browseRequests.contains(url, reply))
609  {
610  m_browseRequests.remove(url, reply);
611  browse = true;
612  }
613  m_lock.unlock();
614 
615  if (browse && valid)
616  {
617  ParseBrowse(url, reply);
618  if (m_fullscan)
620  }
621  else if (description)
622  {
623  if (!valid || !ParseDescription(url, reply))
624  {
625  // if there will be no more attempts, update the logs
626  CheckFailure(url);
627  // try again
628  ScheduleUpdate();
629  }
630  }
631  else
632  LOG(VB_UPNP, LOG_ERR, LOC + "Received unknown reply");
633 
634  reply->deleteLater();
635 }
636 
640 void UPNPScanner::customEvent(QEvent *event)
641 {
642  if (event->type() != MythEvent::MythEventMessage)
643  return;
644 
645  // UPnP events
646  auto *me = dynamic_cast<MythEvent *>(event);
647  if (me == nullptr)
648  return;
649 
650  const QString& ev = me->Message();
651 
652  if (ev == "UPNP_STARTSCAN")
653  {
655  return;
656  }
657  if (ev == "UPNP_BROWSEOBJECT")
658  {
659  if (me->ExtraDataCount() == 2)
660  {
661  QUrl url;
662  const QString& usn = me->ExtraData(0);
663  const QString& objectid = me->ExtraData(1);
664  m_lock.lock();
665  if (m_servers.contains(usn))
666  {
667  url = m_servers[usn]->m_controlURL;
668  LOG(VB_GENERAL, LOG_INFO, QString("UPNP_BROWSEOBJECT: %1->%2")
669  .arg(m_servers[usn]->m_friendlyName).arg(objectid));
670  }
671  m_lock.unlock();
672  if (!url.isEmpty())
673  SendBrowseRequest(url, objectid);
674  }
675  return;
676  }
677  if (ev == "UPNP_EVENT")
678  {
679  auto *info = (MythInfoMapEvent*)event;
680  if (!info)
681  return;
682  if (!info->GetInfoMap())
683  return;
684 
685  QString usn = info->GetInfoMap()->value("usn");
686  QString id = info->GetInfoMap()->value("SystemUpdateID");
687  if (usn.isEmpty() || id.isEmpty())
688  return;
689 
690  m_lock.lock();
691  if (m_servers.contains(usn))
692  {
693  int newid = id.toInt();
694  if (m_servers[usn]->m_systemUpdateID != newid)
695  {
696  m_scanComplete &= m_servers[usn]->ResetContent(newid);
697  LOG(VB_GENERAL, LOG_INFO, LOC +
698  QString("New SystemUpdateID '%1' for %2").arg(id).arg(usn));
699  Debug();
700  }
701  }
702  m_lock.unlock();
703  return;
704  }
705 
706  // process SSDP cache updates
707  QString uri = me->ExtraDataCount() > 0 ? me->ExtraData(0) : QString();
708  QString usn = me->ExtraDataCount() > 1 ? me->ExtraData(1) : QString();
709 
710  // FIXME UPNP version comparision done wrong, we are using urn:schemas-upnp-org:device:MediaServer:4 ourselves
711  if (uri == "urn:schemas-upnp-org:device:MediaServer:1")
712  {
713  QString url = (ev == "SSDP_ADD") ? me->ExtraData(2) : QString();
714  AddServer(usn, url);
715  }
716 }
717 
722 void UPNPScanner::timerEvent(QTimerEvent * event)
723 {
724  int id = event->timerId();
725  if (id)
726  killTimer(id);
727 
728  int timeout = 0;
729  QString usn;
730 
731  m_lock.lock();
732  QHashIterator<QString,MediaServer*> it(m_servers);
733  while (it.hasNext())
734  {
735  it.next();
736  if (it.value()->m_renewalTimerId == id)
737  {
738  it.value()->m_renewalTimerId = 0;
739  usn = it.key();
740  if (m_subscription)
741  timeout = m_subscription->Renew(usn);
742  }
743  }
744  m_lock.unlock();
745 
746  if (timeout > 0)
747  {
748  ScheduleRenewal(usn, timeout);
749  LOG(VB_GENERAL, LOG_INFO, LOC +
750  QString("Re-subscribed for %1 seconds to %2")
751  .arg(timeout).arg(usn));
752  }
753 }
754 
759 {
760  m_lock.lock();
761  if (m_updateTimer && !m_updateTimer->isActive())
762  m_updateTimer->start(200);
763  m_lock.unlock();
764 }
765 
770 void UPNPScanner::CheckFailure(const QUrl &url)
771 {
772  m_lock.lock();
773  QHashIterator<QString,MediaServer*> it(m_servers);
774  while (it.hasNext())
775  {
776  it.next();
777  if (it.value()->m_url == url &&
778  it.value()->m_connectionAttempts == MAX_ATTEMPTS)
779  {
780  Debug();
781  break;
782  }
783  }
784  m_lock.unlock();
785 }
786 
791 {
792  m_lock.lock();
793  LOG(VB_UPNP, LOG_INFO, LOC + QString("%1 media servers discovered:")
794  .arg(m_servers.size()));
795  QHashIterator<QString,MediaServer*> it(m_servers);
796  while (it.hasNext())
797  {
798  it.next();
799  QString status = "Probing";
800  if (it.value()->m_controlURL.toString().isEmpty())
801  {
802  if (it.value()->m_connectionAttempts >= MAX_ATTEMPTS)
803  status = "Failed";
804  }
805  else
806  status = "Yes";
807  LOG(VB_UPNP, LOG_INFO, LOC +
808  QString("'%1' Connected: %2 Subscribed: %3 SystemUpdateID: "
809  "%4 timerId: %5")
810  .arg(it.value()->m_friendlyName).arg(status)
811  .arg(it.value()->m_subscribed ? "Yes" : "No")
812  .arg(it.value()->m_systemUpdateID)
813  .arg(it.value()->m_renewalTimerId));
814  }
815  m_lock.unlock();
816 }
817 
827 {
828  QMutexLocker locker(&m_lock);
829 
830  QHashIterator<QString,MediaServer*> it(m_servers);
831  bool complete = true;
832  while (it.hasNext())
833  {
834  it.next();
835  if (it.value()->m_subscribed)
836  {
837  // limit browse requests to one active per server
838  if (m_browseRequests.contains(it.value()->m_controlURL))
839  {
840  complete = false;
841  continue;
842  }
843 
844  QString next = it.value()->NextUnbrowsed();
845  if (!next.isEmpty())
846  {
847  complete = false;
848  SendBrowseRequest(it.value()->m_controlURL, next);
849  continue;
850  }
851 
852  LOG(VB_UPNP, LOG_INFO, LOC + QString("Scan completed for %1")
853  .arg(it.value()->m_friendlyName));
854  }
855  }
856 
857  if (complete)
858  {
859  LOG(VB_GENERAL, LOG_INFO, LOC +
860  QString("Media Server scan is complete."));
861  m_scanComplete = true;
862  m_fullscan = false;
863  }
864 }
865 
871 void UPNPScanner::SendBrowseRequest(const QUrl &url, const QString &objectid)
872 {
873  QNetworkRequest req = QNetworkRequest(url);
874  req.setRawHeader("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
875  req.setRawHeader("SOAPACTION",
876  "\"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"");
877 #if 0
878  req.setRawHeader("MAN", "\"http://schemasxmlsoap.org/soap/envelope/\"");
879  req.setRawHeader("01-SOAPACTION",
880  "\"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"");
881 #endif
882 
883  QByteArray body;
884  QTextStream data(&body);
885  data.setCodec(QTextCodec::codecForName("UTF-8"));
886  data << "<?xml version=\"1.0\"?>\r\n";
887  data << "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n";
888  data << " <s:Body>\r\n";
889  data << " <u:Browse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">\r\n";
890  data << " <ObjectID>" << objectid.toUtf8() << "</ObjectID>\r\n";
891  data << " <BrowseFlag>BrowseDirectChildren</BrowseFlag>\r\n";
892  data << " <Filter>*</Filter>\r\n";
893  data << " <StartingIndex>0</StartingIndex>\r\n";
894  data << " <RequestedCount>0</RequestedCount>\r\n";
895  data << " <SortCriteria></SortCriteria>\r\n";
896  data << " </u:Browse>\r\n";
897  data << " </s:Body>\r\n";
898  data << "</s:Envelope>\r\n";
899  data.flush();
900 
901  m_lock.lock();
902  QNetworkReply *reply = m_network->post(req, body);
903  if (reply)
904  m_browseRequests.insert(url, reply);
905  m_lock.unlock();
906 }
907 
913 void UPNPScanner::AddServer(const QString &usn, const QString &url)
914 {
915  if (url.isEmpty())
916  {
917  RemoveServer(usn);
918  return;
919  }
920 
921  // sometimes initialisation is too early and m_masterHost is empty
922  if (m_masterHost.isEmpty())
923  {
926  }
927 
928  QUrl qurl(url);
929  if (qurl.host() == m_masterHost && qurl.port() == m_masterPort)
930  {
931  LOG(VB_UPNP, LOG_INFO, LOC + "Ignoring master backend.");
932  return;
933  }
934 
935  m_lock.lock();
936  if (!m_servers.contains(usn))
937  {
938  m_servers.insert(usn, new MediaServer(url));
939  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Adding: %1").arg(usn));
940  ScheduleUpdate();
941  }
942  m_lock.unlock();
943 }
944 
948 void UPNPScanner::RemoveServer(const QString &usn)
949 {
950  m_lock.lock();
951  if (m_servers.contains(usn))
952  {
953  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Removing: %1").arg(usn));
954  MediaServer* old = m_servers[usn];
955  if (old->m_renewalTimerId)
956  killTimer(old->m_renewalTimerId);
957  m_servers.remove(usn);
958  delete old;
959  if (m_subscription)
960  m_subscription->Remove(usn);
961  }
962  m_lock.unlock();
963 
964  Debug();
965 }
966 
971 void UPNPScanner::ScheduleRenewal(const QString &usn, int timeout)
972 {
973  // sanitise the timeout
974  int renew = timeout - 10;
975  if (renew < 10)
976  renew = 10;
977  if (renew > 43200)
978  renew = 43200;
979 
980  m_lock.lock();
981  if (m_servers.contains(usn))
982  m_servers[usn]->m_renewalTimerId = startTimer(renew * 1000);
983  m_lock.unlock();
984 }
985 
990 void UPNPScanner::ParseBrowse(const QUrl &url, QNetworkReply *reply)
991 {
992  QByteArray data = reply->readAll();
993  if (data.isEmpty())
994  return;
995 
996  // Open the response for parsing
997  auto *parent = new QDomDocument();
998  QString errorMessage;
999  int errorLine = 0;
1000  int errorColumn = 0;
1001  if (!parent->setContent(data, false, &errorMessage, &errorLine,
1002  &errorColumn))
1003  {
1004  LOG(VB_GENERAL, LOG_ERR, LOC +
1005  QString("DIDL Parse error, Line: %1 Col: %2 Error: '%3'")
1006  .arg(errorLine).arg(errorColumn).arg(errorMessage));
1007  delete parent;
1008  return;
1009  }
1010 
1011  LOG(VB_UPNP, LOG_INFO, "\n\n" + parent->toString(4) + "\n\n");
1012 
1013  // pull out the actual result
1014  QDomDocument *result = nullptr;
1015  uint num = 0;
1016  uint total = 0;
1017  uint updateid = 0;
1018  QDomElement docElem = parent->documentElement();
1019  QDomNode n = docElem.firstChild();
1020  if (!n.isNull())
1021  result = FindResult(n, num, total, updateid);
1022  delete parent;
1023 
1024  if (!result || num < 1 || total < 1)
1025  {
1026  LOG(VB_GENERAL, LOG_ERR, LOC +
1027  QString("Failed to find result for %1") .arg(url.toString()));
1028  return;
1029  }
1030 
1031  // determine the 'server' which requested the browse
1032  m_lock.lock();
1033 
1034  MediaServer* server = nullptr;
1035  QHashIterator<QString,MediaServer*> it(m_servers);
1036  while (it.hasNext())
1037  {
1038  it.next();
1039  if (url == it.value()->m_controlURL)
1040  {
1041  server = it.value();
1042  break;
1043  }
1044  }
1045 
1046  // discard unmatched responses
1047  if (!server)
1048  {
1049  m_lock.unlock();
1050  LOG(VB_GENERAL, LOG_ERR, LOC +
1051  QString("Received unknown response for %1").arg(url.toString()));
1052  return;
1053  }
1054 
1055  // check the update ID
1056  if (server->m_systemUpdateID != (int)updateid)
1057  {
1058  // if this is not the root container, this browse will now fail
1059  // as the appropriate parentID will not be found
1060  LOG(VB_GENERAL, LOG_ERR, LOC +
1061  QString("%1 updateID changed during browse (old %2 new %3)")
1062  .arg(server->m_friendlyName).arg(server->m_systemUpdateID)
1063  .arg(updateid));
1064  m_scanComplete &= server->ResetContent(updateid);
1065  Debug();
1066  }
1067 
1068  // find containers (directories) and actual items and add them and reset
1069  // the parent when we have found the first item
1070  bool reset = true;
1071  docElem = result->documentElement();
1072  n = docElem.firstChild();
1073  while (!n.isNull())
1074  {
1075  FindItems(n, *server, reset);
1076  n = n.nextSibling();
1077  }
1078  delete result;
1079 
1080  m_lock.unlock();
1081 }
1082 
1084  bool &resetparent)
1085 {
1086  QDomElement node = n.toElement();
1087  if (node.isNull())
1088  return;
1089 
1090  if (node.tagName() == "container")
1091  {
1092  QString title = "ERROR";
1093  QDomNode next = node.firstChild();
1094  while (!next.isNull())
1095  {
1096  QDomElement container = next.toElement();
1097  if (!container.isNull() && container.tagName() == "title")
1098  title = container.text();
1099  next = next.nextSibling();
1100  }
1101 
1102  QString thisid = node.attribute("id", "ERROR");
1103  QString parentid = node.attribute("parentID", "ERROR");
1104  MediaServerItem container =
1105  MediaServerItem(thisid, parentid, title, QString());
1106  MediaServerItem *parent = content.Find(parentid);
1107  if (parent)
1108  {
1109  if (resetparent)
1110  {
1111  parent->Reset();
1112  resetparent = false;
1113  }
1114  parent->m_scanned = true;
1115  parent->Add(container);
1116  }
1117  return;
1118  }
1119 
1120  if (node.tagName() == "item")
1121  {
1122  QString title = "ERROR";
1123  QString url = "ERROR";
1124  QDomNode next = node.firstChild();
1125  while (!next.isNull())
1126  {
1127  QDomElement item = next.toElement();
1128  if (!item.isNull())
1129  {
1130  if(item.tagName() == "res")
1131  url = item.text();
1132  if(item.tagName() == "title")
1133  title = item.text();
1134  }
1135  next = next.nextSibling();
1136  }
1137 
1138  QString thisid = node.attribute("id", "ERROR");
1139  QString parentid = node.attribute("parentID", "ERROR");
1140  MediaServerItem item =
1141  MediaServerItem(thisid, parentid, title, url);
1142  item.m_scanned = true;
1143  MediaServerItem *parent = content.Find(parentid);
1144  if (parent)
1145  {
1146  if (resetparent)
1147  {
1148  parent->Reset();
1149  resetparent = false;
1150  }
1151  parent->m_scanned = true;
1152  parent->Add(item);
1153  }
1154  return;
1155  }
1156 
1157  QDomNode next = node.firstChild();
1158  while (!next.isNull())
1159  {
1160  FindItems(next, content, resetparent);
1161  next = next.nextSibling();
1162  }
1163 }
1164 
1165 QDomDocument* UPNPScanner::FindResult(const QDomNode &n, uint &num,
1166  uint &total, uint &updateid)
1167 {
1168  QDomDocument *result = nullptr;
1169  QDomElement node = n.toElement();
1170  if (node.isNull())
1171  return nullptr;
1172 
1173  if (node.tagName() == "NumberReturned")
1174  num = node.text().toUInt();
1175  if (node.tagName() == "TotalMatches")
1176  total = node.text().toUInt();
1177  if (node.tagName() == "UpdateID")
1178  updateid = node.text().toUInt();
1179  if (node.tagName() == "Result" && !result)
1180  {
1181  QString errorMessage;
1182  int errorLine = 0;
1183  int errorColumn = 0;
1184  result = new QDomDocument();
1185  if (!result->setContent(node.text(), true, &errorMessage, &errorLine, &errorColumn))
1186  {
1187  LOG(VB_GENERAL, LOG_ERR, LOC +
1188  QString("DIDL Parse error, Line: %1 Col: %2 Error: '%3'")
1189  .arg(errorLine).arg(errorColumn).arg(errorMessage));
1190  delete result;
1191  result = nullptr;
1192  }
1193  }
1194 
1195  QDomNode next = node.firstChild();
1196  while (!next.isNull())
1197  {
1198  QDomDocument *res = FindResult(next, num, total, updateid);
1199  if (res)
1200  result = res;
1201  next = next.nextSibling();
1202  }
1203  return result;
1204 }
1205 
1210 bool UPNPScanner::ParseDescription(const QUrl &url, QNetworkReply *reply)
1211 {
1212  if (url.isEmpty() || !reply)
1213  return false;
1214 
1215  QByteArray data = reply->readAll();
1216  if (data.isEmpty())
1217  {
1218  LOG(VB_GENERAL, LOG_ERR, LOC +
1219  QString("%1 returned an empty device description.")
1220  .arg(url.toString()));
1221  return false;
1222  }
1223 
1224  // parse the device description
1225  QString controlURL = QString();
1226  QString eventURL = QString();
1227  QString friendlyName = QString("Unknown");
1228  QString URLBase = QString();
1229 
1230  QDomDocument doc;
1231  QString errorMessage;
1232  int errorLine = 0;
1233  int errorColumn = 0;
1234  if (!doc.setContent(data, false, &errorMessage, &errorLine, &errorColumn))
1235  {
1236  LOG(VB_GENERAL, LOG_ERR, LOC +
1237  QString("Failed to parse device description from %1")
1238  .arg(url.toString()));
1239  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Line: %1 Col: %2 Error: '%3'")
1240  .arg(errorLine).arg(errorColumn).arg(errorMessage));
1241  return false;
1242  }
1243 
1244  QDomElement docElem = doc.documentElement();
1245  QDomNode n = docElem.firstChild();
1246  while (!n.isNull())
1247  {
1248  QDomElement e1 = n.toElement();
1249  if (!e1.isNull())
1250  {
1251  if(e1.tagName() == "device")
1252  ParseDevice(e1, controlURL, eventURL, friendlyName);
1253  if (e1.tagName() == "URLBase")
1254  URLBase = e1.text();
1255  }
1256  n = n.nextSibling();
1257  }
1258 
1259  if (controlURL.isEmpty())
1260  {
1261  LOG(VB_UPNP, LOG_ERR, LOC +
1262  QString("Failed to parse device description for %1")
1263  .arg(url.toString()));
1264  return false;
1265  }
1266 
1267  // if no URLBase was provided, use the known url
1268  if (URLBase.isEmpty())
1269  URLBase = url.toString(QUrl::RemovePath | QUrl::RemoveFragment |
1270  QUrl::RemoveQuery);
1271 
1272  // strip leading slashes off the controlURL
1273  while (!controlURL.isEmpty() && controlURL.startsWith("/"))
1274  controlURL = controlURL.mid(1);
1275 
1276  // strip leading slashes off the eventURL
1277  //while (!eventURL.isEmpty() && eventURL.startsWith("/"))
1278  // eventURL = eventURL.mid(1);
1279 
1280  // strip trailing slashes off URLBase
1281  while (!URLBase.isEmpty() && URLBase.endsWith("/"))
1282  URLBase = URLBase.mid(0, URLBase.size() - 1);
1283 
1284  controlURL = URLBase + "/" + controlURL;
1285  QString fulleventURL = URLBase + "/" + eventURL;
1286 
1287  LOG(VB_UPNP, LOG_INFO, LOC + QString("Control URL for %1 at %2")
1288  .arg(friendlyName).arg(controlURL));
1289  LOG(VB_UPNP, LOG_INFO, LOC + QString("Event URL for %1 at %2")
1290  .arg(friendlyName).arg(fulleventURL));
1291 
1292  // update the server details. If the server has gone away since the request
1293  // was posted, this will silently fail and we won't try again
1294  QString usn;
1295  QUrl qeventurl = QUrl(fulleventURL);
1296  int timeout = 0;
1297 
1298  m_lock.lock();
1299  QHashIterator<QString,MediaServer*> it(m_servers);
1300  while (it.hasNext())
1301  {
1302  it.next();
1303  if (it.value()->m_url == url)
1304  {
1305  usn = it.key();
1306  QUrl qcontrolurl(controlURL);
1307  it.value()->m_controlURL = qcontrolurl;
1308  it.value()->m_eventSubURL = qeventurl;
1309  it.value()->m_eventSubPath = eventURL;
1310  it.value()->m_friendlyName = friendlyName;
1311  it.value()->m_name = friendlyName;
1312  break;
1313  }
1314  }
1315 
1316  if (m_subscription && !usn.isEmpty())
1317  {
1318  timeout = m_subscription->Subscribe(usn, qeventurl, eventURL);
1319  m_servers[usn]->m_subscribed = (timeout > 0);
1320  }
1321  m_lock.unlock();
1322 
1323  if (timeout > 0)
1324  {
1325  LOG(VB_GENERAL, LOG_INFO, LOC +
1326  QString("Subscribed for %1 seconds to %2") .arg(timeout).arg(usn));
1327  ScheduleRenewal(usn, timeout);
1328  // we only scan servers we are subscribed to - and the scan is now
1329  // incomplete
1330  m_scanComplete = false;
1331  }
1332 
1333  Debug();
1334  return true;
1335 }
1336 
1337 
1338 void UPNPScanner::ParseDevice(QDomElement &element, QString &controlURL,
1339  QString &eventURL, QString &friendlyName)
1340 {
1341  QDomNode dev = element.firstChild();
1342  while (!dev.isNull())
1343  {
1344  QDomElement e = dev.toElement();
1345  if (!e.isNull())
1346  {
1347  if (e.tagName() == "friendlyName")
1348  friendlyName = e.text();
1349  if (e.tagName() == "serviceList")
1350  ParseServiceList(e, controlURL, eventURL);
1351  }
1352  dev = dev.nextSibling();
1353  }
1354 }
1355 
1356 void UPNPScanner::ParseServiceList(QDomElement &element, QString &controlURL,
1357  QString &eventURL)
1358 {
1359  QDomNode list = element.firstChild();
1360  while (!list.isNull())
1361  {
1362  QDomElement e = list.toElement();
1363  if (!e.isNull())
1364  if (e.tagName() == "service")
1365  ParseService(e, controlURL, eventURL);
1366  list = list.nextSibling();
1367  }
1368 }
1369 
1370 void UPNPScanner::ParseService(QDomElement &element, QString &controlURL,
1371  QString &eventURL)
1372 {
1373  bool iscds = false;
1374  QString control_url = QString();
1375  QString event_url = QString();
1376  QDomNode service = element.firstChild();
1377 
1378  while (!service.isNull())
1379  {
1380  QDomElement e = service.toElement();
1381  if (!e.isNull())
1382  {
1383  if (e.tagName() == "serviceType")
1384  // FIXME UPNP version comparision done wrong, we are using urn:schemas-upnp-org:device:MediaServer:4 ourselves
1385  iscds = (e.text() == "urn:schemas-upnp-org:service:ContentDirectory:1");
1386  if (e.tagName() == "controlURL")
1387  control_url = e.text();
1388  if (e.tagName() == "eventSubURL")
1389  event_url = e.text();
1390  }
1391  service = service.nextSibling();
1392  }
1393 
1394  if (iscds)
1395  {
1396  controlURL = control_url;
1397  eventURL = event_url;
1398  }
1399 }
#define MAX_ATTEMPTS
Definition: upnpscanner.cpp:16
QMultiMap< QUrl, QNetworkReply * > m_browseRequests
Definition: upnpscanner.h:120
void CheckStatus(void)
Removes media servers that can no longer be found in the SSDP cache.
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
void BrowseNextContainer(void)
For each known media server, find the next container which needs to be browsed and trigger sending of...
QHash< QString, MediaServer * > m_servers
Definition: upnpscanner.h:115
#define LOC
Definition: upnpscanner.cpp:13
UPnPScanner detects UPnP Media Servers available on the local network (via the UPnP SSDP cache),...
Definition: upnpscanner.h:45
static UPNPScanner * gUPNPScanner
Definition: upnpscanner.h:108
QString m_masterHost
Definition: upnpscanner.h:125
int m_renewalTimerId
void ScheduleUpdate(void)
bool Add(MediaServerItem &item)
Definition: upnpscanner.cpp:59
void GetMetadata(VideoMetadataListManager::metadata_list *list, meta_dir_node *node)
Fill the given metadata_list and meta_dir_node with the metadata of content retrieved from known medi...
static Type MythEventMessage
Definition: mythevent.h:73
void removeListener(QObject *listener)
Remove a listener to the observable.
QMultiMap< QUrl, QNetworkReply * > m_descriptionRequests
Definition: upnpscanner.h:119
void GetInitialMetadata(VideoMetadataListManager::metadata_list *list, meta_dir_node *node)
Fill the given metadata_list and meta_dir_node with the root media server metadata (i....
bool ResetContent(int new_id)
Definition: upnpscanner.cpp:97
void customEvent(QEvent *event) override
Processes subscription and SSDP cache update events.
A simple wrapper containing details about a UPnP Media Server.
Definition: mediaserver.h:32
void StartFullScan(void)
Instruct the UPNPScanner thread to start a full scan of metadata from known media servers.
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
bool m_fullscan
Definition: upnpscanner.h:129
static void AddListener(QObject *listener)
Definition: ssdp.h:121
static const char URL[]
Definition: cddb.cpp:29
UPNPScanner(UPNPSubscription *sub)
Definition: upnpscanner.h:75
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QUrl m_eventSubURL
void addListener(QObject *listener)
Add a listener to the observable.
void SetData(const QVariant &data)
void setPathRoot(bool is_root=true)
QString GetMasterServerIP(void)
Returns the Master Backend IP address If the address is an IPv6 address, the scope Id is removed.
QNetworkAccessManager * m_network
Definition: upnpscanner.h:116
void Stop(void)
Stops scanning.
This class is used as a container for messages.
Definition: mythevent.h:16
QMap< QString, MediaServerItem > m_children
Definition: upnpscanner.h:42
QDomDocument * FindResult(const QDomNode &n, uint &num, uint &total, uint &updateid)
MediaServerItem * Find(QString &id)
Definition: upnpscanner.cpp:42
void RemoveServer(const QString &usn)
QString m_friendlyName
void AddServer(const QString &usn, const QString &url)
Adds the server identified by usn and reachable via url to the list of known media servers and schedu...
UPNPSubscription * m_subscription
Definition: upnpscanner.h:113
void GetServerContent(QString &usn, MediaServerItem *content, VideoMetadataListManager::metadata_list *list, meta_dir_node *node)
Recursively search a MediaServerItem for video metadata and add it to the metadata_list and meta_dir_...
bool isRunning(void) const
Definition: mthread.cpp:274
QTimer * m_updateTimer
Definition: upnpscanner.h:122
QMap< QString, QString > ServerList(void)
Returns a list of valid Media Servers discovered on the network.
void Reset(void)
Definition: upnpscanner.cpp:69
int Renew(const QString &usn)
unsigned int uint
Definition: compat.h:140
bool m_scanComplete
Definition: upnpscanner.h:128
static void ParseDevice(QDomElement &element, QString &controlURL, QString &eventURL, QString &friendlyName)
static void RemoveListener(QObject *listener)
Definition: ssdp.h:123
void replyFinished(QNetworkReply *reply)
Validates network responses against known requests and parses expected responses for the required dat...
~UPNPScanner() override
void Remove(const QString &usn)
void ParseBrowse(const QUrl &url, QNetworkReply *reply)
Parse the XML returned from Content Directory Service browse request.
int m_connectionAttempts
QTimer * m_watchdogTimer
Definition: upnpscanner.h:123
smart_dir_node addSubDir(const QString &subdir, const QString &name="", const QString &host="", const QString &prefix="", const QVariant &data=QVariant())
QString m_eventSubPath
void Start()
Initialises the scanner, hooks it up to the subscription service and the SSDP cache and starts scanni...
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void ScheduleRenewal(const QString &usn, int timeout)
Creates a QTimer to trigger a subscription renewal for a given media server.
static MThread * gUPNPScannerThread
Definition: upnpscanner.h:110
simple_ref_ptr< meta_data_node > smart_meta_node
PictureAttribute next(PictureAttributeSupported Supported, PictureAttribute Attribute)
MediaServer(QUrl URL)
Definition: upnpscanner.cpp:89
void SendBrowseRequest(const QUrl &url, const QString &objectid)
Formulates and sends a ContentDirectory Service Browse Request to the given control URL,...
int m_systemUpdateID
bool ParseDescription(const QUrl &url, QNetworkReply *reply)
Parse the device description XML return my a media server.
static bool gUPNPScannerEnabled
Definition: upnpscanner.h:109
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
Definition: mthread.cpp:244
int Subscribe(const QString &usn, const QUrl &url, const QString &path)
void Unsubscribe(const QString &usn)
void CheckFailure(const QUrl &url)
Updates the logs for failed server connections.
T * get() const
Definition: quicksp.h:65
int GetMasterServerStatusPort(void)
Returns the Master Backend status port If no master server status port has been defined in the databa...
static void ParseService(QDomElement &element, QString &controlURL, QString &eventURL)
QString m_parentid
Definition: upnpscanner.h:38
QString NextUnbrowsed(void)
Definition: upnpscanner.cpp:19
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:126
static void Enable(bool enable, UPNPSubscription *sub=nullptr)
Creates or destroys the global UPNPScanner instance.
int m_masterPort
Definition: upnpscanner.h:126
void Update(void)
Iterates through the list of known servers and initialises a connection by requesting the device desc...
QMutex m_lock
Definition: upnpscanner.h:114
void timerEvent(QTimerEvent *event) override
Handles subscription renewal timer events.
static UPNPScanner * Instance(UPNPSubscription *sub=nullptr)
Returns the global UPNPScanner instance if it has been enabled or nullptr if UPNPScanner is currently...
void Debug(void)
static void ParseServiceList(QDomElement &element, QString &controlURL, QString &eventURL)
static QMutex * gUPNPScannerLock
Definition: upnpscanner.h:111
void addEntry(const smart_meta_node &entry)
void quit(void)
calls exit(0)
Definition: mthread.cpp:306
std::list< VideoMetadataPtr > metadata_list
void FindItems(const QDomNode &n, MediaServerItem &content, bool &resetparent)