MythTV  master
upnpscanner.h
Go to the documentation of this file.
1 #ifndef UPNPSCANNER_H
2 #define UPNPSCANNER_H
3 
4 #include <QNetworkAccessManager>
5 #include <QNetworkRequest>
6 #include <QNetworkReply>
7 #include <QDomDocument>
8 #include <QMutex>
9 #include <QTimer>
10 
11 #include "upnpsubscription.h"
12 #include "mthread.h"
13 #include "upnpexp.h"
14 
16 
17 class MediaServer;
18 class UPNPSubscription;
19 class meta_dir_node;
20 
22 {
23  public:
25  MediaServerItem(const QString& id, const QString& parent,
26  const QString& name, const QString& url)
27  : m_id(id), m_parentid(parent), m_name(name), m_url(url) { }
28  QString NextUnbrowsed(void);
29  MediaServerItem* Find(QString &id);
30  bool Add(MediaServerItem &item);
31  void Reset(void);
32 
33  QString m_id;
34  QString m_parentid;
35  QString m_name;
36  QString m_url;
37  bool m_scanned {false};
38  QMap<QString, MediaServerItem> m_children;
39 };
40 
41 class UPNPScanner : public QObject
42 {
43  Q_OBJECT
44 
45  public:
46  ~UPNPScanner();
47 
48  static void Enable(bool enable, UPNPSubscription *sub = nullptr);
49  static UPNPScanner* Instance(UPNPSubscription *sub = nullptr);
50 
51  void StartFullScan(void);
53  meta_dir_node *node);
55  meta_dir_node *node);
56  bool GetMetadata(QVariant &data);
57  QMap<QString,QString> ServerList(void);
58 
59  protected:
60  void customEvent(QEvent *event) override; // QObject
61  void timerEvent(QTimerEvent * event) override; // QObject
62 
63  private slots:
64  void Start();
65  void Stop(void);
66  void Update(void);
67  void CheckStatus(void);
68  void replyFinished(QNetworkReply *reply);
69 
70  private:
72  : QObject(), m_subscription(sub) {}
73  void ScheduleUpdate(void);
74  void CheckFailure(const QUrl &url);
75  void Debug(void);
76  void BrowseNextContainer(void);
77  void SendBrowseRequest(const QUrl &url, const QString &objectid);
78  void AddServer(const QString &usn, const QString &url);
79  void RemoveServer(const QString &usn);
80  void ScheduleRenewal(const QString &usn, int timeout);
81 
82  // xml parsing of browse requests
83  void ParseBrowse(const QUrl &url, QNetworkReply *reply);
84  void FindItems(const QDomNode &n, MediaServerItem &content,
85  bool &resetparent);
86  QDomDocument* FindResult(const QDomNode &n, uint &num,
87  uint &total, uint &updateid);
88 
89  // xml parsing of device description
90  bool ParseDescription(const QUrl &url, QNetworkReply *reply);
91  void ParseDevice(QDomElement &element, QString &controlURL,
92  QString &eventURL, QString &friendlyName);
93  void ParseServiceList(QDomElement &element, QString &controlURL,
94  QString &eventURL);
95  void ParseService(QDomElement &element, QString &controlURL,
96  QString &eventURL);
97 
98  // convert MediaServerItems to video metadata
99  void GetServerContent(QString &usn, MediaServerItem *content,
101  meta_dir_node *node);
102 
103  private:
105  static bool gUPNPScannerEnabled;
107  static QMutex* gUPNPScannerLock;
108 
110  QMutex m_lock {QMutex::Recursive};
111  QHash<QString,MediaServer*> m_servers;
112  QNetworkAccessManager *m_network {nullptr};
113  // TODO Move to QMultiHash when we move to Qt >=4.7
114  // QHash(QUrl) unsupported on < 4.7
115  QMultiMap<QUrl, QNetworkReply*> m_descriptionRequests;
116  QMultiMap<QUrl, QNetworkReply*> m_browseRequests;
117 
118  QTimer *m_updateTimer {nullptr};
119  QTimer *m_watchdogTimer {nullptr};
120 
121  QString m_masterHost;
122  int m_masterPort {0};
123 
124  bool m_scanComplete {false};
125  bool m_fullscan {false};
126 };
127 
128 #endif // UPNPSCANNER_H
QMultiMap< QUrl, QNetworkReply * > m_browseRequests
Definition: upnpscanner.h:116
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:111
unsigned int slots[4]
Definition: element.c:38
UPnPScanner detects UPnP Media Servers available on the local network (via the UPnP SSDP cache),...
Definition: upnpscanner.h:41
static UPNPScanner * gUPNPScanner
Definition: upnpscanner.h:104
QString m_masterHost
Definition: upnpscanner.h:121
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...
QMultiMap< QUrl, QNetworkReply * > m_descriptionRequests
Definition: upnpscanner.h:115
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....
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 m_fullscan
Definition: upnpscanner.h:125
UPNPScanner(UPNPSubscription *sub)
Definition: upnpscanner.h:71
QString m_name
Definition: upnpscanner.h:35
unsigned int uint
Definition: compat.h:140
QNetworkAccessManager * m_network
Definition: upnpscanner.h:112
void Stop(void)
Stops scanning.
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
QMap< QString, MediaServerItem > m_children
Definition: upnpscanner.h:38
QDomDocument * FindResult(const QDomNode &n, uint &num, uint &total, uint &updateid)
MediaServerItem * Find(QString &id)
Definition: upnpscanner.cpp:42
void RemoveServer(const QString &usn)
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:109
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_...
QTimer * m_updateTimer
Definition: upnpscanner.h:118
QMap< QString, QString > ServerList(void)
Returns a list of valid Media Servers discovered on the network.
void Reset(void)
Definition: upnpscanner.cpp:69
MediaServerItem(const QString &id, const QString &parent, const QString &name, const QString &url)
Definition: upnpscanner.h:25
const char * name
Definition: ParseText.cpp:328
bool m_scanComplete
Definition: upnpscanner.h:124
void ParseDevice(QDomElement &element, QString &controlURL, QString &eventURL, QString &friendlyName)
void replyFinished(QNetworkReply *reply)
Validates network responses against known requests and parses expected responses for the required dat...
void ParseBrowse(const QUrl &url, QNetworkReply *reply)
Parse the XML returned from Content Directory Service browse request.
QTimer * m_watchdogTimer
Definition: upnpscanner.h:119
std::list< VideoMetadataPtr > metadata_list
void Start()
Initialises the scanner, hooks it up to the subscription service and the SSDP cache and starts scanni...
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:106
void SendBrowseRequest(const QUrl &url, const QString &objectid)
Formulates and sends a ContentDirectory Service Browse Request to the given control URL,...
bool ParseDescription(const QUrl &url, QNetworkReply *reply)
Parse the device description XML return my a media server.
static bool gUPNPScannerEnabled
Definition: upnpscanner.h:105
void CheckFailure(const QUrl &url)
Updates the logs for failed server connections.
void ParseService(QDomElement &element, QString &controlURL, QString &eventURL)
QString m_parentid
Definition: upnpscanner.h:34
QString NextUnbrowsed(void)
Definition: upnpscanner.cpp:19
static void Enable(bool enable, UPNPSubscription *sub=nullptr)
Creates or destroys the global UPNPScanner instance.
int m_masterPort
Definition: upnpscanner.h:122
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:110
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)
void ParseServiceList(QDomElement &element, QString &controlURL, QString &eventURL)
static QMutex * gUPNPScannerLock
Definition: upnpscanner.h:107
void FindItems(const QDomNode &n, MediaServerItem &content, bool &resetparent)