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