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