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>
42 #include <vlc_playlist.h>
43 #include "vlc_strings.h"
48 struct services_discovery_sys_t
50 playlist_item_t *p_node_cat;
51 playlist_item_t *p_node_one;
57 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
58 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
64 class MediaServerList;
68 // Cookie that is passed to the callback
72 services_discovery_t* serviceDiscovery;
73 UpnpClient_Handle clientHandle;
74 MediaServerList* serverList;
78 // Class definitions...
86 vlc_mutex_init( c->serviceDiscovery, &_mutex );
91 vlc_mutex_destroy( &_mutex );
94 void lock() { vlc_mutex_lock( &_mutex ); }
95 void unlock() { vlc_mutex_unlock( &_mutex ); }
106 Locker( Lockable* l )
126 static void parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie );
128 MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie );
131 const char* getUDN() const;
132 const char* getFriendlyName() const;
134 void setContentDirectoryEventURL( const char* url );
135 const char* getContentDirectoryEventURL() const;
137 void setContentDirectoryControlURL( const char* url );
138 const char* getContentDirectoryControlURL() const;
140 void subscribeToContentDirectory();
141 void fetchContents();
143 void setPlaylistNode( playlist_item_t* node );
145 bool compareSID( const char* sid );
149 bool _fetchContents( Container* parent );
150 void _buildPlaylist( Container* container );
151 IXML_Document* _browseAction( const char*, const char*, const char*, const char*, const char*, const char* );
155 Container* _contents;
156 playlist_item_t* _playlistNode;
159 std::string _friendlyName;
161 std::string _contentDirectoryEventURL;
162 std::string _contentDirectoryControlURL;
164 int _subscriptionTimeOut;
165 Upnp_SID _subscriptionID;
169 class MediaServerList
173 MediaServerList( Cookie* cookie );
176 bool addServer( MediaServer* s );
177 void removeServer( const char* UDN );
179 MediaServer* getServer( const char* UDN );
180 MediaServer* getServerBySID( const char* );
186 std::vector<MediaServer*> _list;
194 Item( Container* parent, const char* objectID, const char* title, const char* resource );
196 const char* getObjectID() const;
197 const char* getTitle() const;
198 const char* getResource() const;
200 void setPlaylistNode( playlist_item_t* node );
201 playlist_item_t* getPlaylistNode() const ;
205 playlist_item_t* _playlistNode;
208 std::string _objectID;
210 std::string _resource;
218 Container( Container* parent, const char* objectID, const char* title );
221 void addItem( Item* item );
222 void addContainer( Container* container );
224 const char* getObjectID() const;
225 const char* getTitle() const;
227 unsigned int getNumItems() const;
228 unsigned int getNumContainers() const;
230 Item* getItem( unsigned int i ) const;
231 Container* getContainer( unsigned int i ) const;
233 void setPlaylistNode( playlist_item_t* node );
234 playlist_item_t* getPlaylistNode() const;
238 playlist_item_t* _playlistNode;
242 std::string _objectID;
244 std::vector<Item*> _items;
245 std::vector<Container*> _containers;
249 // VLC callback prototypes
251 static int Open( vlc_object_t* );
252 static void Close( vlc_object_t* );
253 static void Run( services_discovery_t *p_sd );
258 set_shortname( "UPnP" );
259 set_description( _( "Universal Plug'n'Play discovery ( Intel SDK )" ) );
260 set_category( CAT_PLAYLIST );
261 set_subcategory( SUBCAT_PLAYLIST_SD );
262 set_capability( "services_discovery", 0 );
263 set_callbacks( Open, Close );
267 // More prototypes...
269 static Lockable* CallbackLock;
270 static int Callback( Upnp_EventType eventType, void* event, void* pCookie );
272 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName );
273 IXML_Document* parseBrowseResult( IXML_Document* doc );
278 static int Open( vlc_object_t *p_this )
280 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
281 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
282 malloc( sizeof( services_discovery_sys_t ) );
287 /* Create our playlist node */
288 playlist_NodesPairCreate( pl_Get( p_sd ), _("Devices"),
289 &p_sys->p_node_cat, &p_sys->p_node_one,
295 static void Close( vlc_object_t *p_this )
297 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
298 services_discovery_sys_t *p_sys = p_sd->p_sys;
300 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_one, VLC_TRUE,
302 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_cat, VLC_TRUE,
308 static void Run( services_discovery_t* p_sd )
312 res = UpnpInit( 0, 0 );
313 if( res != UPNP_E_SUCCESS )
315 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
320 cookie.serviceDiscovery = p_sd;
321 cookie.serverList = new MediaServerList( &cookie );
323 CallbackLock = new Lockable( &cookie );
325 res = UpnpRegisterClient( Callback, &cookie, &cookie.clientHandle );
326 if( res != UPNP_E_SUCCESS )
328 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
332 res = UpnpSearchAsync( cookie.clientHandle, 5, MEDIA_SERVER_DEVICE_TYPE, &cookie );
333 if( res != UPNP_E_SUCCESS )
335 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
339 msg_Dbg( p_sd, "UPnP discovery started" );
340 while( !p_sd->b_die )
345 msg_Dbg( p_sd, "UPnP discovery stopped" );
349 delete cookie.serverList;
354 // XML utility functions:
356 // Returns the value of a child element, or 0 on error
357 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName )
359 if ( !parent ) return 0;
360 if ( !tagName ) return 0;
362 char* s = strdup( tagName );
363 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
365 if ( !nodeList ) return 0;
367 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
368 ixmlNodeList_free( nodeList );
369 if ( !element ) return 0;
371 IXML_Node* textNode = ixmlNode_getFirstChild( element );
372 if ( !textNode ) return 0;
374 return ixmlNode_getNodeValue( textNode );
377 // Extracts the result document from a SOAP response
378 IXML_Document* parseBrowseResult( IXML_Document* doc )
380 if ( !doc ) return 0;
382 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc, "Result" );
383 if ( !resultList ) return 0;
385 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
387 ixmlNodeList_free( resultList );
389 if ( !resultNode ) return 0;
391 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
392 if ( !textNode ) return 0;
394 const char* resultString = ixmlNode_getNodeValue( textNode );
395 char* resultXML = strdup( resultString );
397 resolve_xml_special_chars( resultXML );
399 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
407 // Handles all UPnP events
408 static int Callback( Upnp_EventType eventType, void* event, void* pCookie )
410 Locker locker( CallbackLock );
412 Cookie* cookie = ( Cookie* )pCookie;
414 switch( eventType ) {
416 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
417 case UPNP_DISCOVERY_SEARCH_RESULT:
419 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
421 IXML_Document *descriptionDoc = 0;
424 res = UpnpDownloadXmlDoc( discovery->Location, &descriptionDoc );
425 if ( res != UPNP_E_SUCCESS )
427 msg_Dbg( cookie->serviceDiscovery, "%s:%d: Could not download device description!", __FILE__, __LINE__ );
431 MediaServer::parseDeviceDescription( descriptionDoc, discovery->Location, cookie );
433 ixmlDocument_free( descriptionDoc );
437 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
439 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
441 cookie->serverList->removeServer( discovery->DeviceId );
445 case UPNP_EVENT_RECEIVED:
447 Upnp_Event* e = ( Upnp_Event* )event;
449 MediaServer* server = cookie->serverList->getServerBySID( e->Sid );
450 if ( server ) server->fetchContents();
454 case UPNP_EVENT_AUTORENEWAL_FAILED:
455 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
459 Upnp_Event_Subscribe* s = ( Upnp_Event_Subscribe* )event;
461 MediaServer* server = cookie->serverList->getServerBySID( s->Sid );
462 if ( server ) server->subscribeToContentDirectory();
466 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
467 msg_Warn( cookie->serviceDiscovery, "subscription complete" );
470 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
471 msg_Warn( cookie->serviceDiscovery, "search timeout" );
475 msg_Dbg( cookie->serviceDiscovery, "%s:%d: DEBUG: UNHANDLED EVENT ( TYPE=%d )", __FILE__, __LINE__, eventType );
479 return UPNP_E_SUCCESS;
483 // Class implementations...
487 void MediaServer::parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie )
489 if ( !doc ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
490 if ( !location ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
492 const char* baseURL = location;
494 // Try to extract baseURL
496 IXML_NodeList* urlList = ixmlDocument_getElementsByTagName( doc, "baseURL" );
499 if ( IXML_Node* urlNode = ixmlNodeList_item( urlList, 0 ) )
501 IXML_Node* textNode = ixmlNode_getFirstChild( urlNode );
502 if ( textNode ) baseURL = ixmlNode_getNodeValue( textNode );
505 ixmlNodeList_free( urlList );
510 IXML_NodeList* deviceList = ixmlDocument_getElementsByTagName( doc, "device" );
514 for ( unsigned int i = 0; i < ixmlNodeList_length( deviceList ); i++ )
516 IXML_Element* deviceElement = ( IXML_Element* )ixmlNodeList_item( deviceList, i );
518 const char* deviceType = xml_getChildElementValue( deviceElement, "deviceType" );
519 if ( !deviceType ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no deviceType!", __FILE__, __LINE__ ); continue; }
520 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, deviceType ) != 0 ) continue;
522 const char* UDN = xml_getChildElementValue( deviceElement, "UDN" );
523 if ( !UDN ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no UDN!", __FILE__, __LINE__ ); continue; }
524 if ( cookie->serverList->getServer( UDN ) != 0 ) continue;
526 const char* friendlyName = xml_getChildElementValue( deviceElement, "friendlyName" );
527 if ( !friendlyName ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no friendlyName!", __FILE__, __LINE__ ); continue; }
529 MediaServer* server = new MediaServer( UDN, friendlyName, cookie );
530 if ( !cookie->serverList->addServer( server ) ) {
537 // Check for ContentDirectory service...
539 IXML_NodeList* serviceList = ixmlElement_getElementsByTagName( deviceElement, "service" );
542 for ( unsigned int j = 0; j < ixmlNodeList_length( serviceList ); j++ )
544 IXML_Element* serviceElement = ( IXML_Element* )ixmlNodeList_item( serviceList, j );
546 const char* serviceType = xml_getChildElementValue( serviceElement, "serviceType" );
547 if ( !serviceType ) continue;
548 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE, serviceType ) != 0 ) continue;
550 const char* eventSubURL = xml_getChildElementValue( serviceElement, "eventSubURL" );
551 if ( !eventSubURL ) continue;
553 const char* controlURL = xml_getChildElementValue( serviceElement, "controlURL" );
554 if ( !controlURL ) continue;
556 // Try to subscribe to ContentDirectory service
558 char* url = ( char* )malloc( strlen( baseURL ) + strlen( eventSubURL ) + 1 );
561 char* s1 = strdup( baseURL );
562 char* s2 = strdup( eventSubURL );
564 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
566 // msg_Dbg( cookie->serviceDiscovery, "CDS EVENT URL: %s", url );
568 server->setContentDirectoryEventURL( url );
569 server->subscribeToContentDirectory();
577 // Try to browse content directory...
579 url = ( char* )malloc( strlen( baseURL ) + strlen( controlURL ) + 1 );
582 char* s1 = strdup( baseURL );
583 char* s2 = strdup( controlURL );
585 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
587 // msg_Dbg( cookie->serviceDiscovery, "CDS CTRL URL: %s", url );
589 server->setContentDirectoryControlURL( url );
590 server->fetchContents();
599 ixmlNodeList_free( serviceList );
603 ixmlNodeList_free( deviceList );
607 MediaServer::MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie )
612 _friendlyName = friendlyName;
618 MediaServer::~MediaServer()
622 playlist_NodeDelete( pl_Get( _cookie->serviceDiscovery ) ,
623 _playlistNode, VLC_TRUE, VLC_TRUE );
629 const char* MediaServer::getUDN() const
631 const char* s = _UDN.c_str();
635 const char* MediaServer::getFriendlyName() const
637 const char* s = _friendlyName.c_str();
641 void MediaServer::setContentDirectoryEventURL( const char* url )
643 _contentDirectoryEventURL = url;
646 const char* MediaServer::getContentDirectoryEventURL() const
648 const char* s = _contentDirectoryEventURL.c_str();
652 void MediaServer::setContentDirectoryControlURL( const char* url )
654 _contentDirectoryControlURL = url;
657 const char* MediaServer::getContentDirectoryControlURL() const
659 return _contentDirectoryControlURL.c_str();
662 void MediaServer::subscribeToContentDirectory()
664 const char* url = getContentDirectoryEventURL();
665 if ( !url || strcmp( url, "" ) == 0 )
667 msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" );
674 int res = UpnpSubscribe( _cookie->clientHandle, url, &timeOut, sid );
676 if ( res == UPNP_E_SUCCESS )
678 _subscriptionTimeOut = timeOut;
679 memcpy( _subscriptionID, sid, sizeof( Upnp_SID ) );
683 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: WARNING: '%s': %s", __FILE__, __LINE__, getFriendlyName(), UpnpGetErrorMessage( res ) );
687 IXML_Document* MediaServer::_browseAction( const char* pObjectID, const char* pBrowseFlag, const char* pFilter,
688 const char* pStartingIndex, const char* pRequestedCount, const char* pSortCriteria )
690 IXML_Document* action = 0;
691 IXML_Document* response = 0;
693 const char* url = getContentDirectoryControlURL();
694 if ( !url || strcmp( url, "" ) == 0 ) { msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" ); return 0; }
696 char* ObjectID = strdup( pObjectID );
697 char* BrowseFlag = strdup( pBrowseFlag );
698 char* Filter = strdup( pFilter );
699 char* StartingIndex = strdup( pStartingIndex );
700 char* RequestedCount = strdup( pRequestedCount );
701 char* SortCriteria = strdup( pSortCriteria );
703 char* serviceType = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
707 res = UpnpAddToAction( &action, "Browse", serviceType, "ObjectID", ObjectID );
708 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
710 res = UpnpAddToAction( &action, "Browse", serviceType, "BrowseFlag", BrowseFlag );
711 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
713 res = UpnpAddToAction( &action, "Browse", serviceType, "Filter", Filter );
714 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
716 res = UpnpAddToAction( &action, "Browse", serviceType, "StartingIndex", StartingIndex );
717 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
719 res = UpnpAddToAction( &action, "Browse", serviceType, "RequestedCount", RequestedCount );
720 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
722 res = UpnpAddToAction( &action, "Browse", serviceType, "SortCriteria", SortCriteria );
723 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
725 res = UpnpSendAction( _cookie->clientHandle,
727 CONTENT_DIRECTORY_SERVICE_TYPE,
731 if ( res != UPNP_E_SUCCESS )
733 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) );
734 ixmlDocument_free( response );
743 free( StartingIndex );
744 free( RequestedCount );
745 free( SortCriteria );
749 ixmlDocument_free( action );
753 void MediaServer::fetchContents()
755 Container* root = new Container( 0, "0", getFriendlyName() );
756 playlist_t * p_playlist = pl_Get( _cookie->serviceDiscovery );
757 _fetchContents( root );
762 playlist_NodeEmpty( p_playlist, _playlistNode, VLC_TRUE );
768 _contents->setPlaylistNode( _playlistNode );
770 _buildPlaylist( _contents );
773 bool MediaServer::_fetchContents( Container* parent )
775 if (!parent) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: parent==NULL", __FILE__, __LINE__ ); return false; }
777 IXML_Document* response = _browseAction( parent->getObjectID(), "BrowseDirectChildren", "*", "0", "0", "" );
778 if ( !response ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
780 IXML_Document* result = parseBrowseResult( response );
781 ixmlDocument_free( response );
782 if ( !result ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
784 IXML_NodeList* containerNodeList = ixmlDocument_getElementsByTagName( result, "container" );
785 if ( containerNodeList )
787 for ( unsigned int i = 0; i < ixmlNodeList_length( containerNodeList ); i++ )
789 IXML_Element* containerElement = ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
791 const char* objectID = ixmlElement_getAttribute( containerElement, "id" );
792 if ( !objectID ) continue;
794 const char* childCountStr = ixmlElement_getAttribute( containerElement, "childCount" );
795 if ( !childCountStr ) continue;
796 int childCount = atoi( childCountStr );
798 const char* title = xml_getChildElementValue( containerElement, "dc:title" );
799 if ( !title ) continue;
801 const char* resource = xml_getChildElementValue( containerElement, "res" );
803 if ( resource && childCount < 1 )
805 Item* item = new Item( parent, objectID, title, resource );
806 parent->addItem( item );
810 Container* container = new Container( parent, objectID, title );
811 parent->addContainer( container );
813 if ( childCount > 0 ) _fetchContents( container );
817 ixmlNodeList_free( containerNodeList );
820 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result, "item" );
823 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
825 IXML_Element* itemElement = ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
827 const char* objectID = ixmlElement_getAttribute( itemElement, "id" );
828 if ( !objectID ) continue;
830 const char* title = xml_getChildElementValue( itemElement, "dc:title" );
831 if ( !title ) continue;
833 const char* resource = xml_getChildElementValue( itemElement, "res" );
834 if ( !resource ) continue;
836 Item* item = new Item( parent, objectID, title, resource );
837 parent->addItem( item );
840 ixmlNodeList_free( itemNodeList );
843 ixmlDocument_free( result );
848 void MediaServer::_buildPlaylist( Container* parent )
850 playlist_t *p_playlist = pl_Get( _cookie->serviceDiscovery );
851 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
853 Container* container = parent->getContainer( i );
854 playlist_item_t* parentNode = parent->getPlaylistNode();
856 char* title = strdup( container->getTitle() );
857 playlist_item_t* node = playlist_NodeCreate( p_playlist, title, parentNode );
860 container->setPlaylistNode( node );
861 _buildPlaylist( container );
864 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
866 Item* item = parent->getItem( i );
867 playlist_item_t* parentNode = parent->getPlaylistNode();
869 input_item_t* p_input = input_ItemNew( _cookie->serviceDiscovery,
873 playlist_BothAddInput( p_playlist, p_input, parentNode,
874 PLAYLIST_APPEND, PLAYLIST_END, &i_cat, NULL );
875 /* TODO: do this better by storing ids */
876 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, i_cat, VLC_FALSE );
878 item->setPlaylistNode( p_node );
882 void MediaServer::setPlaylistNode( playlist_item_t* playlistNode )
884 _playlistNode = playlistNode;
887 bool MediaServer::compareSID( const char* sid )
889 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
893 // MediaServerList...
895 MediaServerList::MediaServerList( Cookie* cookie )
900 MediaServerList::~MediaServerList()
902 for ( unsigned int i = 0; i < _list.size(); i++ )
908 bool MediaServerList::addServer( MediaServer* s )
910 if ( getServer( s->getUDN() ) != 0 ) return false;
912 msg_Dbg( _cookie->serviceDiscovery, "Adding server '%s'", s->getFriendlyName() );
914 _list.push_back( s );
916 char* name = strdup( s->getFriendlyName() );
917 playlist_item_t* node = playlist_NodeCreate( pl_Get( _cookie->serviceDiscovery ),
919 _cookie->serviceDiscovery->p_sys->p_node_cat );
921 s->setPlaylistNode( node );
926 MediaServer* MediaServerList::getServer( const char* UDN )
928 MediaServer* result = 0;
930 for ( unsigned int i = 0; i < _list.size(); i++ )
932 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
942 MediaServer* MediaServerList::getServerBySID( const char* sid )
944 MediaServer* server = 0;
946 for ( unsigned int i = 0; i < _list.size(); i++ )
948 if ( _list[i]->compareSID( sid ) )
958 void MediaServerList::removeServer( const char* UDN )
960 MediaServer* server = getServer( UDN );
961 if ( !server ) return;
963 msg_Dbg( _cookie->serviceDiscovery, "Removing server '%s'", server->getFriendlyName() );
965 std::vector<MediaServer*>::iterator it;
966 for ( it = _list.begin(); it != _list.end(); it++ )
980 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
984 _objectID = objectID;
986 _resource = resource;
991 const char* Item::getObjectID() const
993 return _objectID.c_str();
996 const char* Item::getTitle() const
998 return _title.c_str();
1001 const char* Item::getResource() const
1003 return _resource.c_str();
1006 void Item::setPlaylistNode( playlist_item_t* node )
1008 _playlistNode = node;
1011 playlist_item_t* Item::getPlaylistNode() const
1013 return _playlistNode;
1019 Container::Container( Container* parent, const char* objectID, const char* title )
1023 _objectID = objectID;
1029 Container::~Container()
1031 for ( unsigned int i = 0; i < _containers.size(); i++ )
1033 delete _containers[i];
1036 for ( unsigned int i = 0; i < _items.size(); i++ )
1042 void Container::addItem( Item* item )
1044 _items.push_back( item );
1047 void Container::addContainer( Container* container )
1049 _containers.push_back( container );
1052 const char* Container::getObjectID() const
1054 return _objectID.c_str();
1057 const char* Container::getTitle() const
1059 return _title.c_str();
1062 unsigned int Container::getNumItems() const
1064 return _items.size();
1067 unsigned int Container::getNumContainers() const
1069 return _containers.size();
1072 Item* Container::getItem( unsigned int i ) const
1074 if ( i < _items.size() ) return _items[i];
1078 Container* Container::getContainer( unsigned int i ) const
1080 if ( i < _containers.size() ) return _containers[i];
1084 void Container::setPlaylistNode( playlist_item_t* node )
1086 _playlistNode = node;
1089 playlist_item_t* Container::getPlaylistNode() const
1091 return _playlistNode;