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>
47 #include "vlc_strings.h"
52 struct services_discovery_sys_t
54 playlist_item_t *p_node_cat;
55 playlist_item_t *p_node_one;
61 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
62 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
68 class MediaServerList;
72 // Cookie that is passed to the callback
76 services_discovery_t* serviceDiscovery;
77 UpnpClient_Handle clientHandle;
78 MediaServerList* serverList;
82 // Class definitions...
90 vlc_mutex_init( c->serviceDiscovery, &_mutex );
95 vlc_mutex_destroy( &_mutex );
98 void lock() { vlc_mutex_lock( &_mutex ); }
99 void unlock() { vlc_mutex_unlock( &_mutex ); }
110 Locker( Lockable* l )
130 static void parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie );
132 MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie );
135 const char* getUDN() const;
136 const char* getFriendlyName() const;
138 void setContentDirectoryEventURL( const char* url );
139 const char* getContentDirectoryEventURL() const;
141 void setContentDirectoryControlURL( const char* url );
142 const char* getContentDirectoryControlURL() const;
144 void subscribeToContentDirectory();
145 void fetchContents();
147 void setPlaylistNode( playlist_item_t* node );
149 bool compareSID( const char* sid );
153 bool _fetchContents( Container* parent );
154 void _buildPlaylist( Container* container );
155 IXML_Document* _browseAction( const char*, const char*, const char*, const char*, const char*, const char* );
159 Container* _contents;
160 playlist_item_t* _playlistNode;
163 std::string _friendlyName;
165 std::string _contentDirectoryEventURL;
166 std::string _contentDirectoryControlURL;
168 int _subscriptionTimeOut;
169 Upnp_SID _subscriptionID;
173 class MediaServerList
177 MediaServerList( Cookie* cookie );
180 bool addServer( MediaServer* s );
181 void removeServer( const char* UDN );
183 MediaServer* getServer( const char* UDN );
184 MediaServer* getServerBySID( const char* );
190 std::vector<MediaServer*> _list;
198 Item( Container* parent, const char* objectID, const char* title, const char* resource );
200 const char* getObjectID() const;
201 const char* getTitle() const;
202 const char* getResource() const;
204 void setPlaylistNode( playlist_item_t* node );
205 playlist_item_t* getPlaylistNode() const ;
209 playlist_item_t* _playlistNode;
212 std::string _objectID;
214 std::string _resource;
222 Container( Container* parent, const char* objectID, const char* title );
225 void addItem( Item* item );
226 void addContainer( Container* container );
228 const char* getObjectID() const;
229 const char* getTitle() const;
231 unsigned int getNumItems() const;
232 unsigned int getNumContainers() const;
234 Item* getItem( unsigned int i ) const;
235 Container* getContainer( unsigned int i ) const;
237 void setPlaylistNode( playlist_item_t* node );
238 playlist_item_t* getPlaylistNode() const;
242 playlist_item_t* _playlistNode;
246 std::string _objectID;
248 std::vector<Item*> _items;
249 std::vector<Container*> _containers;
253 // VLC callback prototypes
255 static int Open( vlc_object_t* );
256 static void Close( vlc_object_t* );
257 static void Run( services_discovery_t *p_sd );
262 set_shortname( "UPnP" );
263 set_description( _( "Universal Plug'n'Play discovery ( Intel SDK )" ) );
264 set_category( CAT_PLAYLIST );
265 set_subcategory( SUBCAT_PLAYLIST_SD );
266 set_capability( "services_discovery", 0 );
267 set_callbacks( Open, Close );
271 // More prototypes...
273 static Lockable* CallbackLock;
274 static int Callback( Upnp_EventType eventType, void* event, void* pCookie );
276 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName );
277 IXML_Document* parseBrowseResult( IXML_Document* doc );
282 static int Open( vlc_object_t *p_this )
284 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
285 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
286 malloc( sizeof( services_discovery_sys_t ) );
291 /* Create our playlist node */
292 playlist_NodesPairCreate( pl_Get( p_sd ), _("Devices"),
293 &p_sys->p_node_cat, &p_sys->p_node_one,
299 static void Close( vlc_object_t *p_this )
301 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
302 services_discovery_sys_t *p_sys = p_sd->p_sys;
304 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_one, VLC_TRUE,
306 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_cat, VLC_TRUE,
312 static void Run( services_discovery_t* p_sd )
316 res = UpnpInit( 0, 0 );
317 if( res != UPNP_E_SUCCESS )
319 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
324 cookie.serviceDiscovery = p_sd;
325 cookie.serverList = new MediaServerList( &cookie );
327 CallbackLock = new Lockable( &cookie );
329 res = UpnpRegisterClient( Callback, &cookie, &cookie.clientHandle );
330 if( res != UPNP_E_SUCCESS )
332 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
336 res = UpnpSearchAsync( cookie.clientHandle, 5, MEDIA_SERVER_DEVICE_TYPE, &cookie );
337 if( res != UPNP_E_SUCCESS )
339 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
343 msg_Dbg( p_sd, "UPnP discovery started" );
344 while( !p_sd->b_die )
349 msg_Dbg( p_sd, "UPnP discovery stopped" );
353 delete cookie.serverList;
358 // XML utility functions:
360 // Returns the value of a child element, or 0 on error
361 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName )
363 if ( !parent ) return 0;
364 if ( !tagName ) return 0;
366 char* s = strdup( tagName );
367 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
369 if ( !nodeList ) return 0;
371 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
372 ixmlNodeList_free( nodeList );
373 if ( !element ) return 0;
375 IXML_Node* textNode = ixmlNode_getFirstChild( element );
376 if ( !textNode ) return 0;
378 return ixmlNode_getNodeValue( textNode );
381 // Extracts the result document from a SOAP response
382 IXML_Document* parseBrowseResult( IXML_Document* doc )
384 if ( !doc ) return 0;
386 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc, "Result" );
387 if ( !resultList ) return 0;
389 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
391 ixmlNodeList_free( resultList );
393 if ( !resultNode ) return 0;
395 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
396 if ( !textNode ) return 0;
398 const char* resultString = ixmlNode_getNodeValue( textNode );
399 char* resultXML = strdup( resultString );
401 resolve_xml_special_chars( resultXML );
403 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
411 // Handles all UPnP events
412 static int Callback( Upnp_EventType eventType, void* event, void* pCookie )
414 Locker locker( CallbackLock );
416 Cookie* cookie = ( Cookie* )pCookie;
418 switch( eventType ) {
420 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
421 case UPNP_DISCOVERY_SEARCH_RESULT:
423 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
425 IXML_Document *descriptionDoc = 0;
428 res = UpnpDownloadXmlDoc( discovery->Location, &descriptionDoc );
429 if ( res != UPNP_E_SUCCESS )
431 msg_Dbg( cookie->serviceDiscovery, "%s:%d: Could not download device description!", __FILE__, __LINE__ );
435 MediaServer::parseDeviceDescription( descriptionDoc, discovery->Location, cookie );
437 ixmlDocument_free( descriptionDoc );
441 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
443 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
445 cookie->serverList->removeServer( discovery->DeviceId );
449 case UPNP_EVENT_RECEIVED:
451 Upnp_Event* e = ( Upnp_Event* )event;
453 MediaServer* server = cookie->serverList->getServerBySID( e->Sid );
454 if ( server ) server->fetchContents();
458 case UPNP_EVENT_AUTORENEWAL_FAILED:
459 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
463 Upnp_Event_Subscribe* s = ( Upnp_Event_Subscribe* )event;
465 MediaServer* server = cookie->serverList->getServerBySID( s->Sid );
466 if ( server ) server->subscribeToContentDirectory();
470 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
471 msg_Warn( cookie->serviceDiscovery, "subscription complete" );
474 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
475 msg_Warn( cookie->serviceDiscovery, "search timeout" );
479 msg_Dbg( cookie->serviceDiscovery, "%s:%d: DEBUG: UNHANDLED EVENT ( TYPE=%d )", __FILE__, __LINE__, eventType );
483 return UPNP_E_SUCCESS;
487 // Class implementations...
491 void MediaServer::parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie )
493 if ( !doc ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
494 if ( !location ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
496 const char* baseURL = location;
498 // Try to extract baseURL
500 IXML_NodeList* urlList = ixmlDocument_getElementsByTagName( doc, "baseURL" );
503 if ( IXML_Node* urlNode = ixmlNodeList_item( urlList, 0 ) )
505 IXML_Node* textNode = ixmlNode_getFirstChild( urlNode );
506 if ( textNode ) baseURL = ixmlNode_getNodeValue( textNode );
509 ixmlNodeList_free( urlList );
514 IXML_NodeList* deviceList = ixmlDocument_getElementsByTagName( doc, "device" );
518 for ( unsigned int i = 0; i < ixmlNodeList_length( deviceList ); i++ )
520 IXML_Element* deviceElement = ( IXML_Element* )ixmlNodeList_item( deviceList, i );
522 const char* deviceType = xml_getChildElementValue( deviceElement, "deviceType" );
523 if ( !deviceType ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no deviceType!", __FILE__, __LINE__ ); continue; }
524 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, deviceType ) != 0 ) continue;
526 const char* UDN = xml_getChildElementValue( deviceElement, "UDN" );
527 if ( !UDN ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no UDN!", __FILE__, __LINE__ ); continue; }
528 if ( cookie->serverList->getServer( UDN ) != 0 ) continue;
530 const char* friendlyName = xml_getChildElementValue( deviceElement, "friendlyName" );
531 if ( !friendlyName ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no friendlyName!", __FILE__, __LINE__ ); continue; }
533 MediaServer* server = new MediaServer( UDN, friendlyName, cookie );
534 if ( !cookie->serverList->addServer( server ) ) {
541 // Check for ContentDirectory service...
543 IXML_NodeList* serviceList = ixmlElement_getElementsByTagName( deviceElement, "service" );
546 for ( unsigned int j = 0; j < ixmlNodeList_length( serviceList ); j++ )
548 IXML_Element* serviceElement = ( IXML_Element* )ixmlNodeList_item( serviceList, j );
550 const char* serviceType = xml_getChildElementValue( serviceElement, "serviceType" );
551 if ( !serviceType ) continue;
552 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE, serviceType ) != 0 ) continue;
554 const char* eventSubURL = xml_getChildElementValue( serviceElement, "eventSubURL" );
555 if ( !eventSubURL ) continue;
557 const char* controlURL = xml_getChildElementValue( serviceElement, "controlURL" );
558 if ( !controlURL ) continue;
560 // Try to subscribe to ContentDirectory service
562 char* url = ( char* )malloc( strlen( baseURL ) + strlen( eventSubURL ) + 1 );
565 char* s1 = strdup( baseURL );
566 char* s2 = strdup( eventSubURL );
568 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
570 // msg_Dbg( cookie->serviceDiscovery, "CDS EVENT URL: %s", url );
572 server->setContentDirectoryEventURL( url );
573 server->subscribeToContentDirectory();
581 // Try to browse content directory...
583 url = ( char* )malloc( strlen( baseURL ) + strlen( controlURL ) + 1 );
586 char* s1 = strdup( baseURL );
587 char* s2 = strdup( controlURL );
589 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
591 // msg_Dbg( cookie->serviceDiscovery, "CDS CTRL URL: %s", url );
593 server->setContentDirectoryControlURL( url );
594 server->fetchContents();
603 ixmlNodeList_free( serviceList );
607 ixmlNodeList_free( deviceList );
611 MediaServer::MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie )
616 _friendlyName = friendlyName;
622 MediaServer::~MediaServer()
626 playlist_NodeDelete( pl_Get( _cookie->serviceDiscovery ) ,
627 _playlistNode, VLC_TRUE, VLC_TRUE );
633 const char* MediaServer::getUDN() const
635 const char* s = _UDN.c_str();
639 const char* MediaServer::getFriendlyName() const
641 const char* s = _friendlyName.c_str();
645 void MediaServer::setContentDirectoryEventURL( const char* url )
647 _contentDirectoryEventURL = url;
650 const char* MediaServer::getContentDirectoryEventURL() const
652 const char* s = _contentDirectoryEventURL.c_str();
656 void MediaServer::setContentDirectoryControlURL( const char* url )
658 _contentDirectoryControlURL = url;
661 const char* MediaServer::getContentDirectoryControlURL() const
663 return _contentDirectoryControlURL.c_str();
666 void MediaServer::subscribeToContentDirectory()
668 const char* url = getContentDirectoryEventURL();
669 if ( !url || strcmp( url, "" ) == 0 )
671 msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" );
678 int res = UpnpSubscribe( _cookie->clientHandle, url, &timeOut, sid );
680 if ( res == UPNP_E_SUCCESS )
682 _subscriptionTimeOut = timeOut;
683 memcpy( _subscriptionID, sid, sizeof( Upnp_SID ) );
687 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: WARNING: '%s': %s", __FILE__, __LINE__, getFriendlyName(), UpnpGetErrorMessage( res ) );
691 IXML_Document* MediaServer::_browseAction( const char* pObjectID, const char* pBrowseFlag, const char* pFilter,
692 const char* pStartingIndex, const char* pRequestedCount, const char* pSortCriteria )
694 IXML_Document* action = 0;
695 IXML_Document* response = 0;
697 const char* url = getContentDirectoryControlURL();
698 if ( !url || strcmp( url, "" ) == 0 ) { msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" ); return 0; }
700 char* ObjectID = strdup( pObjectID );
701 char* BrowseFlag = strdup( pBrowseFlag );
702 char* Filter = strdup( pFilter );
703 char* StartingIndex = strdup( pStartingIndex );
704 char* RequestedCount = strdup( pRequestedCount );
705 char* SortCriteria = strdup( pSortCriteria );
707 char* serviceType = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
711 res = UpnpAddToAction( &action, "Browse", serviceType, "ObjectID", ObjectID );
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, "BrowseFlag", BrowseFlag );
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, "Filter", Filter );
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, "StartingIndex", StartingIndex );
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, "RequestedCount", RequestedCount );
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, "SortCriteria", SortCriteria );
727 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
729 res = UpnpSendAction( _cookie->clientHandle,
731 CONTENT_DIRECTORY_SERVICE_TYPE,
735 if ( res != UPNP_E_SUCCESS )
737 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) );
738 ixmlDocument_free( response );
747 free( StartingIndex );
748 free( RequestedCount );
749 free( SortCriteria );
753 ixmlDocument_free( action );
757 void MediaServer::fetchContents()
759 Container* root = new Container( 0, "0", getFriendlyName() );
760 playlist_t * p_playlist = pl_Get( _cookie->serviceDiscovery );
761 _fetchContents( root );
766 playlist_NodeEmpty( p_playlist, _playlistNode, VLC_TRUE );
772 _contents->setPlaylistNode( _playlistNode );
774 _buildPlaylist( _contents );
777 bool MediaServer::_fetchContents( Container* parent )
779 if (!parent) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: parent==NULL", __FILE__, __LINE__ ); return false; }
781 IXML_Document* response = _browseAction( parent->getObjectID(), "BrowseDirectChildren", "*", "0", "0", "" );
782 if ( !response ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
784 IXML_Document* result = parseBrowseResult( response );
785 ixmlDocument_free( response );
786 if ( !result ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
788 IXML_NodeList* containerNodeList = ixmlDocument_getElementsByTagName( result, "container" );
789 if ( containerNodeList )
791 for ( unsigned int i = 0; i < ixmlNodeList_length( containerNodeList ); i++ )
793 IXML_Element* containerElement = ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
795 const char* objectID = ixmlElement_getAttribute( containerElement, "id" );
796 if ( !objectID ) continue;
798 const char* childCountStr = ixmlElement_getAttribute( containerElement, "childCount" );
799 if ( !childCountStr ) continue;
800 int childCount = atoi( childCountStr );
802 const char* title = xml_getChildElementValue( containerElement, "dc:title" );
803 if ( !title ) continue;
805 const char* resource = xml_getChildElementValue( containerElement, "res" );
807 if ( resource && childCount < 1 )
809 Item* item = new Item( parent, objectID, title, resource );
810 parent->addItem( item );
814 Container* container = new Container( parent, objectID, title );
815 parent->addContainer( container );
817 if ( childCount > 0 ) _fetchContents( container );
821 ixmlNodeList_free( containerNodeList );
824 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result, "item" );
827 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
829 IXML_Element* itemElement = ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
831 const char* objectID = ixmlElement_getAttribute( itemElement, "id" );
832 if ( !objectID ) continue;
834 const char* title = xml_getChildElementValue( itemElement, "dc:title" );
835 if ( !title ) continue;
837 const char* resource = xml_getChildElementValue( itemElement, "res" );
838 if ( !resource ) continue;
840 Item* item = new Item( parent, objectID, title, resource );
841 parent->addItem( item );
844 ixmlNodeList_free( itemNodeList );
847 ixmlDocument_free( result );
852 void MediaServer::_buildPlaylist( Container* parent )
854 playlist_t *p_playlist = pl_Get( _cookie->serviceDiscovery );
855 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
857 Container* container = parent->getContainer( i );
858 playlist_item_t* parentNode = parent->getPlaylistNode();
860 char* title = strdup( container->getTitle() );
861 playlist_item_t* node = playlist_NodeCreate( p_playlist, title, parentNode, 0, NULL );
864 container->setPlaylistNode( node );
865 _buildPlaylist( container );
868 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
870 Item* item = parent->getItem( i );
871 playlist_item_t* parentNode = parent->getPlaylistNode();
873 input_item_t* p_input = input_ItemNew( _cookie->serviceDiscovery,
877 playlist_BothAddInput( p_playlist, p_input, parentNode,
878 PLAYLIST_APPEND, PLAYLIST_END, &i_cat, NULL,
880 /* TODO: do this better by storing ids */
881 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, i_cat, VLC_FALSE );
883 item->setPlaylistNode( p_node );
887 void MediaServer::setPlaylistNode( playlist_item_t* playlistNode )
889 _playlistNode = playlistNode;
892 bool MediaServer::compareSID( const char* sid )
894 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
898 // MediaServerList...
900 MediaServerList::MediaServerList( Cookie* cookie )
905 MediaServerList::~MediaServerList()
907 for ( unsigned int i = 0; i < _list.size(); i++ )
913 bool MediaServerList::addServer( MediaServer* s )
915 if ( getServer( s->getUDN() ) != 0 ) return false;
917 msg_Dbg( _cookie->serviceDiscovery, "Adding server '%s'", s->getFriendlyName() );
919 _list.push_back( s );
921 char* name = strdup( s->getFriendlyName() );
922 playlist_item_t* node = playlist_NodeCreate( pl_Get( _cookie->serviceDiscovery ),
924 _cookie->serviceDiscovery->p_sys->p_node_cat, 0, NULL );
926 s->setPlaylistNode( node );
931 MediaServer* MediaServerList::getServer( const char* UDN )
933 MediaServer* result = 0;
935 for ( unsigned int i = 0; i < _list.size(); i++ )
937 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
947 MediaServer* MediaServerList::getServerBySID( const char* sid )
949 MediaServer* server = 0;
951 for ( unsigned int i = 0; i < _list.size(); i++ )
953 if ( _list[i]->compareSID( sid ) )
963 void MediaServerList::removeServer( const char* UDN )
965 MediaServer* server = getServer( UDN );
966 if ( !server ) return;
968 msg_Dbg( _cookie->serviceDiscovery, "Removing server '%s'", server->getFriendlyName() );
970 std::vector<MediaServer*>::iterator it;
971 for ( it = _list.begin(); it != _list.end(); it++ )
985 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
989 _objectID = objectID;
991 _resource = resource;
996 const char* Item::getObjectID() const
998 return _objectID.c_str();
1001 const char* Item::getTitle() const
1003 return _title.c_str();
1006 const char* Item::getResource() const
1008 return _resource.c_str();
1011 void Item::setPlaylistNode( playlist_item_t* node )
1013 _playlistNode = node;
1016 playlist_item_t* Item::getPlaylistNode() const
1018 return _playlistNode;
1024 Container::Container( Container* parent, const char* objectID, const char* title )
1028 _objectID = objectID;
1034 Container::~Container()
1036 for ( unsigned int i = 0; i < _containers.size(); i++ )
1038 delete _containers[i];
1041 for ( unsigned int i = 0; i < _items.size(); i++ )
1047 void Container::addItem( Item* item )
1049 _items.push_back( item );
1052 void Container::addContainer( Container* container )
1054 _containers.push_back( container );
1057 const char* Container::getObjectID() const
1059 return _objectID.c_str();
1062 const char* Container::getTitle() const
1064 return _title.c_str();
1067 unsigned int Container::getNumItems() const
1069 return _items.size();
1072 unsigned int Container::getNumContainers() const
1074 return _containers.size();
1077 Item* Container::getItem( unsigned int i ) const
1079 if ( i < _items.size() ) return _items[i];
1083 Container* Container::getContainer( unsigned int i ) const
1085 if ( i < _containers.size() ) return _containers[i];
1089 void Container::setPlaylistNode( playlist_item_t* node )
1091 _playlistNode = node;
1094 playlist_item_t* Container::getPlaylistNode() const
1096 return _playlistNode;