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