]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/input.c
lua/libs: Fix a left over lonely UnlockExtension().
[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 #include <vlc_charset.h>
38
39 #include <vlc_playlist.h>
40
41 #include <lua.h>        /* Low level lua C API */
42 #include <lauxlib.h>    /* Higher level C API */
43 #include <assert.h>
44
45 #include "input.h"
46 #include "playlist.h"
47 #include "../vlc.h"
48 #include "../libs.h"
49 #include "../extension.h"
50
51 static const luaL_Reg vlclua_input_reg[];
52 static const luaL_Reg vlclua_input_item_reg[];
53
54 input_thread_t * vlclua_get_input_internal( lua_State *L )
55 {
56     extension_t *p_extension = vlclua_extension_get( L );
57     if( p_extension )
58     {
59         input_thread_t *p_input = p_extension->p_sys->p_input;
60         if( p_input )
61         {
62             vlc_object_hold(p_input);
63             return p_input;
64         }
65     }
66     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
67     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
68     vlclua_release_playlist_internal( p_playlist );
69     return p_input;
70 }
71
72 static int vlclua_input_info( lua_State *L )
73 {
74     input_thread_t * p_input = vlclua_get_input_internal( L );
75     int i_cat;
76     int i;
77     if( !p_input ) return vlclua_error( L );
78     //vlc_mutex_lock( &input_GetItem(p_input)->lock );
79     i_cat = input_GetItem(p_input)->i_categories;
80     lua_createtable( L, 0, i_cat );
81     for( i = 0; i < i_cat; i++ )
82     {
83         info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
84         int i_infos = p_category->i_infos;
85         int j;
86         lua_pushstring( L, p_category->psz_name );
87         lua_createtable( L, 0, i_infos );
88         for( j = 0; j < i_infos; j++ )
89         {
90             info_t *p_info = p_category->pp_infos[j];
91             lua_pushstring( L, p_info->psz_name );
92             lua_pushstring( L, p_info->psz_value );
93             lua_settable( L, -3 );
94         }
95         lua_settable( L, -3 );
96     }
97     vlc_object_release( p_input );
98     return 1;
99 }
100
101 static int vlclua_input_is_playing( lua_State *L )
102 {
103     input_thread_t * p_input = vlclua_get_input_internal( L );
104     lua_pushboolean( L, !!p_input );
105     if( p_input )
106         vlc_object_release( p_input );
107     return 1;
108 }
109
110 static int vlclua_input_get_title( lua_State *L )
111 {
112     input_thread_t *p_input = vlclua_get_input_internal( L );
113     if( !p_input )
114         lua_pushnil( L );
115     else
116     {
117         lua_pushstring( L, input_GetItem(p_input)->psz_name );
118         vlc_object_release( p_input );
119     }
120     return 1;
121 }
122
123 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
124 {
125     if( !p_item )
126     {
127         lua_pushnil( L );
128         return 1;
129     }
130
131     lua_newtable( L );
132     char *psz_meta;
133
134     psz_meta = input_item_GetName( p_item );
135     lua_pushstring( L, psz_meta );
136     lua_setfield( L, -2, "filename" );
137     free( psz_meta );
138
139 #define PUSH_META( n, m ) \
140     psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
141     lua_pushstring( L, psz_meta ); \
142     lua_setfield( L, -2, m ); \
143     free( psz_meta )
144
145     PUSH_META( Title, "title" );
146     PUSH_META( Artist, "artist" );
147     PUSH_META( Genre, "genre" );
148     PUSH_META( Copyright, "copyright" );
149     PUSH_META( Album, "album" );
150     PUSH_META( TrackNumber, "track_number" );
151     PUSH_META( Description, "description" );
152     PUSH_META( Rating, "rating" );
153     PUSH_META( Date, "date" );
154     PUSH_META( Setting, "setting" );
155     PUSH_META( URL, "url" );
156     PUSH_META( Language, "language" );
157     PUSH_META( NowPlaying, "now_playing" );
158     PUSH_META( Publisher, "publisher" );
159     PUSH_META( EncodedBy, "encoded_by" );
160     PUSH_META( ArtworkURL, "artwork_url" );
161     PUSH_META( TrackID, "track_id" );
162
163 #undef PUSH_META
164
165     vlc_mutex_lock(&p_item->lock);
166     if (p_item->p_meta) {
167         char ** names = vlc_meta_CopyExtraNames(p_item->p_meta);
168         for(int i = 0; names[i]; i++)
169         {
170             const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
171             lua_pushstring( L, meta );
172             lua_setfield( L, -2, names[i] );
173             free(names[i]);
174         }
175         free(names);
176     }
177     vlc_mutex_unlock(&p_item->lock);
178
179     return 1;
180 }
181
182 static int vlclua_input_metas( lua_State *L )
183 {
184     input_thread_t *p_input = vlclua_get_input_internal( L );
185     input_item_t *p_item = p_input && p_input->p
186                          ? input_GetItem( p_input ) : NULL;
187     vlclua_input_metas_internal( L, p_item );
188     if( p_input )
189         vlc_object_release( p_input );
190     return 1;
191 }
192
193 static int vlclua_input_stats( lua_State *L )
194 {
195     input_thread_t *p_input = vlclua_get_input_internal( L );
196     input_item_t *p_item = p_input && p_input->p
197                          ? input_GetItem( p_input ) : NULL;
198     lua_newtable( L );
199     if( p_item )
200     {
201         vlc_mutex_lock( &p_item->p_stats->lock );
202 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
203                        lua_setfield( L, -2, #n );
204 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
205                          lua_setfield( L, -2, #n );
206         STATS_INT( read_bytes )
207         STATS_FLOAT( input_bitrate )
208         STATS_INT( demux_read_bytes )
209         STATS_FLOAT( demux_bitrate )
210         STATS_INT( demux_corrupted )
211         STATS_INT( demux_discontinuity )
212         STATS_INT( decoded_video )
213         STATS_INT( displayed_pictures )
214         STATS_INT( lost_pictures )
215         STATS_INT( decoded_audio )
216         STATS_INT( played_abuffers )
217         STATS_INT( lost_abuffers )
218         STATS_INT( sent_packets )
219         STATS_INT( sent_bytes )
220         STATS_FLOAT( send_bitrate )
221 #undef STATS_INT
222 #undef STATS_FLOAT
223         vlc_mutex_unlock( &p_item->p_stats->lock );
224     }
225     if( p_input )
226         vlc_object_release( p_input );
227     return 1;
228 }
229
230 static int vlclua_input_add_subtitle( lua_State *L )
231 {
232     input_thread_t *p_input = vlclua_get_input_internal( L );
233     if( !p_input )
234         return luaL_error( L, "can't add subtitle: no current input" );
235     if( !lua_isstring( L, 1 ) )
236         return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
237     const char *psz_url = luaL_checkstring( L, 1 );
238     input_AddSubtitle( p_input, psz_url, false );
239     vlc_object_release( p_input );
240     return 1;
241 }
242
243 /*****************************************************************************
244  * Input items
245  *****************************************************************************/
246
247 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
248 {
249     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
250     input_item_t *p_item = *pp_item;
251
252     if( !p_item )
253         luaL_error( L, "script went completely foobar" );
254
255     return p_item;
256 }
257
258 /* Garbage collection of an input_item_t */
259 static int vlclua_input_item_delete( lua_State *L )
260 {
261     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
262     input_item_t *p_item = *pp_item;
263
264     if( !p_item )
265         return luaL_error( L, "script went completely foobar" );
266
267     *pp_item = NULL;
268     vlc_gc_decref( p_item );
269
270     return 1;
271 }
272
273 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
274 {
275     vlc_gc_incref( p_item );
276     input_item_t **pp = lua_newuserdata( L, sizeof( void* ) );
277     *pp = p_item;
278
279     if( luaL_newmetatable( L, "input_item" ) )
280     {
281         lua_newtable( L );
282         luaL_register( L, NULL, vlclua_input_item_reg );
283         lua_setfield( L, -2, "__index" );
284         lua_pushcfunction( L, vlclua_input_item_delete );
285         lua_setfield( L, -2, "__gc" );
286     }
287
288     lua_setmetatable(L, -2);
289
290     return 1;
291 }
292
293 static int vlclua_input_item_get_current( lua_State *L )
294 {
295     input_thread_t *p_input = vlclua_get_input_internal( L );
296     input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
297     if( !p_item )
298     {
299         lua_pushnil( L );
300         if( p_input ) vlc_object_release( p_input );
301         return 1;
302     }
303
304     vlclua_input_item_get( L, p_item );
305
306     if( p_input ) vlc_object_release( p_input );
307     return 1;
308 }
309
310 static int vlclua_input_item_metas( lua_State *L )
311 {
312     vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
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     { "info", vlclua_input_info },
369     { "is_playing", vlclua_input_is_playing },
370     { "get_title", vlclua_input_get_title },
371     { "metas", vlclua_input_metas },
372     { "item", vlclua_input_item_get_current },
373     { "stats", vlclua_input_stats },
374     { "add_subtitle", vlclua_input_add_subtitle },
375     { NULL, NULL }
376 };
377
378 void luaopen_input( lua_State *L )
379 {
380     lua_newtable( L );
381     luaL_register( L, NULL, vlclua_input_reg );
382     lua_setfield( L, -2, "input" );
383 }
384
385 static const luaL_Reg vlclua_input_item_reg[] = {
386     { "metas", vlclua_input_item_metas },
387     { "set_meta", vlclua_input_item_set_meta },
388     { NULL, NULL }
389 };
390
391
392 void luaopen_input_item( lua_State *L, input_item_t *item )
393 {
394     assert(item);
395     vlclua_input_item_get( L, item );
396     lua_setfield( L, -2, "item" );
397 }