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