MythTV  master
upnpscanner.h
Go to the documentation of this file.
1 #ifndef UPNPSCANNER_H
2 #define UPNPSCANNER_H
3 
4 #include <utility>
5 
6 // Qt headers
7 #include <QDomDocument>
8 #include <QMutex>
9 #include <QNetworkAccessManager>
10 #include <QNetworkReply>
11 #include <QNetworkRequest>
12 #include <QTimer>
13 
14 // MythTV headers
15 #include "upnpsubscription.h"
16 #include "mthread.h"
17 #include "upnpexp.h"
19 
20 class MediaServer;
21 class UPNPSubscription;
22 class meta_dir_node;
23 
25 {
26  public:
27  MediaServerItem() = default;
28  MediaServerItem(QString id, QString parent,
29  QString name, QString url)
30  : m_id(std::move(id)), m_parentid(std::move(parent)),
31  m_name(std::move(name)), m_url(std::move(url)) { }
32  QString NextUnbrowsed(void);
33  MediaServerItem* Find(QString &id);
34  bool Add(const MediaServerItem &item);
35  void Reset(void);
36 
37  QString m_id;
38  QString m_parentid;
39  QString m_name;
40  QString m_url;
41  bool m_scanned {false};
42  QMap<QString, MediaServerItem> m_children;
43 };
44 
45 class UPNPScanner : public QObject
46 {
47  Q_OBJECT
48 
49  public:
50  ~UPNPScanner() override;
51 
52  static void Enable(bool enable, UPNPSubscription *sub = nullptr);
53  static UPNPScanner* Instance(UPNPSubscription *sub = nullptr);
54 
55  void StartFullScan(void);
57  meta_dir_node *node);
59  meta_dir_node *node);
60  bool GetMetadata(QVariant &data);
61  QMap<QString,QString> ServerList(void);
62 
63  protected:
64  void customEvent(QEvent *event) override; // QObject
65  void timerEvent(QTimerEvent * event) override; // QObject
66 
67  private slots:
68  void Start();
69  void Stop(void);
70  void Update(void);
71  void CheckStatus(void);
72  void replyFinished(QNetworkReply *reply);
73 
74  private:
76  : m_subscription(sub) {}
77  void ScheduleUpdate(void);
78  void CheckFailure(const QUrl &url);
79  void Debug(void);
80  void BrowseNextContainer(void);
81  void SendBrowseRequest(const QUrl &url, const QString &objectid);
82  void AddServer(const QString &usn, const QString &url);
83  void RemoveServer(const QString &usn);
84  void ScheduleRenewal(const QString &usn, int timeout);
85 
86  // xml parsing of browse requests
87  void ParseBrowse(const QUrl &url, QNetworkReply *reply);
88  void FindItems(const QDomNode &n, MediaServerItem &content,
89  bool &resetparent);
90  QDomDocument* FindResult(const QDomNode &n, uint &num,
91  uint &total, uint &updateid);
92 
93  // xml parsing of device description
94  bool ParseDescription(const QUrl &url, QNetworkReply *reply);
95  static void ParseDevice(QDomElement &element, QString &controlURL,
96  QString &eventURL, QString &friendlyName);
97  static void ParseServiceList(QDomElement &element, QString &controlURL,
98  QString &eventURL);
99  static void ParseService(QDomElement &element, QString &controlURL,
100  QString &eventURL);
101 
102  // convert MediaServerItems to video metadata
103  void GetServerContent(QString &usn, MediaServerItem *content,
105  meta_dir_node *node);
106 
107  private:
109  static bool gUPNPScannerEnabled;
111  static QMutex* gUPNPScannerLock;
112 
114  QMutex m_lock {QMutex::Recursive};
115  QHash<QString,MediaServer*> m_servers;
116  QNetworkAccessManager *m_network {nullptr};
117  // TODO Move to QMultiHash when we move to Qt >=4.7
118  // QHash(QUrl) unsupported on < 4.7
119  QMultiMap<QUrl, QNetworkReply*> m_descriptionRequests;
120  QMultiMap<QUrl, QNetworkReply*> m_browseRequests;
121 
122  QTimer *m_updateTimer {nullptr};
123  QTimer *m_watchdogTimer {nullptr};
124 
125  QString m_masterHost;
126  int m_masterPort {0};
127 
128  bool m_scanComplete {false};
129  bool m_fullscan {false};
130 };
131 
132 #endif // UPNPSCANNER_H
MediaServerItem::MediaServerItem
MediaServerItem(QString id, QString parent, QString name, QString url)
Definition: upnpscanner.h:28
UPNPScanner::m_servers
QHash< QString, MediaServer * > m_servers
Definition: upnpscanner.h:115
UPNPScanner::m_fullscan
bool m_fullscan
Definition: upnpscanner.h:129
MediaServerItem::Add
bool Add(const MediaServerItem &item)
Definition: upnpscanner.cpp:59
UPNPScanner::customEvent
void customEvent(QEvent *event) override
Processes subscription and SSDP cache update events.
Definition: upnpscanner.cpp:637
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
UPNPScanner::m_lock
QMutex m_lock
Definition: upnpscanner.h:114
UPNPScanner::timerEvent
void timerEvent(QTimerEvent *event) override
Handles subscription renewal timer events.
Definition: upnpscanner.cpp:719
UPNPScanner::gUPNPScanner
static UPNPScanner * gUPNPScanner
Definition: upnpscanner.h:108
MediaServerItem
Definition: upnpscanner.h:25
UPNPScanner::ScheduleUpdate
void ScheduleUpdate(void)
Definition: upnpscanner.cpp:755
MediaServer
A simple wrapper containing details about a UPnP Media Server.
Definition: mediaserver.h:33
MediaServerItem::m_children
QMap< QString, MediaServerItem > m_children
Definition: upnpscanner.h:42
UPNPScanner::gUPNPScannerLock
static QMutex * gUPNPScannerLock
Definition: upnpscanner.h:111
upnpsubscription.h
UPNPScanner::GetInitialMetadata
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....
Definition: upnpscanner.cpp:205
UPNPSubscription
Definition: upnpsubscription.h:10
MediaServerItem::m_url
QString m_url
Definition: upnpscanner.h:40
UPNPScanner::m_browseRequests
QMultiMap< QUrl, QNetworkReply * > m_browseRequests
Definition: upnpscanner.h:120
build_compdb.content
content
Definition: build_compdb.py:38
UPNPScanner::UPNPScanner
UPNPScanner(UPNPSubscription *sub)
Definition: upnpscanner.h:75
meta_dir_node
Definition: videometadatalistmanager.h:83
MediaServerItem::m_id
QString m_id
Definition: upnpscanner.h:37
UPNPScanner::StartFullScan
void StartFullScan(void)
Instruct the UPNPScanner thread to start a full scan of metadata from known media servers.
Definition: upnpscanner.cpp:192
UPNPScanner::m_descriptionRequests
QMultiMap< QUrl, QNetworkReply * > m_descriptionRequests
Definition: upnpscanner.h:119
UPNPScanner::m_network
QNetworkAccessManager * m_network
Definition: upnpscanner.h:116
UPNPScanner::GetServerContent
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_...
Definition: upnpscanner.cpp:343
UPNPScanner::Stop
void Stop(void)
Stops scanning.
Definition: upnpscanner.cpp:446
UPNPScanner::m_updateTimer
QTimer * m_updateTimer
Definition: upnpscanner.h:122
UPNPScanner
UPnPScanner detects UPnP Media Servers available on the local network (via the UPnP SSDP cache),...
Definition: upnpscanner.h:46
UPNPScanner::FindResult
QDomDocument * FindResult(const QDomNode &n, uint &num, uint &total, uint &updateid)
Definition: upnpscanner.cpp:1161
UPNPScanner::m_subscription
UPNPSubscription * m_subscription
Definition: upnpscanner.h:113
UPNPScanner::ServerList
QMap< QString, QString > ServerList(void)
Returns a list of valid Media Servers discovered on the network.
Definition: upnpscanner.cpp:389
UPNPScanner::RemoveServer
void RemoveServer(const QString &usn)
Definition: upnpscanner.cpp:944
UPNPScanner::AddServer
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...
Definition: upnpscanner.cpp:909
UPNPScanner::m_scanComplete
bool m_scanComplete
Definition: upnpscanner.h:128
MediaServerItem::m_name
QString m_name
Definition: upnpscanner.h:39
UPNPScanner::ParseBrowse
void ParseBrowse(const QUrl &url, QNetworkReply *reply)
Parse the XML returned from Content Directory Service browse request.
Definition: upnpscanner.cpp:986
uint
unsigned int uint
Definition: compat.h:140
UPNPScanner::SendBrowseRequest
void SendBrowseRequest(const QUrl &url, const QString &objectid)
Formulates and sends a ContentDirectory Service Browse Request to the given control URL,...
Definition: upnpscanner.cpp:867
MediaServerItem::Find
MediaServerItem * Find(QString &id)
Definition: upnpscanner.cpp:42
videometadatalistmanager.h
UPNPScanner::gUPNPScannerThread
static MThread * gUPNPScannerThread
Definition: upnpscanner.h:110
UPNPScanner::replyFinished
void replyFinished(QNetworkReply *reply)
Validates network responses against known requests and parses expected responses for the required dat...
Definition: upnpscanner.cpp:581
MediaServerItem::Reset
void Reset(void)
Definition: upnpscanner.cpp:69
UPNPScanner::ParseDescription
bool ParseDescription(const QUrl &url, QNetworkReply *reply)
Parse the device description XML return my a media server.
Definition: upnpscanner.cpp:1206
UPNPScanner::Start
void Start()
Initialises the scanner, hooks it up to the subscription service and the SSDP cache and starts scanni...
Definition: upnpscanner.cpp:408
UPNPScanner::Instance
static UPNPScanner * Instance(UPNPSubscription *sub=nullptr)
Returns the global UPNPScanner instance if it has been enabled or nullptr if UPNPScanner is currently...
Definition: upnpscanner.cpp:154
UPNPScanner::ParseDevice
static void ParseDevice(QDomElement &element, QString &controlURL, QString &eventURL, QString &friendlyName)
Definition: upnpscanner.cpp:1334
UPNPScanner::~UPNPScanner
~UPNPScanner() override
Definition: upnpscanner.cpp:133
MediaServerItem::MediaServerItem
MediaServerItem()=default
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
MediaServerItem::NextUnbrowsed
QString NextUnbrowsed(void)
Definition: upnpscanner.cpp:19
mthread.h
UPNPScanner::m_masterPort
int m_masterPort
Definition: upnpscanner.h:126
UPNPScanner::m_watchdogTimer
QTimer * m_watchdogTimer
Definition: upnpscanner.h:123
UPNPScanner::Update
void Update(void)
Iterates through the list of known servers and initialises a connection by requesting the device desc...
Definition: upnpscanner.cpp:505
UPNPScanner::Enable
static void Enable(bool enable, UPNPSubscription *sub=nullptr)
Creates or destroys the global UPNPScanner instance.
Definition: upnpscanner.cpp:142
VideoMetadataListManager::metadata_list
std::list< VideoMetadataPtr > metadata_list
Definition: videometadatalistmanager.h:14
MediaServerItem::m_scanned
bool m_scanned
Definition: upnpscanner.h:41
UPNPScanner::ScheduleRenewal
void ScheduleRenewal(const QString &usn, int timeout)
Creates a QTimer to trigger a subscription renewal for a given media server.
Definition: upnpscanner.cpp:967
UPNPScanner::FindItems
void FindItems(const QDomNode &n, MediaServerItem &content, bool &resetparent)
Definition: upnpscanner.cpp:1079
UPNPScanner::CheckFailure
void CheckFailure(const QUrl &url)
Updates the logs for failed server connections.
Definition: upnpscanner.cpp:767
UPNPScanner::BrowseNextContainer
void BrowseNextContainer(void)
For each known media server, find the next container which needs to be browsed and trigger sending of...
Definition: upnpscanner.cpp:822
UPNPScanner::ParseServiceList
static void ParseServiceList(QDomElement &element, QString &controlURL, QString &eventURL)
Definition: upnpscanner.cpp:1352
UPNPScanner::gUPNPScannerEnabled
static bool gUPNPScannerEnabled
Definition: upnpscanner.h:109
UPNPScanner::m_masterHost
QString m_masterHost
Definition: upnpscanner.h:125
MediaServerItem::m_parentid
QString m_parentid
Definition: upnpscanner.h:38
UPNPScanner::CheckStatus
void CheckStatus(void)
Removes media servers that can no longer be found in the SSDP cache.
Definition: upnpscanner.cpp:553
UPNPScanner::GetMetadata
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...
Definition: upnpscanner.cpp:237
upnpexp.h
UPNPScanner::ParseService
static void ParseService(QDomElement &element, QString &controlURL, QString &eventURL)
Definition: upnpscanner.cpp:1366
UPNPScanner::Debug
void Debug(void)
Definition: upnpscanner.cpp:786