]> git.sesse.net Git - vlc/blob - modules/misc/lua/services_discovery.c
luasd: Force a garbage collect at the end of Run().
[vlc] / modules / misc / lua / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Services discovery using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2009 VideoLAN and AUTHORS
5  *
6  * Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_services_discovery.h>
29 #include <vlc_playlist.h>
30
31 #include "vlc.h"
32 #include "libs.h"
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static void *Run( void * );
38
39 static const char * const ppsz_sd_options[] = { "sd", "longname", NULL };
40
41 /*****************************************************************************
42  * Local structures
43  *****************************************************************************/
44 struct services_discovery_sys_t
45 {
46     lua_State *L;
47     char *psz_filename;
48     vlc_thread_t thread;
49 };
50 static const luaL_Reg p_reg[] = { { NULL, NULL } };
51
52 /*****************************************************************************
53  * Open: initialize and create stuff
54  *****************************************************************************/
55 int Open_LuaSD( vlc_object_t *p_this )
56 {
57     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
58     services_discovery_sys_t *p_sys;
59     lua_State *L = NULL;
60     char *psz_name = NULL;
61
62     if( !strcmp(p_sd->psz_name, "lua"))
63     {
64         // We want to load the module name "lua"
65         // This module can be used to load lua script not registered
66         // as builtin lua SD modules.
67         config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
68         psz_name = var_CreateGetString( p_sd, "lua-sd" );
69     }
70     else
71     {
72         // We are loading a builtin lua sd module.
73         psz_name = strdup(p_sd->psz_name);
74     }
75
76     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
77     {
78         free( psz_name );
79         return VLC_ENOMEM;
80     }
81     p_sd->p_sys = p_sys;
82     p_sys->psz_filename = vlclua_find_file( p_this, "sd", psz_name );
83     if( !p_sys->psz_filename )
84     {
85         msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
86                  psz_name );
87         free( psz_name );
88         goto error;
89     }
90     free( psz_name );
91     L = luaL_newstate();
92     if( !L )
93     {
94         msg_Err( p_sd, "Could not create new Lua State" );
95         goto error;
96     }
97     vlclua_set_this( L, p_sd );
98     luaL_openlibs( L );
99     luaL_register( L, "vlc", p_reg );
100     luaopen_input( L );
101     luaopen_msg( L );
102     luaopen_misc( L );
103     luaopen_net( L );
104     luaopen_object( L );
105     luaopen_playlist( L );
106     luaopen_sd( L );
107     luaopen_strings( L );
108     luaopen_variables( L );
109     luaopen_stream( L );
110     luaopen_gettext( L );
111     luaopen_xml( L );
112     luaopen_md5( L );
113     lua_pop( L, 1 );
114
115     if( vlclua_add_modules_path( p_sd, L, p_sys->psz_filename ) )
116     {
117         msg_Warn( p_sd, "Error while setting the module search path for %s",
118                   p_sys->psz_filename );
119         goto error;
120     }
121     if( luaL_dofile( L, p_sys->psz_filename ) )
122     {
123
124         msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
125                   lua_tostring( L, lua_gettop( L ) ) );
126         lua_pop( L, 1 );
127         goto error;
128     }
129     p_sys->L = L;
130     if( vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW) )
131     {
132         goto error;
133     }
134     return VLC_SUCCESS;
135 error:
136     if( L )
137         lua_close( L );
138     free( p_sys->psz_filename );
139     free( p_sys );
140     return VLC_EGENERIC;
141 }
142
143 /*****************************************************************************
144  * Close: cleanup
145  *****************************************************************************/
146 void Close_LuaSD( vlc_object_t *p_this )
147 {
148     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
149
150     vlc_join (p_sd->p_sys->thread, NULL);
151     free( p_sd->p_sys->psz_filename );
152     lua_close( p_sd->p_sys->L );
153     free( p_sd->p_sys );
154 }
155
156 /*****************************************************************************
157  * Run: Thread entry-point
158  ****************************************************************************/
159 static void* Run( void *data )
160 {
161     services_discovery_t *p_sd = ( services_discovery_t * )data;
162     services_discovery_sys_t *p_sys = p_sd->p_sys;
163     lua_State *L = p_sys->L;
164
165     lua_getglobal( L, "main" );
166     if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
167     {
168         msg_Err( p_sd, "Error while runing script %s, "
169                   "function main(): %s", p_sys->psz_filename,
170                   lua_tostring( L, lua_gettop( L ) ) );
171         lua_pop( L, 1 );
172         return NULL;
173     }
174     msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sys->psz_filename );
175
176     /* Force garbage collection, because the core will keep the SD
177      * open, but lua will never gc until lua_close(). */
178     lua_gc( L, LUA_GCCOLLECT, 0 );
179
180     return NULL;
181 }