1 /*****************************************************************************
2 * stream.c: stream functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
41 #include <lua.h> /* Low level lua C API */
42 #include <lauxlib.h> /* Higher level C API */
47 /*****************************************************************************
49 *****************************************************************************/
50 static int vlclua_stream_read( lua_State * );
51 static int vlclua_stream_readline( lua_State * );
52 static int vlclua_stream_delete( lua_State * );
53 static int vlclua_stream_add_filter( lua_State *L );
55 static const luaL_Reg vlclua_stream_reg[] = {
56 { "read", vlclua_stream_read },
57 { "readline", vlclua_stream_readline },
58 { "addfilter", vlclua_stream_add_filter },
62 static int vlclua_stream_new_inner( lua_State *L, stream_t *p_stream )
67 lua_pushliteral( L, "Error when opening stream" );
71 stream_t **pp_stream = lua_newuserdata( L, sizeof( stream_t * ) );
72 *pp_stream = p_stream;
74 if( luaL_newmetatable( L, "stream" ) )
77 luaL_register( L, NULL, vlclua_stream_reg );
78 lua_setfield( L, -2, "__index" );
79 lua_pushcfunction( L, vlclua_stream_delete );
80 lua_setfield( L, -2, "__gc" );
83 lua_setmetatable( L, -2 );
87 static int vlclua_stream_new( lua_State *L )
89 vlc_object_t * p_this = vlclua_get_this( L );
90 const char * psz_url = luaL_checkstring( L, 1 );
91 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
92 return vlclua_stream_new_inner( L, p_stream );
95 static int vlclua_memory_stream_new( lua_State *L )
97 vlc_object_t * p_this = vlclua_get_this( L );
98 /* FIXME: duplicating the whole buffer is suboptimal. Keeping a reference to the string so that it doesn't get garbage collected would be better */
99 char * psz_content = strdup( luaL_checkstring( L, 1 ) );
100 stream_t *p_stream = stream_MemoryNew( p_this, (uint8_t *)psz_content, strlen( psz_content ), false );
101 return vlclua_stream_new_inner( L, p_stream );
104 static int vlclua_stream_read( lua_State *L )
107 stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
108 int n = luaL_checkint( L, 2 );
109 uint8_t *p_read = malloc( n );
110 if( !p_read ) return vlclua_error( L );
112 i_read = stream_Read( *pp_stream, p_read, n );
114 lua_pushlstring( L, (const char *)p_read, i_read );
121 static int vlclua_stream_readline( lua_State *L )
123 stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
124 char *psz_line = stream_ReadLine( *pp_stream );
127 lua_pushstring( L, psz_line );
135 static int vlclua_stream_add_filter( lua_State *L )
137 vlc_object_t *p_this = vlclua_get_this( L );
139 /* Make sure that we have 1 argument (+ 1 object) */
142 stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
143 if( !*pp_stream ) return vlclua_error( L );
144 const char *psz_filter = NULL;
146 if( lua_isstring( L, 2 ) )
147 psz_filter = lua_tostring( L, 2 );
149 if( !psz_filter || !*psz_filter )
151 msg_Dbg( p_this, "adding all automatic stream filters" );
154 /* Add next automatic stream */
155 stream_t *p_filtered = stream_FilterNew( *pp_stream, NULL );
160 msg_Dbg( p_this, "inserted an automatic stream filter" );
161 *pp_stream = p_filtered;
164 luaL_getmetatable( L, "stream" );
165 lua_setmetatable( L, 1 );
169 /* Add a named filter */
170 stream_t *p_filter = stream_FilterNew( *pp_stream, psz_filter );
172 msg_Dbg( p_this, "Unable to open requested stream filter '%s'",
176 *pp_stream = p_filter;
177 luaL_getmetatable( L, "stream" );
178 lua_setmetatable( L, 1 );
185 static int vlclua_stream_delete( lua_State *L )
187 stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
188 stream_Delete( *pp_stream );
192 /*****************************************************************************
194 *****************************************************************************/
195 void luaopen_stream( lua_State *L )
197 lua_pushcfunction( L, vlclua_stream_new );
198 lua_setfield( L, -2, "stream" );
199 lua_pushcfunction( L, vlclua_memory_stream_new );
200 lua_setfield( L, -2, "memory_stream" );