Commit 84170570 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

IMediaLibrary: Allow a duration to be provided for external media

Fix #275
parent 3fc8b721
Pipeline #40871 failed with stage
in 21 minutes and 26 seconds
......@@ -383,6 +383,7 @@ public:
/**
* @brief addExternalMedia Adds an external media to the list of known media
* @param mrl This media MRL
* @param duration A duration for this media (values <= 0 are ignored)
*
* Once created, this media can be used just like any other media, except
* it won't have a subType, and won't be analyzed to extract tracks and
......@@ -392,7 +393,7 @@ public:
*
* The media can be fetched using media( std::string ) afterward.
*/
virtual MediaPtr addExternalMedia( const std::string& mrl ) = 0;
virtual MediaPtr addExternalMedia( const std::string& mrl, int64_t duration ) = 0;
/**
* @brief addStream Create an external media of type IMedia::Type::Stream
*
......
......@@ -122,12 +122,13 @@ Media::Media( MediaLibraryPtr ml, const std::string& title, Type type,
{
}
Media::Media(MediaLibraryPtr ml, const std::string& fileName, ImportType importType )
Media::Media( MediaLibraryPtr ml, const std::string& fileName,
ImportType importType , int64_t duration )
: m_ml( ml )
, m_id( 0 )
, m_type( Type::Unknown )
, m_subType( SubType::Unknown )
, m_duration( -1 )
, m_duration( duration )
, m_progress( -1.0f )
, m_playCount( 0 )
, m_lastPlayedDate( 0 )
......@@ -168,19 +169,22 @@ std::shared_ptr<Media> Media::create( MediaLibraryPtr ml, Type type,
std::shared_ptr<Media> Media::createExternalMedia( MediaLibraryPtr ml,
const std::string& mrl,
ImportType importType )
ImportType importType,
int64_t duration )
{
std::unique_ptr<sqlite::Transaction> t;
if ( sqlite::Transaction::transactionInProgress() == false )
t = ml->getConn()->newTransaction();
if ( duration <= 0 )
duration = -1;
auto fileName = utils::url::decode( utils::file::fileName( mrl ) );
auto self = std::make_shared<Media>( ml, fileName, importType );
auto self = std::make_shared<Media>( ml, fileName, importType, duration );
static const std::string req = "INSERT INTO " + Media::Table::Name +
"(type, insertion_date, title, filename, import_type) "
"VALUES(?, ?, ?, ?, ?)";
"(type, duration, insertion_date, title, filename, import_type) "
"VALUES(?, ?, ?, ?, ?, ?)";
if ( insert( ml, self, req, Type::Unknown, self->m_insertionDate,
if ( insert( ml, self, req, Type::Unknown, duration, self->m_insertionDate,
self->m_title, self->m_filename, importType ) == false )
return nullptr;
......@@ -193,14 +197,15 @@ std::shared_ptr<Media> Media::createExternalMedia( MediaLibraryPtr ml,
}
std::shared_ptr<Media> Media::createExternal( MediaLibraryPtr ml,
const std::string& fileName )
const std::string& fileName,
int64_t duration )
{
return createExternalMedia( ml, fileName, ImportType::External );
return createExternalMedia( ml, fileName, ImportType::External, duration );
}
std::shared_ptr<Media> Media::createStream( MediaLibraryPtr ml, const std::string& fileName )
{
return createExternalMedia( ml, fileName, ImportType::Stream );
return createExternalMedia( ml, fileName, ImportType::Stream, -1 );
}
AlbumTrackPtr Media::albumTrack() const
......
......@@ -107,14 +107,16 @@ class Media : public IMedia,
Media( MediaLibraryPtr ml, const std::string& title, Type type,
int64_t duration, int64_t deviceId, int64_t folderId );
Media( MediaLibraryPtr ml, const std::string& fileName, ImportType type );
Media( MediaLibraryPtr ml, const std::string& fileName, ImportType type,
int64_t duration );
static std::shared_ptr<Media> create( MediaLibraryPtr ml, Type type,
int64_t deviceId, int64_t folderId,
const std::string& fileName,
int64_t duration );
static std::shared_ptr<Media> createExternal( MediaLibraryPtr ml,
const std::string& fileName );
const std::string& fileName,
int64_t duration );
static std::shared_ptr<Media> createStream( MediaLibraryPtr ml,
const std::string& fileName );
static void createTable( sqlite::Connection* connection );
......@@ -323,7 +325,8 @@ private:
static bool shouldUpdateThumbnail( const Thumbnail& currentThumbnail );
static std::shared_ptr<Media> createExternalMedia( MediaLibraryPtr ml,
const std::string& mrl,
ImportType importType );
ImportType importType,
int64_t duration );
std::vector<std::shared_ptr<Media>> fetchMatchingUngrouped();
/*
......
......@@ -649,9 +649,9 @@ MediaPtr MediaLibrary::media( const std::string& mrl ) const
return file->media();
}
MediaPtr MediaLibrary::addExternalMedia( const std::string& mrl )
MediaPtr MediaLibrary::addExternalMedia( const std::string& mrl, int64_t duration )
{
return Media::createExternal( this, mrl );
return Media::createExternal( this, mrl, duration );
}
MediaPtr MediaLibrary::addStream( const std::string& mrl )
......
......@@ -77,7 +77,7 @@ public:
virtual MediaPtr media( int64_t mediaId ) const override;
virtual MediaPtr media( const std::string& mrl ) const override;
virtual MediaPtr addExternalMedia( const std::string& mrl ) override;
virtual MediaPtr addExternalMedia( const std::string& mrl, int64_t duration ) override;
virtual MediaPtr addStream( const std::string& mrl ) override;
virtual bool removeExternalMedia( MediaPtr media ) override;
virtual Query<IMedia> audioFiles( const QueryParameters* params ) const override;
......
......@@ -332,7 +332,7 @@ void MetadataAnalyzer::addPlaylistElement( IItem& item,
// subitem, ie. the playlist item we're adding.
if ( File::exists( m_ml, mrl ) == false )
{
auto externalMedia = Media::createExternal( m_ml, mrl );
auto externalMedia = Media::createExternal( m_ml, mrl, -1 );
if ( externalMedia == nullptr )
{
LOG_ERROR( "Failed to create external media for ", mrl, " in the playlist ", playlistMrl );
......
......@@ -103,7 +103,7 @@ TEST_F( MiscTests, ExportRestorePlaylist )
auto pl1 = std::static_pointer_cast<Playlist>( m_ml->createPlaylist( "Exported Playlist 1" ) );
auto m1 = media[0];
auto m2 = media[1];
auto m3 = m_ml->addExternalMedia( "http://example.org/sea&otter.avi" );
auto m3 = m_ml->addExternalMedia( "http://example.org/sea&otter.avi", -1 );
pl1->append( *m1 );
pl1->append( *m2 );
pl1->append( *m3 );
......
......@@ -266,7 +266,7 @@ TEST_P( ReducedTests, OverrideExternalMedia )
auto ext = utils::file::extension( f->mrl() );
if ( m_ml->isMediaExtensionSupported( ext.c_str() ) == false )
continue;
auto media = m_ml->addExternalMedia( f->mrl() );
auto media = m_ml->addExternalMedia( f->mrl(), -1 );
ASSERT_NE( nullptr, media );
ASSERT_TRUE( media->isExternalMedia() );
ASSERT_EQ( -1, media->duration() );
......
Subproject commit 5d2ddef4a11d3eacd4a7c60349671b89e68936e7
Subproject commit 17982c4be5ba0f4173af3f57f86c5b6870979218
......@@ -140,7 +140,7 @@ TEST_F( Files, SetMediaId )
TEST_F( Files, ByMrlNetwork )
{
auto m1 = Media::createExternal( ml.get(), "smb://1.2.3.4/path/to/file.mkv" );
auto m1 = Media::createExternal( ml.get(), "smb://1.2.3.4/path/to/file.mkv", -1 );
ASSERT_NE( nullptr, m1 );
auto f1 = File::fromExternalMrl( ml.get(), "smb://1.2.3.4/path/to/file.mkv" );
ASSERT_NE( nullptr, f1 );
......
......@@ -642,7 +642,7 @@ TEST_F( Medias, SortByFilename )
TEST_F( Medias, SetType )
{
auto m1 = std::static_pointer_cast<Media>( ml->addExternalMedia( "media1.mp3" ) );
auto m1 = std::static_pointer_cast<Media>( ml->addExternalMedia( "media1.mp3", -1 ) );
ASSERT_TRUE( m1->isExternalMedia() );
auto res = m1->setType( IMedia::Type::Video );
......@@ -661,7 +661,7 @@ TEST_F( Medias, SetType )
TEST_F( Medias, SetTypeBuffered )
{
auto m1 = std::static_pointer_cast<Media>( ml->addExternalMedia( "media1.mp3" ) );
auto m1 = std::static_pointer_cast<Media>( ml->addExternalMedia( "media1.mp3", -1 ) );
ASSERT_TRUE( m1->isExternalMedia() );
m1->setTypeBuffered( IMedia::Type::Video );
......@@ -826,10 +826,11 @@ TEST_F( Medias, MetadataCheckDbModel )
TEST_F( Medias, ExternalMrl )
{
auto m = ml->addExternalMedia( "https://foo.bar/sea-otters.mkv" );
auto m = ml->addExternalMedia( "https://foo.bar/sea-otters.mkv", 1234 );
ASSERT_NE( nullptr, m );
ASSERT_EQ( m->title(), "sea-otters.mkv" );
ASSERT_EQ( 1234, m->duration() );
ASSERT_TRUE( m->isExternalMedia() );
// External files shouldn't appear in listings
......@@ -864,6 +865,10 @@ TEST_F( Medias, ExternalMrl )
ASSERT_TRUE( files[1]->isExternal() );
ASSERT_FALSE( files[1]->isNetwork() );
ASSERT_EQ( File::Type::Subtitles, files[1]->type() );
auto m3 = ml->addExternalMedia( "https://foo.bar/media.mkv", -1234 );
ASSERT_NE( nullptr, m3 );
ASSERT_EQ( -1, m3->duration() );
}
TEST_F( Medias, AddStream )
......@@ -879,8 +884,8 @@ TEST_F( Medias, AddStream )
TEST_F( Medias, DuplicatedExternalMrl )
{
auto m = ml->addExternalMedia( "http://foo.bar" );
auto m2 = ml->addExternalMedia( "http://foo.bar" );
auto m = ml->addExternalMedia( "http://foo.bar", 1 );
auto m2 = ml->addExternalMedia( "http://foo.bar", 2 );
ASSERT_NE( nullptr, m );
ASSERT_EQ( nullptr, m2 );
}
......@@ -950,7 +955,7 @@ TEST_F( Medias, CreateStream )
TEST_F( Medias, SearchExternal )
{
auto m1 = std::static_pointer_cast<Media>( ml->addExternalMedia( "localfile.mkv" ) );
auto m1 = std::static_pointer_cast<Media>( ml->addExternalMedia( "localfile.mkv", -1 ) );
m1->setTitle( "local otter", false );
auto m2 = std::static_pointer_cast<Media>( ml->addStream( "http://remote.file/media.asf" ) );
m2->setTitle( "remote otter", false );
......@@ -969,8 +974,8 @@ TEST_F( Medias, SearchExternal )
TEST_F( Medias, VacuumOldExternal )
{
auto m1 = ml->addExternalMedia( "foo.avi" );
auto m2 = ml->addExternalMedia( "bar.mp3" );
auto m1 = ml->addExternalMedia( "foo.avi", -1 );
auto m2 = ml->addExternalMedia( "bar.mp3", -1 );
auto s1 = ml->addStream( "http://baz.mkv" );
ASSERT_NE( nullptr, m1 );
......@@ -1005,8 +1010,8 @@ TEST_F( Medias, VacuumOldExternal )
TEST_F( Medias, VacuumNeverPlayedMedia )
{
auto m1 = ml->addExternalMedia( "foo.avi" );
auto m2 = ml->addExternalMedia( "bar.mp3" );
auto m1 = ml->addExternalMedia( "foo.avi", -1 );
auto m2 = ml->addExternalMedia( "bar.mp3", -1 );
auto s1 = ml->addStream( "http://baz.mkv" );
ASSERT_NE( nullptr, m1 );
......@@ -1028,7 +1033,7 @@ TEST_F( Medias, VacuumNeverPlayedMedia )
TEST_F( Medias, RemoveExternal )
{
auto m = ml->addExternalMedia( "http://extern.al/media.mkv" );
auto m = ml->addExternalMedia( "http://extern.al/media.mkv", -1 );
ASSERT_NE( nullptr, m );
auto res = ml->removeExternalMedia( m );
......@@ -1042,7 +1047,7 @@ TEST_F( Medias, RemoveExternal )
TEST_F( Medias, NbPlaylists )
{
auto m = std::static_pointer_cast<Media>( ml->addExternalMedia( "media.mkv" ) );
auto m = std::static_pointer_cast<Media>( ml->addExternalMedia( "media.mkv", -1 ) );
ASSERT_EQ( 0u, m->nbPlaylists() );
auto playlist = ml->createPlaylist( "playlisẗ" );
......
......@@ -452,7 +452,7 @@ TEST_F( DbModel, Upgrade4to5 )
// The culprit with V4 was an invalid migration, leading to missing fields
// in File and most likely Playlist tables. Simply try to create/fetch a file
auto m = ml->addExternalMedia( "test.mkv" );
auto m = ml->addExternalMedia( "test.mkv", -1 );
ASSERT_NE( m, nullptr );
auto files = ml->files();
ASSERT_NE( files.size(), 0u );
......
......@@ -451,8 +451,8 @@ TEST_F( Playlists, SearchMedia )
TEST_F( Playlists, ReinsertMedia )
{
auto m1 = ml->addExternalMedia( "http://sea.otters/fluffy.mkv" );
auto m2 = ml->addExternalMedia( "https:///cuteotters.org/holding_hands.mp4" );
auto m1 = ml->addExternalMedia( "http://sea.otters/fluffy.mkv", -1 );
auto m2 = ml->addExternalMedia( "https:///cuteotters.org/holding_hands.mp4", -1 );
auto m3 = ml->addMedia( "media.mp3", IMedia::Type::Audio );
pl->append( *m1 );
pl->append( *m2 );
......@@ -475,8 +475,8 @@ TEST_F( Playlists, ReinsertMedia )
pl = std::static_pointer_cast<Playlist>( ml->playlist( pl->id() ) );
m1 = ml->addExternalMedia( "http://sea.otters/fluffy.mkv" );
m2 = ml->addExternalMedia( "https:///cuteotters.org/holding_hands.mp4" );
m1 = ml->addExternalMedia( "http://sea.otters/fluffy.mkv", -1 );
m2 = ml->addExternalMedia( "https:///cuteotters.org/holding_hands.mp4", -1 );
media = pl->media()->all();
ASSERT_EQ( 3u, media.size() );
......@@ -490,7 +490,7 @@ TEST_F( Playlists, ReinsertMedia )
TEST_F( Playlists, RemoveMedia )
{
auto m1 = ml->addExternalMedia( "http://sea.otters/fluffy.mkv" );
auto m1 = ml->addExternalMedia( "http://sea.otters/fluffy.mkv", -1 );
auto m2 = ml->addMedia( "file:///cute_otters_holding_hands.mp4", IMedia::Type::Video );
auto m3 = ml->addMedia( "media.mp3", IMedia::Type::Audio );
pl->append( *m1 );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment