1 /*****************************************************************************
2 * services_discovery.c : Services discovery using lua scripts
3 *****************************************************************************
4 * Copyright (C) 2010 VideoLAN and AUTHORS
6 * Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
7 * RĂ©mi Duraffort <ivoire at videolan -dot- 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_services_discovery.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static void *Run( void * );
38 static int DoSearch( services_discovery_t *p_sd, const char *psz_query );
39 static int FillDescriptor( services_discovery_t *, services_discovery_descriptor_t * );
40 static int Control( services_discovery_t *p_sd, int i_command, va_list args );
42 static const char * const ppsz_sd_options[] = { "sd", "longname", NULL };
44 /*****************************************************************************
46 *****************************************************************************/
47 struct services_discovery_sys_t
60 static const luaL_Reg p_reg[] = { { NULL, NULL } };
62 /*****************************************************************************
63 * Open: initialize and create stuff
64 *****************************************************************************/
65 int Open_LuaSD( vlc_object_t *p_this )
67 services_discovery_t *p_sd = ( services_discovery_t * )p_this;
68 services_discovery_sys_t *p_sys;
72 if( !strcmp( p_sd->psz_name, "lua" ) )
74 // We want to load the module name "lua"
75 // This module can be used to load lua script not registered
76 // as builtin lua SD modules.
77 config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
78 psz_name = var_GetString( p_sd, "lua-sd" );
82 // We are loading a builtin lua sd module.
83 psz_name = strdup(p_sd->psz_name);
86 if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
92 p_sd->pf_control = Control;
93 p_sys->psz_filename = vlclua_find_file( p_this, "sd", psz_name );
94 if( !p_sys->psz_filename )
96 msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
105 msg_Err( p_sd, "Could not create new Lua State" );
108 vlclua_set_this( L, p_sd );
110 luaL_register( L, "vlc", p_reg );
117 luaopen_strings( L );
118 luaopen_variables( L );
120 luaopen_gettext( L );
125 if( vlclua_add_modules_path( p_sd, L, p_sys->psz_filename ) )
127 msg_Warn( p_sd, "Error while setting the module search path for %s",
128 p_sys->psz_filename );
131 if( luaL_dofile( L, p_sys->psz_filename ) )
133 msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
134 lua_tostring( L, lua_gettop( L ) ) );
139 vlc_mutex_init( &p_sys->lock );
140 vlc_cond_init( &p_sys->cond );
141 p_sys->b_exiting = false;
142 TAB_INIT( p_sys->i_query, p_sys->ppsz_query );
144 if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
146 TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
147 vlc_cond_destroy( &p_sys->cond );
148 vlc_mutex_destroy( &p_sys->lock );
156 free( p_sys->psz_filename );
161 /*****************************************************************************
163 *****************************************************************************/
164 void Close_LuaSD( vlc_object_t *p_this )
166 services_discovery_t *p_sd = ( services_discovery_t * )p_this;
167 services_discovery_sys_t *p_sys = p_sd->p_sys;
169 vlc_mutex_lock( &p_sys->lock );
170 p_sys->b_exiting = true;
171 vlc_mutex_unlock( &p_sys->lock );
173 vlc_cancel( p_sys->thread );
174 vlc_join( p_sys->thread, NULL );
176 for( int i = 0; i < p_sys->i_query; i++ )
177 free( p_sys->ppsz_query[i] );
178 TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
180 vlc_cond_destroy( &p_sys->cond );
181 vlc_mutex_destroy( &p_sys->lock );
182 free( p_sys->psz_filename );
183 lua_close( p_sys->L );
187 /*****************************************************************************
188 * Run: Thread entry-point
189 ****************************************************************************/
190 static void* Run( void *data )
192 services_discovery_t *p_sd = ( services_discovery_t * )data;
193 services_discovery_sys_t *p_sys = p_sd->p_sys;
194 lua_State *L = p_sys->L;
196 int cancel = vlc_savecancel();
198 lua_getglobal( L, "main" );
199 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
201 msg_Err( p_sd, "Error while running script %s, "
202 "function main(): %s", p_sys->psz_filename,
203 lua_tostring( L, lua_gettop( L ) ) );
205 vlc_restorecancel( cancel );
208 msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sys->psz_filename );
210 /* Force garbage collection, because the core will keep the SD
211 * open, but lua will never gc until lua_close(). */
212 lua_gc( L, LUA_GCCOLLECT, 0 );
214 vlc_restorecancel( cancel );
216 /* Main loop to handle search requests */
217 vlc_mutex_lock( &p_sys->lock );
218 mutex_cleanup_push( &p_sys->lock );
219 while( !p_sys->b_exiting )
221 /* Wait for a request */
222 while( !p_sys->i_query )
223 vlc_cond_wait( &p_sys->cond, &p_sys->lock );
225 /* Execute every query each one protected against cancelation */
226 cancel = vlc_savecancel();
227 while( !p_sys->b_exiting && p_sys->i_query )
229 char *psz_query = p_sys->ppsz_query[p_sys->i_query - 1];
230 REMOVE_ELEM( p_sys->ppsz_query, p_sys->i_query, p_sys->i_query - 1 );
232 vlc_mutex_unlock( &p_sys->lock );
233 DoSearch( p_sd, psz_query );
235 vlc_mutex_lock( &p_sys->lock );
237 /* Force garbage collection, because the core will keep the SD
238 * open, but lua will never gc until lua_close(). */
239 lua_gc( L, LUA_GCCOLLECT, 0 );
241 vlc_restorecancel( cancel );
248 /*****************************************************************************
249 * Control: services discrovery control
250 ****************************************************************************/
251 static int Control( services_discovery_t *p_sd, int i_command, va_list args )
253 services_discovery_sys_t *p_sys = p_sd->p_sys;
259 const char *psz_query = va_arg( args, const char * );
260 vlc_mutex_lock( &p_sys->lock );
261 TAB_APPEND( p_sys->i_query, p_sys->ppsz_query, strdup( psz_query ) );
262 vlc_cond_signal( &p_sys->cond );
263 vlc_mutex_unlock( &p_sys->lock );
267 case SD_CMD_DESCRIPTOR:
269 services_discovery_descriptor_t *p_desc = va_arg( args,
270 services_discovery_descriptor_t * );
271 return FillDescriptor( p_sd, p_desc );
278 /*****************************************************************************
279 * DoSearch: search for a given query
280 ****************************************************************************/
281 static int DoSearch( services_discovery_t *p_sd, const char *psz_query )
283 services_discovery_sys_t *p_sys = p_sd->p_sys;
284 lua_State *L = p_sys->L;
286 /* Lookup for the 'search' function */
287 lua_getglobal( L, "search" );
288 if( !lua_isfunction( L, lua_gettop( L ) ) )
290 msg_Err( p_sd, "The script '%s' does not define any 'search' function",
291 p_sys->psz_filename );
297 lua_pushstring( L, psz_query );
299 /* Call the 'search' function */
300 if( lua_pcall( L, 1, 0, 0 ) )
302 msg_Err( p_sd, "Error while running the script '%s': %s",
303 p_sys->psz_filename, lua_tostring( L, lua_gettop( L ) ) );
311 /** List of capabilities */
312 static const char *const ppsz_capabilities[] = {
317 /*****************************************************************************
318 * FillDescriptor: call the descriptor function and fill the structure
319 ****************************************************************************/
320 static int FillDescriptor( services_discovery_t *p_sd,
321 services_discovery_descriptor_t *p_desc )
323 services_discovery_sys_t *p_sys = p_sd->p_sys;
324 int i_ret = VLC_EGENERIC;
326 /* Create a new lua thread */
327 lua_State *L = luaL_newstate();
328 if( luaL_dofile( L, p_sys->psz_filename ) )
330 msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
331 lua_tostring( L, -1 ) );
335 /* Call the "descriptor" function */
336 lua_getglobal( L, "descriptor" );
337 if( !lua_isfunction( L, -1 ) || lua_pcall( L, 0, 1, 0 ) )
339 msg_Warn( p_sd, "Error getting the descriptor in '%s': %s",
340 p_sys->psz_filename, lua_tostring( L, -1 ) );
344 /* Get the different fields of the returned table */
345 lua_getfield( L, -1, "short_description" );
346 p_desc->psz_short_desc = luaL_strdupornull( L, -1 );
349 lua_getfield( L, -1, "icon" );
350 p_desc->psz_icon_url = luaL_strdupornull( L, -1 );
353 lua_getfield( L, -1, "url" );
354 p_desc->psz_url = luaL_strdupornull( L, -1 );
357 lua_getfield( L, -1, "capabilities" );
358 p_desc->i_capabilities = 0;
359 if( lua_istable( L, -1 ) )
361 /* List all table entries */
363 while( lua_next( L, -2 ) != 0 )
365 /* Key is at index -2 and value at index -1 */
366 const char *psz_cap = luaL_checkstring( L, -1 );
368 const char *psz_iter;
369 for( psz_iter = *ppsz_capabilities; psz_iter;
370 psz_iter = ppsz_capabilities[ ++i_cap ] )
372 if( !strcmp( psz_iter, psz_cap ) )
374 p_desc->i_capabilities |= 1 << i_cap;
379 msg_Warn( p_sd, "Services discovery capability '%s' unknown in "
380 "script '%s'", psz_cap, p_sys->psz_filename );