]> Untitled Git - mpdstream/commitdiff
TEMP: Test missing events
authorHugo Villeneuve <hvilleneuve@dimonoff.com>
Wed, 1 Apr 2026 23:32:30 +0000 (19:32 -0400)
committerHugo Villeneuve <hvilleneuve@dimonoff.com>
Wed, 1 Apr 2026 23:47:14 +0000 (19:47 -0400)
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 <hvilleneuve@dimonoff.com>
src/main.c

index 7eb99dfec99fcf689fe0292ba0f64a40ea70966f..08dc18dddddf51171cd821818b98a9121dbbfd16 100644 (file)
@@ -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);