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