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>
44 #include "vlc_strings.h"
49 struct services_discovery_sys_t
51 playlist_item_t *p_node;
52 playlist_t *p_playlist;
58 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
59 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
65 class MediaServerList;
69 // Cookie that is passed to the callback
73 services_discovery_t* serviceDiscovery;
74 UpnpClient_Handle clientHandle;
75 MediaServerList* serverList;
79 // Class definitions...
87 vlc_mutex_init( c->serviceDiscovery, &_mutex );
92 vlc_mutex_destroy( &_mutex );
95 void lock() { vlc_mutex_lock( &_mutex ); }
96 void unlock() { vlc_mutex_unlock( &_mutex ); }
107 Locker( Lockable* l )
127 static void parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie );
129 MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie );
132 const char* getUDN() const;
133 const char* getFriendlyName() const;
135 void setContentDirectoryEventURL( const char* url );
136 const char* getContentDirectoryEventURL() const;
138 void setContentDirectoryControlURL( const char* url );
139 const char* getContentDirectoryControlURL() const;
141 void subscribeToContentDirectory();
142 void fetchContents();
144 void setPlaylistNode( playlist_item_t* node );
146 bool compareSID( const char* sid );
150 bool _fetchContents( Container* parent );
151 void _buildPlaylist( Container* container );
152 IXML_Document* _browseAction( const char*, const char*, const char*, const char*, const char*, const char* );
156 Container* _contents;
157 playlist_item_t* _playlistNode;
160 std::string _friendlyName;
162 std::string _contentDirectoryEventURL;
163 std::string _contentDirectoryControlURL;
165 int _subscriptionTimeOut;
166 Upnp_SID _subscriptionID;
170 class MediaServerList
174 MediaServerList( Cookie* cookie );
177 bool addServer( MediaServer* s );
178 void removeServer( const char* UDN );
180 MediaServer* getServer( const char* UDN );
181 MediaServer* getServerBySID( const char* );
187 std::vector<MediaServer*> _list;
195 Item( Container* parent, const char* objectID, const char* title, const char* resource );
197 const char* getObjectID() const;
198 const char* getTitle() const;
199 const char* getResource() const;
201 void setPlaylistNode( playlist_item_t* node );
202 playlist_item_t* getPlaylistNode() const ;
206 playlist_item_t* _playlistNode;
209 std::string _objectID;
211 std::string _resource;
219 Container( Container* parent, const char* objectID, const char* title );
222 void addItem( Item* item );
223 void addContainer( Container* container );
225 const char* getObjectID() const;
226 const char* getTitle() const;
228 unsigned int getNumItems() const;
229 unsigned int getNumContainers() const;
231 Item* getItem( unsigned int i ) const;
232 Container* getContainer( unsigned int i ) const;
234 void setPlaylistNode( playlist_item_t* node );
235 playlist_item_t* getPlaylistNode() const;
239 playlist_item_t* _playlistNode;
243 std::string _objectID;
245 std::vector<Item*> _items;
246 std::vector<Container*> _containers;
250 // VLC callback prototypes
252 static int Open( vlc_object_t* );
253 static void Close( vlc_object_t* );
254 static void Run( services_discovery_t *p_sd );
259 set_shortname( "UPnP" );
260 set_description( _( "Universal Plug'n'Play discovery ( Intel SDK )" ) );
261 set_category( CAT_PLAYLIST );
262 set_subcategory( SUBCAT_PLAYLIST_SD );
263 set_capability( "services_discovery", 0 );
264 set_callbacks( Open, Close );
268 // More prototypes...
270 static Lockable* CallbackLock;
271 static int Callback( Upnp_EventType eventType, void* event, void* pCookie );
273 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName );
274 IXML_Document* parseBrowseResult( IXML_Document* doc );
279 static int Open( vlc_object_t *p_this )
281 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
282 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
283 malloc( sizeof( services_discovery_sys_t ) );
285 playlist_view_t *p_view;
291 /* Create our playlist node */
292 p_sys->p_playlist = ( playlist_t * )vlc_object_find( p_sd,
295 if( !p_sys->p_playlist )
297 msg_Warn( p_sd, "unable to find playlist, cancelling UPnP listening" );
301 p_view = playlist_ViewFind( p_sys->p_playlist, VIEW_CATEGORY );
302 p_sys->p_node = playlist_NodeCreate( p_sys->p_playlist, VIEW_CATEGORY,
303 "UPnP", p_view->p_root );
304 p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
305 p_sys->p_node->i_flags &= ~PLAYLIST_SKIP_FLAG;
306 val.b_bool = VLC_TRUE;
307 var_Set( p_sys->p_playlist, "intf-change", val );
312 static void Close( vlc_object_t *p_this )
314 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
315 services_discovery_sys_t *p_sys = p_sd->p_sys;
317 if( p_sys->p_playlist )
319 playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node, VLC_TRUE,
321 vlc_object_release( p_sys->p_playlist );
327 static void Run( services_discovery_t* p_sd )
331 res = UpnpInit( 0, 0 );
332 if( res != UPNP_E_SUCCESS )
334 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
339 cookie.serviceDiscovery = p_sd;
340 cookie.serverList = new MediaServerList( &cookie );
342 CallbackLock = new Lockable( &cookie );
344 res = UpnpRegisterClient( Callback, &cookie, &cookie.clientHandle );
345 if( res != UPNP_E_SUCCESS )
347 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
351 res = UpnpSearchAsync( cookie.clientHandle, 5, MEDIA_SERVER_DEVICE_TYPE, &cookie );
352 if( res != UPNP_E_SUCCESS )
354 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
358 msg_Dbg( p_sd, "UPnP discovery started" );
359 while( !p_sd->b_die )
364 msg_Dbg( p_sd, "UPnP discovery stopped" );
368 delete cookie.serverList;
373 // XML utility functions:
375 // Returns the value of a child element, or 0 on error
376 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName )
378 if ( !parent ) return 0;
379 if ( !tagName ) return 0;
381 char* s = strdup( tagName );
382 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
384 if ( !nodeList ) return 0;
386 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
387 ixmlNodeList_free( nodeList );
388 if ( !element ) return 0;
390 IXML_Node* textNode = ixmlNode_getFirstChild( element );
391 if ( !textNode ) return 0;
393 return ixmlNode_getNodeValue( textNode );
396 // Extracts the result document from a SOAP response
397 IXML_Document* parseBrowseResult( IXML_Document* doc )
399 if ( !doc ) return 0;
401 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc, "Result" );
402 if ( !resultList ) return 0;
404 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
406 ixmlNodeList_free( resultList );
408 if ( !resultNode ) return 0;
410 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
411 if ( !textNode ) return 0;
413 const char* resultString = ixmlNode_getNodeValue( textNode );
414 char* resultXML = strdup( resultString );
416 resolve_xml_special_chars( resultXML );
418 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
426 // Handles all UPnP events
427 static int Callback( Upnp_EventType eventType, void* event, void* pCookie )
429 Locker locker( CallbackLock );
431 Cookie* cookie = ( Cookie* )pCookie;
433 switch( eventType ) {
435 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
436 case UPNP_DISCOVERY_SEARCH_RESULT:
438 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
440 IXML_Document *descriptionDoc = 0;
443 res = UpnpDownloadXmlDoc( discovery->Location, &descriptionDoc );
444 if ( res != UPNP_E_SUCCESS )
446 msg_Dbg( cookie->serviceDiscovery, "%s:%d: Could not download device description!", __FILE__, __LINE__ );
450 MediaServer::parseDeviceDescription( descriptionDoc, discovery->Location, cookie );
452 ixmlDocument_free( descriptionDoc );
456 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
458 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
460 cookie->serverList->removeServer( discovery->DeviceId );
464 case UPNP_EVENT_RECEIVED:
466 Upnp_Event* e = ( Upnp_Event* )event;
468 MediaServer* server = cookie->serverList->getServerBySID( e->Sid );
469 if ( server ) server->fetchContents();
473 case UPNP_EVENT_AUTORENEWAL_FAILED:
474 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
478 Upnp_Event_Subscribe* s = ( Upnp_Event_Subscribe* )event;
480 MediaServer* server = cookie->serverList->getServerBySID( s->Sid );
481 if ( server ) server->subscribeToContentDirectory();
486 msg_Dbg( cookie->serviceDiscovery, "%s:%d: DEBUG: UNHANDLED EVENT ( TYPE=%d )", __FILE__, __LINE__, eventType );
490 return UPNP_E_SUCCESS;
494 // Class implementations...
498 void MediaServer::parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie )
500 if ( !doc ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
501 if ( !location ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: NULL", __FILE__, __LINE__ ); return; }
503 const char* baseURL = location;
505 // Try to extract baseURL
507 IXML_NodeList* urlList = ixmlDocument_getElementsByTagName( doc, "baseURL" );
510 if ( IXML_Node* urlNode = ixmlNodeList_item( urlList, 0 ) )
512 IXML_Node* textNode = ixmlNode_getFirstChild( urlNode );
513 if ( textNode ) baseURL = ixmlNode_getNodeValue( textNode );
516 ixmlNodeList_free( urlList );
521 IXML_NodeList* deviceList = ixmlDocument_getElementsByTagName( doc, "device" );
525 for ( unsigned int i = 0; i < ixmlNodeList_length( deviceList ); i++ )
527 IXML_Element* deviceElement = ( IXML_Element* )ixmlNodeList_item( deviceList, i );
529 const char* deviceType = xml_getChildElementValue( deviceElement, "deviceType" );
530 if ( !deviceType ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no deviceType!", __FILE__, __LINE__ ); continue; }
531 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, deviceType ) != 0 ) continue;
533 const char* UDN = xml_getChildElementValue( deviceElement, "UDN" );
534 if ( !UDN ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no UDN!", __FILE__, __LINE__ ); continue; }
535 if ( cookie->serverList->getServer( UDN ) != 0 ) continue;
537 const char* friendlyName = xml_getChildElementValue( deviceElement, "friendlyName" );
538 if ( !friendlyName ) { msg_Dbg( cookie->serviceDiscovery, "%s:%d: no friendlyName!", __FILE__, __LINE__ ); continue; }
540 MediaServer* server = new MediaServer( UDN, friendlyName, cookie );
541 if ( !cookie->serverList->addServer( server ) ) {
548 // Check for ContentDirectory service...
550 IXML_NodeList* serviceList = ixmlElement_getElementsByTagName( deviceElement, "service" );
553 for ( unsigned int j = 0; j < ixmlNodeList_length( serviceList ); j++ )
555 IXML_Element* serviceElement = ( IXML_Element* )ixmlNodeList_item( serviceList, j );
557 const char* serviceType = xml_getChildElementValue( serviceElement, "serviceType" );
558 if ( !serviceType ) continue;
559 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE, serviceType ) != 0 ) continue;
561 const char* eventSubURL = xml_getChildElementValue( serviceElement, "eventSubURL" );
562 if ( !eventSubURL ) continue;
564 const char* controlURL = xml_getChildElementValue( serviceElement, "controlURL" );
565 if ( !controlURL ) continue;
567 // Try to subscribe to ContentDirectory service
569 char* url = ( char* )malloc( strlen( baseURL ) + strlen( eventSubURL ) + 1 );
572 char* s1 = strdup( baseURL );
573 char* s2 = strdup( eventSubURL );
575 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
577 // msg_Dbg( cookie->serviceDiscovery, "CDS EVENT URL: %s", url );
579 server->setContentDirectoryEventURL( url );
580 server->subscribeToContentDirectory();
588 // Try to browse content directory...
590 url = ( char* )malloc( strlen( baseURL ) + strlen( controlURL ) + 1 );
593 char* s1 = strdup( baseURL );
594 char* s2 = strdup( controlURL );
596 if ( UpnpResolveURL( s1, s2, url ) == UPNP_E_SUCCESS )
598 // msg_Dbg( cookie->serviceDiscovery, "CDS CTRL URL: %s", url );
600 server->setContentDirectoryControlURL( url );
601 server->fetchContents();
610 ixmlNodeList_free( serviceList );
614 ixmlNodeList_free( deviceList );
618 MediaServer::MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie )
623 _friendlyName = friendlyName;
629 MediaServer::~MediaServer()
633 playlist_NodeDelete( _cookie->serviceDiscovery->p_sys->p_playlist,
642 const char* MediaServer::getUDN() const
644 const char* s = _UDN.c_str();
648 const char* MediaServer::getFriendlyName() const
650 const char* s = _friendlyName.c_str();
654 void MediaServer::setContentDirectoryEventURL( const char* url )
656 _contentDirectoryEventURL = url;
659 const char* MediaServer::getContentDirectoryEventURL() const
661 const char* s = _contentDirectoryEventURL.c_str();
665 void MediaServer::setContentDirectoryControlURL( const char* url )
667 _contentDirectoryControlURL = url;
670 const char* MediaServer::getContentDirectoryControlURL() const
672 return _contentDirectoryControlURL.c_str();
675 void MediaServer::subscribeToContentDirectory()
677 const char* url = getContentDirectoryEventURL();
678 if ( !url || strcmp( url, "" ) == 0 )
680 msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" );
687 int res = UpnpSubscribe( _cookie->clientHandle, url, &timeOut, sid );
689 if ( res == UPNP_E_SUCCESS )
691 _subscriptionTimeOut = timeOut;
692 memcpy( _subscriptionID, sid, sizeof( Upnp_SID ) );
696 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: WARNING: '%s': %s", __FILE__, __LINE__, getFriendlyName(), UpnpGetErrorMessage( res ) );
700 IXML_Document* MediaServer::_browseAction( const char* pObjectID, const char* pBrowseFlag, const char* pFilter,
701 const char* pStartingIndex, const char* pRequestedCount, const char* pSortCriteria )
703 IXML_Document* action = 0;
704 IXML_Document* response = 0;
706 const char* url = getContentDirectoryControlURL();
707 if ( !url || strcmp( url, "" ) == 0 ) { msg_Dbg( _cookie->serviceDiscovery, "No subscription url set!" ); return 0; }
709 char* ObjectID = strdup( pObjectID );
710 char* BrowseFlag = strdup( pBrowseFlag );
711 char* Filter = strdup( pFilter );
712 char* StartingIndex = strdup( pStartingIndex );
713 char* RequestedCount = strdup( pRequestedCount );
714 char* SortCriteria = strdup( pSortCriteria );
716 char* serviceType = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
720 res = UpnpAddToAction( &action, "Browse", serviceType, "ObjectID", ObjectID );
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, "BrowseFlag", BrowseFlag );
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, "Filter", Filter );
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, "StartingIndex", StartingIndex );
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, "RequestedCount", RequestedCount );
733 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
735 res = UpnpAddToAction( &action, "Browse", serviceType, "SortCriteria", SortCriteria );
736 if ( res != UPNP_E_SUCCESS ) { /* msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) ); */ goto browseActionCleanup; }
738 res = UpnpSendAction( _cookie->clientHandle,
740 CONTENT_DIRECTORY_SERVICE_TYPE,
744 if ( res != UPNP_E_SUCCESS )
746 msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR: %s", __FILE__, __LINE__, UpnpGetErrorMessage( res ) );
747 ixmlDocument_free( response );
756 free( StartingIndex );
757 free( RequestedCount );
758 free( SortCriteria );
762 ixmlDocument_free( action );
766 void MediaServer::fetchContents()
768 Container* root = new Container( 0, "0", getFriendlyName() );
769 _fetchContents( root );
773 vlc_mutex_lock( &_cookie->serviceDiscovery->p_sys->p_playlist->object_lock );
775 playlist_NodeEmpty( _cookie->serviceDiscovery->p_sys->p_playlist,
779 vlc_mutex_unlock( &_cookie->serviceDiscovery->p_sys->p_playlist->object_lock );
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 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
869 Container* container = parent->getContainer( i );
870 playlist_item_t* parentNode = parent->getPlaylistNode();
872 char* title = strdup( container->getTitle() );
873 playlist_item_t* node = playlist_NodeCreate( _cookie->serviceDiscovery->p_sys->p_playlist,
879 container->setPlaylistNode( node );
880 _buildPlaylist( container );
883 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
885 Item* item = parent->getItem( i );
886 playlist_item_t* parentNode = parent->getPlaylistNode();
888 playlist_item_t* node = playlist_ItemNew( _cookie->serviceDiscovery,
892 playlist_NodeAddItem( _cookie->serviceDiscovery->p_sys->p_playlist,
895 parentNode, PLAYLIST_APPEND, PLAYLIST_END );
897 item->setPlaylistNode( node );
901 void MediaServer::setPlaylistNode( playlist_item_t* playlistNode )
903 _playlistNode = playlistNode;
906 bool MediaServer::compareSID( const char* sid )
908 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
912 // MediaServerList...
914 MediaServerList::MediaServerList( Cookie* cookie )
919 MediaServerList::~MediaServerList()
921 for ( unsigned int i = 0; i < _list.size(); i++ )
927 bool MediaServerList::addServer( MediaServer* s )
929 if ( getServer( s->getUDN() ) != 0 ) return false;
931 msg_Dbg( _cookie->serviceDiscovery, "Adding server '%s'", s->getFriendlyName() );
933 _list.push_back( s );
935 char* name = strdup( s->getFriendlyName() );
936 playlist_item_t* node = playlist_NodeCreate( _cookie->serviceDiscovery->p_sys->p_playlist,
939 _cookie->serviceDiscovery->p_sys->p_node );
941 s->setPlaylistNode( node );
946 MediaServer* MediaServerList::getServer( const char* UDN )
948 MediaServer* result = 0;
950 for ( unsigned int i = 0; i < _list.size(); i++ )
952 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
962 MediaServer* MediaServerList::getServerBySID( const char* sid )
964 MediaServer* server = 0;
966 for ( unsigned int i = 0; i < _list.size(); i++ )
968 if ( _list[i]->compareSID( sid ) )
978 void MediaServerList::removeServer( const char* UDN )
980 MediaServer* server = getServer( UDN );
981 if ( !server ) return;
983 msg_Dbg( _cookie->serviceDiscovery, "Removing server '%s'", server->getFriendlyName() );
985 std::vector<MediaServer*>::iterator it;
986 for ( it = _list.begin(); it != _list.end(); it++ )
1000 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
1004 _objectID = objectID;
1006 _resource = resource;
1011 const char* Item::getObjectID() const
1013 return _objectID.c_str();
1016 const char* Item::getTitle() const
1018 return _title.c_str();
1021 const char* Item::getResource() const
1023 return _resource.c_str();
1026 void Item::setPlaylistNode( playlist_item_t* node )
1028 _playlistNode = node;
1031 playlist_item_t* Item::getPlaylistNode() const
1033 return _playlistNode;
1039 Container::Container( Container* parent, const char* objectID, const char* title )
1043 _objectID = objectID;
1049 Container::~Container()
1051 for ( unsigned int i = 0; i < _containers.size(); i++ )
1053 delete _containers[i];
1056 for ( unsigned int i = 0; i < _items.size(); i++ )
1062 void Container::addItem( Item* item )
1064 _items.push_back( item );
1067 void Container::addContainer( Container* container )
1069 _containers.push_back( container );
1072 const char* Container::getObjectID() const
1074 return _objectID.c_str();
1077 const char* Container::getTitle() const
1079 return _title.c_str();
1082 unsigned int Container::getNumItems() const
1084 return _items.size();
1087 unsigned int Container::getNumContainers() const
1089 return _containers.size();
1092 Item* Container::getItem( unsigned int i ) const
1094 if ( i < _items.size() ) return _items[i];
1098 Container* Container::getContainer( unsigned int i ) const
1100 if ( i < _containers.size() ) return _containers[i];
1104 void Container::setPlaylistNode( playlist_item_t* node )
1106 _playlistNode = node;
1109 playlist_item_t* Container::getPlaylistNode() const
1111 return _playlistNode;