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 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
9 #include <QMutex>
10 #else
11 #include <QRecursiveMutex>
12 #endif
13 #include <QNetworkAccessManager>
14 #include <QNetworkReply>
15 #include <QNetworkRequest>
16 #include <QTimer>
17 
18 // MythTV headers
19 #include "upnpsubscription.h"
20 #include "mthread.h"
21 #include "upnpexp.h"
23 
24 class MediaServer;
25 class UPNPSubscription;
26 class meta_dir_node;
27 
29 {
30  public:
31  MediaServerItem() = default;
32  MediaServerItem(QString id, QString parent,
33  QString name, QString url)
34  : m_id(std::move(id)), m_parentid(std::move(parent)),
35  m_name(std::move(name)), m_url(std::move(url)) { }
36  QString NextUnbrowsed(void);
37  MediaServerItem* Find(QString &id);
38  bool Add(const MediaServerItem &item);
39  void Reset(void);
40 
41  QString m_id;
42  QString m_parentid;
43  QString m_name;
44  QString m_url;
45  bool m_scanned {false};
46  QMap<QString, MediaServerItem> m_children;
47 };
48 
49 class UPNPScanner : public QObject
50 {
51  Q_OBJECT
52 
53  public:
54  ~UPNPScanner() override;
55 
56  static void Enable(bool enable, UPNPSubscription *sub = nullptr);
57  static UPNPScanner* Instance(UPNPSubscription *sub = nullptr);
58 
59  void StartFullScan(void);
61  meta_dir_node *node);
63  meta_dir_node *node);
64  bool GetMetadata(QVariant &data);
65  QMap<QString,QString> ServerList(void);
66 
67  protected:
68  void customEvent(QEvent *event) override; // QObject
69  void timerEvent(QTimerEvent * event) override; // QObject
70 
71  private slots:
72  void Start();
73  void Stop(void);
74  void Update(void);
75  void CheckStatus(void);
76  void replyFinished(QNetworkReply *reply);
77 
78  private:
80  : m_subscription(sub) {}
81  void ScheduleUpdate(void);
82  void CheckFailure(const QUrl &url);
83  void Debug(void);
84  void BrowseNextContainer(void);
85  void SendBrowseRequest(const QUrl &url, const QString &objectid);
86  void AddServer(const QString &usn, const QString &url);
87  void RemoveServer(const QString &usn);
88  void ScheduleRenewal(const QString &usn, std::chrono::seconds timeout);
89 
90  // xml parsing of browse requests
91  void ParseBrowse(const QUrl &url, QNetworkReply *reply);
92  void FindItems(const QDomNode &n, MediaServerItem &content,
93  bool &resetparent);
94  QDomDocument* FindResult(const QDomNode &n, uint &num,
95  uint &total, uint &updateid);
96 
97  // xml parsing of device description
98  bool ParseDescription(const QUrl &url, QNetworkReply *reply);
99  static void ParseDevice(QDomElement &element, QString &controlURL,
100  QString &eventURL, QString &friendlyName);
101  static void ParseServiceList(QDomElement &element, QString &controlURL,
102  QString &eventURL);
103  static void ParseService(QDomElement &element, QString &controlURL,
104  QString &eventURL);
105 
106  // convert MediaServerItems to video metadata
107  void GetServerContent(QString &usn, MediaServerItem *content,
109  meta_dir_node *node);
110 
111  private:
113  static bool gUPNPScannerEnabled;
115 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
116  static QMutex* gUPNPScannerLock;
117 #else
118  static QRecursiveMutex* gUPNPScannerLock;
119 #endif
120 
122 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
123  QMutex m_lock {QMutex::Recursive};
124 #else
125  QRecursiveMutex m_lock;
126 #endif
127  QHash<QString,MediaServer*> m_servers;
128  QNetworkAccessManager *m_network {nullptr};
129  // TODO Move to QMultiHash when we move to Qt >=4.7
130  // QHash(QUrl) unsupported on < 4.7
131  QMultiMap<QUrl, QNetworkReply*> m_descriptionRequests;
132  QMultiMap<QUrl, QNetworkReply*> m_browseRequests;
133 
134  QTimer *m_updateTimer {nullptr};
135  QTimer *m_watchdogTimer {nullptr};
136 
137  QString m_masterHost;
138  int m_masterPort {0};
139 
140  bool m_scanComplete {false};
141  bool m_fullscan {false};
142 };
143 
144 #endif // UPNPSCANNER_H
MediaServerItem::MediaServerItem
MediaServerItem(QString id, QString parent, QString name, QString url)
Definition: upnpscanner.h:32
UPNPScanner::m_servers
QHash< QString, MediaServer * > m_servers
Definition: upnpscanner.h:127
UPNPScanner::m_fullscan
bool m_fullscan
Definition: upnpscanner.h:141
MediaServerItem::Add
bool Add(const MediaServerItem &item)
Definition: upnpscanner.cpp:63
UPNPScanner::customEvent
void customEvent(QEvent *event) override
Processes subscription and SSDP cache update events.
Definition: upnpscanner.cpp:645
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
UPNPScanner::timerEvent
void timerEvent(QTimerEvent *event) override
Definition: upnpscanner.cpp:727
UPNPScanner::gUPNPScanner
static UPNPScanner * gUPNPScanner
Definition: upnpscanner.h:112
MediaServerItem
Definition: upnpscanner.h:28
UPNPScanner::ScheduleUpdate
void ScheduleUpdate(void)
Definition: upnpscanner.cpp:763
MediaServer
Definition: mediaserver.h:32
MediaServerItem::m_children
QMap< QString, MediaServerItem > m_children
Definition: upnpscanner.h:46
upnpsubscription.h
UPNPScanner::GetInitialMetadata
void GetInitialMetadata(VideoMetadataListManager::metadata_list *list, meta_dir_node *node)
Definition: upnpscanner.cpp:213
UPNPSubscription
Definition: upnpsubscription.h:9
MediaServerItem::m_url
QString m_url
Definition: upnpscanner.h:44
UPNPScanner::m_browseRequests
QMultiMap< QUrl, QNetworkReply * > m_browseRequests
Definition: upnpscanner.h:132
build_compdb.content
content
Definition: build_compdb.py:38
UPNPScanner::UPNPScanner
UPNPScanner(UPNPSubscription *sub)
Definition: upnpscanner.h:79
meta_dir_node
Definition: videometadatalistmanager.h:82
UPNPScanner::m_lock
QRecursiveMutex m_lock
Definition: upnpscanner.h:125
MediaServerItem::m_id
QString m_id
Definition: upnpscanner.h:41
UPNPScanner::StartFullScan
void StartFullScan(void)
Definition: upnpscanner.cpp:200
UPNPScanner::m_descriptionRequests
QMultiMap< QUrl, QNetworkReply * > m_descriptionRequests
Definition: upnpscanner.h:131
UPNPScanner::m_network
QNetworkAccessManager * m_network
Definition: upnpscanner.h:128
UPNPScanner::GetServerContent
void GetServerContent(QString &usn, MediaServerItem *content, VideoMetadataListManager::metadata_list *list, meta_dir_node *node)
Definition: upnpscanner.cpp:351
UPNPScanner::Stop
void Stop(void)
Definition: upnpscanner.cpp:454
UPNPScanner::m_updateTimer
QTimer * m_updateTimer
Definition: upnpscanner.h:134
UPNPScanner::ScheduleRenewal
void ScheduleRenewal(const QString &usn, std::chrono::seconds timeout)
Creates a QTimer to trigger a subscription renewal for a given media server.
Definition: upnpscanner.cpp:978
UPNPScanner
Definition: upnpscanner.h:49
UPNPScanner::FindResult
QDomDocument * FindResult(const QDomNode &n, uint &num, uint &total, uint &updateid)
Definition: upnpscanner.cpp:1169
UPNPScanner::m_subscription
UPNPSubscription * m_subscription
Definition: upnpscanner.h:121
UPNPScanner::ServerList
QMap< QString, QString > ServerList(void)
Definition: upnpscanner.cpp:397
UPNPScanner::RemoveServer
void RemoveServer(const QString &usn)
Definition: upnpscanner.cpp:956
UPNPScanner::AddServer
void AddServer(const QString &usn, const QString &url)
Definition: upnpscanner.cpp:921
UPNPScanner::m_scanComplete
bool m_scanComplete
Definition: upnpscanner.h:140
MediaServerItem::m_name
QString m_name
Definition: upnpscanner.h:43
UPNPScanner::ParseBrowse
void ParseBrowse(const QUrl &url, QNetworkReply *reply)
Definition: upnpscanner.cpp:994
uint
unsigned int uint
Definition: compat.h:140
UPNPScanner::SendBrowseRequest
void SendBrowseRequest(const QUrl &url, const QString &objectid)
Definition: upnpscanner.cpp:875
MediaServerItem::Find
MediaServerItem * Find(QString &id)
Definition: upnpscanner.cpp:46
videometadatalistmanager.h
UPNPScanner::gUPNPScannerThread
static MThread * gUPNPScannerThread
Definition: upnpscanner.h:114
UPNPScanner::replyFinished
void replyFinished(QNetworkReply *reply)
Definition: upnpscanner.cpp:589
MediaServerItem::Reset
void Reset(void)
Definition: upnpscanner.cpp:73
UPNPScanner::ParseDescription
bool ParseDescription(const QUrl &url, QNetworkReply *reply)
Definition: upnpscanner.cpp:1214
UPNPScanner::Start
void Start()
Definition: upnpscanner.cpp:416
UPNPScanner::Instance
static UPNPScanner * Instance(UPNPSubscription *sub=nullptr)
Definition: upnpscanner.cpp:162
std
Definition: mythchrono.h:23
UPNPScanner::ParseDevice
static void ParseDevice(QDomElement &element, QString &controlURL, QString &eventURL, QString &friendlyName)
Definition: upnpscanner.cpp:1342
UPNPScanner::~UPNPScanner
~UPNPScanner() override
Definition: upnpscanner.cpp:141
MediaServerItem::MediaServerItem
MediaServerItem()=default
UPNPScanner::gUPNPScannerLock
static QRecursiveMutex * gUPNPScannerLock
Definition: upnpscanner.h:118
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
MediaServerItem::NextUnbrowsed
QString NextUnbrowsed(void)
Definition: upnpscanner.cpp:23
mthread.h
UPNPScanner::m_masterPort
int m_masterPort
Definition: upnpscanner.h:138
UPNPScanner::m_watchdogTimer
QTimer * m_watchdogTimer
Definition: upnpscanner.h:135
UPNPScanner::Update
void Update(void)
Definition: upnpscanner.cpp:513
UPNPScanner::Enable
static void Enable(bool enable, UPNPSubscription *sub=nullptr)
Definition: upnpscanner.cpp:150
VideoMetadataListManager::metadata_list
std::list< VideoMetadataPtr > metadata_list
Definition: videometadatalistmanager.h:14
MediaServerItem::m_scanned
bool m_scanned
Definition: upnpscanner.h:45
UPNPScanner::FindItems
void FindItems(const QDomNode &n, MediaServerItem &content, bool &resetparent)
Definition: upnpscanner.cpp:1087
UPNPScanner::CheckFailure
void CheckFailure(const QUrl &url)
Definition: upnpscanner.cpp:775
UPNPScanner::BrowseNextContainer
void BrowseNextContainer(void)
Definition: upnpscanner.cpp:830
UPNPScanner::ParseServiceList
static void ParseServiceList(QDomElement &element, QString &controlURL, QString &eventURL)
Definition: upnpscanner.cpp:1360
UPNPScanner::gUPNPScannerEnabled
static bool gUPNPScannerEnabled
Definition: upnpscanner.h:113
UPNPScanner::m_masterHost
QString m_masterHost
Definition: upnpscanner.h:137
MediaServerItem::m_parentid
QString m_parentid
Definition: upnpscanner.h:42
UPNPScanner::CheckStatus
void CheckStatus(void)
Definition: upnpscanner.cpp:561
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:245
upnpexp.h
UPNPScanner::ParseService
static void ParseService(QDomElement &element, QString &controlURL, QString &eventURL)
Definition: upnpscanner.cpp:1374
UPNPScanner::Debug
void Debug(void)
Definition: upnpscanner.cpp:794