[vlc-commits] Update to medialibrary API changes

Hugo Beauzée-Luyssen git at videolan.org
Tue Sep 24 14:34:14 CEST 2019


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Mon Jul  8 15:10:17 2019 +0200| [caf1a4af37bf3a9d2e8998db26bd4fd8b0ce70aa] | committer: Hugo Beauzée-Luyssen

Update to medialibrary API changes

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=caf1a4af37bf3a9d2e8998db26bd4fd8b0ce70aa
---

 contrib/src/medialibrary/rules.mak                 |  2 +-
 include/vlc_media_library.h                        |  4 +
 .../components/mediacenter/mlalbumtrackmodel.cpp   |  6 +-
 .../components/mediacenter/mlrecentsvideomodel.cpp |  3 -
 .../gui/qt/components/mediacenter/mlvideomodel.cpp |  3 -
 modules/misc/medialibrary/fs/directory.cpp         | 17 +++++
 modules/misc/medialibrary/fs/directory.h           |  1 +
 modules/misc/medialibrary/fs/fs.cpp                |  8 ++
 modules/misc/medialibrary/fs/fs.h                  |  3 +
 modules/misc/medialibrary/medialib.cpp             | 89 ++++++++++++++++------
 modules/misc/medialibrary/medialibrary.h           | 11 +--
 11 files changed, 107 insertions(+), 40 deletions(-)

diff --git a/contrib/src/medialibrary/rules.mak b/contrib/src/medialibrary/rules.mak
index 711797fbb9..dcf198e18c 100644
--- a/contrib/src/medialibrary/rules.mak
+++ b/contrib/src/medialibrary/rules.mak
@@ -1,4 +1,4 @@
-MEDIALIBRARY_HASH := 00d30ec824749b73bb7c5be6e22fe441785ba971
+MEDIALIBRARY_HASH := 18148bcaf26964d22ebdda26a475ed1ed533046b
 MEDIALIBRARY_VERSION := git-$(MEDIALIBRARY_HASH)
 MEDIALIBRARY_GITURL := https://code.videolan.org/videolan/medialibrary.git
 
diff --git a/include/vlc_media_library.h b/include/vlc_media_library.h
index e6205f31ca..07a2345836 100644
--- a/include/vlc_media_library.h
+++ b/include/vlc_media_library.h
@@ -671,6 +671,10 @@ typedef struct vlc_ml_event_t
             const vlc_ml_album_t* p_album;
             const vlc_ml_playlist_t* p_playlist;
             const vlc_ml_genre_t* p_genre;
+        } creation;
+        struct
+        {
+            int64_t i_entity_id;
         } modification;
         struct
         {
diff --git a/modules/gui/qt/components/mediacenter/mlalbumtrackmodel.cpp b/modules/gui/qt/components/mediacenter/mlalbumtrackmodel.cpp
index 788b656b0e..cd1965145d 100644
--- a/modules/gui/qt/components/mediacenter/mlalbumtrackmodel.cpp
+++ b/modules/gui/qt/components/mediacenter/mlalbumtrackmodel.cpp
@@ -163,10 +163,10 @@ void MLAlbumTrackModel::onVlcMlEvent(const vlc_ml_event_t* event)
     switch (event->i_type)
     {
         case VLC_ML_EVENT_MEDIA_ADDED:
-        case VLC_ML_EVENT_MEDIA_UPDATED:
-            if ( event->modification.p_media->i_subtype == VLC_ML_MEDIA_SUBTYPE_ALBUMTRACK )
+            if ( event->creation.p_media->i_subtype == VLC_ML_MEDIA_SUBTYPE_ALBUMTRACK )
                 m_need_reset = true;
             break;
+        case VLC_ML_EVENT_MEDIA_UPDATED:
         case VLC_ML_EVENT_MEDIA_DELETED:
             // FIXME: Not optimal, this will trigger a clean/refresh for video
             // media as well, but this needs fixing in the medialibrary
@@ -174,7 +174,7 @@ void MLAlbumTrackModel::onVlcMlEvent(const vlc_ml_event_t* event)
             break;
         case VLC_ML_EVENT_ALBUM_UPDATED:
             if ( m_parent.id != 0 && m_parent.type == VLC_ML_PARENT_ALBUM &&
-                 m_parent.id == event->modification.p_album->i_id )
+                 m_parent.id == event->modification.i_entity_id )
                 m_need_reset = true;
             break;
         case VLC_ML_EVENT_ALBUM_DELETED:
diff --git a/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.cpp b/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.cpp
index a1dc8ede7d..b588a9fb00 100644
--- a/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.cpp
+++ b/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.cpp
@@ -128,9 +128,6 @@ void MLRecentsVideoModel::onVlcMlEvent( const vlc_ml_event_t* event )
     {
         case VLC_ML_EVENT_MEDIA_ADDED:
         case VLC_ML_EVENT_MEDIA_UPDATED:
-            if ( event->modification.p_media->i_type == VLC_ML_MEDIA_TYPE_VIDEO )
-                m_need_reset = true;
-            break;
         case VLC_ML_EVENT_MEDIA_DELETED:
             m_need_reset = true;
             break;
diff --git a/modules/gui/qt/components/mediacenter/mlvideomodel.cpp b/modules/gui/qt/components/mediacenter/mlvideomodel.cpp
index c779a14736..c2395ac294 100644
--- a/modules/gui/qt/components/mediacenter/mlvideomodel.cpp
+++ b/modules/gui/qt/components/mediacenter/mlvideomodel.cpp
@@ -158,9 +158,6 @@ void MLVideoModel::onVlcMlEvent(const vlc_ml_event_t* event)
     {
         case VLC_ML_EVENT_MEDIA_ADDED:
         case VLC_ML_EVENT_MEDIA_UPDATED:
-            if ( event->modification.p_media->i_type == VLC_ML_MEDIA_TYPE_VIDEO )
-                m_need_reset = true;
-            break;
         case VLC_ML_EVENT_MEDIA_DELETED:
             m_need_reset = true;
             break;
diff --git a/modules/misc/medialibrary/fs/directory.cpp b/modules/misc/medialibrary/fs/directory.cpp
index 2e66e8311e..f370ef147c 100644
--- a/modules/misc/medialibrary/fs/directory.cpp
+++ b/modules/misc/medialibrary/fs/directory.cpp
@@ -24,7 +24,9 @@
 
 #include "directory.h"
 #include "file.h"
+#include "util.h"
 
+#include <algorithm>
 #include <assert.h>
 #include <vector>
 #include <system_error>
@@ -79,6 +81,21 @@ SDDirectory::device() const
     return m_device;
 }
 
+std::shared_ptr<IFile> SDDirectory::file(const std::string& mrl) const
+{
+    auto fs = files();
+    // Don't compare entire mrls, this might yield false negative when a
+    // device has multiple mountpoints.
+    auto fileName = utils::fileName( mrl );
+    auto it = std::find_if( cbegin( fs ), cend( fs ),
+                            [&fileName]( const std::shared_ptr<fs::IFile> f ) {
+                                return f->name() == fileName;
+                            });
+    if ( it == cend( fs ) )
+        throw std::runtime_error( mrl + " wasn't found in the directory" );
+    return *it;
+}
+
 struct metadata_request {
     vlc::threads::mutex lock;
     vlc::threads::condition_variable cond;
diff --git a/modules/misc/medialibrary/fs/directory.h b/modules/misc/medialibrary/fs/directory.h
index bc5395b396..948cd7eca2 100644
--- a/modules/misc/medialibrary/fs/directory.h
+++ b/modules/misc/medialibrary/fs/directory.h
@@ -39,6 +39,7 @@ public:
     const std::vector<std::shared_ptr<IFile>> &files() const override;
     const std::vector<std::shared_ptr<IDirectory>> &dirs() const override;
     std::shared_ptr<IDevice> device() const override;
+    std::shared_ptr<IFile> file( const std::string& mrl ) const override;
 
 private:
     void read() const;
diff --git a/modules/misc/medialibrary/fs/fs.cpp b/modules/misc/medialibrary/fs/fs.cpp
index a133979cf5..ccc7c86e2d 100644
--- a/modules/misc/medialibrary/fs/fs.cpp
+++ b/modules/misc/medialibrary/fs/fs.cpp
@@ -80,6 +80,14 @@ SDFileSystemFactory::createDirectory(const std::string &mrl)
     return std::make_shared<SDDirectory>(mrl, *this);
 }
 
+std::shared_ptr<IFile>
+SDFileSystemFactory::createFile(const std::string& mrl)
+{
+    auto dir = createDirectory(mrl);
+    assert(dir != nullptr);
+    return dir->file(mrl);
+}
+
 std::shared_ptr<IDevice>
 SDFileSystemFactory::createDevice(const std::string &uuid)
 {
diff --git a/modules/misc/medialibrary/fs/fs.h b/modules/misc/medialibrary/fs/fs.h
index 95e5956941..04c2e9e99f 100644
--- a/modules/misc/medialibrary/fs/fs.h
+++ b/modules/misc/medialibrary/fs/fs.h
@@ -53,6 +53,9 @@ public:
     std::shared_ptr<IDirectory>
     createDirectory(const std::string &mrl) override;
 
+    std::shared_ptr<fs::IFile>
+    createFile(const std::string& mrl) override;
+
     std::shared_ptr<IDevice>
     createDevice(const std::string &uuid) override;
 
diff --git a/modules/misc/medialibrary/medialib.cpp b/modules/misc/medialibrary/medialib.cpp
index 1c7dc6ef08..73509624b8 100644
--- a/modules/misc/medialibrary/medialib.cpp
+++ b/modules/misc/medialibrary/medialib.cpp
@@ -26,6 +26,7 @@
 #include <vlc_plugin.h>
 #include <vlc_url.h>
 #include <vlc_media_library.h>
+#include <vlc_dialog.h>
 #include "medialibrary.h"
 #include "fs/fs.h"
 
@@ -75,15 +76,16 @@ private:
 namespace
 {
 
-void assignToEvent( vlc_ml_event_t* ev, vlc_ml_media_t* m )    { ev->modification.p_media    = m; }
-void assignToEvent( vlc_ml_event_t* ev, vlc_ml_artist_t* a )   { ev->modification.p_artist   = a; }
-void assignToEvent( vlc_ml_event_t* ev, vlc_ml_album_t* a )    { ev->modification.p_album    = a; }
-void assignToEvent( vlc_ml_event_t* ev, vlc_ml_genre_t* g )    { ev->modification.p_genre    = g; }
-void assignToEvent( vlc_ml_event_t* ev, vlc_ml_playlist_t* p ) { ev->modification.p_playlist = p; }
+void assignToEvent( vlc_ml_event_t* ev, vlc_ml_media_t* m )    { ev->creation.p_media    = m; }
+void assignToEvent( vlc_ml_event_t* ev, vlc_ml_artist_t* a )   { ev->creation.p_artist   = a; }
+void assignToEvent( vlc_ml_event_t* ev, vlc_ml_album_t* a )    { ev->creation.p_album    = a; }
+void assignToEvent( vlc_ml_event_t* ev, vlc_ml_genre_t* g )    { ev->creation.p_genre    = g; }
+void assignToEvent( vlc_ml_event_t* ev, vlc_ml_playlist_t* p ) { ev->creation.p_playlist = p; }
 
 template <typename To, typename From>
-void wrapEntityEventCallback( vlc_medialibrary_module_t* ml,
-                              const std::vector<From>& entities, vlc_ml_event_type evType )
+void wrapEntityCreatedEventCallback( vlc_medialibrary_module_t* ml,
+                                     const std::vector<From>& entities,
+                                     vlc_ml_event_type evType )
 {
     vlc_ml_event_t ev;
     ev.i_type = evType;
@@ -100,6 +102,19 @@ void wrapEntityEventCallback( vlc_medialibrary_module_t* ml,
     }
 }
 
+void wrapEntityModifiedEventCallback( vlc_medialibrary_module_t* ml,
+                                      const std::vector<int64_t>& ids,
+                                      vlc_ml_event_type evType )
+{
+    vlc_ml_event_t ev;
+    ev.i_type = evType;
+    for ( const auto& id : ids )
+    {
+        ev.modification.i_entity_id = id;
+        ml->cbs->pf_send_event( ml, &ev );
+    }
+}
+
 void wrapEntityDeletedEventCallback( vlc_medialibrary_module_t* ml,
                                      const std::vector<int64_t>& ids, vlc_ml_event_type evType )
 {
@@ -116,12 +131,12 @@ void wrapEntityDeletedEventCallback( vlc_medialibrary_module_t* ml,
 
 void MediaLibrary::onMediaAdded( std::vector<medialibrary::MediaPtr> media )
 {
-    wrapEntityEventCallback<vlc_ml_media_t>( m_vlc_ml, media, VLC_ML_EVENT_MEDIA_ADDED );
+    wrapEntityCreatedEventCallback<vlc_ml_media_t>( m_vlc_ml, media, VLC_ML_EVENT_MEDIA_ADDED );
 }
 
-void MediaLibrary::onMediaModified( std::vector<medialibrary::MediaPtr> media )
+void MediaLibrary::onMediaModified( std::vector<int64_t> mediaIds )
 {
-    wrapEntityEventCallback<vlc_ml_media_t>( m_vlc_ml, media, VLC_ML_EVENT_MEDIA_UPDATED );
+    wrapEntityModifiedEventCallback( m_vlc_ml, mediaIds, VLC_ML_EVENT_MEDIA_UPDATED );
 }
 
 void MediaLibrary::onMediaDeleted( std::vector<int64_t> mediaIds )
@@ -131,12 +146,12 @@ void MediaLibrary::onMediaDeleted( std::vector<int64_t> mediaIds )
 
 void MediaLibrary::onArtistsAdded( std::vector<medialibrary::ArtistPtr> artists )
 {
-    wrapEntityEventCallback<vlc_ml_artist_t>( m_vlc_ml, artists, VLC_ML_EVENT_ARTIST_ADDED );
+    wrapEntityCreatedEventCallback<vlc_ml_artist_t>( m_vlc_ml, artists, VLC_ML_EVENT_ARTIST_ADDED );
 }
 
-void MediaLibrary::onArtistsModified( std::vector<medialibrary::ArtistPtr> artists )
+void MediaLibrary::onArtistsModified( std::vector<int64_t> artistIds )
 {
-    wrapEntityEventCallback<vlc_ml_artist_t>( m_vlc_ml, artists, VLC_ML_EVENT_ARTIST_UPDATED );
+    wrapEntityModifiedEventCallback( m_vlc_ml, artistIds, VLC_ML_EVENT_ARTIST_UPDATED );
 }
 
 void MediaLibrary::onArtistsDeleted( std::vector<int64_t> artistIds )
@@ -146,12 +161,12 @@ void MediaLibrary::onArtistsDeleted( std::vector<int64_t> artistIds )
 
 void MediaLibrary::onAlbumsAdded( std::vector<medialibrary::AlbumPtr> albums )
 {
-    wrapEntityEventCallback<vlc_ml_album_t>( m_vlc_ml, albums, VLC_ML_EVENT_ALBUM_ADDED );
+    wrapEntityCreatedEventCallback<vlc_ml_album_t>( m_vlc_ml, albums, VLC_ML_EVENT_ALBUM_ADDED );
 }
 
-void MediaLibrary::onAlbumsModified( std::vector<medialibrary::AlbumPtr> albums )
+void MediaLibrary::onAlbumsModified( std::vector<int64_t> albumIds )
 {
-    wrapEntityEventCallback<vlc_ml_album_t>( m_vlc_ml, albums, VLC_ML_EVENT_ALBUM_UPDATED );
+    wrapEntityModifiedEventCallback( m_vlc_ml, albumIds, VLC_ML_EVENT_ALBUM_UPDATED );
 }
 
 void MediaLibrary::onAlbumsDeleted( std::vector<int64_t> albumIds )
@@ -161,12 +176,12 @@ void MediaLibrary::onAlbumsDeleted( std::vector<int64_t> albumIds )
 
 void MediaLibrary::onPlaylistsAdded( std::vector<medialibrary::PlaylistPtr> playlists )
 {
-    wrapEntityEventCallback<vlc_ml_playlist_t>( m_vlc_ml, playlists, VLC_ML_EVENT_PLAYLIST_ADDED );
+    wrapEntityCreatedEventCallback<vlc_ml_playlist_t>( m_vlc_ml, playlists, VLC_ML_EVENT_PLAYLIST_ADDED );
 }
 
-void MediaLibrary::onPlaylistsModified( std::vector<medialibrary::PlaylistPtr> playlists )
+void MediaLibrary::onPlaylistsModified( std::vector<int64_t> playlistIds )
 {
-    wrapEntityEventCallback<vlc_ml_playlist_t>( m_vlc_ml, playlists, VLC_ML_EVENT_PLAYLIST_UPDATED );
+    wrapEntityModifiedEventCallback( m_vlc_ml, playlistIds, VLC_ML_EVENT_PLAYLIST_UPDATED );
 }
 
 void MediaLibrary::onPlaylistsDeleted( std::vector<int64_t> playlistIds )
@@ -176,12 +191,12 @@ void MediaLibrary::onPlaylistsDeleted( std::vector<int64_t> playlistIds )
 
 void MediaLibrary::onGenresAdded( std::vector<medialibrary::GenrePtr> genres )
 {
-    wrapEntityEventCallback<vlc_ml_genre_t>( m_vlc_ml, genres, VLC_ML_EVENT_GENRE_ADDED );
+    wrapEntityCreatedEventCallback<vlc_ml_genre_t>( m_vlc_ml, genres, VLC_ML_EVENT_GENRE_ADDED );
 }
 
-void MediaLibrary::onGenresModified( std::vector<medialibrary::GenrePtr> genres )
+void MediaLibrary::onGenresModified( std::vector<int64_t> genreIds )
 {
-    wrapEntityEventCallback<vlc_ml_genre_t>( m_vlc_ml, genres, VLC_ML_EVENT_GENRE_UPDATED );
+    wrapEntityModifiedEventCallback( m_vlc_ml, genreIds, VLC_ML_EVENT_GENRE_UPDATED );
 }
 
 void MediaLibrary::onGenresDeleted( std::vector<int64_t> genreIds )
@@ -302,6 +317,10 @@ void MediaLibrary::onMediaThumbnailReady( medialibrary::MediaPtr media,
     m_vlc_ml->cbs->pf_send_event( m_vlc_ml, &ev );
 }
 
+void MediaLibrary::onHistoryChanged( medialibrary::HistoryType )
+{
+}
+
 MediaLibrary::MediaLibrary( vlc_medialibrary_module_t* ml )
     : m_vlc_ml( ml )
 {
@@ -321,9 +340,8 @@ bool MediaLibrary::Start()
 
     auto userDir = vlc::wrap_cptr( config_GetUserDir( VLC_USERDATA_DIR ) );
     std::string mlDir = std::string{ userDir.get() } + "/ml/";
-    auto thumbnailsDir = mlDir + "thumbnails/";
 
-    auto initStatus = ml->initialize( mlDir + "ml.db", thumbnailsDir, this );
+    auto initStatus = ml->initialize( mlDir + "ml.db", mlDir + "/mlstorage/", this );
     switch ( initStatus )
     {
         case medialibrary::InitializeResult::AlreadyInitialized:
@@ -338,6 +356,27 @@ bool MediaLibrary::Start()
         case medialibrary::InitializeResult::Success:
             msg_Dbg( m_vlc_ml, "MediaLibrary successfully initialized" );
             break;
+        case medialibrary::InitializeResult::DbCorrupted:
+        {
+            auto res = vlc_dialog_wait_question(VLC_OBJECT( m_vlc_ml ),
+                VLC_DIALOG_QUESTION_NORMAL, _( "Ignore" ), _( "Recover" ),
+                _( "Recreate" ), _( "Media database corrupted" ),
+                "Your media database appears to be corrupted. You can try to "
+                "recover it, recreate it entirely, or ignore this error (the "
+                "mediacenter will be disabled)." );
+            switch ( res )
+            {
+                case 1:
+                    ml->clearDatabase( true );
+                    break;
+                case 2:
+                    ml->clearDatabase( false );
+                    break;
+                default:
+                    return false;
+            }
+            break;
+        }
     }
 
     ml->addParserService( std::make_shared<MetadataExtractor>( VLC_OBJECT( m_vlc_ml ) ) );
@@ -936,7 +975,7 @@ int MediaLibrary::getMeta( const medialibrary::IMedia& media, int meta, char** r
         *result = nullptr;
         return VLC_SUCCESS;
     }
-    *result = strdup( md.str().c_str() );
+    *result = strdup( md.asStr().c_str() );
     if ( *result == nullptr )
         return VLC_ENOMEM;
     return VLC_SUCCESS;
diff --git a/modules/misc/medialibrary/medialibrary.h b/modules/misc/medialibrary/medialibrary.h
index 861a41c44a..1b24380558 100644
--- a/modules/misc/medialibrary/medialibrary.h
+++ b/modules/misc/medialibrary/medialibrary.h
@@ -165,19 +165,19 @@ private:
     // IMediaLibraryCb interface
 public:
     virtual void onMediaAdded(std::vector<medialibrary::MediaPtr> media) override;
-    virtual void onMediaModified(std::vector<medialibrary::MediaPtr> media) override;
+    virtual void onMediaModified(std::vector<int64_t> media) override;
     virtual void onMediaDeleted(std::vector<int64_t> mediaIds) override;
     virtual void onArtistsAdded(std::vector<medialibrary::ArtistPtr> artists) override;
-    virtual void onArtistsModified(std::vector<medialibrary::ArtistPtr> artists) override;
+    virtual void onArtistsModified(std::vector<int64_t> artists) override;
     virtual void onArtistsDeleted(std::vector<int64_t> artistsIds) override;
     virtual void onAlbumsAdded(std::vector<medialibrary::AlbumPtr> albums) override;
-    virtual void onAlbumsModified(std::vector<medialibrary::AlbumPtr> albums) override;
+    virtual void onAlbumsModified(std::vector<int64_t> albums) override;
     virtual void onAlbumsDeleted(std::vector<int64_t> albumsIds) override;
     virtual void onPlaylistsAdded(std::vector<medialibrary::PlaylistPtr> playlists) override;
-    virtual void onPlaylistsModified(std::vector<medialibrary::PlaylistPtr> playlists) override;
+    virtual void onPlaylistsModified(std::vector<int64_t> playlists) override;
     virtual void onPlaylistsDeleted(std::vector<int64_t> playlistIds) override;
     virtual void onGenresAdded(std::vector<medialibrary::GenrePtr> genres) override;
-    virtual void onGenresModified(std::vector<medialibrary::GenrePtr> genres) override;
+    virtual void onGenresModified(std::vector<int64_t> genres) override;
     virtual void onGenresDeleted(std::vector<int64_t> genreIds) override;
     virtual void onDiscoveryStarted(const std::string& entryPoint) override;
     virtual void onDiscoveryProgress(const std::string& entryPoint) override;
@@ -193,6 +193,7 @@ public:
     virtual void onMediaThumbnailReady(medialibrary::MediaPtr media,
                                        medialibrary::ThumbnailSizeType sizeType,
                                        bool success) override;
+    virtual void onHistoryChanged( medialibrary::HistoryType historyType ) override;
 };
 
 bool Convert( const medialibrary::IMedia* input, vlc_ml_media_t& output );



More information about the vlc-commits mailing list