]> git.sesse.net Git - vlc/blob - modules/lua/libs/input.c
V4L2: merge two switches
[vlc] / modules / 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_name;
117     const char *psz_meta;
118
119     psz_name = input_item_GetName( p_item );
120     lua_pushstring( L, psz_name );
121     lua_setfield( L, -2, "filename" );
122     free( psz_name );
123
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 )
128
129     vlc_mutex_lock(&p_item->lock);
130
131     if (p_item->p_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         char **names = vlc_meta_CopyExtraNames(p_item->p_meta);
154         for(int i = 0; names[i]; i++)
155         {
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] );
159             free(names[i]);
160         }
161         free(names);
162     }
163     vlc_mutex_unlock(&p_item->lock);
164
165     return 1;
166 }
167
168 static int vlclua_input_item_stats( lua_State *L )
169 {
170     input_item_t *p_item = vlclua_input_item_get_internal( L );
171     lua_newtable( L );
172     if( p_item )
173     {
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 )
198 #undef STATS_INT
199 #undef STATS_FLOAT
200         vlc_mutex_unlock( &p_item->p_stats->lock );
201     }
202     return 1;
203 }
204
205 static int vlclua_input_add_subtitle( lua_State *L )
206 {
207     input_thread_t *p_input = vlclua_get_input_internal( L );
208     if( !p_input )
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 );
215     return 1;
216 }
217
218 /*****************************************************************************
219  * Input items
220  *****************************************************************************/
221
222 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
223 {
224     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
225     input_item_t *p_item = *pp_item;
226
227     if( !p_item )
228         luaL_error( L, "script went completely foobar" );
229
230     return p_item;
231 }
232
233 /* Garbage collection of an input_item_t */
234 static int vlclua_input_item_delete( lua_State *L )
235 {
236     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
237     input_item_t *p_item = *pp_item;
238
239     if( !p_item )
240         return luaL_error( L, "script went completely foobar" );
241
242     *pp_item = NULL;
243     vlc_gc_decref( p_item );
244
245     return 1;
246 }
247
248 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
249 {
250     vlc_gc_incref( p_item );
251     input_item_t **pp = lua_newuserdata( L, sizeof( input_item_t* ) );
252     *pp = p_item;
253
254     if( luaL_newmetatable( L, "input_item" ) )
255     {
256         lua_newtable( L );
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" );
261     }
262
263     lua_setmetatable(L, -2);
264
265     return 1;
266 }
267
268 static int vlclua_input_item_get_current( lua_State *L )
269 {
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;
272     if( !p_item )
273     {
274         lua_pushnil( L );
275         if( p_input ) vlc_object_release( p_input );
276         return 1;
277     }
278
279     vlclua_input_item_get( L, p_item );
280
281     if( p_input ) vlc_object_release( p_input );
282     return 1;
283 }
284
285 static int vlclua_input_item_metas( lua_State *L )
286 {
287     vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
288     return 1;
289 }
290
291 static int vlclua_input_item_is_preparsed( lua_State *L )
292 {
293     lua_pushboolean( L, input_item_IsPreparsed( vlclua_input_item_get_internal( L ) ) );
294     return 1;
295 }
296
297 static int vlclua_input_item_uri( lua_State *L )
298 {
299     lua_pushstring( L, input_item_GetURI( vlclua_input_item_get_internal( L ) ) );
300     return 1;
301 }
302
303 static int vlclua_input_item_name( lua_State *L )
304 {
305     lua_pushstring( L, input_item_GetName( vlclua_input_item_get_internal( L ) ) );
306     return 1;
307 }
308
309 static int vlclua_input_item_duration( lua_State *L )
310 {
311     mtime_t duration = input_item_GetDuration( vlclua_input_item_get_internal( L ) );
312     lua_pushnumber( L, ((double)duration)/1000000. );
313     return 1;
314 }
315
316 static int vlclua_input_item_set_meta( lua_State *L )
317 {
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 );
322
323 #define META_TYPE( n, s ) { s, vlc_meta_ ## n },
324     static const struct
325     {
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" )
346     };
347 #undef META_TYPE
348
349     vlc_meta_type_t type = vlc_meta_Title;
350     for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
351     {
352         if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
353         {
354             type = pp_meta_types[i].type;
355             input_item_SetMeta( p_item, type, psz_value );
356             return 1;
357         }
358     }
359
360     vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
361     return 1;
362 }
363
364 /*****************************************************************************
365  * Lua bindings
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 },
371     { NULL, NULL }
372 };
373
374 void luaopen_input( lua_State *L )
375 {
376     lua_newtable( L );
377     luaL_register( L, NULL, vlclua_input_reg );
378     lua_setfield( L, -2, "input" );
379 }
380
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 },
390     { NULL, NULL }
391 };
392
393
394 void luaopen_input_item( lua_State *L, input_item_t *item )
395 {
396     assert(item);
397     vlclua_input_item_get( L, item );
398     lua_setfield( L, -2, "item" );
399 }