#include "../vlc.h"
#include "../libs.h"
+static const luaL_Reg vlclua_input_reg[];
+static const luaL_Reg vlclua_input_item_reg[];
+
input_thread_t * vlclua_get_input_internal( lua_State *L )
{
playlist_t *p_playlist = vlclua_get_playlist_internal( L );
- PL_LOCK;
- input_thread_t *p_input = p_playlist->p_input;
- if( p_input ) vlc_object_hold( p_input );
- PL_UNLOCK;
+ input_thread_t *p_input = playlist_CurrentInput( p_playlist );
vlclua_release_playlist_internal( p_playlist );
return p_input;
}
static int vlclua_input_info( lua_State *L )
{
input_thread_t * p_input = vlclua_get_input_internal( L );
- vlc_object_lock( p_input );
int i_cat;
int i;
if( !p_input ) return vlclua_error( L );
}
lua_settable( L, -3 );
}
- vlc_object_unlock( p_input );
vlc_object_release( p_input );
return 1;
}
-static int vlclua_is_playing( lua_State *L )
+static int vlclua_input_is_playing( lua_State *L )
{
input_thread_t * p_input = vlclua_get_input_internal( L );
lua_pushboolean( L, !!p_input );
+ if( p_input )
+ vlc_object_release( p_input );
return 1;
}
-static int vlclua_get_title( lua_State *L )
+static int vlclua_input_get_title( lua_State *L )
{
input_thread_t *p_input = vlclua_get_input_internal( L );
if( !p_input )
return 1;
}
+static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
+{
+ if( !p_item )
+ {
+ lua_pushnil( L );
+ return 1;
+ }
+
+ lua_newtable( L );
+ char *psz_meta;
+
+#define PUSH_META( n, m ) \
+ psz_meta = input_item_GetMeta( p_item, vlc_meta_ ## n ); \
+ lua_pushstring( L, psz_meta ); \
+ lua_setfield( L, -2, m ); \
+ free( psz_meta )
+
+ PUSH_META( Title, "title" );
+ PUSH_META( Artist, "artist" );
+ PUSH_META( Genre, "genre" );
+ PUSH_META( Copyright, "copyright" );
+ PUSH_META( Album, "album" );
+ PUSH_META( TrackNumber, "track_number" );
+ PUSH_META( Description, "description" );
+ PUSH_META( Rating, "rating" );
+ PUSH_META( Date, "date" );
+ PUSH_META( Setting, "setting" );
+ PUSH_META( URL, "url" );
+ PUSH_META( Language, "language" );
+ PUSH_META( NowPlaying, "now_playing" );
+ PUSH_META( Publisher, "publisher" );
+ PUSH_META( EncodedBy, "encoded_by" );
+ PUSH_META( ArtworkURL, "artwork_url" );
+ PUSH_META( TrackID, "track_id" );
+
+#undef PUSH_META
+
+ return 1;
+}
+
+static int vlclua_input_metas( lua_State *L )
+{
+ input_thread_t *p_input = vlclua_get_input_internal( L );
+ input_item_t *p_item = p_input && p_input->p
+ ? input_GetItem( p_input ) : NULL;
+ vlclua_input_metas_internal( L, p_item );
+ if( p_input )
+ vlc_object_release( p_input );
+ return 1;
+}
+
static int vlclua_input_stats( lua_State *L )
{
input_thread_t *p_input = vlclua_get_input_internal( L );
- input_item_t *p_item = p_input && p_input->p ? input_GetItem( p_input ) : NULL;
+ input_item_t *p_item = p_input && p_input->p
+ ? input_GetItem( p_input ) : NULL;
lua_newtable( L );
if( p_item )
{
+ vlc_mutex_lock( &p_item->p_stats->lock );
#define STATS_INT( n ) lua_pushinteger( L, p_item->p_stats->i_ ## n ); \
lua_setfield( L, -2, #n );
#define STATS_FLOAT( n ) lua_pushnumber( L, p_item->p_stats->f_ ## n ); \
STATS_FLOAT( input_bitrate )
STATS_INT( demux_read_bytes )
STATS_FLOAT( demux_bitrate )
+ STATS_INT( demux_corrupted )
+ STATS_INT( demux_discontinuity )
STATS_INT( decoded_video )
STATS_INT( displayed_pictures )
STATS_INT( lost_pictures )
STATS_FLOAT( send_bitrate )
#undef STATS_INT
#undef STATS_FLOAT
+ vlc_mutex_unlock( &p_item->p_stats->lock );
}
+ if( p_input )
+ vlc_object_release( p_input );
+ return 1;
+}
+
+static int vlclua_input_add_subtitle( lua_State *L )
+{
+ input_thread_t *p_input = vlclua_get_input_internal( L );
+ if( !p_input )
+ return luaL_error( L, "can't add subtitle: no current input" );
+ if( !lua_isstring( L, 1 ) )
+ return luaL_error( L, "vlc.input.add_subtitle() usage: (url)" );
+ const char *psz_url = luaL_checkstring( L, 1 );
+ input_AddSubtitle( p_input, psz_url, false );
+ vlc_object_release( p_input );
return 1;
}
/*****************************************************************************
- *
+ * Input items
+ *****************************************************************************/
+
+static input_item_t* vlclua_input_item_get_internal( lua_State *L )
+{
+ input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
+ input_item_t *p_item = *pp_item;
+
+ if( !p_item )
+ luaL_error( L, "script went completely foobar" );
+
+ return p_item;
+}
+
+/* Garbage collection of an input_item_t */
+static int vlclua_input_item_delete( lua_State *L )
+{
+ input_item_t **pp_item = luaL_checkudata( L, 1, "input_item" );
+ input_item_t *p_item = *pp_item;
+
+ if( !p_item )
+ return luaL_error( L, "script went completely foobar" );
+
+ *pp_item = NULL;
+ vlc_gc_decref( p_item );
+
+ return 1;
+}
+
+static int vlclua_input_item_get_current( lua_State *L )
+{
+ input_thread_t *p_input = vlclua_get_input_internal( L );
+ input_item_t *p_item = ( p_input && p_input->p ) ? input_GetItem( p_input ) : NULL;
+ if( !p_item )
+ {
+ lua_pushnil( L );
+ if( p_input ) vlc_object_release( p_input );
+ return 1;
+ }
+
+ vlc_gc_incref( p_item );
+
+ input_item_t **pp = lua_newuserdata( L, sizeof( void* ) );
+ *pp = p_item;
+
+ if( luaL_newmetatable( L, "input_item" ) )
+ {
+ lua_newtable( L );
+ luaL_register( L, NULL, vlclua_input_item_reg );
+ lua_setfield( L, -2, "__index" );
+ lua_pushcfunction( L, vlclua_input_item_delete );
+ lua_setfield( L, -2, "__gc" );
+ }
+
+ lua_setmetatable( L, -2 );
+
+ if( p_input ) vlc_object_release( p_input );
+ return 1;
+}
+
+static int vlclua_input_item_metas( lua_State *L )
+{
+ vlclua_input_metas_internal( L, vlclua_input_item_get_internal( L ) );
+ return 1;
+}
+
+static int vlclua_input_item_set_meta( lua_State *L )
+{
+ input_item_t *p_item = vlclua_input_item_get_internal( L );
+ lua_settop( L, 1 + 2 ); // two arguments
+ const char *psz_name = luaL_checkstring( L, 2 ),
+ *psz_value = luaL_checkstring( L, 3 );
+
+#define META_TYPE( n ) { #n, vlc_meta_ ## n }
+ static const struct
+ {
+ const char *psz_name;
+ vlc_meta_type_t type;
+ } pp_meta_types[] = {
+ META_TYPE( Title ),
+ META_TYPE( Artist ),
+ META_TYPE( Genre ),
+ META_TYPE( Copyright ),
+ META_TYPE( Album ),
+ META_TYPE( TrackNumber ),
+ META_TYPE( Description ),
+ META_TYPE( Rating ),
+ META_TYPE( Date ),
+ META_TYPE( Setting ),
+ META_TYPE( URL ),
+ META_TYPE( Language ),
+ META_TYPE( NowPlaying ),
+ META_TYPE( Publisher ),
+ META_TYPE( EncodedBy ),
+ META_TYPE( ArtworkURL ),
+ META_TYPE( TrackID ),
+ };
+#undef META_TYPE
+
+ vlc_meta_type_t type = vlc_meta_Title;
+ bool ok = false;
+ for( unsigned i = 0; i < VLC_META_TYPE_COUNT; i++ )
+ {
+ if( !strcasecmp( pp_meta_types[i].psz_name, psz_name ) )
+ {
+ type = pp_meta_types[i].type;
+ ok = true;
+ }
+ }
+
+ if( !ok )
+ return luaL_error( L, "unknown meta type '%s'", psz_name );
+
+ input_item_SetMeta( p_item, type, psz_value );
+
+ return 1;
+}
+
+/*****************************************************************************
+ * Lua bindings
*****************************************************************************/
static const luaL_Reg vlclua_input_reg[] = {
{ "info", vlclua_input_info },
- { "is_playing", vlclua_is_playing },
- { "get_title", vlclua_get_title },
+ { "is_playing", vlclua_input_is_playing },
+ { "get_title", vlclua_input_get_title },
+ { "metas", vlclua_input_metas },
+ { "item", vlclua_input_item_get_current },
{ "stats", vlclua_input_stats },
+ { "add_subtitle", vlclua_input_add_subtitle },
{ NULL, NULL }
};
luaL_register( L, NULL, vlclua_input_reg );
lua_setfield( L, -2, "input" );
}
+
+static const luaL_Reg vlclua_input_item_reg[] = {
+ { "metas", vlclua_input_item_metas },
+ { "set_meta", vlclua_input_item_set_meta },
+ { NULL, NULL }
+};