From 0803dd6c01fa7a0261f2e798108a3bc37e08a9ef Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Wed, 1 Apr 2026 19:32:30 -0400 Subject: [PATCH] TEMP: Test missing events When calling mpd_run_play_id() to restart a broken stream, it is possible that MPD executes the command and changes its state before we had a chance to call mpd_run_idle_mask()... Signed-off-by: Hugo Villeneuve --- src/main.c | 84 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/src/main.c b/src/main.c index 7eb99df..08dc18d 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ extern struct options_t options; +static enum mpd_state last_state; static struct mpd_connection *mpdc; static bool is_stream; static int song_id; @@ -144,10 +145,52 @@ int mpd_check_abnormal_stop(struct mpd_status *status) return 0; } +int mpdstream_monitor_status(void) +{ + struct mpd_status *status; + enum mpd_state state; + int abnormal_stop; + + status = mpd_run_status(mpdc); + if (status == NULL) + return 0; + + state = mpd_status_get_state(status); + if (state == last_state) + goto end; + + switch (state) { + case MPD_STATE_PLAY: + log_info("Playback started\n"); + is_stream = is_stream_uri(status); + break; + case MPD_STATE_PAUSE: + log_info("Playback paused\n"); /* No action */ + break; + case MPD_STATE_STOP: + log_info("Playback stopped\n"); + abnormal_stop = mpd_check_abnormal_stop(status); + if (abnormal_stop) { + log_warn("Auto restart stream after error...\n"); + (void) mpd_replay(song_id); + } + break; + default: + log_warn("Playback unknown state\n"); + break; + } + + last_state = state; + +end: + mpd_status_free(status); + + return 0; +} + int main(int argc, char **argv) { - enum mpd_state last_state; int rc; openlog(PACKAGE_NAME, 0, LOG_USER); @@ -172,10 +215,9 @@ main(int argc, char **argv) last_state = -1; while (true) { - struct mpd_status *status; - enum mpd_state state; enum mpd_idle mask; - int abnormal_stop; + + (void) mpdstream_monitor_status(); /* * Wait for specified event(s): @@ -190,40 +232,6 @@ main(int argc, char **argv) log_debug("MPD_IDLE_QUEUE idle event"); continue; } - - status = mpd_run_status(mpdc); - if (status == NULL) - continue; - - state = mpd_status_get_state(status); - if (state == last_state) - continue; - - switch (state) { - case MPD_STATE_PLAY: - log_info("Playback started\n"); - is_stream = is_stream_uri(status); - break; - case MPD_STATE_PAUSE: - log_info("Playback paused\n"); /* No action */ - break; - case MPD_STATE_STOP: - log_info("Playback stopped\n"); - abnormal_stop = mpd_check_abnormal_stop(status); - - if (abnormal_stop) { - log_warn("Auto restart stream after error...\n"); - (void) mpd_replay(song_id); - } - break; - default: - log_warn("Playback unknown state\n"); - break; - } - - mpd_status_free(status); - - last_state = state; } mpd_connection_free(mpdc); -- 2.47.3