[vlc-commits] medialibrary: Add an input_item_t from media getter
Hugo Beauzée-Luyssen
git at videolan.org
Wed Jan 23 10:01:36 CET 2019
vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Mon Nov 19 16:12:49 2018 +0100| [c6fbe73545242d65a577df2b21a9e94169b40fdb] | committer: Hugo Beauzée-Luyssen
medialibrary: Add an input_item_t from media getter
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=c6fbe73545242d65a577df2b21a9e94169b40fdb
---
include/vlc_media_library.h | 6 ++
modules/misc/medialibrary/entities.cpp | 94 ++++++++++++++++++++++++++++----
modules/misc/medialibrary/medialib.cpp | 5 ++
modules/misc/medialibrary/medialibrary.h | 1 +
4 files changed, 95 insertions(+), 11 deletions(-)
diff --git a/include/vlc_media_library.h b/include/vlc_media_library.h
index d9ee715825..a0b5d921f8 100644
--- a/include/vlc_media_library.h
+++ b/include/vlc_media_library.h
@@ -324,6 +324,7 @@ struct vlc_ml_query_params_t
enum vlc_ml_get_queries
{
VLC_ML_GET_MEDIA, /**< arg1: Media ID; ret: vlc_ml_media_t* */
+ VLC_ML_GET_INPUT_ITEM, /**< arg1: Media ID; ret: input_item_t* */
VLC_ML_GET_ALBUM, /**< arg1: Album ID; ret: vlc_ml_album_t* */
VLC_ML_GET_ARTIST, /**< arg1: Artist ID; ret: vlc_ml_artist_t* */
VLC_ML_GET_GENRE, /**< arg1: Genre ID; ret: vlc_ml_genre_t* */
@@ -856,6 +857,11 @@ static inline vlc_ml_media_t* vlc_ml_get_media( vlc_medialibrary_t* p_ml, int64_
return (vlc_ml_media_t*)vlc_ml_get( p_ml, VLC_ML_GET_MEDIA, i_media_id );
}
+static inline input_item_t* vlc_ml_get_input_item( vlc_medialibrary_t* p_ml, int64_t i_media_id )
+{
+ return (input_item_t*)vlc_ml_get( p_ml, VLC_ML_GET_INPUT_ITEM, i_media_id );
+}
+
static inline vlc_ml_album_t* vlc_ml_get_album( vlc_medialibrary_t* p_ml, int64_t i_album_id )
{
return (vlc_ml_album_t*)vlc_ml_get( p_ml, VLC_ML_GET_ALBUM, i_album_id );
diff --git a/modules/misc/medialibrary/entities.cpp b/modules/misc/medialibrary/entities.cpp
index f6476c76c7..2272ad60bc 100644
--- a/modules/misc/medialibrary/entities.cpp
+++ b/modules/misc/medialibrary/entities.cpp
@@ -39,6 +39,8 @@
#include <medialibrary/IVideoTrack.h>
#include <medialibrary/IFolder.h>
+#include <algorithm>
+
static auto const strdup_helper = []( std::string const& src, char*& dst )
{
dst = nullptr;
@@ -299,23 +301,25 @@ bool Convert( const medialibrary::IAlbum* input, vlc_ml_album_t& output )
return true;
}
-bool Convert( const medialibrary::IArtist* input, vlc_ml_artist_t& output )
+static const char* artistName( const medialibrary::IArtist* artist )
{
- output.i_id = input->id();
- output.i_nb_album = input->nbAlbums();
- output.i_nb_tracks = input->nbTracks();
- switch ( input->id() )
+ switch ( artist->id() )
{
case medialibrary::UnknownArtistID:
- output.psz_name = strdup( _( "Unknown Artist" ) );
- break;
+ return _( "Unknown Artist" );
case medialibrary::VariousArtistID:
- output.psz_name = strdup( _( "Various Artist" ) );
- break;
+ return _( "Various Artist" );
default:
- output.psz_name = strdup( input->name().c_str() );
- break;
+ return artist->name().c_str();
}
+}
+
+bool Convert( const medialibrary::IArtist* input, vlc_ml_artist_t& output )
+{
+ output.i_id = input->id();
+ output.i_nb_album = input->nbAlbums();
+ output.i_nb_tracks = input->nbTracks();
+ output.psz_name = strdup( artistName( input ) );
if ( unlikely( output.psz_name == nullptr ) )
return false;
@@ -381,3 +385,71 @@ bool Convert( const medialibrary::IFolder* input, vlc_ml_entry_point_t& output )
output.b_banned = input->isBanned();
return true;
}
+
+input_item_t* MediaToInputItem( const medialibrary::IMedia* media )
+{
+ auto files = media->files();
+ const auto it = std::find_if( cbegin( files ), cend( files ),
+ [](const medialibrary::FilePtr& f) {
+ return f->type() == medialibrary::IFile::Type::Main;
+ });
+ assert( it != cend( files ) );
+ auto inputItem = vlc::wrap_cptr<input_item_t>(
+ input_item_NewExt( (*it)->mrl().c_str(), media->fileName().c_str(),
+ VLC_TICK_FROM_MS( media->duration() ),
+ ITEM_TYPE_FILE, ITEM_NET_UNKNOWN ),
+ &input_item_Release );
+ if ( media->isThumbnailGenerated() == true )
+ {
+ auto thumbnail = media->thumbnail();
+ if ( thumbnail.length() > 0 )
+ input_item_SetArtworkURL( inputItem.get(), thumbnail.c_str() );
+ }
+ switch ( media->type() )
+ {
+ case medialibrary::IMedia::Type::External:
+ case medialibrary::IMedia::Type::Stream:
+ case medialibrary::IMedia::Type::Unknown:
+ // Those types are not analyzed
+ break;
+ case medialibrary::IMedia::Type::Video:
+ break;
+ case medialibrary::IMedia::Type::Audio:
+ {
+ if ( media->subType() != medialibrary::IMedia::SubType::AlbumTrack )
+ break;
+ auto track = media->albumTrack();
+ if ( track == nullptr )
+ return nullptr;
+ auto album = track->album();
+ if ( album == nullptr )
+ return nullptr;
+ auto artist = track->artist();
+ if ( artist == nullptr )
+ return nullptr;
+ // From the track itself:
+ input_item_SetTitle( inputItem.get(), media->title().c_str() );
+ input_item_SetDiscNumber( inputItem.get(),
+ std::to_string( track->discNumber() ).c_str() );
+ input_item_SetTrackNumber( inputItem.get(),
+ std::to_string( track->trackNumber() ).c_str() );
+
+ // From the album:
+ input_item_SetTrackTotal( inputItem.get(),
+ std::to_string( album->nbTracks() ).c_str() );
+ auto albumTitle = album->title();
+ if ( albumTitle.empty() == true )
+ input_item_SetAlbum( inputItem.get(), _( "Unknown album" ) );
+ else
+ input_item_SetAlbum( inputItem.get(), albumTitle.c_str() );
+
+ // From the artist/albumArtist
+ input_item_SetArtist( inputItem.get(), artistName( artist.get() ) );
+ auto albumArtist = album->albumArtist();
+ if ( albumArtist != nullptr )
+ input_item_SetArtist( inputItem.get(), artistName( albumArtist.get() ) );
+ }
+ }
+
+ return inputItem.release();
+}
diff --git a/modules/misc/medialibrary/medialib.cpp b/modules/misc/medialibrary/medialib.cpp
index c5245c81b5..a6a8fe7099 100644
--- a/modules/misc/medialibrary/medialib.cpp
+++ b/modules/misc/medialibrary/medialib.cpp
@@ -760,6 +760,11 @@ void* MediaLibrary::Get( int query, int64_t id )
auto media = m_ml->media( id );
return CreateAndConvert<vlc_ml_media_t>( media.get() );
}
+ case VLC_ML_GET_INPUT_ITEM:
+ {
+ auto media = m_ml->media( id );
+ return MediaToInputItem( media.get() );
+ }
case VLC_ML_GET_ALBUM:
{
auto album = m_ml->album( id );
diff --git a/modules/misc/medialibrary/medialibrary.h b/modules/misc/medialibrary/medialibrary.h
index 47aac1f1f6..759fd38b1a 100644
--- a/modules/misc/medialibrary/medialibrary.h
+++ b/modules/misc/medialibrary/medialibrary.h
@@ -179,6 +179,7 @@ bool Convert( const medialibrary::IShow* input, vlc_ml_show_t& output );
bool Convert( const medialibrary::ILabel* input, vlc_ml_label_t& output );
bool Convert( const medialibrary::IPlaylist* input, vlc_ml_playlist_t& output );
bool Convert( const medialibrary::IFolder* input, vlc_ml_entry_point_t& output );
+input_item_t* MediaToInputItem( const medialibrary::IMedia* media );
template <typename To, typename ItemType, typename From>
To* ml_convert_list( const std::vector<std::shared_ptr<From>>& input )
More information about the vlc-commits
mailing list