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_getChildElementValue( IXML_Document* p_doc,
89 const char* psz_tag_name );
91 const char* xml_getChildElementAttributeValue( IXML_Element* p_parent,
92 const char* psz_tag_name,
93 const char* psz_attribute );
95 int xml_getNumber( IXML_Document* p_doc,
96 const char* psz_tag_name );
98 IXML_Document* parseBrowseResult( IXML_Document* p_doc );
101 * Initializes UPNP instance.
103 static int Open( vlc_object_t *p_this )
106 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
107 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
108 calloc( 1, sizeof( services_discovery_sys_t ) );
110 if( !( p_sd->p_sys = p_sys ) )
113 #ifdef UPNP_ENABLE_IPV6
115 psz_miface = var_InheritString( p_sd, "miface" );
116 msg_Info( p_sd, "Initializing libupnp on '%s' interface", psz_miface );
117 i_res = UpnpInit2( psz_miface, 0 );
120 /* If UpnpInit2 isnt available, initialize on first IPv4-capable interface */
121 i_res = UpnpInit( 0, 0 );
123 if( i_res != UPNP_E_SUCCESS )
125 msg_Err( p_sd, "Initialization failed: %s", UpnpGetErrorMessage( i_res ) );
130 ixmlRelaxParser( 1 );
132 p_sys->p_server_list = new MediaServerList( p_sd );
133 vlc_mutex_init( &p_sys->callback_lock );
135 /* Register a control point */
136 i_res = UpnpRegisterClient( Callback, p_sd, &p_sys->client_handle );
137 if( i_res != UPNP_E_SUCCESS )
139 msg_Err( p_sd, "Client registration failed: %s", UpnpGetErrorMessage( i_res ) );
140 Close( (vlc_object_t*) p_sd );
144 /* Search for media servers */
145 i_res = UpnpSearchAsync( p_sys->client_handle, 5,
146 MEDIA_SERVER_DEVICE_TYPE, p_sd );
147 if( i_res != UPNP_E_SUCCESS )
149 msg_Err( p_sd, "Error sending search request: %s", UpnpGetErrorMessage( i_res ) );
150 Close( (vlc_object_t*) p_sd );
154 /* libupnp does not treat a maximum content length of 0 as unlimited
155 * until 64dedf (~ pupnp v1.6.7) and provides no sane way to discriminate
156 * between versions */
157 if( (i_res = UpnpSetMaxContentLength( INT_MAX )) != UPNP_E_SUCCESS )
159 msg_Err( p_sd, "Failed to set maximum content length: %s",
160 UpnpGetErrorMessage( i_res ));
162 Close( (vlc_object_t*) p_sd );
170 * Releases resources.
172 static void Close( vlc_object_t *p_this )
174 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
176 UpnpUnRegisterClient( p_sd->p_sys->client_handle );
179 delete p_sd->p_sys->p_server_list;
180 vlc_mutex_destroy( &p_sd->p_sys->callback_lock );
185 /* XML utility functions */
188 * Returns the value of a child element, or NULL on error
190 const char* xml_getChildElementValue( IXML_Element* p_parent,
191 const char* psz_tag_name )
194 assert( psz_tag_name );
196 IXML_NodeList* p_node_list;
197 p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name );
198 if ( !p_node_list ) return NULL;
200 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
201 ixmlNodeList_free( p_node_list );
202 if ( !p_element ) return NULL;
204 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_element );
205 if ( !p_text_node ) return NULL;
207 return ixmlNode_getNodeValue( p_text_node );
211 * Returns the value of a child element's attribute, or NULL on error
213 const char* xml_getChildElementAttributeValue( IXML_Element* p_parent,
214 const char* psz_tag_name,
215 const char* psz_attribute )
218 assert( psz_tag_name );
219 assert( psz_attribute );
221 IXML_NodeList* p_node_list;
222 p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name );
223 if ( !p_node_list ) return NULL;
225 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
226 ixmlNodeList_free( p_node_list );
227 if ( !p_element ) return NULL;
229 return ixmlElement_getAttribute( (IXML_Element*) p_element, psz_attribute );
233 * Returns the value of a child element, or NULL on error
235 const char* xml_getChildElementValue( IXML_Document* p_doc,
236 const char* psz_tag_name )
239 assert( psz_tag_name );
241 IXML_NodeList* p_node_list;
242 p_node_list = ixmlDocument_getElementsByTagName( p_doc, psz_tag_name );
243 if ( !p_node_list ) return NULL;
245 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
246 ixmlNodeList_free( p_node_list );
247 if ( !p_element ) return NULL;
249 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_element );
250 if ( !p_text_node ) return NULL;
252 return ixmlNode_getNodeValue( p_text_node );
256 * Extracts the result document from a SOAP response
258 IXML_Document* parseBrowseResult( IXML_Document* p_doc )
262 const char* psz_result_string = xml_getChildElementValue( p_doc, "Result" );
264 if( !psz_result_string )
267 IXML_Document* p_browse_doc = ixmlParseBuffer( psz_result_string );
273 * Get the number value from a SOAP response
275 int xml_getNumber( IXML_Document* p_doc,
276 const char* psz_tag_name )
279 assert( psz_tag_name );
281 const char* psz = xml_getChildElementValue( p_doc, psz_tag_name );
287 long l = strtol( psz, &psz_end, 10 );
289 if( *psz_end || l < 0 || l > INT_MAX )
296 * Handles all UPnP events
298 static int Callback( Upnp_EventType event_type, void* p_event, void* p_user_data )
300 services_discovery_t* p_sd = ( services_discovery_t* ) p_user_data;
301 services_discovery_sys_t* p_sys = p_sd->p_sys;
302 vlc_mutex_locker locker( &p_sys->callback_lock );
306 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
307 case UPNP_DISCOVERY_SEARCH_RESULT:
309 struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
311 IXML_Document *p_description_doc = 0;
314 i_res = UpnpDownloadXmlDoc( p_discovery->Location, &p_description_doc );
315 if ( i_res != UPNP_E_SUCCESS )
317 msg_Warn( p_sd, "Could not download device description! "
318 "Fetching data from %s failed: %s",
319 p_discovery->Location, UpnpGetErrorMessage( i_res ) );
323 MediaServer::parseDeviceDescription( p_description_doc,
324 p_discovery->Location, p_sd );
326 ixmlDocument_free( p_description_doc );
330 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
332 struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
334 p_sys->p_server_list->removeServer( p_discovery->DeviceId );
339 case UPNP_EVENT_RECEIVED:
341 Upnp_Event* p_e = ( Upnp_Event* )p_event;
343 MediaServer* p_server = p_sys->p_server_list->getServerBySID( p_e->Sid );
344 if ( p_server ) p_server->fetchContents();
348 case UPNP_EVENT_AUTORENEWAL_FAILED:
349 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
353 Upnp_Event_Subscribe* p_s = ( Upnp_Event_Subscribe* )p_event;
355 MediaServer* p_server = p_sys->p_server_list->getServerBySID( p_s->Sid );
356 if ( p_server ) p_server->subscribeToContentDirectory();
360 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
361 msg_Warn( p_sd, "subscription complete" );
364 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
365 msg_Warn( p_sd, "search timeout" );
369 msg_Err( p_sd, "Unhandled event, please report ( type=%d )", event_type );
373 return UPNP_E_SUCCESS;
378 * Local class implementations.
385 void MediaServer::parseDeviceDescription( IXML_Document* p_doc,
386 const char* p_location,
387 services_discovery_t* p_sd )
391 msg_Err( p_sd, "Null IXML_Document" );
397 msg_Err( p_sd, "Null location" );
401 const char* psz_base_url = p_location;
403 /* Try to extract baseURL */
404 IXML_NodeList* p_url_list = ixmlDocument_getElementsByTagName( p_doc, "URLBase" );
408 if ( IXML_Node* p_url_node = ixmlNodeList_item( p_url_list, 0 ) )
410 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_url_node );
411 if ( p_text_node ) psz_base_url = ixmlNode_getNodeValue( p_text_node );
414 ixmlNodeList_free( p_url_list );
418 IXML_NodeList* p_device_list =
419 ixmlDocument_getElementsByTagName( p_doc, "device" );
423 for ( unsigned int i = 0; i < ixmlNodeList_length( p_device_list ); i++ )
425 IXML_Element* p_device_element =
426 ( IXML_Element* ) ixmlNodeList_item( p_device_list, i );
428 if( !p_device_element )
431 const char* psz_device_type =
432 xml_getChildElementValue( p_device_element, "deviceType" );
434 if ( !psz_device_type )
436 msg_Warn( p_sd, "No deviceType found!" );
440 if ( strncmp( MEDIA_SERVER_DEVICE_TYPE, psz_device_type,
441 strlen( MEDIA_SERVER_DEVICE_TYPE ) - 1 ) != 0 )
444 const char* psz_udn = xml_getChildElementValue( p_device_element,
448 msg_Warn( p_sd, "No UDN!" );
452 /* Check if server is already added */
453 if ( p_sd->p_sys->p_server_list->getServer( psz_udn ) != 0 )
455 msg_Warn( p_sd, "Server with uuid '%s' already exists.", psz_udn );
459 const char* psz_friendly_name =
460 xml_getChildElementValue( p_device_element,
463 if ( !psz_friendly_name )
465 msg_Dbg( p_sd, "No friendlyName!" );
469 MediaServer* p_server = new MediaServer( psz_udn,
470 psz_friendly_name, p_sd );
472 if ( !p_sd->p_sys->p_server_list->addServer( p_server ) )
479 /* Check for ContentDirectory service. */
480 IXML_NodeList* p_service_list =
481 ixmlElement_getElementsByTagName( p_device_element,
483 if ( p_service_list )
485 for ( unsigned int j = 0;
486 j < ixmlNodeList_length( p_service_list ); j++ )
488 IXML_Element* p_service_element =
489 ( IXML_Element* ) ixmlNodeList_item( p_service_list, j );
491 const char* psz_service_type =
492 xml_getChildElementValue( p_service_element,
494 if ( !psz_service_type )
496 msg_Warn( p_sd, "No service type found." );
500 int k = strlen( CONTENT_DIRECTORY_SERVICE_TYPE ) - 1;
501 if ( strncmp( CONTENT_DIRECTORY_SERVICE_TYPE,
502 psz_service_type, k ) != 0 )
505 p_server->_i_content_directory_service_version =
508 const char* psz_event_sub_url =
509 xml_getChildElementValue( p_service_element,
511 if ( !psz_event_sub_url )
513 msg_Warn( p_sd, "No event subscription url found." );
517 const char* psz_control_url =
518 xml_getChildElementValue( p_service_element,
520 if ( !psz_control_url )
522 msg_Warn( p_sd, "No control url found." );
526 /* Try to subscribe to ContentDirectory service */
528 char* psz_url = ( char* ) malloc( strlen( psz_base_url ) +
529 strlen( psz_event_sub_url ) + 1 );
532 if ( UpnpResolveURL( psz_base_url, psz_event_sub_url, psz_url ) ==
535 p_server->setContentDirectoryEventURL( psz_url );
536 p_server->subscribeToContentDirectory();
542 /* Try to browse content directory. */
544 psz_url = ( char* ) malloc( strlen( psz_base_url ) +
545 strlen( psz_control_url ) + 1 );
548 if ( UpnpResolveURL( psz_base_url, psz_control_url, psz_url ) ==
551 p_server->setContentDirectoryControlURL( psz_url );
552 p_server->fetchContents();
558 ixmlNodeList_free( p_service_list );
561 ixmlNodeList_free( p_device_list );
565 MediaServer::MediaServer( const char* psz_udn,
566 const char* psz_friendly_name,
567 services_discovery_t* p_sd )
572 _friendly_name = psz_friendly_name;
575 _p_input_item = NULL;
576 _i_content_directory_service_version = 1;
579 MediaServer::~MediaServer()
584 const char* MediaServer::getUDN() const
589 const char* MediaServer::getFriendlyName() const
591 return _friendly_name.c_str();
594 void MediaServer::setContentDirectoryEventURL( const char* psz_url )
596 _content_directory_event_url = psz_url;
599 const char* MediaServer::getContentDirectoryEventURL() const
601 return _content_directory_event_url.c_str();
604 void MediaServer::setContentDirectoryControlURL( const char* psz_url )
606 _content_directory_control_url = psz_url;
609 const char* MediaServer::getContentDirectoryControlURL() const
611 return _content_directory_control_url.c_str();
615 * Subscribes current client handle to Content Directory Service.
616 * CDS exports the server shares to clients.
618 void MediaServer::subscribeToContentDirectory()
620 const char* psz_url = getContentDirectoryEventURL();
623 msg_Dbg( _p_sd, "No subscription url set!" );
627 int i_timeout = 1810;
630 int i_res = UpnpSubscribe( _p_sd->p_sys->client_handle, psz_url, &i_timeout, sid );
632 if ( i_res == UPNP_E_SUCCESS )
634 _i_subscription_timeout = i_timeout;
635 memcpy( _subscription_id, sid, sizeof( Upnp_SID ) );
639 msg_Dbg( _p_sd, "Subscribe failed: '%s': %s",
640 getFriendlyName(), UpnpGetErrorMessage( i_res ) );
644 * Constructs UpnpAction to browse available content.
646 IXML_Document* MediaServer::_browseAction( const char* psz_object_id_,
647 const char* psz_browser_flag_,
648 const char* psz_filter_,
649 const char* psz_starting_index_,
650 const char* psz_requested_count_,
651 const char* psz_sort_criteria_ )
653 IXML_Document* p_action = 0;
654 IXML_Document* p_response = 0;
655 const char* psz_url = getContentDirectoryControlURL();
659 msg_Dbg( _p_sd, "No subscription url set!" );
663 char* psz_service_type = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
665 psz_service_type[strlen( psz_service_type ) - 1] =
666 _i_content_directory_service_version;
670 i_res = UpnpAddToAction( &p_action, "Browse",
671 psz_service_type, "ObjectID", psz_object_id_ );
673 if ( i_res != UPNP_E_SUCCESS )
675 msg_Dbg( _p_sd, "AddToAction 'ObjectID' failed: %s",
676 UpnpGetErrorMessage( i_res ) );
677 goto browseActionCleanup;
680 i_res = UpnpAddToAction( &p_action, "Browse",
681 psz_service_type, "BrowseFlag", psz_browser_flag_ );
683 if ( i_res != UPNP_E_SUCCESS )
685 msg_Dbg( _p_sd, "AddToAction 'BrowseFlag' failed: %s",
686 UpnpGetErrorMessage( i_res ) );
687 goto browseActionCleanup;
690 i_res = UpnpAddToAction( &p_action, "Browse",
691 psz_service_type, "Filter", psz_filter_ );
693 if ( i_res != UPNP_E_SUCCESS )
695 msg_Dbg( _p_sd, "AddToAction 'Filter' failed: %s",
696 UpnpGetErrorMessage( i_res ) );
697 goto browseActionCleanup;
700 i_res = UpnpAddToAction( &p_action, "Browse",
701 psz_service_type, "StartingIndex", psz_starting_index_ );
703 if ( i_res != UPNP_E_SUCCESS )
705 msg_Dbg( _p_sd, "AddToAction 'StartingIndex' failed: %s",
706 UpnpGetErrorMessage( i_res ) );
707 goto browseActionCleanup;
710 i_res = UpnpAddToAction( &p_action, "Browse",
711 psz_service_type, "RequestedCount", psz_requested_count_ );
713 if ( i_res != UPNP_E_SUCCESS )
715 msg_Dbg( _p_sd, "AddToAction 'RequestedCount' failed: %s",
716 UpnpGetErrorMessage( i_res ) );
717 goto browseActionCleanup;
720 i_res = UpnpAddToAction( &p_action, "Browse",
721 psz_service_type, "SortCriteria", psz_sort_criteria_ );
723 if ( i_res != UPNP_E_SUCCESS )
725 msg_Dbg( _p_sd, "AddToAction 'SortCriteria' failed: %s",
726 UpnpGetErrorMessage( i_res ) );
727 goto browseActionCleanup;
730 i_res = UpnpSendAction( _p_sd->p_sys->client_handle,
733 0, /* ignored in SDK, must be NULL */
737 if ( i_res != UPNP_E_SUCCESS )
739 msg_Err( _p_sd, "%s when trying the send() action with URL: %s",
740 UpnpGetErrorMessage( i_res ), psz_url );
742 ixmlDocument_free( p_response );
748 free( psz_service_type );
750 ixmlDocument_free( p_action );
754 void MediaServer::fetchContents()
756 /* Delete previous contents to prevent duplicate entries */
760 services_discovery_RemoveItem( _p_sd, _p_input_item );
761 services_discovery_AddItem( _p_sd, _p_input_item, NULL );
764 Container* root = new Container( 0, "0", getFriendlyName() );
766 _fetchContents( root, 0 );
769 _p_contents->setInputItem( _p_input_item );
771 _buildPlaylist( _p_contents, NULL );
775 * Fetches and parses the UPNP response
777 bool MediaServer::_fetchContents( Container* p_parent, int i_offset )
781 msg_Err( _p_sd, "No parent" );
785 char* psz_starting_index;
786 if( asprintf( &psz_starting_index, "%d", i_offset ) < 0 )
788 msg_Err( _p_sd, "asprintf error:%d", i_offset );
792 IXML_Document* p_response = _browseAction( p_parent->getObjectID(),
793 "BrowseDirectChildren",
795 psz_starting_index, /* StartingIndex */
796 "0", /* RequestedCount */
797 "" /* SortCriteria */
799 free( psz_starting_index );
802 msg_Err( _p_sd, "No response from browse() action" );
806 IXML_Document* p_result = parseBrowseResult( p_response );
807 int i_number_returned = xml_getNumber( p_response, "NumberReturned" );
808 int i_total_matches = xml_getNumber( p_response , "TotalMatches" );
811 msg_Dbg( _p_sd, "i_offset[%d]i_number_returned[%d]_total_matches[%d]\n",
812 i_offset, i_number_returned, i_total_matches );
815 ixmlDocument_free( p_response );
819 msg_Err( _p_sd, "browse() response parsing failed" );
824 msg_Dbg( _p_sd, "Got DIDL document: %s", ixmlPrintDocument( p_result ) );
827 IXML_NodeList* containerNodeList =
828 ixmlDocument_getElementsByTagName( p_result, "container" );
830 if ( containerNodeList )
832 for ( unsigned int i = 0;
833 i < ixmlNodeList_length( containerNodeList ); i++ )
835 IXML_Element* containerElement =
836 ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
838 const char* objectID = ixmlElement_getAttribute( containerElement,
843 const char* title = xml_getChildElementValue( containerElement,
849 Container* container = new Container( p_parent, objectID, title );
850 p_parent->addContainer( container );
851 _fetchContents( container, 0 );
853 ixmlNodeList_free( containerNodeList );
856 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( p_result,
860 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
862 IXML_Element* itemElement =
863 ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
865 const char* objectID =
866 ixmlElement_getAttribute( itemElement, "id" );
872 xml_getChildElementValue( itemElement, "dc:title" );
877 const char* resource =
878 xml_getChildElementValue( itemElement, "res" );
883 const char* psz_duration = xml_getChildElementAttributeValue( itemElement,
887 mtime_t i_duration = -1;
888 int i_hours, i_minutes, i_seconds, i_decis;
892 if( sscanf( psz_duration, "%02d:%02d:%02d.%d",
893 &i_hours, &i_minutes, &i_seconds, &i_decis ))
894 i_duration = INT64_C(1000000) * ( i_hours*3600 +
897 INT64_C(100000) * i_decis;
900 Item* item = new Item( p_parent, objectID, title, resource, i_duration );
901 p_parent->addItem( item );
903 ixmlNodeList_free( itemNodeList );
906 ixmlDocument_free( p_result );
908 if( i_offset + i_number_returned < i_total_matches )
909 return _fetchContents( p_parent, i_offset + i_number_returned );
914 // TODO: Create a permanent fix for the item duplication bug. The current fix
915 // is essentially only a small hack. Although it fixes the problem, it introduces
916 // annoying cosmetic issues with the playlist. For example, when the UPnP Server
917 // rebroadcasts it's directory structure, the VLC Client deletes the old directory
918 // structure, causing the user to go back to the root node of the directory. The
919 // directory is then rebuilt, and the user is forced to traverse through the directory
920 // to find the item they were looking for. Some servers may not push the directory
921 // structure too often, but we cannot rely on this fix.
923 // I have thought up another fix, but this would require certain features to
924 // be present within the VLC services discovery. Currently, services_discovery_AddItem
925 // does not allow the programmer to nest items. It only allows a "2 deep" scope.
926 // An example of the limitation is below:
932 // services_discovery_AddItem will not let the programmer specify a child-node to
933 // insert items into, so we would not be able to do the following:
939 // + Sub Item 1 of Item 2
940 // + Sub-Sub Item 1 of Sub Item 1
942 // This creates a HUGE limitation on what we are able to do. If we were able to do
943 // the above, we could simply preserve the old directory listing, and compare what items
944 // do not exist in the new directory listing, then remove them from the shown listing using
945 // services_discovery_RemoveItem. If new files were introduced within an already existing
946 // container, we could simply do so with services_discovery_AddItem.
949 * Builds playlist based on available input items.
951 void MediaServer::_buildPlaylist( Container* p_parent, input_item_node_t *p_input_node )
953 bool b_send = p_input_node == NULL;
955 p_input_node = input_item_node_Create( p_parent->getInputItem() );
957 for ( unsigned int i = 0; i < p_parent->getNumContainers(); i++ )
959 Container* p_container = p_parent->getContainer( i );
961 input_item_t* p_input_item = input_item_New( "vlc://nop",
962 p_container->getTitle() );
963 input_item_node_t *p_new_node =
964 input_item_node_AppendItem( p_input_node, p_input_item );
966 p_container->setInputItem( p_input_item );
967 _buildPlaylist( p_container, p_new_node );
970 for ( unsigned int i = 0; i < p_parent->getNumItems(); i++ )
972 Item* p_item = p_parent->getItem( i );
974 input_item_t* p_input_item = input_item_NewExt( p_item->getResource(),
979 p_item->getDuration() );
981 assert( p_input_item );
982 input_item_node_AppendItem( p_input_node, p_input_item );
983 p_item->setInputItem( p_input_item );
987 input_item_node_PostAndDelete( p_input_node );
990 void MediaServer::setInputItem( input_item_t* p_input_item )
992 if( _p_input_item == p_input_item )
996 vlc_gc_decref( _p_input_item );
998 vlc_gc_incref( p_input_item );
999 _p_input_item = p_input_item;
1002 input_item_t* MediaServer::getInputItem() const
1004 return _p_input_item;
1007 bool MediaServer::compareSID( const char* psz_sid )
1009 return ( strncmp( _subscription_id, psz_sid, sizeof( Upnp_SID ) ) == 0 );
1014 * MediaServerList class
1016 MediaServerList::MediaServerList( services_discovery_t* p_sd )
1021 MediaServerList::~MediaServerList()
1023 for ( unsigned int i = 0; i < _list.size(); i++ )
1029 bool MediaServerList::addServer( MediaServer* p_server )
1031 input_item_t* p_input_item = NULL;
1032 if ( getServer( p_server->getUDN() ) != 0 ) return false;
1034 msg_Dbg( _p_sd, "Adding server '%s' with uuid '%s'", p_server->getFriendlyName(), p_server->getUDN() );
1036 p_input_item = input_item_New( "vlc://nop", p_server->getFriendlyName() );
1038 input_item_SetDescription( p_input_item, p_server->getUDN() );
1040 p_server->setInputItem( p_input_item );
1042 services_discovery_AddItem( _p_sd, p_input_item, NULL );
1044 _list.push_back( p_server );
1049 MediaServer* MediaServerList::getServer( const char* psz_udn )
1051 MediaServer* p_result = 0;
1053 for ( unsigned int i = 0; i < _list.size(); i++ )
1055 if( strcmp( psz_udn, _list[i]->getUDN() ) == 0 )
1057 p_result = _list[i];
1065 MediaServer* MediaServerList::getServerBySID( const char* psz_sid )
1067 MediaServer* p_server = 0;
1069 for ( unsigned int i = 0; i < _list.size(); i++ )
1071 if ( _list[i]->compareSID( psz_sid ) )
1073 p_server = _list[i];
1081 void MediaServerList::removeServer( const char* psz_udn )
1083 MediaServer* p_server = getServer( psz_udn );
1084 if ( !p_server ) return;
1086 msg_Dbg( _p_sd, "Removing server '%s'", p_server->getFriendlyName() );
1088 services_discovery_RemoveItem( _p_sd, p_server->getInputItem() );
1090 std::vector<MediaServer*>::iterator it;
1091 for ( it = _list.begin(); it != _list.end(); ++it )
1093 if ( *it == p_server )
1106 Item::Item( Container* p_parent, const char* psz_object_id, const char* psz_title,
1107 const char* psz_resource, mtime_t i_duration )
1111 _objectID = psz_object_id;
1113 _resource = psz_resource;
1114 _duration = i_duration;
1116 _p_input_item = NULL;
1122 vlc_gc_decref( _p_input_item );
1125 const char* Item::getObjectID() const
1127 return _objectID.c_str();
1130 const char* Item::getTitle() const
1132 return _title.c_str();
1135 const char* Item::getResource() const
1137 return _resource.c_str();
1140 mtime_t Item::getDuration() const
1145 void Item::setInputItem( input_item_t* p_input_item )
1147 if( _p_input_item == p_input_item )
1151 vlc_gc_decref( _p_input_item );
1153 vlc_gc_incref( p_input_item );
1154 _p_input_item = p_input_item;
1160 Container::Container( Container* p_parent,
1161 const char* psz_object_id,
1162 const char* psz_title )
1166 _objectID = psz_object_id;
1169 _p_input_item = NULL;
1172 Container::~Container()
1174 for ( unsigned int i = 0; i < _containers.size(); i++ )
1176 delete _containers[i];
1179 for ( unsigned int i = 0; i < _items.size(); i++ )
1185 vlc_gc_decref( _p_input_item );
1188 void Container::addItem( Item* item )
1190 _items.push_back( item );
1193 void Container::addContainer( Container* p_container )
1195 _containers.push_back( p_container );
1198 const char* Container::getObjectID() const
1200 return _objectID.c_str();
1203 const char* Container::getTitle() const
1205 return _title.c_str();
1208 unsigned int Container::getNumItems() const
1210 return _items.size();
1213 unsigned int Container::getNumContainers() const
1215 return _containers.size();
1218 Item* Container::getItem( unsigned int i_index ) const
1220 if ( i_index < _items.size() ) return _items[i_index];
1224 Container* Container::getContainer( unsigned int i_index ) const
1226 if ( i_index < _containers.size() ) return _containers[i_index];
1230 Container* Container::getParent()
1235 void Container::setInputItem( input_item_t* p_input_item )
1237 if( _p_input_item == p_input_item )
1241 vlc_gc_decref( _p_input_item );
1243 vlc_gc_incref( p_input_item );
1244 _p_input_item = p_input_item;
1247 input_item_t* Container::getInputItem() const
1249 return _p_input_item;