1 /*****************************************************************************
2 * demux.c : Lua playlist demux module
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
36 #include <vlc_strings.h>
40 #include "libs/playlist.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Demux( demux_t *p_demux );
47 static int Control( demux_t *p_demux, int i_query, va_list args );
49 /*****************************************************************************
50 * Demux specific functions
51 *****************************************************************************/
58 static int vlclua_demux_peek( lua_State *L )
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 );
68 static int vlclua_demux_read( lua_State *L )
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);
80 static int vlclua_demux_readline( lua_State *L )
82 demux_t *p_demux = (demux_t *)vlclua_get_this( L );
83 char *psz_line = stream_ReadLine( p_demux->s );
86 lua_pushstring( L, psz_line );
96 /*****************************************************************************
98 *****************************************************************************/
99 /* Functions to register */
100 static const luaL_Reg p_reg[] =
102 { "peek", vlclua_demux_peek },
106 /* Functions to register for parse() function call only */
107 static const luaL_Reg p_reg_parse[] =
109 { "read", vlclua_demux_read },
110 { "readline", vlclua_demux_readline },
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,
121 VLC_UNUSED(user_data);
122 demux_t * p_demux = (demux_t *)p_this;
124 p_demux->p_sys->psz_filename = strdup(psz_filename);
126 /* Initialise Lua state structure */
127 lua_State *L = luaL_newstate();
130 msg_Err( p_demux, "Could not create new Lua State" );
133 p_demux->p_sys->L = L;
135 /* Load Lua libraries */
136 luaL_openlibs( L ); /* FIXME: Don't open all the libs? */
138 luaL_register( L, "vlc", p_reg );
140 luaopen_strings( L );
141 lua_pushlightuserdata( L, p_demux );
142 lua_setfield( L, -2, "private" );
143 lua_pushstring( L, p_demux->psz_path );
144 lua_setfield( L, -2, "path" );
145 lua_pushstring( L, p_demux->psz_access );
146 lua_setfield( L, -2, "access" );
150 /* Setup the module search path */
151 if( vlclua_add_modules_path( p_demux, L, psz_filename ) )
153 msg_Warn( p_demux, "Error while setting the module search path for %s",
158 /* Load and run the script(s) */
159 if( luaL_dofile( L, psz_filename ) )
161 msg_Warn( p_demux, "Error loading script %s: %s", psz_filename,
162 lua_tostring( L, lua_gettop( L ) ) );
166 lua_getglobal( L, "probe" );
168 if( !lua_isfunction( L, -1 ) )
170 msg_Warn( p_demux, "Error while runing script %s, "
171 "function probe() not found", psz_filename );
175 if( lua_pcall( L, 0, 1, 0 ) )
177 msg_Warn( p_demux, "Error while runing script %s, "
178 "function probe(): %s", psz_filename,
179 lua_tostring( L, lua_gettop( L ) ) );
183 if( lua_gettop( L ) )
185 if( lua_toboolean( L, 1 ) )
187 msg_Dbg( p_demux, "Lua playlist script %s's "
188 "probe() function was successful", psz_filename );
196 lua_close( p_demux->p_sys->L );
197 p_demux->p_sys->L = NULL;
198 FREENULL( p_demux->p_sys->psz_filename );
202 /*****************************************************************************
203 * Import_LuaPlaylist: main import function
204 *****************************************************************************/
205 int Import_LuaPlaylist( vlc_object_t *p_this )
207 demux_t *p_demux = (demux_t *)p_this;
210 p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
211 if( !p_demux->p_sys )
216 p_demux->p_sys->psz_filename = NULL;
218 p_demux->pf_control = Control;
219 p_demux->pf_demux = Demux;
221 ret = vlclua_scripts_batch_execute( p_this, "playlist",
222 &probe_luascript, NULL );
224 Close_LuaPlaylist( p_this );
229 /*****************************************************************************
230 * Deactivate: frees unused data
231 *****************************************************************************/
232 void Close_LuaPlaylist( vlc_object_t *p_this )
234 demux_t *p_demux = (demux_t *)p_this;
235 if( p_demux->p_sys->L )
236 lua_close( p_demux->p_sys->L );
237 free( p_demux->p_sys->psz_filename );
238 free( p_demux->p_sys );
241 static int Demux( demux_t *p_demux )
243 lua_State *L = p_demux->p_sys->L;
244 char *psz_filename = p_demux->p_sys->psz_filename;
246 input_thread_t *p_input_thread = demux_GetParentInput( p_demux );
247 input_item_t *p_current_input = input_GetItem( p_input_thread );
248 playlist_t *p_playlist = pl_Get( p_demux );
250 luaL_register( L, "vlc", p_reg_parse );
252 lua_getglobal( L, "parse" );
254 if( !lua_isfunction( L, -1 ) )
256 msg_Warn( p_demux, "Error while runing script %s, "
257 "function parse() not found", psz_filename );
261 if( lua_pcall( L, 0, 1, 0 ) )
263 msg_Warn( p_demux, "Error while runing script %s, "
264 "function parse(): %s", psz_filename,
265 lua_tostring( L, lua_gettop( L ) ) );
269 if( lua_gettop( L ) )
270 vlclua_playlist_add_internal( p_demux, L, p_playlist,
271 p_current_input, 0 );
273 msg_Err( p_demux, "Script went completely foobar" );
275 vlc_object_release( p_input_thread );
277 return -1; /* Needed for correct operation of go back */
280 static int Control( demux_t *p_demux, int i_query, va_list args )
282 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);