]> git.sesse.net Git - vlc/blobdiff - src/playlist/item.c
src/playlist/item.c: Avoid a dead lock.
[vlc] / src / playlist / item.c
index 9c4f6b72f9b90b29dc7ef1bfa84c8f17f1b028dc..31eaf8bc6aec715601a5893903383044984d96ea 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * item.c : Playlist item creation/deletion/add/removal functions
  *****************************************************************************
- * Copyright (C) 1999-2004 the VideoLAN team
+ * Copyright (C) 1999-2007 the VideoLAN team
  * $Id$
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
@@ -34,6 +34,66 @@ static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
                         vlc_bool_t b_stop );
 
+/*****************************************************************************
+ * An input item has gained a subitem (Event Callback)
+ *****************************************************************************/
+static void input_item_subitem_added( const vlc_event_t * p_event,
+                                      void * user_data )
+{
+    playlist_t * p_playlist = user_data;
+    input_item_t * p_parent, * p_child;
+    vlc_bool_t b_play = var_CreateGetBool( p_playlist, "playlist-autostart" );
+    playlist_item_t *p_item_in_category;
+    playlist_item_t *p_current;
+
+    p_parent = p_event->p_obj;
+    p_child = p_event->u.input_item_subitem_added.p_new_child;
+
+    p_current = playlist_ItemGetByInput( p_playlist, p_parent, VLC_FALSE );
+    if( p_current->i_children == -1 )
+        p_item_in_category = playlist_ItemToNode( p_playlist, p_current,
+                                              VLC_FALSE );
+    else
+        p_item_in_category = p_current;
+    p_item_in_category = p_current;
+    b_play = b_play && p_current == p_playlist->status.p_item;
+
+    playlist_NodeAddInput( p_playlist, p_child, p_item_in_category, 
+        PLAYLIST_APPEND | PLAYLIST_SPREPARSE , PLAYLIST_END,
+        VLC_FALSE );
+
+    if( b_play )
+    {
+        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
+                          VLC_TRUE, p_item_in_category, NULL );
+        vlc_object_release( p_playlist );
+    }
+}
+
+/*****************************************************************************
+ * Listen to vlc_InputItemAddSubItem event
+ *****************************************************************************/
+static void install_input_item_observer( playlist_t * p_playlist,
+                                         input_item_t * p_input )
+{
+    msg_Dbg( p_playlist, "Listening to %s with %p", p_input->psz_name, p_playlist);
+
+    vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
+                      input_item_subitem_added,
+                      p_playlist );
+}
+
+static void uninstall_input_item_observer( playlist_t * p_playlist,
+                                           input_item_t * p_input )
+{
+    msg_Dbg( p_playlist, "Not Listening to %s with %p", p_input->psz_name, p_playlist);
+
+    vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
+                      input_item_subitem_added,
+                      p_playlist );
+                      
+}
+
 /*****************************************************************************
  * Playlist item creation
  *****************************************************************************/
@@ -56,8 +116,7 @@ playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
                                               input_item_t *p_input )
 {
     DECMALLOC_NULL( p_item, playlist_item_t );
-    playlist_t *p_playlist = p_obj->p_libvlc->p_playlist;
-    vlc_object_yield( p_playlist );
+    playlist_t *p_playlist = pl_Yield( p_obj );
 
     p_item->p_input = p_input;
     vlc_gc_incref( p_item->p_input );
@@ -68,8 +127,11 @@ playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
     p_item->i_children = -1;
     p_item->pp_children = NULL;
     p_item->i_flags = 0;
+    p_item->p_playlist = p_playlist;
 
-    vlc_object_release( p_playlist );
+    install_input_item_observer( p_playlist, p_input );
+
+    pl_Release( p_item->p_playlist );
 
     return p_item;
 }
@@ -81,6 +143,8 @@ playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
 /** Delete a playlist item and detach its input item */
 int playlist_ItemDelete( playlist_item_t *p_item )
 {
+    uninstall_input_item_observer( p_item->p_playlist, p_item->p_input );
+
     vlc_gc_decref( p_item->p_input );
     free( p_item );
     return VLC_SUCCESS;
@@ -349,8 +413,15 @@ playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
                                             p_playlist, p_item->p_input->i_id,
                                             p_playlist->p_root_onelevel,
                                             VLC_TRUE );
-        assert( p_item_in_one );
+        /* We already have it, and there is nothing more to do */
         ChangeToNode( p_playlist, p_item_in_category );
+
+        if( !p_item_in_one )
+        {
+            if( !b_locked ) PL_UNLOCK;
+            return p_item_in_category;
+        }
+
         /* Item in one is a root, change it to node */
         if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
             ChangeToNode( p_playlist, p_item_in_one );
@@ -408,6 +479,8 @@ static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
 {
     int j;
     playlist_item_t *p_detach = p_item->p_parent;
+    (void)p_playlist;
+
     if( p_node->i_children == -1 ) return VLC_EGENERIC;
 
     for( j = 0; j < p_detach->i_children; j++ )
@@ -509,8 +582,7 @@ int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
 {
     if( psz_name && p_item )
     {
-        if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
-        p_item->p_input->psz_name = strdup( psz_name );
+        input_ItemSetName( p_item->p_input, psz_name );
         return VLC_SUCCESS;
     }
     return VLC_EGENERIC;
@@ -554,18 +626,14 @@ static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
     if( p_playlist->b_auto_preparse &&
           (i_mode & PLAYLIST_PREPARSE ||
           ( i_mode & PLAYLIST_SPREPARSE &&
-            ( !p_item_cat->p_input->p_meta || (p_item_cat->p_input->p_meta &&
-              ( EMPTY_STR( p_item_cat->p_input->p_meta->psz_artist ) ||
-                EMPTY_STR( p_item_cat->p_input->p_meta->psz_album ) )
-              )
-            )
+            ( EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) ||
+            ( EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) ) )
           ) ) )
         playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
     /* If we already have it, signal it */
-    else if( p_item_cat->p_input->p_meta &&
-             !EMPTY_STR( p_item_cat->p_input->p_meta->psz_artist ) &&
-             !EMPTY_STR( p_item_cat->p_input->p_meta->psz_album ) )
-        p_item_cat->p_input->p_meta->i_status = ITEM_PREPARSED;
+    else if( !EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) &&
+             !EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) )
+        input_item_SetPreparsed( p_item_cat->p_input, VLC_TRUE );
 }
 
 /* Add the playlist item to the requested node and fire a notification */
@@ -605,7 +673,7 @@ static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
 {
     int i;
     int i_id = p_item->i_id;
-    vlc_bool_t b_flag = VLC_FALSE;
+    vlc_bool_t b_delay_deletion = VLC_FALSE;
 
     if( p_item->i_children > -1 )
     {
@@ -631,7 +699,7 @@ static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
             msg_Info( p_playlist, "stopping playback" );
             vlc_cond_signal( &p_playlist->object_wait );
         }
-        b_flag = VLC_TRUE;
+        b_delay_deletion = VLC_TRUE;
     }
 
     PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
@@ -639,7 +707,7 @@ static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
     /* Remove the item from its parent */
     playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
 
-    if( b_flag == VLC_FALSE )
+    if( !b_delay_deletion )
         playlist_ItemDelete( p_item );
     else
     {