]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/input.c
lua: Add the possibility to expose vlc.item.
[vlc] / modules / misc / lua / libs / input.c
1 /*****************************************************************************
2  * input.c
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_meta.h>
37 #include <vlc_charset.h>
38
39 #include <vlc_playlist.h>
40
41 #include <lua.h>        /* Low level lua C API */
42 #include <lauxlib.h>    /* Higher level C API */
43
44 #include "input.h"
45 #include "playlist.h"
46 #include "../vlc.h"
47 #include "../libs.h"
48
49 static const luaL_Reg vlclua_input_reg[];
50 static const luaL_Reg vlclua_input_item_reg[];
51
52 input_thread_t * vlclua_get_input_internal( lua_State *L )
53 {
54     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
55     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
56     vlclua_release_playlist_internal( p_playlist );
57     return p_input;
58 }
59
60 static int vlclua_input_info( lua_State *L )
61 {
62     input_thread_t * p_input = vlclua_get_input_internal( L );
63     int i_cat;
64     int i;
65     if( !p_input ) return vlclua_error( L );
66     //vlc_mutex_lock( &input_GetItem(p_input)->lock );
67     i_cat = input_GetItem(p_input)->i_categories;
68     lua_createtable( L, 0, i_cat );
69     for( i = 0; i < i_cat; i++ )
70     {
71         info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
72         int i_infos = p_category->i_infos;
73         int j;
74         lua_pushstring( L, p_category->psz_name );
75         lua_createtable( L, 0, i_infos );
76         for( j = 0; j < i_infos; j++ )
77         {
78             info_t *p_info = p_category->pp_infos[j];
79             lua_pushstring( L, p_info->psz_name );
80             lua_pushstring( L, p_info->psz_value );
81             lua_settable( L, -3 );
82         }
83         lua_settable( L, -3 );
84     }
85     vlc_object_release( p_input );
86     return 1;
87 }
88
89 static int vlclua_input_is_playing( lua_State *L )
90 {
91     input_thread_t * p_input = vlclua_get_input_internal( L );
92     lua_pushboolean( L, !!p_input );
93     if( p_input )
94         vlc_object_release( p_input );
95     return 1;
96 }
97
98 static int vlclua_input_get_title( lua_State *L )
99 {
100     input_thread_t *p_input = vlclua_get_input_internal( L );
101     if( !p_input )
102         lua_pushnil( L );
103     else
104     {
105         lua_pushstring( L, input_GetItem(p_input)->psz_name );
106         vlc_object_release( p_input );
107     }
108     return 1;
109 }
110
111 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
112 {
113     if( !p_item )
114     {
115         lua_pushnil( L );
116         return 1;
117     }
118
119     lua_newtable( L );
120     char *psz_meta;
121
122     psz_meta = input_item_GetName( p_item );
123     lua_pushstring( L, psz_meta );
124     lua_setfield( L, -2, "filename" );
125     free( psz_meta );
126     
127 #define PUSH_META( n, m ) \
128     psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
129     lua_pushstring( L, psz_meta ); \
130     lua_setfield( L, -2, m ); \
131     free( psz_meta )
132
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" );
150
151 #undef PUSH_META
152
153     return 1;
154 }
155
156 static int vlclua_input_metas( lua_State *L )
157 {
158     input_thread_t *p_input = vlclua_get_input_internal( L );
159     input_item_t *p_item = p_input && p_input->p
160                          ? input_GetItem( p_input ) : NULL;
161     vlclua_input_metas_internal( L, p_item );
162     if( p_input )
163         vlc_object_release( p_input );
164     return 1;
165 }
166
167 static int vlclua_input_stats( lua_State *L )
168 {
169     input_thread_t *p_input = vlclua_get_input_internal( L );
170     input_item_t *p_item = p_input && p_input->p
171                          ? input_GetItem( p_input ) : NULL;
172     lua_newtable( L );
173     if( p_item )
174     {
175         vlc_mutex_lock( &p_item->p_stats->lock );
176 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
177                        lua_setfield( L, -2, #n );
178 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
179                          lua_setfield( L, -2, #n );
180         STATS_INT( read_bytes )
181         STATS_FLOAT( input_bitrate )
182         STATS_INT( demux_read_bytes )
183         STATS_FLOAT( demux_bitrate )
184         STATS_INT( demux_corrupted )
185         STATS_INT( demux_discontinuity )
186         STATS_INT( decoded_video )
187         STATS_INT( displayed_pictures )
188         STATS_INT( lost_pictures )
189         STATS_INT( decoded_audio )
190         STATS_INT( played_abuffers )
191         STATS_INT( lost_abuffers )
192         STATS_INT( sent_packets )
193         STATS_INT( sent_bytes )
194         STATS_FLOAT( send_bitrate )
195 #undef STATS_INT
196 #undef STATS_FLOAT
197         vlc_mutex_unlock( &p_item->p_stats->lock );
198     }
199     if( p_input )
200         vlc_object_release( p_input );
201     return 1;
202 }
203
204 static int vlclua_input_add_subtitle( lua_State *L )
205 {
206     input_thread_t *p_input = vlclua_get_input_internal( L );
207     if( !p_input )
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 );
214     return 1;
215 }
216
217 /*****************************************************************************
218  * Input items
219  *****************************************************************************/
220
221 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
222 {
223     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
224     input_item_t *p_item = *pp_item;
225
226     if( !p_item )
227         luaL_error( L, "script went completely foobar" );
228
229     return p_item;
230 }
231
232 /* Garbage collection of an input_item_t */
233 static int vlclua_input_item_delete( lua_State *L )
234 {
235     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
236     input_item_t *p_item = *pp_item;
237
238     if( !p_item )
239         return luaL_error( L, "script went completely foobar" );
240
241     *pp_item = NULL;
242     vlc_gc_decref( p_item );
243
244     return 1;
245 }
246
247 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
248 {
249     vlc_gc_incref( p_item );
250     input_item_t **pp = lua_newuserdata( L, sizeof( void* ) );
251     *pp = p_item;
252
253     if( luaL_newmetatable( L, "input_item" ) )
254     {
255         lua_newtable( L );
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" );
260     }
261
262     lua_setmetatable(L, -2);
263     
264     return 1;
265 }
266
267 static int vlclua_input_item_get_current( lua_State *L )
268 {
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;
271     if( !p_item )
272     {
273         lua_pushnil( L );
274         if( p_input ) vlc_object_release( p_input );
275         return 1;
276     }
277
278     vlclua_input_item_get( L, p_item );
279
280     if( p_input ) vlc_object_release( p_input );
281     return 1;
282 }
283
284 static int vlclua_input_item_metas( lua_State *L )
285 {
286     vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
287     return 1;
288 }
289
290 static int vlclua_input_item_set_meta( lua_State *L )
291 {
292     input_item_t *p_item = vlclua_input_item_get_internal( L );
293     lua_settop( L, 1 + 2 ); // two arguments
294     const char *psz_name = luaL_checkstring( L, 2 ),
295                *psz_value = luaL_checkstring( L, 3 );
296
297 #define META_TYPE( n ) { #n, vlc_meta_ ## n }
298     static const struct
299     {
300         const char *psz_name;
301         vlc_meta_type_t type;
302     } pp_meta_types[] = {
303         META_TYPE( Title ),
304         META_TYPE( Artist ),
305         META_TYPE( Genre ),
306         META_TYPE( Copyright ),
307         META_TYPE( Album ),
308         META_TYPE( TrackNumber ),
309         META_TYPE( Description ),
310         META_TYPE( Rating ),
311         META_TYPE( Date ),
312         META_TYPE( Setting ),
313         META_TYPE( URL ),
314         META_TYPE( Language ),
315         META_TYPE( NowPlaying ),
316         META_TYPE( Publisher ),
317         META_TYPE( EncodedBy ),
318         META_TYPE( ArtworkURL ),
319         META_TYPE( TrackID ),
320     };
321 #undef META_TYPE
322
323     vlc_meta_type_t type = vlc_meta_Title;
324     bool ok = false;
325     for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
326     {
327         if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
328         {
329             type = pp_meta_types[i].type;
330             ok = true;
331         }
332     }
333
334     if( !ok ) {
335         vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
336         return 1;
337     }
338
339     input_item_SetMeta( p_item, type, psz_value );
340     return 1;
341 }
342
343 /*****************************************************************************
344  * Lua bindings
345  *****************************************************************************/
346 static const luaL_Reg vlclua_input_reg[] = {
347     { "info", vlclua_input_info },
348     { "is_playing", vlclua_input_is_playing },
349     { "get_title", vlclua_input_get_title },
350     { "metas", vlclua_input_metas },
351     { "item", vlclua_input_item_get_current },
352     { "stats", vlclua_input_stats },
353     { "add_subtitle", vlclua_input_add_subtitle },
354     { NULL, NULL }
355 };
356
357 void luaopen_input( lua_State *L )
358 {
359     lua_newtable( L );
360     luaL_register( L, NULL, vlclua_input_reg );
361     lua_setfield( L, -2, "input" );
362 }
363
364 static const luaL_Reg vlclua_input_item_reg[] = {
365     { "metas", vlclua_input_item_metas },
366     { "set_meta", vlclua_input_item_set_meta },
367     { NULL, NULL }
368 };
369
370
371 void luaopen_input_item( lua_State *L, input_item_t *item )
372 {
373     vlclua_input_item_get( L, item );
374     lua_setfield( L, -2, "item" );
375 }