]> git.sesse.net Git - vlc/blob - modules/services_discovery/upnp.cpp
b427cf75196ed40067cb05c9ec350d78ce5fe7fd
[vlc] / modules / services_discovery / upnp.cpp
1 /*****************************************************************************
2  * upnp.cpp :  UPnP discovery module (libupnp)
3  *****************************************************************************
4  * Copyright (C) 2004-2011 the VideoLAN team
5  * $Id$
6  *
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>
10  *          Hugo Beauzée-Luyssen <hugo@beauzee.fr>
11  *
12  * UPnP Plugin using the Intel SDK (libupnp) instead of CyberLink
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 #define __STDC_CONSTANT_MACROS 1
30
31 #undef PACKAGE_NAME
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include "upnp.hpp"
37
38 #include <vlc_access.h>
39 #include <vlc_plugin.h>
40 #include <vlc_services_discovery.h>
41 #include <vlc_url.h>
42
43 #include <assert.h>
44 #include <limits.h>
45 #include <algorithm>
46
47 /*
48  * Constants
49 */
50 const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
51 const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
52
53 /*
54  * VLC handle
55  */
56 struct services_discovery_sys_t
57 {
58     UpnpInstanceWrapper* p_upnp;
59     SD::MediaServerList* p_server_list;
60 };
61
62 struct access_sys_t
63 {
64     UpnpInstanceWrapper* p_upnp;
65 };
66
67 UpnpInstanceWrapper* UpnpInstanceWrapper::s_instance;
68 vlc_mutex_t UpnpInstanceWrapper::s_lock = VLC_STATIC_MUTEX;
69
70 /*
71  * VLC callback prototypes
72  */
73 namespace SD
74 {
75     static int Open( vlc_object_t* );
76     static void Close( vlc_object_t* );
77 }
78
79 namespace Access
80 {
81     static int Open( vlc_object_t* );
82     static void Close( vlc_object_t* );
83 }
84
85 VLC_SD_PROBE_HELPER( "upnp", "Universal Plug'n'Play", SD_CAT_LAN )
86
87 /*
88  * Module descriptor
89  */
90 vlc_module_begin()
91     set_shortname( "UPnP" );
92     set_description( N_( "Universal Plug'n'Play" ) );
93     set_category( CAT_PLAYLIST );
94     set_subcategory( SUBCAT_PLAYLIST_SD );
95     set_capability( "services_discovery", 0 );
96     set_callbacks( SD::Open, SD::Close );
97
98     add_submodule()
99         set_category( CAT_INPUT )
100         set_subcategory( SUBCAT_INPUT_ACCESS )
101         set_callbacks( Access::Open, Access::Close )
102         set_capability( "access", 0 )
103
104     VLC_SD_PROBE_SUBMODULE
105 vlc_module_end()
106
107
108 /*
109  * Returns the value of a child element, or NULL on error
110  */
111 const char* xml_getChildElementValue( IXML_Element* p_parent,
112                                       const char*   psz_tag_name )
113 {
114     assert( p_parent );
115     assert( psz_tag_name );
116
117     IXML_NodeList* p_node_list;
118     p_node_list = ixmlElement_getElementsByTagName( p_parent, psz_tag_name );
119     if ( !p_node_list ) return NULL;
120
121     IXML_Node* p_element = ixmlNodeList_item( p_node_list, 0 );
122     ixmlNodeList_free( p_node_list );
123     if ( !p_element )   return NULL;
124
125     IXML_Node* p_text_node = ixmlNode_getFirstChild( p_element );
126     if ( !p_text_node ) return NULL;
127
128     return ixmlNode_getNodeValue( p_text_node );
129 }
130
131 /*
132  * Extracts the result document from a SOAP response
133  */
134 IXML_Document* parseBrowseResult( IXML_Document* p_doc )
135 {
136     assert( p_doc );
137
138     // ixml*_getElementsByTagName will ultimately only case the pointer to a Node
139     // pointer, and pass it to a private function. Don't bother have a IXML_Document
140     // version of getChildElementValue
141     const char* psz_raw_didl = xml_getChildElementValue( (IXML_Element*)p_doc, "Result" );
142
143     if( !psz_raw_didl )
144         return NULL;
145
146     /* First, try parsing the buffer as is */
147     IXML_Document* p_result_doc = ixmlParseBuffer( psz_raw_didl );
148     if( !p_result_doc ) {
149         /* Missing namespaces confuse the ixml parser. This is a very ugly
150          * hack but it is needeed until devices start sending valid XML.
151          *
152          * It works that way:
153          *
154          * The DIDL document is extracted from the Result tag, then wrapped into
155          * a valid XML header and a new root tag which contains missing namespace
156          * definitions so the ixml parser understands it.
157          *
158          * If you know of a better workaround, please oh please fix it */
159         const char* psz_xml_result_fmt = "<?xml version=\"1.0\" ?>"
160             "<Result xmlns:sec=\"urn:samsung:metadata:2009\">%s</Result>";
161
162         char* psz_xml_result_string = NULL;
163         if( -1 == asprintf( &psz_xml_result_string,
164                              psz_xml_result_fmt,
165                              psz_raw_didl) )
166             return NULL;
167
168         p_result_doc = ixmlParseBuffer( psz_xml_result_string );
169         free( psz_xml_result_string );
170     }
171
172     if( !p_result_doc )
173         return NULL;
174
175     IXML_NodeList *p_elems = ixmlDocument_getElementsByTagName( p_result_doc,
176                                                                 "DIDL-Lite" );
177
178     IXML_Node *p_node = ixmlNodeList_item( p_elems, 0 );
179     ixmlNodeList_free( p_elems );
180
181     return (IXML_Document*)p_node;
182 }
183
184 namespace SD
185 {
186
187 /*
188  * Initializes UPNP instance.
189  */
190 static int Open( vlc_object_t *p_this )
191 {
192     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
193     services_discovery_sys_t *p_sys  = ( services_discovery_sys_t * )
194             calloc( 1, sizeof( services_discovery_sys_t ) );
195
196     if( !( p_sd->p_sys = p_sys ) )
197         return VLC_ENOMEM;
198
199     p_sys->p_server_list = new(std::nothrow) SD::MediaServerList( p_sd );
200     if ( unlikely( p_sys->p_server_list == NULL ) )
201     {
202         return VLC_ENOMEM;
203     }
204
205     p_sys->p_upnp = UpnpInstanceWrapper::get( p_this, SD::MediaServerList::Callback, p_sys->p_server_list );
206     if ( !p_sys->p_upnp )
207     {
208         Close( p_this );
209         return VLC_EGENERIC;
210     }
211
212     /* Search for media servers */
213     int i_res = UpnpSearchAsync( p_sys->p_upnp->handle(), 5,
214             MEDIA_SERVER_DEVICE_TYPE, p_sys->p_upnp );
215     if( i_res != UPNP_E_SUCCESS )
216     {
217         msg_Err( p_sd, "Error sending search request: %s", UpnpGetErrorMessage( i_res ) );
218         Close( p_this );
219         return VLC_EGENERIC;
220     }
221
222     return VLC_SUCCESS;
223 }
224
225 /*
226  * Releases resources.
227  */
228 static void Close( vlc_object_t *p_this )
229 {
230     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
231     services_discovery_sys_t *p_sys = p_sd->p_sys;
232
233     if (p_sys->p_upnp)
234         p_sys->p_upnp->release( true );
235     delete p_sys->p_server_list;
236     free( p_sys );
237 }
238
239 MediaServerDesc::MediaServerDesc(const std::string& udn, const std::string& fName, const std::string& loc)
240     : UDN( udn )
241     , friendlyName( fName )
242     , location( loc )
243     , inputItem( NULL )
244 {
245 }
246
247 MediaServerDesc::~MediaServerDesc()
248 {
249     if (inputItem)
250         vlc_gc_decref( inputItem );
251 }
252
253 /*
254  * MediaServerList class
255  */
256 MediaServerList::MediaServerList( services_discovery_t* p_sd )
257     : p_sd_( p_sd )
258 {
259     vlc_mutex_init( &lock_ );
260 }
261
262 MediaServerList::~MediaServerList()
263 {
264     vlc_delete_all(list_);
265     vlc_mutex_destroy( &lock_ );
266 }
267
268 bool MediaServerList::addServer( MediaServerDesc* desc )
269 {
270     vlc_mutex_locker lock( &lock_ );
271     input_item_t* p_input_item = NULL;
272     if ( getServer( desc->UDN ) )
273         return false;
274
275     msg_Dbg( p_sd_, "Adding server '%s' with uuid '%s'", desc->friendlyName.c_str(), desc->UDN.c_str() );
276
277     char* psz_mrl;
278     if( asprintf(&psz_mrl, "upnp://%s?ObjectID=%s", desc->location.c_str(), desc->UDN.c_str() ) < 0 )
279         return false;
280
281     p_input_item = input_item_NewWithType( psz_mrl, desc->friendlyName.c_str(), 0,
282                                            NULL, 0, -1, ITEM_TYPE_NODE );
283     free( psz_mrl );
284     if ( !p_input_item )
285         return false;
286     desc->inputItem = p_input_item;
287     input_item_SetDescription( p_input_item, desc->UDN.c_str() );
288     services_discovery_AddItem( p_sd_, p_input_item, NULL );
289     list_.push_back( desc );
290     return true;
291 }
292
293 MediaServerDesc* MediaServerList::getServer( const std::string& udn )
294 {
295     std::vector<MediaServerDesc*>::const_iterator it = list_.begin();
296     std::vector<MediaServerDesc*>::const_iterator ite = list_.end();
297
298     for ( ; it != ite; ++it )
299     {
300         if( udn == (*it)->UDN )
301         {
302             return *it;
303         }
304     }
305     return NULL;
306 }
307
308 void MediaServerList::parseNewServer( IXML_Document *doc, const std::string &location )
309 {
310     if ( !doc )
311     {
312         msg_Err( p_sd_, "Null IXML_Document" );
313         return;
314     }
315
316     if ( location.empty() )
317     {
318         msg_Err( p_sd_, "Empty location" );
319         return;
320     }
321
322     const char* psz_base_url = location.c_str();
323
324     /* Try to extract baseURL */
325     IXML_NodeList* p_url_list = ixmlDocument_getElementsByTagName( doc, "URLBase" );
326     if ( p_url_list )
327     {
328         if ( IXML_Node* p_url_node = ixmlNodeList_item( p_url_list, 0 ) )
329         {
330             IXML_Node* p_text_node = ixmlNode_getFirstChild( p_url_node );
331             if ( p_text_node )
332                 psz_base_url = ixmlNode_getNodeValue( p_text_node );
333         }
334         ixmlNodeList_free( p_url_list );
335     }
336
337     /* Get devices */
338     IXML_NodeList* p_device_list = ixmlDocument_getElementsByTagName( doc, "device" );
339
340     if ( !p_device_list )
341         return;
342     for ( unsigned int i = 0; i < ixmlNodeList_length( p_device_list ); i++ )
343     {
344         IXML_Element* p_device_element = ( IXML_Element* ) ixmlNodeList_item( p_device_list, i );
345
346         if( !p_device_element )
347             continue;
348
349         const char* psz_device_type = xml_getChildElementValue( p_device_element, "deviceType" );
350
351         if ( !psz_device_type )
352         {
353             msg_Warn( p_sd_, "No deviceType found!" );
354             continue;
355         }
356
357         if ( strncmp( MEDIA_SERVER_DEVICE_TYPE, psz_device_type,
358                 strlen( MEDIA_SERVER_DEVICE_TYPE ) - 1 ) )
359             continue;
360
361         const char* psz_udn = xml_getChildElementValue( p_device_element,
362                                                         "UDN" );
363         if ( !psz_udn )
364         {
365             msg_Warn( p_sd_, "No UDN!" );
366             continue;
367         }
368
369         /* Check if server is already added */
370         if ( p_sd_->p_sys->p_server_list->getServer( psz_udn ) )
371         {
372             msg_Warn( p_sd_, "Server with uuid '%s' already exists.", psz_udn );
373             continue;
374         }
375
376         const char* psz_friendly_name =
377                    xml_getChildElementValue( p_device_element,
378                                              "friendlyName" );
379
380         if ( !psz_friendly_name )
381         {
382             msg_Dbg( p_sd_, "No friendlyName!" );
383             continue;
384         }
385
386         // We now have basic info, we need to get the content browsing url
387         // so the access module can browse without fetching the manifest again
388
389         /* Check for ContentDirectory service. */
390         IXML_NodeList* p_service_list = ixmlElement_getElementsByTagName( p_device_element, "service" );
391         if ( !p_service_list )
392             continue;
393         for ( unsigned int j = 0; j < ixmlNodeList_length( p_service_list ); j++ )
394         {
395             IXML_Element* p_service_element = (IXML_Element*)ixmlNodeList_item( p_service_list, j );
396
397             const char* psz_service_type = xml_getChildElementValue( p_service_element, "serviceType" );
398             if ( !psz_service_type )
399             {
400                 msg_Warn( p_sd_, "No service type found." );
401                 continue;
402             }
403
404             int k = strlen( CONTENT_DIRECTORY_SERVICE_TYPE ) - 1;
405             if ( strncmp( CONTENT_DIRECTORY_SERVICE_TYPE,
406                         psz_service_type, k ) )
407                 continue;
408
409             const char* psz_control_url = xml_getChildElementValue( p_service_element,
410                                           "controlURL" );
411             if ( !psz_control_url )
412             {
413                 msg_Warn( p_sd_, "No control url found." );
414                 continue;
415             }
416
417             /* Try to browse content directory. */
418             char* psz_url = ( char* ) malloc( strlen( psz_base_url ) + strlen( psz_control_url ) + 1 );
419             if ( psz_url )
420             {
421                 if ( UpnpResolveURL( psz_base_url, psz_control_url, psz_url ) == UPNP_E_SUCCESS )
422                 {
423                     SD::MediaServerDesc* p_server = new(std::nothrow) SD::MediaServerDesc( psz_udn,
424                             psz_friendly_name, psz_url );
425                     free( psz_url );
426                     if ( unlikely( !p_server ) )
427                         break;
428
429                     if ( !addServer( p_server ) )
430                     {
431                         delete p_server;
432                         continue;
433                     }
434                 }
435                 else
436                     free( psz_url );
437             }
438         }
439         ixmlNodeList_free( p_service_list );
440     }
441     ixmlNodeList_free( p_device_list );
442 }
443
444 void MediaServerList::removeServer( const std::string& udn )
445 {
446     vlc_mutex_locker lock( &lock_ );
447
448     MediaServerDesc* p_server = getServer( udn );
449     if ( !p_server )
450         return;
451
452     msg_Dbg( p_sd_, "Removing server '%s'", p_server->friendlyName.c_str() );
453
454     assert(p_server->inputItem);
455     services_discovery_RemoveItem( p_sd_, p_server->inputItem );
456
457     std::vector<MediaServerDesc*>::iterator it = std::find(list_.begin(), list_.end(), p_server);
458     if (it != list_.end())
459     {
460         list_.erase( it );
461     }
462     delete p_server;
463 }
464
465 /*
466  * Handles servers listing UPnP events
467  */
468 int MediaServerList::Callback( Upnp_EventType event_type, void* p_event, void* p_user_data )
469 {
470     MediaServerList* self = static_cast<MediaServerList*>( p_user_data );
471     services_discovery_t* p_sd = self->p_sd_;
472
473     switch( event_type )
474     {
475     case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
476     case UPNP_DISCOVERY_SEARCH_RESULT:
477     {
478         struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
479
480         IXML_Document *p_description_doc = NULL;
481
482         int i_res;
483         i_res = UpnpDownloadXmlDoc( p_discovery->Location, &p_description_doc );
484         if ( i_res != UPNP_E_SUCCESS )
485         {
486             msg_Warn( p_sd, "Could not download device description! "
487                             "Fetching data from %s failed: %s",
488                             p_discovery->Location, UpnpGetErrorMessage( i_res ) );
489             return i_res;
490         }
491         self->parseNewServer( p_description_doc, p_discovery->Location );
492         ixmlDocument_free( p_description_doc );
493     }
494     break;
495
496     case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
497     {
498         struct Upnp_Discovery* p_discovery = ( struct Upnp_Discovery* )p_event;
499
500         self->removeServer( p_discovery->DeviceId );
501     }
502     break;
503
504     case UPNP_EVENT_SUBSCRIBE_COMPLETE:
505         msg_Warn( p_sd, "subscription complete" );
506         break;
507
508     case UPNP_DISCOVERY_SEARCH_TIMEOUT:
509         msg_Warn( p_sd, "search timeout" );
510         break;
511
512     case UPNP_EVENT_RECEIVED:
513     case UPNP_EVENT_AUTORENEWAL_FAILED:
514     case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
515         // Those are for the access part
516         break;
517
518     default:
519         msg_Err( p_sd, "Unhandled event, please report ( type=%d )", event_type );
520         break;
521     }
522
523     return UPNP_E_SUCCESS;
524 }
525
526 }
527
528 namespace Access
529 {
530
531 MediaServer::MediaServer(const char *psz_url, access_t *p_access, input_item_node_t *node)
532     : url_( psz_url )
533     , access_( p_access )
534     , node_( node )
535 {
536 }
537
538 void MediaServer::addItem(const char *objectID, const char *title )
539 {
540     vlc_url_t url;
541     vlc_UrlParse( &url, url_.c_str(), '?' );
542     char* psz_url;
543
544     if (asprintf( &psz_url, "upnp://%s://%s:%u%s?ObjectID=%s", url.psz_protocol,
545                   url.psz_host, url.i_port ? url.i_port : 80, url.psz_path, objectID ) < 0 )
546     {
547         vlc_UrlClean( &url );
548         return ;
549     }
550     vlc_UrlClean( &url );
551
552     input_item_t* p_item = input_item_NewWithType( psz_url, title, 0, NULL,
553                                                    0, -1, ITEM_TYPE_NODE );
554     free( psz_url);
555     if ( !p_item )
556         return;
557     input_item_CopyOptions( node_->p_item, p_item );
558     input_item_node_AppendItem( node_, p_item );
559     input_item_Release( p_item );
560 }
561
562 void MediaServer::addItem(const char* title, const char*, const char*,
563                           mtime_t duration, const char* psz_url)
564 {
565     input_item_t* p_item = input_item_NewExt( psz_url, title, 0, NULL, 0, duration );
566     input_item_node_AppendItem( node_, p_item );
567     input_item_Release( p_item );
568 }
569
570 /* Access part */
571 IXML_Document* MediaServer::_browseAction( const char* psz_object_id_,
572                                            const char* psz_browser_flag_,
573                                            const char* psz_filter_,
574                                            const char* psz_requested_count_,
575                                            const char* psz_sort_criteria_ )
576 {
577     IXML_Document* p_action = NULL;
578     IXML_Document* p_response = NULL;
579     const char* psz_url = url_.c_str();
580
581     if ( url_.empty() )
582     {
583         msg_Dbg( access_, "No subscription url set!" );
584         return NULL;
585     }
586
587     int i_res;
588
589     i_res = UpnpAddToAction( &p_action, "Browse",
590             CONTENT_DIRECTORY_SERVICE_TYPE, "ObjectID", psz_object_id_ );
591
592     if ( i_res != UPNP_E_SUCCESS )
593     {
594         msg_Dbg( access_, "AddToAction 'ObjectID' failed: %s",
595                 UpnpGetErrorMessage( i_res ) );
596         goto browseActionCleanup;
597     }
598
599     i_res = UpnpAddToAction( &p_action, "Browse",
600             CONTENT_DIRECTORY_SERVICE_TYPE, "StartingIndex", "0" );
601     if ( i_res != UPNP_E_SUCCESS )
602     {
603         msg_Dbg( access_, "AddToAction 'StartingIndex' failed: %s",
604                 UpnpGetErrorMessage( i_res ) );
605         goto browseActionCleanup;
606     }
607
608     i_res = UpnpAddToAction( &p_action, "Browse",
609             CONTENT_DIRECTORY_SERVICE_TYPE, "BrowseFlag", psz_browser_flag_ );
610
611     if ( i_res != UPNP_E_SUCCESS )
612     {
613         msg_Dbg( access_, "AddToAction 'BrowseFlag' failed: %s",
614                 UpnpGetErrorMessage( i_res ) );
615         goto browseActionCleanup;
616     }
617
618     i_res = UpnpAddToAction( &p_action, "Browse",
619             CONTENT_DIRECTORY_SERVICE_TYPE, "Filter", psz_filter_ );
620
621     if ( i_res != UPNP_E_SUCCESS )
622     {
623         msg_Dbg( access_, "AddToAction 'Filter' failed: %s",
624                 UpnpGetErrorMessage( i_res ) );
625         goto browseActionCleanup;
626     }
627
628     i_res = UpnpAddToAction( &p_action, "Browse",
629             CONTENT_DIRECTORY_SERVICE_TYPE, "RequestedCount", psz_requested_count_ );
630
631     if ( i_res != UPNP_E_SUCCESS )
632     {
633         msg_Dbg( access_, "AddToAction 'RequestedCount' failed: %s",
634                 UpnpGetErrorMessage( i_res ) );
635         goto browseActionCleanup;
636     }
637
638     i_res = UpnpAddToAction( &p_action, "Browse",
639             CONTENT_DIRECTORY_SERVICE_TYPE, "SortCriteria", psz_sort_criteria_ );
640
641     if ( i_res != UPNP_E_SUCCESS )
642     {
643         msg_Dbg( access_, "AddToAction 'SortCriteria' failed: %s",
644                 UpnpGetErrorMessage( i_res ) );
645         goto browseActionCleanup;
646     }
647
648     i_res = UpnpSendAction( access_->p_sys->p_upnp->handle(),
649               psz_url,
650               CONTENT_DIRECTORY_SERVICE_TYPE,
651               NULL, /* ignored in SDK, must be NULL */
652               p_action,
653               &p_response );
654
655     if ( i_res != UPNP_E_SUCCESS )
656     {
657         msg_Err( access_, "%s when trying the send() action with URL: %s",
658                 UpnpGetErrorMessage( i_res ), psz_url );
659
660         ixmlDocument_free( p_response );
661         p_response = NULL;
662     }
663
664 browseActionCleanup:
665     ixmlDocument_free( p_action );
666     return p_response;
667 }
668
669 /*
670  * Fetches and parses the UPNP response
671  */
672 bool MediaServer::fetchContents()
673 {
674     const char* objectID = "";
675     vlc_url_t url;
676     vlc_UrlParse( &url, access_->psz_location, '?');
677
678     if ( url.psz_option && !strncmp( url.psz_option, "ObjectID=", strlen( "ObjectID=" ) ) )
679     {
680         objectID = &url.psz_option[strlen( "ObjectID=" )];
681     }
682
683     IXML_Document* p_response = _browseAction( objectID,
684                                       "BrowseDirectChildren",
685                                       "id,dc:title,res," /* Filter */
686                                       "sec:CaptionInfo,sec:CaptionInfoEx,"
687                                       "pv:subtitlefile",
688                                       "0", /* RequestedCount */
689                                       "" /* SortCriteria */
690                                       );
691     vlc_UrlClean( &url );
692     if ( !p_response )
693     {
694         msg_Err( access_, "No response from browse() action" );
695         return false;
696     }
697
698     IXML_Document* p_result = parseBrowseResult( p_response );
699
700     ixmlDocument_free( p_response );
701
702     if ( !p_result )
703     {
704         msg_Err( access_, "browse() response parsing failed" );
705         return false;
706     }
707
708 #ifndef NDEBUG
709     msg_Dbg( access_, "Got DIDL document: %s", ixmlPrintDocument( p_result ) );
710 #endif
711
712     IXML_NodeList* containerNodeList =
713                 ixmlDocument_getElementsByTagName( p_result, "container" );
714
715     if ( containerNodeList )
716     {
717         for ( unsigned int i = 0; i < ixmlNodeList_length( containerNodeList ); i++ )
718         {
719             IXML_Element* containerElement = (IXML_Element*)ixmlNodeList_item( containerNodeList, i );
720
721             const char* objectID = ixmlElement_getAttribute( containerElement,
722                                                              "id" );
723             if ( !objectID )
724                 continue;
725
726             const char* title = xml_getChildElementValue( containerElement,
727                                                           "dc:title" );
728             if ( !title )
729                 continue;
730             addItem(objectID, title);
731         }
732         ixmlNodeList_free( containerNodeList );
733     }
734
735     IXML_NodeList* itemNodeList = ixmlDocument_getElementsByTagName( p_result,
736                                                                      "item" );
737     if ( itemNodeList )
738     {
739         for ( unsigned int i = 0; i < ixmlNodeList_length( itemNodeList ); i++ )
740         {
741             IXML_Element* itemElement =
742                         ( IXML_Element* )ixmlNodeList_item( itemNodeList, i );
743
744             const char* objectID =
745                         ixmlElement_getAttribute( itemElement, "id" );
746
747             if ( !objectID )
748                 continue;
749
750             const char* title =
751                         xml_getChildElementValue( itemElement, "dc:title" );
752
753             if ( !title )
754                 continue;
755
756             const char* psz_subtitles = xml_getChildElementValue( itemElement,
757                     "sec:CaptionInfo" );
758
759             if ( !psz_subtitles )
760                 psz_subtitles = xml_getChildElementValue( itemElement,
761                         "sec:CaptionInfoEx" );
762
763             if ( !psz_subtitles )
764                 psz_subtitles = xml_getChildElementValue( itemElement,
765                         "pv:subtitlefile" );
766
767             /* Try to extract all resources in DIDL */
768             IXML_NodeList* p_resource_list = ixmlDocument_getElementsByTagName( (IXML_Document*) itemElement, "res" );
769             if ( p_resource_list )
770             {
771                 int i_length = ixmlNodeList_length( p_resource_list );
772                 for ( int i = 0; i < i_length; i++ )
773                 {
774                     mtime_t i_duration = -1;
775                     int i_hours, i_minutes, i_seconds;
776                     IXML_Element* p_resource = ( IXML_Element* ) ixmlNodeList_item( p_resource_list, i );
777                     const char* psz_resource_url = xml_getChildElementValue( p_resource, "res" );
778                     if( !psz_resource_url )
779                         continue;
780                     const char* psz_duration = ixmlElement_getAttribute( p_resource, "duration" );
781
782                     if ( psz_duration )
783                     {
784                         if( sscanf( psz_duration, "%d:%02d:%02d",
785                             &i_hours, &i_minutes, &i_seconds ) )
786                             i_duration = INT64_C(1000000) * ( i_hours*3600 +
787                                                               i_minutes*60 +
788                                                               i_seconds );
789                     }
790
791                     addItem( title, objectID, psz_subtitles, i_duration, psz_resource_url );
792                 }
793                 ixmlNodeList_free( p_resource_list );
794             }
795             else
796                 continue;
797         }
798         ixmlNodeList_free( itemNodeList );
799     }
800
801     ixmlDocument_free( p_result );
802     return true;
803 }
804
805 static int ReadDirectory( access_t *p_access, input_item_node_t* p_node )
806 {
807     MediaServer server( p_access->psz_location, p_access, p_node );
808
809     if ( !server.fetchContents() )
810         return VLC_EGENERIC;
811     return VLC_SUCCESS;
812 }
813
814 static int Control( access_t *, int i_query, va_list args )
815 {
816     switch ( i_query )
817     {
818     case ACCESS_CAN_SEEK:
819     case ACCESS_CAN_FASTSEEK:
820     case ACCESS_CAN_PAUSE:
821     case ACCESS_CAN_CONTROL_PACE:
822         *va_arg( args, bool* ) = false;
823         break;
824
825     case ACCESS_GET_SIZE:
826     {
827         *va_arg( args, uint64_t * ) = 0;
828         break;
829     }
830     case ACCESS_GET_PTS_DELAY:
831         *va_arg( args, int64_t * ) = 0;
832         break;
833
834     case ACCESS_SET_PAUSE_STATE:
835         /* Nothing to do */
836         break;
837
838     default:
839         return VLC_EGENERIC;
840     }
841     return VLC_SUCCESS;
842 }
843
844 static int Open( vlc_object_t *p_this )
845 {
846     access_t* p_access = (access_t*)p_this;
847     access_sys_t* p_sys = new(std::nothrow) access_sys_t;
848     if ( unlikely( !p_sys ) )
849         return VLC_ENOMEM;
850
851     p_access->p_sys = p_sys;
852     p_sys->p_upnp = UpnpInstanceWrapper::get( p_this, NULL, NULL );
853     if ( !p_sys->p_upnp )
854     {
855         delete p_sys;
856         return VLC_EGENERIC;
857     }
858
859     p_access->pf_readdir = ReadDirectory;
860     ACCESS_SET_CALLBACKS( NULL, NULL, Control, NULL );
861
862     return VLC_SUCCESS;
863 }
864
865 static void Close( vlc_object_t* p_this )
866 {
867     access_t* p_access = (access_t*)p_this;
868     p_access->p_sys->p_upnp->release( false );
869     delete p_access->p_sys;
870 }
871
872 }
873
874 UpnpInstanceWrapper::UpnpInstanceWrapper()
875     : handle_( -1 )
876     , opaque_( NULL )
877     , callback_( NULL )
878     , refcount_( 0 )
879 {
880 }
881
882 UpnpInstanceWrapper::~UpnpInstanceWrapper()
883 {
884     UpnpUnRegisterClient( handle_ );
885     UpnpFinish();
886 }
887
888 UpnpInstanceWrapper *UpnpInstanceWrapper::get(vlc_object_t *p_obj, Upnp_FunPtr callback, SD::MediaServerList *opaque)
889 {
890     vlc_mutex_locker lock( &s_lock );
891     if ( s_instance == NULL )
892     {
893         UpnpInstanceWrapper* instance = new(std::nothrow) UpnpInstanceWrapper;
894         if ( unlikely( !instance ) )
895             return NULL;
896
897     #ifdef UPNP_ENABLE_IPV6
898         char* psz_miface = var_InheritString( p_obj, "miface" );
899         msg_Info( p_obj, "Initializing libupnp on '%s' interface", psz_miface );
900         int i_res = UpnpInit2( psz_miface, 0 );
901         free( psz_miface );
902     #else
903         /* If UpnpInit2 isnt available, initialize on first IPv4-capable interface */
904         int i_res = UpnpInit( 0, 0 );
905     #endif
906         if( i_res != UPNP_E_SUCCESS )
907         {
908             msg_Err( p_obj, "Initialization failed: %s", UpnpGetErrorMessage( i_res ) );
909             delete instance;
910             return NULL;
911         }
912
913         ixmlRelaxParser( 1 );
914
915         /* Register a control point */
916         i_res = UpnpRegisterClient( Callback, instance, &instance->handle_ );
917         if( i_res != UPNP_E_SUCCESS )
918         {
919             msg_Err( p_obj, "Client registration failed: %s", UpnpGetErrorMessage( i_res ) );
920             delete instance;
921             return NULL;
922         }
923
924         /* libupnp does not treat a maximum content length of 0 as unlimited
925          * until 64dedf (~ pupnp v1.6.7) and provides no sane way to discriminate
926          * between versions */
927         if( (i_res = UpnpSetMaxContentLength( INT_MAX )) != UPNP_E_SUCCESS )
928         {
929             msg_Err( p_obj, "Failed to set maximum content length: %s",
930                     UpnpGetErrorMessage( i_res ));
931             delete instance;
932             return NULL;
933         }
934         s_instance = instance;
935     }
936     s_instance->refcount_++;
937     // This assumes a single UPNP SD instance
938     if (callback && opaque)
939     {
940         assert(!s_instance->callback_ && !s_instance->opaque_);
941         s_instance->opaque_ = opaque;
942         s_instance->callback_ = callback;
943     }
944     return s_instance;
945 }
946
947 void UpnpInstanceWrapper::release(bool isSd)
948 {
949     vlc_mutex_locker lock( &s_lock );
950     if ( isSd )
951     {
952         callback_ = NULL;
953         opaque_ = NULL;
954     }
955     if (--s_instance->refcount_ == 0)
956     {
957         delete s_instance;
958         s_instance = NULL;
959     }
960 }
961
962 UpnpClient_Handle UpnpInstanceWrapper::handle() const
963 {
964     return handle_;
965 }
966
967 int UpnpInstanceWrapper::Callback(Upnp_EventType event_type, void *p_event, void *p_user_data)
968 {
969     UpnpInstanceWrapper* self = static_cast<UpnpInstanceWrapper*>( p_user_data );
970     vlc_mutex_locker lock( &self->s_lock );
971     if ( !self->callback_ )
972         return 0;
973     self->callback_( event_type, p_event, self->opaque_ );
974     return 0;
975 }