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