fix(mpd): Always update mpd data

Only updating when an mpd event occurred would cause issues when mpd was
playing and the machine was put to sleep because the elapsed time was
calculated by taking the time difference of the last update and now
which would give you wrong numbers, if the machine was in standby in
between.

Since the update function on the module is only called once a second (or
when an event happens), we can just update the data every time without a
huge performance hit.

Fixes #915
This commit is contained in:
patrick96 2017-12-27 16:34:24 +01:00 committed by Patrick Ziegler
parent bb0f68aef0
commit 645a3142a1
3 changed files with 10 additions and 13 deletions

View File

@ -140,7 +140,6 @@ namespace mpd {
void fetch_data(mpdconnection* conn); void fetch_data(mpdconnection* conn);
void update(int event, mpdconnection* connection); void update(int event, mpdconnection* connection);
void update_timer();
bool random() const; bool random() const;
bool repeat() const; bool repeat() const;

View File

@ -370,6 +370,10 @@ namespace mpd {
} }
void mpdstatus::update(int event, mpdconnection* connection) { void mpdstatus::update(int event, mpdconnection* connection) {
/*
* Only update if either the player state (play, stop, pause, seek, ...), the options (random, repeat, ...),
* or the playlist has been changed
*/
if (connection == nullptr || !static_cast<bool>(event & (MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST))) { if (connection == nullptr || !static_cast<bool>(event & (MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST))) {
return; return;
} }
@ -395,14 +399,6 @@ namespace mpd {
} }
} }
void mpdstatus::update_timer() {
auto diff = chrono::system_clock::now() - m_updated_at;
auto dur = chrono::duration_cast<chrono::milliseconds>(diff);
m_elapsed_time_ms += dur.count();
m_elapsed_time = m_elapsed_time_ms / 1000 + 0.5f;
m_updated_at = chrono::system_clock::now();
}
bool mpdstatus::random() const { bool mpdstatus::random() const {
return m_random; return m_random;
} }

View File

@ -162,13 +162,10 @@ namespace modules {
int idle_flags = 0; int idle_flags = 0;
if ((idle_flags = m_mpd->noidle()) != 0) { if ((idle_flags = m_mpd->noidle()) != 0) {
// Update status on every event
m_status->update(idle_flags, m_mpd.get()); m_status->update(idle_flags, m_mpd.get());
return true; return true;
} }
if (m_status->match_state(mpdstate::PLAYING)) {
m_status->update_timer();
}
} catch (const mpd_exception& err) { } catch (const mpd_exception& err) {
m_log.err("%s: %s", name(), err.what()); m_log.err("%s: %s", name(), err.what());
m_mpd.reset(); m_mpd.reset();
@ -203,6 +200,11 @@ namespace modules {
} }
} }
if (m_status->match_state(mpdstate::PLAYING)) {
// Always update the status while playing
m_status->update(-1, m_mpd.get());
}
string artist; string artist;
string album; string album;
string title; string title;