]> git.sesse.net Git - vlc/blob - modules/lua/libs/input.c
Lua: kill some relocs, pack
[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 <assert.h>
41
42 #include "../vlc.h"
43 #include "input.h"
44 #include "playlist.h"
45 #include "../libs.h"
46 #include "../extension.h"
47
48 static const luaL_Reg vlclua_input_reg[];
49 static const luaL_Reg vlclua_input_item_reg[];
50
51 static input_item_t* vlclua_input_item_get_internal( lua_State *L );
52
53 input_thread_t * vlclua_get_input_internal( lua_State *L )
54 {
55     extension_t *p_extension = vlclua_extension_get( L );
56     if( p_extension )
57     {
58         input_thread_t *p_input = p_extension->p_sys->p_input;
59         if( p_input )
60         {
61             vlc_object_hold(p_input);
62             return p_input;
63         }
64     }
65     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
66     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
67     return p_input;
68 }
69
70 static int vlclua_input_item_info( lua_State *L )
71 {
72     input_item_t *p_item = vlclua_input_item_get_internal( L );
73     int i_cat;
74     int i;
75     i_cat = p_item->i_categories;
76     lua_createtable( L, 0, i_cat );
77     for( i = 0; i < i_cat; i++ )
78     {
79         info_category_t *p_category = p_item->pp_categories[i];
80         int i_infos = p_category->i_infos;
81         int j;
82         lua_pushstring( L, p_category->psz_name );
83         lua_createtable( L, 0, i_infos );
84         for( j = 0; j < i_infos; j++ )
85         {
86             info_t *p_info = p_category->pp_infos[j];
87             lua_pushstring( L, p_info->psz_name );
88             lua_pushstring( L, p_info->psz_value );
89             lua_settable( L, -3 );
90         }
91         lua_settable( L, -3 );
92     }
93     return 1;
94 }
95
96 static int vlclua_input_is_playing( lua_State *L )
97 {
98     input_thread_t * p_input = vlclua_get_input_internal( L );
99     lua_pushboolean( L, !!p_input );
100     if( p_input )
101         vlc_object_release( p_input );
102     return 1;
103 }
104
105 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
106 {
107     if( !p_item )
108     {
109         lua_pushnil( L );
110         return 1;
111     }
112
113     lua_newtable( L );
114     char *psz_name;
115     const char *psz_meta;
116
117     psz_name = input_item_GetName( p_item );
118     lua_pushstring( L, psz_name );
119     lua_setfield( L, -2, "filename" );
120     free( psz_name );
121
122 #define PUSH_META( n, m ) \
123     psz_meta = vlc_meta_Get( p_item->p_meta, vlc_meta_ ## n ); \
124     lua_pushstring( L, psz_meta ); \
125     lua_setfield( L, -2, m )
126
127     vlc_mutex_lock(&p_item->lock);
128
129     if (p_item->p_meta)
130     {
131         PUSH_META( Title, "title" );
132         PUSH_META( Artist, "artist" );
133         PUSH_META( Genre, "genre" );
134         PUSH_META( Copyright, "copyright" );
135         PUSH_META( Album, "album" );
136         PUSH_META( TrackNumber, "track_number" );
137         PUSH_META( Description, "description" );
138         PUSH_META( Rating, "rating" );
139         PUSH_META( Date, "date" );
140         PUSH_META( Setting, "setting" );
141         PUSH_META( URL, "url" );
142         PUSH_META( Language, "language" );
143         PUSH_META( NowPlaying, "now_playing" );
144         PUSH_META( Publisher, "publisher" );
145         PUSH_META( EncodedBy, "encoded_by" );
146         PUSH_META( ArtworkURL, "artwork_url" );
147         PUSH_META( TrackID, "track_id" );
148
149 #undef PUSH_META
150
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( input_item_t* ) );
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[15];
325         unsigned char 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     static_assert( sizeof(pp_meta_types)
348                       == VLC_META_TYPE_COUNT * sizeof(pp_meta_types[0]),
349                    "Inconsistent meta data types" );
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     { "is_playing", vlclua_input_is_playing },
370     { "item", vlclua_input_item_get_current },
371     { "add_subtitle", vlclua_input_add_subtitle },
372     { NULL, NULL }
373 };
374
375 void luaopen_input( lua_State *L )
376 {
377     lua_newtable( L );
378     luaL_register( L, NULL, vlclua_input_reg );
379     lua_setfield( L, -2, "input" );
380 }
381
382 static const luaL_Reg vlclua_input_item_reg[] = {
383     { "is_preparsed", vlclua_input_item_is_preparsed },
384     { "metas", vlclua_input_item_metas },
385     { "set_meta", vlclua_input_item_set_meta },
386     { "uri", vlclua_input_item_uri },
387     { "name", vlclua_input_item_name },
388     { "duration", vlclua_input_item_duration },
389     { "stats", vlclua_input_item_stats },
390     { "info", vlclua_input_item_info },
391     { NULL, NULL }
392 };
393
394
395 void luaopen_input_item( lua_State *L, input_item_t *item )
396 {
397     assert(item);
398     vlclua_input_item_get( L, item );
399     lua_setfield( L, -2, "item" );
400 }