1 /*****************************************************************************
2 * Upnp_intel.cpp : UPnP discovery module (Intel SDK)
3 *****************************************************************************
4 * Copyright (C) 2004-2008 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 *****************************************************************************/
29 \TODO: Debug messages: "__FILE__, __LINE__" ok ???, Wrn/Err ???
30 \TODO: Change names to VLC standard ???
37 #include "upnp_intel.hpp"
39 #include <vlc_plugin.h>
40 #include <vlc_services_discovery.h>
44 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
45 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
48 struct services_discovery_sys_t
50 UpnpClient_Handle clientHandle;
51 MediaServerList* serverList;
52 vlc_mutex_t callbackLock;
55 // VLC callback prototypes
56 static int Open( vlc_object_t* );
57 static void Close( vlc_object_t* );
62 set_shortname( "UPnP" );
63 set_description( N_( "Universal Plug'n'Play discovery" ) );
64 set_category( CAT_PLAYLIST );
65 set_subcategory( SUBCAT_PLAYLIST_SD );
66 set_capability( "services_discovery", 0 );
67 set_callbacks( Open, Close );
73 static int Callback( Upnp_EventType eventType, void* event, void* user_data );
75 const char* xml_getChildElementValue( IXML_Element* parent,
76 const char* tagName );
78 IXML_Document* parseBrowseResult( IXML_Document* doc );
83 static int Open( vlc_object_t *p_this )
86 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
87 services_discovery_sys_t *p_sys = ( services_discovery_sys_t * )
88 calloc( 1, sizeof( services_discovery_sys_t ) );
90 if(!(p_sd->p_sys = p_sys))
93 res = UpnpInit( 0, 0 );
94 if( res != UPNP_E_SUCCESS )
96 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
101 p_sys->serverList = new MediaServerList( p_sd );
102 vlc_mutex_init( &p_sys->callbackLock );
104 res = UpnpRegisterClient( Callback, p_sd, &p_sys->clientHandle );
105 if( res != UPNP_E_SUCCESS )
107 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
108 Close( (vlc_object_t*) p_sd );
112 res = UpnpSearchAsync( p_sys->clientHandle, 5,
113 MEDIA_SERVER_DEVICE_TYPE, p_sd );
115 if( res != UPNP_E_SUCCESS )
117 msg_Err( p_sd, "%s", UpnpGetErrorMessage( res ) );
118 Close( (vlc_object_t*) p_sd );
125 static void Close( vlc_object_t *p_this )
127 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
130 delete p_sd->p_sys->serverList;
131 vlc_mutex_destroy( &p_sd->p_sys->callbackLock );
136 // XML utility functions:
138 // Returns the value of a child element, or 0 on error
139 const char* xml_getChildElementValue( IXML_Element* parent,
140 const char* tagName )
142 if ( !parent ) return 0;
143 if ( !tagName ) return 0;
145 char* s = strdup( tagName );
146 IXML_NodeList* nodeList = ixmlElement_getElementsByTagName( parent, s );
148 if ( !nodeList ) return 0;
150 IXML_Node* element = ixmlNodeList_item( nodeList, 0 );
151 ixmlNodeList_free( nodeList );
152 if ( !element ) return 0;
154 IXML_Node* textNode = ixmlNode_getFirstChild( element );
155 if ( !textNode ) return 0;
157 return ixmlNode_getNodeValue( textNode );
160 // Extracts the result document from a SOAP response
161 IXML_Document* parseBrowseResult( IXML_Document* doc )
165 if ( !doc ) return 0;
167 IXML_NodeList* resultList = ixmlDocument_getElementsByTagName( doc,
170 if ( !resultList ) return 0;
172 IXML_Node* resultNode = ixmlNodeList_item( resultList, 0 );
174 ixmlNodeList_free( resultList );
176 if ( !resultNode ) return 0;
178 IXML_Node* textNode = ixmlNode_getFirstChild( resultNode );
179 if ( !textNode ) return 0;
181 const char* resultString = ixmlNode_getNodeValue( textNode );
182 char* resultXML = strdup( resultString );
184 IXML_Document* browseDoc = ixmlParseBuffer( resultXML );
192 // Handles all UPnP events
193 static int Callback( Upnp_EventType eventType, void* event, void* user_data )
195 services_discovery_t *p_sd = ( services_discovery_t* ) user_data;
196 services_discovery_sys_t* p_sys = p_sd->p_sys;
197 vlc_mutex_locker locker( &p_sys->callbackLock );
199 switch( eventType ) {
201 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
202 case UPNP_DISCOVERY_SEARCH_RESULT:
204 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
206 IXML_Document *descriptionDoc = 0;
209 res = UpnpDownloadXmlDoc( discovery->Location, &descriptionDoc );
210 if ( res != UPNP_E_SUCCESS )
213 "%s:%d: Could not download device description!",
214 __FILE__, __LINE__ );
218 MediaServer::parseDeviceDescription( descriptionDoc,
219 discovery->Location, p_sd );
221 ixmlDocument_free( descriptionDoc );
225 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
227 struct Upnp_Discovery* discovery = ( struct Upnp_Discovery* )event;
229 p_sys->serverList->removeServer( discovery->DeviceId );
233 case UPNP_EVENT_RECEIVED:
235 Upnp_Event* e = ( Upnp_Event* )event;
237 MediaServer* server = p_sys->serverList->getServerBySID( e->Sid );
238 if ( server ) server->fetchContents();
242 case UPNP_EVENT_AUTORENEWAL_FAILED:
243 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
247 Upnp_Event_Subscribe* s = ( Upnp_Event_Subscribe* )event;
249 MediaServer* server = p_sys->serverList->getServerBySID( s->Sid );
250 if ( server ) server->subscribeToContentDirectory();
254 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
255 msg_Warn( p_sd, "subscription complete" );
258 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
259 msg_Warn( p_sd, "search timeout" );
264 "%s:%d: DEBUG: UNHANDLED EVENT ( TYPE=%d )",
265 __FILE__, __LINE__, eventType );
269 return UPNP_E_SUCCESS;
273 // Class implementations...
277 void MediaServer::parseDeviceDescription( IXML_Document* doc,
278 const char* location,
279 services_discovery_t* p_sd )
283 msg_Dbg( p_sd, "%s:%d: NULL", __FILE__, __LINE__ );
289 msg_Dbg( p_sd, "%s:%d: NULL", __FILE__, __LINE__ );
293 const char* baseURL = location;
295 // Try to extract baseURL
297 IXML_NodeList* urlList = ixmlDocument_getElementsByTagName( doc, "baseURL" );
301 if ( IXML_Node* urlNode = ixmlNodeList_item( urlList, 0 ) )
303 IXML_Node* textNode = ixmlNode_getFirstChild( urlNode );
304 if ( textNode ) baseURL = ixmlNode_getNodeValue( textNode );
307 ixmlNodeList_free( urlList );
312 IXML_NodeList* deviceList =
313 ixmlDocument_getElementsByTagName( doc, "device" );
317 for ( unsigned int i = 0; i < ixmlNodeList_length( deviceList ); i++ )
319 IXML_Element* deviceElement =
320 ( IXML_Element* ) ixmlNodeList_item( deviceList, i );
322 const char* deviceType = xml_getChildElementValue( deviceElement,
327 "%s:%d: no deviceType!",
328 __FILE__, __LINE__ );
332 if ( strcmp( MEDIA_SERVER_DEVICE_TYPE, deviceType ) != 0 )
335 const char* UDN = xml_getChildElementValue( deviceElement, "UDN" );
338 msg_Dbg( p_sd, "%s:%d: no UDN!",
339 __FILE__, __LINE__ );
343 if ( p_sd->p_sys->serverList->getServer( UDN ) != 0 )
346 const char* friendlyName =
347 xml_getChildElementValue( deviceElement,
352 msg_Dbg( p_sd, "%s:%d: no friendlyName!", __FILE__, __LINE__ );
356 MediaServer* server = new MediaServer( UDN, friendlyName, p_sd );
358 if ( !p_sd->p_sys->serverList->addServer( server ) )
366 // Check for ContentDirectory service...
367 IXML_NodeList* serviceList =
368 ixmlElement_getElementsByTagName( deviceElement,
372 for ( unsigned int j = 0;
373 j < ixmlNodeList_length( serviceList ); j++ )
375 IXML_Element* serviceElement =
376 ( IXML_Element* ) ixmlNodeList_item( serviceList, j );
378 const char* serviceType =
379 xml_getChildElementValue( serviceElement,
384 if ( strcmp( CONTENT_DIRECTORY_SERVICE_TYPE,
388 const char* eventSubURL =
389 xml_getChildElementValue( serviceElement,
394 const char* controlURL =
395 xml_getChildElementValue( serviceElement,
400 // Try to subscribe to ContentDirectory service
402 char* url = ( char* ) malloc( strlen( baseURL ) +
403 strlen( eventSubURL ) + 1 );
406 char* s1 = strdup( baseURL );
407 char* s2 = strdup( eventSubURL );
409 if ( UpnpResolveURL( s1, s2, url ) ==
412 server->setContentDirectoryEventURL( url );
413 server->subscribeToContentDirectory();
421 // Try to browse content directory...
423 url = ( char* ) malloc( strlen( baseURL ) +
424 strlen( controlURL ) + 1 );
427 char* s1 = strdup( baseURL );
428 char* s2 = strdup( controlURL );
430 if ( UpnpResolveURL( s1, s2, url ) ==
433 server->setContentDirectoryControlURL( url );
434 server->fetchContents();
442 ixmlNodeList_free( serviceList );
445 ixmlNodeList_free( deviceList );
449 MediaServer::MediaServer( const char* UDN,
450 const char* friendlyName,
451 services_discovery_t* p_sd )
456 _friendlyName = friendlyName;
462 MediaServer::~MediaServer()
467 const char* MediaServer::getUDN() const
469 const char* s = _UDN.c_str();
473 const char* MediaServer::getFriendlyName() const
475 const char* s = _friendlyName.c_str();
479 void MediaServer::setContentDirectoryEventURL( const char* url )
481 _contentDirectoryEventURL = url;
484 const char* MediaServer::getContentDirectoryEventURL() const
486 const char* s = _contentDirectoryEventURL.c_str();
490 void MediaServer::setContentDirectoryControlURL( const char* url )
492 _contentDirectoryControlURL = url;
495 const char* MediaServer::getContentDirectoryControlURL() const
497 return _contentDirectoryControlURL.c_str();
500 void MediaServer::subscribeToContentDirectory()
502 const char* url = getContentDirectoryEventURL();
503 if ( !url || strcmp( url, "" ) == 0 )
505 msg_Dbg( _p_sd, "No subscription url set!" );
512 int res = UpnpSubscribe( _p_sd->p_sys->clientHandle, url, &timeOut, sid );
514 if ( res == UPNP_E_SUCCESS )
516 _subscriptionTimeOut = timeOut;
517 memcpy( _subscriptionID, sid, sizeof( Upnp_SID ) );
522 "%s:%d: WARNING: '%s': %s", __FILE__, __LINE__,
523 getFriendlyName(), UpnpGetErrorMessage( res ) );
527 IXML_Document* MediaServer::_browseAction( const char* pObjectID,
528 const char* pBrowseFlag,
530 const char* pStartingIndex,
531 const char* pRequestedCount,
532 const char* pSortCriteria )
534 IXML_Document* action = 0;
535 IXML_Document* response = 0;
536 const char* url = getContentDirectoryControlURL();
538 if ( !url || strcmp( url, "" ) == 0 )
540 msg_Dbg( _p_sd, "No subscription url set!" );
544 char* ObjectID = strdup( pObjectID );
545 char* BrowseFlag = strdup( pBrowseFlag );
546 char* Filter = strdup( pFilter );
547 char* StartingIndex = strdup( pStartingIndex );
548 char* RequestedCount = strdup( pRequestedCount );
549 char* SortCriteria = strdup( pSortCriteria );
550 char* serviceType = strdup( CONTENT_DIRECTORY_SERVICE_TYPE );
554 res = UpnpAddToAction( &action, "Browse",
555 serviceType, "ObjectID", ObjectID );
557 if ( res != UPNP_E_SUCCESS )
560 "%s:%d: ERROR: %s", __FILE__, __LINE__,
561 UpnpGetErrorMessage( res ) );
562 goto browseActionCleanup;
565 res = UpnpAddToAction( &action, "Browse",
566 serviceType, "BrowseFlag", BrowseFlag );
568 if ( res != UPNP_E_SUCCESS )
571 "%s:%d: ERROR: %s", __FILE__, __LINE__,
572 UpnpGetErrorMessage( res ) );
573 goto browseActionCleanup;
576 res = UpnpAddToAction( &action, "Browse",
577 serviceType, "Filter", Filter );
579 if ( res != UPNP_E_SUCCESS )
582 "%s:%d: ERROR: %s", __FILE__, __LINE__,
583 UpnpGetErrorMessage( res ) );
584 goto browseActionCleanup;
587 res = UpnpAddToAction( &action, "Browse",
588 serviceType, "StartingIndex", StartingIndex );
590 if ( res != UPNP_E_SUCCESS )
593 "%s:%d: ERROR: %s", __FILE__, __LINE__,
594 UpnpGetErrorMessage( res ) );
595 goto browseActionCleanup;
598 res = UpnpAddToAction( &action, "Browse",
599 serviceType, "RequestedCount", RequestedCount );
601 if ( res != UPNP_E_SUCCESS )
604 "%s:%d: ERROR: %s", __FILE__, __LINE__,
605 UpnpGetErrorMessage( res ) ); goto browseActionCleanup; }
607 res = UpnpAddToAction( &action, "Browse",
608 serviceType, "SortCriteria", SortCriteria );
610 if ( res != UPNP_E_SUCCESS )
613 "%s:%d: ERROR: %s", __FILE__, __LINE__,
614 UpnpGetErrorMessage( res ) );
615 goto browseActionCleanup;
618 res = UpnpSendAction( _p_sd->p_sys->clientHandle,
620 CONTENT_DIRECTORY_SERVICE_TYPE,
625 if ( res != UPNP_E_SUCCESS )
628 "%s:%d: ERROR: %s when trying the send() action with URL: %s",
630 UpnpGetErrorMessage( res ), url );
632 ixmlDocument_free( response );
641 free( StartingIndex );
642 free( RequestedCount );
643 free( SortCriteria );
647 ixmlDocument_free( action );
651 void MediaServer::fetchContents()
653 Container* root = new Container( 0, "0", getFriendlyName() );
654 _fetchContents( root );
657 _contents->setInputItem( _inputItem );
659 _buildPlaylist( _contents );
662 bool MediaServer::_fetchContents( Container* parent )
667 "%s:%d: parent==NULL", __FILE__, __LINE__ );
671 IXML_Document* response = _browseAction( parent->getObjectID(),
672 "BrowseDirectChildren",
677 "%s:%d: ERROR! No response from browse() action",
678 __FILE__, __LINE__ );
682 IXML_Document* result = parseBrowseResult( response );
683 ixmlDocument_free( response );
688 "%s:%d: ERROR! browse() response parsing failed",
689 __FILE__, __LINE__ );
693 IXML_NodeList* containerNodeList =
694 ixmlDocument_getElementsByTagName( result, "container" );
696 if ( containerNodeList )
698 for ( unsigned int i = 0;
699 i < ixmlNodeList_length( containerNodeList ); i++ )
701 IXML_Element* containerElement =
702 ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
704 const char* objectID = ixmlElement_getAttribute( containerElement,
709 const char* childCountStr =
710 ixmlElement_getAttribute( containerElement, "childCount" );
712 if ( !childCountStr )
715 int childCount = atoi( childCountStr );
716 const char* title = xml_getChildElementValue( containerElement,
722 const char* resource = xml_getChildElementValue( containerElement,
725 if ( resource && childCount < 1 )
727 Item* item = new Item( parent, objectID, title, resource );
728 parent->addItem( item );
733 Container* container = new Container( parent, objectID, title );
734 parent->addContainer( container );
736 if ( childCount > 0 )
737 _fetchContents( container );
740 ixmlNodeList_free( containerNodeList );
743 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result,
747 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
749 IXML_Element* itemElement =
750 ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
752 const char* objectID =
753 ixmlElement_getAttribute( itemElement, "id" );
759 xml_getChildElementValue( itemElement, "dc:title" );
764 const char* resource =
765 xml_getChildElementValue( itemElement, "res" );
770 Item* item = new Item( parent, objectID, title, resource );
771 parent->addItem( item );
773 ixmlNodeList_free( itemNodeList );
776 ixmlDocument_free( result );
780 void MediaServer::_buildPlaylist( Container* parent )
782 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
784 Container* container = parent->getContainer( i );
786 input_item_t* p_input_item = input_item_New( _p_sd, "vlc://nop", parent->getTitle() );
787 input_item_AddSubItem( parent->getInputItem(), p_input_item );
789 container->setInputItem( p_input_item );
790 _buildPlaylist( container );
793 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
795 Item* item = parent->getItem( i );
797 input_item_t* p_input_item = input_item_New( _p_sd,
800 assert( p_input_item );
801 input_item_AddSubItem( parent->getInputItem(), p_input_item );
802 item->setInputItem( p_input_item );
806 void MediaServer::setInputItem( input_item_t* p_input_item )
808 if(_inputItem == p_input_item)
812 vlc_gc_decref( _inputItem );
814 vlc_gc_incref( p_input_item );
815 _inputItem = p_input_item;
818 bool MediaServer::compareSID( const char* sid )
820 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
824 // MediaServerList...
826 MediaServerList::MediaServerList( services_discovery_t* p_sd )
831 MediaServerList::~MediaServerList()
833 for ( unsigned int i = 0; i < _list.size(); i++ )
839 bool MediaServerList::addServer( MediaServer* s )
841 input_item_t* p_input_item = NULL;
842 if ( getServer( s->getUDN() ) != 0 ) return false;
844 msg_Dbg( _p_sd, "Adding server '%s'",
845 s->getFriendlyName() );
847 services_discovery_t* p_sd = _p_sd;
849 p_input_item = input_item_New( p_sd, "vlc://nop", s->getFriendlyName() );
850 s->setInputItem( p_input_item );
852 services_discovery_AddItem( p_sd, p_input_item, NULL );
854 _list.push_back( s );
859 MediaServer* MediaServerList::getServer( const char* UDN )
861 MediaServer* result = 0;
863 for ( unsigned int i = 0; i < _list.size(); i++ )
865 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
875 MediaServer* MediaServerList::getServerBySID( const char* sid )
877 MediaServer* server = 0;
879 for ( unsigned int i = 0; i < _list.size(); i++ )
881 if ( _list[i]->compareSID( sid ) )
891 void MediaServerList::removeServer( const char* UDN )
893 MediaServer* server = getServer( UDN );
894 if ( !server ) return;
897 "Removing server '%s'", server->getFriendlyName() );
899 std::vector<MediaServer*>::iterator it;
900 for ( it = _list.begin(); it != _list.end(); it++ )
914 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
918 _objectID = objectID;
920 _resource = resource;
928 vlc_gc_decref( _inputItem );
931 const char* Item::getObjectID() const
933 return _objectID.c_str();
936 const char* Item::getTitle() const
938 return _title.c_str();
941 const char* Item::getResource() const
943 return _resource.c_str();
946 void Item::setInputItem( input_item_t* p_input_item )
948 if(_inputItem == p_input_item)
952 vlc_gc_decref( _inputItem );
954 vlc_gc_incref( p_input_item );
955 _inputItem = p_input_item;
958 input_item_t* Item::getInputItem() const
966 Container::Container( Container* parent,
967 const char* objectID,
972 _objectID = objectID;
978 Container::~Container()
980 for ( unsigned int i = 0; i < _containers.size(); i++ )
982 delete _containers[i];
985 for ( unsigned int i = 0; i < _items.size(); i++ )
991 vlc_gc_decref( _inputItem );
994 void Container::addItem( Item* item )
996 _items.push_back( item );
999 void Container::addContainer( Container* container )
1001 _containers.push_back( container );
1004 const char* Container::getObjectID() const
1006 return _objectID.c_str();
1009 const char* Container::getTitle() const
1011 return _title.c_str();
1014 unsigned int Container::getNumItems() const
1016 return _items.size();
1019 unsigned int Container::getNumContainers() const
1021 return _containers.size();
1024 Item* Container::getItem( unsigned int i ) const
1026 if ( i < _items.size() ) return _items[i];
1030 Container* Container::getContainer( unsigned int i ) const
1032 if ( i < _containers.size() ) return _containers[i];
1036 Container* Container::getParent()
1041 void Container::setInputItem( input_item_t* p_input_item )
1043 if(_inputItem == p_input_item)
1047 vlc_gc_decref( _inputItem );
1049 vlc_gc_incref( p_input_item );
1050 _inputItem = p_input_item;
1053 input_item_t* Container::getInputItem() const