]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/input.c
7eab977ea7464995d8c87c561e10df4e961d9a21
[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 = p_playlist->p_input;
53     if( p_input ) vlc_object_yield( p_input );
54     pl_Release( p_playlist );
55     return p_input;
56 }
57
58 static int vlclua_input_info( lua_State *L )
59 {
60     input_thread_t * p_input = vlclua_get_input_internal( L );
61     int i_cat;
62     int i;
63     if( !p_input ) return vlclua_error( L );
64     //vlc_mutex_lock( &input_GetItem(p_input)->lock );
65     i_cat = input_GetItem(p_input)->i_categories;
66     lua_createtable( L, 0, i_cat );
67     for( i = 0; i < i_cat; i++ )
68     {
69         info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
70         int i_infos = p_category->i_infos;
71         int j;
72         lua_pushstring( L, p_category->psz_name );
73         lua_createtable( L, 0, i_infos );
74         for( j = 0; j < i_infos; j++ )
75         {
76             info_t *p_info = p_category->pp_infos[j];
77             lua_pushstring( L, p_info->psz_name );
78             lua_pushstring( L, p_info->psz_value );
79             lua_settable( L, -3 );
80         }
81         lua_settable( L, -3 );
82     }
83     //vlc_object_release( p_input );
84     return 1;
85 }
86
87 static int vlclua_is_playing( lua_State *L )
88 {
89     input_thread_t * p_input = vlclua_get_input_internal( L );
90     lua_pushboolean( L, !!p_input );
91     return 1;
92 }
93
94 static int vlclua_get_title( lua_State *L )
95 {
96     input_thread_t *p_input = vlclua_get_input_internal( L );
97     if( !p_input )
98         lua_pushnil( L );
99     else
100     {
101         lua_pushstring( L, input_GetItem(p_input)->psz_name );
102         vlc_object_release( p_input );
103     }
104     return 1;
105 }
106
107 static int vlclua_input_stats( lua_State *L )
108 {
109     input_thread_t *p_input = vlclua_get_input_internal( L );
110     input_item_t *p_item = p_input && p_input->p ? input_GetItem( p_input ) : NULL;
111     lua_newtable( L );
112     if( p_item )
113     {
114 #define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
115                        lua_setfield( L, -2, #n );
116 #define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
117                          lua_setfield( L, -2, #n );
118         STATS_INT( read_bytes )
119         STATS_FLOAT( input_bitrate )
120         STATS_INT( demux_read_bytes )
121         STATS_FLOAT( demux_bitrate )
122         STATS_INT( decoded_video )
123         STATS_INT( displayed_pictures )
124         STATS_INT( lost_pictures )
125         STATS_INT( decoded_audio )
126         STATS_INT( played_abuffers )
127         STATS_INT( lost_abuffers )
128         STATS_INT( sent_packets )
129         STATS_INT( sent_bytes )
130         STATS_FLOAT( send_bitrate )
131 #undef STATS_INT
132 #undef STATS_FLOAT
133     }
134     return 1;
135 }
136
137 /*****************************************************************************
138  *
139  *****************************************************************************/
140 static const luaL_Reg vlclua_input_reg[] = {
141     { "info", vlclua_input_info },
142     { "is_playing", vlclua_is_playing },
143     { "get_title", vlclua_get_title },
144     { "stats", vlclua_input_stats },
145     { NULL, NULL }
146 };
147
148 void luaopen_input( lua_State *L )
149 {
150     lua_newtable( L );
151     luaL_register( L, NULL, vlclua_input_reg );
152     lua_setfield( L, -2, "input" );
153 }