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

Provide a parsing progress callback

parent 47fd2a41
......@@ -52,6 +52,16 @@ public:
virtual void onDiscoveryCompleted( const std::string& entryPoint ) = 0;
virtual void onReloadStarted() = 0;
virtual void onReloadCompleted() = 0;
/**
* @brief onParsingStatsUpdated Called when the parser statistics are updated
*
* There is no waranty about how often this will be called. However: when
* nbParsed == nbToParse, the callback will be invoked, and nbToParse will be reset to 0
*
* @param nbParsed The number of media that have been parsed
* @param nbToParse The total number of media to parse.
*/
virtual void onParsingStatsUpdated( uint32_t nbParsed, uint32_t nbToParse ) = 0;
};
class IMediaLibrary
......
......@@ -30,6 +30,8 @@ Parser::Parser(DBConnection dbConnection , IMediaLibraryCb* cb)
: m_stopParser( false )
, m_dbConnection( dbConnection )
, m_callback( cb )
, m_nbParsed( 0 )
, m_nbToParse( 0 )
{
}
......@@ -69,6 +71,7 @@ void Parser::parse( std::shared_ptr<Media> file )
if ( m_services.size() == 0 )
return;
m_tasks.push( new Task( file, m_services, m_callback ) );
updateStats( false, true );
m_cond.notify_all();
}
......@@ -120,6 +123,34 @@ void Parser::restore()
}
}
void Parser::updateStats( bool newMediaParsed, bool newMediaQueued )
{
if ( m_callback == nullptr )
return;
if ( newMediaParsed == true )
m_nbParsed++;
else if ( newMediaQueued == true )
m_nbToParse++;
else
assert(false);
if ( m_nbParsed == m_nbToParse )
{
m_callback->onParsingStatsUpdated( m_nbParsed + 1, m_nbToParse );
m_nbParsed = 0;
m_nbToParse = 0;
return;
}
// Only send an update every X new elem
const uint32_t NbElems = 10;
if ( ( newMediaParsed == true && m_nbParsed % NbElems == 0 ) ||
( newMediaQueued == true && m_nbToParse % NbElems == 0 ) )
{
m_callback->onParsingStatsUpdated( m_nbParsed, m_nbToParse );
}
}
Parser::Task::Task(std::shared_ptr<Media> file, Parser::ServiceList& serviceList, IMediaLibraryCb* metadataCb )
: file(file)
......@@ -136,6 +167,7 @@ void Parser::done(std::shared_ptr<Media> file, IMetadataService::Status status,
if ( status == IMetadataService::Status::TemporaryUnavailable ||
status == IMetadataService::Status::Fatal )
{
updateStats( true, false );
delete t;
return;
}
......@@ -147,6 +179,7 @@ void Parser::done(std::shared_ptr<Media> file, IMetadataService::Status status,
++t->it;
if (t->it == t->end)
{
updateStats( true, false );
file->markParsed();
delete t;
return;
......
......@@ -47,6 +47,7 @@ class Parser : public IMetadataServiceCb
void run();
// Queues all unparsed files for parsing.
void restore();
void updateStats(bool newMediaParsed, bool newMediaQueue);
private:
typedef std::unique_ptr<IMetadataService> ServicePtr;
......@@ -69,6 +70,8 @@ class Parser : public IMetadataServiceCb
std::atomic_bool m_stopParser;
DBConnection m_dbConnection;
IMediaLibraryCb* m_callback;
std::atomic_uint m_nbParsed;
std::atomic_uint m_nbToParse;
};
#endif // PARSER_H
......@@ -52,7 +52,8 @@ class ServiceCb : public IMediaLibraryCb
virtual void onDiscoveryStarted( const std::string& ) override {}
virtual void onDiscoveryCompleted( const std::string& ) override {}
virtual void onReloadStarted() override {}
virtual void onReloadCompleted() override {}
virtual void onReloadCompleted() override {}
virtual void onParsingStatsUpdated( uint32_t, uint32_t ) {}
};
class VLCMetadataServices : public Tests
......
......@@ -61,6 +61,8 @@ public:
m_reloadCond.notify_all();
}
virtual void onParsingStatsUpdated( uint32_t, uint32_t ) {}
bool wait()
{
std::unique_lock<std::mutex> lock( m_mutex );
......
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