[vlc-devel] [PATCH 6/9] lua: add functions to the player/playlist API

Romain Vimont rom1v at videolabs.io
Mon Mar 4 11:53:26 CET 2019


Many features were handled from LUA via VLC variables.

Expose functions for the new player and playlist instead.
---
 modules/lua/libs/input.c    | 447 ++++++++++++++++++++++++++++++++++++
 modules/lua/libs/playlist.c |  45 ++++
 2 files changed, 492 insertions(+)

diff --git a/modules/lua/libs/input.c b/modules/lua/libs/input.c
index cae6747ddf..ad8813a61d 100644
--- a/modules/lua/libs/input.c
+++ b/modules/lua/libs/input.c
@@ -32,6 +32,7 @@
 #endif
 
 #include <vlc_common.h>
+#include <vlc_es.h>
 #include <vlc_meta.h>
 #include <vlc_url.h>
 #include <vlc_playlist_legacy.h>
@@ -100,6 +101,421 @@ static int vlclua_input_is_playing( lua_State *L )
     return 1;
 }
 
+static int vlclua_player_get_title_index(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    ssize_t idx = vlc_player_GetSelectedTitleIdx(player);
+    vlc_player_Unlock(player);
+
+    lua_pushinteger(L, idx);
+    return 1;
+}
+
+static int vlclua_player_get_titles_count(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    const struct vlc_player_title_list *titles =
+            vlc_player_GetTitleList(player);
+    size_t count = titles ? vlc_player_title_list_GetCount(titles) : 0;
+    vlc_player_Unlock(player);
+
+    lua_pushinteger(L, count);
+    return 1;
+}
+
+static int vlclua_player_title_next(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_SelectNextTitle(player);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_title_prev(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_SelectPrevTitle(player);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_title_goto(lua_State *L)
+{
+    int idx = luaL_checkinteger(L, 1);
+
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_SelectTitleIdx(player, idx);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_get_chapter_index(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    ssize_t idx = vlc_player_GetSelectedChapterIdx(player);
+    vlc_player_Unlock(player);
+
+    lua_pushinteger(L, idx);
+    return 1;
+}
+
+static int vlclua_player_get_chapters_count(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    const struct vlc_player_title *current_title =
+        vlc_player_GetSelectedTitle(player);
+
+    size_t count = current_title ? current_title->chapter_count : 0;
+    vlc_player_Unlock(player);
+
+    lua_pushinteger(L, count);
+    return 1;
+}
+
+static int vlclua_player_chapter_next(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_SelectNextChapter(player);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_chapter_prev(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_SelectPrevChapter(player);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_chapter_goto(lua_State *L)
+{
+    int idx = luaL_checkinteger(L, 1);
+
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_SelectChapterIdx(player, idx);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_get_time(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_tick_t time = vlc_player_GetTime(player);
+    vlc_player_Unlock(player);
+
+    lua_pushinteger(L, US_FROM_VLC_TICK(time));
+    return 1;
+}
+
+static int vlclua_player_get_position(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    float pos = vlc_player_GetPosition(player);
+    vlc_player_Unlock(player);
+
+    lua_pushnumber(L, pos);
+    return 1;
+}
+
+static int vlclua_player_get_rate(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    float rate = vlc_player_GetRate(player);
+    vlc_player_Unlock(player);
+
+    lua_pushnumber(L, rate);
+    return 1;
+}
+
+static int vlclua_player_set_rate(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    float rate = luaL_checknumber(L, 1);
+
+    vlc_player_Lock(player);
+    vlc_player_ChangeRate(player, rate);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_increment_rate(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_IncrementRate(player);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_decrement_rate(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_DecrementRate(player);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_get_tracks_(lua_State *L,
+                                     enum es_format_category_e cat)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+
+    size_t count = vlc_player_GetTrackCount(player, cat);
+    lua_createtable(L, count, 0);
+
+    for (size_t i = 0; i < count; ++i)
+    {
+        const struct vlc_player_track *track =
+                vlc_player_GetTrackAt(player, cat, i);
+        if (!track) {
+            continue;
+        }
+
+        lua_newtable(L);
+
+        lua_pushinteger(L, vlc_es_id_GetInputId(track->es_id));
+        lua_setfield(L, -2, "id");
+
+        lua_pushstring(L, track->name);
+        lua_setfield(L, -2, "name");
+
+        lua_pushboolean(L, track->selected);
+        lua_setfield(L, -2, "selected");
+
+        lua_rawseti(L, -2, i + 1);
+    }
+
+    vlc_player_Unlock(player);
+
+    return 1;
+}
+
+static int vlclua_player_get_video_tracks(lua_State *L)
+{
+    return vlclua_player_get_tracks_(L, VIDEO_ES);
+}
+
+static int vlclua_player_get_audio_tracks(lua_State *L)
+{
+    return vlclua_player_get_tracks_(L, AUDIO_ES);
+}
+
+static int vlclua_player_get_spu_tracks(lua_State *L)
+{
+    return vlclua_player_get_tracks_(L, SPU_ES);
+}
+
+static const struct vlc_player_track *
+FindTrack(vlc_player_t *player, enum es_format_category_e cat, int id)
+{
+    size_t count = vlc_player_GetTrackCount(player, cat);
+    for (size_t i = 0; i < count; ++i)
+    {
+        const struct vlc_player_track *track =
+                vlc_player_GetTrackAt(player, cat, i);
+        if (id == vlc_es_id_GetInputId(track->es_id))
+            return track;
+    }
+    return NULL;
+}
+
+static int vlclua_player_toggle_track_(lua_State *L,
+                                       enum es_format_category_e cat,
+                                       int id)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+
+    const struct vlc_player_track *track = FindTrack(player, cat, id);
+    if (track) {
+        if (track->selected)
+            vlc_player_UnselectTrack(player, track->es_id);
+        else
+            vlc_player_SelectTrack(player, track->es_id);
+    }
+
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_toggle_video_track(lua_State *L)
+{
+    int id = luaL_checkinteger(L, 1);
+    return vlclua_player_toggle_track_(L, VIDEO_ES, id);
+}
+
+static int vlclua_player_toggle_audio_track(lua_State *L)
+{
+    int id = luaL_checkinteger(L, 1);
+    return vlclua_player_toggle_track_(L, AUDIO_ES, id);
+}
+
+static int vlclua_player_toggle_spu_track(lua_State *L)
+{
+    int id = luaL_checkinteger(L, 1);
+    return vlclua_player_toggle_track_(L, SPU_ES, id);
+}
+
+static int vlclua_player_next_video_frame(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_NextVideoFrame(player);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_seek_by_pos_(lua_State *L,
+                                      enum vlc_player_whence whence)
+{
+    float position = luaL_checknumber(L, 1);
+
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_SeekByPos(player, position, VLC_PLAYER_SEEK_PRECISE, whence);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_seek_by_pos_absolute(lua_State *L)
+{
+    return vlclua_player_seek_by_pos_(L, VLC_PLAYER_WHENCE_ABSOLUTE);
+}
+
+static int vlclua_player_seek_by_pos_relative(lua_State *L)
+{
+    return vlclua_player_seek_by_pos_(L, VLC_PLAYER_WHENCE_RELATIVE);
+}
+
+static int vlclua_player_seek_by_time_(lua_State *L,
+                                       enum vlc_player_whence whence)
+{
+    int usec = luaL_checkinteger(L, 1);
+    vlc_tick_t time = VLC_TICK_FROM_US(usec);
+
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_player_SeekByTime(player, time, VLC_PLAYER_SEEK_PRECISE, whence);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_seek_by_time_absolute(lua_State *L)
+{
+    return vlclua_player_seek_by_time_(L, VLC_PLAYER_WHENCE_ABSOLUTE);
+}
+
+static int vlclua_player_seek_by_time_relative(lua_State *L)
+{
+    return vlclua_player_seek_by_time_(L, VLC_PLAYER_WHENCE_RELATIVE);
+}
+
+static int vlclua_player_get_audio_delay(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_tick_t delay = vlc_player_GetAudioDelay(player);
+    vlc_player_Unlock(player);
+
+    double delay_sec = secf_from_vlc_tick(delay);
+
+    lua_pushnumber(L, delay_sec);
+    return 1;
+}
+
+static int vlclua_player_set_audio_delay(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    double delay_sec = luaL_checknumber(L, 1);
+    vlc_tick_t delay = vlc_tick_from_sec(delay_sec);
+
+    vlc_player_Lock(player);
+    vlc_player_SetAudioDelay(player, delay, VLC_PLAYER_WHENCE_ABSOLUTE);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
+static int vlclua_player_get_subtitle_delay(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    vlc_player_Lock(player);
+    vlc_tick_t delay = vlc_player_GetSubtitleDelay(player);
+    vlc_player_Unlock(player);
+
+    double delay_sec = secf_from_vlc_tick(delay);
+
+    lua_pushnumber(L, delay_sec);
+    return 1;
+}
+
+static int vlclua_player_set_subtitle_delay(lua_State *L)
+{
+    vlc_player_t *player = vlclua_get_player_internal(L);
+
+    double delay_sec = luaL_checknumber(L, 1);
+    vlc_tick_t delay = vlc_tick_from_sec(delay_sec);
+
+    vlc_player_Lock(player);
+    vlc_player_SetSubtitleDelay(player, delay, VLC_PLAYER_WHENCE_ABSOLUTE);
+    vlc_player_Unlock(player);
+
+    return 0;
+}
+
 static int vlclua_input_metas_internal( lua_State *L, input_item_t *p_item )
 {
     if( !p_item )
@@ -401,6 +817,37 @@ static const luaL_Reg vlclua_input_reg[] = {
     { "item", vlclua_input_item_get_current },
     { "add_subtitle", vlclua_input_add_subtitle_path },
     { "add_subtitle_mrl", vlclua_input_add_subtitle_mrl },
+    { "get_title_index", vlclua_player_get_title_index },
+    { "get_titles_count", vlclua_player_get_titles_count },
+    { "title_next", vlclua_player_title_next },
+    { "title_prev", vlclua_player_title_prev },
+    { "title_goto", vlclua_player_title_goto },
+    { "get_chapter_index", vlclua_player_get_chapter_index },
+    { "get_chapters_count", vlclua_player_get_chapters_count },
+    { "chapter_next", vlclua_player_chapter_next },
+    { "chapter_prev", vlclua_player_chapter_prev },
+    { "chapter_goto", vlclua_player_chapter_goto },
+    { "get_time", vlclua_player_get_time },
+    { "get_position", vlclua_player_get_position },
+    { "get_rate", vlclua_player_get_rate },
+    { "set_rate", vlclua_player_set_rate },
+    { "increment_rate", vlclua_player_increment_rate },
+    { "decrement_rate", vlclua_player_decrement_rate },
+    { "get_video_tracks", vlclua_player_get_video_tracks },
+    { "get_audio_tracks", vlclua_player_get_audio_tracks },
+    { "get_spu_tracks", vlclua_player_get_spu_tracks },
+    { "toggle_video_track", vlclua_player_toggle_video_track },
+    { "toggle_audio_track", vlclua_player_toggle_audio_track },
+    { "toggle_spu_track", vlclua_player_toggle_spu_track },
+    { "next_video_frame", vlclua_player_next_video_frame },
+    { "seek_by_pos_absolute", vlclua_player_seek_by_pos_absolute },
+    { "seek_by_pos_relative", vlclua_player_seek_by_pos_relative },
+    { "seek_by_time_absolute", vlclua_player_seek_by_time_absolute },
+    { "seek_by_time_relative", vlclua_player_seek_by_time_relative },
+    { "get_audio_delay", vlclua_player_get_audio_delay },
+    { "set_audio_delay", vlclua_player_set_audio_delay },
+    { "get_subtitle_delay", vlclua_player_get_subtitle_delay },
+    { "set_subtitle_delay", vlclua_player_set_subtitle_delay },
     { NULL, NULL }
 };
 
diff --git a/modules/lua/libs/playlist.c b/modules/lua/libs/playlist.c
index 73c334e927..956da0a677 100644
--- a/modules/lua/libs/playlist.c
+++ b/modules/lua/libs/playlist.c
@@ -184,6 +184,34 @@ static int vlclua_playlist_loop(lua_State *L)
     return vlclua_playlist_repeat_(L, VLC_PLAYLIST_PLAYBACK_REPEAT_ALL);
 }
 
+static int vlclua_playlist_get_repeat(lua_State *L)
+{
+    vlc_playlist_t *playlist = vlclua_get_playlist_internal(L);
+
+    vlc_playlist_Lock(playlist);
+    enum vlc_playlist_playback_repeat repeat =
+            vlc_playlist_GetPlaybackRepeat(playlist);
+    bool result = repeat != VLC_PLAYLIST_PLAYBACK_REPEAT_NONE;
+    vlc_playlist_Unlock(playlist);
+
+    lua_pushboolean(L, result);
+    return 1;
+}
+
+static int vlclua_playlist_get_loop(lua_State *L)
+{
+    vlc_playlist_t *playlist = vlclua_get_playlist_internal(L);
+
+    vlc_playlist_Lock(playlist);
+    enum vlc_playlist_playback_repeat repeat =
+            vlc_playlist_GetPlaybackRepeat(playlist);
+    bool result = repeat == VLC_PLAYLIST_PLAYBACK_REPEAT_ALL;
+    vlc_playlist_Unlock(playlist);
+
+    lua_pushboolean(L, result);
+    return 1;
+}
+
 static int vlclua_playlist_random(lua_State *L)
 {
     vlc_playlist_t *playlist = vlclua_get_playlist_internal(L);
@@ -218,6 +246,20 @@ static int vlclua_playlist_random(lua_State *L)
     return 1;
 }
 
+static int vlclua_playlist_get_random(lua_State *L)
+{
+    vlc_playlist_t *playlist = vlclua_get_playlist_internal(L);
+
+    vlc_playlist_Lock(playlist);
+    enum vlc_playlist_playback_order order =
+            vlc_playlist_GetPlaybackOrder(playlist);
+    bool result = order == VLC_PLAYLIST_PLAYBACK_ORDER_RANDOM;
+    vlc_playlist_Unlock(playlist);
+
+    lua_pushboolean(L, result);
+    return 1;
+}
+
 static int vlclua_playlist_gotoitem(lua_State *L)
 {
     uint64_t id = luaL_checkinteger(L, 1);
@@ -547,6 +589,9 @@ static const luaL_Reg vlclua_playlist_reg[] = {
     { "repeat_", vlclua_playlist_repeat }, // ... provide repeat_ too.
     { "loop", vlclua_playlist_loop },
     { "random", vlclua_playlist_random },
+    { "get_repeat", vlclua_playlist_get_repeat },
+    { "get_loop", vlclua_playlist_get_loop },
+    { "get_random", vlclua_playlist_get_random },
 #if LUA_VERSION_NUM < 502
     { "goto", vlclua_playlist_gotoitem },
 #endif
-- 
2.20.1



More information about the vlc-devel mailing list