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 ???
30 \TODO: Rewrite this using the new service discovery API (see sap.c, shout.c).
37 #include <upnp/upnp.h>
38 #include <upnp/upnptools.h>
46 #include <vlc_playlist.h>
51 struct services_discovery_sys_t
53 playlist_item_t *p_node_cat;
54 playlist_item_t *p_node_one;
60 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
61 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
67 class MediaServerList;
71 // Cookie that is passed to the callback
75 services_discovery_t* serviceDiscovery;
76 UpnpClient_Handle clientHandle;
77 MediaServerList* serverList;
81 // Class definitions...
89 vlc_mutex_init( c->serviceDiscovery, &_mutex );
94 vlc_mutex_destroy( &_mutex );
97 void lock() { vlc_mutex_lock( &_mutex ); }
98 void unlock() { vlc_mutex_unlock( &_mutex ); }
109 Locker( Lockable* l )
129 static void parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie );
131 MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie );
134 const char* getUDN() const;
135 const char* getFriendlyName() const;
137 void setContentDirectoryEventURL( const char* url );
138 const char* getContentDirectoryEventURL() const;
140 void setContentDirectoryControlURL( const char* url );
141 const char* getContentDirectoryControlURL() const;
143 void subscribeToContentDirectory();
144 void fetchContents();
146 void setPlaylistNode( playlist_item_t* node );
148 bool compareSID( const char* sid );
152 bool _fetchContents( Container* parent );
153 void _buildPlaylist( Container* container );
154 IXML_Document* _browseAction( const char*, const char*, const char*, const char*, const char*, const char* );
158 Container* _contents;
159 playlist_item_t* _playlistNode;
162 std::string _friendlyName;
164 std::string _contentDirectoryEventURL;
165 std::string _contentDirectoryControlURL;
167 int _subscriptionTimeOut;
168 Upnp_SID _subscriptionID;
172 class MediaServerList
176 MediaServerList( Cookie* cookie );
179 bool addServer( MediaServer* s );
180 void removeServer( const char* UDN );
182 MediaServer* getServer( const char* UDN );
183 MediaServer* getServerBySID( const char* );
189 std::vector<MediaServer*> _list;
197 Item( Container* parent, const char* objectID, const char* title, const char* resource );
199 const char* getObjectID() const;
200 const char* getTitle() const;
201 const char* getResource() const;
203 void setPlaylistNode( playlist_item_t* node );
204 playlist_item_t* getPlaylistNode() const ;
208 playlist_item_t* _playlistNode;
211 std::string _objectID;
213 std::string _resource;
221 Container( Container* parent, const char* objectID, const char* title );
224 void addItem( Item* item );
225 void addContainer( Container* container );
227 const char* getObjectID() const;
228 const char* getTitle() const;
230 unsigned int getNumItems() const;
231 unsigned int getNumContainers() const;
233 Item* getItem( unsigned int i ) const;
234 Container* getContainer( unsigned int i ) const;
236 void setPlaylistNode( playlist_item_t* node );
237 playlist_item_t* getPlaylistNode() const;
241 playlist_item_t* _playlistNode;
245 std::string _objectID;
247 std::vector<Item*> _items;
248 std::vector<Container*> _containers;
252 // VLC callback prototypes
254 static int Open( vlc_object_t* );
255 static void Close( vlc_object_t* );
256 static void Run( services_discovery_t *p_sd );
261 set_shortname( "UPnP" );
262 set_description( _( "Universal Plug'n'Play discovery ( Intel SDK )" ) );
263 set_category( CAT_PLAYLIST );
264 set_subcategory( SUBCAT_PLAYLIST_SD );
265 set_capability( "services_discovery", 0 );
266 set_callbacks( Open, Close );
270 // More prototypes...
272 static Lockable* CallbackLock;
273 static int Callback( Upnp_EventType eventType, void* event, void* pCookie );
275 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName );
276 IXML_Document* parseBrowseResult( IXML_Document* doc );
281 static int Open( vlc_object_t *p_this )
283 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
284 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
285 malloc( sizeof( services_discovery_sys_t ) );
290 /* Create our playlist node */
291 playlist_NodesPairCreate( pl_Get( p_sd ), _("Devices"),
292 &p_sys->p_node_cat, &p_sys->p_node_one,
298 static void Close( vlc_object_t *p_this )
300 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
301 services_discovery_sys_t *p_sys = p_sd->p_sys;
303 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_one, VLC_TRUE,
305 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_cat, VLC_TRUE,
311 static void Run( services_discovery_t* p_sd )
315 res = UpnpInit( 0, 0 );
316 if( res != UPNP_E_SUCCESS )
318 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
323 cookie.serviceDiscovery = p_sd;
324 cookie.serverList = new MediaServerList( &cookie );
326 CallbackLock = new Lockable( &cookie );
328 res = UpnpRegisterClient( Callback, &cookie, &cookie.clientHandle );
329 if( res != UPNP_E_SUCCESS )
331 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
335 res = UpnpSearchAsync( cookie.clientHandle, 5, MEDIA_SERVER_DEVICE_TYPE, &cookie );
336 if( res != UPNP_E_SUCCESS )
338 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
342 msg_Dbg( p_sd, "UPnP discovery started" );
343 while( !p_sd->b_die )
348 msg_Dbg( p_sd, "UPnP discovery stopped" );
352 delete cookie.serverList;
357 // XML utility functions:
359 // Returns the value of a child element, or 0 on error
360 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName )
362 if ( !parent ) return 0;
363 if ( !tagName ) return 0;
365 char* s = strdup( tagName );
366 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
368 if ( !nodeList ) return 0;
370 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
371 ixmlNodeList_free( nodeList );
372 if ( !element ) return 0;
374 IXML_Node* textNode = ixmlNode_getFirstChild( element );
375 if ( !textNode ) return 0;
377 return ixmlNode_getNodeValue( textNode );
380 // Extracts the result document from a SOAP response
381 IXML_Document* parseBrowseResult( IXML_Document* doc )
383 if ( !doc ) return 0;
385 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc, "Result" );
386 if ( !resultList ) return 0;
388 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
390 ixmlNodeList_free( resultList );
392 if ( !resultNode ) return 0;
394 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
395 if ( !textNode ) return 0;
397 const char* resultString = ixmlNode_getNodeValue( textNode );
398 char* resultXML = strdup( resultString );
400 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
408 // Handles all UPnP events
409 static int Callback( Upnp_EventType eventType, void* event, void* pCookie )
411 Locker locker( CallbackLock );
413 Cookie* cookie = ( Cookie* )pCookie;
415 switch( eventType ) {
417 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
418 case UPNP_DISCOVERY_SEARCH_RESULT:
420 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
422 IXML_Document *descriptionDoc = 0;
425 res = UpnpDownloadXmlDoc( discovery->Location, &descriptionDoc );
426 if ( res != UPNP_E_SUCCESS )
428 msg_Dbg( cookie->serviceDiscovery, "%s:%d: Could not download device description!", __FILE__, __LINE__ );
432 MediaServer::parseDeviceDescription( descriptionDoc, discovery->Location, cookie );
434 ixmlDocument_free( descriptionDoc );
438 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
440 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
442 cookie->serverList->removeServer( discovery->DeviceId );
446 case UPNP_EVENT_RECEIVED:
448 Upnp_Event* e = ( Upnp_Event* )event;
450 MediaServer* server = cookie->serverList->getServerBySID( e->Sid );
451 if ( server ) server->fetchContents();
455 case UPNP_EVENT_AUTORENEWAL_FAILED:
456 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
460 Upnp_Event_Subscribe* s = ( Upnp_Event_Subscribe* )event;
462 MediaServer* server = cookie->serverList->getServerBySID( s->Sid );
463 if ( server ) server->subscribeToContentDirectory();
467 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
468 msg_Warn( cookie->serviceDiscovery, "subscription complete" );
471 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
472 msg_Warn( cookie->serviceDiscovery, "search timeout" );
476 msg_Dbg( cookie->serviceDiscovery, "%s:%d: DEBUG: UNHANDLED EVENT ( TYPE=%d )", __FILE__, __LINE__, eventType );
480 return UPNP_E_SUCCESS;
484 // Class implementations...
488 void MediaServer::parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie )
490 if ( !doc ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
491 if ( !location ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
493 const char* baseURL = location;
495 // Try to extract baseURL
497 IXML_NodeList* urlList = ixmlDocument_getElementsByTagName( doc, "baseURL" );
500 if ( IXML_Node* urlNode = ixmlNodeList_item( urlList, 0 ) )
502 IXML_Node* textNode = ixmlNode_getFirstChild( urlNode );
503 if ( textNode ) baseURL = ixmlNode_getNodeValue( textNode );
506 ixmlNodeList_free( urlList );
511 IXML_NodeList* deviceList = ixmlDocument_getElementsByTagName( doc, "device" );
515 for ( unsigned int i = 0; i < ixmlNodeList_length( deviceList ); i++ )
517 IXML_Element* deviceElement = ( IXML_Element* )ixmlNodeList_item( deviceList, i );
519 const char* deviceType = xml_getChildElementValue( deviceElement, "deviceType" );
520 if ( !deviceType ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no deviceType!", __FILE__, __LINE__ ); continue; }
521 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, deviceType ) != 0 ) continue;
523 const char* UDN = xml_getChildElementValue( deviceElement, "UDN" );
524 if ( !UDN ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no UDN!", __FILE__, __LINE__ ); continue; }
525 if ( cookie->serverList->getServer( UDN ) != 0 ) continue;
527 const char* friendlyName = xml_getChildElementValue( deviceElement, "friendlyName" );
528 if ( !friendlyName ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no friendlyName!", __FILE__, __LINE__ ); continue; }
530 MediaServer* server = new MediaServer( UDN, friendlyName, cookie );
531 if ( !cookie->serverList->addServer( server ) ) {
538 // Check for ContentDirectory service...
540 IXML_NodeList* serviceList = ixmlElement_getElementsByTagName( deviceElement, "service" );
543 for ( unsigned int j = 0; j < ixmlNodeList_length( serviceList ); j++ )
545 IXML_Element* serviceElement = ( IXML_Element* )ixmlNodeList_item( serviceList, j );
547 const char* serviceType = xml_getChildElementValue( serviceElement, "serviceType" );
548 if ( !serviceType ) continue;
549 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE, serviceType ) != 0 ) continue;
551 const char* eventSubURL = xml_getChildElementValue( serviceElement, "eventSubURL" );
552 if ( !eventSubURL ) continue;
554 const char* controlURL = xml_getChildElementValue( serviceElement, "controlURL" );
555 if ( !controlURL ) continue;
557 // Try to subscribe to ContentDirectory service
559 char* url = ( char* )malloc( strlen( baseURL ) + strlen( eventSubURL ) + 1 );
562 char* s1 = strdup( baseURL );
563 char* s2 = strdup( eventSubURL );
565 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
567 // msg_Dbg( cookie->serviceDiscovery, "CDS EVENT URL: %s", url );
569 server->setContentDirectoryEventURL( url );
570 server->subscribeToContentDirectory();
578 // Try to browse content directory...
580 url = ( char* )malloc( strlen( baseURL ) + strlen( controlURL ) + 1 );
583 char* s1 = strdup( baseURL );
584 char* s2 = strdup( controlURL );
586 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
588 // msg_Dbg( cookie->serviceDiscovery, "CDS CTRL URL: %s", url );
590 server->setContentDirectoryControlURL( url );
591 server->fetchContents();
600 ixmlNodeList_free( serviceList );
604 ixmlNodeList_free( deviceList );
608 MediaServer::MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie )
613 _friendlyName = friendlyName;
619 MediaServer::~MediaServer()
623 playlist_NodeDelete( pl_Get( _cookie->serviceDiscovery ) ,
624 _playlistNode, VLC_TRUE, VLC_TRUE );
630 const char* MediaServer::getUDN() const
632 const char* s = _UDN.c_str();
636 const char* MediaServer::getFriendlyName() const
638 const char* s = _friendlyName.c_str();
642 void MediaServer::setContentDirectoryEventURL( const char* url )
644 _contentDirectoryEventURL = url;
647 const char* MediaServer::getContentDirectoryEventURL() const
649 const char* s = _contentDirectoryEventURL.c_str();
653 void MediaServer::setContentDirectoryControlURL( const char* url )
655 _contentDirectoryControlURL = url;
658 const char* MediaServer::getContentDirectoryControlURL() const
660 return _contentDirectoryControlURL.c_str();
663 void MediaServer::subscribeToContentDirectory()
665 const char* url = getContentDirectoryEventURL();
666 if ( !url || strcmp( url, "" ) == 0 )
668 msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" );
675 int res = UpnpSubscribe( _cookie->clientHandle, url, &timeOut, sid );
677 if ( res == UPNP_E_SUCCESS )
679 _subscriptionTimeOut = timeOut;
680 memcpy( _subscriptionID, sid, sizeof( Upnp_SID ) );
684 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: WARNING: '%s': %s", __FILE__, __LINE__, getFriendlyName(), UpnpGetErrorMessage( res ) );
688 IXML_Document* MediaServer::_browseAction( const char* pObjectID, const char* pBrowseFlag, const char* pFilter,
689 const char* pStartingIndex, const char* pRequestedCount, const char* pSortCriteria )
691 IXML_Document* action = 0;
692 IXML_Document* response = 0;
694 const char* url = getContentDirectoryControlURL();
695 if ( !url || strcmp( url, "" ) == 0 ) { msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" ); return 0; }
697 char* ObjectID = strdup( pObjectID );
698 char* BrowseFlag = strdup( pBrowseFlag );
699 char* Filter = strdup( pFilter );
700 char* StartingIndex = strdup( pStartingIndex );
701 char* RequestedCount = strdup( pRequestedCount );
702 char* SortCriteria = strdup( pSortCriteria );
704 char* serviceType = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
708 res = UpnpAddToAction( &action, "Browse", serviceType, "ObjectID", ObjectID );
709 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
711 res = UpnpAddToAction( &action, "Browse", serviceType, "BrowseFlag", BrowseFlag );
712 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
714 res = UpnpAddToAction( &action, "Browse", serviceType, "Filter", Filter );
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, "StartingIndex", StartingIndex );
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, "RequestedCount", RequestedCount );
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, "SortCriteria", SortCriteria );
724 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
726 res = UpnpSendAction( _cookie->clientHandle,
728 CONTENT_DIRECTORY_SERVICE_TYPE,
732 if ( res != UPNP_E_SUCCESS )
734 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) );
735 ixmlDocument_free( response );
744 free( StartingIndex );
745 free( RequestedCount );
746 free( SortCriteria );
750 ixmlDocument_free( action );
754 void MediaServer::fetchContents()
756 Container* root = new Container( 0, "0", getFriendlyName() );
757 playlist_t * p_playlist = pl_Get( _cookie->serviceDiscovery );
758 _fetchContents( root );
763 playlist_NodeEmpty( p_playlist, _playlistNode, VLC_TRUE );
769 _contents->setPlaylistNode( _playlistNode );
771 _buildPlaylist( _contents );
774 bool MediaServer::_fetchContents( Container* parent )
776 if (!parent) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: parent==NULL", __FILE__, __LINE__ ); return false; }
778 IXML_Document* response = _browseAction( parent->getObjectID(), "BrowseDirectChildren", "*", "0", "0", "" );
779 if ( !response ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
781 IXML_Document* result = parseBrowseResult( response );
782 ixmlDocument_free( response );
783 if ( !result ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
785 IXML_NodeList* containerNodeList = ixmlDocument_getElementsByTagName( result, "container" );
786 if ( containerNodeList )
788 for ( unsigned int i = 0; i < ixmlNodeList_length( containerNodeList ); i++ )
790 IXML_Element* containerElement = ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
792 const char* objectID = ixmlElement_getAttribute( containerElement, "id" );
793 if ( !objectID ) continue;
795 const char* childCountStr = ixmlElement_getAttribute( containerElement, "childCount" );
796 if ( !childCountStr ) continue;
797 int childCount = atoi( childCountStr );
799 const char* title = xml_getChildElementValue( containerElement, "dc:title" );
800 if ( !title ) continue;
802 const char* resource = xml_getChildElementValue( containerElement, "res" );
804 if ( resource && childCount < 1 )
806 Item* item = new Item( parent, objectID, title, resource );
807 parent->addItem( item );
811 Container* container = new Container( parent, objectID, title );
812 parent->addContainer( container );
814 if ( childCount > 0 ) _fetchContents( container );
818 ixmlNodeList_free( containerNodeList );
821 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result, "item" );
824 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
826 IXML_Element* itemElement = ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
828 const char* objectID = ixmlElement_getAttribute( itemElement, "id" );
829 if ( !objectID ) continue;
831 const char* title = xml_getChildElementValue( itemElement, "dc:title" );
832 if ( !title ) continue;
834 const char* resource = xml_getChildElementValue( itemElement, "res" );
835 if ( !resource ) continue;
837 Item* item = new Item( parent, objectID, title, resource );
838 parent->addItem( item );
841 ixmlNodeList_free( itemNodeList );
844 ixmlDocument_free( result );
849 void MediaServer::_buildPlaylist( Container* parent )
851 playlist_t *p_playlist = pl_Get( _cookie->serviceDiscovery );
852 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
854 Container* container = parent->getContainer( i );
855 playlist_item_t* parentNode = parent->getPlaylistNode();
857 char* title = strdup( container->getTitle() );
858 playlist_item_t* node = playlist_NodeCreate( p_playlist, title, parentNode, 0, NULL );
861 container->setPlaylistNode( node );
862 _buildPlaylist( container );
865 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
867 Item* item = parent->getItem( i );
868 playlist_item_t* parentNode = parent->getPlaylistNode();
870 input_item_t* p_input = input_ItemNew( _cookie->serviceDiscovery,
874 /* FIXME: playlist_AddInput() can fail */
875 playlist_BothAddInput( p_playlist, p_input, parentNode,
876 PLAYLIST_APPEND, PLAYLIST_END, &i_cat, NULL,
878 /* TODO: do this better by storing ids */
879 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, i_cat, VLC_FALSE );
881 item->setPlaylistNode( p_node );
885 void MediaServer::setPlaylistNode( playlist_item_t* playlistNode )
887 _playlistNode = playlistNode;
890 bool MediaServer::compareSID( const char* sid )
892 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
896 // MediaServerList...
898 MediaServerList::MediaServerList( Cookie* cookie )
903 MediaServerList::~MediaServerList()
905 for ( unsigned int i = 0; i < _list.size(); i++ )
911 bool MediaServerList::addServer( MediaServer* s )
913 if ( getServer( s->getUDN() ) != 0 ) return false;
915 msg_Dbg( _cookie->serviceDiscovery, "Adding server '%s'", s->getFriendlyName() );
917 _list.push_back( s );
919 char* name = strdup( s->getFriendlyName() );
920 playlist_item_t* node = playlist_NodeCreate( pl_Get( _cookie->serviceDiscovery ),
922 _cookie->serviceDiscovery->p_sys->p_node_cat, 0, NULL );
924 s->setPlaylistNode( node );
929 MediaServer* MediaServerList::getServer( const char* UDN )
931 MediaServer* result = 0;
933 for ( unsigned int i = 0; i < _list.size(); i++ )
935 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
945 MediaServer* MediaServerList::getServerBySID( const char* sid )
947 MediaServer* server = 0;
949 for ( unsigned int i = 0; i < _list.size(); i++ )
951 if ( _list[i]->compareSID( sid ) )
961 void MediaServerList::removeServer( const char* UDN )
963 MediaServer* server = getServer( UDN );
964 if ( !server ) return;
966 msg_Dbg( _cookie->serviceDiscovery, "Removing server '%s'", server->getFriendlyName() );
968 std::vector<MediaServer*>::iterator it;
969 for ( it = _list.begin(); it != _list.end(); it++ )
983 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
987 _objectID = objectID;
989 _resource = resource;
994 const char* Item::getObjectID() const
996 return _objectID.c_str();
999 const char* Item::getTitle() const
1001 return _title.c_str();
1004 const char* Item::getResource() const
1006 return _resource.c_str();
1009 void Item::setPlaylistNode( playlist_item_t* node )
1011 _playlistNode = node;
1014 playlist_item_t* Item::getPlaylistNode() const
1016 return _playlistNode;
1022 Container::Container( Container* parent, const char* objectID, const char* title )
1026 _objectID = objectID;
1032 Container::~Container()
1034 for ( unsigned int i = 0; i < _containers.size(); i++ )
1036 delete _containers[i];
1039 for ( unsigned int i = 0; i < _items.size(); i++ )
1045 void Container::addItem( Item* item )
1047 _items.push_back( item );
1050 void Container::addContainer( Container* container )
1052 _containers.push_back( container );
1055 const char* Container::getObjectID() const
1057 return _objectID.c_str();
1060 const char* Container::getTitle() const
1062 return _title.c_str();
1065 unsigned int Container::getNumItems() const
1067 return _items.size();
1070 unsigned int Container::getNumContainers() const
1072 return _containers.size();
1075 Item* Container::getItem( unsigned int i ) const
1077 if ( i < _items.size() ) return _items[i];
1081 Container* Container::getContainer( unsigned int i ) const
1083 if ( i < _containers.size() ) return _containers[i];
1087 void Container::setPlaylistNode( playlist_item_t* node )
1089 _playlistNode = node;
1092 playlist_item_t* Container::getPlaylistNode() const
1094 return _playlistNode;