]> git.sesse.net Git - vlc/blob - modules/lua/demux.c
linsys_hdsdi: remove redundant nul terminator
[vlc] / modules / lua / demux.c
1 /*****************************************************************************
2  * demux.c :  Lua playlist demux module
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod 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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32
33 #include "vlc.h"
34 #include "libs.h"
35
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int Demux( demux_t *p_demux );
41 static int Control( demux_t *p_demux, int i_query, va_list args );
42
43 /*****************************************************************************
44  * Demux specific functions
45  *****************************************************************************/
46 struct demux_sys_t
47 {
48     lua_State *L;
49     char *psz_filename;
50 };
51
52 static int vlclua_demux_peek( lua_State *L )
53 {
54     demux_t *p_demux = (demux_t *)vlclua_get_this( L );
55     int n = luaL_checkint( L, 1 );
56     const uint8_t *p_peek;
57
58     int i_peek = stream_Peek( p_demux->s, &p_peek, n );
59     if( i_peek > 0 )
60         lua_pushlstring( L, (const char *)p_peek, i_peek );
61     else
62         lua_pushnil( L );
63     return 1;
64 }
65
66 static int vlclua_demux_read( lua_State *L )
67 {
68     demux_t *p_demux = (demux_t *)vlclua_get_this( L );
69     const uint8_t *p_read;
70     int n = luaL_checkint( L, 1 );
71     int i_read = stream_Peek( p_demux->s, &p_read, n );
72
73     if( i_read > 0 )
74     {
75         lua_pushlstring( L, (const char *)p_read, i_read );
76         int i_seek = stream_Read( p_demux->s, NULL, i_read );
77         assert( i_read == i_seek );
78     }
79     else
80         lua_pushnil( L );
81
82     return 1;
83 }
84
85 static int vlclua_demux_readline( lua_State *L )
86 {
87     demux_t *p_demux = (demux_t *)vlclua_get_this( L );
88     char *psz_line = stream_ReadLine( p_demux->s );
89     if( psz_line )
90     {
91         lua_pushstring( L, psz_line );
92         free( psz_line );
93     }
94     else
95     {
96         lua_pushnil( L );
97     }
98     return 1;
99 }
100
101 /*****************************************************************************
102  *
103  *****************************************************************************/
104 /* Functions to register */
105 static const luaL_Reg p_reg[] =
106 {
107     { "peek", vlclua_demux_peek },
108     { NULL, NULL }
109 };
110
111 /* Functions to register for parse() function call only */
112 static const luaL_Reg p_reg_parse[] =
113 {
114     { "read", vlclua_demux_read },
115     { "readline", vlclua_demux_readline },
116     { NULL, NULL }
117 };
118
119 /*****************************************************************************
120  * Called through lua_scripts_batch_execute to call 'probe' on
121  * the script pointed by psz_filename.
122  *****************************************************************************/
123 static int probe_luascript( vlc_object_t *p_this, const char * psz_filename,
124                             const luabatch_context_t *p_context )
125 {
126     VLC_UNUSED(p_context);
127     demux_t * p_demux = (demux_t *)p_this;
128
129     p_demux->p_sys->psz_filename = strdup(psz_filename);
130
131     /* Initialise Lua state structure */
132     lua_State *L = luaL_newstate();
133     if( !L )
134     {
135         msg_Err( p_demux, "Could not create new Lua State" );
136         goto error;
137     }
138     p_demux->p_sys->L = L;
139
140     /* Load Lua libraries */
141     luaL_openlibs( L ); /* FIXME: Don't open all the libs? */
142
143     vlclua_set_this( L, p_demux );
144     luaL_register( L, "vlc", p_reg );
145     luaopen_msg( L );
146     luaopen_strings( L );
147     luaopen_stream( L );
148     luaopen_variables( L );
149     luaopen_xml( L );
150     lua_pushstring( L, p_demux->psz_location );
151     lua_setfield( L, -2, "path" );
152     lua_pushstring( L, p_demux->psz_access );
153     lua_setfield( L, -2, "access" );
154
155     lua_pop( L, 1 );
156
157     /* Setup the module search path */
158     if( vlclua_add_modules_path( L, psz_filename ) )
159     {
160         msg_Warn( p_demux, "Error while setting the module search path for %s",
161                   psz_filename );
162         goto error;
163     }
164
165     /* Load and run the script(s) */
166     if( luaL_dofile( L, psz_filename ) )
167     {
168         msg_Warn( p_demux, "Error loading script %s: %s", psz_filename,
169                   lua_tostring( L, lua_gettop( L ) ) );
170         goto error;
171     }
172
173     lua_getglobal( L, "probe" );
174
175     if( !lua_isfunction( L, -1 ) )
176     {
177         msg_Warn( p_demux, "Error while running script %s, "
178                   "function probe() not found", psz_filename );
179         goto error;
180     }
181
182     if( lua_pcall( L, 0, 1, 0 ) )
183     {
184         msg_Warn( p_demux, "Error while running script %s, "
185                   "function probe(): %s", psz_filename,
186                   lua_tostring( L, lua_gettop( L ) ) );
187         goto error;
188     }
189
190     if( lua_gettop( L ) )
191     {
192         if( lua_toboolean( L, 1 ) )
193         {
194             msg_Dbg( p_demux, "Lua playlist script %s's "
195                      "probe() function was successful", psz_filename );
196             lua_pop( L, 1 );
197             return VLC_SUCCESS;
198         }
199     }
200
201 error:
202     lua_pop( L, 1 );
203     lua_close( p_demux->p_sys->L );
204     p_demux->p_sys->L = NULL;
205     FREENULL( p_demux->p_sys->psz_filename );
206     return VLC_EGENERIC;
207 }
208
209 /*****************************************************************************
210  * Import_LuaPlaylist: main import function
211  *****************************************************************************/
212 int Import_LuaPlaylist( vlc_object_t *p_this )
213 {
214     demux_t *p_demux = (demux_t *)p_this;
215     int ret;
216
217     p_demux->p_sys = calloc( 1, sizeof( demux_sys_t ) );
218     if( !p_demux->p_sys )
219         return VLC_ENOMEM;
220
221     p_demux->pf_control = Control;
222     p_demux->pf_demux = Demux;
223
224     ret = vlclua_scripts_batch_execute( p_this, "playlist",
225                                         &probe_luascript, NULL );
226     if( ret )
227         Close_LuaPlaylist( p_this );
228     return ret;
229 }
230
231
232 /*****************************************************************************
233  * Deactivate: frees unused data
234  *****************************************************************************/
235 void Close_LuaPlaylist( vlc_object_t *p_this )
236 {
237     demux_t *p_demux = (demux_t *)p_this;
238     if( p_demux->p_sys->L )
239         lua_close( p_demux->p_sys->L );
240     free( p_demux->p_sys->psz_filename );
241     free( p_demux->p_sys );
242 }
243
244 static int Demux( demux_t *p_demux )
245 {
246     lua_State *L = p_demux->p_sys->L;
247     char *psz_filename = p_demux->p_sys->psz_filename;
248
249     input_thread_t *p_input_thread = demux_GetParentInput( p_demux );
250     input_item_t *p_current_input = input_GetItem( p_input_thread );
251
252     luaL_register( L, "vlc", p_reg_parse );
253
254     lua_getglobal( L, "parse" );
255
256     if( !lua_isfunction( L, -1 ) )
257     {
258         msg_Warn( p_demux, "Error while running script %s, "
259                   "function parse() not found", psz_filename );
260         vlc_object_release( p_input_thread );
261         return VLC_EGENERIC;
262     }
263
264     if( lua_pcall( L, 0, 1, 0 ) )
265     {
266         msg_Warn( p_demux, "Error while running script %s, "
267                   "function parse(): %s", psz_filename,
268                   lua_tostring( L, lua_gettop( L ) ) );
269         vlc_object_release( p_input_thread );
270         return VLC_EGENERIC;
271     }
272
273     if( lua_gettop( L ) )
274         vlclua_playlist_add_internal( p_demux, L, NULL, p_current_input, 0 );
275     else
276         msg_Err( p_demux, "Script went completely foobar" );
277
278     vlc_object_release( p_input_thread );
279
280     return -1; /* Needed for correct operation of go back */
281 }
282
283 static int Control( demux_t *p_demux, int i_query, va_list args )
284 {
285     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
286     return VLC_EGENERIC;
287 }