[vlc-devel] [PATCH 06/15] lua: Add a minimal medialib API

David Loiret loiret.d at gmail.com
Tue Jun 23 14:42:41 CEST 2020


From: Hugo Beauzée-Luyssen <hugo at beauzee.fr>

---
 include/vlc_media_library.h             |   1 +
 modules/lua/Makefile.am                 |   3 +-
 modules/lua/extension.c                 |   1 +
 modules/lua/intf.c                      |   1 +
 modules/lua/libs.h                      |   1 +
 modules/lua/libs/medialibrary.c         | 483 ++++++++++++++++++++++++
 modules/misc/medialibrary/entities.cpp  |   2 +
 share/Makefile.am                       |   1 +
 share/lua/intf/http.lua                 |   2 +
 share/lua/intf/modules/httprequests.lua |   1 -
 share/lua/intf/modules/medialib.lua     | 204 ++++++++++
 src/misc/medialibrary.c                 |   1 +
 12 files changed, 699 insertions(+), 2 deletions(-)
 create mode 100644 modules/lua/libs/medialibrary.c
 create mode 100644 share/lua/intf/modules/medialib.lua

diff --git a/include/vlc_media_library.h b/include/vlc_media_library.h
index fd82a71a23..9fb65fe45b 100644
--- a/include/vlc_media_library.h
+++ b/include/vlc_media_library.h
@@ -203,6 +203,7 @@ typedef struct vlc_ml_media_t
     uint32_t i_playcount;
     time_t i_last_played_date;
     char* psz_title;
+    char* psz_filename;
 
     vlc_ml_thumbnail_t thumbnails[VLC_ML_THUMBNAIL_SIZE_COUNT];
 
diff --git a/modules/lua/Makefile.am b/modules/lua/Makefile.am
index 39852e5116..e389669697 100644
--- a/modules/lua/Makefile.am
+++ b/modules/lua/Makefile.am
@@ -32,7 +32,8 @@ liblua_plugin_la_SOURCES = \
 	lua/libs/io.c \
 	lua/libs/errno.c \
 	lua/libs/rand.c \
-	lua/libs/renderers.c
+	lua/libs/renderers.c \
+	lua/libs/medialibrary.c
 
 if HAVE_WIN32
 liblua_plugin_la_SOURCES += lua/libs/win.c
diff --git a/modules/lua/extension.c b/modules/lua/extension.c
index 998ba5a0cd..2d2c486c1e 100644
--- a/modules/lua/extension.c
+++ b/modules/lua/extension.c
@@ -835,6 +835,7 @@ static lua_State* GetLuaState( extensions_manager_t *p_mgr,
         luaopen_errno( L );
         luaopen_rand( L );
         luaopen_rd( L );
+        luaopen_ml( L );
 #if defined(_WIN32) && !VLC_WINSTORE_APP
         luaopen_win( L );
 #endif
diff --git a/modules/lua/intf.c b/modules/lua/intf.c
index 409432e8ec..5ca4cb58a7 100644
--- a/modules/lua/intf.c
+++ b/modules/lua/intf.c
@@ -272,6 +272,7 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
     luaopen_errno( L );
     luaopen_rand( L );
     luaopen_rd( L );
+    luaopen_ml( L );
 #if defined(_WIN32) && !VLC_WINSTORE_APP
     luaopen_win( L );
 #endif
diff --git a/modules/lua/libs.h b/modules/lua/libs.h
index ba8cbb2496..11ed10c411 100644
--- a/modules/lua/libs.h
+++ b/modules/lua/libs.h
@@ -50,6 +50,7 @@ void luaopen_vlcio( lua_State *L );
 void luaopen_errno( lua_State *L );
 void luaopen_rand( lua_State *L );
 void luaopen_rd( lua_State *L );
+void luaopen_ml( lua_State *L );
 #ifdef _WIN32
 void luaopen_win( lua_State *L );
 #endif
diff --git a/modules/lua/libs/medialibrary.c b/modules/lua/libs/medialibrary.c
new file mode 100644
index 0000000000..5e7ea28fe5
--- /dev/null
+++ b/modules/lua/libs/medialibrary.c
@@ -0,0 +1,483 @@
+/*****************************************************************************
+ * medialibrary.c
+ *****************************************************************************
+ * Copyright (C) 2020 the VideoLAN team
+ *
+ * Authors: Hugo Beauzée-Luyssen <hugo at beauzee.fr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_media_library.h>
+
+#include "../libs.h"
+
+static void vlclua_ml_push_media( lua_State *L, const vlc_ml_media_t *media )
+{
+    lua_newtable( L );
+    lua_pushinteger( L, media->i_id );
+    lua_setfield( L, -2, "id" );
+    lua_pushstring( L, media->psz_title );
+    lua_setfield( L, -2, "title" );
+    lua_pushstring( L, media->psz_filename );
+    lua_setfield( L, -2, "filename" );
+    lua_pushinteger( L, media->i_type );
+    lua_setfield( L, -2, "type" );
+    lua_pushinteger( L, media->i_duration );
+    lua_setfield( L, -2, "duration" );
+    lua_pushboolean( L, media->thumbnails[VLC_ML_THUMBNAIL_SMALL].b_generated );
+    lua_setfield( L, -2, "hasThumbnail" );
+    switch ( media->i_subtype )
+    {
+        case VLC_ML_MEDIA_SUBTYPE_ALBUMTRACK:
+        {
+            lua_newtable( L );
+
+            lua_pushinteger( L, media->album_track.i_artist_id );
+            lua_setfield( L, -2, "artistId" );
+            lua_pushinteger( L, media->album_track.i_album_id );
+            lua_setfield( L, -2, "albumId" );
+            lua_pushinteger( L, media->album_track.i_genre_id );
+            lua_setfield( L, -2, "genreId" );
+            lua_pushinteger( L, media->album_track.i_track_nb );
+            lua_setfield( L, -2, "trackId" );
+            lua_pushinteger( L, media->album_track.i_disc_nb );
+            lua_setfield( L, -2, "discId" );
+
+            lua_setfield( L, -2, "albumTrack" );
+            lua_pushstring( L, "albumTrack" );
+            break;
+        }
+        case VLC_ML_MEDIA_SUBTYPE_MOVIE:
+        {
+            lua_newtable( L );
+
+            lua_pushstring( L, media->movie.psz_summary );
+            lua_setfield( L, -2, "summary" );
+
+            lua_setfield( L, -2, "movie" );
+            lua_pushstring( L, "movie" );
+            break;
+        }
+        case VLC_ML_MEDIA_SUBTYPE_SHOW_EPISODE:
+        {
+            lua_newtable( L );
+
+            lua_pushstring( L, media->show_episode.psz_summary );
+            lua_setfield( L, -2, "summary" );
+            lua_pushinteger( L, media->show_episode.i_episode_nb );
+            lua_setfield( L, -2, "episodeId" );
+            lua_pushinteger( L, media->show_episode.i_season_number );
+            lua_setfield( L, -2, "seasonId" );
+
+            lua_setfield( L, -2, "showEpisode" );
+            lua_pushstring( L, "showEpisode" );
+            break;
+        }
+        default:
+            lua_pushstring( L, "unknown" );
+            break;
+    }
+    lua_setfield( L, -2, "subType" );
+
+    for ( size_t i = 0; i < media->p_files->i_nb_items; ++i )
+    {
+        if ( media->p_files->p_items[i].i_type == VLC_ML_FILE_TYPE_MAIN )
+        {
+            lua_pushstring( L, media->p_files->p_items[i].psz_mrl );
+            lua_setfield( L, -2, "mrl" );
+            break;
+        }
+    }
+
+    if ( media->i_type != VLC_ML_MEDIA_TYPE_VIDEO )
+        return;
+
+    const char* quality = NULL;
+    uint32_t maxChannels = 0;
+    for ( size_t i = 0; i < media->p_tracks->i_nb_items; ++i )
+    {
+        vlc_ml_media_track_t* track = &media->p_tracks->p_items[i];
+        if ( track->i_type == VLC_ML_TRACK_TYPE_VIDEO && quality == NULL )
+        {
+            uint32_t width = track->v.i_width > track->v.i_height ?
+                        track->v.i_width : track->v.i_height;
+
+            if ( width >= 3840 )
+            {
+                quality = "4K";
+                break;
+            }
+            else if ( width >= 1920 )
+            {
+                quality = "1080p";
+                break;
+            }
+            else if ( width >= 1280 )
+            {
+                quality = "720p";
+                break;
+            }
+        }
+        if ( track->i_type == VLC_ML_TRACK_TYPE_AUDIO )
+        {
+            if ( track->a.i_nbChannels > maxChannels )
+                maxChannels = track->a.i_nbChannels;
+        }
+    }
+    if ( quality == NULL )
+        quality = "SD";
+    lua_pushstring( L, quality );
+    lua_setfield( L, -2, "quality" );
+    lua_pushinteger( L, maxChannels );
+    lua_setfield( L, -2, "nbChannels" );
+}
+
+static void vlclua_ml_push_album( lua_State* L, const vlc_ml_album_t *album )
+{
+    lua_newtable( L );
+    lua_pushinteger( L, album->i_id );
+    lua_setfield( L, -2, "id" );
+    lua_pushstring( L, album->psz_title );
+    lua_setfield( L, -2, "title" );
+    lua_pushboolean( L, album->thumbnails[VLC_ML_THUMBNAIL_SMALL].b_generated );
+    lua_setfield( L, -2, "hasThumbnail" );
+    lua_pushinteger( L, album->i_artist_id );
+    lua_setfield( L, -2, "artistId" );
+    lua_pushstring( L, album->psz_artist );
+    lua_setfield( L, -2, "artist" );
+    lua_pushinteger( L, album->i_nb_tracks );
+    lua_setfield( L, -2, "nbTracks" );
+    lua_pushinteger( L, album->i_duration );
+    lua_setfield( L, -2, "duration" );
+    lua_pushinteger( L, album->i_year );
+    lua_setfield( L, -2, "releaseYear" );
+}
+
+static void vlclua_ml_push_artist( lua_State* L, const vlc_ml_artist_t* artist )
+{
+    lua_newtable( L );
+    lua_pushinteger( L, artist->i_id );
+    lua_setfield( L, -2, "id" );
+    lua_pushstring( L, artist->psz_name);
+    lua_setfield( L, -2, "name" );
+    lua_pushboolean( L, artist->thumbnails[VLC_ML_THUMBNAIL_SMALL].b_generated );
+    lua_setfield( L, -2, "hasThumbnail" );
+    lua_pushinteger( L, artist->i_nb_tracks );
+    lua_setfield( L, -2, "nbTracks" );
+    lua_pushinteger( L, artist->i_nb_album );
+    lua_setfield( L, -2, "nbAlbums" );
+}
+
+static void vlclua_ml_push_genre( lua_State* L, const vlc_ml_genre_t* genre )
+{
+    lua_newtable( L );
+    lua_pushinteger( L, genre->i_id );
+    lua_setfield( L, -2, "id" );
+    lua_pushstring( L, genre->psz_name );
+    lua_setfield( L, -2, "name" );
+    lua_pushinteger( L, genre->i_nb_tracks );
+    lua_setfield( L, -2, "nbTracks" );
+}
+
+static int vlclua_ml_list_media( lua_State* L, vlc_ml_media_list_t* list )
+{
+    if ( list == NULL )
+        return luaL_error( L, "Failed to list media" );
+    lua_createtable( L, list->i_nb_items, 0 );
+    for ( size_t i = 0; i < list->i_nb_items; ++i )
+    {
+        vlclua_ml_push_media( L, &list->p_items[i] );
+        lua_rawseti( L, -2, i + 1 );
+    }
+    vlc_ml_release( list );
+    return 1;
+}
+
+static void vlclua_ml_assign_params( lua_State *L, vlc_ml_query_params_t *params )
+{
+    params->i_nbResults = lua_tointeger( L, 1 );
+    params->i_offset = lua_tointeger( L, 2 );
+    params->b_desc = lua_toboolean( L, 3 );
+    params->psz_pattern = NULL;
+    params->i_sort = VLC_ML_SORTING_DEFAULT;
+}
+
+static int vlclua_ml_video( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_media_list_t* list = vlc_ml_list_video_media( ml, &params );
+    return vlclua_ml_list_media( L, list );
+}
+
+static int vlclua_ml_audio( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_media_list_t* list = vlc_ml_list_audio_media( ml, &params );
+    return vlclua_ml_list_media( L, list );
+}
+
+static int vlclua_ml_list_albums( lua_State *L, vlc_ml_album_list_t *list )
+{
+    if ( list == NULL )
+        return luaL_error( L, "Failed to list albums" );
+    lua_createtable( L, list->i_nb_items, 0 );
+    for ( size_t i = 0; i < list->i_nb_items; ++i )
+    {
+        vlclua_ml_push_album( L, &list->p_items[i] );
+        lua_rawseti( L, -2, i + 1 );
+    }
+    vlc_ml_release( list );
+    return 1;
+}
+
+static int vlclua_ml_list_all_albums( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_album_list_t* list = vlc_ml_list_albums( ml, &params );
+    return vlclua_ml_list_albums( L, list );
+}
+
+static int vlclua_ml_list_artist_albums( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    lua_Integer artistId = luaL_checkinteger( L, 4 );
+    vlc_ml_album_list_t* list = vlc_ml_list_artist_albums( ml, &params, artistId );
+    return vlclua_ml_list_albums( L, list );
+}
+
+static int vlclua_ml_list_genre_albums( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    lua_Integer genreId = luaL_checkinteger( L, 4 );
+    vlc_ml_album_list_t* list = vlc_ml_list_genre_albums( ml, &params, genreId );
+    return vlclua_ml_list_albums( L, list );
+}
+
+static int vlclua_ml_get_album( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    lua_Integer albumId = luaL_checkinteger( L, 1 );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_album_t* album = vlc_ml_get_album( ml, albumId );
+    if ( album == NULL )
+        return luaL_error( L, "Failed to get album" );
+    vlclua_ml_push_album( L, album );
+    vlc_ml_release( album );
+    return 1;
+}
+
+static int vlclua_ml_list_artists( lua_State *L, vlc_ml_artist_list_t *list )
+{
+    if ( list == NULL )
+        return luaL_error( L, "Failed to list artists" );
+    lua_createtable( L, list->i_nb_items, 0 );
+    for ( size_t i = 0; i < list->i_nb_items; ++i )
+    {
+        vlclua_ml_push_artist( L, &list->p_items[i] );
+        lua_rawseti( L, -2, i + 1 );
+    }
+    vlc_ml_release( list );
+    return 1;
+}
+
+static int vlclua_ml_list_all_artists( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_artist_list_t* list = vlc_ml_list_artists( ml, &params, true );
+    return vlclua_ml_list_artists( L, list );
+}
+
+static int vlclua_ml_list_genre_artists( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    lua_Integer genreId = luaL_checkinteger( L, 4 );
+    vlc_ml_artist_list_t* list = vlc_ml_list_genre_artists( ml, &params, genreId );
+    return vlclua_ml_list_artists( L, list );
+}
+
+static int vlclua_ml_get_artist( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    lua_Integer artistId = luaL_checkinteger( L, 1 );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_artist_t* artist = vlc_ml_get_artist( ml, artistId );
+    if ( artist == NULL )
+        return luaL_error( L, "Failed to get artist" );
+    vlclua_ml_push_artist( L, artist );
+    vlc_ml_release( artist );
+    return 1;
+}
+
+static int vlclua_ml_list_genres( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_genre_list_t* list = vlc_ml_list_genres( ml, &params );
+    if ( list == NULL )
+        return luaL_error( L, "Failed to list genres" );
+    lua_createtable( L, list->i_nb_items, 0 );
+    for ( size_t i = 0; i < list->i_nb_items; ++i )
+    {
+        vlclua_ml_push_genre( L, &list->p_items[i] );
+        lua_rawseti( L, -2, i + 1 );
+    }
+    vlc_ml_release( list );
+    return 1;
+}
+
+static int vlclua_ml_get_genre( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    lua_Integer genreId = luaL_checkinteger( L, 1 );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_genre_t* genre = vlc_ml_get_genre( ml, genreId );
+    if ( genre == NULL )
+        return luaL_error( L, "Failed to get genre" );
+    vlclua_ml_push_genre( L, genre );
+    vlc_ml_release( genre );
+    return 1;
+}
+
+static int vlclua_ml_get_media_thumbnail( lua_State *L )
+{
+    if( lua_gettop( L ) < 1 ) return vlclua_error( L );
+
+    lua_Integer mediaId = luaL_checkinteger( L, 1 );
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_media_t* media = vlc_ml_get_media( ml, mediaId );
+    if ( media == NULL ||
+         media->thumbnails[VLC_ML_THUMBNAIL_SMALL].psz_mrl == NULL )
+    {
+        vlc_ml_release( media );
+        return 0;
+    }
+    lua_pushstring( L, media->thumbnails[VLC_ML_THUMBNAIL_SMALL].psz_mrl );
+    vlc_ml_release( media );
+    return 1;
+}
+
+static int vlclua_ml_get_artist_thumbnail( lua_State *L )
+{
+    if( lua_gettop( L ) < 1 ) return vlclua_error( L );
+
+    lua_Integer artistId = luaL_checkinteger( L, 1 );
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_artist_t* artist = vlc_ml_get_artist( ml, artistId );
+    if ( artist == NULL ||
+         artist->thumbnails[VLC_ML_THUMBNAIL_SMALL].psz_mrl == NULL )
+    {
+        vlc_ml_release( artist );
+        return 0;
+    }
+    lua_pushstring( L, artist->thumbnails[VLC_ML_THUMBNAIL_SMALL].psz_mrl );
+    vlc_ml_release( artist );
+    return 1;
+}
+
+static int vlclua_ml_get_album_thumbnail( lua_State *L )
+{
+    if( lua_gettop( L ) < 1 ) return vlclua_error( L );
+
+    lua_Integer albumId = luaL_checkinteger( L, 1 );
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_album_t* album = vlc_ml_get_album( ml, albumId );
+    if ( album == NULL ||
+         album->thumbnails[VLC_ML_THUMBNAIL_SMALL].psz_mrl == NULL )
+    {
+        vlc_ml_release( album );
+        return 0;
+    }
+    lua_pushstring( L, album->thumbnails[VLC_ML_THUMBNAIL_SMALL].psz_mrl );
+    vlc_ml_release( album );
+    return 1;
+}
+
+static int vlclua_ml_list_album_tracks( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_query_params_t params;
+    vlclua_ml_assign_params( L, &params );
+    lua_Integer albumId = luaL_checkinteger( L, 4 );
+    vlc_ml_media_list_t *list = vlc_ml_list_album_tracks( ml, &params, albumId );
+    return vlclua_ml_list_media( L, list );
+}
+
+static int vlclua_ml_reload( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    vlc_medialibrary_t* ml = vlc_ml_instance_get( p_this );
+    vlc_ml_reload_folder( ml, NULL );
+    return 0;
+}
+
+static const luaL_Reg vlclua_ml_reg[] = {
+    { "video", vlclua_ml_video },
+    { "audio", vlclua_ml_audio },
+    { "media_thumbnail", vlclua_ml_get_media_thumbnail },
+    { "albums", vlclua_ml_list_all_albums },
+    { "album", vlclua_ml_get_album },
+    { "artists", vlclua_ml_list_all_artists },
+    { "artist", vlclua_ml_get_artist },
+    { "genres", vlclua_ml_list_genres },
+    { "genre", vlclua_ml_get_genre },
+    { "album_tracks", vlclua_ml_list_album_tracks },
+    { "artist_albums", vlclua_ml_list_artist_albums },
+    { "genre_albums", vlclua_ml_list_genre_albums },
+    { "genre_artists", vlclua_ml_list_genre_artists },
+    { "artist_thumbnail", vlclua_ml_get_artist_thumbnail },
+    { "album_thumbnail", vlclua_ml_get_album_thumbnail },
+    { "reload", vlclua_ml_reload },
+    { NULL, NULL }
+};
+
+void luaopen_ml( lua_State *L )
+{
+    lua_newtable( L );
+    luaL_register( L, NULL, vlclua_ml_reg );
+    lua_setfield( L, -2, "ml" );
+}
diff --git a/modules/misc/medialibrary/entities.cpp b/modules/misc/medialibrary/entities.cpp
index 8c567f954a..ab0c4b2a96 100644
--- a/modules/misc/medialibrary/entities.cpp
+++ b/modules/misc/medialibrary/entities.cpp
@@ -246,6 +246,8 @@ bool Convert( const medialibrary::IMedia* input, vlc_ml_media_t& output )
     output.p_files = ml_convert_list<vlc_ml_file_list_t, vlc_ml_file_t>( files );
     if ( output.p_files == nullptr )
         return false;
+    if ( strdup_helper( input->fileName(), output.psz_filename ) == false )
+        return false;
 
     if ( convertTracks( input, output ) == false )
         return false;
diff --git a/share/Makefile.am b/share/Makefile.am
index 49db4aafd9..7fc3b323ec 100644
--- a/share/Makefile.am
+++ b/share/Makefile.am
@@ -320,6 +320,7 @@ EXTRA_DIST += \
 	lua/intf/luac.lua \
 	lua/intf/modules/host.lua \
 	lua/intf/modules/httprequests.lua \
+	lua/intf/modules/medialib.lua \
 	lua/intf/telnet.lua \
 	lua/meta/README.txt \
 	lua/meta/art/README.txt \
diff --git a/share/lua/intf/http.lua b/share/lua/intf/http.lua
index 04f60b091a..ddb229842a 100644
--- a/share/lua/intf/http.lua
+++ b/share/lua/intf/http.lua
@@ -31,6 +31,7 @@ Configuration options:
 
 
 require "common"
+require "medialib"
 
 vlc.msg.info("Lua HTTP interface")
 
@@ -334,3 +335,4 @@ password = vlc.var.inherit(nil,"http-password")
 h = vlc.httpd()
 load_dir( http_dir )
 a = h:handler("/art",nil,password,callback_art,nil)
+medialibHandlers = medialib.setupHandlers(h, password)
diff --git a/share/lua/intf/modules/httprequests.lua b/share/lua/intf/modules/httprequests.lua
index 33f2836a00..d8adbd3c78 100644
--- a/share/lua/intf/modules/httprequests.lua
+++ b/share/lua/intf/modules/httprequests.lua
@@ -510,4 +510,3 @@ getstatus = function (includecategories)
     end
     return s
 end
-
diff --git a/share/lua/intf/modules/medialib.lua b/share/lua/intf/modules/medialib.lua
new file mode 100644
index 0000000000..1782432f1e
--- /dev/null
+++ b/share/lua/intf/modules/medialib.lua
@@ -0,0 +1,204 @@
+--[==========================================================================[
+ medialib.lua: Exposes the media library to lua interfaces
+--[==========================================================================[
+ Copyright (C) 2020 the VideoLAN team
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+--]==========================================================================]
+
+module("medialib",package.seeall)
+local dkjson = require ("dkjson")
+
+function wrapResult(code, content, mime)
+    -- Automatically encode the content in json when the request didn't fail
+    -- and when no mime is provided
+    if code >= 200 and code < 300 and not mime then
+        content = dkjson.encode (content, { indent = true })
+    end
+    if not mime then
+        mime = "text/plain"
+    end
+    local res= "Status: " .. tostring(code) .. "\r\n" ..
+    "Content-Type: " .. mime .. "\r\n" ..
+    "Content-Length: " .. #content ..
+    "\r\n\r\n" .. content
+--    print(res)
+    return res
+end
+
+listVideo = function(get)
+    local v = vlc.ml.video(get['limit'], get['offset'], get['desc'])
+    return wrapResult(200, v)
+end
+
+listAudio = function(get)
+    local a = vlc.ml.audio(get['limit'], get['offset'], get['desc'])
+    return wrapResult(200, a)
+end
+
+listAlbums = function(get)
+    local a = vlc.ml.albums(get['limit'], get['offset'], get['desc'])
+    return wrapResult(200, a)
+end
+
+getAlbum = function(get)
+    local albumId = get['albumId']
+    if not albumId then
+        return wrapResult(400, "Missing albumId" )
+    end
+    local a = vlc.ml.album(albumId)
+    return wrapResult(200, a)
+end
+
+getAlbumTracks = function(get)
+    local albumId = get['albumId']
+    if not albumId then
+        return wrapResult(400, "Missing albumId" )
+    end
+    local tracks = vlc.ml.album_tracks(get['limit'], get['offset'], get['desc'], albumId)
+    return wrapResult(200, tracks)
+end
+
+listArtists = function(get)
+    local a = vlc.ml.artists(get['limit'], get['offset'], get['desc'])
+    return wrapResult(200, a)
+end
+
+getArtist = function(get)
+    local artistId = get['artistId']
+    if not artistId then
+        return wrapResult(400, "Missing artistId" )
+    end
+    local a = vlc.ml.artist(artistId)
+    return wrapResult(200, a)
+end
+
+getArtistAlbums = function(get)
+    local artistId = get['artistId']
+    if not artistId then
+        return wrapResult(400, "Missing artistId" )
+    end
+    local albums = vlc.ml.artist_albums(get['limit'], get['offset'], get['desc'], artistId)
+    return wrapResult(200, albums)
+end
+
+listGenres = function(get)
+    local a = vlc.ml.genres(get['limit'], get['offset'], get['desc'])
+    return wrapResult(200, a)
+end
+
+getGenre = function(get)
+    local genreId = get['genreId']
+    if not genreId then
+        return wrapResult(400, "Missing genreId" )
+    end
+    local a = vlc.ml.genre(genreId)
+    return wrapResult(200, a)
+end
+
+getGenreAlbums = function(get)
+    local genreId = get['genreId']
+    if not genreId then
+        return wrapResult(400, "Missing genreId" )
+    end
+    local albums = vlc.ml.genre_albums(get['limit'], get['offset'], get['desc'], genreId)
+    return wrapResult(200, albums)
+end
+
+getGenreArtists = function(get)
+    local genreId = get['genreId']
+    if not genreId then
+        return wrapResult(400, "Missing genreId" )
+    end
+    local artists = vlc.ml.genre_artists(get['limit'], get['offset'], get['desc'], genreId)
+    return wrapResult(200, artists)
+end
+
+wrapThumbnail = function(thumbMrl)
+    if not thumbMrl then
+        return wrapResult(404, "Thumbnail not found")
+    end
+    filename = vlc.strings.make_path(thumbMrl)
+    local windowsdrive = string.match(filename, "^/%a:/.+$")  --match windows drive letter
+    if windowsdrive then
+        filename = string.sub(filename, 2)  --remove starting forward slash before the drive letter
+    end
+    local size = vlc.net.stat(filename).size
+    local ext = string.match(filename,"%.([^%.]-)$")
+    local raw = io.open(filename, 'rb'):read("*a")
+    return wrapResult(200, raw, "image/jpeg")
+end
+
+getThumbnail = function(get)
+    local mediaId = get['mediaId']
+    if not mediaId then
+        return wrapResult(400, "Missing mediaId")
+    end
+    local thumbMrl = vlc.ml.media_thumbnail(mediaId)
+    return wrapThumbnail(thumbMrl)
+end
+
+getArtistThumbnail = function(get)
+    local artistId = get['artistId']
+    if not artistId then
+        return wrapResult(400, "Missing artistId")
+    end
+    local thumbMrl = vlc.ml.artist_thumbnail(artistId)
+    return wrapThumbnail(thumbMrl)
+end
+
+getAlbumThumbnail = function(get)
+    local albumId = get['albumId']
+    if not albumId then
+        return wrapResult(400, "Missing albumId")
+    end
+    local thumbMrl = vlc.ml.album_thumbnail(albumId)
+    return wrapThumbnail(thumbMrl)
+end
+
+initialReload = false
+
+function wrapHandler(func)
+    return function(data, url, request)
+        if not initialReload then
+            vlc.ml.reload()
+            initialReload = true
+        end
+        local get = parse_url_request(request)
+        return func(get)
+    end
+end
+
+setupHandlers = function(h, password)
+    handlers = {
+        h:handler("/medialib/video", nil, password, wrapHandler(listVideo), nil),
+        h:handler("/medialib/audio", nil, password, wrapHandler(listAudio), nil),
+        h:handler("/medialib/thumbnail", nil, password, wrapHandler(getThumbnail), nil),
+        h:handler("/medialib/albums", nil, password, wrapHandler(listAlbums), nil),
+        h:handler("/medialib/album", nil, password, wrapHandler(getAlbum), nil),
+        h:handler("/medialib/album/thumbnail", nil, password, wrapHandler(getAlbumThumbnail), nil),
+        h:handler("/medialib/album/tracks", nil, password, wrapHandler(getAlbumTracks), nil),
+        h:handler("/medialib/artists", nil, password, wrapHandler(listArtists), nil),
+        h:handler("/medialib/artist", nil, password, wrapHandler(getArtist), nil),
+        h:handler("/medialib/artist/albums", nil, password, wrapHandler(getArtistAlbums), nil),
+        h:handler("/medialib/artist/thumbnail", nil, password, wrapHandler(getArtistThumbnail), nil),
+        h:handler("/medialib/genres", nil, password, wrapHandler(listGenres), nil),
+        h:handler("/medialib/genre", nil, password, wrapHandler(getGenre), nil),
+        h:handler("/medialib/genre/albums", nil, password, wrapHandler(getGenreAlbums), nil),
+        h:handler("/medialib/genre/artists", nil, password, wrapHandler(getGenreArtists), nil),
+    }
+    -- Return the handlers so they don't immediatly get garbage collected
+    return handlers
+end
diff --git a/src/misc/medialibrary.c b/src/misc/medialibrary.c
index 140f055b5f..a8a7e1951a 100644
--- a/src/misc/medialibrary.c
+++ b/src/misc/medialibrary.c
@@ -172,6 +172,7 @@ static void vlc_ml_media_release_inner( vlc_ml_media_t* p_media )
 {
     vlc_ml_file_list_release( p_media->p_files );
     vlc_ml_media_release_tracks_inner( p_media->p_tracks );
+    free( p_media->psz_filename );
     free( p_media->psz_title );
     vlc_ml_thumbnails_release( p_media->thumbnails );
     switch( p_media->i_subtype )
-- 
2.18.0



More information about the vlc-devel mailing list