/
upnp.cpp
1652 lines (1411 loc) · 52.6 KB
/
upnp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* upnp.cpp : UPnP discovery module (libupnp)
*****************************************************************************
* Copyright (C) 2004-2018 VLC authors and VideoLAN
*
* Authors: Rémi Denis-Courmont (original plugin)
* Christian Henz <henz # c-lab.de>
* Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
* Hugo Beauzée-Luyssen <hugo@beauzee.fr>
* Shaleen Jain <shaleen@jain.sh>
* William Ung <william1.ung@epitech.eu>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "upnp.hpp"
#include <vlc_access.h>
#include <vlc_plugin.h>
#include <vlc_interrupt.h>
#include <vlc_services_discovery.h>
#include <vlc_renderer_discovery.h>
#include <assert.h>
#include <limits.h>
#include <algorithm>
#include <set>
#include <string>
/*
* Constants
*/
const char* MEDIA_SERVER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaServer:1";
const char* MEDIA_RENDERER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaRenderer:1";
const char* CONTENT_DIRECTORY_SERVICE_TYPE = "urn:schemas-upnp-org:service:ContentDirectory:1";
const char* SATIP_SERVER_DEVICE_TYPE = "urn:ses-com:device:SatIPServer:1";
#define UPNP_SEARCH_TIMEOUT_SECONDS 15
#define SATIP_CHANNEL_LIST N_("SAT>IP channel list")
#define SATIP_CHANNEL_LIST_URL N_("Custom SAT>IP channel list URL")
#define HTTP_PORT 7070
#define HTTP_PORT_TEXT N_("HTTP port")
#define HTTP_PORT_LONGTEXT N_("This sets the HTTP port of the local server used to stream the media to the UPnP Renderer.")
#define HAS_VIDEO_TEXT N_("Video")
#define HAS_VIDEO_LONGTEXT N_("The UPnP Renderer can receive video.")
#define IP_ADDR_TEXT N_("IP Address")
#define IP_ADDR_LONGTEXT N_("IP Address of the UPnP Renderer.")
#define PORT_TEXT N_("UPnP Renderer port")
#define PORT_LONGTEXT N_("The port used to talk to the UPnP Renderer.")
#define BASE_URL_TEXT N_("base URL")
#define BASE_URL_LONGTEXT N_("The base Url relative to which all other UPnP operations must be called")
#define URL_TEXT N_("description URL")
#define URL_LONGTEXT N_("The Url used to get the xml descriptor of the UPnP Renderer")
static const char *const ppsz_satip_channel_lists[] = {
"Auto", "ASTRA_19_2E", "ASTRA_28_2E", "ASTRA_23_5E", "MasterList", "ServerList", "CustomList"
};
static const char *const ppsz_readible_satip_channel_lists[] = {
N_("Auto"), "Astra 19.2°E", "Astra 28.2°E", "Astra 23.5°E", N_("SAT>IP Main List"), N_("Device List"), N_("Custom List")
};
namespace {
/*
* VLC handle
*/
struct services_discovery_sys_t
{
UpnpInstanceWrapper* p_upnp;
std::shared_ptr<SD::MediaServerList> p_server_list;
vlc_thread_t thread;
};
struct renderer_discovery_sys_t
{
UpnpInstanceWrapper* p_upnp;
std::shared_ptr<RD::MediaRendererList> p_renderer_list;
vlc_thread_t thread;
};
struct access_sys_t
{
UpnpInstanceWrapper* p_upnp;
};
} // namespace
/*
* VLC callback prototypes
*/
namespace SD
{
static int OpenSD( vlc_object_t* );
static void CloseSD( vlc_object_t* );
}
namespace Access
{
static int OpenAccess( vlc_object_t* );
static void CloseAccess( vlc_object_t* );
}
namespace RD
{
static int OpenRD( vlc_object_t*);
static void CloseRD( vlc_object_t* );
}
VLC_SD_PROBE_HELPER( "upnp", N_("Universal Plug'n'Play"), SD_CAT_LAN )
VLC_RD_PROBE_HELPER( "upnp_renderer", N_("UPnP Renderer Discovery") )
/*
* Module descriptor
*/
vlc_module_begin()
set_shortname( "UPnP" );
set_description( N_( "Universal Plug'n'Play" ) );
set_category( CAT_PLAYLIST );
set_subcategory( SUBCAT_PLAYLIST_SD );
set_capability( "services_discovery", 0 );
set_callbacks( SD::OpenSD, SD::CloseSD );
add_string( "satip-channelist", "auto", SATIP_CHANNEL_LIST,
SATIP_CHANNEL_LIST, false )
change_string_list( ppsz_satip_channel_lists, ppsz_readible_satip_channel_lists )
add_string( "satip-channellist-url", NULL, SATIP_CHANNEL_LIST_URL,
SATIP_CHANNEL_LIST_URL, false )
add_submodule()
set_category( CAT_INPUT )
set_subcategory( SUBCAT_INPUT_ACCESS )
set_callbacks( Access::OpenAccess, Access::CloseAccess )
set_capability( "access", 0 )
VLC_SD_PROBE_SUBMODULE
add_submodule()
set_description( N_( "UPnP Renderer Discovery" ) )
set_category( CAT_SOUT )
set_subcategory( SUBCAT_SOUT_RENDERER )
set_callbacks( RD::OpenRD, RD::CloseRD )
set_capability( "renderer_discovery", 0 )
add_shortcut( "upnp_renderer" )
VLC_RD_PROBE_SUBMODULE
add_submodule()
set_shortname("dlna")
set_description(N_("UPnP/DLNA stream output"))
set_capability("sout stream", 0)
add_shortcut("dlna")
set_category(CAT_SOUT)
set_subcategory(SUBCAT_SOUT_STREAM)
set_callbacks(DLNA::OpenSout, DLNA::CloseSout)
add_string(SOUT_CFG_PREFIX "ip", NULL, IP_ADDR_TEXT, IP_ADDR_LONGTEXT, false)
add_integer(SOUT_CFG_PREFIX "port", NULL, PORT_TEXT, PORT_LONGTEXT, false)
add_integer(SOUT_CFG_PREFIX "http-port", HTTP_PORT, HTTP_PORT_TEXT, HTTP_PORT_LONGTEXT, false)
add_bool(SOUT_CFG_PREFIX "video", true, HAS_VIDEO_TEXT, HAS_VIDEO_LONGTEXT, false)
add_string(SOUT_CFG_PREFIX "base_url", NULL, BASE_URL_TEXT, BASE_URL_LONGTEXT, false)
add_string(SOUT_CFG_PREFIX "url", NULL, URL_TEXT, URL_LONGTEXT, false)
add_renderer_opts(SOUT_CFG_PREFIX)
vlc_module_end()
/*
* Extracts the result document from a SOAP response
*/
IXML_Document* parseBrowseResult( IXML_Document* p_doc )
{
assert( p_doc );
// ixml*_getElementsByTagName will ultimately only case the pointer to a Node
// pointer, and pass it to a private function. Don't bother have a IXML_Document
// version of getChildElementValue
const char* psz_raw_didl = xml_getChildElementValue( (IXML_Element*)p_doc, "Result" );
if( !psz_raw_didl )
return NULL;
/* First, try parsing the buffer as is */
IXML_Document* p_result_doc = ixmlParseBuffer( psz_raw_didl );
if( !p_result_doc ) {
/* Missing namespaces confuse the ixml parser. This is a very ugly
* hack but it is needeed until devices start sending valid XML.
*
* It works that way:
*
* The DIDL document is extracted from the Result tag, then wrapped into
* a valid XML header and a new root tag which contains missing namespace
* definitions so the ixml parser understands it.
*
* If you know of a better workaround, please oh please fix it */
const char* psz_xml_result_fmt = "<?xml version=\"1.0\" ?>"
"<Result xmlns:sec=\"urn:samsung:metadata:2009\">%s</Result>";
char* psz_xml_result_string = NULL;
if( -1 == asprintf( &psz_xml_result_string,
psz_xml_result_fmt,
psz_raw_didl) )
return NULL;
p_result_doc = ixmlParseBuffer( psz_xml_result_string );
free( psz_xml_result_string );
}
if( !p_result_doc )
return NULL;
IXML_NodeList *p_elems = ixmlDocument_getElementsByTagName( p_result_doc,
"DIDL-Lite" );
IXML_Node *p_node = ixmlNodeList_item( p_elems, 0 );
ixmlNodeList_free( p_elems );
return (IXML_Document*)p_node;
}
/**
* Reads the base URL from an XML device list
*
* \param services_discovery_t* p_sd This SD instance
* \param IXML_Document* p_desc an XML device list document
*
* \return const char* The base URL
*/
static const char *parseBaseUrl( IXML_Document *p_desc )
{
const char *psz_base_url = nullptr;
IXML_NodeList *p_url_list = nullptr;
if( ( p_url_list = ixmlDocument_getElementsByTagName( p_desc, "URLBase" ) ) )
{
if ( IXML_Node* p_url_node = ixmlNodeList_item( p_url_list, 0 ) )
{
IXML_Node* p_text_node = ixmlNode_getFirstChild( p_url_node );
if ( p_text_node )
psz_base_url = ixmlNode_getNodeValue( p_text_node );
}
ixmlNodeList_free( p_url_list );
}
return psz_base_url;
}
namespace SD
{
static void *
SearchThread( void *p_data )
{
services_discovery_t *p_sd = ( services_discovery_t* )p_data;
services_discovery_sys_t *p_sys = reinterpret_cast<services_discovery_sys_t *>( p_sd->p_sys );
/* Search for media servers */
int i_res = UpnpSearchAsync( p_sys->p_upnp->handle(), 5,
MEDIA_SERVER_DEVICE_TYPE, MEDIA_SERVER_DEVICE_TYPE );
if( i_res != UPNP_E_SUCCESS )
{
msg_Err( p_sd, "Error sending search request: %s", UpnpGetErrorMessage( i_res ) );
return NULL;
}
/* Search for Sat Ip servers*/
i_res = UpnpSearchAsync( p_sys->p_upnp->handle(), 5,
SATIP_SERVER_DEVICE_TYPE, MEDIA_SERVER_DEVICE_TYPE );
if( i_res != UPNP_E_SUCCESS )
msg_Err( p_sd, "Error sending search request: %s", UpnpGetErrorMessage( i_res ) );
return NULL;
}
/*
* Initializes UPNP instance.
*/
static int OpenSD( vlc_object_t *p_this )
{
services_discovery_t *p_sd = ( services_discovery_t* )p_this;
services_discovery_sys_t *p_sys = new (std::nothrow) services_discovery_sys_t();
if( !( p_sd->p_sys = p_sys ) )
return VLC_ENOMEM;
p_sd->description = _("Universal Plug'n'Play");
p_sys->p_upnp = UpnpInstanceWrapper::get( p_this );
if ( !p_sys->p_upnp )
{
delete p_sys;
return VLC_EGENERIC;
}
try
{
p_sys->p_server_list = std::make_shared<SD::MediaServerList>( p_sd );
}
catch ( const std::bad_alloc& )
{
msg_Err( p_sd, "Failed to create a MediaServerList");
p_sys->p_upnp->release();
delete p_sys;
return VLC_EGENERIC;
}
p_sys->p_upnp->addListener( p_sys->p_server_list );
/* XXX: Contrary to what the libupnp doc states, UpnpSearchAsync is
* blocking (select() and send() are called). Therefore, Call
* UpnpSearchAsync from an other thread. */
if ( vlc_clone( &p_sys->thread, SearchThread, p_this,
VLC_THREAD_PRIORITY_LOW ) )
{
p_sys->p_upnp->removeListener( p_sys->p_server_list );
p_sys->p_upnp->release();
delete p_sys;
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
/*
* Releases resources.
*/
static void CloseSD( vlc_object_t *p_this )
{
services_discovery_t *p_sd = ( services_discovery_t* )p_this;
services_discovery_sys_t *p_sys = reinterpret_cast<services_discovery_sys_t *>( p_sd->p_sys );
vlc_join( p_sys->thread, NULL );
p_sys->p_upnp->removeListener( p_sys->p_server_list );
p_sys->p_upnp->release();
delete p_sys;
}
MediaServerDesc::MediaServerDesc( const std::string& udn, const std::string& fName,
const std::string& loc, const std::string& iconUrl )
: UDN( udn )
, friendlyName( fName )
, location( loc )
, iconUrl( iconUrl )
, inputItem( NULL )
, isSatIp( false )
{
}
MediaServerDesc::~MediaServerDesc()
{
if (inputItem)
input_item_Release( inputItem );
}
/*
* MediaServerList class
*/
MediaServerList::MediaServerList( services_discovery_t* p_sd )
: m_sd( p_sd )
{
}
MediaServerList::~MediaServerList()
{
vlc_delete_all(m_list);
}
bool MediaServerList::addServer( MediaServerDesc* desc )
{
input_item_t* p_input_item = NULL;
if ( getServer( desc->UDN ) )
return false;
msg_Dbg( m_sd, "Adding server '%s' with uuid '%s'", desc->friendlyName.c_str(), desc->UDN.c_str() );
if ( desc->isSatIp )
{
p_input_item = input_item_NewDirectory( desc->location.c_str(),
desc->friendlyName.c_str(),
ITEM_NET );
if ( !p_input_item )
return false;
input_item_SetSetting( p_input_item, SATIP_SERVER_DEVICE_TYPE );
char *psz_playlist_option;
if (asprintf( &psz_playlist_option, "satip-host=%s",
desc->satIpHost.c_str() ) >= 0 ) {
input_item_AddOption( p_input_item, psz_playlist_option, 0 );
free( psz_playlist_option );
}
} else {
char* psz_mrl;
// We might already have some options specified in the location.
char opt_delim = desc->location.find( '?' ) == 0 ? '?' : '&';
if( asprintf( &psz_mrl, "upnp://%s%cObjectID=0", desc->location.c_str(), opt_delim ) < 0 )
return false;
p_input_item = input_item_NewDirectory( psz_mrl,
desc->friendlyName.c_str(),
ITEM_NET );
free( psz_mrl );
if ( !p_input_item )
return false;
input_item_SetSetting( p_input_item, MEDIA_SERVER_DEVICE_TYPE );
}
if ( desc->iconUrl.empty() == false )
input_item_SetArtworkURL( p_input_item, desc->iconUrl.c_str() );
desc->inputItem = p_input_item;
input_item_SetDescription( p_input_item, desc->UDN.c_str() );
services_discovery_AddItem( m_sd, p_input_item );
m_list.push_back( desc );
return true;
}
MediaServerDesc* MediaServerList::getServer( const std::string& udn )
{
std::vector<MediaServerDesc*>::const_iterator it = m_list.begin();
std::vector<MediaServerDesc*>::const_iterator ite = m_list.end();
for ( ; it != ite; ++it )
{
if( udn == (*it)->UDN )
{
return *it;
}
}
return NULL;
}
void MediaServerList::parseNewServer( IXML_Document *doc, const std::string &location )
{
if ( !doc )
{
msg_Err( m_sd, "Null IXML_Document" );
return;
}
if ( location.empty() )
{
msg_Err( m_sd, "Empty location" );
return;
}
const char* psz_base_url = location.c_str();
/* Try to extract baseURL */
IXML_NodeList* p_url_list = ixmlDocument_getElementsByTagName( doc, "URLBase" );
if ( p_url_list )
{
if ( IXML_Node* p_url_node = ixmlNodeList_item( p_url_list, 0 ) )
{
IXML_Node* p_text_node = ixmlNode_getFirstChild( p_url_node );
if ( p_text_node )
psz_base_url = ixmlNode_getNodeValue( p_text_node );
}
ixmlNodeList_free( p_url_list );
}
/* Get devices */
IXML_NodeList* p_device_list = ixmlDocument_getElementsByTagName( doc, "device" );
if ( !p_device_list )
return;
for ( unsigned int i = 0; i < ixmlNodeList_length( p_device_list ); i++ )
{
IXML_Element* p_device_element = ( IXML_Element* ) ixmlNodeList_item( p_device_list, i );
if( !p_device_element )
continue;
const char* psz_device_type = xml_getChildElementValue( p_device_element, "deviceType" );
if ( !psz_device_type )
{
msg_Warn( m_sd, "No deviceType found!" );
continue;
}
if ( strncmp( MEDIA_SERVER_DEVICE_TYPE, psz_device_type,
strlen( MEDIA_SERVER_DEVICE_TYPE ) - 1 )
&& strncmp( SATIP_SERVER_DEVICE_TYPE, psz_device_type,
strlen( SATIP_SERVER_DEVICE_TYPE ) - 1 ) )
continue;
const char* psz_udn = xml_getChildElementValue( p_device_element,
"UDN" );
if ( !psz_udn )
{
msg_Warn( m_sd, "No UDN!" );
continue;
}
/* Check if server is already added */
if ( getServer( psz_udn ) )
{
msg_Warn( m_sd, "Server with uuid '%s' already exists.", psz_udn );
continue;
}
const char* psz_friendly_name =
xml_getChildElementValue( p_device_element,
"friendlyName" );
if ( !psz_friendly_name )
{
msg_Dbg( m_sd, "No friendlyName!" );
continue;
}
std::string iconUrl = getIconURL( p_device_element, psz_base_url );
// We now have basic info, we need to get the content browsing url
// so the access module can browse without fetching the manifest again
if ( !strncmp( SATIP_SERVER_DEVICE_TYPE, psz_device_type,
strlen( SATIP_SERVER_DEVICE_TYPE ) - 1 ) ) {
parseSatipServer( p_device_element, psz_base_url, psz_udn, psz_friendly_name, iconUrl );
}
/* Check for ContentDirectory service. */
IXML_NodeList* p_service_list = ixmlElement_getElementsByTagName( p_device_element, "service" );
if ( !p_service_list )
continue;
for ( unsigned int j = 0; j < ixmlNodeList_length( p_service_list ); j++ )
{
IXML_Element* p_service_element = (IXML_Element*)ixmlNodeList_item( p_service_list, j );
const char* psz_service_type = xml_getChildElementValue( p_service_element, "serviceType" );
if ( !psz_service_type )
{
msg_Warn( m_sd, "No service type found." );
continue;
}
int k = strlen( CONTENT_DIRECTORY_SERVICE_TYPE ) - 1;
if ( strncmp( CONTENT_DIRECTORY_SERVICE_TYPE,
psz_service_type, k ) )
continue;
const char* psz_control_url = xml_getChildElementValue( p_service_element,
"controlURL" );
if ( !psz_control_url )
{
msg_Warn( m_sd, "No control url found." );
continue;
}
/* Try to browse content directory. */
char* psz_url = NULL;
if ( UpnpResolveURL2( psz_base_url, psz_control_url, &psz_url ) == UPNP_E_SUCCESS )
{
SD::MediaServerDesc* p_server = new(std::nothrow) SD::MediaServerDesc( psz_udn,
psz_friendly_name, psz_url, iconUrl );
free( psz_url );
if ( unlikely( !p_server ) )
break;
if ( !addServer( p_server ) )
{
delete p_server;
continue;
}
}
}
ixmlNodeList_free( p_service_list );
}
ixmlNodeList_free( p_device_list );
}
std::string MediaServerList::getIconURL( IXML_Element* p_device_elem, const char* psz_base_url )
{
std::string res;
IXML_NodeList* p_icon_lists = ixmlElement_getElementsByTagName( p_device_elem, "iconList" );
if ( p_icon_lists == NULL )
return res;
IXML_Element* p_icon_list = (IXML_Element*)ixmlNodeList_item( p_icon_lists, 0 );
if ( p_icon_list != NULL )
{
IXML_NodeList* p_icons = ixmlElement_getElementsByTagName( p_icon_list, "icon" );
if ( p_icons != NULL )
{
unsigned int maxWidth = 0;
unsigned int maxHeight = 0;
for ( unsigned int i = 0; i < ixmlNodeList_length( p_icons ); ++i )
{
IXML_Element* p_icon = (IXML_Element*)ixmlNodeList_item( p_icons, i );
const char* widthStr = xml_getChildElementValue( p_icon, "width" );
const char* heightStr = xml_getChildElementValue( p_icon, "height" );
if ( widthStr == NULL || heightStr == NULL )
continue;
unsigned int width = atoi( widthStr );
unsigned int height = atoi( heightStr );
if ( width <= maxWidth || height <= maxHeight )
continue;
const char* iconUrl = xml_getChildElementValue( p_icon, "url" );
if ( iconUrl == NULL )
continue;
maxWidth = width;
maxHeight = height;
res = iconUrl;
}
ixmlNodeList_free( p_icons );
}
}
ixmlNodeList_free( p_icon_lists );
if ( res.empty() == false )
{
vlc_url_t url;
vlc_UrlParse( &url, psz_base_url );
char* psz_url;
if ( asprintf( &psz_url, "%s://%s:%u%s", url.psz_protocol, url.psz_host, url.i_port, res.c_str() ) < 0 )
res.clear();
else
{
res = psz_url;
free( psz_url );
}
vlc_UrlClean( &url );
}
return res;
}
void
MediaServerList::parseSatipServer( IXML_Element* p_device_element, const char *psz_base_url, const char *psz_udn, const char *psz_friendly_name, std::string iconUrl )
{
SD::MediaServerDesc* p_server = NULL;
char *psz_satip_channellist = config_GetPsz("satip-channelist");
if( !psz_satip_channellist ) {
psz_satip_channellist = strdup("Auto");
}
if( unlikely( !psz_satip_channellist ) )
return;
vlc_url_t url;
vlc_UrlParse( &url, psz_base_url );
/* Part 1: a user may have provided a custom playlist url */
if (strncmp(psz_satip_channellist, "CustomList", 10) == 0) {
char *psz_satip_playlist_url = config_GetPsz( "satip-channellist-url" );
if ( psz_satip_playlist_url ) {
p_server = new(std::nothrow) SD::MediaServerDesc( psz_udn, psz_friendly_name, psz_satip_playlist_url, iconUrl );
if( likely( p_server ) ) {
p_server->satIpHost = url.psz_host;
p_server->isSatIp = true;
if( !addServer( p_server ) ) {
delete p_server;
}
}
/* to comply with the SAT>IP specification, we don't fall back on another channel list if this path failed */
free( psz_satip_channellist );
free( psz_satip_playlist_url );
vlc_UrlClean( &url );
return;
}
}
/* Part 2: device playlist
* In Automatic mode, or if requested by the user, check for a SAT>IP m3u list on the device */
if (strncmp(psz_satip_channellist, "ServerList", 10) == 0 ||
strncmp(psz_satip_channellist, "Auto", strlen ("Auto")) == 0 ) {
const char* psz_m3u_url = xml_getChildElementValue( p_device_element, "satip:X_SATIPM3U" );
if ( psz_m3u_url ) {
if ( strncmp( "http", psz_m3u_url, 4) )
{
char* psz_url = NULL;
if ( UpnpResolveURL2( psz_base_url, psz_m3u_url, &psz_url ) == UPNP_E_SUCCESS )
{
p_server = new(std::nothrow) SD::MediaServerDesc( psz_udn, psz_friendly_name, psz_url, iconUrl );
free(psz_url);
}
} else {
p_server = new(std::nothrow) SD::MediaServerDesc( psz_udn, psz_friendly_name, psz_m3u_url, iconUrl );
}
if ( unlikely( !p_server ) )
{
free( psz_satip_channellist );
vlc_UrlClean( &url );
return;
}
p_server->satIpHost = url.psz_host;
p_server->isSatIp = true;
if ( !addServer( p_server ) )
delete p_server;
} else {
msg_Dbg( m_sd, "SAT>IP server '%s' did not provide a playlist", url.psz_host);
}
if(strncmp(psz_satip_channellist, "ServerList", 10) == 0) {
/* to comply with the SAT>IP specifications, we don't fallback on another channel list if this path failed,
* but in Automatic mode, we continue */
free(psz_satip_channellist);
vlc_UrlClean( &url );
return;
}
}
/* Part 3: satip.info playlist
* In the normal case, fetch a playlist from the satip website,
* which will be processed by a lua script a bit later, to make it work sanely
* MasterList is a list of usual Satellites */
/* In Auto mode, default to MasterList list from satip.info */
if( strncmp(psz_satip_channellist, "Auto", strlen ("Auto")) == 0 ) {
free(psz_satip_channellist);
psz_satip_channellist = strdup( "MasterList" );
}
char *psz_url;
if (asprintf( &psz_url, "http://www.satip.info/Playlists/%s.m3u",
psz_satip_channellist ) < 0 ) {
vlc_UrlClean( &url );
free( psz_satip_channellist );
return;
}
p_server = new(std::nothrow) SD::MediaServerDesc( psz_udn,
psz_friendly_name, psz_url, iconUrl );
if( likely( p_server ) ) {
p_server->satIpHost = url.psz_host;
p_server->isSatIp = true;
if( !addServer( p_server ) ) {
delete p_server;
}
}
free( psz_url );
free( psz_satip_channellist );
vlc_UrlClean( &url );
}
void MediaServerList::removeServer( const std::string& udn )
{
MediaServerDesc* p_server = getServer( udn );
if ( !p_server )
return;
msg_Dbg( m_sd, "Removing server '%s'", p_server->friendlyName.c_str() );
assert(p_server->inputItem);
services_discovery_RemoveItem( m_sd, p_server->inputItem );
std::vector<MediaServerDesc*>::iterator it = std::find(m_list.begin(), m_list.end(), p_server);
if (it != m_list.end())
{
m_list.erase( it );
}
delete p_server;
}
/*
* Handles servers listing UPnP events
*/
int MediaServerList::onEvent( Upnp_EventType event_type, UpnpEventPtr p_event, void* p_user_data )
{
if (p_user_data != MEDIA_SERVER_DEVICE_TYPE)
return 0;
switch( event_type )
{
case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
case UPNP_DISCOVERY_SEARCH_RESULT:
{
const UpnpDiscovery* p_discovery = ( const UpnpDiscovery* )p_event;
IXML_Document *p_description_doc = NULL;
int i_res;
i_res = UpnpDownloadXmlDoc( UpnpDiscovery_get_Location_cstr( p_discovery ), &p_description_doc );
if ( i_res != UPNP_E_SUCCESS )
{
msg_Warn( m_sd, "Could not download device description! "
"Fetching data from %s failed: %s",
UpnpDiscovery_get_Location_cstr( p_discovery ), UpnpGetErrorMessage( i_res ) );
return i_res;
}
parseNewServer( p_description_doc, UpnpDiscovery_get_Location_cstr( p_discovery ) );
ixmlDocument_free( p_description_doc );
}
break;
case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
{
const UpnpDiscovery* p_discovery = ( const UpnpDiscovery* )p_event;
removeServer( UpnpDiscovery_get_DeviceID_cstr( p_discovery ) );
}
break;
case UPNP_EVENT_SUBSCRIBE_COMPLETE:
{
msg_Warn( m_sd, "subscription complete" );
}
break;
case UPNP_DISCOVERY_SEARCH_TIMEOUT:
{
msg_Warn( m_sd, "search timeout" );
}
break;
case UPNP_EVENT_RECEIVED:
case UPNP_EVENT_AUTORENEWAL_FAILED:
case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
// Those are for the access part
break;
default:
{
msg_Err( m_sd, "Unhandled event, please report ( type=%d )", event_type );
}
break;
}
return UPNP_E_SUCCESS;
}
}
namespace Access
{
namespace
{
class ItemDescriptionHolder
{
private:
struct Slave : std::string
{
slave_type type;
Slave(std::string const &url, slave_type type) :
std::string(url), type(type)
{
}
};
std::set<Slave> slaves;
const char* objectID,
* title,
* psz_artist,
* psz_genre,
* psz_album,
* psz_date,
* psz_orig_track_nb,
* psz_album_artist,
* psz_albumArt;
public:
enum MEDIA_TYPE
{
VIDEO = 0,
AUDIO,
IMAGE,
CONTAINER
};
MEDIA_TYPE media_type;
ItemDescriptionHolder()
{
}
bool init(IXML_Element *itemElement)
{
objectID = ixmlElement_getAttribute( itemElement, "id" );
if ( !objectID )
return false;
title = xml_getChildElementValue( itemElement, "dc:title" );
if ( !title )
return false;
const char *psz_subtitles = xml_getChildElementValue( itemElement, "sec:CaptionInfo" );
if ( !psz_subtitles &&
!(psz_subtitles = xml_getChildElementValue( itemElement, "sec:CaptionInfoEx" )) )
psz_subtitles = xml_getChildElementValue( itemElement, "pv:subtitlefile" );
addSlave(psz_subtitles, SLAVE_TYPE_SPU);
psz_artist = xml_getChildElementValue( itemElement, "upnp:artist" );
psz_genre = xml_getChildElementValue( itemElement, "upnp:genre" );
psz_album = xml_getChildElementValue( itemElement, "upnp:album" );
psz_date = xml_getChildElementValue( itemElement, "dc:date" );
psz_orig_track_nb = xml_getChildElementValue( itemElement, "upnp:originalTrackNumber" );
psz_album_artist = xml_getChildElementValue( itemElement, "upnp:albumArtist" );
psz_albumArt = xml_getChildElementValue( itemElement, "upnp:albumArtURI" );
const char *psz_media_type = xml_getChildElementValue( itemElement, "upnp:class" );
if (strncmp(psz_media_type, "object.item.videoItem", 21) == 0)
media_type = VIDEO;
else if (strncmp(psz_media_type, "object.item.audioItem", 21) == 0)
media_type = AUDIO;
else if (strncmp(psz_media_type, "object.item.imageItem", 21) == 0)
media_type = IMAGE;
else if (strncmp(psz_media_type, "object.container", 16 ) == 0)
media_type = CONTAINER;
else
return false;
return true;
}
void addSlave(const char *psz_slave, slave_type type)
{
if (psz_slave)
slaves.insert(Slave(psz_slave, type));
}
void addSubtitleSlave(IXML_Element* p_resource)
{
if (slaves.empty())
addSlave(ixmlElement_getAttribute( p_resource, "pv:subtitleFileUri" ),
SLAVE_TYPE_SPU);
}
void setArtworkURL(IXML_Element* p_resource)
{
psz_albumArt = xml_getChildElementValue( p_resource, "res" );
}
void apply(input_item_t *p_item)
{
if ( psz_artist != NULL )
input_item_SetArtist( p_item, psz_artist );
if ( psz_genre != NULL )
input_item_SetGenre( p_item, psz_genre );
if ( psz_album != NULL )
input_item_SetAlbum( p_item, psz_album );
if ( psz_date != NULL )
input_item_SetDate( p_item, psz_date );
if ( psz_orig_track_nb != NULL )
input_item_SetTrackNumber( p_item, psz_orig_track_nb );
if ( psz_album_artist != NULL )
input_item_SetAlbumArtist( p_item, psz_album_artist );
if ( psz_albumArt != NULL )
input_item_SetArtworkURL( p_item, psz_albumArt );
for (std::set<Slave>::iterator it = slaves.begin(); it != slaves.end(); ++it)
{
input_item_slave *p_slave = input_item_slave_New( it->c_str(), it->type,
SLAVE_PRIORITY_MATCH_ALL );
if ( p_slave )
input_item_AddSlave( p_item, p_slave );
}
}
input_item_t *createNewItem(IXML_Element *p_resource)
{
vlc_tick_t i_duration = INPUT_DURATION_INDEFINITE;
const char* psz_resource_url = xml_getChildElementValue( p_resource, "res" );
if( !psz_resource_url )
return NULL;
const char* psz_duration = ixmlElement_getAttribute( p_resource, "duration" );
if ( psz_duration )
{
int i_hours, i_minutes, i_seconds;
if( sscanf( psz_duration, "%d:%02d:%02d", &i_hours, &i_minutes, &i_seconds ) )
i_duration = vlc_tick_from_sec( i_hours * 3600 + i_minutes * 60 +
i_seconds );
}
return input_item_NewExt( psz_resource_url, title, i_duration,
ITEM_TYPE_FILE, ITEM_NET );
}
input_item_t *createNewContainerItem( const char* psz_root )
{
if ( objectID == NULL || title == NULL )
return NULL;
char* psz_url;
if( asprintf( &psz_url, "upnp://%s?ObjectID=%s", psz_root, objectID ) < 0 )
return NULL;
input_item_t* p_item = input_item_NewDirectory( psz_url, title, ITEM_NET );
free( psz_url);
return p_item;
}
};
}