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