1 /*****************************************************************************
2 * playlist.c : Lua playlist demux module
3 *****************************************************************************
4 * Copyright (C) 2007 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 *****************************************************************************/
32 #include <vlc_demux.h>
34 #include <vlc_strings.h>
35 #include <vlc_charset.h>
37 #include <errno.h> /* ENOMEM */
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Demux( demux_t *p_demux );
49 static int Control( demux_t *p_demux, int i_query, va_list args );
51 /*****************************************************************************
53 *****************************************************************************/
60 /*****************************************************************************
62 *****************************************************************************/
64 static int vlclua_demux_peek( lua_State *L )
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 );
74 static int vlclua_demux_read( lua_State *L )
76 demux_t *p_demux = (demux_t *)vlclua_get_this( L );
78 int n = luaL_checkint( L, 1 );
79 int i_read = stream_Read( p_demux->s, &p_read, n );
80 lua_pushlstring( L, (const char *)p_read, i_read );
84 static int vlclua_demux_readline( lua_State *L )
86 demux_t *p_demux = (demux_t *)vlclua_get_this( L );
87 char *psz_line = stream_ReadLine( p_demux->s );
90 lua_pushstring( L, psz_line );
101 /* Functions to register */
102 static luaL_Reg p_reg[] =
104 { "peek", vlclua_demux_peek },
105 { "decode_uri", vlclua_decode_uri },
106 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
107 { "msg_dbg", vlclua_msg_dbg },
108 { "msg_warn", vlclua_msg_warn },
109 { "msg_err", vlclua_msg_err },
110 { "msg_info", vlclua_msg_info },
114 /* Functions to register for parse() function call only */
115 static luaL_Reg p_reg_parse[] =
117 { "read", vlclua_demux_read },
118 { "readline", vlclua_demux_readline },
122 /*****************************************************************************
123 * Called through lua_scripts_batch_execute to call 'probe' on
124 * the script pointed by psz_filename.
125 *****************************************************************************/
126 static int probe_luascript( vlc_object_t *p_this, const char * psz_filename,
127 lua_State * L, void * user_data )
129 VLC_UNUSED(user_data);
130 demux_t * p_demux = (demux_t *)p_this;
132 p_demux->p_sys->psz_filename = strdup(psz_filename);
134 /* In lua, setting a variable's value to nil is equivalent to deleting it */
137 lua_setglobal( L, "probe" );
138 lua_setglobal( L, "parse" );
140 /* Load and run the script(s) */
141 if( luaL_dofile( L, psz_filename ) )
143 msg_Warn( p_demux, "Error loading script %s: %s", psz_filename,
144 lua_tostring( L, lua_gettop( L ) ) );
149 lua_getglobal( L, "probe" );
151 if( !lua_isfunction( L, -1 ) )
153 msg_Warn( p_demux, "Error while runing script %s, "
154 "function probe() not found", psz_filename );
159 if( lua_pcall( L, 0, 1, 0 ) )
161 msg_Warn( p_demux, "Error while runing script %s, "
162 "function probe(): %s", psz_filename,
163 lua_tostring( L, lua_gettop( L ) ) );
168 if( lua_gettop( L ) )
170 int i_ret = VLC_EGENERIC;
171 if( lua_toboolean( L, 1 ) )
173 msg_Dbg( p_demux, "Lua playlist script %s's "
174 "probe() function was successful", psz_filename );
184 /*****************************************************************************
185 * Import_LuaPlaylist: main import function
186 *****************************************************************************/
187 int E_(Import_LuaPlaylist)( vlc_object_t *p_this )
189 demux_t *p_demux = (demux_t *)p_this;
193 p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
194 if( !p_demux->p_sys )
199 p_demux->p_sys->psz_filename = NULL;
201 p_demux->pf_control = Control;
202 p_demux->pf_demux = Demux;
204 /* Initialise Lua state structure */
208 msg_Err( p_demux, "Could not create new Lua State" );
209 free( p_demux->p_sys );
212 p_demux->p_sys->L = L;
214 /* Load Lua libraries */
215 luaL_openlibs( L ); /* FIXME: Don't open all the libs? */
217 luaL_register( L, "vlc", p_reg );
218 lua_pushlightuserdata( L, p_demux );
219 lua_setfield( L, -2, "private" );
220 lua_pushstring( L, p_demux->psz_path );
221 lua_setfield( L, -2, "path" );
222 lua_pushstring( L, p_demux->psz_access );
223 lua_setfield( L, -2, "access" );
227 ret = vlclua_scripts_batch_execute( p_this, "playlist",
228 &probe_luascript, L, NULL );
230 E_(Close_LuaPlaylist)( p_this );
235 /*****************************************************************************
236 * Deactivate: frees unused data
237 *****************************************************************************/
238 void E_(Close_LuaPlaylist)( vlc_object_t *p_this )
240 demux_t *p_demux = (demux_t *)p_this;
241 lua_close( p_demux->p_sys->L );
242 free( p_demux->p_sys->psz_filename );
243 free( p_demux->p_sys );
246 static int Demux( demux_t *p_demux )
248 lua_State *L = p_demux->p_sys->L;
249 char *psz_filename = p_demux->p_sys->psz_filename;
251 playlist_t *p_playlist = pl_Yield( p_demux );
252 input_thread_t *p_input_thread = (input_thread_t *)
253 vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
254 input_item_t *p_current_input = input_GetItem( p_input_thread );
256 luaL_register( L, "vlc", p_reg_parse );
258 lua_getglobal( L, "parse" );
260 if( !lua_isfunction( L, -1 ) )
262 msg_Warn( p_demux, "Error while runing script %s, "
263 "function parse() not found", psz_filename );
264 E_(Close_LuaPlaylist)( VLC_OBJECT( p_demux ) );
268 if( lua_pcall( L, 0, 1, 0 ) )
270 msg_Warn( p_demux, "Error while runing script %s, "
271 "function parse(): %s", psz_filename,
272 lua_tostring( L, lua_gettop( L ) ) );
273 E_(Close_LuaPlaylist)( VLC_OBJECT( p_demux ) );
277 if( lua_gettop( L ) )
278 vlclua_playlist_add_internal( p_demux, L, p_playlist,
279 p_current_input, 0 );
281 msg_Err( p_demux, "Script went completely foobar" );
283 vlc_object_release( p_input_thread );
284 vlc_object_release( p_playlist );
286 return -1; /* Needed for correct operation of go back */
289 static int Control( demux_t *p_demux, int i_query, va_list args )
291 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);