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>
46 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
47 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
52 struct services_discovery_sys_t
54 UpnpClient_Handle client_handle;
55 MediaServerList* p_server_list;
56 vlc_mutex_t callback_lock;
60 * VLC callback prototypes
62 static int Open( vlc_object_t* );
63 static void Close( vlc_object_t* );
64 VLC_SD_PROBE_HELPER( "upnp", "Universal Plug'n'Play", SD_CAT_LAN )
70 set_shortname( "UPnP" );
71 set_description( N_( "Universal Plug'n'Play" ) );
72 set_category( CAT_PLAYLIST );
73 set_subcategory( SUBCAT_PLAYLIST_SD );
74 set_capability( "services_discovery", 0 );
75 set_callbacks( Open, Close );
77 VLC_SD_PROBE_SUBMODULE
83 static int Callback( Upnp_EventType event_type, void* p_event, void* p_user_data );
85 const char* xml_getChildElementValue( IXML_Element* p_parent,
86 const char* psz_tag_name );
88 const char* xml_getChildElementAttributeValue( IXML_Element* p_parent,
89 const char* psz_tag_name_,
90 const char* psz_attribute_ );
92 IXML_Document* parseBrowseResult( IXML_Document* p_doc );
95 * Initializes UPNP instance.
97 static int Open( vlc_object_t *p_this )
100 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
101 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
102 calloc( 1, sizeof( services_discovery_sys_t ) );
104 if( !( p_sd->p_sys = p_sys ) )
107 /* Initialize on first IPv4-capable adapter and first open port
108 * TODO: use UpnpInit2() to utilize IPv6.
110 i_res = UpnpInit( 0, 0 );
111 if( i_res != UPNP_E_SUCCESS )
113 msg_Err( p_sd, "Initialization failed: %s", UpnpGetErrorMessage( i_res ) );
118 p_sys->p_server_list = new MediaServerList( p_sd );
119 vlc_mutex_init( &p_sys->callback_lock );
121 /* Register a control point */
122 i_res = UpnpRegisterClient( Callback, p_sd, &p_sys->client_handle );
123 if( i_res != UPNP_E_SUCCESS )
125 msg_Err( p_sd, "Client registration failed: %s", UpnpGetErrorMessage( i_res ) );
126 Close( (vlc_object_t*) p_sd );
130 /* Search for media servers */
131 i_res = UpnpSearchAsync( p_sys->client_handle, 5,
132 MEDIA_SERVER_DEVICE_TYPE, p_sd );
133 if( i_res != UPNP_E_SUCCESS )
135 msg_Err( p_sd, "Error sending search request: %s", UpnpGetErrorMessage( i_res ) );
136 Close( (vlc_object_t*) p_sd );
140 /* libupnp does not treat a maximum content length of 0 as unlimited
141 * until 64dedf (~ pupnp v1.6.7) and provides no sane way to discriminate
142 * between versions */
143 if( (i_res = UpnpSetMaxContentLength( INT_MAX )) != UPNP_E_SUCCESS )
145 msg_Err( p_sd, "Failed to set maximum content length: %s",
146 UpnpGetErrorMessage( i_res ));
148 Close( (vlc_object_t*) p_sd );
156 * Releases resources.
158 static void Close( vlc_object_t *p_this )
160 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
162 UpnpUnRegisterClient( p_sd->p_sys->client_handle );
165 delete p_sd->p_sys->p_server_list;
166 vlc_mutex_destroy( &p_sd->p_sys->callback_lock );
171 /* XML utility functions */
174 * Returns the value of a child element, or NULL on error
176 const char* xml_getChildElementValue( IXML_Element* p_parent,
177 const char* psz_tag_name_ )
179 if ( !p_parent ) return NULL;
180 if ( !psz_tag_name_ ) return NULL;
182 IXML_NodeList* p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name_ );
183 if ( !p_node_list ) return NULL;
185 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
186 ixmlNodeList_free( p_node_list );
187 if ( !p_element ) return NULL;
189 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_element );
190 if ( !p_text_node ) return NULL;
192 return ixmlNode_getNodeValue( p_text_node );
196 * Returns the value of a child element's attribute, or NULL on error
198 const char* xml_getChildElementAttributeValue( IXML_Element* p_parent,
199 const char* psz_tag_name_,
200 const char* psz_attribute_ )
202 if ( !p_parent ) return NULL;
203 if ( !psz_tag_name_ ) return NULL;
204 if ( !psz_attribute_ ) return NULL;
206 IXML_NodeList* p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name_ );
207 if ( !p_node_list ) return NULL;
209 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
210 ixmlNodeList_free( p_node_list );
211 if ( !p_element ) return NULL;
213 return ixmlElement_getAttribute( (IXML_Element*) p_element, psz_attribute_ );
217 * Extracts the result document from a SOAP response
219 IXML_Document* parseBrowseResult( IXML_Document* p_doc )
221 ixmlRelaxParser( 1 );
223 if ( !p_doc ) return 0;
225 IXML_NodeList* p_result_list = ixmlDocument_getElementsByTagName( p_doc,
228 if ( !p_result_list ) return 0;
230 IXML_Node* p_result_node = ixmlNodeList_item( p_result_list, 0 );
232 ixmlNodeList_free( p_result_list );
234 if ( !p_result_node ) return 0;
236 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_result_node );
237 if ( !p_text_node ) return 0;
239 const char* psz_result_string = ixmlNode_getNodeValue( p_text_node );
241 IXML_Document* p_browse_doc = ixmlParseBuffer( psz_result_string );
248 * Handles all UPnP events
250 static int Callback( Upnp_EventType event_type, void* p_event, void* p_user_data )
252 services_discovery_t* p_sd = ( services_discovery_t* ) p_user_data;
253 services_discovery_sys_t* p_sys = p_sd->p_sys;
254 vlc_mutex_locker locker( &p_sys->callback_lock );
258 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
259 case UPNP_DISCOVERY_SEARCH_RESULT:
261 struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
263 IXML_Document *p_description_doc = 0;
266 i_res = UpnpDownloadXmlDoc( p_discovery->Location, &p_description_doc );
267 if ( i_res != UPNP_E_SUCCESS )
269 msg_Warn( p_sd, "Could not download device description! "
270 "Fetching data from %s failed: %s",
271 p_discovery->Location, UpnpGetErrorMessage( i_res ) );
275 MediaServer::parseDeviceDescription( p_description_doc,
276 p_discovery->Location, p_sd );
278 ixmlDocument_free( p_description_doc );
282 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
284 struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
286 p_sys->p_server_list->removeServer( p_discovery->DeviceId );
291 case UPNP_EVENT_RECEIVED:
293 Upnp_Event* p_e = ( Upnp_Event* )p_event;
295 MediaServer* p_server = p_sys->p_server_list->getServerBySID( p_e->Sid );
296 if ( p_server ) p_server->fetchContents();
300 case UPNP_EVENT_AUTORENEWAL_FAILED:
301 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
305 Upnp_Event_Subscribe* p_s = ( Upnp_Event_Subscribe* )p_event;
307 MediaServer* p_server = p_sys->p_server_list->getServerBySID( p_s->Sid );
308 if ( p_server ) p_server->subscribeToContentDirectory();
312 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
313 msg_Warn( p_sd, "subscription complete" );
316 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
317 msg_Warn( p_sd, "search timeout" );
321 msg_Err( p_sd, "Unhandled event, please report ( type=%d )", event_type );
325 return UPNP_E_SUCCESS;
330 * Local class implementations.
337 void MediaServer::parseDeviceDescription( IXML_Document* p_doc,
338 const char* p_location,
339 services_discovery_t* p_sd )
343 msg_Err( p_sd, "Null IXML_Document" );
349 msg_Err( p_sd, "Null location" );
353 const char* psz_base_url = p_location;
355 /* Try to extract baseURL */
356 IXML_NodeList* p_url_list = ixmlDocument_getElementsByTagName( p_doc, "URLBase" );
360 if ( IXML_Node* p_url_node = ixmlNodeList_item( p_url_list, 0 ) )
362 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_url_node );
363 if ( p_text_node ) psz_base_url = ixmlNode_getNodeValue( p_text_node );
366 ixmlNodeList_free( p_url_list );
370 IXML_NodeList* p_device_list =
371 ixmlDocument_getElementsByTagName( p_doc, "device" );
375 for ( unsigned int i = 0; i < ixmlNodeList_length( p_device_list ); i++ )
377 IXML_Element* p_device_element =
378 ( IXML_Element* ) ixmlNodeList_item( p_device_list, i );
380 const char* psz_device_type = xml_getChildElementValue( p_device_element,
382 if ( !psz_device_type )
384 msg_Warn( p_sd, "No deviceType found!" );
388 if ( strncmp( MEDIA_SERVER_DEVICE_TYPE, psz_device_type,
389 strlen( MEDIA_SERVER_DEVICE_TYPE ) - 1 ) != 0 )
392 const char* psz_udn = xml_getChildElementValue( p_device_element, "UDN" );
395 msg_Warn( p_sd, "No UDN!" );
399 /* Check if server is already added */
400 if ( p_sd->p_sys->p_server_list->getServer( psz_udn ) != 0 )
402 msg_Warn( p_sd, "Server with uuid '%s' already exists.", psz_udn );
406 const char* psz_friendly_name =
407 xml_getChildElementValue( p_device_element,
410 if ( !psz_friendly_name )
412 msg_Dbg( p_sd, "No friendlyName!" );
416 MediaServer* p_server = new MediaServer( psz_udn, psz_friendly_name, p_sd );
418 if ( !p_sd->p_sys->p_server_list->addServer( p_server ) )
425 /* Check for ContentDirectory service. */
426 IXML_NodeList* p_service_list =
427 ixmlElement_getElementsByTagName( p_device_element,
429 if ( p_service_list )
431 for ( unsigned int j = 0;
432 j < ixmlNodeList_length( p_service_list ); j++ )
434 IXML_Element* p_service_element =
435 ( IXML_Element* ) ixmlNodeList_item( p_service_list, j );
437 const char* psz_service_type =
438 xml_getChildElementValue( p_service_element,
440 if ( !psz_service_type )
442 msg_Warn( p_sd, "No service type found." );
446 int k = strlen( CONTENT_DIRECTORY_SERVICE_TYPE ) - 1;
447 if ( strncmp( CONTENT_DIRECTORY_SERVICE_TYPE,
448 psz_service_type, k ) != 0 )
451 p_server->_i_content_directory_service_version =
454 const char* psz_event_sub_url =
455 xml_getChildElementValue( p_service_element,
457 if ( !psz_event_sub_url )
459 msg_Warn( p_sd, "No event subscription url found." );
463 const char* psz_control_url =
464 xml_getChildElementValue( p_service_element,
466 if ( !psz_control_url )
468 msg_Warn( p_sd, "No control url found." );
472 /* Try to subscribe to ContentDirectory service */
474 char* psz_url = ( char* ) malloc( strlen( psz_base_url ) +
475 strlen( psz_event_sub_url ) + 1 );
478 if ( UpnpResolveURL( psz_base_url, psz_event_sub_url, psz_url ) ==
481 p_server->setContentDirectoryEventURL( psz_url );
482 p_server->subscribeToContentDirectory();
488 /* Try to browse content directory. */
490 psz_url = ( char* ) malloc( strlen( psz_base_url ) +
491 strlen( psz_control_url ) + 1 );
494 if ( UpnpResolveURL( psz_base_url, psz_control_url, psz_url ) ==
497 p_server->setContentDirectoryControlURL( psz_url );
498 p_server->fetchContents();
504 ixmlNodeList_free( p_service_list );
507 ixmlNodeList_free( p_device_list );
511 MediaServer::MediaServer( const char* psz_udn,
512 const char* psz_friendly_name,
513 services_discovery_t* p_sd )
518 _friendly_name = psz_friendly_name;
521 _p_input_item = NULL;
522 _i_content_directory_service_version = 1;
525 MediaServer::~MediaServer()
530 const char* MediaServer::getUDN() const
535 const char* MediaServer::getFriendlyName() const
537 return _friendly_name.c_str();
540 void MediaServer::setContentDirectoryEventURL( const char* psz_url )
542 _content_directory_event_url = psz_url;
545 const char* MediaServer::getContentDirectoryEventURL() const
547 return _content_directory_event_url.c_str();
550 void MediaServer::setContentDirectoryControlURL( const char* psz_url )
552 _content_directory_control_url = psz_url;
555 const char* MediaServer::getContentDirectoryControlURL() const
557 return _content_directory_control_url.c_str();
561 * Subscribes current client handle to Content Directory Service.
562 * CDS exports the server shares to clients.
564 void MediaServer::subscribeToContentDirectory()
566 const char* psz_url = getContentDirectoryEventURL();
569 msg_Dbg( _p_sd, "No subscription url set!" );
573 int i_timeout = 1810;
576 int i_res = UpnpSubscribe( _p_sd->p_sys->client_handle, psz_url, &i_timeout, sid );
578 if ( i_res == UPNP_E_SUCCESS )
580 _i_subscription_timeout = i_timeout;
581 memcpy( _subscription_id, sid, sizeof( Upnp_SID ) );
585 msg_Dbg( _p_sd, "Subscribe failed: '%s': %s",
586 getFriendlyName(), UpnpGetErrorMessage( i_res ) );
590 * Constructs UpnpAction to browse available content.
592 IXML_Document* MediaServer::_browseAction( const char* psz_object_id_,
593 const char* psz_browser_flag_,
594 const char* psz_filter_,
595 const char* psz_starting_index_,
596 const char* psz_requested_count_,
597 const char* psz_sort_criteria_ )
599 IXML_Document* p_action = 0;
600 IXML_Document* p_response = 0;
601 const char* psz_url = getContentDirectoryControlURL();
605 msg_Dbg( _p_sd, "No subscription url set!" );
609 char* psz_service_type = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
611 psz_service_type[strlen( psz_service_type ) - 1] =
612 _i_content_directory_service_version;
616 i_res = UpnpAddToAction( &p_action, "Browse",
617 psz_service_type, "ObjectID", psz_object_id_ );
619 if ( i_res != UPNP_E_SUCCESS )
621 msg_Dbg( _p_sd, "AddToAction 'ObjectID' failed: %s",
622 UpnpGetErrorMessage( i_res ) );
623 goto browseActionCleanup;
626 i_res = UpnpAddToAction( &p_action, "Browse",
627 psz_service_type, "BrowseFlag", psz_browser_flag_ );
629 if ( i_res != UPNP_E_SUCCESS )
631 msg_Dbg( _p_sd, "AddToAction 'BrowseFlag' failed: %s",
632 UpnpGetErrorMessage( i_res ) );
633 goto browseActionCleanup;
636 i_res = UpnpAddToAction( &p_action, "Browse",
637 psz_service_type, "Filter", psz_filter_ );
639 if ( i_res != UPNP_E_SUCCESS )
641 msg_Dbg( _p_sd, "AddToAction 'Filter' failed: %s",
642 UpnpGetErrorMessage( i_res ) );
643 goto browseActionCleanup;
646 i_res = UpnpAddToAction( &p_action, "Browse",
647 psz_service_type, "StartingIndex", psz_starting_index_ );
649 if ( i_res != UPNP_E_SUCCESS )
651 msg_Dbg( _p_sd, "AddToAction 'StartingIndex' failed: %s",
652 UpnpGetErrorMessage( i_res ) );
653 goto browseActionCleanup;
656 i_res = UpnpAddToAction( &p_action, "Browse",
657 psz_service_type, "RequestedCount", psz_requested_count_ );
659 if ( i_res != UPNP_E_SUCCESS )
661 msg_Dbg( _p_sd, "AddToAction 'RequestedCount' failed: %s",
662 UpnpGetErrorMessage( i_res ) );
663 goto browseActionCleanup;
666 i_res = UpnpAddToAction( &p_action, "Browse",
667 psz_service_type, "SortCriteria", psz_sort_criteria_ );
669 if ( i_res != UPNP_E_SUCCESS )
671 msg_Dbg( _p_sd, "AddToAction 'SortCriteria' failed: %s",
672 UpnpGetErrorMessage( i_res ) );
673 goto browseActionCleanup;
676 i_res = UpnpSendAction( _p_sd->p_sys->client_handle,
679 0, /* ignored in SDK, must be NULL */
683 if ( i_res != UPNP_E_SUCCESS )
685 msg_Err( _p_sd, "%s when trying the send() action with URL: %s",
686 UpnpGetErrorMessage( i_res ), psz_url );
688 ixmlDocument_free( p_response );
694 free( psz_service_type );
696 ixmlDocument_free( p_action );
700 void MediaServer::fetchContents()
702 /* Delete previous contents to prevent duplicate entries */
706 services_discovery_RemoveItem( _p_sd, _p_input_item );
707 services_discovery_AddItem( _p_sd, _p_input_item, NULL );
710 Container* root = new Container( 0, "0", getFriendlyName() );
712 _fetchContents( root );
715 _p_contents->setInputItem( _p_input_item );
717 _buildPlaylist( _p_contents, NULL );
721 * Fetches and parses the UPNP response
723 bool MediaServer::_fetchContents( Container* p_parent )
727 msg_Err( _p_sd, "No parent" );
731 IXML_Document* p_response = _browseAction( p_parent->getObjectID(),
732 "BrowseDirectChildren",
736 msg_Err( _p_sd, "No response from browse() action" );
740 IXML_Document* p_result = parseBrowseResult( p_response );
741 ixmlDocument_free( p_response );
745 msg_Err( _p_sd, "browse() response parsing failed" );
751 msg_Dbg( _p_sd, "Got DIDL document: %s",
752 ixmlPrintDocument( p_result ) );
756 IXML_NodeList* containerNodeList =
757 ixmlDocument_getElementsByTagName( p_result, "container" );
759 if ( containerNodeList )
761 for ( unsigned int i = 0;
762 i < ixmlNodeList_length( containerNodeList ); i++ )
764 IXML_Element* containerElement =
765 ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
767 const char* objectID = ixmlElement_getAttribute( containerElement,
772 const char* title = xml_getChildElementValue( containerElement,
778 Container* container = new Container( p_parent, objectID, title );
779 p_parent->addContainer( container );
780 _fetchContents( container );
782 ixmlNodeList_free( containerNodeList );
785 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( p_result,
789 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
791 IXML_Element* itemElement =
792 ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
794 const char* objectID =
795 ixmlElement_getAttribute( itemElement, "id" );
801 xml_getChildElementValue( itemElement, "dc:title" );
806 const char* resource =
807 xml_getChildElementValue( itemElement, "res" );
812 const char* psz_duration = xml_getChildElementAttributeValue( itemElement,
816 mtime_t i_duration = -1;
817 int i_hours, i_minutes, i_seconds, i_decis;
821 if( sscanf( psz_duration, "%02d:%02d:%02d.%d",
822 &i_hours, &i_minutes, &i_seconds, &i_decis ))
823 i_duration = INT64_C(1000000) * ( i_hours*3600 +
826 INT64_C(100000) * i_decis;
829 Item* item = new Item( p_parent, objectID, title, resource, i_duration );
830 p_parent->addItem( item );
832 ixmlNodeList_free( itemNodeList );
835 ixmlDocument_free( p_result );
839 // TODO: Create a permanent fix for the item duplication bug. The current fix
840 // is essentially only a small hack. Although it fixes the problem, it introduces
841 // annoying cosmetic issues with the playlist. For example, when the UPnP Server
842 // rebroadcasts it's directory structure, the VLC Client deletes the old directory
843 // structure, causing the user to go back to the root node of the directory. The
844 // directory is then rebuilt, and the user is forced to traverse through the directory
845 // to find the item they were looking for. Some servers may not push the directory
846 // structure too often, but we cannot rely on this fix.
848 // I have thought up another fix, but this would require certain features to
849 // be present within the VLC services discovery. Currently, services_discovery_AddItem
850 // does not allow the programmer to nest items. It only allows a "2 deep" scope.
851 // An example of the limitation is below:
857 // services_discovery_AddItem will not let the programmer specify a child-node to
858 // insert items into, so we would not be able to do the following:
864 // + Sub Item 1 of Item 2
865 // + Sub-Sub Item 1 of Sub Item 1
867 // This creates a HUGE limitation on what we are able to do. If we were able to do
868 // the above, we could simply preserve the old directory listing, and compare what items
869 // do not exist in the new directory listing, then remove them from the shown listing using
870 // services_discovery_RemoveItem. If new files were introduced within an already existing
871 // container, we could simply do so with services_discovery_AddItem.
874 * Builds playlist based on available input items.
876 void MediaServer::_buildPlaylist( Container* p_parent, input_item_node_t *p_input_node )
878 bool b_send = p_input_node == NULL;
880 p_input_node = input_item_node_Create( p_parent->getInputItem() );
882 for ( unsigned int i = 0; i < p_parent->getNumContainers(); i++ )
884 Container* p_container = p_parent->getContainer( i );
886 input_item_t* p_input_item = input_item_New( "vlc://nop",
887 p_container->getTitle() );
888 input_item_node_t *p_new_node =
889 input_item_node_AppendItem( p_input_node, p_input_item );
891 p_container->setInputItem( p_input_item );
892 _buildPlaylist( p_container, p_new_node );
895 for ( unsigned int i = 0; i < p_parent->getNumItems(); i++ )
897 Item* p_item = p_parent->getItem( i );
899 input_item_t* p_input_item = input_item_NewExt( p_item->getResource(),
904 p_item->getDuration() );
906 assert( p_input_item );
907 input_item_node_AppendItem( p_input_node, p_input_item );
908 p_item->setInputItem( p_input_item );
912 input_item_node_PostAndDelete( p_input_node );
915 void MediaServer::setInputItem( input_item_t* p_input_item )
917 if( _p_input_item == p_input_item )
921 vlc_gc_decref( _p_input_item );
923 vlc_gc_incref( p_input_item );
924 _p_input_item = p_input_item;
927 input_item_t* MediaServer::getInputItem() const
929 return _p_input_item;
932 bool MediaServer::compareSID( const char* psz_sid )
934 return ( strncmp( _subscription_id, psz_sid, sizeof( Upnp_SID ) ) == 0 );
939 * MediaServerList class
941 MediaServerList::MediaServerList( services_discovery_t* p_sd )
946 MediaServerList::~MediaServerList()
948 for ( unsigned int i = 0; i < _list.size(); i++ )
954 bool MediaServerList::addServer( MediaServer* p_server )
956 input_item_t* p_input_item = NULL;
957 if ( getServer( p_server->getUDN() ) != 0 ) return false;
959 msg_Dbg( _p_sd, "Adding server '%s' with uuid '%s'", p_server->getFriendlyName(), p_server->getUDN() );
961 p_input_item = input_item_New( "vlc://nop", p_server->getFriendlyName() );
963 input_item_SetDescription( p_input_item, p_server->getUDN() );
965 p_server->setInputItem( p_input_item );
967 services_discovery_AddItem( _p_sd, p_input_item, NULL );
969 _list.push_back( p_server );
974 MediaServer* MediaServerList::getServer( const char* psz_udn )
976 MediaServer* p_result = 0;
978 for ( unsigned int i = 0; i < _list.size(); i++ )
980 if( strcmp( psz_udn, _list[i]->getUDN() ) == 0 )
990 MediaServer* MediaServerList::getServerBySID( const char* psz_sid )
992 MediaServer* p_server = 0;
994 for ( unsigned int i = 0; i < _list.size(); i++ )
996 if ( _list[i]->compareSID( psz_sid ) )
1006 void MediaServerList::removeServer( const char* psz_udn )
1008 MediaServer* p_server = getServer( psz_udn );
1009 if ( !p_server ) return;
1011 msg_Dbg( _p_sd, "Removing server '%s'", p_server->getFriendlyName() );
1013 services_discovery_RemoveItem( _p_sd, p_server->getInputItem() );
1015 std::vector<MediaServer*>::iterator it;
1016 for ( it = _list.begin(); it != _list.end(); ++it )
1018 if ( *it == p_server )
1031 Item::Item( Container* p_parent, const char* psz_object_id, const char* psz_title,
1032 const char* psz_resource, mtime_t i_duration )
1036 _objectID = psz_object_id;
1038 _resource = psz_resource;
1039 _duration = i_duration;
1041 _p_input_item = NULL;
1047 vlc_gc_decref( _p_input_item );
1050 const char* Item::getObjectID() const
1052 return _objectID.c_str();
1055 const char* Item::getTitle() const
1057 return _title.c_str();
1060 const char* Item::getResource() const
1062 return _resource.c_str();
1065 mtime_t Item::getDuration() const
1070 void Item::setInputItem( input_item_t* p_input_item )
1072 if( _p_input_item == p_input_item )
1076 vlc_gc_decref( _p_input_item );
1078 vlc_gc_incref( p_input_item );
1079 _p_input_item = p_input_item;
1085 Container::Container( Container* p_parent,
1086 const char* psz_object_id,
1087 const char* psz_title )
1091 _objectID = psz_object_id;
1094 _p_input_item = NULL;
1097 Container::~Container()
1099 for ( unsigned int i = 0; i < _containers.size(); i++ )
1101 delete _containers[i];
1104 for ( unsigned int i = 0; i < _items.size(); i++ )
1110 vlc_gc_decref( _p_input_item );
1113 void Container::addItem( Item* item )
1115 _items.push_back( item );
1118 void Container::addContainer( Container* p_container )
1120 _containers.push_back( p_container );
1123 const char* Container::getObjectID() const
1125 return _objectID.c_str();
1128 const char* Container::getTitle() const
1130 return _title.c_str();
1133 unsigned int Container::getNumItems() const
1135 return _items.size();
1138 unsigned int Container::getNumContainers() const
1140 return _containers.size();
1143 Item* Container::getItem( unsigned int i_index ) const
1145 if ( i_index < _items.size() ) return _items[i_index];
1149 Container* Container::getContainer( unsigned int i_index ) const
1151 if ( i_index < _containers.size() ) return _containers[i_index];
1155 Container* Container::getParent()
1160 void Container::setInputItem( input_item_t* p_input_item )
1162 if( _p_input_item == p_input_item )
1166 vlc_gc_decref( _p_input_item );
1168 vlc_gc_incref( p_input_item );
1169 _p_input_item = p_input_item;
1172 input_item_t* Container::getInputItem() const
1174 return _p_input_item;