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 */
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 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
56 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
57 vlclua_release_playlist_internal( p_playlist );
61 static int vlclua_input_info( lua_State *L )
63 input_thread_t * p_input = vlclua_get_input_internal( L );
66 if( !p_input ) return vlclua_error( L );
67 //vlc_mutex_lock( &input_GetItem(p_input)->lock );
68 i_cat = input_GetItem(p_input)->i_categories;
69 lua_createtable( L, 0, i_cat );
70 for( i = 0; i < i_cat; i++ )
72 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
73 int i_infos = p_category->i_infos;
75 lua_pushstring( L, p_category->psz_name );
76 lua_createtable( L, 0, i_infos );
77 for( j = 0; j < i_infos; j++ )
79 info_t *p_info = p_category->pp_infos[j];
80 lua_pushstring( L, p_info->psz_name );
81 lua_pushstring( L, p_info->psz_value );
82 lua_settable( L, -3 );
84 lua_settable( L, -3 );
86 vlc_object_release( p_input );
90 static int vlclua_input_is_playing( lua_State *L )
92 input_thread_t * p_input = vlclua_get_input_internal( L );
93 lua_pushboolean( L, !!p_input );
95 vlc_object_release( p_input );
99 static int vlclua_input_get_title( lua_State *L )
101 input_thread_t *p_input = vlclua_get_input_internal( L );
106 lua_pushstring( L, input_GetItem(p_input)->psz_name );
107 vlc_object_release( p_input );
112 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
123 psz_meta = input_item_GetName( p_item );
124 lua_pushstring( L, psz_meta );
125 lua_setfield( L, -2, "filename" );
128 #define PUSH_META( n, m ) \
129 psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
130 lua_pushstring( L, psz_meta ); \
131 lua_setfield( L, -2, m ); \
134 PUSH_META( Title, "title" );
135 PUSH_META( Artist, "artist" );
136 PUSH_META( Genre, "genre" );
137 PUSH_META( Copyright, "copyright" );
138 PUSH_META( Album, "album" );
139 PUSH_META( TrackNumber, "track_number" );
140 PUSH_META( Description, "description" );
141 PUSH_META( Rating, "rating" );
142 PUSH_META( Date, "date" );
143 PUSH_META( Setting, "setting" );
144 PUSH_META( URL, "url" );
145 PUSH_META( Language, "language" );
146 PUSH_META( NowPlaying, "now_playing" );
147 PUSH_META( Publisher, "publisher" );
148 PUSH_META( EncodedBy, "encoded_by" );
149 PUSH_META( ArtworkURL, "artwork_url" );
150 PUSH_META( TrackID, "track_id" );
154 vlc_mutex_lock(&p_item->lock);
155 if (p_item->p_meta) {
156 char ** names = vlc_meta_CopyExtraNames(p_item->p_meta);
157 for(int i = 0; names[i]; i++)
159 const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
160 lua_pushstring( L, meta );
161 lua_setfield( L, -2, names[i] );
162 printf("setting %s=%s\n", names[i], meta);
167 vlc_mutex_unlock(&p_item->lock);
172 static int vlclua_input_metas( lua_State *L )
174 input_thread_t *p_input = vlclua_get_input_internal( L );
175 input_item_t *p_item = p_input && p_input->p
176 ? input_GetItem( p_input ) : NULL;
177 vlclua_input_metas_internal( L, p_item );
179 vlc_object_release( p_input );
183 static int vlclua_input_stats( lua_State *L )
185 input_thread_t *p_input = vlclua_get_input_internal( L );
186 input_item_t *p_item = p_input && p_input->p
187 ? input_GetItem( p_input ) : NULL;
191 vlc_mutex_lock( &p_item->p_stats->lock );
192 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
193 lua_setfield( L, -2, #n );
194 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
195 lua_setfield( L, -2, #n );
196 STATS_INT( read_bytes )
197 STATS_FLOAT( input_bitrate )
198 STATS_INT( demux_read_bytes )
199 STATS_FLOAT( demux_bitrate )
200 STATS_INT( demux_corrupted )
201 STATS_INT( demux_discontinuity )
202 STATS_INT( decoded_video )
203 STATS_INT( displayed_pictures )
204 STATS_INT( lost_pictures )
205 STATS_INT( decoded_audio )
206 STATS_INT( played_abuffers )
207 STATS_INT( lost_abuffers )
208 STATS_INT( sent_packets )
209 STATS_INT( sent_bytes )
210 STATS_FLOAT( send_bitrate )
213 vlc_mutex_unlock( &p_item->p_stats->lock );
216 vlc_object_release( p_input );
220 static int vlclua_input_add_subtitle( lua_State *L )
222 input_thread_t *p_input = vlclua_get_input_internal( L );
224 return luaL_error( L, "can't add subtitle: no current input" );
225 if( !lua_isstring( L, 1 ) )
226 return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
227 const char *psz_url = luaL_checkstring( L, 1 );
228 input_AddSubtitle( p_input, psz_url, false );
229 vlc_object_release( p_input );
233 /*****************************************************************************
235 *****************************************************************************/
237 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
239 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
240 input_item_t *p_item = *pp_item;
243 luaL_error( L, "script went completely foobar" );
248 /* Garbage collection of an input_item_t */
249 static int vlclua_input_item_delete( lua_State *L )
251 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
252 input_item_t *p_item = *pp_item;
255 return luaL_error( L, "script went completely foobar" );
258 vlc_gc_decref( p_item );
263 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
265 vlc_gc_incref( p_item );
266 input_item_t **pp = lua_newuserdata( L, sizeof( void* ) );
269 if( luaL_newmetatable( L, "input_item" ) )
272 luaL_register( L, NULL, vlclua_input_item_reg );
273 lua_setfield( L, -2, "__index" );
274 lua_pushcfunction( L, vlclua_input_item_delete );
275 lua_setfield( L, -2, "__gc" );
278 lua_setmetatable(L, -2);
283 static int vlclua_input_item_get_current( lua_State *L )
285 input_thread_t *p_input = vlclua_get_input_internal( L );
286 input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
290 if( p_input ) vlc_object_release( p_input );
294 vlclua_input_item_get( L, p_item );
296 if( p_input ) vlc_object_release( p_input );
300 static int vlclua_input_item_metas( lua_State *L )
302 vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
306 static int vlclua_input_item_set_meta( lua_State *L )
308 input_item_t *p_item = vlclua_input_item_get_internal( L );
309 lua_settop( L, 1 + 2 ); // two arguments
310 const char *psz_name = luaL_checkstring( L, 2 ),
311 *psz_value = luaL_checkstring( L, 3 );
313 #define META_TYPE( n ) { #n, vlc_meta_ ## n }
316 const char *psz_name;
317 vlc_meta_type_t type;
318 } pp_meta_types[] = {
322 META_TYPE( Copyright ),
324 META_TYPE( TrackNumber ),
325 META_TYPE( Description ),
328 META_TYPE( Setting ),
330 META_TYPE( Language ),
331 META_TYPE( NowPlaying ),
332 META_TYPE( Publisher ),
333 META_TYPE( EncodedBy ),
334 META_TYPE( ArtworkURL ),
335 META_TYPE( TrackID ),
339 vlc_meta_type_t type = vlc_meta_Title;
341 for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
343 if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
345 type = pp_meta_types[i].type;
351 vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
355 input_item_SetMeta( p_item, type, psz_value );
359 /*****************************************************************************
361 *****************************************************************************/
362 static const luaL_Reg vlclua_input_reg[] = {
363 { "info", vlclua_input_info },
364 { "is_playing", vlclua_input_is_playing },
365 { "get_title", vlclua_input_get_title },
366 { "metas", vlclua_input_metas },
367 { "item", vlclua_input_item_get_current },
368 { "stats", vlclua_input_stats },
369 { "add_subtitle", vlclua_input_add_subtitle },
373 void luaopen_input( lua_State *L )
376 luaL_register( L, NULL, vlclua_input_reg );
377 lua_setfield( L, -2, "input" );
380 static const luaL_Reg vlclua_input_item_reg[] = {
381 { "metas", vlclua_input_item_metas },
382 { "set_meta", vlclua_input_item_set_meta },
387 void luaopen_input_item( lua_State *L, input_item_t *item )
390 vlclua_input_item_get( L, item );
391 lua_setfield( L, -2, "item" );