[vlc-devel] [PATCH] libvlc: add playlist_item_added and playlist_item_deleted event callback

Michel Perrocheau michel.perrocheau at gmail.com
Tue Oct 2 00:36:07 CEST 2018


fix wrong item played when libvlc_media_list_player_next is called after deletion or insertion in media list.
---
 lib/media_list_player.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/lib/media_list_player.c b/lib/media_list_player.c
index 7f4d427f63..d803b4f5d0 100644
--- a/lib/media_list_player.c
+++ b/lib/media_list_player.c
@@ -374,8 +374,40 @@ media_player_reached_end(const libvlc_event_t * p_event, void * p_user_data)
 static void
 mlist_item_deleted(const libvlc_event_t * p_event, void * p_user_data)
 {
-    // Nothing to do. For now.
-    (void)p_event; (void)p_user_data;
+    libvlc_media_list_player_t * p_mlp = p_user_data;
+    int index = p_event->u.media_list_item_added.index;
+
+    vlc_mutex_lock(&p_mlp->mp_callback_lock);
+
+    if (p_mlp->p_mlist)
+        if (p_mlp->current_playing_item_path)
+            // When deletion change current item position, update item path.
+            if (index < p_mlp->current_playing_item_path[0])
+                p_mlp->current_playing_item_path[0]--;
+
+    vlc_mutex_unlock(&p_mlp->mp_callback_lock);
+}
+
+
+/**************************************************************************
+ *       playlist_item_added (private) (Event Callback)
+ **************************************************************************/
+static void
+mlist_item_added(libvlc_event_t * p_event, void * p_user_data)
+{
+    libvlc_media_list_player_t * p_mlp = p_user_data;
+    int index = p_event->u.media_list_item_added.index;
+
+    vlc_mutex_lock(&p_mlp->mp_callback_lock);
+
+    if (p_mlp->p_mlist)
+        if (p_mlp->current_playing_item_path)
+            // When insertion change current item position, update item path.
+            if (index <= p_mlp->current_playing_item_path[0])
+                p_mlp->current_playing_item_path[0]++;
+
+    vlc_mutex_unlock(&p_mlp->mp_callback_lock);
+
 }
 
 
@@ -387,6 +419,7 @@ install_playlist_observer(libvlc_media_list_player_t * p_mlp)
 {
     assert_locked(p_mlp);
     libvlc_event_attach(mlist_em(p_mlp), libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp);
+    libvlc_event_attach(mlist_em(p_mlp), libvlc_MediaListItemAdded, mlist_item_added, p_mlp);
 }
 
 /**************************************************************************
@@ -398,6 +431,7 @@ uninstall_playlist_observer(libvlc_media_list_player_t * p_mlp)
     assert_locked(p_mlp);
     if (!p_mlp->p_mlist) return;
     libvlc_event_detach(mlist_em(p_mlp), libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp);
+    libvlc_event_detach(mlist_em(p_mlp), libvlc_MediaListItemAdded, mlist_item_added, p_mlp);
 }
 
 /**************************************************************************
-- 
2.17.1



More information about the vlc-devel mailing list