]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/stream.c
Kill warning.
[vlc] / modules / misc / 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 <lua.h>        /* Low level lua C API */
42 #include <lauxlib.h>    /* Higher level C API */
43
44 #include "../vlc.h"
45 #include "../libs.h"
46
47 /*****************************************************************************
48  * Stream handling
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 );
54
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     { NULL, NULL }
60 };
61
62 static int vlclua_stream_new_inner( lua_State *L, stream_t *p_stream )
63 {
64     if( !p_stream )
65         return luaL_error( L, "Error when opening stream" );
66
67     stream_t **pp_stream = lua_newuserdata( L, sizeof( stream_t * ) );
68     *pp_stream = p_stream;
69
70     if( luaL_newmetatable( L, "stream" ) )
71     {
72         lua_newtable( L );
73         luaL_register( L, NULL, vlclua_stream_reg );
74         lua_setfield( L, -2, "__index" );
75         lua_pushcfunction( L, vlclua_stream_delete );
76         lua_setfield( L, -2, "__gc" );
77     }
78
79     lua_setmetatable( L, -2 );
80     return 1;
81 }
82
83 static int vlclua_stream_new( lua_State *L )
84 {
85     vlc_object_t * p_this = vlclua_get_this( L );
86     const char * psz_url = luaL_checkstring( L, 1 );
87     stream_t *p_stream = stream_UrlNew( p_this, psz_url );
88     return vlclua_stream_new_inner( L, p_stream );
89 }
90
91 static int vlclua_memory_stream_new( lua_State *L )
92 {
93     vlc_object_t * p_this = vlclua_get_this( L );
94     /* FIXME: duplicating the whole buffer is suboptimal. Keeping a reference to the string so that it doesn't get garbage collected would be better */
95     char * psz_content = strdup( luaL_checkstring( L, 1 ) );
96     stream_t *p_stream = stream_MemoryNew( p_this, (uint8_t *)psz_content, strlen( psz_content ), false );
97     return vlclua_stream_new_inner( L, p_stream );
98 }
99
100 static int vlclua_stream_read( lua_State *L )
101 {
102     int i_read;
103     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
104     int n = luaL_checkint( L, 2 );
105     uint8_t *p_read = malloc( n );
106     if( !p_read ) return vlclua_error( L );
107     i_read = stream_Read( *pp_stream, p_read, n );
108     lua_pushlstring( L, (const char *)p_read, i_read );
109     free( p_read );
110     return 1;
111 }
112
113 static int vlclua_stream_readline( lua_State *L )
114 {
115     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
116     char *psz_line = stream_ReadLine( *pp_stream );
117     if( psz_line )
118     {
119         lua_pushstring( L, psz_line );
120         free( psz_line );
121     }
122     else
123         lua_pushnil( L );
124     return 1;
125 }
126
127 static int vlclua_stream_add_filter( lua_State *L )
128 {
129     vlc_object_t *p_this = vlclua_get_this( L );
130
131     /* Make sure that we have 1 argument (+ 1 object) */
132     lua_settop( L, 2 );
133
134     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
135     if( !*pp_stream ) return vlclua_error( L );
136     const char *psz_filter = NULL;
137
138     if( lua_isstring( L, 2 ) )
139         psz_filter = lua_tostring( L, 2 );
140
141     if( !psz_filter || !*psz_filter )
142     {
143         msg_Dbg( p_this, "adding all automatic stream filters" );
144         while( true )
145         {
146             /* Add next automatic stream */
147             stream_t *p_filtered = stream_FilterNew( *pp_stream, NULL );
148             if( !p_filtered )
149                 break;
150             else
151             {
152                 msg_Dbg( p_this, "inserted an automatic stream filter" );
153                 *pp_stream = p_filtered;
154             }
155         }
156         luaL_getmetatable( L, "stream" );
157         lua_setmetatable( L, 1 );
158     }
159     else
160     {
161         /* Add a named filter */
162         stream_t *p_filter = stream_FilterNew( *pp_stream, psz_filter );
163         if( !p_filter )
164             msg_Dbg( p_this, "Unable to open requested stream filter '%s'",
165                      psz_filter );
166         else
167         {
168             *pp_stream = p_filter;
169             luaL_getmetatable( L, "stream" );
170             lua_setmetatable( L, 1 );
171         }
172     }
173
174     return 1;
175 }
176
177 static int vlclua_stream_delete( lua_State *L )
178 {
179     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
180     stream_Delete( *pp_stream );
181     return 0;
182 }
183
184 /*****************************************************************************
185  *
186  *****************************************************************************/
187 void luaopen_stream( lua_State *L )
188 {
189     lua_pushcfunction( L, vlclua_stream_new );
190     lua_setfield( L, -2, "stream" );
191     lua_pushcfunction( L, vlclua_memory_stream_new );
192     lua_setfield( L, -2, "memory_stream" );
193 }