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

MediaLibrary: Provide a way to pause background operations

So that we can save all resources for playback
parent adf8bf09
......@@ -123,7 +123,17 @@ class IMediaLibrary
virtual void discover( const std::string& entryPoint ) = 0;
virtual const std::string& snapshotPath() const = 0;
virtual void setLogger( ILogger* logger ) = 0;
/**
* @brief pauseBackgroundOperations Will stop potentially CPU intensive background
* operations, until resumeBackgroundOperations() is called.
* If an operation is currently running, it will finish before pausing.
*/
virtual void pauseBackgroundOperations() = 0;
/**
* @brief resumeBackgroundOperations Resumes background tasks, previously
* interrupted by pauseBackgroundOperations().
*/
virtual void resumeBackgroundOperations() = 0;
virtual void reload() = 0;
};
......
......@@ -361,6 +361,16 @@ void MediaLibrary::reload()
m_discoverer->reload();
}
void MediaLibrary::pauseBackgroundOperations()
{
m_parser->pause();
}
void MediaLibrary::resumeBackgroundOperations()
{
m_parser->resume();
}
void MediaLibrary::discover( const std::string &entryPoint )
{
m_discoverer->discover( entryPoint );
......
......@@ -84,6 +84,9 @@ class MediaLibrary : public IMediaLibrary
//Temporarily public, move back to private as soon as we start monitoring the FS
virtual void reload() override;
virtual void pauseBackgroundOperations() override;
virtual void resumeBackgroundOperations() override;
private:
static const std::vector<std::string> supportedVideoExtensions;
static const std::vector<std::string> supportedAudioExtensions;
......
......@@ -32,6 +32,7 @@ Parser::Parser(DBConnection dbConnection , IMediaLibraryCb* cb)
, m_callback( cb )
, m_nbParsed( 0 )
, m_nbToParse( 0 )
, m_paused( false )
{
}
......@@ -72,7 +73,8 @@ void Parser::parse( std::shared_ptr<Media> file )
return;
m_tasks.push( new Task( file, m_services, m_callback ) );
updateStats( false, true );
m_cond.notify_all();
if ( m_paused == false )
m_cond.notify_all();
}
void Parser::start()
......@@ -83,6 +85,19 @@ void Parser::start()
m_thread = std::thread{ &Parser::run, this };
}
void Parser::pause()
{
std::lock_guard<std::mutex> lock( m_lock );
m_paused = true;
}
void Parser::resume()
{
std::lock_guard<std::mutex> lock( m_lock );
m_paused = false;
m_cond.notify_all();
}
void Parser::run()
{
LOG_INFO("Starting Parser thread");
......@@ -93,9 +108,12 @@ void Parser::run()
Task* task = nullptr;
{
std::unique_lock<std::mutex> lock( m_lock );
if ( m_tasks.empty() == true )
if ( m_tasks.empty() == true || m_paused == true )
{
m_cond.wait( lock, [this]() { return m_tasks.empty() == false || m_stopParser == true; });
m_cond.wait( lock, [this]() {
return ( m_tasks.empty() == false && m_paused == false )
|| m_stopParser == true;
});
// We might have been woken up because the parser is being destroyed
if ( m_stopParser == true )
break;
......
......@@ -41,6 +41,8 @@ class Parser : public IMetadataServiceCb
void addService(std::unique_ptr<IMetadataService> service );
void parse( std::shared_ptr<Media> file );
void start();
void pause();
void resume();
private:
virtual void done( std::shared_ptr<Media> file, IMetadataService::Status status, void* data ) override;
......@@ -72,6 +74,7 @@ class Parser : public IMetadataServiceCb
IMediaLibraryCb* m_callback;
std::atomic_uint m_nbParsed;
std::atomic_uint m_nbToParse;
bool m_paused;
};
#endif // PARSER_H
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