]> git.sesse.net Git - vlc/blob - modules/lua/libs/input.c
LUA: synchronise meta with the core
[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         PUSH_META( TrackTotal, "track_total" );
149
150 #undef PUSH_META
151
152         char **names = vlc_meta_CopyExtraNames(p_item->p_meta);
153         for(int i = 0; names[i]; i++)
154         {
155             const char *meta = vlc_meta_GetExtra(p_item->p_meta, names[i]);
156             lua_pushstring( L, meta );
157             lua_setfield( L, -2, names[i] );
158             free(names[i]);
159         }
160         free(names);
161     }
162     vlc_mutex_unlock(&p_item->lock);
163
164     return 1;
165 }
166
167 static int vlclua_input_item_stats( lua_State *L )
168 {
169     input_item_t *p_item = vlclua_input_item_get_internal( L );
170     lua_newtable( L );
171     if( p_item )
172     {
173         vlc_mutex_lock( &p_item->p_stats->lock );
174 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
175                        lua_setfield( L, -2, #n );
176 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
177                          lua_setfield( L, -2, #n );
178         STATS_INT( read_packets )
179         STATS_INT( read_bytes )
180         STATS_FLOAT( input_bitrate )
181         STATS_FLOAT( average_input_bitrate )
182         STATS_INT( demux_read_packets )
183         STATS_INT( demux_read_bytes )
184         STATS_FLOAT( demux_bitrate )
185         STATS_FLOAT( average_demux_bitrate )
186         STATS_INT( demux_corrupted )
187         STATS_INT( demux_discontinuity )
188         STATS_INT( decoded_audio )
189         STATS_INT( decoded_video )
190         STATS_INT( displayed_pictures )
191         STATS_INT( lost_pictures )
192         STATS_INT( sent_packets )
193         STATS_INT( sent_bytes )
194         STATS_FLOAT( send_bitrate )
195         STATS_INT( played_abuffers )
196         STATS_INT( lost_abuffers )
197 #undef STATS_INT
198 #undef STATS_FLOAT
199         vlc_mutex_unlock( &p_item->p_stats->lock );
200     }
201     return 1;
202 }
203
204 static int vlclua_input_add_subtitle( lua_State *L )
205 {
206     input_thread_t *p_input = vlclua_get_input_internal( L );
207     if( !p_input )
208         return luaL_error( L, "can't add subtitle: no current input" );
209     if( !lua_isstring( L, 1 ) )
210         return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
211     const char *psz_url = luaL_checkstring( L, 1 );
212     input_AddSubtitle( p_input, psz_url, false );
213     vlc_object_release( p_input );
214     return 1;
215 }
216
217 /*****************************************************************************
218  * Input items
219  *****************************************************************************/
220
221 static input_item_t* vlclua_input_item_get_internal( lua_State *L )
222 {
223     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
224     input_item_t *p_item = *pp_item;
225
226     if( !p_item )
227         luaL_error( L, "script went completely foobar" );
228
229     return p_item;
230 }
231
232 /* Garbage collection of an input_item_t */
233 static int vlclua_input_item_delete( lua_State *L )
234 {
235     input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
236     input_item_t *p_item = *pp_item;
237
238     if( !p_item )
239         return luaL_error( L, "script went completely foobar" );
240
241     *pp_item = NULL;
242     vlc_gc_decref( p_item );
243
244     return 1;
245 }
246
247 static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
248 {
249     vlc_gc_incref( p_item );
250     input_item_t **pp = lua_newuserdata( L, sizeof( input_item_t* ) );
251     *pp = p_item;
252
253     if( luaL_newmetatable( L, "input_item" ) )
254     {
255         lua_newtable( L );
256         luaL_register( L, NULL, vlclua_input_item_reg );
257         lua_setfield( L, -2, "__index" );
258         lua_pushcfunction( L, vlclua_input_item_delete );
259         lua_setfield( L, -2, "__gc" );
260     }
261
262     lua_setmetatable(L, -2);
263
264     return 1;
265 }
266
267 static int vlclua_input_item_get_current( lua_State *L )
268 {
269     input_thread_t *p_input = vlclua_get_input_internal( L );
270     input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
271     if( !p_item )
272     {
273         lua_pushnil( L );
274         if( p_input ) vlc_object_release( p_input );
275         return 1;
276     }
277
278     vlclua_input_item_get( L, p_item );
279
280     if( p_input ) vlc_object_release( p_input );
281     return 1;
282 }
283
284 static int vlclua_input_item_metas( lua_State *L )
285 {
286     vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
287     return 1;
288 }
289
290 static int vlclua_input_item_is_preparsed( lua_State *L )
291 {
292     lua_pushboolean( L, input_item_IsPreparsed( vlclua_input_item_get_internal( L ) ) );
293     return 1;
294 }
295
296 static int vlclua_input_item_uri( lua_State *L )
297 {
298     lua_pushstring( L, input_item_GetURI( vlclua_input_item_get_internal( L ) ) );
299     return 1;
300 }
301
302 static int vlclua_input_item_name( lua_State *L )
303 {
304     lua_pushstring( L, input_item_GetName( vlclua_input_item_get_internal( L ) ) );
305     return 1;
306 }
307
308 static int vlclua_input_item_duration( lua_State *L )
309 {
310     mtime_t duration = input_item_GetDuration( vlclua_input_item_get_internal( L ) );
311     lua_pushnumber( L, ((double)duration)/1000000. );
312     return 1;
313 }
314
315 static int vlclua_input_item_set_meta( lua_State *L )
316 {
317     input_item_t *p_item = vlclua_input_item_get_internal( L );
318     lua_settop( L, 1 + 2 ); // two arguments
319     const char *psz_name = luaL_checkstring( L, 2 ),
320                *psz_value = luaL_checkstring( L, 3 );
321
322 #define META_TYPE( n, s ) { s, vlc_meta_ ## n },
323     static const struct
324     {
325         const char psz_name[15];
326         unsigned char type;
327     } pp_meta_types[] = {
328         META_TYPE( Title, "title" )
329         META_TYPE( Artist, "artist" )
330         META_TYPE( Genre, "genre" )
331         META_TYPE( Copyright, "copyright" )
332         META_TYPE( Album, "album" )
333         META_TYPE( TrackNumber, "track_number" )
334         META_TYPE( Description, "description" )
335         META_TYPE( Rating, "rating" )
336         META_TYPE( Date, "date" )
337         META_TYPE( Setting, "setting" )
338         META_TYPE( URL, "url" )
339         META_TYPE( Language, "language" )
340         META_TYPE( NowPlaying, "now_playing" )
341         META_TYPE( Publisher, "publisher" )
342         META_TYPE( EncodedBy, "encoded_by" )
343         META_TYPE( ArtworkURL, "artwork_url" )
344         META_TYPE( TrackID, "track_id" )
345         META_TYPE( TrackTotal, "track_total" )
346     };
347 #undef META_TYPE
348
349     static_assert( sizeof(pp_meta_types)
350                       == VLC_META_TYPE_COUNT * sizeof(pp_meta_types[0]),
351                    "Inconsistent meta data types" );
352     vlc_meta_type_t type = vlc_meta_Title;
353     for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
354     {
355         if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
356         {
357             type = pp_meta_types[i].type;
358             input_item_SetMeta( p_item, type, psz_value );
359             return 1;
360         }
361     }
362
363     vlc_meta_AddExtra( p_item->p_meta, psz_name, psz_value );
364     return 1;
365 }
366
367 /*****************************************************************************
368  * Lua bindings
369  *****************************************************************************/
370 static const luaL_Reg vlclua_input_reg[] = {
371     { "is_playing", vlclua_input_is_playing },
372     { "item", vlclua_input_item_get_current },
373     { "add_subtitle", vlclua_input_add_subtitle },
374     { NULL, NULL }
375 };
376
377 void luaopen_input( lua_State *L )
378 {
379     lua_newtable( L );
380     luaL_register( L, NULL, vlclua_input_reg );
381     lua_setfield( L, -2, "input" );
382 }
383
384 static const luaL_Reg vlclua_input_item_reg[] = {
385     { "is_preparsed", vlclua_input_item_is_preparsed },
386     { "metas", vlclua_input_item_metas },
387     { "set_meta", vlclua_input_item_set_meta },
388     { "uri", vlclua_input_item_uri },
389     { "name", vlclua_input_item_name },
390     { "duration", vlclua_input_item_duration },
391     { "stats", vlclua_input_item_stats },
392     { "info", vlclua_input_item_info },
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 }