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