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>
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_playlist.h>
52 struct services_discovery_sys_t
54 playlist_t *p_playlist;
55 playlist_item_t *p_node_cat;
56 playlist_item_t *p_node_one;
62 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
63 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
69 class MediaServerList;
73 // Cookie that is passed to the callback
77 services_discovery_t* serviceDiscovery;
78 UpnpClient_Handle clientHandle;
79 MediaServerList* serverList;
83 // Class definitions...
91 vlc_mutex_init( &_mutex );
96 vlc_mutex_destroy( &_mutex );
99 void lock() { vlc_mutex_lock( &_mutex ); }
100 void unlock() { vlc_mutex_unlock( &_mutex ); }
111 Locker( Lockable* l )
131 static void parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie );
133 MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie );
136 const char* getUDN() const;
137 const char* getFriendlyName() const;
139 void setContentDirectoryEventURL( const char* url );
140 const char* getContentDirectoryEventURL() const;
142 void setContentDirectoryControlURL( const char* url );
143 const char* getContentDirectoryControlURL() const;
145 void subscribeToContentDirectory();
146 void fetchContents();
148 void setPlaylistNode( playlist_item_t* node );
150 bool compareSID( const char* sid );
154 bool _fetchContents( Container* parent );
155 void _buildPlaylist( Container* container );
156 IXML_Document* _browseAction( const char*, const char*, const char*, const char*, const char*, const char* );
160 Container* _contents;
161 playlist_item_t* _playlistNode;
164 std::string _friendlyName;
166 std::string _contentDirectoryEventURL;
167 std::string _contentDirectoryControlURL;
169 int _subscriptionTimeOut;
170 Upnp_SID _subscriptionID;
174 class MediaServerList
178 MediaServerList( Cookie* cookie );
181 bool addServer( MediaServer* s );
182 void removeServer( const char* UDN );
184 MediaServer* getServer( const char* UDN );
185 MediaServer* getServerBySID( const char* );
191 std::vector<MediaServer*> _list;
199 Item( Container* parent, const char* objectID, const char* title, const char* resource );
201 const char* getObjectID() const;
202 const char* getTitle() const;
203 const char* getResource() const;
205 void setPlaylistNode( playlist_item_t* node );
206 playlist_item_t* getPlaylistNode() const ;
210 playlist_item_t* _playlistNode;
213 std::string _objectID;
215 std::string _resource;
223 Container( Container* parent, const char* objectID, const char* title );
226 void addItem( Item* item );
227 void addContainer( Container* container );
229 const char* getObjectID() const;
230 const char* getTitle() const;
232 unsigned int getNumItems() const;
233 unsigned int getNumContainers() const;
235 Item* getItem( unsigned int i ) const;
236 Container* getContainer( unsigned int i ) const;
238 void setPlaylistNode( playlist_item_t* node );
239 playlist_item_t* getPlaylistNode() const;
243 playlist_item_t* _playlistNode;
247 std::string _objectID;
249 std::vector<Item*> _items;
250 std::vector<Container*> _containers;
254 // VLC callback prototypes
256 static int Open( vlc_object_t* );
257 static void Close( vlc_object_t* );
258 static void Run( services_discovery_t *p_sd );
259 static playlist_t *pl_Get( services_discovery_t *p_sd )
261 return p_sd->p_sys->p_playlist;
267 set_shortname( "UPnP" );
268 set_description( N_( "Universal Plug'n'Play discovery ( Intel SDK )" ) );
269 set_category( CAT_PLAYLIST );
270 set_subcategory( SUBCAT_PLAYLIST_SD );
271 set_capability( "services_discovery", 0 );
272 set_callbacks( Open, Close );
276 // More prototypes...
278 static Lockable* CallbackLock;
279 static int Callback( Upnp_EventType eventType, void* event, void* pCookie );
281 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName );
282 IXML_Document* parseBrowseResult( IXML_Document* doc );
287 static int Open( vlc_object_t *p_this )
289 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
290 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
291 malloc( sizeof( services_discovery_sys_t ) );
295 p_sys->p_playlist = pl_Yield( p_sd );
297 /* Create our playlist node */
298 vlc_object_lock( p_sys->p_playlist );
299 playlist_NodesPairCreate( pl_Get( p_sd ), _("Devices"),
300 &p_sys->p_node_cat, &p_sys->p_node_one,
302 vlc_object_unlock( p_sys->p_playlist );
307 static void Close( vlc_object_t *p_this )
309 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
310 services_discovery_sys_t *p_sys = p_sd->p_sys;
312 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_one, true,
314 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_cat, true,
320 static void Run( services_discovery_t* p_sd )
324 res = UpnpInit( 0, 0 );
325 if( res != UPNP_E_SUCCESS )
327 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
332 cookie.serviceDiscovery = p_sd;
333 cookie.serverList = new MediaServerList( &cookie );
335 CallbackLock = new Lockable( &cookie );
337 res = UpnpRegisterClient( Callback, &cookie, &cookie.clientHandle );
338 if( res != UPNP_E_SUCCESS )
340 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
344 res = UpnpSearchAsync( cookie.clientHandle, 5, MEDIA_SERVER_DEVICE_TYPE, &cookie );
345 if( res != UPNP_E_SUCCESS )
347 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
351 msg_Dbg( p_sd, "UPnP discovery started" );
352 while( vlc_object_alive (p_sd) )
357 msg_Dbg( p_sd, "UPnP discovery stopped" );
361 delete cookie.serverList;
366 // XML utility functions:
368 // Returns the value of a child element, or 0 on error
369 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName )
371 if ( !parent ) return 0;
372 if ( !tagName ) return 0;
374 char* s = strdup( tagName );
375 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
377 if ( !nodeList ) return 0;
379 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
380 ixmlNodeList_free( nodeList );
381 if ( !element ) return 0;
383 IXML_Node* textNode = ixmlNode_getFirstChild( element );
384 if ( !textNode ) return 0;
386 return ixmlNode_getNodeValue( textNode );
389 // Extracts the result document from a SOAP response
390 IXML_Document* parseBrowseResult( IXML_Document* doc )
392 if ( !doc ) return 0;
394 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc, "Result" );
395 if ( !resultList ) return 0;
397 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
399 ixmlNodeList_free( resultList );
401 if ( !resultNode ) return 0;
403 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
404 if ( !textNode ) return 0;
406 const char* resultString = ixmlNode_getNodeValue( textNode );
407 char* resultXML = strdup( resultString );
409 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
417 // Handles all UPnP events
418 static int Callback( Upnp_EventType eventType, void* event, void* pCookie )
420 Locker locker( CallbackLock );
422 Cookie* cookie = ( Cookie* )pCookie;
424 switch( eventType ) {
426 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
427 case UPNP_DISCOVERY_SEARCH_RESULT:
429 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
431 IXML_Document *descriptionDoc = 0;
434 res = UpnpDownloadXmlDoc( discovery->Location, &descriptionDoc );
435 if ( res != UPNP_E_SUCCESS )
437 msg_Dbg( cookie->serviceDiscovery, "%s:%d: Could not download device description!", __FILE__, __LINE__ );
441 MediaServer::parseDeviceDescription( descriptionDoc, discovery->Location, cookie );
443 ixmlDocument_free( descriptionDoc );
447 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
449 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
451 cookie->serverList->removeServer( discovery->DeviceId );
455 case UPNP_EVENT_RECEIVED:
457 Upnp_Event* e = ( Upnp_Event* )event;
459 MediaServer* server = cookie->serverList->getServerBySID( e->Sid );
460 if ( server ) server->fetchContents();
464 case UPNP_EVENT_AUTORENEWAL_FAILED:
465 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
469 Upnp_Event_Subscribe* s = ( Upnp_Event_Subscribe* )event;
471 MediaServer* server = cookie->serverList->getServerBySID( s->Sid );
472 if ( server ) server->subscribeToContentDirectory();
476 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
477 msg_Warn( cookie->serviceDiscovery, "subscription complete" );
480 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
481 msg_Warn( cookie->serviceDiscovery, "search timeout" );
485 msg_Dbg( cookie->serviceDiscovery, "%s:%d: DEBUG: UNHANDLED EVENT ( TYPE=%d )", __FILE__, __LINE__, eventType );
489 return UPNP_E_SUCCESS;
493 // Class implementations...
497 void MediaServer::parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie )
499 if ( !doc ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
500 if ( !location ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
502 const char* baseURL = location;
504 // Try to extract baseURL
506 IXML_NodeList* urlList = ixmlDocument_getElementsByTagName( doc, "baseURL" );
509 if ( IXML_Node* urlNode = ixmlNodeList_item( urlList, 0 ) )
511 IXML_Node* textNode = ixmlNode_getFirstChild( urlNode );
512 if ( textNode ) baseURL = ixmlNode_getNodeValue( textNode );
515 ixmlNodeList_free( urlList );
520 IXML_NodeList* deviceList = ixmlDocument_getElementsByTagName( doc, "device" );
524 for ( unsigned int i = 0; i < ixmlNodeList_length( deviceList ); i++ )
526 IXML_Element* deviceElement = ( IXML_Element* )ixmlNodeList_item( deviceList, i );
528 const char* deviceType = xml_getChildElementValue( deviceElement, "deviceType" );
529 if ( !deviceType ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no deviceType!", __FILE__, __LINE__ ); continue; }
530 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, deviceType ) != 0 ) continue;
532 const char* UDN = xml_getChildElementValue( deviceElement, "UDN" );
533 if ( !UDN ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no UDN!", __FILE__, __LINE__ ); continue; }
534 if ( cookie->serverList->getServer( UDN ) != 0 ) continue;
536 const char* friendlyName = xml_getChildElementValue( deviceElement, "friendlyName" );
537 if ( !friendlyName ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no friendlyName!", __FILE__, __LINE__ ); continue; }
539 MediaServer* server = new MediaServer( UDN, friendlyName, cookie );
540 if ( !cookie->serverList->addServer( server ) ) {
547 // Check for ContentDirectory service...
549 IXML_NodeList* serviceList = ixmlElement_getElementsByTagName( deviceElement, "service" );
552 for ( unsigned int j = 0; j < ixmlNodeList_length( serviceList ); j++ )
554 IXML_Element* serviceElement = ( IXML_Element* )ixmlNodeList_item( serviceList, j );
556 const char* serviceType = xml_getChildElementValue( serviceElement, "serviceType" );
557 if ( !serviceType ) continue;
558 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE, serviceType ) != 0 ) continue;
560 const char* eventSubURL = xml_getChildElementValue( serviceElement, "eventSubURL" );
561 if ( !eventSubURL ) continue;
563 const char* controlURL = xml_getChildElementValue( serviceElement, "controlURL" );
564 if ( !controlURL ) continue;
566 // Try to subscribe to ContentDirectory service
568 char* url = ( char* )malloc( strlen( baseURL ) + strlen( eventSubURL ) + 1 );
571 char* s1 = strdup( baseURL );
572 char* s2 = strdup( eventSubURL );
574 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
576 // msg_Dbg( cookie->serviceDiscovery, "CDS EVENT URL: %s", url );
578 server->setContentDirectoryEventURL( url );
579 server->subscribeToContentDirectory();
587 // Try to browse content directory...
589 url = ( char* )malloc( strlen( baseURL ) + strlen( controlURL ) + 1 );
592 char* s1 = strdup( baseURL );
593 char* s2 = strdup( controlURL );
595 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
597 // msg_Dbg( cookie->serviceDiscovery, "CDS CTRL URL: %s", url );
599 server->setContentDirectoryControlURL( url );
600 server->fetchContents();
609 ixmlNodeList_free( serviceList );
613 ixmlNodeList_free( deviceList );
617 MediaServer::MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie )
622 _friendlyName = friendlyName;
628 MediaServer::~MediaServer()
632 playlist_NodeDelete( pl_Get( _cookie->serviceDiscovery ) ,
633 _playlistNode, true, true );
639 const char* MediaServer::getUDN() const
641 const char* s = _UDN.c_str();
645 const char* MediaServer::getFriendlyName() const
647 const char* s = _friendlyName.c_str();
651 void MediaServer::setContentDirectoryEventURL( const char* url )
653 _contentDirectoryEventURL = url;
656 const char* MediaServer::getContentDirectoryEventURL() const
658 const char* s = _contentDirectoryEventURL.c_str();
662 void MediaServer::setContentDirectoryControlURL( const char* url )
664 _contentDirectoryControlURL = url;
667 const char* MediaServer::getContentDirectoryControlURL() const
669 return _contentDirectoryControlURL.c_str();
672 void MediaServer::subscribeToContentDirectory()
674 const char* url = getContentDirectoryEventURL();
675 if ( !url || strcmp( url, "" ) == 0 )
677 msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" );
684 int res = UpnpSubscribe( _cookie->clientHandle, url, &timeOut, sid );
686 if ( res == UPNP_E_SUCCESS )
688 _subscriptionTimeOut = timeOut;
689 memcpy( _subscriptionID, sid, sizeof( Upnp_SID ) );
693 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: WARNING: '%s': %s", __FILE__, __LINE__, getFriendlyName(), UpnpGetErrorMessage( res ) );
697 IXML_Document* MediaServer::_browseAction( const char* pObjectID, const char* pBrowseFlag, const char* pFilter,
698 const char* pStartingIndex, const char* pRequestedCount, const char* pSortCriteria )
700 IXML_Document* action = 0;
701 IXML_Document* response = 0;
703 const char* url = getContentDirectoryControlURL();
704 if ( !url || strcmp( url, "" ) == 0 ) { msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" ); return 0; }
706 char* ObjectID = strdup( pObjectID );
707 char* BrowseFlag = strdup( pBrowseFlag );
708 char* Filter = strdup( pFilter );
709 char* StartingIndex = strdup( pStartingIndex );
710 char* RequestedCount = strdup( pRequestedCount );
711 char* SortCriteria = strdup( pSortCriteria );
713 char* serviceType = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
717 res = UpnpAddToAction( &action, "Browse", serviceType, "ObjectID", ObjectID );
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, "BrowseFlag", BrowseFlag );
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, "Filter", Filter );
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, "StartingIndex", StartingIndex );
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, "RequestedCount", RequestedCount );
730 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
732 res = UpnpAddToAction( &action, "Browse", serviceType, "SortCriteria", SortCriteria );
733 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
735 res = UpnpSendAction( _cookie->clientHandle,
737 CONTENT_DIRECTORY_SERVICE_TYPE,
741 if ( res != UPNP_E_SUCCESS )
743 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) );
744 ixmlDocument_free( response );
753 free( StartingIndex );
754 free( RequestedCount );
755 free( SortCriteria );
759 ixmlDocument_free( action );
763 void MediaServer::fetchContents()
765 Container* root = new Container( 0, "0", getFriendlyName() );
766 playlist_t * p_playlist = pl_Get( _cookie->serviceDiscovery );
767 _fetchContents( root );
772 playlist_NodeEmpty( p_playlist, _playlistNode, true );
778 _contents->setPlaylistNode( _playlistNode );
780 _buildPlaylist( _contents );
783 bool MediaServer::_fetchContents( Container* parent )
785 if (!parent) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: parent==NULL", __FILE__, __LINE__ ); return false; }
787 IXML_Document* response = _browseAction( parent->getObjectID(), "BrowseDirectChildren", "*", "0", "0", "" );
788 if ( !response ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
790 IXML_Document* result = parseBrowseResult( response );
791 ixmlDocument_free( response );
792 if ( !result ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
794 IXML_NodeList* containerNodeList = ixmlDocument_getElementsByTagName( result, "container" );
795 if ( containerNodeList )
797 for ( unsigned int i = 0; i < ixmlNodeList_length( containerNodeList ); i++ )
799 IXML_Element* containerElement = ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
801 const char* objectID = ixmlElement_getAttribute( containerElement, "id" );
802 if ( !objectID ) continue;
804 const char* childCountStr = ixmlElement_getAttribute( containerElement, "childCount" );
805 if ( !childCountStr ) continue;
806 int childCount = atoi( childCountStr );
808 const char* title = xml_getChildElementValue( containerElement, "dc:title" );
809 if ( !title ) continue;
811 const char* resource = xml_getChildElementValue( containerElement, "res" );
813 if ( resource && childCount < 1 )
815 Item* item = new Item( parent, objectID, title, resource );
816 parent->addItem( item );
820 Container* container = new Container( parent, objectID, title );
821 parent->addContainer( container );
823 if ( childCount > 0 ) _fetchContents( container );
827 ixmlNodeList_free( containerNodeList );
830 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result, "item" );
833 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
835 IXML_Element* itemElement = ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
837 const char* objectID = ixmlElement_getAttribute( itemElement, "id" );
838 if ( !objectID ) continue;
840 const char* title = xml_getChildElementValue( itemElement, "dc:title" );
841 if ( !title ) continue;
843 const char* resource = xml_getChildElementValue( itemElement, "res" );
844 if ( !resource ) continue;
846 Item* item = new Item( parent, objectID, title, resource );
847 parent->addItem( item );
850 ixmlNodeList_free( itemNodeList );
853 ixmlDocument_free( result );
858 void MediaServer::_buildPlaylist( Container* parent )
860 playlist_t *p_playlist = pl_Get( _cookie->serviceDiscovery );
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( p_playlist, title, parentNode, 0, NULL );
870 container->setPlaylistNode( node );
871 _buildPlaylist( container );
874 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
876 Item* item = parent->getItem( i );
877 playlist_item_t* parentNode = parent->getPlaylistNode();
879 input_item_t* p_input = input_ItemNew( _cookie->serviceDiscovery,
883 /* FIXME: playlist_AddInput() can fail */
884 playlist_BothAddInput( p_playlist, p_input, parentNode,
885 PLAYLIST_APPEND, PLAYLIST_END, &i_cat, NULL,
887 vlc_gc_decref( p_input );
888 /* TODO: do this better by storing ids */
889 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, i_cat, false );
891 item->setPlaylistNode( p_node );
895 void MediaServer::setPlaylistNode( playlist_item_t* playlistNode )
897 _playlistNode = playlistNode;
900 bool MediaServer::compareSID( const char* sid )
902 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
906 // MediaServerList...
908 MediaServerList::MediaServerList( Cookie* cookie )
913 MediaServerList::~MediaServerList()
915 for ( unsigned int i = 0; i < _list.size(); i++ )
921 bool MediaServerList::addServer( MediaServer* s )
923 if ( getServer( s->getUDN() ) != 0 ) return false;
925 msg_Dbg( _cookie->serviceDiscovery, "Adding server '%s'", s->getFriendlyName() );
927 _list.push_back( s );
929 char* name = strdup( s->getFriendlyName() );
930 playlist_item_t* node = playlist_NodeCreate( pl_Get( _cookie->serviceDiscovery ),
932 _cookie->serviceDiscovery->p_sys->p_node_cat, 0, NULL );
934 s->setPlaylistNode( node );
939 MediaServer* MediaServerList::getServer( const char* UDN )
941 MediaServer* result = 0;
943 for ( unsigned int i = 0; i < _list.size(); i++ )
945 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
955 MediaServer* MediaServerList::getServerBySID( const char* sid )
957 MediaServer* server = 0;
959 for ( unsigned int i = 0; i < _list.size(); i++ )
961 if ( _list[i]->compareSID( sid ) )
971 void MediaServerList::removeServer( const char* UDN )
973 MediaServer* server = getServer( UDN );
974 if ( !server ) return;
976 msg_Dbg( _cookie->serviceDiscovery, "Removing server '%s'", server->getFriendlyName() );
978 std::vector<MediaServer*>::iterator it;
979 for ( it = _list.begin(); it != _list.end(); it++ )
993 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
997 _objectID = objectID;
999 _resource = resource;
1004 const char* Item::getObjectID() const
1006 return _objectID.c_str();
1009 const char* Item::getTitle() const
1011 return _title.c_str();
1014 const char* Item::getResource() const
1016 return _resource.c_str();
1019 void Item::setPlaylistNode( playlist_item_t* node )
1021 _playlistNode = node;
1024 playlist_item_t* Item::getPlaylistNode() const
1026 return _playlistNode;
1032 Container::Container( Container* parent, const char* objectID, const char* title )
1036 _objectID = objectID;
1042 Container::~Container()
1044 for ( unsigned int i = 0; i < _containers.size(); i++ )
1046 delete _containers[i];
1049 for ( unsigned int i = 0; i < _items.size(); i++ )
1055 void Container::addItem( Item* item )
1057 _items.push_back( item );
1060 void Container::addContainer( Container* container )
1062 _containers.push_back( container );
1065 const char* Container::getObjectID() const
1067 return _objectID.c_str();
1070 const char* Container::getTitle() const
1072 return _title.c_str();
1075 unsigned int Container::getNumItems() const
1077 return _items.size();
1080 unsigned int Container::getNumContainers() const
1082 return _containers.size();
1085 Item* Container::getItem( unsigned int i ) const
1087 if ( i < _items.size() ) return _items[i];
1091 Container* Container::getContainer( unsigned int i ) const
1093 if ( i < _containers.size() ) return _containers[i];
1097 void Container::setPlaylistNode( playlist_item_t* node )
1099 _playlistNode = node;
1102 playlist_item_t* Container::getPlaylistNode() const
1104 return _playlistNode;