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 )
117 const char *psz_meta;
119 psz_name = input_item_GetName( p_item );
120 lua_pushstring( L, psz_name );
121 lua_setfield( L, -2, "filename" );
124 #define PUSH_META( n, m ) \
125 psz_meta = vlc_meta_Get( p_item->p_meta, vlc_meta_ ## n ); \
126 lua_pushstring( L, psz_meta ); \
127 lua_setfield( L, -2, m )
129 vlc_mutex_lock(&p_item->lock);
133 PUSH_META( Title, "title" );
134 PUSH_META( Artist, "artist" );
135 PUSH_META( Genre, "genre" );
136 PUSH_META( Copyright, "copyright" );
137 PUSH_META( Album, "album" );
138 PUSH_META( TrackNumber, "track_number" );
139 PUSH_META( Description, "description" );
140 PUSH_META( Rating, "rating" );
141 PUSH_META( Date, "date" );
142 PUSH_META( Setting, "setting" );
143 PUSH_META( URL, "url" );
144 PUSH_META( Language, "language" );
145 PUSH_META( NowPlaying, "now_playing" );
146 PUSH_META( Publisher, "publisher" );
147 PUSH_META( EncodedBy, "encoded_by" );
148 PUSH_META( ArtworkURL, "artwork_url" );
149 PUSH_META( TrackID, "track_id" );
153 char **names = vlc_meta_CopyExtraNames(p_item->p_meta);
154 for(int i = 0; names[i]; i++)
156 const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
157 lua_pushstring( L, meta );
158 lua_setfield( L, -2, names[i] );
163 vlc_mutex_unlock(&p_item->lock);
168 static int vlclua_input_item_stats( lua_State *L )
170 input_item_t *p_item = vlclua_input_item_get_internal( L );
174 vlc_mutex_lock( &p_item->p_stats->lock );
175 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
176 lua_setfield( L, -2, #n );
177 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
178 lua_setfield( L, -2, #n );
179 STATS_INT( read_packets )
180 STATS_INT( read_bytes )
181 STATS_FLOAT( input_bitrate )
182 STATS_FLOAT( average_input_bitrate )
183 STATS_INT( demux_read_packets )
184 STATS_INT( demux_read_bytes )
185 STATS_FLOAT( demux_bitrate )
186 STATS_FLOAT( average_demux_bitrate )
187 STATS_INT( demux_corrupted )
188 STATS_INT( demux_discontinuity )
189 STATS_INT( decoded_audio )
190 STATS_INT( decoded_video )
191 STATS_INT( displayed_pictures )
192 STATS_INT( lost_pictures )
193 STATS_INT( sent_packets )
194 STATS_INT( sent_bytes )
195 STATS_FLOAT( send_bitrate )
196 STATS_INT( played_abuffers )
197 STATS_INT( lost_abuffers )
200 vlc_mutex_unlock( &p_item->p_stats->lock );
205 static int vlclua_input_add_subtitle( lua_State *L )
207 input_thread_t *p_input = vlclua_get_input_internal( L );
209 return luaL_error( L, "can't add subtitle: no current input" );
210 if( !lua_isstring( L, 1 ) )
211 return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
212 const char *psz_url = luaL_checkstring( L, 1 );
213 input_AddSubtitle( p_input, psz_url, false );
214 vlc_object_release( p_input );
218 /*****************************************************************************
220 *****************************************************************************/
222 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
224 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
225 input_item_t *p_item = *pp_item;
228 luaL_error( L, "script went completely foobar" );
233 /* Garbage collection of an input_item_t */
234 static int vlclua_input_item_delete( lua_State *L )
236 input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
237 input_item_t *p_item = *pp_item;
240 return luaL_error( L, "script went completely foobar" );
243 vlc_gc_decref( p_item );
248 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
250 vlc_gc_incref( p_item );
251 input_item_t **pp = lua_newuserdata( L, sizeof( input_item_t* ) );
254 if( luaL_newmetatable( L, "input_item" ) )
257 luaL_register( L, NULL, vlclua_input_item_reg );
258 lua_setfield( L, -2, "__index" );
259 lua_pushcfunction( L, vlclua_input_item_delete );
260 lua_setfield( L, -2, "__gc" );
263 lua_setmetatable(L, -2);
268 static int vlclua_input_item_get_current( lua_State *L )
270 input_thread_t *p_input = vlclua_get_input_internal( L );
271 input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
275 if( p_input ) vlc_object_release( p_input );
279 vlclua_input_item_get( L, p_item );
281 if( p_input ) vlc_object_release( p_input );
285 static int vlclua_input_item_metas( lua_State *L )
287 vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
291 static int vlclua_input_item_is_preparsed( lua_State *L )
293 lua_pushboolean( L, input_item_IsPreparsed( vlclua_input_item_get_internal( L ) ) );
297 static int vlclua_input_item_uri( lua_State *L )
299 lua_pushstring( L, input_item_GetURI( vlclua_input_item_get_internal( L ) ) );
303 static int vlclua_input_item_name( lua_State *L )
305 lua_pushstring( L, input_item_GetName( vlclua_input_item_get_internal( L ) ) );
309 static int vlclua_input_item_duration( lua_State *L )
311 mtime_t duration = input_item_GetDuration( vlclua_input_item_get_internal( L ) );
312 lua_pushnumber( L, ((double)duration)/1000000. );
316 static int vlclua_input_item_set_meta( lua_State *L )
318 input_item_t *p_item = vlclua_input_item_get_internal( L );
319 lua_settop( L, 1 + 2 ); // two arguments
320 const char *psz_name = luaL_checkstring( L, 2 ),
321 *psz_value = luaL_checkstring( L, 3 );
323 #define META_TYPE( n, s ) { s, vlc_meta_ ## n },
326 const char *psz_name;
327 vlc_meta_type_t type;
328 } pp_meta_types[] = {
329 META_TYPE( Title, "title" )
330 META_TYPE( Artist, "artist" )
331 META_TYPE( Genre, "genre" )
332 META_TYPE( Copyright, "copyright" )
333 META_TYPE( Album, "album" )
334 META_TYPE( TrackNumber, "track_number" )
335 META_TYPE( Description, "description" )
336 META_TYPE( Rating, "rating" )
337 META_TYPE( Date, "date" )
338 META_TYPE( Setting, "setting" )
339 META_TYPE( URL, "url" )
340 META_TYPE( Language, "language" )
341 META_TYPE( NowPlaying, "now_playing" )
342 META_TYPE( Publisher, "publisher" )
343 META_TYPE( EncodedBy, "encoded_by" )
344 META_TYPE( ArtworkURL, "artwork_url" )
345 META_TYPE( TrackID, "track_id" )
349 vlc_meta_type_t type = vlc_meta_Title;
350 for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
352 if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
354 type = pp_meta_types[i].type;
355 input_item_SetMeta( p_item, type, psz_value );
360 vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
364 /*****************************************************************************
366 *****************************************************************************/
367 static const luaL_Reg vlclua_input_reg[] = {
368 { "is_playing", vlclua_input_is_playing },
369 { "item", vlclua_input_item_get_current },
370 { "add_subtitle", vlclua_input_add_subtitle },
374 void luaopen_input( lua_State *L )
377 luaL_register( L, NULL, vlclua_input_reg );
378 lua_setfield( L, -2, "input" );
381 static const luaL_Reg vlclua_input_item_reg[] = {
382 { "is_preparsed", vlclua_input_item_is_preparsed },
383 { "metas", vlclua_input_item_metas },
384 { "set_meta", vlclua_input_item_set_meta },
385 { "uri", vlclua_input_item_uri },
386 { "name", vlclua_input_item_name },
387 { "duration", vlclua_input_item_duration },
388 { "stats", vlclua_input_item_stats },
389 { "info", vlclua_input_item_info },
394 void luaopen_input_item( lua_State *L, input_item_t *item )
397 vlclua_input_item_get( L, item );
398 lua_setfield( L, -2, "item" );