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