2007-11-04 22:42:27 +01:00
|
|
|
/*****************************************************************************
|
2019-02-07 01:13:13 +01:00
|
|
|
* stream_filter.c : Lua playlist stream filter module
|
2007-11-04 22:42:27 +01:00
|
|
|
*****************************************************************************
|
2008-06-11 15:57:58 +02:00
|
|
|
* Copyright (C) 2007-2008 the VideoLAN team
|
2007-11-04 22:42:27 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*****************************************************************************/
|
2008-01-23 22:50:58 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2009-06-12 15:05:56 +02:00
|
|
|
#include <assert.h>
|
2017-04-18 21:35:47 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <vlc_common.h>
|
|
|
|
#include <vlc_access.h>
|
2009-06-12 15:05:56 +02:00
|
|
|
|
2007-11-04 22:42:27 +01:00
|
|
|
#include "vlc.h"
|
2008-06-11 15:57:58 +02:00
|
|
|
#include "libs.h"
|
2007-11-04 22:42:27 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
2008-06-11 15:57:58 +02:00
|
|
|
* Demux specific functions
|
2007-11-04 22:42:27 +01:00
|
|
|
*****************************************************************************/
|
2017-04-18 21:35:47 +02:00
|
|
|
struct vlclua_playlist
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
|
|
|
lua_State *L;
|
2017-04-18 21:35:47 +02:00
|
|
|
char *filename;
|
|
|
|
char *access;
|
|
|
|
const char *path;
|
2007-11-04 22:42:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int vlclua_demux_peek( lua_State *L )
|
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
stream_t *s = (stream_t *)vlclua_get_this(L);
|
2018-11-17 13:53:27 +01:00
|
|
|
int n = luaL_checkinteger( L, 1 );
|
2007-11-04 22:42:27 +01:00
|
|
|
const uint8_t *p_peek;
|
2011-06-26 21:17:11 +02:00
|
|
|
|
2017-12-02 22:22:35 +01:00
|
|
|
ssize_t val = vlc_stream_Peek(s->s, &p_peek, n);
|
2017-04-18 21:35:47 +02:00
|
|
|
if (val > 0)
|
|
|
|
lua_pushlstring(L, (const char *)p_peek, val);
|
2011-06-26 21:17:11 +02:00
|
|
|
else
|
|
|
|
lua_pushnil( L );
|
2007-11-04 22:42:27 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vlclua_demux_read( lua_State *L )
|
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
stream_t *s = (stream_t *)vlclua_get_this(L);
|
2018-11-17 13:53:27 +01:00
|
|
|
int n = luaL_checkinteger( L, 1 );
|
2017-04-18 22:42:50 +02:00
|
|
|
char *buf = malloc(n);
|
2011-06-26 21:17:11 +02:00
|
|
|
|
2017-04-18 22:42:50 +02:00
|
|
|
if (buf != NULL)
|
2011-06-26 21:17:11 +02:00
|
|
|
{
|
2017-12-02 22:22:35 +01:00
|
|
|
ssize_t val = vlc_stream_Read(s->s, buf, n);
|
2017-04-18 22:42:50 +02:00
|
|
|
if (val > 0)
|
|
|
|
lua_pushlstring(L, buf, val);
|
|
|
|
else
|
|
|
|
lua_pushnil( L );
|
|
|
|
free(buf);
|
2011-06-26 21:17:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_pushnil( L );
|
|
|
|
|
2007-11-04 22:42:27 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vlclua_demux_readline( lua_State *L )
|
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
stream_t *s = (stream_t *)vlclua_get_this(L);
|
2017-12-02 22:22:35 +01:00
|
|
|
char *line = vlc_stream_ReadLine(s->s);
|
2017-04-18 21:35:47 +02:00
|
|
|
|
|
|
|
if (line != NULL)
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
lua_pushstring(L, line);
|
|
|
|
free(line);
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
lua_pushnil( L );
|
2017-04-18 21:35:47 +02:00
|
|
|
|
2007-11-04 22:42:27 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Called through lua_scripts_batch_execute to call 'probe' on
|
|
|
|
* the script pointed by psz_filename.
|
|
|
|
*****************************************************************************/
|
2017-04-18 21:35:47 +02:00
|
|
|
static int probe_luascript(vlc_object_t *obj, const char *filename,
|
|
|
|
const luabatch_context_t *ctx)
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
stream_t *s = (stream_t *)obj;
|
|
|
|
struct vlclua_playlist *sys = s->p_sys;
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2010-02-13 14:07:30 +01:00
|
|
|
/* Initialise Lua state structure */
|
|
|
|
lua_State *L = luaL_newstate();
|
|
|
|
if( !L )
|
2017-04-18 21:35:47 +02:00
|
|
|
return VLC_ENOMEM;
|
|
|
|
|
|
|
|
sys->L = L;
|
2010-02-13 14:07:30 +01:00
|
|
|
|
|
|
|
/* Load Lua libraries */
|
|
|
|
luaL_openlibs( L ); /* FIXME: Don't open all the libs? */
|
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
vlclua_set_this(L, s);
|
2023-02-19 12:42:57 +01:00
|
|
|
|
|
|
|
/* Functions to register */
|
|
|
|
static const luaL_Reg p_reg[] =
|
|
|
|
{
|
|
|
|
{ "peek", vlclua_demux_peek },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2016-03-16 18:26:07 +01:00
|
|
|
luaL_register_namespace( L, "vlc", p_reg );
|
2010-02-13 14:07:30 +01:00
|
|
|
luaopen_msg( L );
|
|
|
|
luaopen_strings( L );
|
2010-02-27 00:52:26 +01:00
|
|
|
luaopen_stream( L );
|
2011-09-15 20:36:16 +02:00
|
|
|
luaopen_variables( L );
|
2010-02-13 14:54:04 +01:00
|
|
|
luaopen_xml( L );
|
2017-04-18 21:35:47 +02:00
|
|
|
|
|
|
|
if (sys->path != NULL)
|
|
|
|
lua_pushstring(L, sys->path);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
2010-02-13 14:07:30 +01:00
|
|
|
lua_setfield( L, -2, "path" );
|
2017-04-18 21:35:47 +02:00
|
|
|
|
|
|
|
if (sys->access != NULL)
|
|
|
|
lua_pushstring(L, sys->access);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
2010-02-13 14:07:30 +01:00
|
|
|
lua_setfield( L, -2, "access" );
|
|
|
|
|
|
|
|
lua_pop( L, 1 );
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2010-02-13 14:14:04 +01:00
|
|
|
/* Setup the module search path */
|
2017-04-18 21:35:47 +02:00
|
|
|
if (vlclua_add_modules_path(L, filename))
|
2010-02-13 14:14:04 +01:00
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
msg_Warn(s, "error setting the module search path for %s", filename);
|
2010-02-13 14:14:04 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2007-11-04 22:42:27 +01:00
|
|
|
/* Load and run the script(s) */
|
2017-04-18 21:35:47 +02:00
|
|
|
if (vlclua_dofile(VLC_OBJECT(s), L, filename))
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
msg_Warn(s, "error loading script %s: %s", filename,
|
|
|
|
lua_tostring(L, lua_gettop(L)));
|
2009-09-30 15:38:53 +02:00
|
|
|
goto error;
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_getglobal( L, "probe" );
|
2007-11-05 00:10:10 +01:00
|
|
|
if( !lua_isfunction( L, -1 ) )
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
msg_Warn(s, "error running script %s: function %s(): %s",
|
|
|
|
filename, "probe", "not found");
|
2009-09-30 15:38:53 +02:00
|
|
|
goto error;
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( lua_pcall( L, 0, 1, 0 ) )
|
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
msg_Warn(s, "error running script %s: function %s(): %s",
|
|
|
|
filename, "probe", lua_tostring(L, lua_gettop(L)));
|
2009-09-30 15:38:53 +02:00
|
|
|
goto error;
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( lua_gettop( L ) )
|
|
|
|
{
|
|
|
|
if( lua_toboolean( L, 1 ) )
|
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
msg_Dbg(s, "Lua playlist script %s's "
|
|
|
|
"probe() function was successful", filename );
|
2009-09-30 15:38:53 +02:00
|
|
|
lua_pop( L, 1 );
|
2017-04-18 21:35:47 +02:00
|
|
|
sys->filename = strdup(filename);
|
2009-09-30 15:38:53 +02:00
|
|
|
return VLC_SUCCESS;
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
}
|
2009-09-30 15:38:53 +02:00
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
(void) ctx;
|
2009-09-30 15:38:53 +02:00
|
|
|
error:
|
2009-09-30 15:45:33 +02:00
|
|
|
lua_pop( L, 1 );
|
2017-04-18 21:35:47 +02:00
|
|
|
lua_close(sys->L);
|
2007-11-04 22:42:27 +01:00
|
|
|
return VLC_EGENERIC;
|
|
|
|
}
|
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
static int ReadDir(stream_t *s, input_item_node_t *node)
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
struct vlclua_playlist *sys = s->p_sys;
|
|
|
|
lua_State *L = sys->L;
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2023-02-19 12:42:57 +01:00
|
|
|
/* 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 }
|
|
|
|
};
|
2017-04-18 21:35:47 +02:00
|
|
|
luaL_register_namespace( L, "vlc", p_reg_parse );
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
lua_getglobal( L, "parse" );
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
if( !lua_isfunction( L, -1 ) )
|
|
|
|
{
|
|
|
|
msg_Warn(s, "error running script %s: function %s(): %s",
|
|
|
|
sys->filename, "parse", "not found");
|
2021-07-12 18:42:56 +02:00
|
|
|
return VLC_EINVAL;
|
2017-04-18 21:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( lua_pcall( L, 0, 1, 0 ) )
|
|
|
|
{
|
|
|
|
msg_Warn(s, "error running script %s: function %s(): %s",
|
|
|
|
sys->filename, "parse", lua_tostring(L, lua_gettop(L)));
|
2021-07-12 18:42:56 +02:00
|
|
|
return VLC_EINVAL;
|
2017-04-18 21:35:47 +02:00
|
|
|
}
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
if (!lua_gettop(L))
|
|
|
|
{
|
|
|
|
msg_Err(s, "script went completely foobar");
|
2021-07-12 18:42:56 +02:00
|
|
|
return VLC_EINVAL;
|
2017-04-18 21:35:47 +02:00
|
|
|
}
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
if (!lua_istable(L, -1))
|
|
|
|
{
|
|
|
|
msg_Warn(s, "Playlist should be a table.");
|
2021-07-12 18:42:56 +02:00
|
|
|
return VLC_EINVAL;
|
2017-04-18 21:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
|
|
|
/* playlist nil */
|
|
|
|
while (lua_next(L, -2))
|
|
|
|
{
|
|
|
|
input_item_t *item = vlclua_read_input_item(VLC_OBJECT(s), L);
|
|
|
|
if (item != NULL)
|
|
|
|
{
|
|
|
|
/* copy the original URL to the meta data,
|
|
|
|
* if "URL" is still empty */
|
|
|
|
char *url = input_item_GetURL(item);
|
|
|
|
if (url == NULL && s->psz_url != NULL)
|
|
|
|
input_item_SetURL(item, s->psz_url);
|
|
|
|
free(url);
|
|
|
|
|
|
|
|
input_item_node_AppendItem(node, item);
|
|
|
|
input_item_Release(item);
|
|
|
|
}
|
|
|
|
/* pop the value, keep the key for the next lua_next() call */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
/* playlist */
|
|
|
|
|
|
|
|
return VLC_SUCCESS;
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* Import_LuaPlaylist: main import function
|
|
|
|
*****************************************************************************/
|
|
|
|
int Import_LuaPlaylist(vlc_object_t *obj)
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
2017-10-05 10:00:33 +02:00
|
|
|
stream_t *s = (stream_t *)obj;
|
2020-09-24 19:06:41 +02:00
|
|
|
|
|
|
|
if( lua_Disabled( obj ) )
|
2017-05-17 18:22:03 +02:00
|
|
|
return VLC_EGENERIC;
|
|
|
|
|
|
|
|
struct vlclua_playlist *sys = malloc(sizeof (*sys));
|
2017-04-18 21:35:47 +02:00
|
|
|
if (unlikely(sys == NULL))
|
|
|
|
return VLC_ENOMEM;
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
s->p_sys = sys;
|
|
|
|
sys->access = NULL;
|
|
|
|
sys->path = NULL;
|
2007-11-04 22:42:27 +01:00
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
if (s->psz_url != NULL)
|
|
|
|
{ /* Backward compatibility hack: Lua scripts expect the URI scheme and
|
|
|
|
* the rest of the URI separately. */
|
|
|
|
const char *p = strstr(s->psz_url, "://");
|
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
sys->access = strndup(s->psz_url, p - s->psz_url);
|
|
|
|
sys->path = p + 3;
|
|
|
|
}
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
int ret = vlclua_scripts_batch_execute(VLC_OBJECT(s), "playlist",
|
|
|
|
probe_luascript, NULL);
|
|
|
|
if (ret != VLC_SUCCESS)
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
free(sys->access);
|
|
|
|
free(sys);
|
|
|
|
return ret;
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
s->pf_readdir = ReadDir;
|
|
|
|
s->pf_control = access_vaDirectoryControlHelper;
|
|
|
|
return VLC_SUCCESS;
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 21:35:47 +02:00
|
|
|
void Close_LuaPlaylist(vlc_object_t *obj)
|
2007-11-04 22:42:27 +01:00
|
|
|
{
|
2017-04-18 21:35:47 +02:00
|
|
|
stream_t *s = (stream_t *)obj;
|
|
|
|
struct vlclua_playlist *sys = s->p_sys;
|
|
|
|
|
|
|
|
free(sys->filename);
|
|
|
|
assert(sys->L != NULL);
|
|
|
|
lua_close(sys->L);
|
|
|
|
free(sys->access);
|
|
|
|
free(sys);
|
2007-11-04 22:42:27 +01:00
|
|
|
}
|