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>
45 #include "../extension.h"
47 static const luaL_Reg vlclua_input_reg[];
48 static const luaL_Reg vlclua_input_item_reg[];
50 static input_item_t* vlclua_input_item_get_internal( lua_State *L );
52 input_thread_t * vlclua_get_input_internal( lua_State *L )
54 extension_t *p_extension = vlclua_extension_get( L );
57 input_thread_t *p_input = p_extension->p_sys->p_input;
60 vlc_object_hold(p_input);
65 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
66 if( p_playlist != NULL )
68 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
76 static int vlclua_input_item_info( lua_State *L )
78 input_item_t *p_item = vlclua_input_item_get_internal( L );
81 i_cat = p_item->i_categories;
82 lua_createtable( L, 0, i_cat );
83 for( i = 0; i < i_cat; i++ )
85 info_category_t *p_category = p_item->pp_categories[i];
86 int i_infos = p_category->i_infos;
88 lua_pushstring( L, p_category->psz_name );
89 lua_createtable( L, 0, i_infos );
90 for( j = 0; j < i_infos; j++ )
92 info_t *p_info = p_category->pp_infos[j];
93 lua_pushstring( L, p_info->psz_name );
94 lua_pushstring( L, p_info->psz_value );
95 lua_settable( L, -3 );
97 lua_settable( L, -3 );
102 static int vlclua_input_is_playing( lua_State *L )
104 input_thread_t * p_input = vlclua_get_input_internal( L );
105 lua_pushboolean( L, !!p_input );
107 vlc_object_release( p_input );
111 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
121 const char *psz_meta;
123 psz_name = input_item_GetName( p_item );
124 lua_pushstring( L, psz_name );
125 lua_setfield( L, -2, "filename" );
128 #define PUSH_META( n, m ) \
129 psz_meta = vlc_meta_Get( p_item->p_meta, vlc_meta_ ## n ); \
130 lua_pushstring( L, psz_meta ); \
131 lua_setfield( L, -2, m )
133 vlc_mutex_lock(&p_item->lock);
137 PUSH_META( Title, "title" );
138 PUSH_META( Artist, "artist" );
139 PUSH_META( Genre, "genre" );
140 PUSH_META( Copyright, "copyright" );
141 PUSH_META( Album, "album" );
142 PUSH_META( TrackNumber, "track_number" );
143 PUSH_META( Description, "description" );
144 PUSH_META( Rating, "rating" );
145 PUSH_META( Date, "date" );
146 PUSH_META( Setting, "setting" );
147 PUSH_META( URL, "url" );
148 PUSH_META( Language, "language" );
149 PUSH_META( NowPlaying, "now_playing" );
150 PUSH_META( Publisher, "publisher" );
151 PUSH_META( EncodedBy, "encoded_by" );
152 PUSH_META( ArtworkURL, "artwork_url" );
153 PUSH_META( TrackID, "track_id" );
154 PUSH_META( TrackTotal, "track_total" );
155 PUSH_META( Director, "director" );
156 PUSH_META( Season, "season" );
157 PUSH_META( Episode, "episode" );
158 PUSH_META( ShowName, "show_name" );
159 PUSH_META( Actors, "actors" );
163 char **names = vlc_meta_CopyExtraNames(p_item->p_meta);
164 for(int i = 0; names[i]; i++)
166 const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
167 lua_pushstring( L, meta );
168 lua_setfield( L, -2, names[i] );
173 vlc_mutex_unlock(&p_item->lock);
178 static int vlclua_input_item_stats( lua_State *L )
180 input_item_t *p_item = vlclua_input_item_get_internal( L );
184 vlc_mutex_lock( &p_item->p_stats->lock );
185 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
186 lua_setfield( L, -2, #n );
187 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
188 lua_setfield( L, -2, #n );
189 STATS_INT( read_packets )
190 STATS_INT( read_bytes )
191 STATS_FLOAT( input_bitrate )
192 STATS_FLOAT( average_input_bitrate )
193 STATS_INT( demux_read_packets )
194 STATS_INT( demux_read_bytes )
195 STATS_FLOAT( demux_bitrate )
196 STATS_FLOAT( average_demux_bitrate )
197 STATS_INT( demux_corrupted )
198 STATS_INT( demux_discontinuity )
199 STATS_INT( decoded_audio )
200 STATS_INT( decoded_video )
201 STATS_INT( displayed_pictures )
202 STATS_INT( lost_pictures )
203 STATS_INT( sent_packets )
204 STATS_INT( sent_bytes )
205 STATS_FLOAT( send_bitrate )
206 STATS_INT( played_abuffers )
207 STATS_INT( lost_abuffers )
210 vlc_mutex_unlock( &p_item->p_stats->lock );
215 static int vlclua_input_add_subtitle( lua_State *L )
217 input_thread_t *p_input = vlclua_get_input_internal( L );
219 return luaL_error( L, "can't add subtitle: no current input" );
220 if( !lua_isstring( L, 1 ) )
221 return luaL_error( L, "vlc.input.add_subtitle() usage: (path)" );
222 const char *psz_path = luaL_checkstring( L, 1 );
223 input_AddSubtitle( p_input, psz_path, false );
224 vlc_object_release( p_input );
228 /*****************************************************************************
230 *****************************************************************************/
232 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
234 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
235 input_item_t *p_item = *pp_item;
238 luaL_error( L, "script went completely foobar" );
243 /* Garbage collection of an input_item_t */
244 static int vlclua_input_item_delete( lua_State *L )
246 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
247 input_item_t *p_item = *pp_item;
250 return luaL_error( L, "script went completely foobar" );
253 vlc_gc_decref( p_item );
258 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
260 vlc_gc_incref( p_item );
261 input_item_t **pp = lua_newuserdata( L, sizeof( input_item_t* ) );
264 if( luaL_newmetatable( L, "input_item" ) )
267 luaL_register( L, NULL, vlclua_input_item_reg );
268 lua_setfield( L, -2, "__index" );
269 lua_pushcfunction( L, vlclua_input_item_delete );
270 lua_setfield( L, -2, "__gc" );
273 lua_setmetatable(L, -2);
278 static int vlclua_input_item_get_current( lua_State *L )
280 input_thread_t *p_input = vlclua_get_input_internal( L );
281 input_item_t *p_item = p_input ? input_GetItem( p_input ) : NULL;
285 if( p_input ) vlc_object_release( p_input );
289 vlclua_input_item_get( L, p_item );
291 if( p_input ) vlc_object_release( p_input );
295 static int vlclua_input_item_metas( lua_State *L )
297 vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
301 static int vlclua_input_item_is_preparsed( lua_State *L )
303 lua_pushboolean( L, input_item_IsPreparsed( vlclua_input_item_get_internal( L ) ) );
307 static int vlclua_input_item_uri( lua_State *L )
309 lua_pushstring( L, input_item_GetURI( vlclua_input_item_get_internal( L ) ) );
313 static int vlclua_input_item_name( lua_State *L )
315 lua_pushstring( L, input_item_GetName( vlclua_input_item_get_internal( L ) ) );
319 static int vlclua_input_item_duration( lua_State *L )
321 mtime_t duration = input_item_GetDuration( vlclua_input_item_get_internal( L ) );
322 lua_pushnumber( L, ((double)duration)/1000000. );
326 static int vlclua_input_item_set_meta( lua_State *L )
328 input_item_t *p_item = vlclua_input_item_get_internal( L );
329 lua_settop( L, 1 + 2 ); // two arguments
330 const char *psz_name = luaL_checkstring( L, 2 ),
331 *psz_value = luaL_checkstring( L, 3 );
333 #define META_TYPE( n, s ) { s, vlc_meta_ ## n },
336 const char psz_name[15];
338 } pp_meta_types[] = {
339 META_TYPE( Title, "title" )
340 META_TYPE( Artist, "artist" )
341 META_TYPE( Genre, "genre" )
342 META_TYPE( Copyright, "copyright" )
343 META_TYPE( Album, "album" )
344 META_TYPE( TrackNumber, "track_number" )
345 META_TYPE( Description, "description" )
346 META_TYPE( Rating, "rating" )
347 META_TYPE( Date, "date" )
348 META_TYPE( Setting, "setting" )
349 META_TYPE( URL, "url" )
350 META_TYPE( Language, "language" )
351 META_TYPE( NowPlaying, "now_playing" )
352 META_TYPE( Publisher, "publisher" )
353 META_TYPE( EncodedBy, "encoded_by" )
354 META_TYPE( ArtworkURL, "artwork_url" )
355 META_TYPE( TrackID, "track_id" )
356 META_TYPE( TrackTotal, "track_total" )
357 META_TYPE( Director, "director" )
358 META_TYPE( Season, "season" )
359 META_TYPE( Episode, "episode" )
360 META_TYPE( ShowName, "show_name" )
361 META_TYPE( Actors, "actors" )
365 static_assert( sizeof(pp_meta_types)
366 == VLC_META_TYPE_COUNT * sizeof(pp_meta_types[0]),
367 "Inconsistent meta data types" );
368 vlc_meta_type_t type = vlc_meta_Title;
369 for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
371 if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
373 type = pp_meta_types[i].type;
374 input_item_SetMeta( p_item, type, psz_value );
379 vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
383 /*****************************************************************************
385 *****************************************************************************/
386 static const luaL_Reg vlclua_input_reg[] = {
387 { "is_playing", vlclua_input_is_playing },
388 { "item", vlclua_input_item_get_current },
389 { "add_subtitle", vlclua_input_add_subtitle },
393 void luaopen_input( lua_State *L )
396 luaL_register( L, NULL, vlclua_input_reg );
397 lua_setfield( L, -2, "input" );
400 static const luaL_Reg vlclua_input_item_reg[] = {
401 { "is_preparsed", vlclua_input_item_is_preparsed },
402 { "metas", vlclua_input_item_metas },
403 { "set_meta", vlclua_input_item_set_meta },
404 { "uri", vlclua_input_item_uri },
405 { "name", vlclua_input_item_name },
406 { "duration", vlclua_input_item_duration },
407 { "stats", vlclua_input_item_stats },
408 { "info", vlclua_input_item_info },
413 void luaopen_input_item( lua_State *L, input_item_t *item )
416 vlclua_input_item_get( L, item );
417 lua_setfield( L, -2, "item" );