1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*****************************************************************************
* demux.c : Lua playlist demux module
*****************************************************************************
* Copyright (C) 2007-2008 the VideoLAN team
* $Id: 320267508fa7d25dc78df0b901173df8da27e663 $
*
* Authors: Antoine Cellerier <dionoea at videolan tod org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <vlc_common.h>
#include <vlc_demux.h>
#include <vlc_url.h>
#include <vlc_strings.h>
#include "vlc.h"
#include "libs.h"
#include "libs/playlist.h"
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int Demux( demux_t *p_demux );
static int Control( demux_t *p_demux, int i_query, va_list args );
/*****************************************************************************
* Demux specific functions
*****************************************************************************/
struct demux_sys_t
{
lua_State *L;
char *psz_filename;
};
static int vlclua_demux_peek( lua_State *L )
{
demux_t *p_demux = (demux_t *)vlclua_get_this( L );
int n = luaL_checkint( L, 1 );
const uint8_t *p_peek;
int i_peek = stream_Peek( p_demux->s, &p_peek, n );
lua_pushlstring( L, (const char *)p_peek, i_peek );
return 1;
}
static int vlclua_demux_read( lua_State *L )
{
demux_t *p_demux = (demux_t *)vlclua_get_this( L );
const uint8_t *p_read;
int n = luaL_checkint( L, 1 );
int i_read = stream_Peek( p_demux->s, &p_read, n );
lua_pushlstring( L, (const char *)p_read, i_read );
int i_seek = stream_Read( p_demux->s, NULL, i_read );
assert(i_read==i_seek);
return 1;
}
static int vlclua_demux_readline( lua_State *L )
{
demux_t *p_demux = (demux_t *)vlclua_get_this( L );
char *psz_line = stream_ReadLine( p_demux->s );
if( psz_line )
{
lua_pushstring( L, psz_line );
free( psz_line );
}
else
{
lua_pushnil( L );
}
return 1;
}
/*****************************************************************************
*
*****************************************************************************/
/* Functions to register */
static const luaL_Reg p_reg[] =
{
{ "peek", vlclua_demux_peek },
{ NULL, NULL }
};
/* Functions to register for parse() function call only */
static const luaL_Reg p_reg_parse[] =
{
{ "read", vlclua_demux_read },
{ "readline", vlclua_demux_readline },
{ NULL, NULL }
};
/*****************************************************************************
* Called through lua_scripts_batch_execute to call 'probe' on
* the script pointed by psz_filename.
*****************************************************************************/
static int probe_luascript( vlc_object_t *p_this, const char * psz_filename,
void * user_data )
{
VLC_UNUSED(user_data);
demux_t * p_demux = (demux_t *)p_this;
p_demux->p_sys->psz_filename = strdup(psz_filename);
/* Initialise Lua state structure */
lua_State *L = luaL_newstate();
if( !L )
{
msg_Err( p_demux, "Could not create new Lua State" );
goto error;
}
p_demux->p_sys->L = L;
/* Load Lua libraries */
luaL_openlibs( L ); /* FIXME: Don't open all the libs? */
vlclua_set_this( L, p_demux );
luaL_register( L, "vlc", p_reg );
luaopen_msg( L );
luaopen_strings( L );
luaopen_stream( L );
luaopen_xml( L );
luaopen_md5( L );
lua_pushstring( L, p_demux->psz_path );
lua_setfield( L, -2, "path" );
lua_pushstring( L, p_demux->psz_access );
lua_setfield( L, -2, "access" );
lua_pop( L, 1 );
/* Setup the module search path */
if( vlclua_add_modules_path( p_demux, L, psz_filename ) )
{
msg_Warn( p_demux, "Error while setting the module search path for %s",
psz_filename );
goto error;
}
/* Load and run the script(s) */
if( luaL_dofile( L, psz_filename ) )
{
msg_Warn( p_demux, "Error loading script %s: %s", psz_filename,
lua_tostring( L, lua_gettop( L ) ) );
goto error;
}
lua_getglobal( L, "probe" );
if( !lua_isfunction( L, -1 ) )
{
msg_Warn( p_demux, "Error while running script %s, "
"function probe() not found", psz_filename );
goto error;
}
if( lua_pcall( L, 0, 1, 0 ) )
{
msg_Warn( p_demux, "Error while running script %s, "
"function probe(): %s", psz_filename,
lua_tostring( L, lua_gettop( L ) ) );
goto error;
}
if( lua_gettop( L ) )
{
if( lua_toboolean( L, 1 ) )
{
msg_Dbg( p_demux, "Lua playlist script %s's "
"probe() function was successful", psz_filename );
lua_pop( L, 1 );
return VLC_SUCCESS;
}
}
error:
lua_pop( L, 1 );
lua_close( p_demux->p_sys->L );
p_demux->p_sys->L = NULL;
FREENULL( p_demux->p_sys->psz_filename );
return VLC_EGENERIC;
}
/*****************************************************************************
* Import_LuaPlaylist: main import function
*****************************************************************************/
int Import_LuaPlaylist( vlc_object_t *p_this )
{
demux_t *p_demux = (demux_t *)p_this;
int ret;
p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
if( !p_demux->p_sys )
{
return VLC_ENOMEM;
}
p_demux->p_sys->psz_filename = NULL;
p_demux->pf_control = Control;
p_demux->pf_demux = Demux;
ret = vlclua_scripts_batch_execute( p_this, "playlist",
&probe_luascript, NULL );
if( ret )
Close_LuaPlaylist( p_this );
return ret;
}
/*****************************************************************************
* Deactivate: frees unused data
*****************************************************************************/
void Close_LuaPlaylist( vlc_object_t *p_this )
{
demux_t *p_demux = (demux_t *)p_this;
if( p_demux->p_sys->L )
lua_close( p_demux->p_sys->L );
free( p_demux->p_sys->psz_filename );
free( p_demux->p_sys );
}
static int Demux( demux_t *p_demux )
{
lua_State *L = p_demux->p_sys->L;
char *psz_filename = p_demux->p_sys->psz_filename;
input_thread_t *p_input_thread = demux_GetParentInput( p_demux );
input_item_t *p_current_input = input_GetItem( p_input_thread );
playlist_t *p_playlist = pl_Get( p_demux );
luaL_register( L, "vlc", p_reg_parse );
lua_getglobal( L, "parse" );
if( !lua_isfunction( L, -1 ) )
{
msg_Warn( p_demux, "Error while running script %s, "
"function parse() not found", psz_filename );
vlc_object_release( p_input_thread );
return VLC_EGENERIC;
}
if( lua_pcall( L, 0, 1, 0 ) )
{
msg_Warn( p_demux, "Error while running script %s, "
"function parse(): %s", psz_filename,
lua_tostring( L, lua_gettop( L ) ) );
vlc_object_release( p_input_thread );
return VLC_EGENERIC;
}
if( lua_gettop( L ) )
vlclua_playlist_add_internal( p_demux, L, p_playlist,
p_current_input, 0 );
else
msg_Err( p_demux, "Script went completely foobar" );
vlc_object_release( p_input_thread );
return -1; /* Needed for correct operation of go back */
}
static int Control( demux_t *p_demux, int i_query, va_list args )
{
VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
return VLC_EGENERIC;
}
|