MythTV  master
mediarenderer.cpp
Go to the documentation of this file.
1 // Program Name: mediarenderer.cpp
3 //
4 // Purpose - uPnp Media Renderer 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 #if CONFIG_QTSCRIPT
14 #include <QScriptEngine>
15 #endif
16 #include <QTextStream>
17 
18 // MythTV
19 #include "libmythbase/compat.h"
21 #include "libmythbase/mythdate.h"
22 #include "libmythbase/mythversion.h"
23 #include "libmythupnp/htmlserver.h"
25 #include "libmythupnp/upnputil.h"
26 
27 // MythFrontend
28 #include "mediarenderer.h"
29 #include "mythfexml.h"
31 #include "services/frontend.h"
32 #include "upnpscanner.h"
33 
36 //
37 // UPnp MediaRenderer Class implementaion
38 //
41 
43 //
45 
47 {
48  LOG(VB_UPNP, LOG_INFO, "MediaRenderer(): Begin");
49 
50  int nPort = XmlConfiguration().GetValue("UPnP/MythFrontend/ServicePort", 6547);
51  // frontend upnp server is now ServicePort + 4 (default 6551)
52  nPort += 4;
53 
54  auto *pHttpServer = new HttpServer();
55 
56  if (!pHttpServer)
57  return;
58 
59  if (!pHttpServer->listen(nPort))
60  {
61  LOG(VB_GENERAL, LOG_ERR, "MediaRenderer: HttpServer Create Error");
62  delete pHttpServer;
63  pHttpServer = nullptr;
64  return;
65  }
66 
67  // ------------------------------------------------------------------
68  // Register any HttpServerExtensions...
69  // ------------------------------------------------------------------
70 
71  auto *pHtmlServer =
72  new HtmlServerExtension(pHttpServer->GetSharePath() + "html",
73  "frontend_");
74  pHttpServer->RegisterExtension(pHtmlServer);
75  pHttpServer->RegisterExtension(new FrontendServiceHost(pHttpServer->GetSharePath()));
76 
77  // ------------------------------------------------------------------
78  // Register Service Types with Scripting Engine
79  //
80  // -=>NOTE: We need to know the actual type at compile time for this
81  // to work, so it needs to be done here. I'm still looking
82  // into ways that we may encapsulate this in the service
83  // classes. - dblain
84  // ------------------------------------------------------------------
85 
86 #if CONFIG_QTSCRIPT
87  QScriptEngine* pEngine = pHtmlServer->ScriptEngine();
88 
89  pEngine->globalObject().setProperty("Frontend" ,
90  pEngine->scriptValueFromQMetaObject< ScriptableFrontend >() );
91 #endif
92 
93  // ----------------------------------------------------------------------
94  // Initialize UPnp Stack
95  // ----------------------------------------------------------------------
96 
97  if (Initialize( nPort, pHttpServer ))
98  {
99  // ------------------------------------------------------------------
100  // Create device Description
101  // ------------------------------------------------------------------
102 
103  LOG(VB_UPNP, LOG_INFO, "MediaRenderer: Creating UPnp Description");
104 
106 
107  device.m_sDeviceType = "urn:schemas-upnp-org:device:MediaRenderer:1";
108  device.m_sFriendlyName = "MythTV AV Renderer";
109  device.m_sManufacturer = "MythTV";
110  device.m_sManufacturerURL = "http://www.mythtv.org";
111  device.m_sModelDescription = "MythTV AV Media Renderer";
112  device.m_sModelName = "MythTV AV Media Renderer";
113  device.m_sModelURL = "http://www.mythtv.org";
114  device.m_sUPC = "";
115  device.m_sPresentationURL = "/";
116 
117  QString sSinkProtocols = GetSinkProtocolInfos().join(",");
118 
119  // ------------------------------------------------------------------
120  // Register the MythFEXML protocol...
121  // ------------------------------------------------------------------
122  LOG(VB_UPNP, LOG_INFO, "MediaRenderer: Registering MythFEXML Extension.");
125 
126 #if 0
127  LOG(VB_UPNP, LOG_INFO,
128  "MediaRenderer::Registering AVTransport Service.");
129  m_pUPnpAVT = new UPnpAVTransport( RootDevice() );
130  m_pHttpServer->RegisterExtension( m_pUPnpAVT );
131 #endif
132 
133  LOG(VB_UPNP, LOG_INFO, "MediaRenderer: Registering ConnectionManager Service.");
134  // HttpServer will be responsible for deleting UPnpCMGR
135  m_pUPnpCMGR = new UPnpCMGR(
136  RootDevice(), m_pHttpServer->GetSharePath(), "", sSinkProtocols);
138 
139 #if 0
140  LOG(VB_UPNP, LOG_INFO,
141  "MediaRenderer::Registering RenderingControl Service.");
142  m_pUPnpRCTL= new UPnpRCTL( RootDevice() );
143  m_pHttpServer->RegisterExtension( m_pUPnpRCTL );
144 #endif
145 
146  UPNPSubscription *subscription = nullptr;
147  if (qEnvironmentVariableIsSet("MYTHTV_UPNPSCANNER"))
148  {
149  LOG(VB_UPNP, LOG_INFO, "MediaRenderer: Registering UPnP Subscription Extension.");
150  subscription = new UPNPSubscription(m_pHttpServer->GetSharePath(), nPort);
151  m_pHttpServer->RegisterExtension(subscription);
152  }
153 
154  Start();
155 
156  // Start scanning for UPnP media servers
157  if (subscription)
158  UPNPScanner::Enable(true, subscription);
159 
160  // ensure the frontend is aware of all backends (slave and master) and
161  // other frontends
162  SSDP::Instance()->PerformSearch("ssdp:all");
163  }
164  else
165  {
166  LOG(VB_GENERAL, LOG_ERR,
167  "MediaRenderer: Unable to Initialize UPnp Stack");
168  }
169 
170  LOG(VB_UPNP, LOG_INFO, "MediaRenderer(): End");
171 }
172 
174 //
176 
178 {
179  UPNPScanner::Enable(false);
180  delete m_pHttpServer;
181 }
UPnpDevice::m_sModelURL
QString m_sModelURL
Definition: upnpdevice.h:113
UPnpDevice::m_sManufacturer
QString m_sManufacturer
Definition: upnpdevice.h:108
SSDP::PerformSearch
void PerformSearch(const QString &sST, std::chrono::seconds timeout=2s)
Definition: ssdp.cpp:204
upnpsubscription.h
UPNPSubscription
Definition: upnpsubscription.h:10
UPnp::Start
virtual void Start()
Definition: upnp.cpp:144
MediaRenderer::~MediaRenderer
~MediaRenderer() override
Definition: mediarenderer.cpp:177
UPnpDevice::m_sUPC
QString m_sUPC
Definition: upnpdevice.h:115
HttpServer::RegisterExtension
void RegisterExtension(HttpServerExtension *pExtension)
Definition: httpserver.cpp:321
MythFEXML
Definition: mythfexml.h:24
UPnpDevice::m_sDeviceType
QString m_sDeviceType
Definition: upnpdevice.h:106
FrontendServiceHost
Definition: frontendServiceHost.h:7
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
XmlConfiguration
Definition: configuration.h:38
UPnp::Initialize
bool Initialize(int nServicePort, HttpServer *pHttpServer)
Definition: upnp.cpp:74
SSDP::Instance
static SSDP * Instance()
Definition: ssdp.cpp:55
UPnpDeviceDesc::m_rootDevice
UPnpDevice m_rootDevice
Definition: upnpdevice.h:155
mythdate.h
UPnpDevice::m_sModelName
QString m_sModelName
Definition: upnpdevice.h:111
MediaRenderer::m_pUPnpCMGR
UPnpCMGR * m_pUPnpCMGR
Definition: mediarenderer.h:34
compat.h
GetSinkProtocolInfos
QStringList GetSinkProtocolInfos()
Return a QStringList containing the supported Sink Protocols.
Definition: upnputil.cpp:138
mediarenderer.h
HtmlServerExtension
Definition: htmlserver.h:29
UPnpDevice
Definition: upnpdevice.h:102
UPnpDevice::m_sFriendlyName
QString m_sFriendlyName
Definition: upnpdevice.h:107
htmlserver.h
frontend.h
UPnpCMGR
Definition: upnpcmgr.h:48
HttpServer::GetSharePath
QString GetSharePath(void) const
Definition: httpserver.h:128
UPnp::RootDevice
static UPnpDevice * RootDevice()
Definition: upnp.h:126
mythfexml.h
XmlConfiguration::GetValue
QString GetValue(const QString &setting)
Definition: configuration.cpp:185
UPnpDevice::m_sModelDescription
QString m_sModelDescription
Definition: upnpdevice.h:110
configuration.h
UPnpDevice::m_sManufacturerURL
QString m_sManufacturerURL
Definition: upnpdevice.h:109
HttpServer
Definition: httpserver.h:112
UPNPScanner::Enable
static void Enable(bool enable, UPNPSubscription *sub=nullptr)
Definition: upnpscanner.cpp:149
UPnp::m_pHttpServer
HttpServer * m_pHttpServer
Definition: upnp.h:104
upnpscanner.h
UPnpDevice::m_sPresentationURL
QString m_sPresentationURL
Definition: upnpdevice.h:116
frontendServiceHost.h
UPnp::g_UPnpDeviceDesc
static UPnpDeviceDesc g_UPnpDeviceDesc
Definition: upnp.h:109
upnputil.h
MediaRenderer::MediaRenderer
MediaRenderer()
Definition: mediarenderer.cpp:46