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

parser: Move Status enum to a new public header

parent 1617a814
......@@ -38,6 +38,7 @@ libmedialibrary_la_HEADERS = \
include/medialibrary/IDeviceLister.h \
include/medialibrary/IFolder.h \
include/medialibrary/IQuery.h \
include/medialibrary/Parser.h \
$(NULL)
libmedialibrary_la_SOURCES = \
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015-2018 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
*
* Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*****************************************************************************/
#pragma once
namespace medialibrary
{
namespace parser
{
enum class Status
{
/// Default value.
/// Also, having success = 0 is not the best idea ever.
Unknown,
/// All good
Success,
/// We can't compute this file for now (for instance the file was on a network drive which
/// isn't connected anymore)
TemporaryUnavailable,
/// Something failed and we won't continue
Fatal,
/// The task must now be considered completed, regardless of the
/// current step.
Completed,
/// The task should be discarded, regardless of its status
/// This is likely to be used when trying to parse playlist items,
/// as they already could have been queued before.
Discarded,
};
}
}
......@@ -88,7 +88,7 @@ int MetadataParser::toInt( parser::IItem& item, parser::IItem::Metadata meta )
return 0;
}
parser::Task::Status MetadataParser::run( parser::IItem& item )
parser::Status MetadataParser::run( parser::IItem& item )
{
bool alreadyInParser = false;
int nbSubitem = item.nbSubItems();
......@@ -97,10 +97,10 @@ parser::Task::Status MetadataParser::run( parser::IItem& item )
{
auto res = addPlaylistMedias( item );
if ( res == false ) // playlist addition may fail due to constraint violation
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
assert( item.file() != nullptr );
return parser::Task::Status::Completed;
return parser::Status::Completed;
}
if ( item.file() == nullptr )
......@@ -116,7 +116,7 @@ parser::Task::Status MetadataParser::run( parser::IItem& item )
if ( m == nullptr )
{
LOG_ERROR( "Failed to add media ", mrl, " to the media library" );
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
}
// For now, assume all media are made of a single file
auto file = m->addFile( *item.fileFs(),
......@@ -126,7 +126,7 @@ parser::Task::Status MetadataParser::run( parser::IItem& item )
if ( file == nullptr )
{
LOG_ERROR( "Failed to add file ", mrl, " to media #", m->id() );
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
}
item.setMedia( std::move( m ) );
// Will invoke ITaskCb::updateFileId to upadte m_fileId & its
......@@ -144,11 +144,11 @@ parser::Task::Status MetadataParser::run( parser::IItem& item )
if ( fileInDB == nullptr ) // The file is no longer present in DB, gracefully delete task
{
LOG_ERROR( "File ", mrl, " no longer present in DB, aborting");
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
}
auto media = fileInDB->media();
if ( media == nullptr ) // Without a media, we cannot go further
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
item.setFile( std::move( fileInDB ) );
item.setMedia( std::move( media ) );
......@@ -162,19 +162,19 @@ parser::Task::Status MetadataParser::run( parser::IItem& item )
// Keep in mind that if we are in this code path, we are not analyzing
// a playlist.
assert( false );
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
}
if ( item.parentPlaylist() != nullptr )
item.parentPlaylist()->add( item.media()->id(), item.parentPlaylistIndex() );
if ( alreadyInParser == true )
return parser::Task::Status::Discarded;
return parser::Status::Discarded;
const auto& tracks = item.tracks();
if ( tracks.empty() == true )
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
bool isAudio = true;
{
......@@ -206,19 +206,19 @@ parser::Task::Status MetadataParser::run( parser::IItem& item )
if ( isAudio == true )
{
if ( parseAudioFile( item ) == false )
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
}
else
{
if (parseVideoFile( item ) == false )
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
}
if ( item.file()->isDeleted() == true || item.media()->isDeleted() == true )
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
m_notifier->notifyMediaCreation( item.media() );
return parser::Task::Status::Success;
return parser::Status::Success;
}
/* Playlist files */
......
......@@ -38,7 +38,7 @@ public:
protected:
bool cacheUnknownArtist();
virtual bool initialize( MediaLibrary* ml ) override;
virtual parser::Task::Status run( parser::IItem& item ) override;
virtual parser::Status run( parser::IItem& item ) override;
virtual const char* name() const override;
virtual uint8_t nbThreads() const override;
virtual void onFlushing() override;
......
......@@ -45,7 +45,7 @@ bool VLCMetadataService::initialize( MediaLibrary* )
return true;
}
parser::Task::Status VLCMetadataService::run( parser::IItem& item )
parser::Status VLCMetadataService::run( parser::IItem& item )
{
auto mrl = item.mrl();
LOG_INFO( "Parsing ", mrl );
......@@ -68,14 +68,14 @@ parser::Task::Status VLCMetadataService::run( parser::IItem& item )
if ( vlcMedia.parseWithOptions( VLC::Media::ParseFlags::Local | VLC::Media::ParseFlags::Network |
VLC::Media::ParseFlags::FetchLocal, 5000 ) == false )
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
m_cond.wait( lock, [&status, &done]() {
return done == true;
});
}
event->unregister();
if ( status == VLC::Media::ParsedStatus::Failed || status == VLC::Media::ParsedStatus::Timeout )
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
auto tracks = vlcMedia.tracks();
auto artworkMrl = vlcMedia.meta( libvlc_meta_ArtworkURL );
if ( ( tracks.size() == 0 && vlcMedia.subitems()->count() == 0 ) ||
......@@ -86,10 +86,10 @@ parser::Task::Status VLCMetadataService::run( parser::IItem& item )
VLC::MediaPlayer mp( vlcMedia );
auto res = MetadataCommon::startPlayback( vlcMedia, mp );
if ( res == false )
return parser::Task::Status::Fatal;
return parser::Status::Fatal;
}
mediaToItem( vlcMedia, item );
return parser::Task::Status::Success;
return parser::Status::Success;
}
const char* VLCMetadataService::name() const
......
......@@ -43,7 +43,7 @@ class VLCMetadataService : public IParserService
private:
virtual bool initialize( MediaLibrary* ml ) override;
virtual parser::Task::Status run( parser::IItem& item ) override;
virtual parser::Status run( parser::IItem& item ) override;
virtual const char* name() const override;
virtual uint8_t nbThreads() const override;
virtual void onFlushing() override;
......
......@@ -23,6 +23,7 @@
#pragma once
#include "Task.h"
#include "medialibrary/Parser.h"
namespace medialibrary
{
......@@ -37,7 +38,7 @@ public:
* @param task The task to be processed
* @return A status code
*/
virtual parser::Task::Status run( parser::IItem& item ) = 0;
virtual parser::Status run( parser::IItem& item ) = 0;
/**
* @brief name returns the name of this service.
*
......
......@@ -144,15 +144,15 @@ void Parser::updateStats()
}
}
void Parser::done( std::shared_ptr<parser::Task> t, parser::Task::Status status )
void Parser::done( std::shared_ptr<parser::Task> t, parser::Status status )
{
++m_opDone;
auto serviceIdx = ++t->currentService;
if ( status == parser::Task::Status::TemporaryUnavailable ||
status == parser::Task::Status::Fatal ||
status == parser::Task::Status::Discarded ||
if ( status == parser::Status::TemporaryUnavailable ||
status == parser::Status::Fatal ||
status == parser::Status::Discarded ||
t->isCompleted() )
{
if ( serviceIdx < m_services.size() )
......
......@@ -28,6 +28,8 @@
#include "Task.h"
#include "File.h"
#include "medialibrary/Parser.h"
namespace medialibrary
{
......@@ -39,7 +41,7 @@ class IParserCb
{
public:
virtual ~IParserCb() = default;
virtual void done( std::shared_ptr<parser::Task> task, parser::Task::Status status ) = 0;
virtual void done( std::shared_ptr<parser::Task> task, parser::Status status ) = 0;
virtual void onIdleChanged( bool isIdle ) = 0;
};
......@@ -70,7 +72,7 @@ public:
private:
void updateStats();
virtual void done( std::shared_ptr<parser::Task> task,
parser::Task::Status status ) override;
parser::Status status ) override;
virtual void onIdleChanged( bool idle ) override;
private:
......
......@@ -166,17 +166,17 @@ void ParserWorker::mainloop()
if ( task->isStepCompleted( m_service->targetedStep() ) == true )
{
LOG_INFO( "Skipping completed task [", serviceName, "] on ", task->item().mrl() );
m_parserCb->done( std::move( task ), parser::Task::Status::Success );
m_parserCb->done( std::move( task ), parser::Status::Success );
continue;
}
parser::Task::Status status;
parser::Status status;
try
{
LOG_INFO( "Executing ", serviceName, " task on ", task->item().mrl() );
auto chrono = std::chrono::steady_clock::now();
if ( ( task->item().file() != nullptr && task->item().file()->isDeleted() )
|| ( task->item().media() != nullptr && task->item().media()->isDeleted() ) )
status = parser::Task::Status::Fatal;
status = parser::Status::Fatal;
else
{
task->startParserStep();
......@@ -189,10 +189,10 @@ void ParserWorker::mainloop()
catch ( const std::exception& ex )
{
LOG_ERROR( "Caught an exception during ", task->item().mrl(), " [", serviceName, "] parsing: ", ex.what() );
status = parser::Task::Status::Fatal;
status = parser::Status::Fatal;
}
if ( handleServiceResult( *task, status ) == false )
status = parser::Task::Status::Fatal;
status = parser::Status::Fatal;
m_parserCb->done( std::move( task ), status );
}
LOG_INFO("Exiting ParserService [", serviceName, "] thread");
......@@ -207,9 +207,9 @@ void ParserWorker::setIdle(bool isIdle)
m_parserCb->onIdleChanged( isIdle );
}
bool ParserWorker::handleServiceResult( parser::Task& task, parser::Task::Status status )
bool ParserWorker::handleServiceResult( parser::Task& task, parser::Status status )
{
if ( status == parser::Task::Status::Success )
if ( status == parser::Status::Success )
{
task.markStepCompleted( m_service->targetedStep() );
// We don't want to save the extraction step in database, as restarting a
......@@ -219,12 +219,12 @@ bool ParserWorker::handleServiceResult( parser::Task& task, parser::Task::Status
return task.saveParserStep();
return true;
}
else if ( status == parser::Task::Status::Completed )
else if ( status == parser::Status::Completed )
{
task.markStepCompleted( parser::Task::ParserStep::Completed );
return task.saveParserStep();
}
else if ( status == parser::Task::Status::Discarded )
else if ( status == parser::Status::Discarded )
{
return parser::Task::destroy( m_ml, task.id() );
}
......
......@@ -80,7 +80,7 @@ private:
void start();
void mainloop();
void setIdle( bool isIdle );
bool handleServiceResult( parser::Task& task, parser::Task::Status status );
bool handleServiceResult( parser::Task& task, parser::Status status );
private:
MediaLibrary* m_ml;
......
......@@ -74,27 +74,6 @@ public:
class Task : public DatabaseHelpers<Task, policy::TaskTable, cachepolicy::Uncached<Task>>, private ITaskCb
{
public:
enum class Status
{
/// Default value.
/// Also, having success = 0 is not the best idea ever.
Unknown,
/// All good
Success,
/// We can't compute this file for now (for instance the file was on a network drive which
/// isn't connected anymore)
TemporaryUnavailable,
/// Something failed and we won't continue
Fatal,
/// The task must now be considered completed, regardless of the
/// current step.
Completed,
/// The task should be discarded, regardless of its status
/// This is likely to be used when trying to parse playlist items,
/// as they already could have been queued before.
Discarded,
};
enum class ParserStep : uint8_t
{
None = 0,
......
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