[vlc-devel] [PATCH 2/2] lib/media: fix MediaListEndReached not sent when media doesn't have sub items

Thomas Guillem thomas at gllm.fr
Wed Feb 4 10:55:53 CET 2015


MediaListEndReached event should not be sent only when a new sub item tree is
added, otherwise it won't be sent if media doesn't have any sub items.

To fix this issue, send the MediaListEndReached event when preparse function
terminates (only if the media_list was created either by user or by sub items
callbacks).
---
 lib/media.c | 48 +++++++++++++++++++++++++++++++++---------------
 1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/lib/media.c b/lib/media.c
index 00e1e8a..9ccfb25 100644
--- a/lib/media.c
+++ b/lib/media.c
@@ -104,12 +104,13 @@ static const libvlc_meta_t vlc_to_libvlc_meta[] =
     [vlc_meta_DiscNumber]   = libvlc_meta_DiscNumber
 };
 
-static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md )
+static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
+                                                bool b_create )
 {
     libvlc_media_list_t *p_subitems = NULL;
 
     vlc_mutex_lock( &p_md->subitems_lock );
-    if( p_md->p_subitems == NULL )
+    if( p_md->p_subitems == NULL && b_create )
     {
         p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
         if( p_md->p_subitems != NULL )
@@ -139,7 +140,7 @@ static void input_item_subitem_added( const vlc_event_t *p_event,
                 p_event->u.input_item_subitem_added.p_new_child );
 
     /* Add this to our media list */
-    p_subitems = media_get_subitems( p_md );
+    p_subitems = media_get_subitems( p_md, true );
     if( p_subitems != NULL )
     {
         libvlc_media_list_lock( p_subitems );
@@ -164,18 +165,8 @@ static void input_item_subitemtree_added( const vlc_event_t * p_event,
 {
     VLC_UNUSED( p_event );
     libvlc_media_t * p_md = user_data;
-    libvlc_media_list_t *p_subitems;
     libvlc_event_t event;
 
-    /* notify the media list */
-    p_subitems = media_get_subitems( p_md );
-    if( p_subitems != NULL )
-    {
-        libvlc_media_list_lock( p_subitems );
-        libvlc_media_list_internal_end_reached( p_subitems );
-        libvlc_media_list_unlock( p_subitems );
-    }
-
     /* Construct the event */
     event.type = libvlc_MediaSubItemTreeAdded;
     event.u.media_subitemtree_added.item = p_md;
@@ -246,6 +237,25 @@ static void input_item_preparsed_changed(const vlc_event_t *p_event,
 }
 
 /**************************************************************************
+ * input_item_preparse_ended (Private) (vlc event Callback)
+ **************************************************************************/
+static void input_item_preparse_ended( const vlc_event_t * p_event,
+                                       void * user_data )
+{
+    VLC_UNUSED( p_event );
+    libvlc_media_t * p_md = user_data;
+    libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
+
+    if( p_subitems != NULL )
+    {
+        /* notify the media list */
+        libvlc_media_list_lock( p_subitems );
+        libvlc_media_list_internal_end_reached( p_subitems );
+        libvlc_media_list_unlock( p_subitems );
+    }
+}
+
+/**************************************************************************
  * Install event handler (Private)
  **************************************************************************/
 static void install_input_item_observer( libvlc_media_t *p_md )
@@ -270,6 +280,10 @@ static void install_input_item_observer( libvlc_media_t *p_md )
                       vlc_InputItemSubItemTreeAdded,
                       input_item_subitemtree_added,
                       p_md );
+    vlc_event_attach( &p_md->p_input_item->event_manager,
+                      vlc_InputItemPreparseEnded,
+                      input_item_preparse_ended,
+                      p_md );
 }
 
 /**************************************************************************
@@ -297,6 +311,10 @@ static void uninstall_input_item_observer( libvlc_media_t *p_md )
                       vlc_InputItemSubItemTreeAdded,
                       input_item_subitemtree_added,
                       p_md );
+    vlc_event_detach( &p_md->p_input_item->event_manager,
+                      vlc_InputItemPreparseEnded,
+                      input_item_preparse_ended,
+                      p_md );
 }
 
 /**************************************************************************
@@ -428,7 +446,7 @@ libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
 
     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
 
-    p_subitems = media_get_subitems( p_md );
+    p_subitems = media_get_subitems( p_md, true );
     if( p_subitems == NULL) {
         libvlc_media_release( p_md );
         return NULL;
@@ -606,7 +624,7 @@ libvlc_media_set_state( libvlc_media_t *p_md,
 libvlc_media_list_t *
 libvlc_media_subitems( libvlc_media_t * p_md )
 {
-    libvlc_media_list_t *p_subitems = media_get_subitems( p_md );
+    libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
     if( p_subitems )
         libvlc_media_list_retain( p_subitems );
     return p_subitems;
-- 
2.1.3




More information about the vlc-devel mailing list