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