]> git.sesse.net Git - vlc/blob - modules/lua/services_discovery.c
2d6d91aa7349af3ce000467950313e99afbe56f9
[vlc] / modules / lua / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Services discovery using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2010 VideoLAN and AUTHORS
5  *
6  * Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
7  *          RĂ©mi Duraffort <ivoire at videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_services_discovery.h>
30
31 #include "vlc.h"
32 #include "libs.h"
33
34 /*****************************************************************************
35  * Local prototypes
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 );
41
42 static const char * const ppsz_sd_options[] = { "sd", "longname", NULL };
43
44 /*****************************************************************************
45  * Local structures
46  *****************************************************************************/
47 struct services_discovery_sys_t
48 {
49     lua_State *L;
50     char *psz_filename;
51
52     vlc_thread_t thread;
53     vlc_mutex_t lock;
54     vlc_cond_t cond;
55     bool b_exiting;
56
57     char **ppsz_query;
58     int i_query;
59 };
60 static const luaL_Reg p_reg[] = { { NULL, NULL } };
61
62 /*****************************************************************************
63  * Open: initialize and create stuff
64  *****************************************************************************/
65 int Open_LuaSD( vlc_object_t *p_this )
66 {
67     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
68     services_discovery_sys_t *p_sys;
69     lua_State *L = NULL;
70     char *psz_name;
71
72     if( !strcmp( p_sd->psz_name, "lua" ) )
73     {
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" );
79     }
80     else
81     {
82         // We are loading a builtin lua sd module.
83         psz_name = strdup(p_sd->psz_name);
84     }
85
86     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
87     {
88         free( psz_name );
89         return VLC_ENOMEM;
90     }
91     p_sd->p_sys = p_sys;
92     p_sd->pf_control = Control;
93     p_sys->psz_filename = vlclua_find_file( "sd", psz_name );
94     if( !p_sys->psz_filename )
95     {
96         msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
97                  psz_name );
98         free( psz_name );
99         goto error;
100     }
101     free( psz_name );
102     L = luaL_newstate();
103     if( !L )
104     {
105         msg_Err( p_sd, "Could not create new Lua State" );
106         goto error;
107     }
108     vlclua_set_this( L, p_sd );
109     luaL_openlibs( L );
110     luaL_register( L, "vlc", p_reg );
111     luaopen_input( L );
112     luaopen_msg( L );
113     luaopen_net( L );
114     luaopen_object( L );
115     luaopen_sd( L );
116     luaopen_strings( L );
117     luaopen_variables( L );
118     luaopen_stream( L );
119     luaopen_gettext( L );
120     luaopen_xml( L );
121     lua_pop( L, 1 );
122
123     if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
124     {
125         msg_Warn( p_sd, "Error while setting the module search path for %s",
126                   p_sys->psz_filename );
127         goto error;
128     }
129     if( luaL_dofile( L, p_sys->psz_filename ) )
130     {
131         msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
132                   lua_tostring( L, lua_gettop( L ) ) );
133         lua_pop( L, 1 );
134         goto error;
135     }
136     p_sys->L = L;
137     vlc_mutex_init( &p_sys->lock );
138     vlc_cond_init( &p_sys->cond );
139     p_sys->b_exiting = false;
140     TAB_INIT( p_sys->i_query, p_sys->ppsz_query );
141
142     if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
143     {
144         TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
145         vlc_cond_destroy( &p_sys->cond );
146         vlc_mutex_destroy( &p_sys->lock );
147         goto error;
148     }
149     return VLC_SUCCESS;
150
151 error:
152     if( L )
153         lua_close( L );
154     free( p_sys->psz_filename );
155     free( p_sys );
156     return VLC_EGENERIC;
157 }
158
159 /*****************************************************************************
160  * Close: cleanup
161  *****************************************************************************/
162 void Close_LuaSD( vlc_object_t *p_this )
163 {
164     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
165     services_discovery_sys_t *p_sys = p_sd->p_sys;
166
167     vlc_mutex_lock( &p_sys->lock );
168     p_sys->b_exiting = true;
169     vlc_mutex_unlock( &p_sys->lock );
170
171     vlc_cancel( p_sys->thread );
172     vlc_join( p_sys->thread, NULL );
173
174     for( int i = 0; i < p_sys->i_query; i++ )
175         free( p_sys->ppsz_query[i] );
176     TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
177
178     vlc_cond_destroy( &p_sys->cond );
179     vlc_mutex_destroy( &p_sys->lock );
180     free( p_sys->psz_filename );
181     lua_close( p_sys->L );
182     free( p_sys );
183 }
184
185 /*****************************************************************************
186  * Run: Thread entry-point
187  ****************************************************************************/
188 static void* Run( void *data )
189 {
190     services_discovery_t *p_sd = ( services_discovery_t * )data;
191     services_discovery_sys_t *p_sys = p_sd->p_sys;
192     lua_State *L = p_sys->L;
193
194     int cancel = vlc_savecancel();
195
196     lua_getglobal( L, "main" );
197     if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
198     {
199         msg_Err( p_sd, "Error while running script %s, "
200                   "function main(): %s", p_sys->psz_filename,
201                   lua_tostring( L, lua_gettop( L ) ) );
202         lua_pop( L, 1 );
203         vlc_restorecancel( cancel );
204         return NULL;
205     }
206     msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sys->psz_filename );
207
208     /* Force garbage collection, because the core will keep the SD
209      * open, but lua will never gc until lua_close(). */
210     lua_gc( L, LUA_GCCOLLECT, 0 );
211
212     vlc_restorecancel( cancel );
213
214     /* Main loop to handle search requests */
215     vlc_mutex_lock( &p_sys->lock );
216     mutex_cleanup_push( &p_sys->lock );
217     while( !p_sys->b_exiting )
218     {
219         /* Wait for a request */
220         while( !p_sys->i_query )
221             vlc_cond_wait( &p_sys->cond, &p_sys->lock );
222
223         /* Execute every query each one protected against cancelation */
224         cancel = vlc_savecancel();
225         while( !p_sys->b_exiting && p_sys->i_query )
226         {
227             char *psz_query = p_sys->ppsz_query[p_sys->i_query - 1];
228             REMOVE_ELEM( p_sys->ppsz_query, p_sys->i_query, p_sys->i_query - 1 );
229
230             vlc_mutex_unlock( &p_sys->lock );
231             DoSearch( p_sd, psz_query );
232             free( psz_query );
233             vlc_mutex_lock( &p_sys->lock );
234         }
235         /* Force garbage collection, because the core will keep the SD
236          * open, but lua will never gc until lua_close(). */
237         lua_gc( L, LUA_GCCOLLECT, 0 );
238
239         vlc_restorecancel( cancel );
240     }
241     vlc_cleanup_run();
242
243     return NULL;
244 }
245
246 /*****************************************************************************
247  * Control: services discrovery control
248  ****************************************************************************/
249 static int Control( services_discovery_t *p_sd, int i_command, va_list args )
250 {
251     services_discovery_sys_t *p_sys = p_sd->p_sys;
252
253     switch( i_command )
254     {
255     case SD_CMD_SEARCH:
256     {
257         const char *psz_query = va_arg( args, const char * );
258         vlc_mutex_lock( &p_sys->lock );
259         TAB_APPEND( p_sys->i_query, p_sys->ppsz_query, strdup( psz_query ) );
260         vlc_cond_signal( &p_sys->cond );
261         vlc_mutex_unlock( &p_sys->lock );
262         break;
263     }
264
265     case SD_CMD_DESCRIPTOR:
266     {
267         services_discovery_descriptor_t *p_desc = va_arg( args,
268                                 services_discovery_descriptor_t * );
269         return FillDescriptor( p_sd, p_desc );
270     }
271     }
272
273     return VLC_SUCCESS;
274 }
275
276 /*****************************************************************************
277  * DoSearch: search for a given query
278  ****************************************************************************/
279 static int DoSearch( services_discovery_t *p_sd, const char *psz_query )
280 {
281     services_discovery_sys_t *p_sys = p_sd->p_sys;
282     lua_State *L = p_sys->L;
283
284     /* Lookup for the 'search' function */
285     lua_getglobal( L, "search" );
286     if( !lua_isfunction( L, lua_gettop( L ) ) )
287     {
288         msg_Err( p_sd, "The script '%s' does not define any 'search' function",
289                  p_sys->psz_filename );
290         lua_pop( L, 1 );
291         return VLC_EGENERIC;
292     }
293
294     /* Push the query */
295     lua_pushstring( L, psz_query );
296
297     /* Call the 'search' function */
298     if( lua_pcall( L, 1, 0, 0 ) )
299     {
300         msg_Err( p_sd, "Error while running the script '%s': %s",
301                  p_sys->psz_filename, lua_tostring( L, lua_gettop( L ) ) );
302         lua_pop( L, 1 );
303         return VLC_EGENERIC;
304     }
305
306     return VLC_SUCCESS;
307 }
308
309 /** List of capabilities */
310 static const char *const ppsz_capabilities[] = {
311     "search",
312     NULL
313 };
314
315 /*****************************************************************************
316  * FillDescriptor: call the descriptor function and fill the structure
317  ****************************************************************************/
318 static int FillDescriptor( services_discovery_t *p_sd,
319                            services_discovery_descriptor_t *p_desc )
320 {
321     services_discovery_sys_t *p_sys = p_sd->p_sys;
322     int i_ret = VLC_EGENERIC;
323
324     /* Create a new lua thread */
325     lua_State *L = luaL_newstate();
326     if( luaL_dofile( L, p_sys->psz_filename ) )
327     {
328         msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
329                  lua_tostring( L, -1 ) );
330         goto end;
331     }
332
333     /* Call the "descriptor" function */
334     lua_getglobal( L, "descriptor" );
335     if( !lua_isfunction( L, -1 ) || lua_pcall( L, 0, 1, 0 ) )
336     {
337         msg_Warn( p_sd, "Error getting the descriptor in '%s': %s",
338                   p_sys->psz_filename, lua_tostring( L, -1 ) );
339         goto end;
340     }
341
342     /* Get the different fields of the returned table */
343     lua_getfield( L, -1, "short_description" );
344     p_desc->psz_short_desc = luaL_strdupornull( L, -1 );
345     lua_pop( L, 1 );
346
347     lua_getfield( L, -1, "icon" );
348     p_desc->psz_icon_url = luaL_strdupornull( L, -1 );
349     lua_pop( L, 1 );
350
351     lua_getfield( L, -1, "url" );
352     p_desc->psz_url = luaL_strdupornull( L, -1 );
353     lua_pop( L, 1 );
354
355     lua_getfield( L, -1, "capabilities" );
356     p_desc->i_capabilities = 0;
357     if( lua_istable( L, -1 ) )
358     {
359         /* List all table entries */
360         lua_pushnil( L );
361         while( lua_next( L, -2 ) != 0 )
362         {
363             /* Key is at index -2 and value at index -1 */
364             const char *psz_cap = luaL_checkstring( L, -1 );
365             int i_cap = 0;
366             const char *psz_iter;
367             for( psz_iter = *ppsz_capabilities; psz_iter;
368                  psz_iter = ppsz_capabilities[ ++i_cap ] )
369             {
370                 if( !strcmp( psz_iter, psz_cap ) )
371                 {
372                     p_desc->i_capabilities |= 1 << i_cap;
373                     break;
374                 }
375             }
376             if( !psz_iter )
377                 msg_Warn( p_sd, "Services discovery capability '%s' unknown in "
378                                 "script '%s'", psz_cap, p_sys->psz_filename );
379         }
380     }
381     lua_pop( L, 1 );
382     i_ret = VLC_SUCCESS;
383
384 end:
385     lua_close( L );
386     return i_ret;
387
388 }