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 Lockable* 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 p_sys->callbackLock = new Lockable();
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 delete 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 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 );
659 // playlist_NodeEmpty( p_playlist, _playlistNode, true );
665 _contents->setInputItem( _inputItem );
667 _buildPlaylist( _contents );
670 bool MediaServer::_fetchContents( Container* parent )
675 "%s:%d: parent==NULL", __FILE__, __LINE__ );
679 IXML_Document* response = _browseAction( parent->getObjectID(),
680 "BrowseDirectChildren",
685 "%s:%d: ERROR! No response from browse() action",
686 __FILE__, __LINE__ );
690 IXML_Document* result = parseBrowseResult( response );
691 ixmlDocument_free( response );
696 "%s:%d: ERROR! browse() response parsing failed",
697 __FILE__, __LINE__ );
701 IXML_NodeList* containerNodeList =
702 ixmlDocument_getElementsByTagName( result, "container" );
704 if ( containerNodeList )
706 for ( unsigned int i = 0;
707 i < ixmlNodeList_length( containerNodeList ); i++ )
709 IXML_Element* containerElement =
710 ( IXML_Element* )ixmlNodeList_item( containerNodeList, i );
712 const char* objectID = ixmlElement_getAttribute( containerElement,
717 const char* childCountStr =
718 ixmlElement_getAttribute( containerElement, "childCount" );
720 if ( !childCountStr )
723 int childCount = atoi( childCountStr );
724 const char* title = xml_getChildElementValue( containerElement,
730 const char* resource = xml_getChildElementValue( containerElement,
733 if ( resource && childCount < 1 )
735 Item* item = new Item( parent, objectID, title, resource );
736 parent->addItem( item );
741 Container* container = new Container( parent, objectID, title );
742 parent->addContainer( container );
744 if ( childCount > 0 )
745 _fetchContents( container );
748 ixmlNodeList_free( containerNodeList );
751 IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( result,
755 for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
757 IXML_Element* itemElement =
758 ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
760 const char* objectID =
761 ixmlElement_getAttribute( itemElement, "id" );
767 xml_getChildElementValue( itemElement, "dc:title" );
772 const char* resource =
773 xml_getChildElementValue( itemElement, "res" );
778 Item* item = new Item( parent, objectID, title, resource );
779 parent->addItem( item );
781 ixmlNodeList_free( itemNodeList );
784 ixmlDocument_free( result );
788 void MediaServer::_buildPlaylist( Container* parent )
790 for ( unsigned int i = 0; i < parent->getNumContainers(); i++ )
792 Container* container = parent->getContainer( i );
794 input_item_t* p_input_item = input_item_New( _p_sd, "vlc://nop", parent->getTitle() );
795 input_item_AddSubItem( parent->getInputItem(), p_input_item );
797 container->setInputItem( p_input_item );
798 _buildPlaylist( container );
801 for ( unsigned int i = 0; i < parent->getNumItems(); i++ )
803 Item* item = parent->getItem( i );
805 input_item_t* p_input_item = input_item_New( _p_sd,
808 assert( p_input_item );
809 input_item_AddSubItem( parent->getInputItem(), p_input_item );
810 item->setInputItem( p_input_item );
814 void MediaServer::setInputItem( input_item_t* p_input_item )
816 if(_inputItem == p_input_item)
820 vlc_gc_decref( _inputItem );
822 vlc_gc_incref( p_input_item );
823 _inputItem = p_input_item;
826 bool MediaServer::compareSID( const char* sid )
828 return ( strncmp( _subscriptionID, sid, sizeof( Upnp_SID ) ) == 0 );
832 // MediaServerList...
834 MediaServerList::MediaServerList( services_discovery_t* p_sd )
839 MediaServerList::~MediaServerList()
841 for ( unsigned int i = 0; i < _list.size(); i++ )
847 bool MediaServerList::addServer( MediaServer* s )
849 input_item_t* p_input_item = NULL;
850 if ( getServer( s->getUDN() ) != 0 ) return false;
852 msg_Dbg( _p_sd, "Adding server '%s'",
853 s->getFriendlyName() );
855 services_discovery_t* p_sd = _p_sd;
857 p_input_item = input_item_New( p_sd, "vlc://nop", s->getFriendlyName() );
858 s->setInputItem( p_input_item );
860 services_discovery_AddItem( p_sd, p_input_item, NULL );
862 _list.push_back( s );
867 MediaServer* MediaServerList::getServer( const char* UDN )
869 MediaServer* result = 0;
871 for ( unsigned int i = 0; i < _list.size(); i++ )
873 if( strcmp( UDN, _list[i]->getUDN() ) == 0 )
883 MediaServer* MediaServerList::getServerBySID( const char* sid )
885 MediaServer* server = 0;
887 for ( unsigned int i = 0; i < _list.size(); i++ )
889 if ( _list[i]->compareSID( sid ) )
899 void MediaServerList::removeServer( const char* UDN )
901 MediaServer* server = getServer( UDN );
902 if ( !server ) return;
905 "Removing server '%s'", server->getFriendlyName() );
907 std::vector<MediaServer*>::iterator it;
908 for ( it = _list.begin(); it != _list.end(); it++ )
922 Item::Item( Container* parent, const char* objectID, const char* title, const char* resource )
926 _objectID = objectID;
928 _resource = resource;
936 vlc_gc_decref( _inputItem );
939 const char* Item::getObjectID() const
941 return _objectID.c_str();
944 const char* Item::getTitle() const
946 return _title.c_str();
949 const char* Item::getResource() const
951 return _resource.c_str();
954 void Item::setInputItem( input_item_t* p_input_item )
956 if(_inputItem == p_input_item)
960 vlc_gc_decref( _inputItem );
962 vlc_gc_incref( p_input_item );
963 _inputItem = p_input_item;
966 input_item_t* Item::getInputItem() const
974 Container::Container( Container* parent,
975 const char* objectID,
980 _objectID = objectID;
986 Container::~Container()
988 for ( unsigned int i = 0; i < _containers.size(); i++ )
990 delete _containers[i];
993 for ( unsigned int i = 0; i < _items.size(); i++ )
999 vlc_gc_decref( _inputItem );
1002 void Container::addItem( Item* item )
1004 _items.push_back( item );
1007 void Container::addContainer( Container* container )
1009 _containers.push_back( container );
1012 const char* Container::getObjectID() const
1014 return _objectID.c_str();
1017 const char* Container::getTitle() const
1019 return _title.c_str();
1022 unsigned int Container::getNumItems() const
1024 return _items.size();
1027 unsigned int Container::getNumContainers() const
1029 return _containers.size();
1032 Item* Container::getItem( unsigned int i ) const
1034 if ( i < _items.size() ) return _items[i];
1038 Container* Container::getContainer( unsigned int i ) const
1040 if ( i < _containers.size() ) return _containers[i];
1044 Container* Container::getParent()
1049 void Container::setInputItem( input_item_t* p_input_item )
1051 if(_inputItem == p_input_item)
1055 vlc_gc_decref( _inputItem );
1057 vlc_gc_incref( p_input_item );
1058 _inputItem = p_input_item;
1061 input_item_t* Container::getInputItem() const