From b8ca012eb868426176f571571b26009624075ba6 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 18 Dec 2020 10:56:44 +0100
Subject: [PATCH] Should fix the deadlock

Don't use the deprecated IDLE event, use the {START,END}_FILE events instead
and thus don't be dependent on some callbacks to get the time. Should also fix
the 'minimal' kara duration issue...
---
 src/module/module_sdl2.c |  1 +
 src/module/mpv.c         | 33 +++++++++++++++++++--------------
 src/module/mpv.h         | 10 ++++++++--
 3 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index d294f4a1..66d351f5 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -440,6 +440,7 @@ module_sdl2_close(struct module_sdl2_window *win)
 {
     RETURN_UNLESS(win && win->window, "Invalid arguments", NOTHING);
     win->mpv_time_pos = win->mpv_duration = 0;
+    win->state = STATE_STOP;
     RETURN_UNLESS(win->mpv, "Missing mpv ctx", NOTHING);
     static const char *cmd[] = { "stop", NULL };
     mpv_command_async((mpv_handle *) win->mpv, 0, cmd);
diff --git a/src/module/mpv.c b/src/module/mpv.c
index 5b955b4a..67735f98 100644
--- a/src/module/mpv.c
+++ b/src/module/mpv.c
@@ -10,12 +10,6 @@
 #include <unistd.h>
 #include <sqlite3.h>
 
-enum {
-    STATE_STOP  = 0,
-    STATE_PLAY  = 1,
-    STATE_PAUSE = 2,
-};
-
 void
 lmpv_free(mpv_handle **ctx)
 {
@@ -151,8 +145,7 @@ lmpv_toggle_pause(mpv_handle *ctx)
 
 int
 lmpv_handle(struct lkt_module *mod, mpv_handle *ctx, struct queue *queue,
-            volatile int *time_pos, volatile int *time_duration,
-            volatile int *state)
+            volatile int *time_pos, volatile int *time_duration, volatile int *state)
 {
     size_t ao_volume;
     mpv_event *event = NULL;
@@ -183,8 +176,13 @@ loop:
     case MPV_EVENT_NONE:
         return 1;
 
-    case MPV_EVENT_IDLE:
-        if (*state != STATE_STOP && *time_pos > 0)
+    case MPV_EVENT_START_FILE:
+        LOG_DEBUG("WINDOW", "Start of file!");
+        break;
+
+    case MPV_EVENT_END_FILE:
+        LOG_DEBUG("WINDOW", "End of file!");
+        if ((*state) != STATE_STOP)
             lkt_queue_send(queue, lkt_event_play_next, NULL);
         break;
 
@@ -222,15 +220,22 @@ loop:
         }
         break;
 
+    case MPV_EVENT_COMMAND_REPLY:
+        free((void *) event->reply_userdata);
+        break;
+
     /* Ignored */
     case MPV_EVENT_VIDEO_RECONFIG:
     case MPV_EVENT_AUDIO_RECONFIG:
+    case MPV_EVENT_IDLE:
+    case MPV_EVENT_TRACKS_CHANGED:
+    case MPV_EVENT_FILE_LOADED:
+    case MPV_EVENT_METADATA_UPDATE:
+    case MPV_EVENT_PLAYBACK_RESTART:
         break;
 
-    case MPV_EVENT_COMMAND_REPLY:
-        free((void *) event->reply_userdata);
-        break;
-
+    /* Forgot to ignore an event, or it should not be ignored and should be
+     * handled, or a new event is present in the API */
     default:
         LOG_WARN("WINDOW", "Unhandled mpv event '%s'", mpv_event_name(event->event_id));
         break;
diff --git a/src/module/mpv.h b/src/module/mpv.h
index cff2cc18..d6901e8d 100644
--- a/src/module/mpv.h
+++ b/src/module/mpv.h
@@ -15,5 +15,11 @@ int lmpv_set_volume(mpv_handle *ctx, int vol);
 int lmpv_load_file(mpv_handle *ctx, const char *file);
 int lmpv_toggle_pause(mpv_handle *ctx);
 int lmpv_handle(struct lkt_module *mod, mpv_handle *ctx, struct queue *queue,
-                volatile int *time_pos, volatile int *time_duration,
-                volatile int *state);
+                volatile int *time_pos, volatile int *time_duration, volatile int *state);
+
+enum {
+    STATE_STOP  = 0,
+    STATE_PLAY  = 1,
+    STATE_PAUSE = 2,
+};
+
-- 
GitLab