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
|
/*****************************************************************************
* services_discovery.c : Services discovery using lua scripts
*****************************************************************************
* Copyright (C) 2010 VideoLAN and AUTHORS
*
* Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
* Rémi Duraffort <ivoire at videolan -dot- org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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 <vlc_common.h>
#include <vlc_services_discovery.h>
#include "vlc.h"
#include "libs.h"
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static void *Run( void * );
static int DoSearch( services_discovery_t *p_sd, const char *psz_query );
static int FillDescriptor( services_discovery_t *, services_discovery_descriptor_t * );
static int Control( services_discovery_t *p_sd, int i_command, va_list args );
static const char * const ppsz_sd_options[] = { "sd", "longname", NULL };
/*****************************************************************************
* Local structures
*****************************************************************************/
struct services_discovery_sys_t
{
lua_State *L;
char *psz_filename;
vlc_thread_t thread;
vlc_mutex_t lock;
vlc_cond_t cond;
char **ppsz_query;
int i_query;
};
static const luaL_Reg p_reg[] = { { NULL, NULL } };
/*****************************************************************************
* Open: initialize and create stuff
*****************************************************************************/
int Open_LuaSD( vlc_object_t *p_this )
{
services_discovery_t *p_sd = ( services_discovery_t * )p_this;
services_discovery_sys_t *p_sys;
lua_State *L = NULL;
char *psz_name;
if( !strcmp( p_sd->psz_name, "lua" ) )
{
// We want to load the module name "lua"
// This module can be used to load lua script not registered
// as builtin lua SD modules.
config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
psz_name = var_GetString( p_sd, "lua-sd" );
}
else
{
// We are loading a builtin lua sd module.
psz_name = strdup(p_sd->psz_name);
}
if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
{
free( psz_name );
return VLC_ENOMEM;
}
p_sd->p_sys = p_sys;
p_sd->pf_control = Control;
p_sys->psz_filename = vlclua_find_file( "sd", psz_name );
if( !p_sys->psz_filename )
{
msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
psz_name );
free( psz_name );
goto error;
}
free( psz_name );
L = luaL_newstate();
if( !L )
{
msg_Err( p_sd, "Could not create new Lua State" );
goto error;
}
vlclua_set_this( L, p_sd );
luaL_openlibs( L );
luaL_register( L, "vlc", p_reg );
luaopen_input( L );
luaopen_msg( L );
luaopen_object( L );
luaopen_sd_sd( L );
luaopen_strings( L );
luaopen_variables( L );
luaopen_stream( L );
luaopen_gettext( L );
luaopen_xml( L );
lua_pop( L, 1 );
if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
{
msg_Warn( p_sd, "Error while setting the module search path for %s",
p_sys->psz_filename );
goto error;
}
if( luaL_dofile( L, p_sys->psz_filename ) )
{
msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
lua_tostring( L, lua_gettop( L ) ) );
lua_pop( L, 1 );
goto error;
}
p_sys->L = L;
vlc_mutex_init( &p_sys->lock );
vlc_cond_init( &p_sys->cond );
TAB_INIT( p_sys->i_query, p_sys->ppsz_query );
if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
{
TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
vlc_cond_destroy( &p_sys->cond );
vlc_mutex_destroy( &p_sys->lock );
goto error;
}
return VLC_SUCCESS;
error:
if( L )
lua_close( L );
free( p_sys->psz_filename );
free( p_sys );
return VLC_EGENERIC;
}
/*****************************************************************************
* Close: cleanup
*****************************************************************************/
void Close_LuaSD( vlc_object_t *p_this )
{
services_discovery_t *p_sd = ( services_discovery_t * )p_this;
services_discovery_sys_t *p_sys = p_sd->p_sys;
vlc_cancel( p_sys->thread );
vlc_join( p_sys->thread, NULL );
for( int i = 0; i < p_sys->i_query; i++ )
free( p_sys->ppsz_query[i] );
TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
vlc_cond_destroy( &p_sys->cond );
vlc_mutex_destroy( &p_sys->lock );
free( p_sys->psz_filename );
lua_close( p_sys->L );
free( p_sys );
}
/*****************************************************************************
* Run: Thread entry-point
****************************************************************************/
static void* Run( void *data )
{
services_discovery_t *p_sd = ( services_discovery_t * )data;
services_discovery_sys_t *p_sys = p_sd->p_sys;
lua_State *L = p_sys->L;
int cancel = vlc_savecancel();
lua_getglobal( L, "main" );
if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
{
msg_Err( p_sd, "Error while running script %s, "
"function main(): %s", p_sys->psz_filename,
lua_tostring( L, lua_gettop( L ) ) );
lua_pop( L, 1 );
vlc_restorecancel( cancel );
return NULL;
}
msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sys->psz_filename );
/* Force garbage collection, because the core will keep the SD
* open, but lua will never gc until lua_close(). */
lua_gc( L, LUA_GCCOLLECT, 0 );
vlc_restorecancel( cancel );
/* Main loop to handle search requests */
vlc_mutex_lock( &p_sys->lock );
mutex_cleanup_push( &p_sys->lock );
for( ;; )
{
/* Wait for a request */
if( !p_sys->i_query )
{
vlc_cond_wait( &p_sys->cond, &p_sys->lock );
continue;
}
/* Execute one query (protected against cancellation) */
char *psz_query = p_sys->ppsz_query[p_sys->i_query - 1];
REMOVE_ELEM( p_sys->ppsz_query, p_sys->i_query, p_sys->i_query - 1 );
vlc_mutex_unlock( &p_sys->lock );
cancel = vlc_savecancel();
DoSearch( p_sd, psz_query );
free( psz_query );
/* Force garbage collection, because the core will keep the SD
* open, but lua will never gc until lua_close(). */
lua_gc( L, LUA_GCCOLLECT, 0 );
vlc_restorecancel( cancel );
vlc_mutex_lock( &p_sys->lock );
}
vlc_cleanup_run();
return NULL;
}
/*****************************************************************************
* Control: services discrovery control
****************************************************************************/
static int Control( services_discovery_t *p_sd, int i_command, va_list args )
{
services_discovery_sys_t *p_sys = p_sd->p_sys;
switch( i_command )
{
case SD_CMD_SEARCH:
{
const char *psz_query = va_arg( args, const char * );
vlc_mutex_lock( &p_sys->lock );
TAB_APPEND( p_sys->i_query, p_sys->ppsz_query, strdup( psz_query ) );
vlc_cond_signal( &p_sys->cond );
vlc_mutex_unlock( &p_sys->lock );
break;
}
case SD_CMD_DESCRIPTOR:
{
services_discovery_descriptor_t *p_desc = va_arg( args,
services_discovery_descriptor_t * );
return FillDescriptor( p_sd, p_desc );
}
}
return VLC_SUCCESS;
}
/*****************************************************************************
* DoSearch: search for a given query
****************************************************************************/
static int DoSearch( services_discovery_t *p_sd, const char *psz_query )
{
services_discovery_sys_t *p_sys = p_sd->p_sys;
lua_State *L = p_sys->L;
/* Lookup for the 'search' function */
lua_getglobal( L, "search" );
if( !lua_isfunction( L, lua_gettop( L ) ) )
{
msg_Err( p_sd, "The script '%s' does not define any 'search' function",
p_sys->psz_filename );
lua_pop( L, 1 );
return VLC_EGENERIC;
}
/* Push the query */
lua_pushstring( L, psz_query );
/* Call the 'search' function */
if( lua_pcall( L, 1, 0, 0 ) )
{
msg_Err( p_sd, "Error while running the script '%s': %s",
p_sys->psz_filename, lua_tostring( L, lua_gettop( L ) ) );
lua_pop( L, 1 );
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
/** List of capabilities */
static const char *const ppsz_capabilities[] = {
"search",
NULL
};
/*****************************************************************************
* FillDescriptor: call the descriptor function and fill the structure
****************************************************************************/
static int FillDescriptor( services_discovery_t *p_sd,
services_discovery_descriptor_t *p_desc )
{
services_discovery_sys_t *p_sys = p_sd->p_sys;
int i_ret = VLC_EGENERIC;
/* Create a new lua thread */
lua_State *L = luaL_newstate();
if( luaL_dofile( L, p_sys->psz_filename ) )
{
msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
lua_tostring( L, -1 ) );
goto end;
}
/* Call the "descriptor" function */
lua_getglobal( L, "descriptor" );
if( !lua_isfunction( L, -1 ) || lua_pcall( L, 0, 1, 0 ) )
{
msg_Warn( p_sd, "Error getting the descriptor in '%s': %s",
p_sys->psz_filename, lua_tostring( L, -1 ) );
goto end;
}
/* Get the different fields of the returned table */
lua_getfield( L, -1, "short_description" );
p_desc->psz_short_desc = luaL_strdupornull( L, -1 );
lua_pop( L, 1 );
lua_getfield( L, -1, "icon" );
p_desc->psz_icon_url = luaL_strdupornull( L, -1 );
lua_pop( L, 1 );
lua_getfield( L, -1, "url" );
p_desc->psz_url = luaL_strdupornull( L, -1 );
lua_pop( L, 1 );
lua_getfield( L, -1, "capabilities" );
p_desc->i_capabilities = 0;
if( lua_istable( L, -1 ) )
{
/* List all table entries */
lua_pushnil( L );
while( lua_next( L, -2 ) != 0 )
{
/* Key is at index -2 and value at index -1 */
const char *psz_cap = luaL_checkstring( L, -1 );
int i_cap = 0;
const char *psz_iter;
for( psz_iter = *ppsz_capabilities; psz_iter;
psz_iter = ppsz_capabilities[ ++i_cap ] )
{
if( !strcmp( psz_iter, psz_cap ) )
{
p_desc->i_capabilities |= 1 << i_cap;
break;
}
}
lua_pop( L, 1 );
if( !psz_iter )
msg_Warn( p_sd, "Services discovery capability '%s' unknown in "
"script '%s'", psz_cap, p_sys->psz_filename );
}
}
lua_pop( L, 1 );
i_ret = VLC_SUCCESS;
end:
lua_close( L );
return i_ret;
}
|