1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
37 #include <vlc_charset.h>
39 #include <vlc_playlist.h>
41 #include <lua.h> /* Low level lua C API */
42 #include <lauxlib.h> /* Higher level C API */
49 static const luaL_Reg vlclua_input_reg[];
50 static const luaL_Reg vlclua_input_item_reg[];
52 input_thread_t * vlclua_get_input_internal( lua_State *L )
54 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
55 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
56 vlclua_release_playlist_internal( p_playlist );
60 static int vlclua_input_info( lua_State *L )
62 input_thread_t * p_input = vlclua_get_input_internal( L );
65 if( !p_input ) return vlclua_error( L );
66 //vlc_mutex_lock( &input_GetItem(p_input)->lock );
67 i_cat = input_GetItem(p_input)->i_categories;
68 lua_createtable( L, 0, i_cat );
69 for( i = 0; i < i_cat; i++ )
71 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
72 int i_infos = p_category->i_infos;
74 lua_pushstring( L, p_category->psz_name );
75 lua_createtable( L, 0, i_infos );
76 for( j = 0; j < i_infos; j++ )
78 info_t *p_info = p_category->pp_infos[j];
79 lua_pushstring( L, p_info->psz_name );
80 lua_pushstring( L, p_info->psz_value );
81 lua_settable( L, -3 );
83 lua_settable( L, -3 );
85 vlc_object_release( p_input );
89 static int vlclua_input_is_playing( lua_State *L )
91 input_thread_t * p_input = vlclua_get_input_internal( L );
92 lua_pushboolean( L, !!p_input );
94 vlc_object_release( p_input );
98 static int vlclua_input_get_title( lua_State *L )
100 input_thread_t *p_input = vlclua_get_input_internal( L );
105 lua_pushstring( L, input_GetItem(p_input)->psz_name );
106 vlc_object_release( p_input );
111 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
122 #define PUSH_META( n, m ) \
123 psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
124 lua_pushstring( L, psz_meta ); \
125 lua_setfield( L, -2, m ); \
128 PUSH_META( Title, "title" );
129 PUSH_META( Artist, "artist" );
130 PUSH_META( Genre, "genre" );
131 PUSH_META( Copyright, "copyright" );
132 PUSH_META( Album, "album" );
133 PUSH_META( TrackNumber, "track_number" );
134 PUSH_META( Description, "description" );
135 PUSH_META( Rating, "rating" );
136 PUSH_META( Date, "date" );
137 PUSH_META( Setting, "setting" );
138 PUSH_META( URL, "url" );
139 PUSH_META( Language, "language" );
140 PUSH_META( NowPlaying, "now_playing" );
141 PUSH_META( Publisher, "publisher" );
142 PUSH_META( EncodedBy, "encoded_by" );
143 PUSH_META( ArtworkURL, "artwork_url" );
144 PUSH_META( TrackID, "track_id" );
151 static int vlclua_input_metas( lua_State *L )
153 input_thread_t *p_input = vlclua_get_input_internal( L );
154 input_item_t *p_item = p_input && p_input->p
155 ? input_GetItem( p_input ) : NULL;
156 vlclua_input_metas_internal( L, p_item );
158 vlc_object_release( p_input );
162 static int vlclua_input_stats( lua_State *L )
164 input_thread_t *p_input = vlclua_get_input_internal( L );
165 input_item_t *p_item = p_input && p_input->p
166 ? input_GetItem( p_input ) : NULL;
170 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
171 lua_setfield( L, -2, #n );
172 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
173 lua_setfield( L, -2, #n );
174 STATS_INT( read_bytes )
175 STATS_FLOAT( input_bitrate )
176 STATS_INT( demux_read_bytes )
177 STATS_FLOAT( demux_bitrate )
178 STATS_INT( decoded_video )
179 STATS_INT( displayed_pictures )
180 STATS_INT( lost_pictures )
181 STATS_INT( decoded_audio )
182 STATS_INT( played_abuffers )
183 STATS_INT( lost_abuffers )
184 STATS_INT( sent_packets )
185 STATS_INT( sent_bytes )
186 STATS_FLOAT( send_bitrate )
191 vlc_object_release( p_input );
195 static int vlclua_input_add_subtitle( lua_State *L )
197 input_thread_t *p_input = vlclua_get_input_internal( L );
199 return luaL_error( L, "can't add subtitle: no current input" );
200 if( !lua_isstring( L, 1 ) )
201 return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
202 const char *psz_url = luaL_checkstring( L, 1 );
203 input_AddSubtitle( p_input, psz_url, false );
204 vlc_object_release( p_input );
208 /*****************************************************************************
210 *****************************************************************************/
212 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
214 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
215 input_item_t *p_item = *pp_item;
218 luaL_error( L, "script went completely foobar" );
223 /* Garbage collection of an input_item_t */
224 static int vlclua_input_item_delete( lua_State *L )
226 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
227 input_item_t *p_item = *pp_item;
230 return luaL_error( L, "script went completely foobar" );
233 vlc_gc_decref( p_item );
238 static int vlclua_input_item_get_current( lua_State *L )
240 input_thread_t *p_input = vlclua_get_input_internal( L );
241 input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
245 if( p_input ) vlc_object_release( p_input );
249 vlc_gc_incref( p_item );
251 input_item_t **pp = lua_newuserdata( L, sizeof( void* ) );
254 if( luaL_newmetatable( L, "input_item" ) )
257 luaL_register( L, NULL, vlclua_input_item_reg );
258 lua_setfield( L, -2, "__index" );
259 lua_pushcfunction( L, vlclua_input_item_delete );
260 lua_setfield( L, -2, "__gc" );
263 lua_setmetatable( L, -2 );
265 if( p_input ) vlc_object_release( p_input );
269 static int vlclua_input_item_metas( lua_State *L )
271 vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
275 static int vlclua_input_item_set_meta( lua_State *L )
277 input_item_t *p_item = vlclua_input_item_get_internal( L );
278 lua_settop( L, 1 + 2 ); // two arguments
279 const char *psz_name = luaL_checkstring( L, 2 ),
280 *psz_value = luaL_checkstring( L, 3 );
282 #define META_TYPE( n ) { #n, vlc_meta_ ## n }
285 const char *psz_name;
286 vlc_meta_type_t type;
287 } pp_meta_types[] = {
291 META_TYPE( Copyright ),
293 META_TYPE( TrackNumber ),
294 META_TYPE( Description ),
297 META_TYPE( Setting ),
299 META_TYPE( Language ),
300 META_TYPE( NowPlaying ),
301 META_TYPE( Publisher ),
302 META_TYPE( EncodedBy ),
303 META_TYPE( ArtworkURL ),
304 META_TYPE( TrackID ),
308 vlc_meta_type_t type = vlc_meta_Title;
310 for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
312 if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
314 type = pp_meta_types[i].type;
320 return luaL_error( L, "unknown meta type '%s'", psz_name );
322 input_item_SetMeta( p_item, type, psz_value );
327 /*****************************************************************************
329 *****************************************************************************/
330 static const luaL_Reg vlclua_input_reg[] = {
331 { "info", vlclua_input_info },
332 { "is_playing", vlclua_input_is_playing },
333 { "get_title", vlclua_input_get_title },
334 { "metas", vlclua_input_metas },
335 { "item", vlclua_input_item_get_current },
336 { "stats", vlclua_input_stats },
337 { "add_subtitle", vlclua_input_add_subtitle },
341 void luaopen_input( lua_State *L )
344 luaL_register( L, NULL, vlclua_input_reg );
345 lua_setfield( L, -2, "input" );
348 static const luaL_Reg vlclua_input_item_reg[] = {
349 { "metas", vlclua_input_item_metas },
350 { "set_meta", vlclua_input_item_set_meta },