1 /*****************************************************************************
2 * upnp.cpp : UPnP discovery module (libupnp)
3 *****************************************************************************
4 * Copyright (C) 2004-2011 the VideoLAN team
7 * Authors: RĂ©mi Denis-Courmont <rem # videolan.org> (original plugin)
8 * Christian Henz <henz # c-lab.de>
9 * Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
11 * UPnP Plugin using the Intel SDK (libupnp) instead of CyberLink
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #define __STDC_CONSTANT_MACROS 1
37 #include <vlc_plugin.h>
38 #include <vlc_services_discovery.h>
45 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
46 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
51 struct services_discovery_sys_t
53 UpnpClient_Handle client_handle;
54 MediaServerList* p_server_list;
55 vlc_mutex_t callback_lock;
59 * VLC callback prototypes
61 static int Open( vlc_object_t* );
62 static void Close( vlc_object_t* );
63 VLC_SD_PROBE_HELPER( "upnp", "Universal Plug'n'Play", SD_CAT_LAN )
69 set_shortname( "UPnP" );
70 set_description( N_( "Universal Plug'n'Play" ) );
71 set_category( CAT_PLAYLIST );
72 set_subcategory( SUBCAT_PLAYLIST_SD );
73 set_capability( "services_discovery", 0 );
74 set_callbacks( Open, Close );
76 VLC_SD_PROBE_SUBMODULE
82 static int Callback( Upnp_EventType event_type, void* p_event, void* p_user_data );
84 const char* xml_getChildElementValue( IXML_Element* p_parent,
85 const char* psz_tag_name );
87 const char* xml_getChildElementAttributeValue( IXML_Element* p_parent,
88 const char* psz_tag_name_,
89 const char* psz_attribute_ );
91 IXML_Document* parseBrowseResult( IXML_Document* p_doc );
94 * Initializes UPNP instance.
96 static int Open( vlc_object_t *p_this )
99 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
100 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
101 calloc( 1, sizeof( services_discovery_sys_t ) );
103 if( !( p_sd->p_sys = p_sys ) )
106 /* Initialize on first IPv4-capable adapter and first open port
107 * TODO: use UpnpInit2() to utilize IPv6.
109 i_res = UpnpInit( 0, 0 );
110 if( i_res != UPNP_E_SUCCESS )
112 msg_Err( p_sd, "Initialization failed: %s", UpnpGetErrorMessage( i_res ) );
117 p_sys->p_server_list = new MediaServerList( p_sd );
118 vlc_mutex_init( &p_sys->callback_lock );
120 /* Register a control point */
121 i_res = UpnpRegisterClient( Callback, p_sd, &p_sys->client_handle );
122 if( i_res != UPNP_E_SUCCESS )
124 msg_Err( p_sd, "Client registration failed: %s", UpnpGetErrorMessage( i_res ) );
125 Close( (vlc_object_t*) p_sd );
129 /* Search for media servers */
130 i_res = UpnpSearchAsync( p_sys->client_handle, 5,
131 MEDIA_SERVER_DEVICE_TYPE, p_sd );
132 if( i_res != UPNP_E_SUCCESS )
134 msg_Err( p_sd, "Error sending search request: %s", UpnpGetErrorMessage( i_res ) );
135 Close( (vlc_object_t*) p_sd );
139 i_res = UpnpSetMaxContentLength( 0 );
140 if( i_res != UPNP_E_SUCCESS )
142 msg_Err( p_sd, "Failed to set maximum content length: %s", UpnpGetErrorMessage( i_res ) );
143 Close( (vlc_object_t*) p_sd );
151 * Releases resources.
153 static void Close( vlc_object_t *p_this )
155 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
157 UpnpUnRegisterClient( p_sd->p_sys->client_handle );
160 delete p_sd->p_sys->p_server_list;
161 vlc_mutex_destroy( &p_sd->p_sys->callback_lock );
166 /* XML utility functions */
169 * Returns the value of a child element, or NULL on error
171 const char* xml_getChildElementValue( IXML_Element* p_parent,
172 const char* psz_tag_name_ )
174 if ( !p_parent ) return NULL;
175 if ( !psz_tag_name_ ) return NULL;
177 IXML_NodeList* p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name_ );
178 if ( !p_node_list ) return NULL;
180 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
181 ixmlNodeList_free( p_node_list );
182 if ( !p_element ) return NULL;
184 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_element );
185 if ( !p_text_node ) return NULL;
187 return ixmlNode_getNodeValue( p_text_node );
191 * Returns the value of a child element's attribute, or NULL on error
193 const char* xml_getChildElementAttributeValue( IXML_Element* p_parent,
194 const char* psz_tag_name_,
195 const char* psz_attribute_ )
197 if ( !p_parent ) return NULL;
198 if ( !psz_tag_name_ ) return NULL;
199 if ( !psz_attribute_ ) return NULL;
201 IXML_NodeList* p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name_ );
202 if ( !p_node_list ) return NULL;
204 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
205 ixmlNodeList_free( p_node_list );
206 if ( !p_element ) return NULL;
208 return ixmlElement_getAttribute( (IXML_Element*) p_element, psz_attribute_ );
212 * Extracts the result document from a SOAP response
214 IXML_Document* parseBrowseResult( IXML_Document* p_doc )
216 ixmlRelaxParser( 1 );
218 if ( !p_doc ) return 0;
220 IXML_NodeList* p_result_list = ixmlDocument_getElementsByTagName( p_doc,
223 if ( !p_result_list ) return 0;
225 IXML_Node* p_result_node = ixmlNodeList_item( p_result_list, 0 );
227 ixmlNodeList_free( p_result_list );
229 if ( !p_result_node ) return 0;
231 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_result_node );
232 if ( !p_text_node ) return 0;
234 const char* psz_result_string = ixmlNode_getNodeValue( p_text_node );
236 IXML_Document* p_browse_doc = ixmlParseBuffer( psz_result_string );
243 * Handles all UPnP events
245 static int Callback( Upnp_EventType event_type, void* p_event, void* p_user_data )
247 services_discovery_t* p_sd = ( services_discovery_t* ) p_user_data;
248 services_discovery_sys_t* p_sys = p_sd->p_sys;
249 vlc_mutex_locker locker( &p_sys->callback_lock );
253 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
254 case UPNP_DISCOVERY_SEARCH_RESULT:
256 struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
258 IXML_Document *p_description_doc = 0;
261 i_res = UpnpDownloadXmlDoc( p_discovery->Location, &p_description_doc );
262 if ( i_res != UPNP_E_SUCCESS )
264 msg_Warn( p_sd, "Could not download device description! "
265 "Fetching data from %s failed: %s",
266 p_discovery->Location, UpnpGetErrorMessage( i_res ) );
270 MediaServer::parseDeviceDescription( p_description_doc,
271 p_discovery->Location, p_sd );
273 ixmlDocument_free( p_description_doc );
277 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
279 struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
281 p_sys->p_server_list->removeServer( p_discovery->DeviceId );
286 case UPNP_EVENT_RECEIVED:
288 Upnp_Event* p_e = ( Upnp_Event* )p_event;
290 MediaServer* p_server = p_sys->p_server_list->getServerBySID( p_e->Sid );
291 if ( p_server ) p_server->fetchContents();
295 case UPNP_EVENT_AUTORENEWAL_FAILED:
296 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
300 Upnp_Event_Subscribe* p_s = ( Upnp_Event_Subscribe* )p_event;
302 MediaServer* p_server = p_sys->p_server_list->getServerBySID( p_s->Sid );
303 if ( p_server ) p_server->subscribeToContentDirectory();
307 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
308 msg_Warn( p_sd, "subscription complete" );
311 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
312 msg_Warn( p_sd, "search timeout" );
316 msg_Err( p_sd, "Unhandled event, please report ( type=%d )", event_type );
320 return UPNP_E_SUCCESS;
325 * Local class implementations.
332 void MediaServer::parseDeviceDescription( IXML_Document* p_doc,
333 const char* p_location,
334 services_discovery_t* p_sd )
338 msg_Err( p_sd, "Null IXML_Document" );
344 msg_Err( p_sd, "Null location" );
348 const char* psz_base_url = p_location;
350 /* Try to extract baseURL */
351 IXML_NodeList* p_url_list = ixmlDocument_getElementsByTagName( p_doc, "baseURL" );
355 if ( IXML_Node* p_url_node = ixmlNodeList_item( p_url_list, 0 ) )
357 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_url_node );
358 if ( p_text_node ) psz_base_url = ixmlNode_getNodeValue( p_text_node );
361 ixmlNodeList_free( p_url_list );
365 IXML_NodeList* p_device_list =
366 ixmlDocument_getElementsByTagName( p_doc, "device" );
370 for ( unsigned int i = 0; i < ixmlNodeList_length( p_device_list ); i++ )
372 IXML_Element* p_device_element =
373 ( IXML_Element* ) ixmlNodeList_item( p_device_list, i );
375 const char* psz_device_type = xml_getChildElementValue( p_device_element,
377 if ( !psz_device_type )
379 msg_Warn( p_sd, "No deviceType found!" );
383 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, psz_device_type ) != 0 )
386 const char* psz_udn = xml_getChildElementValue( p_device_element, "UDN" );
389 msg_Warn( p_sd, "No UDN!" );
393 /* Check if server is already added */
394 if ( p_sd->p_sys->p_server_list->getServer( psz_udn ) != 0 )
396 msg_Warn( p_sd, "Server with uuid '%s' already exists.", psz_udn );
400 const char* psz_friendly_name =
401 xml_getChildElementValue( p_device_element,
404 if ( !psz_friendly_name )
406 msg_Dbg( p_sd, "No friendlyName!" );
410 MediaServer* p_server = new MediaServer( psz_udn, psz_friendly_name, p_sd );
412 if ( !p_sd->p_sys->p_server_list->addServer( p_server ) )
419 /* Check for ContentDirectory service. */
420 IXML_NodeList* p_service_list =
421 ixmlElement_getElementsByTagName( p_device_element,
423 if ( p_service_list )
425 for ( unsigned int j = 0;
426 j < ixmlNodeList_length( p_service_list ); j++ )
428 IXML_Element* p_service_element =
429 ( IXML_Element* ) ixmlNodeList_item( p_service_list, j );
431 const char* psz_service_type =
432 xml_getChildElementValue( p_service_element,
434 if ( !psz_service_type )
436 msg_Warn( p_sd, "No service type found." );
440 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE,
441 psz_service_type ) != 0 )
444 const char* psz_event_sub_url =
445 xml_getChildElementValue( p_service_element,
447 if ( !psz_event_sub_url )
449 msg_Warn( p_sd, "No event subscription url found." );
453 const char* psz_control_url =
454 xml_getChildElementValue( p_service_element,
456 if ( !psz_control_url )
458 msg_Warn( p_sd, "No control url found." );
462 /* Try to subscribe to ContentDirectory service */
464 char* psz_url = ( char* ) malloc( strlen( psz_base_url ) +
465 strlen( psz_event_sub_url ) + 1 );
468 if ( UpnpResolveURL( psz_base_url, psz_event_sub_url, psz_url ) ==
471 p_server->setContentDirectoryEventURL( psz_url );
472 p_server->subscribeToContentDirectory();
478 /* Try to browse content directory. */
480 psz_url = ( char* ) malloc( strlen( psz_base_url ) +
481 strlen( psz_control_url ) + 1 );
484 if ( UpnpResolveURL( psz_base_url, psz_control_url, psz_url ) ==
487 p_server->setContentDirectoryControlURL( psz_url );
488 p_server->fetchContents();
494 ixmlNodeList_free( p_service_list );
497 ixmlNodeList_free( p_device_list );
501 MediaServer::MediaServer( const char* psz_udn,
502 const char* psz_friendly_name,
503 services_discovery_t* p_sd )
508 _friendly_name = psz_friendly_name;
511 _p_input_item = NULL;
514 MediaServer::~MediaServer()
519 const char* MediaServer::getUDN() const
524 const char* MediaServer::getFriendlyName() const
526 return _friendly_name.c_str();
529 void MediaServer::setContentDirectoryEventURL( const char* psz_url )
531 _content_directory_event_url = psz_url;
534 const char* MediaServer::getContentDirectoryEventURL() const
536 return _content_directory_event_url.c_str();
539 void MediaServer::setContentDirectoryControlURL( const char* psz_url )
541 _content_directory_control_url = psz_url;
544 const char* MediaServer::getContentDirectoryControlURL() const
546 return _content_directory_control_url.c_str();
550 * Subscribes current client handle to Content Directory Service.
551 * CDS exports the server shares to clients.
553 void MediaServer::subscribeToContentDirectory()
555 const char* psz_url = getContentDirectoryEventURL();
558 msg_Dbg( _p_sd, "No subscription url set!" );
562 int i_timeout = 1810;
565 int i_res = UpnpSubscribe( _p_sd->p_sys->client_handle, psz_url, &i_timeout, sid );
567 if ( i_res == UPNP_E_SUCCESS )
569 _i_subscription_timeout = i_timeout;
570 memcpy( _subscription_id, sid, sizeof( Upnp_SID ) );
574 msg_Dbg( _p_sd, "Subscribe failed: '%s': %s",
575 getFriendlyName(), UpnpGetErrorMessage( i_res ) );
579 * Constructs UpnpAction to browse available content.
581 IXML_Document* MediaServer::_browseAction( const char* psz_object_id_,
582 const char* psz_browser_flag_,
583 const char* psz_filter_,
584 const char* psz_starting_index_,
585 const char* psz_requested_count_,
586 const char* psz_sort_criteria_ )
588 IXML_Document* p_action = 0;
589 IXML_Document* p_response = 0;
590 const char* psz_url = getContentDirectoryControlURL();
594 msg_Dbg( _p_sd, "No subscription url set!" );
598 char* psz_service_type = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
602 i_res = UpnpAddToAction( &p_action, "Browse",
603 psz_service_type, "ObjectID", psz_object_id_ );
605 if ( i_res != UPNP_E_SUCCESS )
607 msg_Dbg( _p_sd, "AddToAction 'ObjectID' failed: %s",
608 UpnpGetErrorMessage( i_res ) );
609 goto browseActionCleanup;
612 i_res = UpnpAddToAction( &p_action, "Browse",
613 psz_service_type, "BrowseFlag", psz_browser_flag_ );
615 if ( i_res != UPNP_E_SUCCESS )
617 msg_Dbg( _p_sd, "AddToAction 'BrowseFlag' failed: %s",
618 UpnpGetErrorMessage( i_res ) );
619 goto browseActionCleanup;
622 i_res = UpnpAddToAction( &p_action, "Browse",
623 psz_service_type, "Filter", psz_filter_ );
625 if ( i_res != UPNP_E_SUCCESS )
627 msg_Dbg( _p_sd, "AddToAction 'Filter' failed: %s",
628 UpnpGetErrorMessage( i_res ) );
629 goto browseActionCleanup;
632 i_res = UpnpAddToAction( &p_action, "Browse",
633 psz_service_type, "StartingIndex", psz_starting_index_ );
635 if ( i_res != UPNP_E_SUCCESS )
637 msg_Dbg( _p_sd, "AddToAction 'StartingIndex' failed: %s",
638 UpnpGetErrorMessage( i_res ) );
639 goto browseActionCleanup;
642 i_res = UpnpAddToAction( &p_action, "Browse",
643 psz_service_type, "RequestedCount", psz_requested_count_ );
645 if ( i_res != UPNP_E_SUCCESS )
647 msg_Dbg( _p_sd, "AddToAction 'RequestedCount' failed: %s",
648 UpnpGetErrorMessage( i_res ) );
649 goto browseActionCleanup;
652 i_res = UpnpAddToAction( &p_action, "Browse",
653 psz_service_type, "SortCriteria", psz_sort_criteria_ );
655 if ( i_res != UPNP_E_SUCCESS )
657 msg_Dbg( _p_sd, "AddToAction 'SortCriteria' failed: %s",
658 UpnpGetErrorMessage( i_res ) );
659 goto browseActionCleanup;
662 i_res = UpnpSendAction( _p_sd->p_sys->client_handle,
665 0, /* ignored in SDK, must be NULL */
669 if ( i_res != UPNP_E_SUCCESS )
671 msg_Err( _p_sd, "%s when trying the send() action with URL: %s",
672 UpnpGetErrorMessage( i_res ), psz_url );
674 ixmlDocument_free( p_response );
680 free( psz_service_type );
682 ixmlDocument_free( p_action );
686 void MediaServer::fetchContents()
688 /* Delete previous contents to prevent duplicate entries */
692 services_discovery_RemoveItem( _p_sd, _p_input_item );
693 services_discovery_AddItem( _p_sd, _p_input_item, NULL );
696 Container* root = new Container( 0, "0", getFriendlyName() );
698 _fetchContents( root );
701 _p_contents->setInputItem( _p_input_item );
703 _buildPlaylist( _p_contents, NULL );
707 * Fetches and parses the UPNP response
709 bool MediaServer::_fetchContents( Container* p_parent )
713 msg_Err( _p_sd, "No parent" );
717 IXML_Document* p_response = _browseAction( p_parent->getObjectID(),
718 "BrowseDirectChildren",
722 msg_Err( _p_sd, "No response from browse() action" );
726 IXML_Document* p_result = parseBrowseResult( p_response );
727 ixmlDocument_free( p_response );
731 msg_Err( _p_sd, "browse() response parsing failed" );
737 msg_Dbg( _p_sd, "Got DIDL document: %s",
738 ixmlPrintDocument( p_result ) );
742 IXML_NodeList* containerNodeList =
743 ixmlDocument_getElementsByTagName( p_result, "container" );
745 if ( containerNodeList )
747 for ( unsigned int i = 0;
748 i < ixmlNodeList_length( containerNodeList ); i++ )
750 IXML_Element* containerElement =
751 ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
753 const char* objectID = ixmlElement_getAttribute( containerElement,
758 const char* title = xml_getChildElementValue( containerElement,
764 Container* container = new Container( p_parent, objectID, title );
765 p_parent->addContainer( container );
766 _fetchContents( container );
768 ixmlNodeList_free( containerNodeList );
771 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( p_result,
775 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
777 IXML_Element* itemElement =
778 ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
780 const char* objectID =
781 ixmlElement_getAttribute( itemElement, "id" );
787 xml_getChildElementValue( itemElement, "dc:title" );
792 const char* resource =
793 xml_getChildElementValue( itemElement, "res" );
798 const char* psz_duration = xml_getChildElementAttributeValue( itemElement,
802 mtime_t i_duration = -1;
803 int i_hours, i_minutes, i_seconds, i_decis;
807 if( sscanf( psz_duration, "%02d:%02d:%02d.%d",
808 &i_hours, &i_minutes, &i_seconds, &i_decis ))
809 i_duration = INT64_C(1000000) * ( i_hours*3600 +
812 INT64_C(100000) * i_decis;
815 Item* item = new Item( p_parent, objectID, title, resource, i_duration );
816 p_parent->addItem( item );
818 ixmlNodeList_free( itemNodeList );
821 ixmlDocument_free( p_result );
825 // TODO: Create a permanent fix for the item duplication bug. The current fix
826 // is essentially only a small hack. Although it fixes the problem, it introduces
827 // annoying cosmetic issues with the playlist. For example, when the UPnP Server
828 // rebroadcasts it's directory structure, the VLC Client deletes the old directory
829 // structure, causing the user to go back to the root node of the directory. The
830 // directory is then rebuilt, and the user is forced to traverse through the directory
831 // to find the item they were looking for. Some servers may not push the directory
832 // structure too often, but we cannot rely on this fix.
834 // I have thought up another fix, but this would require certain features to
835 // be present within the VLC services discovery. Currently, services_discovery_AddItem
836 // does not allow the programmer to nest items. It only allows a "2 deep" scope.
837 // An example of the limitation is below:
843 // services_discovery_AddItem will not let the programmer specify a child-node to
844 // insert items into, so we would not be able to do the following:
850 // + Sub Item 1 of Item 2
851 // + Sub-Sub Item 1 of Sub Item 1
853 // This creates a HUGE limitation on what we are able to do. If we were able to do
854 // the above, we could simply preserve the old directory listing, and compare what items
855 // do not exist in the new directory listing, then remove them from the shown listing using
856 // services_discovery_RemoveItem. If new files were introduced within an already existing
857 // container, we could simply do so with services_discovery_AddItem.
860 * Builds playlist based on available input items.
862 void MediaServer::_buildPlaylist( Container* p_parent, input_item_node_t *p_input_node )
864 bool b_send = p_input_node == NULL;
866 p_input_node = input_item_node_Create( p_parent->getInputItem() );
868 for ( unsigned int i = 0; i < p_parent->getNumContainers(); i++ )
870 Container* p_container = p_parent->getContainer( i );
872 input_item_t* p_input_item = input_item_New( "vlc://nop",
873 p_container->getTitle() );
874 input_item_node_t *p_new_node =
875 input_item_node_AppendItem( p_input_node, p_input_item );
877 p_container->setInputItem( p_input_item );
878 _buildPlaylist( p_container, p_new_node );
881 for ( unsigned int i = 0; i < p_parent->getNumItems(); i++ )
883 Item* p_item = p_parent->getItem( i );
885 input_item_t* p_input_item = input_item_NewExt( p_item->getResource(),
890 p_item->getDuration() );
892 assert( p_input_item );
893 input_item_node_AppendItem( p_input_node, p_input_item );
894 p_item->setInputItem( p_input_item );
898 input_item_node_PostAndDelete( p_input_node );
901 void MediaServer::setInputItem( input_item_t* p_input_item )
903 if( _p_input_item == p_input_item )
907 vlc_gc_decref( _p_input_item );
909 vlc_gc_incref( p_input_item );
910 _p_input_item = p_input_item;
913 input_item_t* MediaServer::getInputItem() const
915 return _p_input_item;
918 bool MediaServer::compareSID( const char* psz_sid )
920 return ( strncmp( _subscription_id, psz_sid, sizeof( Upnp_SID ) ) == 0 );
925 * MediaServerList class
927 MediaServerList::MediaServerList( services_discovery_t* p_sd )
932 MediaServerList::~MediaServerList()
934 for ( unsigned int i = 0; i < _list.size(); i++ )
940 bool MediaServerList::addServer( MediaServer* p_server )
942 input_item_t* p_input_item = NULL;
943 if ( getServer( p_server->getUDN() ) != 0 ) return false;
945 msg_Dbg( _p_sd, "Adding server '%s' with uuid '%s'", p_server->getFriendlyName(), p_server->getUDN() );
947 p_input_item = input_item_New( "vlc://nop", p_server->getFriendlyName() );
949 input_item_SetDescription( p_input_item, p_server->getUDN() );
951 p_server->setInputItem( p_input_item );
953 services_discovery_AddItem( _p_sd, p_input_item, NULL );
955 _list.push_back( p_server );
960 MediaServer* MediaServerList::getServer( const char* psz_udn )
962 MediaServer* p_result = 0;
964 for ( unsigned int i = 0; i < _list.size(); i++ )
966 if( strcmp( psz_udn, _list[i]->getUDN() ) == 0 )
976 MediaServer* MediaServerList::getServerBySID( const char* psz_sid )
978 MediaServer* p_server = 0;
980 for ( unsigned int i = 0; i < _list.size(); i++ )
982 if ( _list[i]->compareSID( psz_sid ) )
992 void MediaServerList::removeServer( const char* psz_udn )
994 MediaServer* p_server = getServer( psz_udn );
995 if ( !p_server ) return;
997 msg_Dbg( _p_sd, "Removing server '%s'", p_server->getFriendlyName() );
999 services_discovery_RemoveItem( _p_sd, p_server->getInputItem() );
1001 std::vector<MediaServer*>::iterator it;
1002 for ( it = _list.begin(); it != _list.end(); ++it )
1004 if ( *it == p_server )
1017 Item::Item( Container* p_parent, const char* psz_object_id, const char* psz_title,
1018 const char* psz_resource, mtime_t i_duration )
1022 _objectID = psz_object_id;
1024 _resource = psz_resource;
1025 _duration = i_duration;
1027 _p_input_item = NULL;
1033 vlc_gc_decref( _p_input_item );
1036 const char* Item::getObjectID() const
1038 return _objectID.c_str();
1041 const char* Item::getTitle() const
1043 return _title.c_str();
1046 const char* Item::getResource() const
1048 return _resource.c_str();
1051 mtime_t Item::getDuration() const
1056 void Item::setInputItem( input_item_t* p_input_item )
1058 if( _p_input_item == p_input_item )
1062 vlc_gc_decref( _p_input_item );
1064 vlc_gc_incref( p_input_item );
1065 _p_input_item = p_input_item;
1071 Container::Container( Container* p_parent,
1072 const char* psz_object_id,
1073 const char* psz_title )
1077 _objectID = psz_object_id;
1080 _p_input_item = NULL;
1083 Container::~Container()
1085 for ( unsigned int i = 0; i < _containers.size(); i++ )
1087 delete _containers[i];
1090 for ( unsigned int i = 0; i < _items.size(); i++ )
1096 vlc_gc_decref( _p_input_item );
1099 void Container::addItem( Item* item )
1101 _items.push_back( item );
1104 void Container::addContainer( Container* p_container )
1106 _containers.push_back( p_container );
1109 const char* Container::getObjectID() const
1111 return _objectID.c_str();
1114 const char* Container::getTitle() const
1116 return _title.c_str();
1119 unsigned int Container::getNumItems() const
1121 return _items.size();
1124 unsigned int Container::getNumContainers() const
1126 return _containers.size();
1129 Item* Container::getItem( unsigned int i_index ) const
1131 if ( i_index < _items.size() ) return _items[i_index];
1135 Container* Container::getContainer( unsigned int i_index ) const
1137 if ( i_index < _containers.size() ) return _containers[i_index];
1141 Container* Container::getParent()
1146 void Container::setInputItem( input_item_t* p_input_item )
1148 if( _p_input_item == p_input_item )
1152 vlc_gc_decref( _p_input_item );
1154 vlc_gc_incref( p_input_item );
1155 _p_input_item = p_input_item;
1158 input_item_t* Container::getInputItem() const
1160 return _p_input_item;