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>
38 #include <vlc_playlist.h>
40 #include <lua.h> /* Low level lua C API */
41 #include <lauxlib.h> /* Higher level C API */
48 #include "../extension.h"
50 static const luaL_Reg vlclua_input_reg[];
51 static const luaL_Reg vlclua_input_item_reg[];
53 input_thread_t * vlclua_get_input_internal( lua_State *L )
55 extension_t *p_extension = vlclua_extension_get( L );
58 input_thread_t *p_input = p_extension->p_sys->p_input;
61 vlc_object_hold(p_input);
65 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
66 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
70 static int vlclua_input_info( lua_State *L )
72 input_thread_t * p_input = vlclua_get_input_internal( L );
75 if( !p_input ) return vlclua_error( L );
76 //vlc_mutex_lock( &input_GetItem(p_input)->lock );
77 i_cat = input_GetItem(p_input)->i_categories;
78 lua_createtable( L, 0, i_cat );
79 for( i = 0; i < i_cat; i++ )
81 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
82 int i_infos = p_category->i_infos;
84 lua_pushstring( L, p_category->psz_name );
85 lua_createtable( L, 0, i_infos );
86 for( j = 0; j < i_infos; j++ )
88 info_t *p_info = p_category->pp_infos[j];
89 lua_pushstring( L, p_info->psz_name );
90 lua_pushstring( L, p_info->psz_value );
91 lua_settable( L, -3 );
93 lua_settable( L, -3 );
95 vlc_object_release( p_input );
99 static int vlclua_input_is_playing( lua_State *L )
101 input_thread_t * p_input = vlclua_get_input_internal( L );
102 lua_pushboolean( L, !!p_input );
104 vlc_object_release( p_input );
108 static int vlclua_input_get_title( lua_State *L )
110 input_thread_t *p_input = vlclua_get_input_internal( L );
115 lua_pushstring( L, input_GetItem(p_input)->psz_name );
116 vlc_object_release( p_input );
121 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
132 psz_meta = input_item_GetName( p_item );
133 lua_pushstring( L, psz_meta );
134 lua_setfield( L, -2, "filename" );
137 #define PUSH_META( n, m ) \
138 psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
139 lua_pushstring( L, psz_meta ); \
140 lua_setfield( L, -2, m ); \
143 PUSH_META( Title, "title" );
144 PUSH_META( Artist, "artist" );
145 PUSH_META( Genre, "genre" );
146 PUSH_META( Copyright, "copyright" );
147 PUSH_META( Album, "album" );
148 PUSH_META( TrackNumber, "track_number" );
149 PUSH_META( Description, "description" );
150 PUSH_META( Rating, "rating" );
151 PUSH_META( Date, "date" );
152 PUSH_META( Setting, "setting" );
153 PUSH_META( URL, "url" );
154 PUSH_META( Language, "language" );
155 PUSH_META( NowPlaying, "now_playing" );
156 PUSH_META( Publisher, "publisher" );
157 PUSH_META( EncodedBy, "encoded_by" );
158 PUSH_META( ArtworkURL, "artwork_url" );
159 PUSH_META( TrackID, "track_id" );
163 vlc_mutex_lock(&p_item->lock);
164 if (p_item->p_meta) {
165 char ** names = vlc_meta_CopyExtraNames(p_item->p_meta);
166 for(int i = 0; names[i]; i++)
168 const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
169 lua_pushstring( L, meta );
170 lua_setfield( L, -2, names[i] );
175 vlc_mutex_unlock(&p_item->lock);
180 static int vlclua_input_metas( lua_State *L )
182 input_thread_t *p_input = vlclua_get_input_internal( L );
183 input_item_t *p_item = p_input && p_input->p
184 ? input_GetItem( p_input ) : NULL;
185 vlclua_input_metas_internal( L, p_item );
187 vlc_object_release( p_input );
191 static int vlclua_input_stats( lua_State *L )
193 input_thread_t *p_input = vlclua_get_input_internal( L );
194 input_item_t *p_item = p_input && p_input->p
195 ? input_GetItem( p_input ) : NULL;
199 vlc_mutex_lock( &p_item->p_stats->lock );
200 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
201 lua_setfield( L, -2, #n );
202 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
203 lua_setfield( L, -2, #n );
204 STATS_INT( read_bytes )
205 STATS_FLOAT( input_bitrate )
206 STATS_INT( demux_read_bytes )
207 STATS_FLOAT( demux_bitrate )
208 STATS_INT( demux_corrupted )
209 STATS_INT( demux_discontinuity )
210 STATS_INT( decoded_video )
211 STATS_INT( displayed_pictures )
212 STATS_INT( lost_pictures )
213 STATS_INT( decoded_audio )
214 STATS_INT( played_abuffers )
215 STATS_INT( lost_abuffers )
216 STATS_INT( sent_packets )
217 STATS_INT( sent_bytes )
218 STATS_FLOAT( send_bitrate )
221 vlc_mutex_unlock( &p_item->p_stats->lock );
224 vlc_object_release( p_input );
228 static int vlclua_input_add_subtitle( lua_State *L )
230 input_thread_t *p_input = vlclua_get_input_internal( L );
232 return luaL_error( L, "can't add subtitle: no current input" );
233 if( !lua_isstring( L, 1 ) )
234 return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
235 const char *psz_url = luaL_checkstring( L, 1 );
236 input_AddSubtitle( p_input, psz_url, false );
237 vlc_object_release( p_input );
241 /*****************************************************************************
243 *****************************************************************************/
245 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
247 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
248 input_item_t *p_item = *pp_item;
251 luaL_error( L, "script went completely foobar" );
256 /* Garbage collection of an input_item_t */
257 static int vlclua_input_item_delete( lua_State *L )
259 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
260 input_item_t *p_item = *pp_item;
263 return luaL_error( L, "script went completely foobar" );
266 vlc_gc_decref( p_item );
271 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
273 vlc_gc_incref( p_item );
274 input_item_t **pp = lua_newuserdata( L, sizeof( void* ) );
277 if( luaL_newmetatable( L, "input_item" ) )
280 luaL_register( L, NULL, vlclua_input_item_reg );
281 lua_setfield( L, -2, "__index" );
282 lua_pushcfunction( L, vlclua_input_item_delete );
283 lua_setfield( L, -2, "__gc" );
286 lua_setmetatable(L, -2);
291 static int vlclua_input_item_get_current( lua_State *L )
293 input_thread_t *p_input = vlclua_get_input_internal( L );
294 input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
298 if( p_input ) vlc_object_release( p_input );
302 vlclua_input_item_get( L, p_item );
304 if( p_input ) vlc_object_release( p_input );
308 static int vlclua_input_item_metas( lua_State *L )
310 vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
314 static int vlclua_input_item_is_preparsed( lua_State *L )
316 lua_pushboolean( L, input_item_IsPreparsed( vlclua_input_item_get_internal( L ) ) );
320 static int vlclua_input_item_set_meta( lua_State *L )
322 input_item_t *p_item = vlclua_input_item_get_internal( L );
323 lua_settop( L, 1 + 2 ); // two arguments
324 const char *psz_name = luaL_checkstring( L, 2 ),
325 *psz_value = luaL_checkstring( L, 3 );
327 #define META_TYPE( n, s ) { s, vlc_meta_ ## n },
330 const char *psz_name;
331 vlc_meta_type_t type;
332 } pp_meta_types[] = {
333 META_TYPE( Title, "title" )
334 META_TYPE( Artist, "artist" )
335 META_TYPE( Genre, "genre" )
336 META_TYPE( Copyright, "copyright" )
337 META_TYPE( Album, "album" )
338 META_TYPE( TrackNumber, "track_number" )
339 META_TYPE( Description, "description" )
340 META_TYPE( Rating, "rating" )
341 META_TYPE( Date, "date" )
342 META_TYPE( Setting, "setting" )
343 META_TYPE( URL, "url" )
344 META_TYPE( Language, "language" )
345 META_TYPE( NowPlaying, "now_playing" )
346 META_TYPE( Publisher, "publisher" )
347 META_TYPE( EncodedBy, "encoded_by" )
348 META_TYPE( ArtworkURL, "artwork_url" )
349 META_TYPE( TrackID, "track_id" )
353 vlc_meta_type_t type = vlc_meta_Title;
354 for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
356 if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
358 type = pp_meta_types[i].type;
359 input_item_SetMeta( p_item, type, psz_value );
364 vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
368 /*****************************************************************************
370 *****************************************************************************/
371 static const luaL_Reg vlclua_input_reg[] = {
372 { "info", vlclua_input_info },
373 { "is_playing", vlclua_input_is_playing },
374 { "get_title", vlclua_input_get_title },
375 { "metas", vlclua_input_metas },
376 { "item", vlclua_input_item_get_current },
377 { "stats", vlclua_input_stats },
378 { "add_subtitle", vlclua_input_add_subtitle },
382 void luaopen_input( lua_State *L )
385 luaL_register( L, NULL, vlclua_input_reg );
386 lua_setfield( L, -2, "input" );
389 static const luaL_Reg vlclua_input_item_reg[] = {
390 { "is_preparsed", vlclua_input_item_is_preparsed },
391 { "metas", vlclua_input_item_metas },
392 { "set_meta", vlclua_input_item_set_meta },
397 void luaopen_input_item( lua_State *L, input_item_t *item )
400 vlclua_input_item_get( L, item );
401 lua_setfield( L, -2, "item" );