[vlc-devel] [PATCH] lua: Expose media tracks through lua bindings
Hugo Beauzée-Luyssen
hugo at beauzee.fr
Fri Dec 30 13:56:11 CET 2016
---
modules/lua/extension.c | 1 +
modules/lua/intf.c | 1 +
modules/lua/libs.h | 1 +
modules/lua/libs/input.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 73 insertions(+), 1 deletion(-)
diff --git a/modules/lua/extension.c b/modules/lua/extension.c
index 92c2eb7fa7..ffd90bd5bf 100644
--- a/modules/lua/extension.c
+++ b/modules/lua/extension.c
@@ -825,6 +825,7 @@ static lua_State* GetLuaState( extensions_manager_t *p_mgr,
luaopen_config( L );
luaopen_dialog( L, p_ext );
luaopen_input( L );
+ luaopen_input_constants( L );
luaopen_msg( L );
if( vlclua_fd_init( L, &p_ext->p_sys->dtable ) )
{
diff --git a/modules/lua/intf.c b/modules/lua/intf.c
index 46738dca76..97d437e842 100644
--- a/modules/lua/intf.c
+++ b/modules/lua/intf.c
@@ -261,6 +261,7 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
luaopen_config( L );
luaopen_httpd( L );
luaopen_input( L );
+ luaopen_input_constants( L );
luaopen_msg( L );
luaopen_misc( L );
if( vlclua_fd_init( L, &p_sys->dtable ) )
diff --git a/modules/lua/libs.h b/modules/lua/libs.h
index c553bbf2ac..5877b9c0b8 100644
--- a/modules/lua/libs.h
+++ b/modules/lua/libs.h
@@ -28,6 +28,7 @@ void luaopen_config( lua_State * );
void luaopen_dialog( lua_State *, void * );
void luaopen_httpd( lua_State * );
void luaopen_input( lua_State * );
+void luaopen_input_constants( lua_State* );
void luaopen_msg( lua_State * );
void luaopen_misc( lua_State * );
void luaopen_object( lua_State * );
diff --git a/modules/lua/libs/input.c b/modules/lua/libs/input.c
index 6d14ebce78..0da4c74a39 100644
--- a/modules/lua/libs/input.c
+++ b/modules/lua/libs/input.c
@@ -382,6 +382,61 @@ static int vlclua_input_item_set_meta( lua_State *L )
return 1;
}
+#define PUSH_INT( val, name ) \
+ lua_pushinteger( L, val ); \
+ lua_setfield( L, -2, name );
+
+#define PUSH_STR( val, name ) \
+ lua_pushstring( L, val ); \
+ lua_setfield( L, -2, name );
+
+static int vlclua_input_item_tracks( lua_State *L )
+{
+ input_item_t *p_item = vlclua_input_item_get_internal( L );
+ lua_newtable( L );
+
+ vlc_mutex_lock( &p_item->lock );
+ for ( int i = 0; i < p_item->i_es; ++i )
+ {
+ const es_format_t* p_es = p_item->es[i];
+
+ lua_newtable( L );
+
+ lua_pushlstring( L, (const char*)&p_es->i_codec, 4 );
+ lua_setfield( L, -2, "codec" );
+ PUSH_INT( p_es->i_id, "id" );
+ PUSH_INT( p_es->i_profile, "profile" );
+ PUSH_INT( p_es->i_level, "level" );
+
+ switch (p_es->i_cat)
+ {
+ //FIXME: Expose constants to avoid magic numbers
+ case VIDEO_ES:
+ PUSH_INT( VIDEO_ES, "type" );
+ PUSH_INT( p_es->video.i_visible_width, "width" );
+ PUSH_INT( p_es->video.i_visible_height, "height" );
+ break;
+ case AUDIO_ES:
+ PUSH_INT( AUDIO_ES, "type" );
+ PUSH_INT( p_es->audio.i_channels, "channels" );
+ PUSH_INT( p_es->audio.i_rate, "rate" );
+ break;
+ case SPU_ES:
+ PUSH_INT( SPU_ES, "type" );
+ break;
+ default:
+ PUSH_INT( UNKNOWN_ES, "type" );
+ break;
+ }
+ lua_rawseti( L, -2, i );
+ }
+ vlc_mutex_unlock( &p_item->lock );
+ return 1;
+}
+
+#undef PUSH_INT
+#undef PUSH_STR
+
/*****************************************************************************
* Lua bindings
*****************************************************************************/
@@ -408,6 +463,7 @@ static const luaL_Reg vlclua_input_item_reg[] = {
{ "duration", vlclua_input_item_duration },
{ "stats", vlclua_input_item_stats },
{ "info", vlclua_input_item_info },
+ { "tracks", vlclua_input_item_tracks },
{ NULL, NULL }
};
@@ -431,10 +487,23 @@ static int vlclua_input_item_get( lua_State *L, input_item_t *p_item )
return 1;
}
-
void luaopen_input_item( lua_State *L, input_item_t *item )
{
assert(item);
vlclua_input_item_get( L, item );
lua_setfield( L, -2, "item" );
}
+
+void luaopen_input_constants( lua_State *L )
+{
+ lua_newtable(L);
+ lua_pushinteger( L, VIDEO_ES );
+ lua_setfield( L, -2, "video" );
+ lua_pushinteger( L, AUDIO_ES );
+ lua_setfield( L, -2, "audio" );
+ lua_pushinteger( L, SPU_ES );
+ lua_setfield( L, -2, "spu" );
+ lua_pushinteger( L, UNKNOWN_ES );
+ lua_setfield( L, -2, "unknown" );
+ lua_setfield( L, -2, "tracks" );
+}
--
2.11.0
More information about the vlc-devel
mailing list