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_cat;
52 playlist_item_t *p_node;
53 playlist_t *p_playlist;
59 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
60 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
66 class MediaServerList;
70 // Cookie that is passed to the callback
74 services_discovery_t* serviceDiscovery;
75 UpnpClient_Handle clientHandle;
76 MediaServerList* serverList;
80 // Class definitions...
88 vlc_mutex_init( c->serviceDiscovery, &_mutex );
93 vlc_mutex_destroy( &_mutex );
96 void lock() { vlc_mutex_lock( &_mutex ); }
97 void unlock() { vlc_mutex_unlock( &_mutex ); }
108 Locker( Lockable* l )
128 static void parseDeviceDescription( IXML_Document* doc, const char* location, Cookie* cookie );
130 MediaServer( const char* UDN, const char* friendlyName, Cookie* cookie );
133 const char* getUDN() const;
134 const char* getFriendlyName() const;
136 void setContentDirectoryEventURL( const char* url );
137 const char* getContentDirectoryEventURL() const;
139 void setContentDirectoryControlURL( const char* url );
140 const char* getContentDirectoryControlURL() const;
142 void subscribeToContentDirectory();
143 void fetchContents();
145 void setPlaylistNode( playlist_item_t* node );
147 bool compareSID( const char* sid );
151 bool _fetchContents( Container* parent );
152 void _buildPlaylist( Container* container );
153 IXML_Document* _browseAction( const char*, const char*, const char*, const char*, const char*, const char* );
157 Container* _contents;
158 playlist_item_t* _playlistNode;
161 std::string _friendlyName;
163 std::string _contentDirectoryEventURL;
164 std::string _contentDirectoryControlURL;
166 int _subscriptionTimeOut;
167 Upnp_SID _subscriptionID;
171 class MediaServerList
175 MediaServerList( Cookie* cookie );
178 bool addServer( MediaServer* s );
179 void removeServer( const char* UDN );
181 MediaServer* getServer( const char* UDN );
182 MediaServer* getServerBySID( const char* );
188 std::vector<MediaServer*> _list;
196 Item( Container* parent, const char* objectID, const char* title, const char* resource );
198 const char* getObjectID() const;
199 const char* getTitle() const;
200 const char* getResource() const;
202 void setPlaylistNode( playlist_item_t* node );
203 playlist_item_t* getPlaylistNode() const ;
207 playlist_item_t* _playlistNode;
210 std::string _objectID;
212 std::string _resource;
220 Container( Container* parent, const char* objectID, const char* title );
223 void addItem( Item* item );
224 void addContainer( Container* container );
226 const char* getObjectID() const;
227 const char* getTitle() const;
229 unsigned int getNumItems() const;
230 unsigned int getNumContainers() const;
232 Item* getItem( unsigned int i ) const;
233 Container* getContainer( unsigned int i ) const;
235 void setPlaylistNode( playlist_item_t* node );
236 playlist_item_t* getPlaylistNode() const;
240 playlist_item_t* _playlistNode;
244 std::string _objectID;
246 std::vector<Item*> _items;
247 std::vector<Container*> _containers;
251 // VLC callback prototypes
253 static int Open( vlc_object_t* );
254 static void Close( vlc_object_t* );
255 static void Run( services_discovery_t *p_sd );
260 set_shortname( "UPnP" );
261 set_description( _( "Universal Plug'n'Play discovery ( Intel SDK )" ) );
262 set_category( CAT_PLAYLIST );
263 set_subcategory( SUBCAT_PLAYLIST_SD );
264 set_capability( "services_discovery", 0 );
265 set_callbacks( Open, Close );
269 // More prototypes...
271 static Lockable* CallbackLock;
272 static int Callback( Upnp_EventType eventType, void* event, void* pCookie );
274 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName );
275 IXML_Document* parseBrowseResult( IXML_Document* doc );
280 static int Open( vlc_object_t *p_this )
282 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
283 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
284 malloc( sizeof( services_discovery_sys_t ) );
289 /* Create our playlist node */
290 p_sys->p_playlist = ( playlist_t * )vlc_object_find( p_sd,
293 if( !p_sys->p_playlist )
295 msg_Warn( p_sd, "unable to find playlist, cancelling UPnP listening" );
299 playlist_NodesPairCreate( p_sys->p_playlist, _("Devices"),
300 &p_sys->p_node_cat, &p_sys->p_node,
306 static void Close( vlc_object_t *p_this )
308 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
309 services_discovery_sys_t *p_sys = p_sd->p_sys;
311 if( p_sys->p_playlist )
313 playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node, VLC_TRUE,
315 playlist_NodeDelete( p_sys->p_playlist, p_sys->p_node_cat, VLC_TRUE,
317 vlc_object_release( p_sys->p_playlist );
323 static void Run( services_discovery_t* p_sd )
327 res = UpnpInit( 0, 0 );
328 if( res != UPNP_E_SUCCESS )
330 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
335 cookie.serviceDiscovery = p_sd;
336 cookie.serverList = new MediaServerList( &cookie );
338 CallbackLock = new Lockable( &cookie );
340 res = UpnpRegisterClient( Callback, &cookie, &cookie.clientHandle );
341 if( res != UPNP_E_SUCCESS )
343 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
347 res = UpnpSearchAsync( cookie.clientHandle, 5, MEDIA_SERVER_DEVICE_TYPE, &cookie );
348 if( res != UPNP_E_SUCCESS )
350 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
354 msg_Dbg( p_sd, "UPnP discovery started" );
355 while( !p_sd->b_die )
360 msg_Dbg( p_sd, "UPnP discovery stopped" );
364 delete cookie.serverList;
369 // XML utility functions:
371 // Returns the value of a child element, or 0 on error
372 const char* xml_getChildElementValue( IXML_Element* parent, const char* tagName )
374 if ( !parent ) return 0;
375 if ( !tagName ) return 0;
377 char* s = strdup( tagName );
378 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
380 if ( !nodeList ) return 0;
382 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
383 ixmlNodeList_free( nodeList );
384 if ( !element ) return 0;
386 IXML_Node* textNode = ixmlNode_getFirstChild( element );
387 if ( !textNode ) return 0;
389 return ixmlNode_getNodeValue( textNode );
392 // Extracts the result document from a SOAP response
393 IXML_Document* parseBrowseResult( IXML_Document* doc )
395 if ( !doc ) return 0;
397 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc, "Result" );
398 if ( !resultList ) return 0;
400 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
402 ixmlNodeList_free( resultList );
404 if ( !resultNode ) return 0;
406 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
407 if ( !textNode ) return 0;
409 const char* resultString = ixmlNode_getNodeValue( textNode );
410 char* resultXML = strdup( resultString );
412 resolve_xml_special_chars( resultXML );
414 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
422 // Handles all UPnP events
423 static int Callback( Upnp_EventType eventType, void* event, void* pCookie )
425 Locker locker( CallbackLock );
427 Cookie* cookie = ( Cookie* )pCookie;
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 playlist_NodeDelete( _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 _fetchContents( root );
777 vlc_mutex_lock( &_cookie->serviceDiscovery->p_sys->p_playlist->object_lock );
779 playlist_NodeEmpty( _cookie->serviceDiscovery->p_sys->p_playlist,
783 vlc_mutex_unlock( &_cookie->serviceDiscovery->p_sys->p_playlist->object_lock );
789 _contents->setPlaylistNode( _playlistNode );
791 _buildPlaylist( _contents );
794 bool MediaServer::_fetchContents( Container* parent )
796 if (!parent) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: parent==NULL", __FILE__, __LINE__ ); return false; }
798 IXML_Document* response = _browseAction( parent->getObjectID(), "BrowseDirectChildren", "*", "0", "0", "" );
799 if ( !response ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
801 IXML_Document* result = parseBrowseResult( response );
802 ixmlDocument_free( response );
803 if ( !result ) { msg_Dbg( _cookie->serviceDiscovery, "%s:%d: ERROR!", __FILE__, __LINE__ ); return false; }
805 IXML_NodeList* containerNodeList = ixmlDocument_getElementsByTagName( result, "container" );
806 if ( containerNodeList )
808 for ( unsigned int i = 0; i < ixmlNodeList_length( containerNodeList ); i++ )
810 IXML_Element* containerElement = ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
812 const char* objectID = ixmlElement_getAttribute( containerElement, "id" );
813 if ( !objectID ) continue;
815 const char* childCountStr = ixmlElement_getAttribute( containerElement, "childCount" );
816 if ( !childCountStr ) continue;
817 int childCount = atoi( childCountStr );
819 const char* title = xml_getChildElementValue( containerElement, "dc:title" );
820 if ( !title ) continue;
822 const char* resource = xml_getChildElementValue( containerElement, "res" );
824 if ( resource && childCount < 1 )
826 Item* item = new Item( parent, objectID, title, resource );
827 parent->addItem( item );
831 Container* container = new Container( parent, objectID, title );
832 parent->addContainer( container );
834 if ( childCount > 0 ) _fetchContents( container );
838 ixmlNodeList_free( containerNodeList );
841 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result, "item" );
844 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
846 IXML_Element* itemElement = ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
848 const char* objectID = ixmlElement_getAttribute( itemElement, "id" );
849 if ( !objectID ) continue;
851 const char* title = xml_getChildElementValue( itemElement, "dc:title" );
852 if ( !title ) continue;
854 const char* resource = xml_getChildElementValue( itemElement, "res" );
855 if ( !resource ) continue;
857 Item* item = new Item( parent, objectID, title, resource );
858 parent->addItem( item );
861 ixmlNodeList_free( itemNodeList );
864 ixmlDocument_free( result );
869 void MediaServer::_buildPlaylist( Container* parent )
871 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
873 Container* container = parent->getContainer( i );
874 playlist_item_t* parentNode = parent->getPlaylistNode();
876 char* title = strdup( container->getTitle() );
877 playlist_item_t* node = playlist_NodeCreate( _cookie->serviceDiscovery->p_sys->p_playlist,
882 container->setPlaylistNode( node );
883 _buildPlaylist( container );
886 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
888 Item* item = parent->getItem( i );
889 playlist_item_t* parentNode = parent->getPlaylistNode();
891 playlist_item_t* node = playlist_ItemNew( _cookie->serviceDiscovery,
895 playlist_NodeAddItem( _cookie->serviceDiscovery->p_sys->p_playlist,
897 parentNode, PLAYLIST_APPEND, PLAYLIST_END );
899 item->setPlaylistNode( node );
903 void MediaServer::setPlaylistNode( playlist_item_t* playlistNode )
905 _playlistNode = playlistNode;
908 bool MediaServer::compareSID( const char* sid )
910 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
914 // MediaServerList...
916 MediaServerList::MediaServerList( Cookie* cookie )
921 MediaServerList::~MediaServerList()
923 for ( unsigned int i = 0; i < _list.size(); i++ )
929 bool MediaServerList::addServer( MediaServer* s )
931 if ( getServer( s->getUDN() ) != 0 ) return false;
933 msg_Dbg( _cookie->serviceDiscovery, "Adding server '%s'", s->getFriendlyName() );
935 _list.push_back( s );
937 char* name = strdup( s->getFriendlyName() );
938 playlist_item_t* node = playlist_NodeCreate( _cookie->serviceDiscovery->p_sys->p_playlist,
940 _cookie->serviceDiscovery->p_sys->p_node );
942 s->setPlaylistNode( node );
947 MediaServer* MediaServerList::getServer( const char* UDN )
949 MediaServer* result = 0;
951 for ( unsigned int i = 0; i < _list.size(); i++ )
953 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
963 MediaServer* MediaServerList::getServerBySID( const char* sid )
965 MediaServer* server = 0;
967 for ( unsigned int i = 0; i < _list.size(); i++ )
969 if ( _list[i]->compareSID( sid ) )
979 void MediaServerList::removeServer( const char* UDN )
981 MediaServer* server = getServer( UDN );
982 if ( !server ) return;
984 msg_Dbg( _cookie->serviceDiscovery, "Removing server '%s'", server->getFriendlyName() );
986 std::vector<MediaServer*>::iterator it;
987 for ( it = _list.begin(); it != _list.end(); it++ )
1001 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
1005 _objectID = objectID;
1007 _resource = resource;
1012 const char* Item::getObjectID() const
1014 return _objectID.c_str();
1017 const char* Item::getTitle() const
1019 return _title.c_str();
1022 const char* Item::getResource() const
1024 return _resource.c_str();
1027 void Item::setPlaylistNode( playlist_item_t* node )
1029 _playlistNode = node;
1032 playlist_item_t* Item::getPlaylistNode() const
1034 return _playlistNode;
1040 Container::Container( Container* parent, const char* objectID, const char* title )
1044 _objectID = objectID;
1050 Container::~Container()
1052 for ( unsigned int i = 0; i < _containers.size(); i++ )
1054 delete _containers[i];
1057 for ( unsigned int i = 0; i < _items.size(); i++ )
1063 void Container::addItem( Item* item )
1065 _items.push_back( item );
1068 void Container::addContainer( Container* container )
1070 _containers.push_back( container );
1073 const char* Container::getObjectID() const
1075 return _objectID.c_str();
1078 const char* Container::getTitle() const
1080 return _title.c_str();
1083 unsigned int Container::getNumItems() const
1085 return _items.size();
1088 unsigned int Container::getNumContainers() const
1090 return _containers.size();
1093 Item* Container::getItem( unsigned int i ) const
1095 if ( i < _items.size() ) return _items[i];
1099 Container* Container::getContainer( unsigned int i ) const
1101 if ( i < _containers.size() ) return _containers[i];
1105 void Container::setPlaylistNode( playlist_item_t* node )
1107 _playlistNode = node;
1110 playlist_item_t* Container::getPlaylistNode() const
1112 return _playlistNode;