MythTV master
mediaserver.cpp
Go to the documentation of this file.
1
2// Program Name: mediaserver.cpp
3//
4// Purpose - uPnp Media Server main Class
5//
6// Created By : David Blain Created On : Jan. 15, 2007
7// Modified By : Modified On:
8//
10
11#include "libmythbase/mythconfig.h"
12
13// Qt
14#include <QChar> // Fix Qt6 GCC SFINAE warning
15#include <QNetworkInterface>
16#include <QNetworkProxy>
17
18// MythTV
19#if CONFIG_LIBDNS_SD
21#endif
23#include "libmythbase/mythdb.h"
26
27// MythBackend
28#include "internetContent.h"
29#include "mediaserver.h"
30#include "upnpcdsmusic.h"
31#include "upnpcdstv.h"
32#include "upnpcdsvideo.h"
33
34
37//
38// UPnp Class implementaion
39//
42
44//
46
48 m_sSharePath(GetShareDir())
49{
50 LOG(VB_UPNP, LOG_INFO, "MediaServer()");
51}
52
53void MediaServer::Init(bool bIsMaster, bool bDisableUPnp /* = false */)
54{
55 LOG(VB_UPNP, LOG_INFO, "MediaServer::Init(): Begin");
56
57 int nPort = GetMythDB()->GetNumSetting("BackendStatusPort", 6544);
58 int nSSLPort = GetMythDB()->GetNumSetting("BackendSSLPort", nPort + 10);
59 int nWSPort = nPort + 5;
60 // UPNP port is now status port + 6 (default 6550)
61 nPort += 6;
62 nSSLPort += 6;
63
64 auto *pHttpServer = new HttpServer();
65
66 if (!pHttpServer->isListening())
67 {
68 pHttpServer->setProxy(QNetworkProxy::NoProxy);
69 // HTTP
70 if (!pHttpServer->listen(nPort))
71 {
72 LOG(VB_GENERAL, LOG_ERR, "MediaServer: HttpServer Create Error");
73 delete pHttpServer;
74 pHttpServer = nullptr;
75 return;
76 }
77
78#ifndef QT_NO_OPENSSL
79 // HTTPS (SSL)
80 if (!pHttpServer->listen(nSSLPort, true, kSSLServer))
81 {
82 LOG(VB_GENERAL, LOG_ERR, "MediaServer: HttpServer failed to create SSL server");
83 }
84#endif
85 }
86
88
90 {
91 if (!m_webSocketServer->listen(nWSPort))
92 {
93 LOG(VB_GENERAL, LOG_ERR, "MediaServer: WebSocketServer Create Error");
94 }
95 }
96
97 QString sFileName = GetMythDB()->GetSetting("upnpDescXmlPath", m_sSharePath);
98
99 if ( bIsMaster )
100 sFileName += "devicemaster.xml";
101 else
102 sFileName += "deviceslave.xml";
103
104 // ------------------------------------------------------------------
105 // Make sure our device Description is loaded.
106 // ------------------------------------------------------------------
107
108 LOG(VB_UPNP, LOG_INFO,
109 "MediaServer: Loading UPnp Description " + sFileName);
110
111 g_UPnpDeviceDesc.Load( sFileName );
112
113 // ------------------------------------------------------------------
114 // Register Http Server Extensions...
115 // ------------------------------------------------------------------
116
117 LOG(VB_UPNP, LOG_INFO, "MediaServer: Registering Http Server Extensions.");
118
119 pHttpServer->RegisterExtension( new InternetContent ( m_sSharePath ));
120
121 if (bDisableUPnp)
122 {
123 LOG(VB_GENERAL, LOG_NOTICE,
124 "*** The UPNP service has been DISABLED with the "
125 "--noupnp option ***");
126 return;
127 }
128
129 QList<QHostAddress> IPAddrList = ServerPool::DefaultListen();
130 if (IPAddrList.contains(QHostAddress(QHostAddress::AnyIPv4)))
131 {
132 IPAddrList.removeAll(QHostAddress(QHostAddress::AnyIPv4));
133 IPAddrList.removeAll(QHostAddress(QHostAddress::AnyIPv6));
134 IPAddrList.append(QNetworkInterface::allAddresses());
135 }
136
137 if (IPAddrList.isEmpty())
138 {
139 LOG(VB_GENERAL, LOG_ERR,
140 "MediaServer: No Listenable IP Addresses found - "
141 "Disabling UPnP");
142 return;
143 }
144
145 // ----------------------------------------------------------------------
146 // Initialize UPnp Stack
147 // ----------------------------------------------------------------------
148
149 if (Initialize( IPAddrList, nPort, pHttpServer ))
150 {
151
152 // ------------------------------------------------------------------
153 // Register any HttpServerExtensions... Only The Master Backend
154 // ------------------------------------------------------------------
155
156 if (bIsMaster)
157 {
158 QString sSourceProtocols = GetSourceProtocolInfos().join(",");
159
160 LOG(VB_UPNP, LOG_INFO, "MediaServer: Registering MS_MediaReceiverRegistrar Service.");
161
163 m_sSharePath ) );
164
165 LOG(VB_UPNP, LOG_INFO, "MediaServer: Registering ConnnectionManager Service.");
166
168 m_sSharePath, sSourceProtocols );
170
171 LOG(VB_UPNP, LOG_INFO, "MediaServer: Registering ContentDirectory Service.");
172
175
176 // ----------------------------------------------------------------
177 // Register CDS Extensions
178 // ----------------------------------------------------------------
179
180 LOG(VB_UPNP, LOG_INFO,
181 "MediaServer: Registering UPnpCDSTv Extension");
182
184
185 LOG(VB_UPNP, LOG_INFO,
186 "MediaServer: Registering UPnpCDSMusic Extension");
187
189
190 LOG(VB_UPNP, LOG_INFO,
191 "MediaServer: Registering UPnpCDSVideo Extension");
192
194 }
195
196#if 0
197 LOG(VB_UPNP, LOG_INFO, "MediaServer::Adding Context Listener");
198
199 gCoreContext->addListener( this );
200#endif
201
202 Start();
203
204#if CONFIG_LIBDNS_SD
205 // advertise using Bonjour
206 if (gCoreContext)
207 {
208 m_bonjour = new BonjourRegister();
209 if (m_bonjour)
210 {
211 QByteArray name("Mythbackend on ");
212 name.append(gCoreContext->GetHostName().toUtf8());
213 QByteArray txt(bIsMaster ? "\x06master" : "\x05slave");
214 m_bonjour->Register(nPort, "_mythbackend._tcp", name, txt);
215 }
216 }
217#endif
218 }
219
220 LOG(VB_UPNP, LOG_INFO, "MediaServer::Init(): End");
221}
222
224//
226
228{
229 // -=>TODO: Need to check to see if calling this more than once is ok.
230
231#if 0
233#endif
234
235 delete m_webSocketServer;
236 delete m_pHttpServer;
237
238#if CONFIG_LIBDNS_SD
239 delete m_bonjour;
240#endif
241}
242
244//
246#if 0
247void MediaServer::customEvent( QEvent *e )
248{
249 if (MythEvent::Type(e->type()) == MythEvent::MythEventMessage)
250 {
251 MythEvent *me = static_cast<MythEvent *>(e);
252 QString message = me->Message();
253
254 //-=>TODO: Need to handle events to notify clients of changes
255 }
256}
257#endif
259//
261
263{
264 m_pUPnpCDS->RegisterExtension( pExtension );
265}
266
268//
270
272{
273 m_pUPnpCDS->UnregisterExtension( pExtension );
274}
void RegisterExtension(HttpServerExtension *pExtension)
Definition: httpserver.cpp:313
void RegisterExtension(UPnpCDSExtension *pExtension)
WebSocketServer * m_webSocketServer
Definition: mediaserver.h:41
UPnpCMGR * m_pUPnpCMGR
Definition: mediaserver.h:46
void Init(bool bIsMaster, bool bDisableUPnp=false)
Definition: mediaserver.cpp:53
~MediaServer() override
void UnregisterExtension(UPnpCDSExtension *pExtension)
QString m_sSharePath
Definition: mediaserver.h:48
UPnpCDS * m_pUPnpCDS
Definition: mediaserver.h:45
QString GetHostName(void)
This class is used as a container for messages.
Definition: mythevent.h:17
const QString & Message() const
Definition: mythevent.h:65
void addListener(QObject *listener)
Add a listener to the observable.
void removeListener(QObject *listener)
Remove a listener to the observable.
static QList< QHostAddress > DefaultListen(void)
Definition: serverpool.cpp:306
bool listen(QList< QHostAddress > addrs, quint16 port, bool requireall=true, PoolServerType type=kTCPServer)
Definition: serverpool.cpp:396
bool isListening(void) const
Definition: serverpool.h:92
void RegisterExtension(UPnpCDSExtension *pExtension)
Definition: upnpcds.cpp:170
void UnregisterExtension(UPnpCDSExtension *pExtension)
Definition: upnpcds.cpp:189
bool Load(const QString &sFileName)
Definition: upnpdevice.cpp:42
static UPnpDevice * RootDevice()
Definition: upnp.h:65
HttpServer * m_pHttpServer
Definition: upnp.h:43
virtual void Start()
Definition: upnp.cpp:145
bool Initialize(int nServicePort, HttpServer *pHttpServer)
Definition: upnp.cpp:75
static UPnpDeviceDesc g_UPnpDeviceDesc
Definition: upnp.h:48
The WebSocket server, which listens for connections.
Definition: websocket.h:39
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythDB * GetMythDB(void)
Definition: mythdb.cpp:50
QString GetShareDir(void)
Definition: mythdirs.cpp:280
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
@ kSSLServer
Definition: serverpool.h:33
QStringList GetSourceProtocolInfos()
Return a QStringList containing the supported Source Protocols.
Definition: upnputil.cpp:69