[vlc-devel] [PATCH] lua: Expose media tracks through lua bindings

Hugo Beauzée-Luyssen hugo at beauzee.fr
Mon Jan 2 16:31:21 CET 2017


---
 modules/lua/libs/input.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++-
 share/lua/README.txt     |  28 +++++++++++
 2 files changed, 150 insertions(+), 1 deletion(-)

diff --git a/modules/lua/libs/input.c b/modules/lua/libs/input.c
index 6d14ebce78..9fd49dbc36 100644
--- a/modules/lua/libs/input.c
+++ b/modules/lua/libs/input.c
@@ -382,6 +382,127 @@ 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 );
+
+#define PUSH_NUM( val, name ) \
+    lua_pushnumber( L, val ); \
+    lua_setfield( L, -2, name );
+
+static const char* orientation_to_str(video_orientation_t orient)
+{
+    switch (orient)
+    {
+    case ORIENT_TOP_LEFT:
+        return "topleft";
+    case ORIENT_TOP_RIGHT:
+        return "topright";
+    case ORIENT_BOTTOM_LEFT:
+        return "bottomleft";
+    case ORIENT_BOTTOM_RIGHT:
+        return "bottomright";
+    case ORIENT_LEFT_TOP:
+        return "lefttop";
+    case ORIENT_LEFT_BOTTOM:
+        return "leftbottom";
+    case ORIENT_RIGHT_TOP:
+        return "righttop";
+    case ORIENT_RIGHT_BOTTOM:
+        return "rightbottom";
+    default:
+        vlc_assert_unreachable();
+    }
+}
+
+static const char* projection_to_str(video_projection_mode_t proj)
+{
+    switch (proj)
+    {
+    case PROJECTION_MODE_RECTANGULAR:
+        return "rectangular";
+    case PROJECTION_MODE_EQUIRECTANGULAR:
+        return "equirectangular";
+    case PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD:
+        return "cubemap";
+    default:
+        vlc_assert_unreachable();
+    }
+}
+
+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" );
+        lua_pushlstring( L, (const char*)&p_es->i_original_fourcc, 4 );
+        lua_setfield( L, -2, "original_fourcc" );
+        PUSH_INT( p_es->i_id, "id" );
+        PUSH_INT( p_es->i_profile, "profile" );
+        PUSH_INT( p_es->i_level, "level" );
+        PUSH_INT( p_es->i_bitrate, "bitrate" );
+        PUSH_STR( p_es->psz_language, "language" );
+        PUSH_STR( p_es->psz_description, "description" );
+
+        switch (p_es->i_cat)
+        {
+        //FIXME: Expose constants to avoid magic numbers
+        case VIDEO_ES:
+            PUSH_STR( "video", "type" );
+            lua_newtable( L );
+            PUSH_INT( p_es->video.i_visible_width, "width" );
+            PUSH_INT( p_es->video.i_visible_height, "height" );
+            PUSH_INT( p_es->video.i_sar_num, "sar_num" );
+            PUSH_INT( p_es->video.i_sar_den, "sar_den" );
+            PUSH_INT( p_es->video.i_frame_rate, "framerate" );
+            PUSH_INT( p_es->video.i_frame_rate_base, "framerate_den" );
+            PUSH_STR( orientation_to_str( p_es->video.orientation ), "orientation" );
+            PUSH_STR( projection_to_str( p_es->video.projection_mode ), "projection" );
+            PUSH_NUM( p_es->video.pose.f_yaw_degrees, "yaw" );
+            PUSH_NUM( p_es->video.pose.f_pitch_degrees, "pitch" );
+            PUSH_NUM( p_es->video.pose.f_roll_degrees, "roll" );
+            PUSH_NUM( p_es->video.pose.f_fov_degrees, "fov" );
+            lua_setfield( L, -2, "video" );
+            break;
+        case AUDIO_ES:
+            PUSH_STR( "audio", "type" );
+            lua_newtable( L );
+            PUSH_INT( p_es->audio.i_channels, "channels" );
+            PUSH_INT( p_es->audio.i_rate, "rate" );
+            lua_setfield( L, -2, "audio" );
+            break;
+        case SPU_ES:
+            PUSH_STR( "spu", "type" );
+            lua_newtable( L );
+            PUSH_STR( p_es->subs.psz_encoding ? p_es->subs.psz_encoding : "", "encoding" );
+            lua_setfield( L, -2, "spu" );
+            break;
+        default:
+            PUSH_STR( "unknown", "type" );
+            break;
+        }
+        lua_rawseti( L, -2, i + 1 );
+    }
+    vlc_mutex_unlock( &p_item->lock );
+    return 1;
+}
+
+#undef PUSH_INT
+#undef PUSH_STR
+
 /*****************************************************************************
  * Lua bindings
  *****************************************************************************/
@@ -408,6 +529,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,7 +553,6 @@ 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);
diff --git a/share/lua/README.txt b/share/lua/README.txt
index d726fcf095..d407d934ca 100644
--- a/share/lua/README.txt
+++ b/share/lua/README.txt
@@ -132,6 +132,34 @@ input.item(): Get the current input item. Input item methods are:
     .send_bitrate
     .played_abuffers
     .lost_abuffers
+  :tracks(): Get information about the current tracks. This is a table with the following fields:
+    .codec
+    .original_fourcc
+    .id
+    .profile
+    .level
+    .bitrate
+    .language
+    .description
+    .type ("video", "audio", "spu" or "unknown")
+    .video (when type == "video")
+        .width
+        .height
+        .sar_num
+        .sar_den
+        .framerate
+        .framerate_den
+        .orientation
+        .projection
+        .yaw
+        .pitch
+        .roll
+        .fov
+    .audio (when type == "audio")
+        .channels
+        .rate
+    .spu (when type == "spu")
+        .encoding
 
 Messages
 --------
-- 
2.11.0



More information about the vlc-devel mailing list