1 /*****************************************************************************
2 * slp.c: SLP access plugin
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: slp.c,v 1.19 2004/01/17 12:28:56 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 *****************************************************************************/
53 #define SRVTYPE_TEXT "SLP service type"
54 #define SRVTYPE_LONGTEXT "The service type string for SLP queries, " \
55 "including the authority string (if any) for the " \
56 "request. May not be empty"
57 #define ATTRIDS_TEXT "SLP attribute identifiers"
58 #define ATTRIDS_LONGTEXT "This string is a comma separated list of " \
59 "attribute identifiers to search for a playlist "\
60 "title or empty to use all attributes"
61 #define SCOPELIST_TEXT "SLP scopes list"
62 #define SCOPELIST_LONGTEXT "This string is a comma separated list of scope " \
63 "names or empty if you want to use the default " \
64 "scopes; it is used in all SLP queries"
65 #define NAMINGAUTHORITY_TEXT "SLP naming authority"
66 #define NAMINGAUTHORITY_LONGTEXT "This string is a list of naming " \
67 "authorities to search. Use \"*\" for all " \
68 "and the empty string for the default of " \
70 #define FILTER_TEXT "SLP LDAP filter"
71 #define FILTER_LONGTEXT "This is a query formulated of attribute pattern " \
72 "matching expressions in the form of an LDAPv3 " \
73 "search filter or empty for all answers"
74 #define LANG_TEXT "Language requested in SLP requests"
75 #define LANG_LONGTEXT "RFC 1766 Language Tag for the natural language " \
76 "locale of requests, leave empty to use the " \
77 "default locale; it is used in all SLP queries"
80 set_description( _("SLP input") );
81 add_category_hint( N_("slp"), NULL, VLC_TRUE );
82 add_string( "slp-attrids", "", NULL, ATTRIDS_TEXT, ATTRIDS_LONGTEXT, VLC_TRUE );
83 add_string( "slp-scopelist", "", NULL, SCOPELIST_TEXT,
84 SCOPELIST_LONGTEXT, VLC_TRUE );
85 add_string( "slp-namingauthority", "*", NULL, NAMINGAUTHORITY_TEXT,
86 NAMINGAUTHORITY_LONGTEXT, VLC_TRUE );
87 add_string( "slp-filter", "", NULL, FILTER_TEXT, FILTER_LONGTEXT, VLC_TRUE );
88 add_string( "slp-lang", "", NULL, LANG_TEXT, LANG_LONGTEXT, VLC_TRUE );
89 set_capability( "access", 0 );
90 set_callbacks( Open, Close );
93 add_shortcut( "demux_slp" );
94 set_capability( "demux", 0 );
95 set_callbacks( Init, End );
98 /*****************************************************************************
99 * AttrCallback: updates the description of a playlist item
100 *****************************************************************************/
101 static SLPBoolean AttrCallback( SLPHandle slph_slp,
102 const char * psz_attrlist,
103 SLPError slpe_errcode,
106 playlist_item_t * p_playlist_item = (playlist_item_t *)p_cookie;
108 /* our callback was only called to tell us there's nothing more to read */
109 if( slpe_errcode == SLP_LAST_CALL )
114 /* or there was a problem with getting the data we requested */
115 if( (slpe_errcode != SLP_OK) )
118 msg_Err( (vlc_object_t*)NULL,
119 "AttrCallback got an error %i with attribute %s",
126 p_playlist_item->psz_name = strdup(psz_attrlist); /* NULL is checked */
130 /*****************************************************************************
131 * SrvUrlCallback: adds an entry to the playlist
132 *****************************************************************************/
133 static SLPBoolean SrvUrlCallback( SLPHandle slph_slp,
134 const char * psz_srvurl,
136 SLPError slpe_errcode,
139 input_thread_t * p_input = (input_thread_t *)p_cookie;
140 playlist_t * p_playlist;
141 char psz_item[42] = ""; //"udp:@";
142 char * psz_s; /* to hold the uri of the stream */
144 SLPError slpe_result;
145 playlist_item_t * p_playlist_item;
147 /* our callback was only called to tell us there's nothing more to read */
148 if( slpe_errcode == SLP_LAST_CALL )
153 msg_Dbg(p_input,"URL: %s",psz_srvurl);
155 /* or there was a problem with getting the data we requested */
156 if( (slpe_errcode != SLP_OK) )
159 "SrvUrlCallback got an error %i with URL %s",
165 /* search the returned address after a double-slash */
166 psz_s = strstr( psz_srvurl, "//" );
169 msg_Err( (input_thread_t *)p_input,
170 "SrvUrlCallback got a strange string of your libslp" );
173 /* skip the slashes */
175 /* add udp:@ in front of the address */
176 psz_s = strncat( psz_item,
178 sizeof(psz_item) - strlen(psz_item) - 1 );
180 /* create a playlist item */
181 p_playlist_item = malloc( sizeof( playlist_item_t ) );
182 memset( p_playlist_item, 0, sizeof( playlist_item_t ) );
183 if( p_playlist_item == NULL )
185 msg_Err( p_input, "out of memory" );
189 p_playlist_item->psz_name = NULL;
190 p_playlist_item->psz_uri = strdup( psz_s );
191 p_playlist_item->i_duration = -1;
192 p_playlist_item->i_group = i_group;
193 p_playlist_item->b_enabled = VLC_TRUE;
195 /* search the description of the stream */
196 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
197 SLP_FALSE, /* synchronous ops */
198 &slph_slp3 ) == SLP_OK )
200 /* search all attributes */
201 slpe_result = SLPFindAttrs( slph_slp3,
203 config_GetPsz( p_input, "slp-scopelist" ),
204 config_GetPsz( p_input, "slp-attrids" ),
209 /* we're done, clean up */
210 SLPClose( slph_slp3 );
213 /* add a default name if we found no attribute */
214 if( p_playlist_item->psz_name == NULL )
216 p_playlist_item->psz_name = strdup( psz_s );
219 /* search the main playlist object */
220 p_playlist = vlc_object_find( (input_thread_t *)p_input,
223 if( p_playlist == NULL )
225 msg_Warn( (input_thread_t *)p_input,
226 "could not find playlist, not adding entries" );
230 playlist_AddItem( p_playlist,
234 vlc_object_release( (vlc_object_t *)p_playlist );
236 msg_Info( (input_thread_t *)p_input,
237 "added « %s » (lifetime %i) to playlist",
244 /*****************************************************************************
245 * SrvTypeCallback: searchs all servers of a certain type
246 *****************************************************************************/
247 static SLPBoolean SrvTypeCallback( SLPHandle slph_slp,
248 const char * psz_srvurl,
249 SLPError slpe_errcode,
252 input_thread_t * p_input = (input_thread_t *)p_cookie;
253 SLPError slpe_result;
258 msg_Dbg(p_input,"Services: %s",psz_srvurl);
259 /* our callback was only called to tell us there's nothing more to read */
260 if( slpe_errcode == SLP_LAST_CALL )
265 msg_Dbg(p_input,"Services: %s",psz_srvurl);
267 /* or there was a problem with getting the data we requested */
268 if( slpe_errcode != SLP_OK )
271 "SrvTypeCallback got an error %i with URL %s",
277 /* get a new handle to the library */
278 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
279 SLP_FALSE, /* synchronous ops */
280 &slph_slp2 ) == SLP_OK )
282 /* search for services */
285 if( *psz_srvurl == '\0') break;
287 if( !strncasecmp( psz_srvurl, "service:", 8 ) )
291 psz_eos = strchr( psz_srvurl, ',');
293 if(!strncasecmp(psz_eos+1,"service:",8)) break;
299 psz_service = strdup( psz_srvurl);
301 msg_Dbg(p_input,"Getting details for %s",psz_service);
303 slpe_result = SLPFindSrvs( slph_slp2,
305 config_GetPsz( p_input, "slp-scopelist" ),
306 config_GetPsz( p_input, "slp-filter" ),
311 psz_srvurl = psz_eos;
314 SLPClose( slph_slp2 );
316 if( slpe_result != SLP_OK )
319 "SLPFindSrvs error %i finding servers of type %s",
327 SLPClose( slph_slp2 );
332 /*****************************************************************************
333 * Open: initialize library for the access module
334 *****************************************************************************/
335 static int Open( vlc_object_t * p_this )
337 input_thread_t * p_input = (input_thread_t *)p_this;
338 SLPError slpe_result;
340 playlist_t * p_playlist;
341 playlist_group_t * p_group;
343 /* remove the "slp:" entry of the playlist */
344 p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
348 msg_Warn( p_input, "hey I can't find the main playlist, I need it" );
352 p_group = playlist_CreateGroup( p_playlist , "SLP" );
353 i_group = p_group->i_id;
354 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
355 vlc_object_release( (vlc_object_t *)p_playlist );
357 /* get a new handle to the library */
358 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
359 SLP_FALSE, /* synchronous ops */
360 &slph_slp ) == SLP_OK )
362 /* search all service types */
364 SLPFindSrvTypes( slph_slp,
365 config_GetPsz( p_input, "slp-namingauthority" ),
366 config_GetPsz( p_input, "slp-scopelist" ),
369 /* we're done, clean up */
370 SLPClose( slph_slp );
373 if( !p_input->psz_demux || !*p_input->psz_demux )
375 p_input->psz_demux = "demux_slp";
378 p_input->pf_read = Read;
379 p_input->pf_set_program = NULL;
380 p_input->pf_set_area = NULL;
381 p_input->pf_seek = NULL;
383 vlc_mutex_lock( &p_input->stream.stream_lock );
384 p_input->stream.b_pace_control = VLC_FALSE;
385 p_input->stream.b_seekable = VLC_FALSE;
386 p_input->stream.p_selected_area->i_tell = 0;
387 p_input->stream.p_selected_area->i_size = 0;
388 p_input->stream.i_method = INPUT_METHOD_SLP;
389 vlc_mutex_unlock( &p_input->stream.stream_lock );
395 /*****************************************************************************
396 * Close: close access
397 *****************************************************************************/
398 static void Close( vlc_object_t * p_this )
403 /*****************************************************************************
404 * Read: should fill but zeroes the buffer
405 *****************************************************************************/
406 static ssize_t Read ( input_thread_t *p_input, byte_t *p_buffer, size_t s )
408 memset( p_buffer, 0, s );
412 /*****************************************************************************
413 * Init: initialize demux
414 *****************************************************************************/
415 static int Init ( vlc_object_t *p_this )
417 input_thread_t *p_input = (input_thread_t *)p_this;
419 if( p_input->stream.i_method != INPUT_METHOD_SLP )
424 p_input->pf_demux = Demux;
425 p_input->pf_demux_control = demux_vaControlDefault;
426 p_input->pf_rewind = NULL;
431 /*****************************************************************************
432 * Demux: should demux but does nothing
433 *****************************************************************************/
434 static int Demux ( input_thread_t * p_input )
439 /*****************************************************************************
441 *****************************************************************************/
442 static void End ( vlc_object_t *p_this )