]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/input.c
3d7fa582953b8e43d2ee110606a557ace9b4f337
[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 #include <assert.h>
44
45 #include "input.h"
46 #include "playlist.h"
47 #include "../vlc.h"
48 #include "../libs.h"
49 #include "../extension.h"
50
51 static const luaL_Reg vlclua_input_reg[];
52 static const luaL_Reg vlclua_input_item_reg[];
53
54 input_thread_t * vlclua_get_input_internal( lua_State *L )
55 {
56     extension_t *p_extension = vlclua_extension_get( L );
57     if( p_extension )
58     {
59         input_thread_t *p_input = p_extension->p_sys->p_input;
60         if (p_input)
61         {
62             vlc_object_hold(p_input);
63             UnlockExtension(p_extension);
64             return p_input;
65         }
66     }
67     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
68     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
69     vlclua_release_playlist_internal( p_playlist );
70     return p_input;
71 }
72
73 static int vlclua_input_info( lua_State *L )
74 {
75     input_thread_t * p_input = vlclua_get_input_internal( L );
76     int i_cat;
77     int i;
78     if( !p_input ) return vlclua_error( L );
79     //vlc_mutex_lock( &input_GetItem(p_input)->lock );
80     i_cat = input_GetItem(p_input)->i_categories;
81     lua_createtable( L, 0, i_cat );
82     for( i = 0; i < i_cat; i++ )
83     {
84         info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
85         int i_infos = p_category->i_infos;
86         int j;
87         lua_pushstring( L, p_category->psz_name );
88         lua_createtable( L, 0, i_infos );
89         for( j = 0; j < i_infos; j++ )
90         {
91             info_t *p_info = p_category->pp_infos[j];
92             lua_pushstring( L, p_info->psz_name );
93             lua_pushstring( L, p_info->psz_value );
94             lua_settable( L, -3 );
95         }
96         lua_settable( L, -3 );
97     }
98     vlc_object_release( p_input );
99     return 1;
100 }
101
102 static int vlclua_input_is_playing( lua_State *L )
103 {
104     input_thread_t * p_input = vlclua_get_input_internal( L );
105     lua_pushboolean( L, !!p_input );
106     if( p_input )
107         vlc_object_release( p_input );
108     return 1;
109 }
110
111 static int vlclua_input_get_title( lua_State *L )
112 {
113     input_thread_t *p_input = vlclua_get_input_internal( L );
114     if( !p_input )
115         lua_pushnil( L );
116     else
117     {
118         lua_pushstring( L, input_GetItem(p_input)->psz_name );
119         vlc_object_release( p_input );
120     }
121     return 1;
122 }
123
124 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
125 {
126     if( !p_item )
127     {
128         lua_pushnil( L );
129         return 1;
130     }
131
132     lua_newtable( L );
133     char *psz_meta;
134
135     psz_meta = input_item_GetName( p_item );
136     lua_pushstring( L, psz_meta );
137     lua_setfield( L, -2, "filename" );
138     free( psz_meta );
139
140 #define PUSH_META( n, m ) \
141     psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
142     lua_pushstring( L, psz_meta ); \
143     lua_setfield( L, -2, m ); \
144     free( psz_meta )
145
146     PUSH_META( Title, "title" );
147     PUSH_META( Artist, "artist" );
148     PUSH_META( Genre, "genre" );
149     PUSH_META( Copyright, "copyright" );
150     PUSH_META( Album, "album" );
151     PUSH_META( TrackNumber, "track_number" );
152     PUSH_META( Description, "description" );
153     PUSH_META( Rating, "rating" );
154     PUSH_META( Date, "date" );
155     PUSH_META( Setting, "setting" );
156     PUSH_META( URL, "url" );
157     PUSH_META( Language, "language" );
158     PUSH_META( NowPlaying, "now_playing" );
159     PUSH_META( Publisher, "publisher" );
160     PUSH_META( EncodedBy, "encoded_by" );
161     PUSH_META( ArtworkURL, "artwork_url" );
162     PUSH_META( TrackID, "track_id" );
163
164 #undef PUSH_META
165
166     vlc_mutex_lock(&p_item->lock);
167     if (p_item->p_meta) {
168         char ** names = vlc_meta_CopyExtraNames(p_item->p_meta);
169         for(int i = 0; names[i]; i++)
170         {
171             const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
172             lua_pushstring( L, meta );
173             lua_setfield( L, -2, names[i] );
174             free(names[i]);
175         }
176         free(names);
177     }
178     vlc_mutex_unlock(&p_item->lock);
179
180     return 1;
181 }
182
183 static int vlclua_input_metas( lua_State *L )
184 {
185     input_thread_t *p_input = vlclua_get_input_internal( L );
186     input_item_t *p_item = p_input && p_input->p
187                          ? input_GetItem( p_input ) : NULL;
188     vlclua_input_metas_internal( L, p_item );
189     if( p_input )
190         vlc_object_release( p_input );
191     return 1;
192 }
193
194 static int vlclua_input_stats( lua_State *L )
195 {
196     input_thread_t *p_input = vlclua_get_input_internal( L );
197     input_item_t *p_item = p_input && p_input->p
198                          ? input_GetItem( p_input ) : NULL;
199     lua_newtable( L );
200     if( p_item )
201     {
202         vlc_mutex_lock( &p_item->p_stats->lock );
203 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
204                        lua_setfield( L, -2, #n );
205 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
206                          lua_setfield( L, -2, #n );
207         STATS_INT( read_bytes )
208         STATS_FLOAT( input_bitrate )
209         STATS_INT( demux_read_bytes )
210         STATS_FLOAT( demux_bitrate )
211         STATS_INT( demux_corrupted )
212         STATS_INT( demux_discontinuity )
213         STATS_INT( decoded_video )
214         STATS_INT( displayed_pictures )
215         STATS_INT( lost_pictures )
216         STATS_INT( decoded_audio )
217         STATS_INT( played_abuffers )
218         STATS_INT( lost_abuffers )
219         STATS_INT( sent_packets )
220         STATS_INT( sent_bytes )
221         STATS_FLOAT( send_bitrate )
222 #undef STATS_INT
223 #undef STATS_FLOAT
224         vlc_mutex_unlock( &p_item->p_stats->lock );
225     }
226     if( p_input )
227         vlc_object_release( p_input );
228     return 1;
229 }
230
231 static int vlclua_input_add_subtitle( lua_State *L )
232 {
233     input_thread_t *p_input = vlclua_get_input_internal( L );
234     if( !p_input )
235         return luaL_error( L, "can't add subtitle: no current input" );
236     if( !lua_isstring( L, 1 ) )
237         return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
238     const char *psz_url = luaL_checkstring( L, 1 );
239     input_AddSubtitle( p_input, psz_url, false );
240     vlc_object_release( p_input );
241     return 1;
242 }
243
244 /*****************************************************************************
245  * Input items
246  *****************************************************************************/
247
248 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
249 {
250     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
251     input_item_t *p_item = *pp_item;
252
253     if( !p_item )
254         luaL_error( L, "script went completely foobar" );
255
256     return p_item;
257 }
258
259 /* Garbage collection of an input_item_t */
260 static int vlclua_input_item_delete( lua_State *L )
261 {
262     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
263     input_item_t *p_item = *pp_item;
264
265     if( !p_item )
266         return luaL_error( L, "script went completely foobar" );
267
268     *pp_item = NULL;
269     vlc_gc_decref( p_item );
270
271     return 1;
272 }
273
274 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
275 {
276     vlc_gc_incref( p_item );
277     input_item_t **pp = lua_newuserdata( L, sizeof( void* ) );
278     *pp = p_item;
279
280     if( luaL_newmetatable( L, "input_item" ) )
281     {
282         lua_newtable( L );
283         luaL_register( L, NULL, vlclua_input_item_reg );
284         lua_setfield( L, -2, "__index" );
285         lua_pushcfunction( L, vlclua_input_item_delete );
286         lua_setfield( L, -2, "__gc" );
287     }
288
289     lua_setmetatable(L, -2);
290
291     return 1;
292 }
293
294 static int vlclua_input_item_get_current( lua_State *L )
295 {
296     input_thread_t *p_input = vlclua_get_input_internal( L );
297     input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
298     if( !p_item )
299     {
300         lua_pushnil( L );
301         if( p_input ) vlc_object_release( p_input );
302         return 1;
303     }
304
305     vlclua_input_item_get( L, p_item );
306
307     if( p_input ) vlc_object_release( p_input );
308     return 1;
309 }
310
311 static int vlclua_input_item_metas( lua_State *L )
312 {
313     vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
314     return 1;
315 }
316
317 static int vlclua_input_item_set_meta( lua_State *L )
318 {
319     input_item_t *p_item = vlclua_input_item_get_internal( L );
320     lua_settop( L, 1 + 2 ); // two arguments
321     const char *psz_name = luaL_checkstring( L, 2 ),
322                *psz_value = luaL_checkstring( L, 3 );
323
324 #define META_TYPE( n, s ) { s, vlc_meta_ ## n },
325     static const struct
326     {
327         const char *psz_name;
328         vlc_meta_type_t type;
329     } pp_meta_types[] = {
330         META_TYPE( Title, "title" )
331         META_TYPE( Artist, "artist" )
332         META_TYPE( Genre, "genre" )
333         META_TYPE( Copyright, "copyright" )
334         META_TYPE( Album, "album" )
335         META_TYPE( TrackNumber, "track_number" )
336         META_TYPE( Description, "description" )
337         META_TYPE( Rating, "rating" )
338         META_TYPE( Date, "date" )
339         META_TYPE( Setting, "setting" )
340         META_TYPE( URL, "url" )
341         META_TYPE( Language, "language" )
342         META_TYPE( NowPlaying, "now_playing" )
343         META_TYPE( Publisher, "publisher" )
344         META_TYPE( EncodedBy, "encoded_by" )
345         META_TYPE( ArtworkURL, "artwork_url" )
346         META_TYPE( TrackID, "track_id" )
347     };
348 #undef META_TYPE
349
350     vlc_meta_type_t type = vlc_meta_Title;
351     for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
352     {
353         if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
354         {
355             type = pp_meta_types[i].type;
356             input_item_SetMeta( p_item, type, psz_value );
357             return 1;
358         }
359     }
360
361     vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
362     return 1;
363 }
364
365 /*****************************************************************************
366  * Lua bindings
367  *****************************************************************************/
368 static const luaL_Reg vlclua_input_reg[] = {
369     { "info", vlclua_input_info },
370     { "is_playing", vlclua_input_is_playing },
371     { "get_title", vlclua_input_get_title },
372     { "metas", vlclua_input_metas },
373     { "item", vlclua_input_item_get_current },
374     { "stats", vlclua_input_stats },
375     { "add_subtitle", vlclua_input_add_subtitle },
376     { NULL, NULL }
377 };
378
379 void luaopen_input( lua_State *L )
380 {
381     lua_newtable( L );
382     luaL_register( L, NULL, vlclua_input_reg );
383     lua_setfield( L, -2, "input" );
384 }
385
386 static const luaL_Reg vlclua_input_item_reg[] = {
387     { "metas", vlclua_input_item_metas },
388     { "set_meta", vlclua_input_item_set_meta },
389     { NULL, NULL }
390 };
391
392
393 void luaopen_input_item( lua_State *L, input_item_t *item )
394 {
395     assert(item);
396     vlclua_input_item_get( L, item );
397     lua_setfield( L, -2, "item" );
398 }