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 
52  auto *pHttpServer = new HttpServer();
53 
54  if (!pHttpServer)
55  return;
56 
57  if (!pHttpServer->listen(nPort))
58  {
59  LOG(VB_GENERAL, LOG_ERR, "MediaRenderer: HttpServer Create Error");
60  delete pHttpServer;
61  pHttpServer = nullptr;
62  return;
63  }
64 
65  // ------------------------------------------------------------------
66  // Register any HttpServerExtensions...
67  // ------------------------------------------------------------------
68 
69  auto *pHtmlServer =
70  new HtmlServerExtension(pHttpServer->GetSharePath() + "html",
71  "frontend_");
72  pHttpServer->RegisterExtension(pHtmlServer);
73  pHttpServer->RegisterExtension(new FrontendServiceHost(pHttpServer->GetSharePath()));
74 
75  // ------------------------------------------------------------------
76  // Register Service Types with Scripting Engine
77  //
78  // -=>NOTE: We need to know the actual type at compile time for this
79  // to work, so it needs to be done here. I'm still looking
80  // into ways that we may encapsulate this in the service
81  // classes. - dblain
82  // ------------------------------------------------------------------
83 
84 #if CONFIG_QTSCRIPT
85  QScriptEngine* pEngine = pHtmlServer->ScriptEngine();
86 
87  pEngine->globalObject().setProperty("Frontend" ,
88  pEngine->scriptValueFromQMetaObject< ScriptableFrontend >() );
89 #endif
90 
91  // ----------------------------------------------------------------------
92  // Initialize UPnp Stack
93  // ----------------------------------------------------------------------
94 
95  if (Initialize( nPort, pHttpServer ))
96  {
97  // ------------------------------------------------------------------
98  // Create device Description
99  // ------------------------------------------------------------------
100 
101  LOG(VB_UPNP, LOG_INFO, "MediaRenderer: Creating UPnp Description");
102 
104 
105  device.m_sDeviceType = "urn:schemas-upnp-org:device:MediaRenderer:1";
106  device.m_sFriendlyName = "MythTV AV Renderer";
107  device.m_sManufacturer = "MythTV";
108  device.m_sManufacturerURL = "http://www.mythtv.org";
109  device.m_sModelDescription = "MythTV AV Media Renderer";
110  device.m_sModelName = "MythTV AV Media Renderer";
111  device.m_sModelURL = "http://www.mythtv.org";
112  device.m_sUPC = "";
113  device.m_sPresentationURL = "/";
114 
115  QString sSinkProtocols = GetSinkProtocolInfos().join(",");
116 
117  // ------------------------------------------------------------------
118  // Register the MythFEXML protocol...
119  // ------------------------------------------------------------------
120  LOG(VB_UPNP, LOG_INFO, "MediaRenderer: Registering MythFEXML Extension.");
123 
124 #if 0
125  LOG(VB_UPNP, LOG_INFO,
126  "MediaRenderer::Registering AVTransport Service.");
127  m_pUPnpAVT = new UPnpAVTransport( RootDevice() );
128  m_pHttpServer->RegisterExtension( m_pUPnpAVT );
129 #endif
130 
131  LOG(VB_UPNP, LOG_INFO, "MediaRenderer: Registering ConnectionManager Service.");
132  // HttpServer will be responsible for deleting UPnpCMGR
133  m_pUPnpCMGR = new UPnpCMGR(
134  RootDevice(), m_pHttpServer->GetSharePath(), "", sSinkProtocols);
136 
137 #if 0
138  LOG(VB_UPNP, LOG_INFO,
139  "MediaRenderer::Registering RenderingControl Service.");
140  m_pUPnpRCTL= new UPnpRCTL( RootDevice() );
141  m_pHttpServer->RegisterExtension( m_pUPnpRCTL );
142 #endif
143 
144  UPNPSubscription *subscription = nullptr;
145  if (qEnvironmentVariableIsSet("MYTHTV_UPNPSCANNER"))
146  {
147  LOG(VB_UPNP, LOG_INFO, "MediaRenderer: Registering UPnP Subscription Extension.");
148  subscription = new UPNPSubscription(m_pHttpServer->GetSharePath(), nPort);
149  m_pHttpServer->RegisterExtension(subscription);
150  }
151 
152  Start();
153 
154  // Start scanning for UPnP media servers
155  if (subscription)
156  UPNPScanner::Enable(true, subscription);
157 
158  // ensure the frontend is aware of all backends (slave and master) and
159  // other frontends
160  SSDP::Instance()->PerformSearch("ssdp:all");
161  }
162  else
163  {
164  LOG(VB_GENERAL, LOG_ERR,
165  "MediaRenderer: Unable to Initialize UPnp Stack");
166  }
167 
168  LOG(VB_UPNP, LOG_INFO, "MediaRenderer(): End");
169 }
170 
172 //
174 
176 {
177  UPNPScanner::Enable(false);
178  delete m_pHttpServer;
179 }
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:143
MediaRenderer::~MediaRenderer
~MediaRenderer() override
Definition: mediarenderer.cpp:175
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:142
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:183
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:153
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