1 /*****************************************************************************
2 * slp.c: SLP access plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
5 * $Id: slp.c,v 1.20 2004/01/25 17:31:22 gbazin Exp $
7 * Authors: Loïc Minier <lool@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc */
30 #include <vlc/input.h>
32 #include <vlc_playlist.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
41 static ssize_t Read( input_thread_t *, byte_t *, size_t );
43 static int Init ( vlc_object_t * );
44 static void End ( vlc_object_t * );
45 static int Demux ( input_thread_t * );
49 /*****************************************************************************
51 *****************************************************************************/
54 #define SRVTYPE_TEXT /*N_*/("SLP service type")
55 #define SRVTYPE_LONGTEXT /*N_*/( \
56 "The service type string for SLP queries, including the authority " \
57 "string (if any) for the request. May not be empty." )
60 #define ATTRIDS_TEXT N_("SLP attribute identifiers")
61 #define ATTRIDS_LONGTEXT N_( \
62 "This string is a comma separated list of attribute identifiers to " \
63 "search for a playlist title or empty to use all attributes." )
65 #define SCOPELIST_TEXT N_("SLP scopes list")
66 #define SCOPELIST_LONGTEXT N_( \
67 "This string is a comma separated list of scope names or empty if you " \
68 "want to use the default scopes. It is used in all SLP queries." )
70 #define NAMINGAUTHORITY_TEXT N_("SLP naming authority")
71 #define NAMINGAUTHORITY_LONGTEXT N_( \
72 "This string is a list of naming authorities to search. " \
73 "Use \"*\" for all and the empty string for the default of IANA." )
75 #define FILTER_TEXT N_("SLP LDAP filter")
76 #define FILTER_LONGTEXT N_( \
77 "This is a query formulated of attribute pattern matching expressions " \
78 "in the form of an LDAPv3 search filter or empty for all answers." )
80 #define LANG_TEXT N_("Language requested in SLP requests")
81 #define LANG_LONGTEXT N_( \
82 "RFC 1766 Language tag for the natural language locale of requests, " \
83 "leave empty to use the default locale. It is used in all SLP queries." )
86 set_description( _("SLP input") );
88 add_string( "slp-attrids", "", NULL, ATTRIDS_TEXT, ATTRIDS_LONGTEXT,
90 add_string( "slp-scopelist", "", NULL, SCOPELIST_TEXT,
91 SCOPELIST_LONGTEXT, VLC_TRUE );
92 add_string( "slp-namingauthority", "*", NULL, NAMINGAUTHORITY_TEXT,
93 NAMINGAUTHORITY_LONGTEXT, VLC_TRUE );
94 add_string( "slp-filter", "", NULL, FILTER_TEXT, FILTER_LONGTEXT,
96 add_string( "slp-lang", "", NULL, LANG_TEXT, LANG_LONGTEXT, VLC_TRUE );
98 set_capability( "access", 0 );
99 set_callbacks( Open, Close );
102 add_shortcut( "demux_slp" );
103 set_capability( "demux", 0 );
104 set_callbacks( Init, End );
107 /*****************************************************************************
108 * AttrCallback: updates the description of a playlist item
109 *****************************************************************************/
110 static SLPBoolean AttrCallback( SLPHandle slph_slp,
111 const char * psz_attrlist,
112 SLPError slpe_errcode,
115 playlist_item_t * p_playlist_item = (playlist_item_t *)p_cookie;
117 /* our callback was only called to tell us there's nothing more to read */
118 if( slpe_errcode == SLP_LAST_CALL )
123 /* or there was a problem with getting the data we requested */
124 if( (slpe_errcode != SLP_OK) )
127 msg_Err( (vlc_object_t*)NULL,
128 "AttrCallback got an error %i with attribute %s",
135 p_playlist_item->psz_name = strdup(psz_attrlist); /* NULL is checked */
139 /*****************************************************************************
140 * SrvUrlCallback: adds an entry to the playlist
141 *****************************************************************************/
142 static SLPBoolean SrvUrlCallback( SLPHandle slph_slp,
143 const char * psz_srvurl,
145 SLPError slpe_errcode,
148 input_thread_t * p_input = (input_thread_t *)p_cookie;
149 playlist_t * p_playlist;
150 char psz_item[42] = ""; //"udp:@";
151 char * psz_s; /* to hold the uri of the stream */
153 SLPError slpe_result;
154 playlist_item_t * p_playlist_item;
156 /* our callback was only called to tell us there's nothing more to read */
157 if( slpe_errcode == SLP_LAST_CALL )
162 msg_Dbg(p_input,"URL: %s",psz_srvurl);
164 /* or there was a problem with getting the data we requested */
165 if( (slpe_errcode != SLP_OK) )
167 msg_Err( p_input, "SrvUrlCallback got an error %i with URL %s",
168 slpe_errcode, psz_srvurl );
172 /* search the returned address after a double-slash */
173 psz_s = strstr( psz_srvurl, "//" );
176 msg_Err( (input_thread_t *)p_input,
177 "SrvUrlCallback got a strange string of your libslp" );
180 /* skip the slashes */
182 /* add udp:@ in front of the address */
183 psz_s = strncat( psz_item,
185 sizeof(psz_item) - strlen(psz_item) - 1 );
187 /* create a playlist item */
188 p_playlist_item = malloc( sizeof( playlist_item_t ) );
189 memset( p_playlist_item, 0, sizeof( playlist_item_t ) );
190 if( p_playlist_item == NULL )
192 msg_Err( p_input, "out of memory" );
196 p_playlist_item->psz_name = NULL;
197 p_playlist_item->psz_uri = strdup( psz_s );
198 p_playlist_item->i_duration = -1;
199 p_playlist_item->i_group = i_group;
200 p_playlist_item->b_enabled = VLC_TRUE;
202 /* search the description of the stream */
203 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
204 SLP_FALSE, /* synchronous ops */
205 &slph_slp3 ) == SLP_OK )
207 /* search all attributes */
208 slpe_result = SLPFindAttrs( slph_slp3,
210 config_GetPsz( p_input, "slp-scopelist" ),
211 config_GetPsz( p_input, "slp-attrids" ),
216 /* we're done, clean up */
217 SLPClose( slph_slp3 );
220 /* add a default name if we found no attribute */
221 if( p_playlist_item->psz_name == NULL )
223 p_playlist_item->psz_name = strdup( psz_s );
226 /* search the main playlist object */
227 p_playlist = vlc_object_find( (input_thread_t *)p_input,
230 if( p_playlist == NULL )
232 msg_Warn( (input_thread_t *)p_input,
233 "could not find playlist, not adding entries" );
237 playlist_AddItem( p_playlist,
241 vlc_object_release( (vlc_object_t *)p_playlist );
243 msg_Info( (input_thread_t *)p_input,
244 "added « %s » (lifetime %i) to playlist",
245 psz_srvurl, i_lifetime );
250 /*****************************************************************************
251 * SrvTypeCallback: searchs all servers of a certain type
252 *****************************************************************************/
253 static SLPBoolean SrvTypeCallback( SLPHandle slph_slp,
254 const char * psz_srvurl,
255 SLPError slpe_errcode,
258 input_thread_t * p_input = (input_thread_t *)p_cookie;
259 SLPError slpe_result;
264 msg_Dbg( p_input, "services: %s", psz_srvurl );
265 /* our callback was only called to tell us there's nothing more to read */
266 if( slpe_errcode == SLP_LAST_CALL )
271 msg_Dbg( p_input, "services: %s", psz_srvurl );
273 /* or there was a problem with getting the data we requested */
274 if( slpe_errcode != SLP_OK )
276 msg_Err( p_input, "SrvTypeCallback got an error %i with URL %s",
277 slpe_errcode, psz_srvurl );
281 /* get a new handle to the library */
282 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
283 SLP_FALSE, /* synchronous ops */
284 &slph_slp2 ) == SLP_OK )
286 /* search for services */
289 if( *psz_srvurl == '\0') break;
291 if( !strncasecmp( psz_srvurl, "service:", 8 ) )
295 psz_eos = strchr( psz_srvurl, ',');
297 if(!strncasecmp(psz_eos+1,"service:",8)) break;
303 psz_service = strdup( psz_srvurl);
305 msg_Dbg( p_input, "getting details for %s", psz_service );
307 slpe_result = SLPFindSrvs( slph_slp2,
309 config_GetPsz( p_input, "slp-scopelist" ),
310 config_GetPsz( p_input, "slp-filter" ),
315 psz_srvurl = psz_eos;
318 SLPClose( slph_slp2 );
320 if( slpe_result != SLP_OK )
323 "SLPFindSrvs error %i finding servers of type %s",
324 slpe_result, psz_service );
330 SLPClose( slph_slp2 );
335 /*****************************************************************************
336 * Open: initialize library for the access module
337 *****************************************************************************/
338 static int Open( vlc_object_t * p_this )
340 input_thread_t * p_input = (input_thread_t *)p_this;
341 SLPError slpe_result;
343 playlist_t * p_playlist;
344 playlist_group_t * p_group;
346 /* remove the "slp:" entry of the playlist */
347 p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
351 msg_Warn( p_input, "hey I can't find the main playlist, I need it" );
355 p_group = playlist_CreateGroup( p_playlist , "SLP" );
356 i_group = p_group->i_id;
357 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
358 vlc_object_release( (vlc_object_t *)p_playlist );
360 /* get a new handle to the library */
361 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
362 SLP_FALSE, /* synchronous ops */
363 &slph_slp ) == SLP_OK )
365 /* search all service types */
367 SLPFindSrvTypes( slph_slp,
368 config_GetPsz( p_input, "slp-namingauthority" ),
369 config_GetPsz( p_input, "slp-scopelist" ),
372 /* we're done, clean up */
373 SLPClose( slph_slp );
376 if( !p_input->psz_demux || !*p_input->psz_demux )
378 p_input->psz_demux = "demux_slp";
381 p_input->pf_read = Read;
382 p_input->pf_set_program = NULL;
383 p_input->pf_set_area = NULL;
384 p_input->pf_seek = NULL;
386 vlc_mutex_lock( &p_input->stream.stream_lock );
387 p_input->stream.b_pace_control = VLC_FALSE;
388 p_input->stream.b_seekable = VLC_FALSE;
389 p_input->stream.p_selected_area->i_tell = 0;
390 p_input->stream.p_selected_area->i_size = 0;
391 p_input->stream.i_method = INPUT_METHOD_SLP;
392 vlc_mutex_unlock( &p_input->stream.stream_lock );
398 /*****************************************************************************
399 * Close: close access
400 *****************************************************************************/
401 static void Close( vlc_object_t * p_this )
406 /*****************************************************************************
407 * Read: should fill but zeroes the buffer
408 *****************************************************************************/
409 static ssize_t Read ( input_thread_t *p_input, byte_t *p_buffer, size_t s )
411 memset( p_buffer, 0, s );
415 /*****************************************************************************
416 * Init: initialize demux
417 *****************************************************************************/
418 static int Init ( vlc_object_t *p_this )
420 input_thread_t *p_input = (input_thread_t *)p_this;
422 if( p_input->stream.i_method != INPUT_METHOD_SLP )
427 p_input->pf_demux = Demux;
428 p_input->pf_demux_control = demux_vaControlDefault;
429 p_input->pf_rewind = NULL;
434 /*****************************************************************************
435 * Demux: should demux but does nothing
436 *****************************************************************************/
437 static int Demux ( input_thread_t * p_input )
442 /*****************************************************************************
444 *****************************************************************************/
445 static void End ( vlc_object_t *p_this )