MythTV  0.26-pre
mediaserver.cpp
Go to the documentation of this file.
00001 
00002 // Program Name: mediaserver.cpp
00003 //
00004 // Purpose - uPnp Media Server main Class
00005 //
00006 // Created By  : David Blain                    Created On : Jan. 15, 2007
00007 // Modified By :                                Modified On:
00008 //
00010 
00011 #include "mediaserver.h"
00012 #include "httpconfig.h"
00013 #include "internetContent.h"
00014 #include "mythdirs.h"
00015 
00016 #include "upnpcdstv.h"
00017 #include "upnpcdsmusic.h"
00018 #include "upnpcdsvideo.h"
00019 
00020 #include <QScriptEngine>
00021 #include <QNetworkProxy>
00022 
00023 #include "serviceHosts/mythServiceHost.h"
00024 #include "serviceHosts/guideServiceHost.h"
00025 #include "serviceHosts/contentServiceHost.h"
00026 #include "serviceHosts/dvrServiceHost.h"
00027 #include "serviceHosts/channelServiceHost.h"
00028 #include "serviceHosts/videoServiceHost.h"
00029 #include "serviceHosts/captureServiceHost.h"
00030 
00031 #ifdef USING_LIBDNS_SD
00032 #include "bonjourregister.h"
00033 #endif
00034 
00037 //
00038 // UPnp Class implementaion
00039 //
00042 
00044 //
00046 
00047 MediaServer::MediaServer(void) :
00048 #ifdef USING_LIBDNS_SD
00049     m_bonjour(NULL),
00050 #endif
00051     m_pUPnpCDS(NULL), m_pUPnpCMGR(NULL),
00052     m_sSharePath(GetShareDir())
00053 {
00054     LOG(VB_UPNP, LOG_INFO, "MediaServer:ctor:Begin");
00055 
00056     // ----------------------------------------------------------------------
00057     // Initialize Configuration class (Database for Servers)
00058     // ----------------------------------------------------------------------
00059 
00060     SetConfiguration( new DBConfiguration() );
00061 
00062     // ----------------------------------------------------------------------
00063     // Create mini HTTP Server
00064     // ----------------------------------------------------------------------
00065 
00066     LOG(VB_UPNP, LOG_INFO, "MediaServer:ctor:End");
00067 }
00068 
00069 void MediaServer::Init(bool bIsMaster, bool bDisableUPnp /* = false */)
00070 {
00071     LOG(VB_UPNP, LOG_INFO, "MediaServer:Init:Begin");
00072 
00073     int     nPort     = g_pConfig->GetValue( "BackendStatusPort", 6544 );
00074 
00075     if (!m_pHttpServer)
00076     {
00077         m_pHttpServer = new HttpServer();
00078         m_pHttpServer->RegisterExtension(new HttpConfig());
00079     }
00080 
00081     if (!m_pHttpServer->isListening())
00082     {
00083         m_pHttpServer->setProxy(QNetworkProxy::NoProxy);
00084         if (!m_pHttpServer->listen(nPort))
00085         {
00086             LOG(VB_GENERAL, LOG_ERR, "MediaServer::HttpServer Create Error");
00087             delete m_pHttpServer;
00088             m_pHttpServer = NULL;
00089             return;
00090         }
00091     }
00092 
00093     QString sFileName = g_pConfig->GetValue( "upnpDescXmlPath",
00094                                                 m_sSharePath );
00095     QString sDeviceType;
00096 
00097     if ( bIsMaster )
00098         sFileName  += "devicemaster.xml";
00099     else
00100         sFileName += "deviceslave.xml";
00101 
00102     // ------------------------------------------------------------------
00103     // Make sure our device Description is loaded.
00104     // ------------------------------------------------------------------
00105 
00106     LOG(VB_UPNP, LOG_INFO,
00107         "MediaServer::Loading UPnp Description " + sFileName);
00108 
00109     g_UPnpDeviceDesc.Load( sFileName );
00110 
00111     // ------------------------------------------------------------------
00112     // Register Http Server Extensions...
00113     // ------------------------------------------------------------------
00114 
00115     LOG(VB_UPNP, LOG_INFO, "MediaServer::Registering Http Server Extensions.");
00116 
00117     m_pHttpServer->RegisterExtension( new InternetContent   ( m_sSharePath ));
00118 
00119     m_pHttpServer->RegisterExtension( new MythServiceHost   ( m_sSharePath ));
00120     m_pHttpServer->RegisterExtension( new GuideServiceHost  ( m_sSharePath ));
00121     m_pHttpServer->RegisterExtension( new ContentServiceHost( m_sSharePath ));
00122     m_pHttpServer->RegisterExtension( new DvrServiceHost    ( m_sSharePath ));
00123     m_pHttpServer->RegisterExtension( new ChannelServiceHost( m_sSharePath ));
00124     m_pHttpServer->RegisterExtension( new VideoServiceHost  ( m_sSharePath ));
00125     m_pHttpServer->RegisterExtension( new CaptureServiceHost( m_sSharePath ));
00126 
00127     QString sIP = g_pConfig->GetValue( "BackendServerIP"  , ""   );
00128     if (sIP.isEmpty())
00129     {
00130         LOG(VB_GENERAL, LOG_ERR,
00131             "MediaServer:: No BackendServerIP Address defined - "
00132             "Disabling UPnP");
00133         return;
00134     }
00135 
00136     // ------------------------------------------------------------------
00137     // Register Service Types with Scripting Engine
00138     //
00139     // -=>NOTE: We need to know the actual type at compile time for this 
00140     //          to work, so it needs to be done here.  I'm still looking
00141     //          into ways that we may encapsulate this in the service 
00142     //          classes.
00143     // ------------------------------------------------------------------
00144 
00145 
00146      QScriptEngine* pEngine = m_pHttpServer->ScriptEngine();
00147 
00148      pEngine->globalObject().setProperty("Myth"   ,
00149          pEngine->scriptValueFromQMetaObject< ScriptableMyth    >() );
00150      pEngine->globalObject().setProperty("Guide"  ,
00151          pEngine->scriptValueFromQMetaObject< ScriptableGuide   >() );
00152      pEngine->globalObject().setProperty("Content",
00153          pEngine->scriptValueFromQMetaObject< Content           >() );
00154      pEngine->globalObject().setProperty("Dvr"    ,
00155          pEngine->scriptValueFromQMetaObject< ScriptableDvr     >() );
00156      pEngine->globalObject().setProperty("Channel",
00157          pEngine->scriptValueFromQMetaObject< ScriptableChannel >() );
00158      pEngine->globalObject().setProperty("Video"  ,
00159          pEngine->scriptValueFromQMetaObject< ScriptableVideo   >() );
00160      pEngine->globalObject().setProperty("Capture"  ,
00161          pEngine->scriptValueFromQMetaObject< ScriptableCapture   >() );
00162 
00163     // ------------------------------------------------------------------
00164 
00165     if (sIP == "localhost" || sIP.startsWith("127."))
00166     {
00167         LOG(VB_GENERAL, LOG_NOTICE,
00168             "MediaServer:: Loopback address specified - " + sIP +
00169             ". Disabling UPnP");
00170         return;
00171     }
00172 
00173     if (bDisableUPnp)
00174     {
00175         LOG(VB_GENERAL, LOG_NOTICE,
00176             "*** The UPNP service has been DISABLED with the "
00177             "--noupnp option ***");
00178         return;
00179     }
00180 
00181     // ----------------------------------------------------------------------
00182     // BackendServerIP is only one IP address at this time... Doing Split anyway
00183     // ----------------------------------------------------------------------
00184 
00185     QStringList sIPAddrList = sIP.split(';', QString::SkipEmptyParts);
00186 
00187     // ----------------------------------------------------------------------
00188     // Initialize UPnp Stack
00189     // ----------------------------------------------------------------------
00190 
00191     if (Initialize( sIPAddrList, nPort, m_pHttpServer ))
00192     {
00193 
00194         // ------------------------------------------------------------------
00195         // Register any HttpServerExtensions... Only The Master Backend
00196         // ------------------------------------------------------------------
00197 
00198         if (bIsMaster)
00199         {
00200             QString sSourceProtocols = "http-get:*:image/gif:*,"
00201                                        "http-get:*:image/jpeg:*,"
00202                                        "http-get:*:image/png:*,"
00203                                        "http-get:*:video/avi:*,"
00204                                        "http-get:*:audio/mpeg:*,"
00205                                        "http-get:*:audio/wav:*,"
00206                                        "http-get:*:video/mpeg:*,"
00207                                        "http-get:*:video/nupplevideo:*,"
00208                                        "http-get:*:video/x-ms-wmv:*";
00209 
00210             LOG(VB_UPNP, LOG_INFO, "MediaServer::Registering MSRR Service.");
00211 
00212             m_pHttpServer->RegisterExtension( new UPnpMSRR( RootDevice(),
00213                                                 m_sSharePath ) );
00214 
00215             LOG(VB_UPNP, LOG_INFO, "MediaServer::Registering CMGR Service.");
00216 
00217             m_pUPnpCMGR = new UPnpCMGR( RootDevice(),
00218                                         m_sSharePath, sSourceProtocols );
00219             m_pHttpServer->RegisterExtension( m_pUPnpCMGR );
00220 
00221             LOG(VB_UPNP, LOG_INFO, "MediaServer::Registering CDS Service.");
00222 
00223             m_pUPnpCDS = new UPnpCDS ( RootDevice(), m_sSharePath );
00224             m_pHttpServer->RegisterExtension( m_pUPnpCDS );
00225 
00226             // ----------------------------------------------------------------
00227             // Register CDS Extensions
00228             // ----------------------------------------------------------------
00229 
00230             LOG(VB_UPNP, LOG_INFO, 
00231                 "MediaServer::Registering UPnpCDSTv Extension");
00232 
00233             RegisterExtension(new UPnpCDSTv());
00234 
00235             LOG(VB_UPNP, LOG_INFO,
00236                 "MediaServer::Registering UPnpCDSMusic Extension");
00237 
00238             RegisterExtension(new UPnpCDSMusic());
00239 
00240             LOG(VB_UPNP, LOG_INFO,
00241                 "MediaServer::Registering UPnpCDSVideo Extension");
00242 
00243             RegisterExtension(new UPnpCDSVideo());
00244         }
00245 
00246 #if 0
00247         LOG(VB_UPNP, LOG_INFO, "MediaServer::Adding Context Listener");
00248 
00249         gCoreContext->addListener( this );
00250 #endif
00251 
00252         Start();
00253 
00254 #ifdef USING_LIBDNS_SD
00255         // advertise using Bonjour
00256         m_bonjour = new BonjourRegister();
00257         if (m_bonjour)
00258         {
00259             QByteArray dummy;
00260             QByteArray name("Mythbackend on ");
00261             name.append(gCoreContext->GetHostName());
00262             m_bonjour->Register(nPort,
00263                                 bIsMaster ? "_mythbackend-master._tcp" :
00264                                             "_mythbackend-slave._tcp",
00265                                 name, dummy);
00266         }
00267 #endif
00268     }
00269 
00270     LOG(VB_UPNP, LOG_INFO, "MediaServer:Init:End");
00271 }
00272 
00274 //
00276 
00277 MediaServer::~MediaServer()
00278 {
00279     // -=>TODO: Need to check to see if calling this more than once is ok.
00280 
00281 #if 0
00282     gCoreContext->removeListener(this);
00283 #endif
00284 
00285     delete m_pHttpServer;
00286 
00287 #ifdef USING_LIBDNS_SD
00288     delete m_bonjour;
00289 #endif
00290 }
00291 
00293 //
00295 #if 0
00296 void MediaServer::customEvent( QEvent *e )
00297 {
00298     if (MythEvent::Type(e->type()) == MythEvent::MythEventMessage)
00299     {
00300         MythEvent *me = (MythEvent *)e;
00301         QString message = me->Message();
00302 
00303         //-=>TODO: Need to handle events to notify clients of changes
00304     }
00305 }
00306 #endif
00307 
00308 //
00310 
00311 void MediaServer::RegisterExtension( UPnpCDSExtension *pExtension )
00312 {
00313     m_pUPnpCDS->RegisterExtension( pExtension );
00314 }
00315 
00317 //
00319 
00320 void MediaServer::UnregisterExtension( UPnpCDSExtension *pExtension )
00321 {
00322     m_pUPnpCDS->UnregisterExtension( pExtension );
00323 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends