1 /*****************************************************************************
2 * slp.c: SLP access plugin
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: slp.c,v 1.8 2003/02/20 01:52:45 sigmunau 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 * );
47 /*****************************************************************************
49 *****************************************************************************/
51 #define SRVTYPE_TEXT "SLP service type"
52 #define SRVTYPE_LONGTEXT "The service type string for SLP queries, " \
53 "including the authority string (if any) for the " \
54 "request. May not be empty"
55 #define ATTRIDS_TEXT "SLP attribute identifiers"
56 #define ATTRIDS_LONGTEXT "This string is a comma separated list of " \
57 "attribute identifiers to search for a playlist "\
58 "title or empty to use all attributes"
59 #define SCOPELIST_TEXT "SLP scopes list"
60 #define SCOPELIST_LONGTEXT "This string is a comma separated list of scope " \
61 "names or empty if you want to use the default " \
62 "scopes; it is used in all SLP queries"
63 #define NAMINGAUTHORITY_TEXT "SLP naming authority"
64 #define NAMINGAUTHORITY_LONGTEXT "This string is a list of naming " \
65 "authorities to search. Use \"*\" for all " \
66 "and the empty string for the default of " \
68 #define FILTER_TEXT "SLP LDAP filter"
69 #define FILTER_LONGTEXT "This is a query formulated of attribute pattern " \
70 "matching expressions in the form of an LDAPv3 " \
71 "search filter or empty for all answers"
72 #define LANG_TEXT "language requested in SLP requests"
73 #define LANG_LONGTEXT "RFC 1766 Language Tag for the natural language " \
74 "locale of requests, leave empty to use the " \
75 "default locale; it is used in all SLP queries"
78 set_description( _("SLP access module") );
79 add_category_hint( N_("slp"), NULL, VLC_TRUE );
80 add_string( "slp-attrids", "", NULL, ATTRIDS_TEXT, ATTRIDS_LONGTEXT, VLC_TRUE );
81 add_string( "slp-scopelist", "", NULL, SCOPELIST_TEXT,
82 SCOPELIST_LONGTEXT, VLC_TRUE );
83 add_string( "slp-namingauthority", "*", NULL, NAMINGAUTHORITY_TEXT,
84 NAMINGAUTHORITY_LONGTEXT, VLC_TRUE );
85 add_string( "slp-filter", "", NULL, FILTER_TEXT, FILTER_LONGTEXT, VLC_TRUE );
86 add_string( "slp-lang", "", NULL, LANG_TEXT, LANG_LONGTEXT, VLC_TRUE );
88 set_capability( "access", 0 );
89 set_callbacks( Open, Close );
91 add_shortcut( "demux_slp" );
92 set_capability( "demux", 0 );
93 set_callbacks( Init, End );
96 /*****************************************************************************
97 * AttrCallback: updates the description of a playlist item
98 *****************************************************************************/
99 static SLPBoolean AttrCallback( SLPHandle slph_slp,
100 const char * psz_attrlist,
101 SLPError slpe_errcode,
104 playlist_item_t * p_playlist_item = (playlist_item_t *)p_cookie;
106 /* our callback was only called to tell us there's nothing more to read */
107 if( slpe_errcode == SLP_LAST_CALL )
112 /* or there was a problem with getting the data we requested */
113 if( (slpe_errcode != SLP_OK) )
115 /* msg_Err( (vlc_object_t*)NULL,
116 "AttrCallback got an error %i with attribute %s",
122 p_playlist_item->psz_name = strdup(psz_attrlist); /* NULL is checked */
126 /*****************************************************************************
127 * SrvUrlCallback: adds an entry to the playlist
128 *****************************************************************************/
129 static SLPBoolean SrvUrlCallback( SLPHandle slph_slp,
130 const char * psz_srvurl,
132 SLPError slpe_errcode,
135 input_thread_t * p_input = (input_thread_t *)p_cookie;
136 playlist_t * p_playlist;
137 char psz_item[42] = "udp:@";
138 char * psz_s; /* to hold the uri of the stream */
140 SLPError slpe_result;
141 playlist_item_t * p_playlist_item;
143 /* our callback was only called to tell us there's nothing more to read */
144 if( slpe_errcode == SLP_LAST_CALL )
149 /* or there was a problem with getting the data we requested */
150 if( (slpe_errcode != SLP_OK) )
153 "SrvUrlCallback got an error %i with URL %s",
159 /* search the returned address after a double-slash */
160 psz_s = strstr( psz_srvurl, "//" );
163 msg_Err( (input_thread_t *)p_input,
164 "SrvUrlCallback got a strange string of your libslp" );
167 /* skip the slashes */
169 /* add udp:@ in front of the address */
170 psz_s = strncat( psz_item,
172 sizeof(psz_item) - strlen(psz_item) - 1 );
174 /* create a playlist item */
175 p_playlist_item = malloc( sizeof( playlist_item_t ) );
176 if( p_playlist_item == NULL )
178 msg_Err( p_input, "out of memory" );
182 p_playlist_item->psz_name = NULL;
183 p_playlist_item->psz_uri = strdup( psz_s );
184 p_playlist_item->i_type = 0;
185 p_playlist_item->i_status = 0;
186 p_playlist_item->b_autodeletion = VLC_FALSE;
188 /* search the description of the stream */
189 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
190 SLP_FALSE, /* synchronous ops */
191 &slph_slp3 ) == SLP_OK )
193 /* search all attributes */
194 slpe_result = SLPFindAttrs( slph_slp3,
196 config_GetPsz( p_input, "slp-scopelist" ),
197 config_GetPsz( p_input, "slp-attrids" ),
202 /* we're done, clean up */
203 SLPClose( slph_slp3 );
206 /* add a default name if we found no attribute */
207 if( p_playlist_item->psz_name == NULL )
209 p_playlist_item->psz_name = strdup( psz_s );
212 /* search the main playlist object */
213 p_playlist = vlc_object_find( (input_thread_t *)p_input,
216 if( p_playlist == NULL )
218 msg_Warn( (input_thread_t *)p_input,
219 "could not find playlist, not adding entries" );
223 playlist_AddItem( p_playlist,
227 vlc_object_release( (vlc_object_t *)p_playlist );
229 msg_Info( (input_thread_t *)p_input,
230 "added « %s » (lifetime %i) to playlist",
237 /*****************************************************************************
238 * SrvTypeCallback: searchs all servers of a certain type
239 *****************************************************************************/
240 static SLPBoolean SrvTypeCallback( SLPHandle slph_slp,
241 const char * psz_srvurl,
242 SLPError slpe_errcode,
245 input_thread_t * p_input = (input_thread_t *)p_cookie;
246 SLPError slpe_result;
249 /* our callback was only called to tell us there's nothing more to read */
250 if( slpe_errcode == SLP_LAST_CALL )
255 /* or there was a problem with getting the data we requested */
256 if( slpe_errcode != SLP_OK )
259 "SrvTypeCallback got an error %i with URL %s",
265 /* get a new handle to the library */
266 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
267 SLP_FALSE, /* synchronous ops */
268 &slph_slp2 ) == SLP_OK )
270 /* search for services */
271 slpe_result = SLPFindSrvs( slph_slp2,
273 config_GetPsz( p_input, "slp-scopelist" ),
274 config_GetPsz( p_input, "slp-filter" ),
278 SLPClose( slph_slp2 );
280 if( slpe_result != SLP_OK )
283 "SLPFindSrvs error %i finding servers of type %s",
292 /*****************************************************************************
293 * Open: initialize library for the access module
294 *****************************************************************************/
295 static int Open( vlc_object_t * p_this )
297 input_thread_t * p_input = (input_thread_t *)p_this;
298 SLPError slpe_result;
300 playlist_t * p_playlist;
302 /* remove the "slp:" entry of the playlist */
303 p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
307 msg_Warn( p_input, "hey I can't find the main playlist, I need it" );
311 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
312 vlc_object_release( (vlc_object_t *)p_playlist );
314 /* get a new handle to the library */
315 if( SLPOpen( config_GetPsz( p_input, "slp-lang" ),
316 SLP_FALSE, /* synchronous ops */
317 &slph_slp ) == SLP_OK )
319 /* search all service types */
321 SLPFindSrvTypes( slph_slp,
322 config_GetPsz( p_input, "slp-namingauthority" ),
323 config_GetPsz( p_input, "slp-scopelist" ),
326 /* we're done, clean up */
327 SLPClose( slph_slp );
330 if( !p_input->psz_demux || !*p_input->psz_demux )
332 p_input->psz_demux = "demux_slp";
335 p_input->pf_read = Read;
336 p_input->pf_set_program = NULL;
337 p_input->pf_set_area = NULL;
338 p_input->pf_seek = NULL;
340 vlc_mutex_lock( &p_input->stream.stream_lock );
341 p_input->stream.b_pace_control = VLC_FALSE;
342 p_input->stream.b_seekable = VLC_FALSE;
343 p_input->stream.b_connected = VLC_TRUE;
344 p_input->stream.p_selected_area->i_tell = 0;
345 p_input->stream.p_selected_area->i_size = 0;
346 p_input->stream.i_method = INPUT_METHOD_SLP;
347 vlc_mutex_unlock( &p_input->stream.stream_lock );
353 /*****************************************************************************
354 * Close: close access
355 *****************************************************************************/
356 static void Close( vlc_object_t * p_this )
361 /*****************************************************************************
362 * Read: should fill but zeroes the buffer
363 *****************************************************************************/
364 static ssize_t Read ( input_thread_t *p_input, byte_t *p_buffer, size_t s )
366 memset( p_buffer, 0, s );
370 /*****************************************************************************
371 * Init: initialize demux
372 *****************************************************************************/
373 static int Init ( vlc_object_t *p_this )
375 input_thread_t *p_input = (input_thread_t *)p_this;
377 if( p_input->stream.i_method != INPUT_METHOD_SLP )
382 p_input->pf_demux = Demux;
383 p_input->pf_rewind = NULL;
388 /*****************************************************************************
389 * Demux: should demux but does nothing
390 *****************************************************************************/
391 static int Demux ( input_thread_t * p_input )
396 /*****************************************************************************
398 *****************************************************************************/
399 static void End ( vlc_object_t *p_this )