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>
39 #include <vlc_stream_extractor.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int vlclua_stream_read( lua_State
* );
48 static int vlclua_stream_readline( lua_State
* );
49 static int vlclua_stream_delete( lua_State
* );
50 static int vlclua_stream_add_filter( lua_State
*L
);
51 static int vlclua_stream_readdir( lua_State
*L
);
52 static int vlclua_stream_getsize( lua_State
*L
);
53 static int vlclua_stream_seek( 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
},
59 { "readdir", vlclua_stream_readdir
},
60 { "getsize", vlclua_stream_getsize
},
61 { "seek", vlclua_stream_seek
},
65 static int vlclua_stream_new_inner( lua_State
*L
, stream_t
*p_stream
)
70 lua_pushliteral( L
, "Error when opening stream" );
74 stream_t
**pp_stream
= lua_newuserdata( L
, sizeof( stream_t
* ) );
75 *pp_stream
= p_stream
;
77 if( luaL_newmetatable( L
, "stream" ) )
80 luaL_register( L
, NULL
, vlclua_stream_reg
);
81 lua_setfield( L
, -2, "__index" );
82 lua_pushcfunction( L
, vlclua_stream_delete
);
83 lua_setfield( L
, -2, "__gc" );
86 lua_setmetatable( L
, -2 );
90 static int vlclua_stream_new( lua_State
*L
)
92 vlc_object_t
* p_this
= vlclua_get_this( L
);
93 const char * psz_url
= luaL_checkstring( L
, 1 );
94 stream_t
*p_stream
= vlc_stream_NewMRL( p_this
, psz_url
);
95 return vlclua_stream_new_inner( L
, p_stream
);
98 static int vlclua_memory_stream_new( lua_State
*L
)
100 vlc_object_t
* p_this
= vlclua_get_this( L
);
101 /* FIXME: duplicating the whole buffer is suboptimal. Keeping a reference to the string so that it doesn't get garbage collected would be better */
102 char * psz_content
= strdup( luaL_checkstring( L
, 1 ) );
103 stream_t
*p_stream
= vlc_stream_MemoryNew( p_this
, (uint8_t *)psz_content
, strlen( psz_content
), false );
104 return vlclua_stream_new_inner( L
, p_stream
);
107 static int vlclua_directory_stream_new( lua_State
*L
)
109 vlc_object_t
* p_this
= vlclua_get_this( L
);
110 const char * psz_url
= luaL_checkstring( L
, 1 );
111 stream_t
*p_stream
= vlc_stream_NewURL( p_this
, psz_url
);
113 return vlclua_error( L
);
114 if( vlc_stream_directory_Attach( &p_stream
, NULL
) != VLC_SUCCESS
)
116 vlc_stream_Delete( p_stream
);
117 return vlclua_error( L
);
119 return vlclua_stream_new_inner( L
, p_stream
);
122 static int vlclua_stream_read( lua_State
*L
)
125 stream_t
**pp_stream
= (stream_t
**)luaL_checkudata( L
, 1, "stream" );
126 int n
= luaL_checkint( L
, 2 );
127 uint8_t *p_read
= malloc( n
);
128 if( !p_read
) return vlclua_error( L
);
130 i_read
= vlc_stream_Read( *pp_stream
, p_read
, n
);
132 lua_pushlstring( L
, (const char *)p_read
, i_read
);
139 static int vlclua_stream_readline( lua_State
*L
)
141 stream_t
**pp_stream
= (stream_t
**)luaL_checkudata( L
, 1, "stream" );
142 char *psz_line
= vlc_stream_ReadLine( *pp_stream
);
145 lua_pushstring( L
, psz_line
);
153 static int vlclua_stream_add_filter( lua_State
*L
)
155 vlc_object_t
*p_this
= vlclua_get_this( L
);
157 /* Make sure that we have 1 argument (+ 1 object) */
160 stream_t
**pp_stream
= (stream_t
**)luaL_checkudata( L
, 1, "stream" );
161 if( !*pp_stream
) return vlclua_error( L
);
162 const char *psz_filter
= NULL
;
164 if( lua_isstring( L
, 2 ) )
165 psz_filter
= lua_tostring( L
, 2 );
167 if( !psz_filter
|| !*psz_filter
)
169 msg_Dbg( p_this
, "adding all automatic stream filters" );
172 /* Add next automatic stream */
173 stream_t
*p_filtered
= vlc_stream_FilterNew( *pp_stream
, NULL
);
178 msg_Dbg( p_this
, "inserted an automatic stream filter" );
179 *pp_stream
= p_filtered
;
182 luaL_getmetatable( L
, "stream" );
183 lua_setmetatable( L
, 1 );
187 /* Add a named filter */
188 stream_t
*p_filter
= vlc_stream_FilterNew( *pp_stream
, psz_filter
);
190 msg_Dbg( p_this
, "Unable to open requested stream filter '%s'",
194 *pp_stream
= p_filter
;
195 luaL_getmetatable( L
, "stream" );
196 lua_setmetatable( L
, 1 );
203 static int vlclua_stream_readdir( lua_State
*L
)
205 stream_t
**pp_stream
= (stream_t
**)luaL_checkudata( L
, 1, "stream" );
206 const char *psz_filter
= NULL
;
207 bool b_show_hidden
= false;
208 if( lua_gettop( L
) >= 2 )
210 psz_filter
= lua_tostring( L
, 2 );
211 if( lua_gettop( L
) >= 3 )
212 b_show_hidden
= lua_toboolean( L
, 3 );
215 if( !pp_stream
|| !*pp_stream
)
216 return vlclua_error( L
);
217 if( (*pp_stream
)->pf_readdir
== NULL
)
218 return vlclua_error( L
);
220 input_item_t
*p_input
= input_item_New( (*pp_stream
)->psz_url
, NULL
);
224 if( asprintf( &psz_opt
, ":ignore-filetype=\"%s\"", psz_filter
) < 0 )
226 input_item_Release( p_input
);
227 return vlclua_error( L
);
229 input_item_AddOption( p_input
, psz_opt
, VLC_INPUT_OPTION_TRUSTED
);
233 input_item_AddOption( p_input
, "ignore-filetypes=\"\"",
234 VLC_INPUT_OPTION_TRUSTED
);
236 input_item_AddOption( p_input
, "show-hiddenfiles",
237 VLC_INPUT_OPTION_TRUSTED
);
238 input_item_node_t
*p_items
= input_item_node_Create( p_input
);
239 input_item_Release( p_input
);
241 return vlclua_error( L
);
242 if ( vlc_stream_ReadDir( *pp_stream
, p_items
) )
244 input_item_node_Delete( p_items
);
245 return vlclua_error( L
);
248 for ( int i
= 0; i
< p_items
->i_children
; ++i
)
250 lua_pushinteger( L
, i
+ 1 );
251 vlclua_input_item_get( L
, p_items
->pp_children
[i
]->p_item
);
252 lua_settable( L
, -3 );
254 input_item_node_Delete( p_items
);
258 static int vlclua_stream_getsize( lua_State
*L
)
260 stream_t
**pp_stream
= (stream_t
**)luaL_checkudata( L
, 1, "stream" );
262 int i_res
= vlc_stream_GetSize( *pp_stream
, &i_size
);
264 return luaL_error( L
, "Failed to get stream size" );
265 lua_pushnumber( L
, i_size
);
269 static int vlclua_stream_seek( lua_State
*L
)
271 stream_t
**pp_stream
= (stream_t
**)luaL_checkudata( L
, 1, "stream" );
272 lua_Integer i_offset
= luaL_checkinteger( L
, 2 );
274 return luaL_error( L
, "Invalid negative seek offset" );
275 int i_res
= vlc_stream_Seek( *pp_stream
, (uint64_t)i_offset
);
276 lua_pushboolean( L
, i_res
== 0 );
280 static int vlclua_stream_delete( lua_State
*L
)
282 stream_t
**pp_stream
= (stream_t
**)luaL_checkudata( L
, 1, "stream" );
283 vlc_stream_Delete( *pp_stream
);
287 /*****************************************************************************
289 *****************************************************************************/
290 void luaopen_stream( lua_State
*L
)
292 lua_pushcfunction( L
, vlclua_stream_new
);
293 lua_setfield( L
, -2, "stream" );
294 lua_pushcfunction( L
, vlclua_memory_stream_new
);
295 lua_setfield( L
, -2, "memory_stream" );
296 lua_pushcfunction( L
, vlclua_directory_stream_new
);
297 lua_setfield( L
, -2, "directory_stream" );