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 static input_item_t* vlclua_input_item_get_internal( lua_State *L );
55 input_thread_t * vlclua_get_input_internal( lua_State *L )
57 extension_t *p_extension = vlclua_extension_get( L );
60 input_thread_t *p_input = p_extension->p_sys->p_input;
63 vlc_object_hold(p_input);
67 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
68 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
72 static int vlclua_input_item_info( lua_State *L )
74 input_item_t *p_item = vlclua_input_item_get_internal( L );
77 i_cat = p_item->i_categories;
78 lua_createtable( L, 0, i_cat );
79 for( i = 0; i < i_cat; i++ )
81 info_category_t *p_category = p_item->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 );
98 static int vlclua_input_is_playing( lua_State *L )
100 input_thread_t * p_input = vlclua_get_input_internal( L );
101 lua_pushboolean( L, !!p_input );
103 vlc_object_release( p_input );
107 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
118 psz_meta = input_item_GetName( p_item );
119 lua_pushstring( L, psz_meta );
120 lua_setfield( L, -2, "filename" );
123 #define PUSH_META( n, m ) \
124 psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
125 lua_pushstring( L, psz_meta ); \
126 lua_setfield( L, -2, m ); \
129 PUSH_META( Title, "title" );
130 PUSH_META( Artist, "artist" );
131 PUSH_META( Genre, "genre" );
132 PUSH_META( Copyright, "copyright" );
133 PUSH_META( Album, "album" );
134 PUSH_META( TrackNumber, "track_number" );
135 PUSH_META( Description, "description" );
136 PUSH_META( Rating, "rating" );
137 PUSH_META( Date, "date" );
138 PUSH_META( Setting, "setting" );
139 PUSH_META( URL, "url" );
140 PUSH_META( Language, "language" );
141 PUSH_META( NowPlaying, "now_playing" );
142 PUSH_META( Publisher, "publisher" );
143 PUSH_META( EncodedBy, "encoded_by" );
144 PUSH_META( ArtworkURL, "artwork_url" );
145 PUSH_META( TrackID, "track_id" );
149 vlc_mutex_lock(&p_item->lock);
150 if (p_item->p_meta) {
151 char ** names = vlc_meta_CopyExtraNames(p_item->p_meta);
152 for(int i = 0; names[i]; i++)
154 const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
155 lua_pushstring( L, meta );
156 lua_setfield( L, -2, names[i] );
161 vlc_mutex_unlock(&p_item->lock);
166 static int vlclua_input_item_stats( lua_State *L )
168 input_item_t *p_item = vlclua_input_item_get_internal( L );
172 vlc_mutex_lock( &p_item->p_stats->lock );
173 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
174 lua_setfield( L, -2, #n );
175 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
176 lua_setfield( L, -2, #n );
177 STATS_INT( read_packets )
178 STATS_INT( read_bytes )
179 STATS_FLOAT( input_bitrate )
180 STATS_FLOAT( average_input_bitrate )
181 STATS_INT( demux_read_packets )
182 STATS_INT( demux_read_bytes )
183 STATS_FLOAT( demux_bitrate )
184 STATS_FLOAT( average_demux_bitrate )
185 STATS_INT( demux_corrupted )
186 STATS_INT( demux_discontinuity )
187 STATS_INT( decoded_audio )
188 STATS_INT( decoded_video )
189 STATS_INT( displayed_pictures )
190 STATS_INT( lost_pictures )
191 STATS_INT( sent_packets )
192 STATS_INT( sent_bytes )
193 STATS_FLOAT( send_bitrate )
194 STATS_INT( played_abuffers )
195 STATS_INT( lost_abuffers )
198 vlc_mutex_unlock( &p_item->p_stats->lock );
203 static int vlclua_input_add_subtitle( lua_State *L )
205 input_thread_t *p_input = vlclua_get_input_internal( L );
207 return luaL_error( L, "can't add subtitle: no current input" );
208 if( !lua_isstring( L, 1 ) )
209 return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
210 const char *psz_url = luaL_checkstring( L, 1 );
211 input_AddSubtitle( p_input, psz_url, false );
212 vlc_object_release( p_input );
216 /*****************************************************************************
218 *****************************************************************************/
220 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
222 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
223 input_item_t *p_item = *pp_item;
226 luaL_error( L, "script went completely foobar" );
231 /* Garbage collection of an input_item_t */
232 static int vlclua_input_item_delete( lua_State *L )
234 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
235 input_item_t *p_item = *pp_item;
238 return luaL_error( L, "script went completely foobar" );
241 vlc_gc_decref( p_item );
246 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
248 vlc_gc_incref( p_item );
249 input_item_t **pp = lua_newuserdata( L, sizeof( input_item_t* ) );
252 if( luaL_newmetatable( L, "input_item" ) )
255 luaL_register( L, NULL, vlclua_input_item_reg );
256 lua_setfield( L, -2, "__index" );
257 lua_pushcfunction( L, vlclua_input_item_delete );
258 lua_setfield( L, -2, "__gc" );
261 lua_setmetatable(L, -2);
266 static int vlclua_input_item_get_current( lua_State *L )
268 input_thread_t *p_input = vlclua_get_input_internal( L );
269 input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
273 if( p_input ) vlc_object_release( p_input );
277 vlclua_input_item_get( L, p_item );
279 if( p_input ) vlc_object_release( p_input );
283 static int vlclua_input_item_metas( lua_State *L )
285 vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
289 static int vlclua_input_item_is_preparsed( lua_State *L )
291 lua_pushboolean( L, input_item_IsPreparsed( vlclua_input_item_get_internal( L ) ) );
295 static int vlclua_input_item_uri( lua_State *L )
297 lua_pushstring( L, input_item_GetURI( vlclua_input_item_get_internal( L ) ) );
301 static int vlclua_input_item_name( lua_State *L )
303 lua_pushstring( L, input_item_GetName( vlclua_input_item_get_internal( L ) ) );
307 static int vlclua_input_item_duration( lua_State *L )
309 mtime_t duration = input_item_GetDuration( vlclua_input_item_get_internal( L ) );
310 lua_pushnumber( L, ((double)duration)/1000000. );
314 static int vlclua_input_item_set_meta( lua_State *L )
316 input_item_t *p_item = vlclua_input_item_get_internal( L );
317 lua_settop( L, 1 + 2 ); // two arguments
318 const char *psz_name = luaL_checkstring( L, 2 ),
319 *psz_value = luaL_checkstring( L, 3 );
321 #define META_TYPE( n, s ) { s, vlc_meta_ ## n },
324 const char *psz_name;
325 vlc_meta_type_t type;
326 } pp_meta_types[] = {
327 META_TYPE( Title, "title" )
328 META_TYPE( Artist, "artist" )
329 META_TYPE( Genre, "genre" )
330 META_TYPE( Copyright, "copyright" )
331 META_TYPE( Album, "album" )
332 META_TYPE( TrackNumber, "track_number" )
333 META_TYPE( Description, "description" )
334 META_TYPE( Rating, "rating" )
335 META_TYPE( Date, "date" )
336 META_TYPE( Setting, "setting" )
337 META_TYPE( URL, "url" )
338 META_TYPE( Language, "language" )
339 META_TYPE( NowPlaying, "now_playing" )
340 META_TYPE( Publisher, "publisher" )
341 META_TYPE( EncodedBy, "encoded_by" )
342 META_TYPE( ArtworkURL, "artwork_url" )
343 META_TYPE( TrackID, "track_id" )
347 vlc_meta_type_t type = vlc_meta_Title;
348 for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
350 if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
352 type = pp_meta_types[i].type;
353 input_item_SetMeta( p_item, type, psz_value );
358 vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
362 /*****************************************************************************
364 *****************************************************************************/
365 static const luaL_Reg vlclua_input_reg[] = {
366 { "is_playing", vlclua_input_is_playing },
367 { "item", vlclua_input_item_get_current },
368 { "add_subtitle", vlclua_input_add_subtitle },
372 void luaopen_input( lua_State *L )
375 luaL_register( L, NULL, vlclua_input_reg );
376 lua_setfield( L, -2, "input" );
379 static const luaL_Reg vlclua_input_item_reg[] = {
380 { "is_preparsed", vlclua_input_item_is_preparsed },
381 { "metas", vlclua_input_item_metas },
382 { "set_meta", vlclua_input_item_set_meta },
383 { "uri", vlclua_input_item_uri },
384 { "name", vlclua_input_item_name },
385 { "duration", vlclua_input_item_duration },
386 { "stats", vlclua_input_item_stats },
387 { "info", vlclua_input_item_info },
392 void luaopen_input_item( lua_State *L, input_item_t *item )
395 vlclua_input_item_get( L, item );
396 lua_setfield( L, -2, "item" );