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 */
49 input_thread_t * vlclua_get_input_internal( lua_State *L )
51 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
52 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
53 vlclua_release_playlist_internal( p_playlist );
57 static int vlclua_input_info( lua_State *L )
59 input_thread_t * p_input = vlclua_get_input_internal( L );
62 if( !p_input ) return vlclua_error( L );
63 //vlc_mutex_lock( &input_GetItem(p_input)->lock );
64 i_cat = input_GetItem(p_input)->i_categories;
65 lua_createtable( L, 0, i_cat );
66 for( i = 0; i < i_cat; i++ )
68 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
69 int i_infos = p_category->i_infos;
71 lua_pushstring( L, p_category->psz_name );
72 lua_createtable( L, 0, i_infos );
73 for( j = 0; j < i_infos; j++ )
75 info_t *p_info = p_category->pp_infos[j];
76 lua_pushstring( L, p_info->psz_name );
77 lua_pushstring( L, p_info->psz_value );
78 lua_settable( L, -3 );
80 lua_settable( L, -3 );
82 vlc_object_release( p_input );
86 static int vlclua_input_is_playing( lua_State *L )
88 input_thread_t * p_input = vlclua_get_input_internal( L );
89 lua_pushboolean( L, !!p_input );
91 vlc_object_release( p_input );
95 static int vlclua_input_get_title( lua_State *L )
97 input_thread_t *p_input = vlclua_get_input_internal( L );
102 lua_pushstring( L, input_GetItem(p_input)->psz_name );
103 vlc_object_release( p_input );
108 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
119 #define PUSH_META( n, m ) \
120 psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
121 lua_pushstring( L, psz_meta ); \
122 lua_setfield( L, -2, m ); \
125 PUSH_META( Title, "title" );
126 PUSH_META( Artist, "artist" );
127 PUSH_META( Genre, "genre" );
128 PUSH_META( Copyright, "copyright" );
129 PUSH_META( Album, "album" );
130 PUSH_META( TrackNumber, "track_number" );
131 PUSH_META( Description, "description" );
132 PUSH_META( Rating, "rating" );
133 PUSH_META( Date, "date" );
134 PUSH_META( Setting, "setting" );
135 PUSH_META( URL, "url" );
136 PUSH_META( Language, "language" );
137 PUSH_META( NowPlaying, "now_playing" );
138 PUSH_META( Publisher, "publisher" );
139 PUSH_META( EncodedBy, "encoded_by" );
140 PUSH_META( ArtworkURL, "artwork_url" );
141 PUSH_META( TrackID, "track_id" );
148 static int vlclua_input_metas( lua_State *L )
150 input_thread_t *p_input = vlclua_get_input_internal( L );
151 input_item_t *p_item = p_input && p_input->p
152 ? input_GetItem( p_input ) : NULL;
153 vlclua_input_metas_internal( L, p_item );
155 vlc_object_release( p_input );
159 static int vlclua_input_stats( lua_State *L )
161 input_thread_t *p_input = vlclua_get_input_internal( L );
162 input_item_t *p_item = p_input && p_input->p
163 ? input_GetItem( p_input ) : NULL;
167 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
168 lua_setfield( L, -2, #n );
169 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
170 lua_setfield( L, -2, #n );
171 STATS_INT( read_bytes )
172 STATS_FLOAT( input_bitrate )
173 STATS_INT( demux_read_bytes )
174 STATS_FLOAT( demux_bitrate )
175 STATS_INT( decoded_video )
176 STATS_INT( displayed_pictures )
177 STATS_INT( lost_pictures )
178 STATS_INT( decoded_audio )
179 STATS_INT( played_abuffers )
180 STATS_INT( lost_abuffers )
181 STATS_INT( sent_packets )
182 STATS_INT( sent_bytes )
183 STATS_FLOAT( send_bitrate )
188 vlc_object_release( p_input );
192 static int vlclua_input_add_subtitle( lua_State *L )
194 input_thread_t *p_input = vlclua_get_input_internal( L );
196 return luaL_error( L, "can't add subtitle: no current input" );
197 if( !lua_isstring( L, 1 ) )
198 return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
199 const char *psz_url = luaL_checkstring( L, 1 );
200 input_AddSubtitle( p_input, psz_url, false );
201 vlc_object_release( p_input );
205 /*****************************************************************************
207 *****************************************************************************/
208 static const luaL_Reg vlclua_input_reg[] = {
209 { "info", vlclua_input_info },
210 { "is_playing", vlclua_input_is_playing },
211 { "get_title", vlclua_input_get_title },
212 { "metas", vlclua_input_metas },
213 { "stats", vlclua_input_stats },
214 { "add_subtitle", vlclua_input_add_subtitle },
218 void luaopen_input( lua_State *L )
221 luaL_register( L, NULL, vlclua_input_reg );
222 lua_setfield( L, -2, "input" );