]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/input.c
27b1a3ad4ba857346f651cf7a3a570d4a58ab850
[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
44 #include "input.h"
45 #include "playlist.h"
46 #include "../vlc.h"
47 #include "../libs.h"
48
49 input_thread_t * vlclua_get_input_internal( lua_State *L )
50 {
51     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
52     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
53     vlclua_release_playlist_internal( p_playlist );
54     return p_input;
55 }
56
57 static int vlclua_input_info( lua_State *L )
58 {
59     input_thread_t * p_input = vlclua_get_input_internal( L );
60     int i_cat;
61     int i;
62     if( !p_input ) return vlclua_error( L );
63     //vlc_mutex_lock( &input_GetItem(p_input)->lock );
64     i_cat = input_GetItem(p_input)->i_categories;
65     lua_createtable( L, 0, i_cat );
66     for( i = 0; i < i_cat; i++ )
67     {
68         info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
69         int i_infos = p_category->i_infos;
70         int j;
71         lua_pushstring( L, p_category->psz_name );
72         lua_createtable( L, 0, i_infos );
73         for( j = 0; j < i_infos; j++ )
74         {
75             info_t *p_info = p_category->pp_infos[j];
76             lua_pushstring( L, p_info->psz_name );
77             lua_pushstring( L, p_info->psz_value );
78             lua_settable( L, -3 );
79         }
80         lua_settable( L, -3 );
81     }
82     vlc_object_release( p_input );
83     return 1;
84 }
85
86 static int vlclua_input_is_playing( lua_State *L )
87 {
88     input_thread_t * p_input = vlclua_get_input_internal( L );
89     lua_pushboolean( L, !!p_input );
90     if( p_input )
91         vlc_object_release( p_input );
92     return 1;
93 }
94
95 static int vlclua_input_get_title( lua_State *L )
96 {
97     input_thread_t *p_input = vlclua_get_input_internal( L );
98     if( !p_input )
99         lua_pushnil( L );
100     else
101     {
102         lua_pushstring( L, input_GetItem(p_input)->psz_name );
103         vlc_object_release( p_input );
104     }
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_meta;
118
119 #define PUSH_META( n, m ) \
120     psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
121     lua_pushstring( L, psz_meta ); \
122     lua_setfield( L, -2, m ); \
123     free( psz_meta )
124
125     PUSH_META( Title, "title" );
126     PUSH_META( Artist, "artist" );
127     PUSH_META( Genre, "genre" );
128     PUSH_META( Copyright, "copyright" );
129     PUSH_META( Album, "album" );
130     PUSH_META( TrackNumber, "track_number" );
131     PUSH_META( Description, "description" );
132     PUSH_META( Rating, "rating" );
133     PUSH_META( Date, "date" );
134     PUSH_META( Setting, "setting" );
135     PUSH_META( URL, "url" );
136     PUSH_META( Language, "language" );
137     PUSH_META( NowPlaying, "now_playing" );
138     PUSH_META( Publisher, "publisher" );
139     PUSH_META( EncodedBy, "encoded_by" );
140     PUSH_META( ArtworkURL, "artwork_url" );
141     PUSH_META( TrackID, "track_id" );
142
143 #undef PUSH_META
144
145     return 1;
146 }
147
148 static int vlclua_input_metas( lua_State *L )
149 {
150     input_thread_t *p_input = vlclua_get_input_internal( L );
151     input_item_t *p_item = p_input && p_input->p
152                          ? input_GetItem( p_input ) : NULL;
153     vlclua_input_metas_internal( L, p_item );
154     if( p_input )
155         vlc_object_release( p_input );
156     return 1;
157 }
158
159 static int vlclua_input_stats( lua_State *L )
160 {
161     input_thread_t *p_input = vlclua_get_input_internal( L );
162     input_item_t *p_item = p_input && p_input->p
163                          ? input_GetItem( p_input ) : NULL;
164     lua_newtable( L );
165     if( p_item )
166     {
167 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
168                        lua_setfield( L, -2, #n );
169 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
170                          lua_setfield( L, -2, #n );
171         STATS_INT( read_bytes )
172         STATS_FLOAT( input_bitrate )
173         STATS_INT( demux_read_bytes )
174         STATS_FLOAT( demux_bitrate )
175         STATS_INT( decoded_video )
176         STATS_INT( displayed_pictures )
177         STATS_INT( lost_pictures )
178         STATS_INT( decoded_audio )
179         STATS_INT( played_abuffers )
180         STATS_INT( lost_abuffers )
181         STATS_INT( sent_packets )
182         STATS_INT( sent_bytes )
183         STATS_FLOAT( send_bitrate )
184 #undef STATS_INT
185 #undef STATS_FLOAT
186     }
187     if( p_input )
188         vlc_object_release( p_input );
189     return 1;
190 }
191
192 static int vlclua_input_add_subtitle( lua_State *L )
193 {
194     input_thread_t *p_input = vlclua_get_input_internal( L );
195     if( !p_input )
196         return luaL_error( L, "can't add subtitle: no current input" );
197     if( !lua_isstring( L, 1 ) )
198         return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
199     const char *psz_url = luaL_checkstring( L, 1 );
200     input_AddSubtitle( p_input, psz_url, false );
201     vlc_object_release( p_input );
202     return 1;
203 }
204
205 /*****************************************************************************
206  * Lua bindings
207  *****************************************************************************/
208 static const luaL_Reg vlclua_input_reg[] = {
209     { "info", vlclua_input_info },
210     { "is_playing", vlclua_input_is_playing },
211     { "get_title", vlclua_input_get_title },
212     { "metas", vlclua_input_metas },
213     { "stats", vlclua_input_stats },
214     { "add_subtitle", vlclua_input_add_subtitle },
215     { NULL, NULL }
216 };
217
218 void luaopen_input( lua_State *L )
219 {
220     lua_newtable( L );
221     luaL_register( L, NULL, vlclua_input_reg );
222     lua_setfield( L, -2, "input" );
223 }