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>
46 #include "../extension.h"
48 static const luaL_Reg vlclua_input_reg[];
49 static const luaL_Reg vlclua_input_item_reg[];
51 static input_item_t* vlclua_input_item_get_internal( lua_State *L );
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_item_info( lua_State *L )
72 input_item_t *p_item = vlclua_input_item_get_internal( L );
75 i_cat = p_item->i_categories;
76 lua_createtable( L, 0, i_cat );
77 for( i = 0; i < i_cat; i++ )
79 info_category_t *p_category = p_item->pp_categories[i];
80 int i_infos = p_category->i_infos;
82 lua_pushstring( L, p_category->psz_name );
83 lua_createtable( L, 0, i_infos );
84 for( j = 0; j < i_infos; j++ )
86 info_t *p_info = p_category->pp_infos[j];
87 lua_pushstring( L, p_info->psz_name );
88 lua_pushstring( L, p_info->psz_value );
89 lua_settable( L, -3 );
91 lua_settable( L, -3 );
96 static int vlclua_input_is_playing( lua_State *L )
98 input_thread_t * p_input = vlclua_get_input_internal( L );
99 lua_pushboolean( L, !!p_input );
101 vlc_object_release( p_input );
105 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
115 const char *psz_meta;
117 psz_name = input_item_GetName( p_item );
118 lua_pushstring( L, psz_name );
119 lua_setfield( L, -2, "filename" );
122 #define PUSH_META( n, m ) \
123 psz_meta = vlc_meta_Get( p_item->p_meta, vlc_meta_ ## n ); \
124 lua_pushstring( L, psz_meta ); \
125 lua_setfield( L, -2, m )
127 vlc_mutex_lock(&p_item->lock);
131 PUSH_META( Title, "title" );
132 PUSH_META( Artist, "artist" );
133 PUSH_META( Genre, "genre" );
134 PUSH_META( Copyright, "copyright" );
135 PUSH_META( Album, "album" );
136 PUSH_META( TrackNumber, "track_number" );
137 PUSH_META( Description, "description" );
138 PUSH_META( Rating, "rating" );
139 PUSH_META( Date, "date" );
140 PUSH_META( Setting, "setting" );
141 PUSH_META( URL, "url" );
142 PUSH_META( Language, "language" );
143 PUSH_META( NowPlaying, "now_playing" );
144 PUSH_META( Publisher, "publisher" );
145 PUSH_META( EncodedBy, "encoded_by" );
146 PUSH_META( ArtworkURL, "artwork_url" );
147 PUSH_META( TrackID, "track_id" );
148 PUSH_META( TrackTotal, "track_total" );
152 char **names = vlc_meta_CopyExtraNames(p_item->p_meta);
153 for(int i = 0; names[i]; i++)
155 const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
156 lua_pushstring( L, meta );
157 lua_setfield( L, -2, names[i] );
162 vlc_mutex_unlock(&p_item->lock);
167 static int vlclua_input_item_stats( lua_State *L )
169 input_item_t *p_item = vlclua_input_item_get_internal( L );
173 vlc_mutex_lock( &p_item->p_stats->lock );
174 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
175 lua_setfield( L, -2, #n );
176 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
177 lua_setfield( L, -2, #n );
178 STATS_INT( read_packets )
179 STATS_INT( read_bytes )
180 STATS_FLOAT( input_bitrate )
181 STATS_FLOAT( average_input_bitrate )
182 STATS_INT( demux_read_packets )
183 STATS_INT( demux_read_bytes )
184 STATS_FLOAT( demux_bitrate )
185 STATS_FLOAT( average_demux_bitrate )
186 STATS_INT( demux_corrupted )
187 STATS_INT( demux_discontinuity )
188 STATS_INT( decoded_audio )
189 STATS_INT( decoded_video )
190 STATS_INT( displayed_pictures )
191 STATS_INT( lost_pictures )
192 STATS_INT( sent_packets )
193 STATS_INT( sent_bytes )
194 STATS_FLOAT( send_bitrate )
195 STATS_INT( played_abuffers )
196 STATS_INT( lost_abuffers )
199 vlc_mutex_unlock( &p_item->p_stats->lock );
204 static int vlclua_input_add_subtitle( lua_State *L )
206 input_thread_t *p_input = vlclua_get_input_internal( L );
208 return luaL_error( L, "can't add subtitle: no current input" );
209 if( !lua_isstring( L, 1 ) )
210 return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
211 const char *psz_url = luaL_checkstring( L, 1 );
212 input_AddSubtitle( p_input, psz_url, false );
213 vlc_object_release( p_input );
217 /*****************************************************************************
219 *****************************************************************************/
221 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
223 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
224 input_item_t *p_item = *pp_item;
227 luaL_error( L, "script went completely foobar" );
232 /* Garbage collection of an input_item_t */
233 static int vlclua_input_item_delete( lua_State *L )
235 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
236 input_item_t *p_item = *pp_item;
239 return luaL_error( L, "script went completely foobar" );
242 vlc_gc_decref( p_item );
247 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
249 vlc_gc_incref( p_item );
250 input_item_t **pp = lua_newuserdata( L, sizeof( input_item_t* ) );
253 if( luaL_newmetatable( L, "input_item" ) )
256 luaL_register( L, NULL, vlclua_input_item_reg );
257 lua_setfield( L, -2, "__index" );
258 lua_pushcfunction( L, vlclua_input_item_delete );
259 lua_setfield( L, -2, "__gc" );
262 lua_setmetatable(L, -2);
267 static int vlclua_input_item_get_current( lua_State *L )
269 input_thread_t *p_input = vlclua_get_input_internal( L );
270 input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
274 if( p_input ) vlc_object_release( p_input );
278 vlclua_input_item_get( L, p_item );
280 if( p_input ) vlc_object_release( p_input );
284 static int vlclua_input_item_metas( lua_State *L )
286 vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
290 static int vlclua_input_item_is_preparsed( lua_State *L )
292 lua_pushboolean( L, input_item_IsPreparsed( vlclua_input_item_get_internal( L ) ) );
296 static int vlclua_input_item_uri( lua_State *L )
298 lua_pushstring( L, input_item_GetURI( vlclua_input_item_get_internal( L ) ) );
302 static int vlclua_input_item_name( lua_State *L )
304 lua_pushstring( L, input_item_GetName( vlclua_input_item_get_internal( L ) ) );
308 static int vlclua_input_item_duration( lua_State *L )
310 mtime_t duration = input_item_GetDuration( vlclua_input_item_get_internal( L ) );
311 lua_pushnumber( L, ((double)duration)/1000000. );
315 static int vlclua_input_item_set_meta( lua_State *L )
317 input_item_t *p_item = vlclua_input_item_get_internal( L );
318 lua_settop( L, 1 + 2 ); // two arguments
319 const char *psz_name = luaL_checkstring( L, 2 ),
320 *psz_value = luaL_checkstring( L, 3 );
322 #define META_TYPE( n, s ) { s, vlc_meta_ ## n },
325 const char psz_name[15];
327 } pp_meta_types[] = {
328 META_TYPE( Title, "title" )
329 META_TYPE( Artist, "artist" )
330 META_TYPE( Genre, "genre" )
331 META_TYPE( Copyright, "copyright" )
332 META_TYPE( Album, "album" )
333 META_TYPE( TrackNumber, "track_number" )
334 META_TYPE( Description, "description" )
335 META_TYPE( Rating, "rating" )
336 META_TYPE( Date, "date" )
337 META_TYPE( Setting, "setting" )
338 META_TYPE( URL, "url" )
339 META_TYPE( Language, "language" )
340 META_TYPE( NowPlaying, "now_playing" )
341 META_TYPE( Publisher, "publisher" )
342 META_TYPE( EncodedBy, "encoded_by" )
343 META_TYPE( ArtworkURL, "artwork_url" )
344 META_TYPE( TrackID, "track_id" )
345 META_TYPE( TrackTotal, "track_total" )
349 static_assert( sizeof(pp_meta_types)
350 == VLC_META_TYPE_COUNT * sizeof(pp_meta_types[0]),
351 "Inconsistent meta data types" );
352 vlc_meta_type_t type = vlc_meta_Title;
353 for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
355 if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
357 type = pp_meta_types[i].type;
358 input_item_SetMeta( p_item, type, psz_value );
363 vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
367 /*****************************************************************************
369 *****************************************************************************/
370 static const luaL_Reg vlclua_input_reg[] = {
371 { "is_playing", vlclua_input_is_playing },
372 { "item", vlclua_input_item_get_current },
373 { "add_subtitle", vlclua_input_add_subtitle },
377 void luaopen_input( lua_State *L )
380 luaL_register( L, NULL, vlclua_input_reg );
381 lua_setfield( L, -2, "input" );
384 static const luaL_Reg vlclua_input_item_reg[] = {
385 { "is_preparsed", vlclua_input_item_is_preparsed },
386 { "metas", vlclua_input_item_metas },
387 { "set_meta", vlclua_input_item_set_meta },
388 { "uri", vlclua_input_item_uri },
389 { "name", vlclua_input_item_name },
390 { "duration", vlclua_input_item_duration },
391 { "stats", vlclua_input_item_stats },
392 { "info", vlclua_input_item_info },
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" );