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 const char* xml_getChildElementValue( IXML_Document* p_doc,
93 const char* psz_tag_name );
95 IXML_Document* parseBrowseResult( IXML_Document* p_doc );
97 int parseBrowseNumberValue( IXML_Document* p_doc,
98 const char* psz_tag_name );
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 /* Initialize on first IPv4-capable adapter and first open port
114 * TODO: use UpnpInit2() to utilize IPv6.
116 i_res = UpnpInit( 0, 0 );
117 if( i_res != UPNP_E_SUCCESS )
119 msg_Err( p_sd, "Initialization failed: %s", UpnpGetErrorMessage( i_res ) );
124 p_sys->p_server_list = new MediaServerList( p_sd );
125 vlc_mutex_init( &p_sys->callback_lock );
127 /* Register a control point */
128 i_res = UpnpRegisterClient( Callback, p_sd, &p_sys->client_handle );
129 if( i_res != UPNP_E_SUCCESS )
131 msg_Err( p_sd, "Client registration failed: %s", UpnpGetErrorMessage( i_res ) );
132 Close( (vlc_object_t*) p_sd );
136 /* Search for media servers */
137 i_res = UpnpSearchAsync( p_sys->client_handle, 5,
138 MEDIA_SERVER_DEVICE_TYPE, p_sd );
139 if( i_res != UPNP_E_SUCCESS )
141 msg_Err( p_sd, "Error sending search request: %s", UpnpGetErrorMessage( i_res ) );
142 Close( (vlc_object_t*) p_sd );
146 /* libupnp does not treat a maximum content length of 0 as unlimited
147 * until 64dedf (~ pupnp v1.6.7) and provides no sane way to discriminate
148 * between versions */
149 if( (i_res = UpnpSetMaxContentLength( INT_MAX )) != UPNP_E_SUCCESS )
151 msg_Err( p_sd, "Failed to set maximum content length: %s",
152 UpnpGetErrorMessage( i_res ));
154 Close( (vlc_object_t*) p_sd );
162 * Releases resources.
164 static void Close( vlc_object_t *p_this )
166 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
168 UpnpUnRegisterClient( p_sd->p_sys->client_handle );
171 delete p_sd->p_sys->p_server_list;
172 vlc_mutex_destroy( &p_sd->p_sys->callback_lock );
177 /* XML utility functions */
180 * Returns the value of a child element, or NULL on error
182 const char* xml_getChildElementValue( IXML_Element* p_parent,
183 const char* psz_tag_name_ )
185 if ( !p_parent ) return NULL;
186 if ( !psz_tag_name_ ) return NULL;
188 IXML_NodeList* p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name_ );
189 if ( !p_node_list ) return NULL;
191 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
192 ixmlNodeList_free( p_node_list );
193 if ( !p_element ) return NULL;
195 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_element );
196 if ( !p_text_node ) return NULL;
198 return ixmlNode_getNodeValue( p_text_node );
202 * Returns the value of a child element's attribute, or NULL on error
204 const char* xml_getChildElementAttributeValue( IXML_Element* p_parent,
205 const char* psz_tag_name_,
206 const char* psz_attribute_ )
208 if ( !p_parent ) return NULL;
209 if ( !psz_tag_name_ ) return NULL;
210 if ( !psz_attribute_ ) return NULL;
212 IXML_NodeList* p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name_ );
213 if ( !p_node_list ) return NULL;
215 IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
216 ixmlNodeList_free( p_node_list );
217 if ( !p_element ) return NULL;
219 return ixmlElement_getAttribute( (IXML_Element*) p_element, psz_attribute_ );
223 * Returns the value of a child element, or NULL on error
225 const char* xml_getChildElementValue( IXML_Document* p_doc,
226 const char* psz_tag_name_ )
228 if ( !p_doc ) return 0;
229 if ( !psz_tag_name_ ) return 0;
231 IXML_NodeList* p_result_list = ixmlDocument_getElementsByTagName( p_doc,
234 if ( !p_result_list ) return 0;
236 IXML_Node* p_result_node = ixmlNodeList_item( p_result_list, 0 );
238 ixmlNodeList_free( p_result_list );
240 if ( !p_result_node ) return 0;
242 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_result_node );
243 if ( !p_text_node ) return 0;
245 return ixmlNode_getNodeValue( p_text_node );
249 * Extracts the result document from a SOAP response
251 IXML_Document* parseBrowseResult( IXML_Document* p_doc )
253 ixmlRelaxParser( 1 );
255 const char* psz_result_string = xml_getChildElementValue( p_doc, "Result" );
256 if( !psz_result_string ) return 0;
258 IXML_Document* p_browse_doc = ixmlParseBuffer( psz_result_string );
264 * Get the number value from a SOAP response
266 int parseBrowseNumberValue( IXML_Document* p_doc,
267 const char* psz_tag_name_ )
269 ixmlRelaxParser( 1 );
271 const char* psz_number_string = xml_getChildElementValue( p_doc,
273 if( !psz_number_string ) return 0;
276 long l = strtol( psz_number_string, &psz_end, 10 );
277 if( *psz_end || l < 0 || l > INT_MAX )
288 * Handles all UPnP events
290 static int Callback( Upnp_EventType event_type, void* p_event, void* p_user_data )
292 services_discovery_t* p_sd = ( services_discovery_t* ) p_user_data;
293 services_discovery_sys_t* p_sys = p_sd->p_sys;
294 vlc_mutex_locker locker( &p_sys->callback_lock );
298 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
299 case UPNP_DISCOVERY_SEARCH_RESULT:
301 struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
303 IXML_Document *p_description_doc = 0;
306 i_res = UpnpDownloadXmlDoc( p_discovery->Location, &p_description_doc );
307 if ( i_res != UPNP_E_SUCCESS )
309 msg_Warn( p_sd, "Could not download device description! "
310 "Fetching data from %s failed: %s",
311 p_discovery->Location, UpnpGetErrorMessage( i_res ) );
315 MediaServer::parseDeviceDescription( p_description_doc,
316 p_discovery->Location, p_sd );
318 ixmlDocument_free( p_description_doc );
322 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
324 struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
326 p_sys->p_server_list->removeServer( p_discovery->DeviceId );
331 case UPNP_EVENT_RECEIVED:
333 Upnp_Event* p_e = ( Upnp_Event* )p_event;
335 MediaServer* p_server = p_sys->p_server_list->getServerBySID( p_e->Sid );
336 if ( p_server ) p_server->fetchContents();
340 case UPNP_EVENT_AUTORENEWAL_FAILED:
341 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
345 Upnp_Event_Subscribe* p_s = ( Upnp_Event_Subscribe* )p_event;
347 MediaServer* p_server = p_sys->p_server_list->getServerBySID( p_s->Sid );
348 if ( p_server ) p_server->subscribeToContentDirectory();
352 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
353 msg_Warn( p_sd, "subscription complete" );
356 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
357 msg_Warn( p_sd, "search timeout" );
361 msg_Err( p_sd, "Unhandled event, please report ( type=%d )", event_type );
365 return UPNP_E_SUCCESS;
370 * Local class implementations.
377 void MediaServer::parseDeviceDescription( IXML_Document* p_doc,
378 const char* p_location,
379 services_discovery_t* p_sd )
383 msg_Err( p_sd, "Null IXML_Document" );
389 msg_Err( p_sd, "Null location" );
393 const char* psz_base_url = p_location;
395 /* Try to extract baseURL */
396 IXML_NodeList* p_url_list = ixmlDocument_getElementsByTagName( p_doc, "URLBase" );
400 if ( IXML_Node* p_url_node = ixmlNodeList_item( p_url_list, 0 ) )
402 IXML_Node* p_text_node = ixmlNode_getFirstChild( p_url_node );
403 if ( p_text_node ) psz_base_url = ixmlNode_getNodeValue( p_text_node );
406 ixmlNodeList_free( p_url_list );
410 IXML_NodeList* p_device_list =
411 ixmlDocument_getElementsByTagName( p_doc, "device" );
415 for ( unsigned int i = 0; i < ixmlNodeList_length( p_device_list ); i++ )
417 IXML_Element* p_device_element =
418 ( IXML_Element* ) ixmlNodeList_item( p_device_list, i );
420 const char* psz_device_type = xml_getChildElementValue( p_device_element,
422 if ( !psz_device_type )
424 msg_Warn( p_sd, "No deviceType found!" );
428 if ( strncmp( MEDIA_SERVER_DEVICE_TYPE, psz_device_type,
429 strlen( MEDIA_SERVER_DEVICE_TYPE ) - 1 ) != 0 )
432 const char* psz_udn = xml_getChildElementValue( p_device_element, "UDN" );
435 msg_Warn( p_sd, "No UDN!" );
439 /* Check if server is already added */
440 if ( p_sd->p_sys->p_server_list->getServer( psz_udn ) != 0 )
442 msg_Warn( p_sd, "Server with uuid '%s' already exists.", psz_udn );
446 const char* psz_friendly_name =
447 xml_getChildElementValue( p_device_element,
450 if ( !psz_friendly_name )
452 msg_Dbg( p_sd, "No friendlyName!" );
456 MediaServer* p_server = new MediaServer( psz_udn, psz_friendly_name, p_sd );
458 if ( !p_sd->p_sys->p_server_list->addServer( p_server ) )
465 /* Check for ContentDirectory service. */
466 IXML_NodeList* p_service_list =
467 ixmlElement_getElementsByTagName( p_device_element,
469 if ( p_service_list )
471 for ( unsigned int j = 0;
472 j < ixmlNodeList_length( p_service_list ); j++ )
474 IXML_Element* p_service_element =
475 ( IXML_Element* ) ixmlNodeList_item( p_service_list, j );
477 const char* psz_service_type =
478 xml_getChildElementValue( p_service_element,
480 if ( !psz_service_type )
482 msg_Warn( p_sd, "No service type found." );
486 int k = strlen( CONTENT_DIRECTORY_SERVICE_TYPE ) - 1;
487 if ( strncmp( CONTENT_DIRECTORY_SERVICE_TYPE,
488 psz_service_type, k ) != 0 )
491 p_server->_i_content_directory_service_version =
494 const char* psz_event_sub_url =
495 xml_getChildElementValue( p_service_element,
497 if ( !psz_event_sub_url )
499 msg_Warn( p_sd, "No event subscription url found." );
503 const char* psz_control_url =
504 xml_getChildElementValue( p_service_element,
506 if ( !psz_control_url )
508 msg_Warn( p_sd, "No control url found." );
512 /* Try to subscribe to ContentDirectory service */
514 char* psz_url = ( char* ) malloc( strlen( psz_base_url ) +
515 strlen( psz_event_sub_url ) + 1 );
518 if ( UpnpResolveURL( psz_base_url, psz_event_sub_url, psz_url ) ==
521 p_server->setContentDirectoryEventURL( psz_url );
522 p_server->subscribeToContentDirectory();
528 /* Try to browse content directory. */
530 psz_url = ( char* ) malloc( strlen( psz_base_url ) +
531 strlen( psz_control_url ) + 1 );
534 if ( UpnpResolveURL( psz_base_url, psz_control_url, psz_url ) ==
537 p_server->setContentDirectoryControlURL( psz_url );
538 p_server->fetchContents();
544 ixmlNodeList_free( p_service_list );
547 ixmlNodeList_free( p_device_list );
551 MediaServer::MediaServer( const char* psz_udn,
552 const char* psz_friendly_name,
553 services_discovery_t* p_sd )
558 _friendly_name = psz_friendly_name;
561 _p_input_item = NULL;
562 _i_content_directory_service_version = 1;
565 MediaServer::~MediaServer()
570 const char* MediaServer::getUDN() const
575 const char* MediaServer::getFriendlyName() const
577 return _friendly_name.c_str();
580 void MediaServer::setContentDirectoryEventURL( const char* psz_url )
582 _content_directory_event_url = psz_url;
585 const char* MediaServer::getContentDirectoryEventURL() const
587 return _content_directory_event_url.c_str();
590 void MediaServer::setContentDirectoryControlURL( const char* psz_url )
592 _content_directory_control_url = psz_url;
595 const char* MediaServer::getContentDirectoryControlURL() const
597 return _content_directory_control_url.c_str();
601 * Subscribes current client handle to Content Directory Service.
602 * CDS exports the server shares to clients.
604 void MediaServer::subscribeToContentDirectory()
606 const char* psz_url = getContentDirectoryEventURL();
609 msg_Dbg( _p_sd, "No subscription url set!" );
613 int i_timeout = 1810;
616 int i_res = UpnpSubscribe( _p_sd->p_sys->client_handle, psz_url, &i_timeout, sid );
618 if ( i_res == UPNP_E_SUCCESS )
620 _i_subscription_timeout = i_timeout;
621 memcpy( _subscription_id, sid, sizeof( Upnp_SID ) );
625 msg_Dbg( _p_sd, "Subscribe failed: '%s': %s",
626 getFriendlyName(), UpnpGetErrorMessage( i_res ) );
630 * Constructs UpnpAction to browse available content.
632 IXML_Document* MediaServer::_browseAction( const char* psz_object_id_,
633 const char* psz_browser_flag_,
634 const char* psz_filter_,
635 const char* psz_starting_index_,
636 const char* psz_requested_count_,
637 const char* psz_sort_criteria_ )
639 IXML_Document* p_action = 0;
640 IXML_Document* p_response = 0;
641 const char* psz_url = getContentDirectoryControlURL();
645 msg_Dbg( _p_sd, "No subscription url set!" );
649 char* psz_service_type = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
651 psz_service_type[strlen( psz_service_type ) - 1] =
652 _i_content_directory_service_version;
656 i_res = UpnpAddToAction( &p_action, "Browse",
657 psz_service_type, "ObjectID", psz_object_id_ );
659 if ( i_res != UPNP_E_SUCCESS )
661 msg_Dbg( _p_sd, "AddToAction 'ObjectID' failed: %s",
662 UpnpGetErrorMessage( i_res ) );
663 goto browseActionCleanup;
666 i_res = UpnpAddToAction( &p_action, "Browse",
667 psz_service_type, "BrowseFlag", psz_browser_flag_ );
669 if ( i_res != UPNP_E_SUCCESS )
671 msg_Dbg( _p_sd, "AddToAction 'BrowseFlag' failed: %s",
672 UpnpGetErrorMessage( i_res ) );
673 goto browseActionCleanup;
676 i_res = UpnpAddToAction( &p_action, "Browse",
677 psz_service_type, "Filter", psz_filter_ );
679 if ( i_res != UPNP_E_SUCCESS )
681 msg_Dbg( _p_sd, "AddToAction 'Filter' failed: %s",
682 UpnpGetErrorMessage( i_res ) );
683 goto browseActionCleanup;
686 i_res = UpnpAddToAction( &p_action, "Browse",
687 psz_service_type, "StartingIndex", psz_starting_index_ );
689 if ( i_res != UPNP_E_SUCCESS )
691 msg_Dbg( _p_sd, "AddToAction 'StartingIndex' failed: %s",
692 UpnpGetErrorMessage( i_res ) );
693 goto browseActionCleanup;
696 i_res = UpnpAddToAction( &p_action, "Browse",
697 psz_service_type, "RequestedCount", psz_requested_count_ );
699 if ( i_res != UPNP_E_SUCCESS )
701 msg_Dbg( _p_sd, "AddToAction 'RequestedCount' failed: %s",
702 UpnpGetErrorMessage( i_res ) );
703 goto browseActionCleanup;
706 i_res = UpnpAddToAction( &p_action, "Browse",
707 psz_service_type, "SortCriteria", psz_sort_criteria_ );
709 if ( i_res != UPNP_E_SUCCESS )
711 msg_Dbg( _p_sd, "AddToAction 'SortCriteria' failed: %s",
712 UpnpGetErrorMessage( i_res ) );
713 goto browseActionCleanup;
716 i_res = UpnpSendAction( _p_sd->p_sys->client_handle,
719 0, /* ignored in SDK, must be NULL */
723 if ( i_res != UPNP_E_SUCCESS )
725 msg_Err( _p_sd, "%s when trying the send() action with URL: %s",
726 UpnpGetErrorMessage( i_res ), psz_url );
728 ixmlDocument_free( p_response );
734 free( psz_service_type );
736 ixmlDocument_free( p_action );
740 void MediaServer::fetchContents()
742 /* Delete previous contents to prevent duplicate entries */
746 services_discovery_RemoveItem( _p_sd, _p_input_item );
747 services_discovery_AddItem( _p_sd, _p_input_item, NULL );
750 Container* root = new Container( 0, "0", getFriendlyName() );
752 _fetchContents( root, 0 );
755 _p_contents->setInputItem( _p_input_item );
757 _buildPlaylist( _p_contents, NULL );
761 * Fetches and parses the UPNP response
763 bool MediaServer::_fetchContents( Container* p_parent, int i_starting_index )
767 msg_Err( _p_sd, "No parent" );
771 char* psz_starting_index;
772 if( asprintf( &psz_starting_index, "%d", i_starting_index ) < 0 )
774 msg_Err( _p_sd, "asprintf error:%d", i_starting_index );
778 IXML_Document* p_response = _browseAction( p_parent->getObjectID(),
779 "BrowseDirectChildren",
781 psz_starting_index, /* StartingIndex */
782 "0", /* RequestedCount */
783 "" /* SortCriteria */
785 free( psz_starting_index );
788 msg_Err( _p_sd, "No response from browse() action" );
792 IXML_Document* p_result = parseBrowseResult( p_response );
793 int i_number_returned = parseBrowseNumberValue( p_response, "NumberReturned" );
794 int i_total_matches = parseBrowseNumberValue( p_response , "TotalMatches" );
796 msg_Dbg( _p_sd, "i_starting_index[%d]i_number_returned[%d]_total_matches[%d]\n",
797 i_starting_index, i_number_returned, i_total_matches );
799 ixmlDocument_free( p_response );
803 msg_Err( _p_sd, "browse() response parsing failed" );
809 msg_Dbg( _p_sd, "Got DIDL document: %s",
810 ixmlPrintDocument( p_result ) );
814 IXML_NodeList* containerNodeList =
815 ixmlDocument_getElementsByTagName( p_result, "container" );
817 if ( containerNodeList )
819 for ( unsigned int i = 0;
820 i < ixmlNodeList_length( containerNodeList ); i++ )
822 IXML_Element* containerElement =
823 ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
825 const char* objectID = ixmlElement_getAttribute( containerElement,
830 const char* title = xml_getChildElementValue( containerElement,
836 Container* container = new Container( p_parent, objectID, title );
837 p_parent->addContainer( container );
838 _fetchContents( container, 0 );
840 ixmlNodeList_free( containerNodeList );
843 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( p_result,
847 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
849 IXML_Element* itemElement =
850 ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
852 const char* objectID =
853 ixmlElement_getAttribute( itemElement, "id" );
859 xml_getChildElementValue( itemElement, "dc:title" );
864 const char* resource =
865 xml_getChildElementValue( itemElement, "res" );
870 const char* psz_duration = xml_getChildElementAttributeValue( itemElement,
874 mtime_t i_duration = -1;
875 int i_hours, i_minutes, i_seconds, i_decis;
879 if( sscanf( psz_duration, "%02d:%02d:%02d.%d",
880 &i_hours, &i_minutes, &i_seconds, &i_decis ))
881 i_duration = INT64_C(1000000) * ( i_hours*3600 +
884 INT64_C(100000) * i_decis;
887 Item* item = new Item( p_parent, objectID, title, resource, i_duration );
888 p_parent->addItem( item );
890 ixmlNodeList_free( itemNodeList );
893 ixmlDocument_free( p_result );
895 if( i_starting_index + i_number_returned < i_total_matches )
897 if( !_fetchContents( p_parent, i_starting_index + i_number_returned ) )
906 // TODO: Create a permanent fix for the item duplication bug. The current fix
907 // is essentially only a small hack. Although it fixes the problem, it introduces
908 // annoying cosmetic issues with the playlist. For example, when the UPnP Server
909 // rebroadcasts it's directory structure, the VLC Client deletes the old directory
910 // structure, causing the user to go back to the root node of the directory. The
911 // directory is then rebuilt, and the user is forced to traverse through the directory
912 // to find the item they were looking for. Some servers may not push the directory
913 // structure too often, but we cannot rely on this fix.
915 // I have thought up another fix, but this would require certain features to
916 // be present within the VLC services discovery. Currently, services_discovery_AddItem
917 // does not allow the programmer to nest items. It only allows a "2 deep" scope.
918 // An example of the limitation is below:
924 // services_discovery_AddItem will not let the programmer specify a child-node to
925 // insert items into, so we would not be able to do the following:
931 // + Sub Item 1 of Item 2
932 // + Sub-Sub Item 1 of Sub Item 1
934 // This creates a HUGE limitation on what we are able to do. If we were able to do
935 // the above, we could simply preserve the old directory listing, and compare what items
936 // do not exist in the new directory listing, then remove them from the shown listing using
937 // services_discovery_RemoveItem. If new files were introduced within an already existing
938 // container, we could simply do so with services_discovery_AddItem.
941 * Builds playlist based on available input items.
943 void MediaServer::_buildPlaylist( Container* p_parent, input_item_node_t *p_input_node )
945 bool b_send = p_input_node == NULL;
947 p_input_node = input_item_node_Create( p_parent->getInputItem() );
949 for ( unsigned int i = 0; i < p_parent->getNumContainers(); i++ )
951 Container* p_container = p_parent->getContainer( i );
953 input_item_t* p_input_item = input_item_New( "vlc://nop",
954 p_container->getTitle() );
955 input_item_node_t *p_new_node =
956 input_item_node_AppendItem( p_input_node, p_input_item );
958 p_container->setInputItem( p_input_item );
959 _buildPlaylist( p_container, p_new_node );
962 for ( unsigned int i = 0; i < p_parent->getNumItems(); i++ )
964 Item* p_item = p_parent->getItem( i );
966 input_item_t* p_input_item = input_item_NewExt( p_item->getResource(),
971 p_item->getDuration() );
973 assert( p_input_item );
974 input_item_node_AppendItem( p_input_node, p_input_item );
975 p_item->setInputItem( p_input_item );
979 input_item_node_PostAndDelete( p_input_node );
982 void MediaServer::setInputItem( input_item_t* p_input_item )
984 if( _p_input_item == p_input_item )
988 vlc_gc_decref( _p_input_item );
990 vlc_gc_incref( p_input_item );
991 _p_input_item = p_input_item;
994 input_item_t* MediaServer::getInputItem() const
996 return _p_input_item;
999 bool MediaServer::compareSID( const char* psz_sid )
1001 return ( strncmp( _subscription_id, psz_sid, sizeof( Upnp_SID ) ) == 0 );
1006 * MediaServerList class
1008 MediaServerList::MediaServerList( services_discovery_t* p_sd )
1013 MediaServerList::~MediaServerList()
1015 for ( unsigned int i = 0; i < _list.size(); i++ )
1021 bool MediaServerList::addServer( MediaServer* p_server )
1023 input_item_t* p_input_item = NULL;
1024 if ( getServer( p_server->getUDN() ) != 0 ) return false;
1026 msg_Dbg( _p_sd, "Adding server '%s' with uuid '%s'", p_server->getFriendlyName(), p_server->getUDN() );
1028 p_input_item = input_item_New( "vlc://nop", p_server->getFriendlyName() );
1030 input_item_SetDescription( p_input_item, p_server->getUDN() );
1032 p_server->setInputItem( p_input_item );
1034 services_discovery_AddItem( _p_sd, p_input_item, NULL );
1036 _list.push_back( p_server );
1041 MediaServer* MediaServerList::getServer( const char* psz_udn )
1043 MediaServer* p_result = 0;
1045 for ( unsigned int i = 0; i < _list.size(); i++ )
1047 if( strcmp( psz_udn, _list[i]->getUDN() ) == 0 )
1049 p_result = _list[i];
1057 MediaServer* MediaServerList::getServerBySID( const char* psz_sid )
1059 MediaServer* p_server = 0;
1061 for ( unsigned int i = 0; i < _list.size(); i++ )
1063 if ( _list[i]->compareSID( psz_sid ) )
1065 p_server = _list[i];
1073 void MediaServerList::removeServer( const char* psz_udn )
1075 MediaServer* p_server = getServer( psz_udn );
1076 if ( !p_server ) return;
1078 msg_Dbg( _p_sd, "Removing server '%s'", p_server->getFriendlyName() );
1080 services_discovery_RemoveItem( _p_sd, p_server->getInputItem() );
1082 std::vector<MediaServer*>::iterator it;
1083 for ( it = _list.begin(); it != _list.end(); ++it )
1085 if ( *it == p_server )
1098 Item::Item( Container* p_parent, const char* psz_object_id, const char* psz_title,
1099 const char* psz_resource, mtime_t i_duration )
1103 _objectID = psz_object_id;
1105 _resource = psz_resource;
1106 _duration = i_duration;
1108 _p_input_item = NULL;
1114 vlc_gc_decref( _p_input_item );
1117 const char* Item::getObjectID() const
1119 return _objectID.c_str();
1122 const char* Item::getTitle() const
1124 return _title.c_str();
1127 const char* Item::getResource() const
1129 return _resource.c_str();
1132 mtime_t Item::getDuration() const
1137 void Item::setInputItem( input_item_t* p_input_item )
1139 if( _p_input_item == p_input_item )
1143 vlc_gc_decref( _p_input_item );
1145 vlc_gc_incref( p_input_item );
1146 _p_input_item = p_input_item;
1152 Container::Container( Container* p_parent,
1153 const char* psz_object_id,
1154 const char* psz_title )
1158 _objectID = psz_object_id;
1161 _p_input_item = NULL;
1164 Container::~Container()
1166 for ( unsigned int i = 0; i < _containers.size(); i++ )
1168 delete _containers[i];
1171 for ( unsigned int i = 0; i < _items.size(); i++ )
1177 vlc_gc_decref( _p_input_item );
1180 void Container::addItem( Item* item )
1182 _items.push_back( item );
1185 void Container::addContainer( Container* p_container )
1187 _containers.push_back( p_container );
1190 const char* Container::getObjectID() const
1192 return _objectID.c_str();
1195 const char* Container::getTitle() const
1197 return _title.c_str();
1200 unsigned int Container::getNumItems() const
1202 return _items.size();
1205 unsigned int Container::getNumContainers() const
1207 return _containers.size();
1210 Item* Container::getItem( unsigned int i_index ) const
1212 if ( i_index < _items.size() ) return _items[i_index];
1216 Container* Container::getContainer( unsigned int i_index ) const
1218 if ( i_index < _containers.size() ) return _containers[i_index];
1222 Container* Container::getParent()
1227 void Container::setInputItem( input_item_t* p_input_item )
1229 if( _p_input_item == p_input_item )
1233 vlc_gc_decref( _p_input_item );
1235 vlc_gc_incref( p_input_item );
1236 _p_input_item = p_input_item;
1239 input_item_t* Container::getInputItem() const
1241 return _p_input_item;