1 /*****************************************************************************
2 * Upnp_intell.cpp : UPnP discovery module (Intel SDK)
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
7 * Authors: RĂ©mi Denis-Courmont <rem # videolan.org> (original plugin)
8 * Christian Henz <henz # c-lab.de>
10 * UPnP Plugin using the Intel SDK (libupnp) instead of CyberLink
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 \TODO: Debug messages: "__FILE__, __LINE__" ok ???, Wrn/Err ???
29 \TODO: Change names to VLC standard ???
37 #include <upnp/upnp.h>
38 #include <upnp/upnptools.h>
44 #include "vlc_strings.h"
49 struct services_discovery_sys_t
51 playlist_item_t *p_node_cat;
52 playlist_item_t *p_node;
53 playlist_t *p_playlist;
59 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
60 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
66 class MediaServerList;
70 // Cookie that is passed to the callback
74 services_discovery_t* serviceDiscovery;
75 UpnpClient_Handle clientHandle;
76 MediaServerList* serverList;
80 // Class definitions...
88 vlc_mutex_init( c->serviceDiscovery, &_mutex );
93 vlc_mutex_destroy( &_mutex );
96 void lock() { vlc_mutex_lock( &_mutex ); }
97 void unlock() { vlc_mutex_unlock( &_mutex ); }
108 Locker( Lockable* l )
128 static void parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie );
130 MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie );
133 const char* getUDN() const;
134 const char* getFriendlyName() const;
136 void setContentDirectoryEventURL( const char* url );
137 const char* getContentDirectoryEventURL() const;
139 void setContentDirectoryControlURL( const char* url );
140 const char* getContentDirectoryControlURL() const;
142 void subscribeToContentDirectory();
143 void fetchContents();
145 void setPlaylistNode( playlist_item_t* node );
147 bool compareSID( const char* sid );
151 bool _fetchContents( Container* parent );
152 void _buildPlaylist( Container* container );
153 IXML_Document* _browseAction( const char*, const char*, const char*, const char*, const char*, const char* );
157 Container* _contents;
158 playlist_item_t* _playlistNode;
161 std::string _friendlyName;
163 std::string _contentDirectoryEventURL;
164 std::string _contentDirectoryControlURL;
166 int _subscriptionTimeOut;
167 Upnp_SID _subscriptionID;
171 class MediaServerList
175 MediaServerList( Cookie* cookie );
178 bool addServer( MediaServer* s );
179 void removeServer( const char* UDN );
181 MediaServer* getServer( const char* UDN );
182 MediaServer* getServerBySID( const char* );
188 std::vector<MediaServer*> _list;
196 Item( Container* parent, const char* objectID, const char* title, const char* resource );
198 const char* getObjectID() const;
199 const char* getTitle() const;
200 const char* getResource() const;
202 void setPlaylistNode( playlist_item_t* node );
203 playlist_item_t* getPlaylistNode() const ;
207 playlist_item_t* _playlistNode;
210 std::string _objectID;
212 std::string _resource;
220 Container( Container* parent, const char* objectID, const char* title );
223 void addItem( Item* item );
224 void addContainer( Container* container );
226 const char* getObjectID() const;
227 const char* getTitle() const;
229 unsigned int getNumItems() const;
230 unsigned int getNumContainers() const;
232 Item* getItem( unsigned int i ) const;
233 Container* getContainer( unsigned int i ) const;
235 void setPlaylistNode( playlist_item_t* node );
236 playlist_item_t* getPlaylistNode() const;
240 playlist_item_t* _playlistNode;
244 std::string _objectID;
246 std::vector<Item*> _items;
247 std::vector<Container*> _containers;
251 // VLC callback prototypes
253 static int Open( vlc_object_t* );
254 static void Close( vlc_object_t* );
255 static void Run( services_discovery_t *p_sd );
260 set_shortname( "UPnP" );
261 set_description( _( "Universal Plug'n'Play discovery ( Intel SDK )" ) );
262 set_category( CAT_PLAYLIST );
263 set_subcategory( SUBCAT_PLAYLIST_SD );
264 set_capability( "services_discovery", 0 );
265 set_callbacks( Open, Close );
269 // More prototypes...
271 static Lockable* CallbackLock;
272 static int Callback( Upnp_EventType eventType, void* event, void* pCookie );
274 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName );
275 IXML_Document* parseBrowseResult( IXML_Document* doc );
280 static int Open( vlc_object_t *p_this )
282 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
283 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
284 malloc( sizeof( services_discovery_sys_t ) );
289 /* Create our playlist node */
290 p_sys->p_playlist = ( playlist_t * )vlc_object_find( p_sd,
293 if( !p_sys->p_playlist )
295 msg_Warn( p_sd, "unable to find playlist, cancelling UPnP listening" );
299 playlist_NodesPairCreate( p_sys->p_playlist, _("Devices"),
300 &p_sys->p_node_cat, &p_sys->p_node,
306 static void Close( vlc_object_t *p_this )
308 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
309 services_discovery_sys_t *p_sys = p_sd->p_sys;
311 if( p_sys->p_playlist )
313 playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node, VLC_TRUE,
315 vlc_object_release( p_sys->p_playlist );
321 static void Run( services_discovery_t* p_sd )
325 res = UpnpInit( 0, 0 );
326 if( res != UPNP_E_SUCCESS )
328 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
333 cookie.serviceDiscovery = p_sd;
334 cookie.serverList = new MediaServerList( &cookie );
336 CallbackLock = new Lockable( &cookie );
338 res = UpnpRegisterClient( Callback, &cookie, &cookie.clientHandle );
339 if( res != UPNP_E_SUCCESS )
341 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
345 res = UpnpSearchAsync( cookie.clientHandle, 5, MEDIA_SERVER_DEVICE_TYPE, &cookie );
346 if( res != UPNP_E_SUCCESS )
348 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
352 msg_Dbg( p_sd, "UPnP discovery started" );
353 while( !p_sd->b_die )
358 msg_Dbg( p_sd, "UPnP discovery stopped" );
362 delete cookie.serverList;
367 // XML utility functions:
369 // Returns the value of a child element, or 0 on error
370 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName )
372 if ( !parent ) return 0;
373 if ( !tagName ) return 0;
375 char* s = strdup( tagName );
376 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
378 if ( !nodeList ) return 0;
380 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
381 ixmlNodeList_free( nodeList );
382 if ( !element ) return 0;
384 IXML_Node* textNode = ixmlNode_getFirstChild( element );
385 if ( !textNode ) return 0;
387 return ixmlNode_getNodeValue( textNode );
390 // Extracts the result document from a SOAP response
391 IXML_Document* parseBrowseResult( IXML_Document* doc )
393 if ( !doc ) return 0;
395 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc, "Result" );
396 if ( !resultList ) return 0;
398 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
400 ixmlNodeList_free( resultList );
402 if ( !resultNode ) return 0;
404 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
405 if ( !textNode ) return 0;
407 const char* resultString = ixmlNode_getNodeValue( textNode );
408 char* resultXML = strdup( resultString );
410 resolve_xml_special_chars( resultXML );
412 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
420 // Handles all UPnP events
421 static int Callback( Upnp_EventType eventType, void* event, void* pCookie )
423 Locker locker( CallbackLock );
425 Cookie* cookie = ( Cookie* )pCookie;
427 switch( eventType ) {
429 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
430 case UPNP_DISCOVERY_SEARCH_RESULT:
432 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
434 IXML_Document *descriptionDoc = 0;
437 res = UpnpDownloadXmlDoc( discovery->Location, &descriptionDoc );
438 if ( res != UPNP_E_SUCCESS )
440 msg_Dbg( cookie->serviceDiscovery, "%s:%d: Could not download device description!", __FILE__, __LINE__ );
444 MediaServer::parseDeviceDescription( descriptionDoc, discovery->Location, cookie );
446 ixmlDocument_free( descriptionDoc );
450 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
452 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
454 cookie->serverList->removeServer( discovery->DeviceId );
458 case UPNP_EVENT_RECEIVED:
460 Upnp_Event* e = ( Upnp_Event* )event;
462 MediaServer* server = cookie->serverList->getServerBySID( e->Sid );
463 if ( server ) server->fetchContents();
467 case UPNP_EVENT_AUTORENEWAL_FAILED:
468 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
472 Upnp_Event_Subscribe* s = ( Upnp_Event_Subscribe* )event;
474 MediaServer* server = cookie->serverList->getServerBySID( s->Sid );
475 if ( server ) server->subscribeToContentDirectory();
480 msg_Dbg( cookie->serviceDiscovery, "%s:%d: DEBUG: UNHANDLED EVENT ( TYPE=%d )", __FILE__, __LINE__, eventType );
484 return UPNP_E_SUCCESS;
488 // Class implementations...
492 void MediaServer::parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie )
494 if ( !doc ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
495 if ( !location ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
497 const char* baseURL = location;
499 // Try to extract baseURL
501 IXML_NodeList* urlList = ixmlDocument_getElementsByTagName( doc, "baseURL" );
504 if ( IXML_Node* urlNode = ixmlNodeList_item( urlList, 0 ) )
506 IXML_Node* textNode = ixmlNode_getFirstChild( urlNode );
507 if ( textNode ) baseURL = ixmlNode_getNodeValue( textNode );
510 ixmlNodeList_free( urlList );
515 IXML_NodeList* deviceList = ixmlDocument_getElementsByTagName( doc, "device" );
519 for ( unsigned int i = 0; i < ixmlNodeList_length( deviceList ); i++ )
521 IXML_Element* deviceElement = ( IXML_Element* )ixmlNodeList_item( deviceList, i );
523 const char* deviceType = xml_getChildElementValue( deviceElement, "deviceType" );
524 if ( !deviceType ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no deviceType!", __FILE__, __LINE__ ); continue; }
525 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, deviceType ) != 0 ) continue;
527 const char* UDN = xml_getChildElementValue( deviceElement, "UDN" );
528 if ( !UDN ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no UDN!", __FILE__, __LINE__ ); continue; }
529 if ( cookie->serverList->getServer( UDN ) != 0 ) continue;
531 const char* friendlyName = xml_getChildElementValue( deviceElement, "friendlyName" );
532 if ( !friendlyName ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no friendlyName!", __FILE__, __LINE__ ); continue; }
534 MediaServer* server = new MediaServer( UDN, friendlyName, cookie );
535 if ( !cookie->serverList->addServer( server ) ) {
542 // Check for ContentDirectory service...
544 IXML_NodeList* serviceList = ixmlElement_getElementsByTagName( deviceElement, "service" );
547 for ( unsigned int j = 0; j < ixmlNodeList_length( serviceList ); j++ )
549 IXML_Element* serviceElement = ( IXML_Element* )ixmlNodeList_item( serviceList, j );
551 const char* serviceType = xml_getChildElementValue( serviceElement, "serviceType" );
552 if ( !serviceType ) continue;
553 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE, serviceType ) != 0 ) continue;
555 const char* eventSubURL = xml_getChildElementValue( serviceElement, "eventSubURL" );
556 if ( !eventSubURL ) continue;
558 const char* controlURL = xml_getChildElementValue( serviceElement, "controlURL" );
559 if ( !controlURL ) continue;
561 // Try to subscribe to ContentDirectory service
563 char* url = ( char* )malloc( strlen( baseURL ) + strlen( eventSubURL ) + 1 );
566 char* s1 = strdup( baseURL );
567 char* s2 = strdup( eventSubURL );
569 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
571 // msg_Dbg( cookie->serviceDiscovery, "CDS EVENT URL: %s", url );
573 server->setContentDirectoryEventURL( url );
574 server->subscribeToContentDirectory();
582 // Try to browse content directory...
584 url = ( char* )malloc( strlen( baseURL ) + strlen( controlURL ) + 1 );
587 char* s1 = strdup( baseURL );
588 char* s2 = strdup( controlURL );
590 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
592 // msg_Dbg( cookie->serviceDiscovery, "CDS CTRL URL: %s", url );
594 server->setContentDirectoryControlURL( url );
595 server->fetchContents();
604 ixmlNodeList_free( serviceList );
608 ixmlNodeList_free( deviceList );
612 MediaServer::MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie )
617 _friendlyName = friendlyName;
623 MediaServer::~MediaServer()
627 playlist_NodeDelete( _cookie->serviceDiscovery->p_sys->p_playlist,
636 const char* MediaServer::getUDN() const
638 const char* s = _UDN.c_str();
642 const char* MediaServer::getFriendlyName() const
644 const char* s = _friendlyName.c_str();
648 void MediaServer::setContentDirectoryEventURL( const char* url )
650 _contentDirectoryEventURL = url;
653 const char* MediaServer::getContentDirectoryEventURL() const
655 const char* s = _contentDirectoryEventURL.c_str();
659 void MediaServer::setContentDirectoryControlURL( const char* url )
661 _contentDirectoryControlURL = url;
664 const char* MediaServer::getContentDirectoryControlURL() const
666 return _contentDirectoryControlURL.c_str();
669 void MediaServer::subscribeToContentDirectory()
671 const char* url = getContentDirectoryEventURL();
672 if ( !url || strcmp( url, "" ) == 0 )
674 msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" );
681 int res = UpnpSubscribe( _cookie->clientHandle, url, &timeOut, sid );
683 if ( res == UPNP_E_SUCCESS )
685 _subscriptionTimeOut = timeOut;
686 memcpy( _subscriptionID, sid, sizeof( Upnp_SID ) );
690 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: WARNING: '%s': %s", __FILE__, __LINE__, getFriendlyName(), UpnpGetErrorMessage( res ) );
694 IXML_Document* MediaServer::_browseAction( const char* pObjectID, const char* pBrowseFlag, const char* pFilter,
695 const char* pStartingIndex, const char* pRequestedCount, const char* pSortCriteria )
697 IXML_Document* action = 0;
698 IXML_Document* response = 0;
700 const char* url = getContentDirectoryControlURL();
701 if ( !url || strcmp( url, "" ) == 0 ) { msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" ); return 0; }
703 char* ObjectID = strdup( pObjectID );
704 char* BrowseFlag = strdup( pBrowseFlag );
705 char* Filter = strdup( pFilter );
706 char* StartingIndex = strdup( pStartingIndex );
707 char* RequestedCount = strdup( pRequestedCount );
708 char* SortCriteria = strdup( pSortCriteria );
710 char* serviceType = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
714 res = UpnpAddToAction( &action, "Browse", serviceType, "ObjectID", ObjectID );
715 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
717 res = UpnpAddToAction( &action, "Browse", serviceType, "BrowseFlag", BrowseFlag );
718 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
720 res = UpnpAddToAction( &action, "Browse", serviceType, "Filter", Filter );
721 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
723 res = UpnpAddToAction( &action, "Browse", serviceType, "StartingIndex", StartingIndex );
724 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
726 res = UpnpAddToAction( &action, "Browse", serviceType, "RequestedCount", RequestedCount );
727 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
729 res = UpnpAddToAction( &action, "Browse", serviceType, "SortCriteria", SortCriteria );
730 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
732 res = UpnpSendAction( _cookie->clientHandle,
734 CONTENT_DIRECTORY_SERVICE_TYPE,
738 if ( res != UPNP_E_SUCCESS )
740 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) );
741 ixmlDocument_free( response );
750 free( StartingIndex );
751 free( RequestedCount );
752 free( SortCriteria );
756 ixmlDocument_free( action );
760 void MediaServer::fetchContents()
762 Container* root = new Container( 0, "0", getFriendlyName() );
763 _fetchContents( root );
767 vlc_mutex_lock( &_cookie->serviceDiscovery->p_sys->p_playlist->object_lock );
769 playlist_NodeEmpty( _cookie->serviceDiscovery->p_sys->p_playlist,
773 vlc_mutex_unlock( &_cookie->serviceDiscovery->p_sys->p_playlist->object_lock );
779 _contents->setPlaylistNode( _playlistNode );
781 _buildPlaylist( _contents );
784 bool MediaServer::_fetchContents( Container* parent )
786 if (!parent) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: parent==NULL", __FILE__, __LINE__ ); return false; }
788 IXML_Document* response = _browseAction( parent->getObjectID(), "BrowseDirectChildren", "*", "0", "0", "" );
789 if ( !response ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
791 IXML_Document* result = parseBrowseResult( response );
792 ixmlDocument_free( response );
793 if ( !result ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
795 IXML_NodeList* containerNodeList = ixmlDocument_getElementsByTagName( result, "container" );
796 if ( containerNodeList )
798 for ( unsigned int i = 0; i < ixmlNodeList_length( containerNodeList ); i++ )
800 IXML_Element* containerElement = ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
802 const char* objectID = ixmlElement_getAttribute( containerElement, "id" );
803 if ( !objectID ) continue;
805 const char* childCountStr = ixmlElement_getAttribute( containerElement, "childCount" );
806 if ( !childCountStr ) continue;
807 int childCount = atoi( childCountStr );
809 const char* title = xml_getChildElementValue( containerElement, "dc:title" );
810 if ( !title ) continue;
812 const char* resource = xml_getChildElementValue( containerElement, "res" );
814 if ( resource && childCount < 1 )
816 Item* item = new Item( parent, objectID, title, resource );
817 parent->addItem( item );
821 Container* container = new Container( parent, objectID, title );
822 parent->addContainer( container );
824 if ( childCount > 0 ) _fetchContents( container );
828 ixmlNodeList_free( containerNodeList );
831 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result, "item" );
834 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
836 IXML_Element* itemElement = ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
838 const char* objectID = ixmlElement_getAttribute( itemElement, "id" );
839 if ( !objectID ) continue;
841 const char* title = xml_getChildElementValue( itemElement, "dc:title" );
842 if ( !title ) continue;
844 const char* resource = xml_getChildElementValue( itemElement, "res" );
845 if ( !resource ) continue;
847 Item* item = new Item( parent, objectID, title, resource );
848 parent->addItem( item );
851 ixmlNodeList_free( itemNodeList );
854 ixmlDocument_free( result );
859 void MediaServer::_buildPlaylist( Container* parent )
861 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
863 Container* container = parent->getContainer( i );
864 playlist_item_t* parentNode = parent->getPlaylistNode();
866 char* title = strdup( container->getTitle() );
867 playlist_item_t* node = playlist_NodeCreate( _cookie->serviceDiscovery->p_sys->p_playlist,
872 container->setPlaylistNode( node );
873 _buildPlaylist( container );
876 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
878 Item* item = parent->getItem( i );
879 playlist_item_t* parentNode = parent->getPlaylistNode();
881 playlist_item_t* node = playlist_ItemNew( _cookie->serviceDiscovery,
885 playlist_NodeAddItem( _cookie->serviceDiscovery->p_sys->p_playlist,
887 parentNode, PLAYLIST_APPEND, PLAYLIST_END );
889 item->setPlaylistNode( node );
893 void MediaServer::setPlaylistNode( playlist_item_t* playlistNode )
895 _playlistNode = playlistNode;
898 bool MediaServer::compareSID( const char* sid )
900 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
904 // MediaServerList...
906 MediaServerList::MediaServerList( Cookie* cookie )
911 MediaServerList::~MediaServerList()
913 for ( unsigned int i = 0; i < _list.size(); i++ )
919 bool MediaServerList::addServer( MediaServer* s )
921 if ( getServer( s->getUDN() ) != 0 ) return false;
923 msg_Dbg( _cookie->serviceDiscovery, "Adding server '%s'", s->getFriendlyName() );
925 _list.push_back( s );
927 char* name = strdup( s->getFriendlyName() );
928 playlist_item_t* node = playlist_NodeCreate( _cookie->serviceDiscovery->p_sys->p_playlist,
930 _cookie->serviceDiscovery->p_sys->p_node );
932 s->setPlaylistNode( node );
937 MediaServer* MediaServerList::getServer( const char* UDN )
939 MediaServer* result = 0;
941 for ( unsigned int i = 0; i < _list.size(); i++ )
943 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
953 MediaServer* MediaServerList::getServerBySID( const char* sid )
955 MediaServer* server = 0;
957 for ( unsigned int i = 0; i < _list.size(); i++ )
959 if ( _list[i]->compareSID( sid ) )
969 void MediaServerList::removeServer( const char* UDN )
971 MediaServer* server = getServer( UDN );
972 if ( !server ) return;
974 msg_Dbg( _cookie->serviceDiscovery, "Removing server '%s'", server->getFriendlyName() );
976 std::vector<MediaServer*>::iterator it;
977 for ( it = _list.begin(); it != _list.end(); it++ )
991 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
995 _objectID = objectID;
997 _resource = resource;
1002 const char* Item::getObjectID() const
1004 return _objectID.c_str();
1007 const char* Item::getTitle() const
1009 return _title.c_str();
1012 const char* Item::getResource() const
1014 return _resource.c_str();
1017 void Item::setPlaylistNode( playlist_item_t* node )
1019 _playlistNode = node;
1022 playlist_item_t* Item::getPlaylistNode() const
1024 return _playlistNode;
1030 Container::Container( Container* parent, const char* objectID, const char* title )
1034 _objectID = objectID;
1040 Container::~Container()
1042 for ( unsigned int i = 0; i < _containers.size(); i++ )
1044 delete _containers[i];
1047 for ( unsigned int i = 0; i < _items.size(); i++ )
1053 void Container::addItem( Item* item )
1055 _items.push_back( item );
1058 void Container::addContainer( Container* container )
1060 _containers.push_back( container );
1063 const char* Container::getObjectID() const
1065 return _objectID.c_str();
1068 const char* Container::getTitle() const
1070 return _title.c_str();
1073 unsigned int Container::getNumItems() const
1075 return _items.size();
1078 unsigned int Container::getNumContainers() const
1080 return _containers.size();
1083 Item* Container::getItem( unsigned int i ) const
1085 if ( i < _items.size() ) return _items[i];
1089 Container* Container::getContainer( unsigned int i ) const
1091 if ( i < _containers.size() ) return _containers[i];
1095 void Container::setPlaylistNode( playlist_item_t* node )
1097 _playlistNode = node;
1100 playlist_item_t* Container::getPlaylistNode() const
1102 return _playlistNode;