]> git.sesse.net Git - vlc/blob - modules/lua/libs/stream.c
Consolidate lua includes in vlc.h
[vlc] / modules / lua / libs / stream.c
1 /*****************************************************************************
2  * stream.c: stream functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_meta.h>
39 #include <vlc_aout.h>
40
41 #include "../vlc.h"
42 #include "../libs.h"
43
44 /*****************************************************************************
45  * Stream handling
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
52 static const luaL_Reg vlclua_stream_reg[] = {
53     { "read", vlclua_stream_read },
54     { "readline", vlclua_stream_readline },
55     { "addfilter", vlclua_stream_add_filter },
56     { NULL, NULL }
57 };
58
59 static int vlclua_stream_new_inner( lua_State *L, stream_t *p_stream )
60 {
61     if( !p_stream )
62     {
63         lua_pushnil( L );
64         lua_pushliteral( L, "Error when opening stream" );
65         return 2;
66     }
67
68     stream_t **pp_stream = lua_newuserdata( L, sizeof( stream_t * ) );
69     *pp_stream = p_stream;
70
71     if( luaL_newmetatable( L, "stream" ) )
72     {
73         lua_newtable( L );
74         luaL_register( L, NULL, vlclua_stream_reg );
75         lua_setfield( L, -2, "__index" );
76         lua_pushcfunction( L, vlclua_stream_delete );
77         lua_setfield( L, -2, "__gc" );
78     }
79
80     lua_setmetatable( L, -2 );
81     return 1;
82 }
83
84 static int vlclua_stream_new( lua_State *L )
85 {
86     vlc_object_t * p_this = vlclua_get_this( L );
87     const char * psz_url = luaL_checkstring( L, 1 );
88     stream_t *p_stream = stream_UrlNew( p_this, psz_url );
89     return vlclua_stream_new_inner( L, p_stream );
90 }
91
92 static int vlclua_memory_stream_new( lua_State *L )
93 {
94     vlc_object_t * p_this = vlclua_get_this( L );
95     /* FIXME: duplicating the whole buffer is suboptimal. Keeping a reference to the string so that it doesn't get garbage collected would be better */
96     char * psz_content = strdup( luaL_checkstring( L, 1 ) );
97     stream_t *p_stream = stream_MemoryNew( p_this, (uint8_t *)psz_content, strlen( psz_content ), false );
98     return vlclua_stream_new_inner( L, p_stream );
99 }
100
101 static int vlclua_stream_read( lua_State *L )
102 {
103     int i_read;
104     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
105     int n = luaL_checkint( L, 2 );
106     uint8_t *p_read = malloc( n );
107     if( !p_read ) return vlclua_error( L );
108
109     i_read = stream_Read( *pp_stream, p_read, n );
110     if( i_read > 0 )
111         lua_pushlstring( L, (const char *)p_read, i_read );
112     else
113         lua_pushnil( L );
114     free( p_read );
115     return 1;
116 }
117
118 static int vlclua_stream_readline( lua_State *L )
119 {
120     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
121     char *psz_line = stream_ReadLine( *pp_stream );
122     if( psz_line )
123     {
124         lua_pushstring( L, psz_line );
125         free( psz_line );
126     }
127     else
128         lua_pushnil( L );
129     return 1;
130 }
131
132 static int vlclua_stream_add_filter( lua_State *L )
133 {
134     vlc_object_t *p_this = vlclua_get_this( L );
135
136     /* Make sure that we have 1 argument (+ 1 object) */
137     lua_settop( L, 2 );
138
139     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
140     if( !*pp_stream ) return vlclua_error( L );
141     const char *psz_filter = NULL;
142
143     if( lua_isstring( L, 2 ) )
144         psz_filter = lua_tostring( L, 2 );
145
146     if( !psz_filter || !*psz_filter )
147     {
148         msg_Dbg( p_this, "adding all automatic stream filters" );
149         while( true )
150         {
151             /* Add next automatic stream */
152             stream_t *p_filtered = stream_FilterNew( *pp_stream, NULL );
153             if( !p_filtered )
154                 break;
155             else
156             {
157                 msg_Dbg( p_this, "inserted an automatic stream filter" );
158                 *pp_stream = p_filtered;
159             }
160         }
161         luaL_getmetatable( L, "stream" );
162         lua_setmetatable( L, 1 );
163     }
164     else
165     {
166         /* Add a named filter */
167         stream_t *p_filter = stream_FilterNew( *pp_stream, psz_filter );
168         if( !p_filter )
169             msg_Dbg( p_this, "Unable to open requested stream filter '%s'",
170                      psz_filter );
171         else
172         {
173             *pp_stream = p_filter;
174             luaL_getmetatable( L, "stream" );
175             lua_setmetatable( L, 1 );
176         }
177     }
178
179     return 1;
180 }
181
182 static int vlclua_stream_delete( lua_State *L )
183 {
184     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
185     stream_Delete( *pp_stream );
186     return 0;
187 }
188
189 /*****************************************************************************
190  *
191  *****************************************************************************/
192 void luaopen_stream( lua_State *L )
193 {
194     lua_pushcfunction( L, vlclua_stream_new );
195     lua_setfield( L, -2, "stream" );
196     lua_pushcfunction( L, vlclua_memory_stream_new );
197     lua_setfield( L, -2, "memory_stream" );
198 }