1 /*****************************************************************************
2 * Upnp_intel.cpp : UPnP discovery module (Intel SDK)
3 *****************************************************************************
4 * Copyright (C) 2004-2008 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>
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open( vlc_object_t* );
53 static void Close( vlc_object_t* );
56 set_shortname( "UPnP" );
57 set_description( N_( "Universal Plug'n'Play discovery ( Intel SDK )" ) );
58 set_category( CAT_PLAYLIST );
59 set_subcategory( SUBCAT_PLAYLIST_SD );
60 set_capability( "services_discovery", 0 );
61 set_callbacks( Open, Close );
65 /*****************************************************************************
67 *****************************************************************************/
71 struct services_discovery_sys_t
73 playlist_t *p_playlist;
74 playlist_item_t *p_node_cat;
75 playlist_item_t *p_node_one;
81 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
82 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
88 class MediaServerList;
93 // Cookie that is passed to the callback
97 services_discovery_t* serviceDiscovery;
98 UpnpClient_Handle clientHandle;
99 MediaServerList* serverList;
104 // Class definitions...
111 vlc_mutex_init( &_mutex );
116 vlc_mutex_destroy( &_mutex );
119 void lock() { vlc_mutex_lock( &_mutex ); }
120 void unlock() { vlc_mutex_unlock( &_mutex ); }
130 Locker( Lockable* l )
150 static void parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie );
152 MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie );
155 const char* getUDN() const;
156 const char* getFriendlyName() const;
158 void setContentDirectoryEventURL( const char* url );
159 const char* getContentDirectoryEventURL() const;
161 void setContentDirectoryControlURL( const char* url );
162 const char* getContentDirectoryControlURL() const;
164 void subscribeToContentDirectory();
165 void fetchContents();
167 void setPlaylistNode( playlist_item_t* node );
169 bool compareSID( const char* sid );
173 bool _fetchContents( Container* parent );
174 void _buildPlaylist( Container* container );
175 IXML_Document* _browseAction( const char*, const char*, const char*, const char*, const char*, const char* );
179 Container* _contents;
180 playlist_item_t* _playlistNode;
183 std::string _friendlyName;
185 std::string _contentDirectoryEventURL;
186 std::string _contentDirectoryControlURL;
188 int _subscriptionTimeOut;
189 Upnp_SID _subscriptionID;
193 class MediaServerList
197 MediaServerList( Cookie* cookie );
200 bool addServer( MediaServer* s );
201 void removeServer( const char* UDN );
203 MediaServer* getServer( const char* UDN );
204 MediaServer* getServerBySID( const char* );
210 std::vector<MediaServer*> _list;
218 Item( Container* parent, const char* objectID, const char* title, const char* resource );
220 const char* getObjectID() const;
221 const char* getTitle() const;
222 const char* getResource() const;
224 void setPlaylistNode( playlist_item_t* node );
225 playlist_item_t* getPlaylistNode() const ;
229 playlist_item_t* _playlistNode;
232 std::string _objectID;
234 std::string _resource;
242 Container( Container* parent, const char* objectID, const char* title );
245 void addItem( Item* item );
246 void addContainer( Container* container );
248 const char* getObjectID() const;
249 const char* getTitle() const;
251 unsigned int getNumItems() const;
252 unsigned int getNumContainers() const;
254 Item* getItem( unsigned int i ) const;
255 Container* getContainer( unsigned int i ) const;
257 void setPlaylistNode( playlist_item_t* node );
258 playlist_item_t* getPlaylistNode() const;
262 playlist_item_t* _playlistNode;
266 std::string _objectID;
268 std::vector<Item*> _items;
269 std::vector<Container*> _containers;
273 // VLC callback prototypes
275 static void Run( services_discovery_t *p_sd );
276 static playlist_t *pl_Get( services_discovery_t *p_sd )
278 return p_sd->p_sys->p_playlist;
281 // More prototypes...
283 static int Callback( Upnp_EventType eventType, void* event, void* pCookie );
285 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName );
286 IXML_Document* parseBrowseResult( IXML_Document* doc );
291 static int Open( vlc_object_t *p_this )
293 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
294 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
295 malloc( sizeof( services_discovery_sys_t ) );
299 p_sys->p_playlist = pl_Yield( p_sd );
301 /* Create our playlist node */
302 vlc_object_lock( p_sys->p_playlist );
303 playlist_NodesPairCreate( pl_Get( p_sd ), _("Devices"),
304 &p_sys->p_node_cat, &p_sys->p_node_one,
306 vlc_object_unlock( p_sys->p_playlist );
311 static void Close( vlc_object_t *p_this )
313 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
314 services_discovery_sys_t *p_sys = p_sd->p_sys;
316 vlc_object_lock( p_sys->p_playlist );
317 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_one, true,
319 playlist_NodeDelete( pl_Get( p_sd ), p_sys->p_node_cat, true,
321 vlc_object_unlock( p_sys->p_playlist );
326 static void Run( services_discovery_t* p_sd )
330 res = UpnpInit( 0, 0 );
331 if( res != UPNP_E_SUCCESS )
333 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
338 cookie.serviceDiscovery = p_sd;
339 cookie.serverList = new MediaServerList( &cookie );
340 cookie.lock = new Lockable();
342 res = UpnpRegisterClient( Callback, &cookie, &cookie.clientHandle );
343 if( res != UPNP_E_SUCCESS )
345 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
349 res = UpnpSearchAsync( cookie.clientHandle, 5, MEDIA_SERVER_DEVICE_TYPE, &cookie );
350 if( res != UPNP_E_SUCCESS )
352 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
356 msg_Dbg( p_sd, "UPnP discovery started" );
357 while( vlc_object_alive (p_sd) )
362 msg_Dbg( p_sd, "UPnP discovery stopped" );
366 delete cookie.serverList;
371 // XML utility functions:
373 // Returns the value of a child element, or 0 on error
374 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName )
376 if ( !parent ) return 0;
377 if ( !tagName ) return 0;
379 char* s = strdup( tagName );
380 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
382 if ( !nodeList ) return 0;
384 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
385 ixmlNodeList_free( nodeList );
386 if ( !element ) return 0;
388 IXML_Node* textNode = ixmlNode_getFirstChild( element );
389 if ( !textNode ) return 0;
391 return ixmlNode_getNodeValue( textNode );
394 // Extracts the result document from a SOAP response
395 IXML_Document* parseBrowseResult( IXML_Document* doc )
397 if ( !doc ) return 0;
399 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc, "Result" );
400 if ( !resultList ) return 0;
402 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
404 ixmlNodeList_free( resultList );
406 if ( !resultNode ) return 0;
408 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
409 if ( !textNode ) return 0;
411 const char* resultString = ixmlNode_getNodeValue( textNode );
412 char* resultXML = strdup( resultString );
414 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
422 // Handles all UPnP events
423 static int Callback( Upnp_EventType eventType, void* event, void* pCookie )
425 Cookie* cookie = ( Cookie* )pCookie;
427 Locker locker( cookie->lock );
429 switch( eventType ) {
431 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
432 case UPNP_DISCOVERY_SEARCH_RESULT:
434 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
436 IXML_Document *descriptionDoc = 0;
439 res = UpnpDownloadXmlDoc( discovery->Location, &descriptionDoc );
440 if ( res != UPNP_E_SUCCESS )
442 msg_Dbg( cookie->serviceDiscovery, "%s:%d: Could not download device description!", __FILE__, __LINE__ );
446 MediaServer::parseDeviceDescription( descriptionDoc, discovery->Location, cookie );
448 ixmlDocument_free( descriptionDoc );
452 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
454 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
456 cookie->serverList->removeServer( discovery->DeviceId );
460 case UPNP_EVENT_RECEIVED:
462 Upnp_Event* e = ( Upnp_Event* )event;
464 MediaServer* server = cookie->serverList->getServerBySID( e->Sid );
465 if ( server ) server->fetchContents();
469 case UPNP_EVENT_AUTORENEWAL_FAILED:
470 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
474 Upnp_Event_Subscribe* s = ( Upnp_Event_Subscribe* )event;
476 MediaServer* server = cookie->serverList->getServerBySID( s->Sid );
477 if ( server ) server->subscribeToContentDirectory();
481 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
482 msg_Warn( cookie->serviceDiscovery, "subscription complete" );
485 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
486 msg_Warn( cookie->serviceDiscovery, "search timeout" );
490 msg_Dbg( cookie->serviceDiscovery, "%s:%d: DEBUG: UNHANDLED EVENT ( TYPE=%d )", __FILE__, __LINE__, eventType );
494 return UPNP_E_SUCCESS;
498 // Class implementations...
502 void MediaServer::parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie )
504 if ( !doc ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
505 if ( !location ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
507 const char* baseURL = location;
509 // Try to extract baseURL
511 IXML_NodeList* urlList = ixmlDocument_getElementsByTagName( doc, "baseURL" );
514 if ( IXML_Node* urlNode = ixmlNodeList_item( urlList, 0 ) )
516 IXML_Node* textNode = ixmlNode_getFirstChild( urlNode );
517 if ( textNode ) baseURL = ixmlNode_getNodeValue( textNode );
520 ixmlNodeList_free( urlList );
525 IXML_NodeList* deviceList = ixmlDocument_getElementsByTagName( doc, "device" );
529 for ( unsigned int i = 0; i < ixmlNodeList_length( deviceList ); i++ )
531 IXML_Element* deviceElement = ( IXML_Element* )ixmlNodeList_item( deviceList, i );
533 const char* deviceType = xml_getChildElementValue( deviceElement, "deviceType" );
534 if ( !deviceType ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no deviceType!", __FILE__, __LINE__ ); continue; }
535 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, deviceType ) != 0 ) continue;
537 const char* UDN = xml_getChildElementValue( deviceElement, "UDN" );
538 if ( !UDN ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no UDN!", __FILE__, __LINE__ ); continue; }
539 if ( cookie->serverList->getServer( UDN ) != 0 ) continue;
541 const char* friendlyName = xml_getChildElementValue( deviceElement, "friendlyName" );
542 if ( !friendlyName ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no friendlyName!", __FILE__, __LINE__ ); continue; }
544 MediaServer* server = new MediaServer( UDN, friendlyName, cookie );
545 if ( !cookie->serverList->addServer( server ) ) {
552 // Check for ContentDirectory service...
554 IXML_NodeList* serviceList = ixmlElement_getElementsByTagName( deviceElement, "service" );
557 for ( unsigned int j = 0; j < ixmlNodeList_length( serviceList ); j++ )
559 IXML_Element* serviceElement = ( IXML_Element* )ixmlNodeList_item( serviceList, j );
561 const char* serviceType = xml_getChildElementValue( serviceElement, "serviceType" );
562 if ( !serviceType ) continue;
563 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE, serviceType ) != 0 ) continue;
565 const char* eventSubURL = xml_getChildElementValue( serviceElement, "eventSubURL" );
566 if ( !eventSubURL ) continue;
568 const char* controlURL = xml_getChildElementValue( serviceElement, "controlURL" );
569 if ( !controlURL ) continue;
571 // Try to subscribe to ContentDirectory service
573 char* url = ( char* )malloc( strlen( baseURL ) + strlen( eventSubURL ) + 1 );
576 char* s1 = strdup( baseURL );
577 char* s2 = strdup( eventSubURL );
579 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
581 // msg_Dbg( cookie->serviceDiscovery, "CDS EVENT URL: %s", url );
583 server->setContentDirectoryEventURL( url );
584 server->subscribeToContentDirectory();
592 // Try to browse content directory...
594 url = ( char* )malloc( strlen( baseURL ) + strlen( controlURL ) + 1 );
597 char* s1 = strdup( baseURL );
598 char* s2 = strdup( controlURL );
600 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
602 // msg_Dbg( cookie->serviceDiscovery, "CDS CTRL URL: %s", url );
604 server->setContentDirectoryControlURL( url );
605 server->fetchContents();
614 ixmlNodeList_free( serviceList );
618 ixmlNodeList_free( deviceList );
622 MediaServer::MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie )
627 _friendlyName = friendlyName;
633 MediaServer::~MediaServer()
637 vlc_object_lock( _cookie->serviceDiscovery->p_sys->p_playlist );
638 playlist_NodeDelete( pl_Get( _cookie->serviceDiscovery ) ,
639 _playlistNode, true, true );
640 vlc_object_unlock( _cookie->serviceDiscovery->p_sys->p_playlist );
646 const char* MediaServer::getUDN() const
648 const char* s = _UDN.c_str();
652 const char* MediaServer::getFriendlyName() const
654 const char* s = _friendlyName.c_str();
658 void MediaServer::setContentDirectoryEventURL( const char* url )
660 _contentDirectoryEventURL = url;
663 const char* MediaServer::getContentDirectoryEventURL() const
665 const char* s = _contentDirectoryEventURL.c_str();
669 void MediaServer::setContentDirectoryControlURL( const char* url )
671 _contentDirectoryControlURL = url;
674 const char* MediaServer::getContentDirectoryControlURL() const
676 return _contentDirectoryControlURL.c_str();
679 void MediaServer::subscribeToContentDirectory()
681 const char* url = getContentDirectoryEventURL();
682 if ( !url || strcmp( url, "" ) == 0 )
684 msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" );
691 int res = UpnpSubscribe( _cookie->clientHandle, url, &timeOut, sid );
693 if ( res == UPNP_E_SUCCESS )
695 _subscriptionTimeOut = timeOut;
696 memcpy( _subscriptionID, sid, sizeof( Upnp_SID ) );
700 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: WARNING: '%s': %s", __FILE__, __LINE__, getFriendlyName(), UpnpGetErrorMessage( res ) );
704 IXML_Document* MediaServer::_browseAction( const char* pObjectID, const char* pBrowseFlag, const char* pFilter,
705 const char* pStartingIndex, const char* pRequestedCount, const char* pSortCriteria )
707 IXML_Document* action = 0;
708 IXML_Document* response = 0;
710 const char* url = getContentDirectoryControlURL();
711 if ( !url || strcmp( url, "" ) == 0 ) { msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" ); return 0; }
713 char* ObjectID = strdup( pObjectID );
714 char* BrowseFlag = strdup( pBrowseFlag );
715 char* Filter = strdup( pFilter );
716 char* StartingIndex = strdup( pStartingIndex );
717 char* RequestedCount = strdup( pRequestedCount );
718 char* SortCriteria = strdup( pSortCriteria );
720 char* serviceType = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
724 res = UpnpAddToAction( &action, "Browse", serviceType, "ObjectID", ObjectID );
725 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
727 res = UpnpAddToAction( &action, "Browse", serviceType, "BrowseFlag", BrowseFlag );
728 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
730 res = UpnpAddToAction( &action, "Browse", serviceType, "Filter", Filter );
731 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
733 res = UpnpAddToAction( &action, "Browse", serviceType, "StartingIndex", StartingIndex );
734 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
736 res = UpnpAddToAction( &action, "Browse", serviceType, "RequestedCount", RequestedCount );
737 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
739 res = UpnpAddToAction( &action, "Browse", serviceType, "SortCriteria", SortCriteria );
740 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
742 res = UpnpSendAction( _cookie->clientHandle,
744 CONTENT_DIRECTORY_SERVICE_TYPE,
748 if ( res != UPNP_E_SUCCESS )
750 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) );
751 ixmlDocument_free( response );
760 free( StartingIndex );
761 free( RequestedCount );
762 free( SortCriteria );
766 ixmlDocument_free( action );
770 void MediaServer::fetchContents()
772 Container* root = new Container( 0, "0", getFriendlyName() );
773 playlist_t * p_playlist = pl_Get( _cookie->serviceDiscovery );
774 _fetchContents( root );
779 playlist_NodeEmpty( p_playlist, _playlistNode, true );
785 _contents->setPlaylistNode( _playlistNode );
787 _buildPlaylist( _contents );
790 bool MediaServer::_fetchContents( Container* parent )
792 if (!parent) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: parent==NULL", __FILE__, __LINE__ ); return false; }
794 IXML_Document* response = _browseAction( parent->getObjectID(), "BrowseDirectChildren", "*", "0", "0", "" );
795 if ( !response ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
797 IXML_Document* result = parseBrowseResult( response );
798 ixmlDocument_free( response );
799 if ( !result ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
801 IXML_NodeList* containerNodeList = ixmlDocument_getElementsByTagName( result, "container" );
802 if ( containerNodeList )
804 for ( unsigned int i = 0; i < ixmlNodeList_length( containerNodeList ); i++ )
806 IXML_Element* containerElement = ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
808 const char* objectID = ixmlElement_getAttribute( containerElement, "id" );
809 if ( !objectID ) continue;
811 const char* childCountStr = ixmlElement_getAttribute( containerElement, "childCount" );
812 if ( !childCountStr ) continue;
813 int childCount = atoi( childCountStr );
815 const char* title = xml_getChildElementValue( containerElement, "dc:title" );
816 if ( !title ) continue;
818 const char* resource = xml_getChildElementValue( containerElement, "res" );
820 if ( resource && childCount < 1 )
822 Item* item = new Item( parent, objectID, title, resource );
823 parent->addItem( item );
827 Container* container = new Container( parent, objectID, title );
828 parent->addContainer( container );
830 if ( childCount > 0 ) _fetchContents( container );
834 ixmlNodeList_free( containerNodeList );
837 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result, "item" );
840 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
842 IXML_Element* itemElement = ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
844 const char* objectID = ixmlElement_getAttribute( itemElement, "id" );
845 if ( !objectID ) continue;
847 const char* title = xml_getChildElementValue( itemElement, "dc:title" );
848 if ( !title ) continue;
850 const char* resource = xml_getChildElementValue( itemElement, "res" );
851 if ( !resource ) continue;
853 Item* item = new Item( parent, objectID, title, resource );
854 parent->addItem( item );
857 ixmlNodeList_free( itemNodeList );
860 ixmlDocument_free( result );
865 void MediaServer::_buildPlaylist( Container* parent )
867 playlist_t *p_playlist = pl_Get( _cookie->serviceDiscovery );
868 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
870 Container* container = parent->getContainer( i );
871 playlist_item_t* parentNode = parent->getPlaylistNode();
873 char* title = strdup( container->getTitle() );
874 playlist_item_t* node = playlist_NodeCreate( p_playlist, title, parentNode, 0, NULL );
877 container->setPlaylistNode( node );
878 _buildPlaylist( container );
881 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
883 Item* item = parent->getItem( i );
884 playlist_item_t* parentNode = parent->getPlaylistNode();
886 input_item_t* p_input = input_item_New( _cookie->serviceDiscovery,
890 /* FIXME: playlist_AddInput() can fail */
891 playlist_BothAddInput( p_playlist, p_input, parentNode,
892 PLAYLIST_APPEND, PLAYLIST_END, &i_cat, NULL,
894 vlc_gc_decref( p_input );
895 /* TODO: do this better by storing ids */
896 playlist_item_t *p_node = playlist_ItemGetById( p_playlist, i_cat, false );
898 item->setPlaylistNode( p_node );
902 void MediaServer::setPlaylistNode( playlist_item_t* playlistNode )
904 _playlistNode = playlistNode;
907 bool MediaServer::compareSID( const char* sid )
909 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
913 // MediaServerList...
915 MediaServerList::MediaServerList( Cookie* cookie )
920 MediaServerList::~MediaServerList()
922 for ( unsigned int i = 0; i < _list.size(); i++ )
928 bool MediaServerList::addServer( MediaServer* s )
930 if ( getServer( s->getUDN() ) != 0 ) return false;
932 msg_Dbg( _cookie->serviceDiscovery, "Adding server '%s'", s->getFriendlyName() );
934 _list.push_back( s );
936 char* name = strdup( s->getFriendlyName() );
937 vlc_object_lock( _cookie->serviceDiscovery->p_sys->p_playlist );
938 playlist_item_t* node = playlist_NodeCreate(
939 pl_Get( _cookie->serviceDiscovery ), name,
940 _cookie->serviceDiscovery->p_sys->p_node_cat,
942 vlc_object_unlock( _cookie->serviceDiscovery->p_sys->p_playlist );
944 s->setPlaylistNode( node );
949 MediaServer* MediaServerList::getServer( const char* UDN )
951 MediaServer* result = 0;
953 for ( unsigned int i = 0; i < _list.size(); i++ )
955 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
965 MediaServer* MediaServerList::getServerBySID( const char* sid )
967 MediaServer* server = 0;
969 for ( unsigned int i = 0; i < _list.size(); i++ )
971 if ( _list[i]->compareSID( sid ) )
981 void MediaServerList::removeServer( const char* UDN )
983 MediaServer* server = getServer( UDN );
984 if ( !server ) return;
986 msg_Dbg( _cookie->serviceDiscovery, "Removing server '%s'", server->getFriendlyName() );
988 std::vector<MediaServer*>::iterator it;
989 for ( it = _list.begin(); it != _list.end(); it++ )
1003 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
1007 _objectID = objectID;
1009 _resource = resource;
1014 const char* Item::getObjectID() const
1016 return _objectID.c_str();
1019 const char* Item::getTitle() const
1021 return _title.c_str();
1024 const char* Item::getResource() const
1026 return _resource.c_str();
1029 void Item::setPlaylistNode( playlist_item_t* node )
1031 _playlistNode = node;
1034 playlist_item_t* Item::getPlaylistNode() const
1036 return _playlistNode;
1042 Container::Container( Container* parent, const char* objectID, const char* title )
1046 _objectID = objectID;
1052 Container::~Container()
1054 for ( unsigned int i = 0; i < _containers.size(); i++ )
1056 delete _containers[i];
1059 for ( unsigned int i = 0; i < _items.size(); i++ )
1065 void Container::addItem( Item* item )
1067 _items.push_back( item );
1070 void Container::addContainer( Container* container )
1072 _containers.push_back( container );
1075 const char* Container::getObjectID() const
1077 return _objectID.c_str();
1080 const char* Container::getTitle() const
1082 return _title.c_str();
1085 unsigned int Container::getNumItems() const
1087 return _items.size();
1090 unsigned int Container::getNumContainers() const
1092 return _containers.size();
1095 Item* Container::getItem( unsigned int i ) const
1097 if ( i < _items.size() ) return _items[i];
1101 Container* Container::getContainer( unsigned int i ) const
1103 if ( i < _containers.size() ) return _containers[i];
1107 void Container::setPlaylistNode( playlist_item_t* node )
1109 _playlistNode = node;
1112 playlist_item_t* Container::getPlaylistNode() const
1114 return _playlistNode;