]> git.sesse.net Git - vlc/blobdiff - src/playlist/control.c
Don't loop continuously the playlist thread, use cond.
[vlc] / src / playlist / control.c
index e82850c16200ce998a5eb65d70fa9747dfb94132..735f58db1fbb284cbbc852eeca0e36e8aae6d1e9 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * control.c : Hanle control of the playlist & running through it
+ * control.c : Handle control of the playlist & running through it
  *****************************************************************************
  * Copyright (C) 1999-2004 the VideoLAN team
  * $Id: /local/vlc/0.8.6-playlist-vlm/src/playlist/playlist.c 13741 2006-03-21T19:29:39.792444Z zorglub  $
@@ -24,6 +24,7 @@
 #include <vlc/vlc.h>
 #include <vlc/input.h>
 #include "vlc_playlist.h"
+#include "playlist_internal.h"
 #include <assert.h>
 
 /*****************************************************************************
@@ -83,10 +84,8 @@ int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
     playlist_item_t *p_item, *p_node;
     vlc_value_t val;
 
-    if( p_playlist->i_size <= 0 )
-    {
+    if( p_playlist->items.i_size <= 0 )
         return VLC_EGENERIC;
-    }
 
     switch( i_query )
     {
@@ -111,6 +110,8 @@ int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
         p_playlist->request.b_request = VLC_TRUE;
         p_playlist->request.p_node = p_node;
         p_playlist->request.p_item = p_item;
+        if( p_item && var_GetBool( p_playlist, "random" ) )
+            p_playlist->b_reset_currently_playing = VLC_TRUE;
         break;
 
     case PLAYLIST_PLAY:
@@ -178,6 +179,7 @@ int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
         return VLC_EBADVAR;
         break;
     }
+    vlc_cond_signal( &p_playlist->object_wait );
 
     return VLC_SUCCESS;
 }
@@ -196,6 +198,7 @@ int playlist_PreparseEnqueue( playlist_t *p_playlist,
                  p_playlist->p_preparse->i_waiting,
                  p_item );
     vlc_mutex_unlock( &p_playlist->p_preparse->object_lock );
+    vlc_cond_signal( &p_playlist->p_preparse->object_wait );
     return VLC_SUCCESS;
 }
 
@@ -212,6 +215,27 @@ int playlist_PreparseEnqueueItem( playlist_t *p_playlist,
     return VLC_SUCCESS;
 }
 
+int playlist_AskForArtEnqueue( playlist_t *p_playlist,
+                              input_item_t *p_item )
+{
+    int i;
+    preparse_item_t p;
+    p.p_item = p_item;
+    p.b_fetch_art = VLC_TRUE;
+
+    vlc_mutex_lock( &p_playlist->p_secondary_preparse->object_lock );
+    for( i = 0; i < p_playlist->p_secondary_preparse->i_waiting &&
+         p_playlist->p_secondary_preparse->p_waiting->b_fetch_art == VLC_TRUE;
+         i++ );
+    vlc_gc_incref( p_item );
+    INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
+                 p_playlist->p_secondary_preparse->i_waiting,
+                 i, p );
+    vlc_mutex_unlock( &p_playlist->p_secondary_preparse->object_lock );
+    vlc_cond_signal( &p_playlist->p_secondary_preparse->object_wait );
+    return VLC_SUCCESS;
+}
+
 void PreparseEnqueueItemSub( playlist_t *p_playlist,
                              playlist_item_t *p_item )
 {
@@ -228,8 +252,7 @@ void PreparseEnqueueItemSub( playlist_t *p_playlist,
     {
         for( i = 0; i < p_item->i_children; i++)
         {
-            PreparseEnqueueItemSub( p_playlist,
-                                             p_item->pp_children[i] );
+            PreparseEnqueueItemSub( p_playlist, p_item->pp_children[i] );
         }
     }
 }
@@ -238,6 +261,66 @@ void PreparseEnqueueItemSub( playlist_t *p_playlist,
  * Playback logic
  *****************************************************************************/
 
+static void ResyncCurrentIndex(playlist_t *p_playlist, playlist_item_t *p_cur )
+{
+     PL_DEBUG("resyncing on %s", PLI_NAME(p_cur) );
+     /* Simply resync index */
+     int i;
+     p_playlist->i_current_index = -1;
+     for( i = 0 ; i< p_playlist->current.i_size; i++ )
+     {
+          if( ARRAY_VAL(p_playlist->current, i) == p_cur )
+          {
+              p_playlist->i_current_index = i;
+              break;
+          }
+     }
+     PL_DEBUG("%s is at %i", PLI_NAME(p_cur), p_playlist->i_current_index );
+}
+
+static void ResetCurrentlyPlaying( playlist_t *p_playlist, vlc_bool_t b_random,
+                                   playlist_item_t *p_cur )
+{
+    playlist_item_t *p_next = NULL;
+    PL_DEBUG("rebuilding array of current - root %s",
+              PLI_NAME(p_playlist->status.p_node) );
+    ARRAY_RESET(p_playlist->current);
+    p_playlist->i_current_index = -1;
+    while( 1 )
+    {
+        /** FIXME: this is *slow* */
+        p_next = playlist_GetNextLeaf( p_playlist,
+                                       p_playlist->status.p_node,
+                                       p_next, VLC_TRUE, VLC_FALSE );
+        if( p_next )
+        {
+            if( p_next == p_cur )
+                p_playlist->i_current_index = p_playlist->current.i_size;
+            ARRAY_APPEND( p_playlist->current, p_next);
+        }
+        else break;
+    }
+    PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
+                                                  p_playlist->i_current_index);
+    if( b_random )
+    {
+        /* Shuffle the array */
+        srand( (unsigned int)mdate() );
+        int swap = 0;
+        int j;
+        for( j = p_playlist->current.i_size - 1; j > 0; j-- )
+        {
+            swap++;
+            int i = rand() % (j+1); /* between 0 and j */
+            playlist_item_t *p_tmp;
+            p_tmp = ARRAY_VAL(p_playlist->current, i);
+            ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
+            ARRAY_VAL(p_playlist->current,j) = p_tmp;
+        }
+    }
+    p_playlist->b_reset_currently_playing = VLC_FALSE;
+}
+
 /** This function calculates the next playlist item, depending
  *  on the playlist course mode (forward, backward, random, view,...). */
 playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
@@ -252,7 +335,7 @@ playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
 
     /* Handle quickly a few special cases */
     /* No items to play */
-    if( p_playlist->i_size == 0 )
+    if( p_playlist->items.i_size == 0 )
     {
         msg_Info( p_playlist, "playlist is empty" );
         return NULL;
@@ -271,109 +354,74 @@ playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
         return NULL;
     }
 
-    if( !p_playlist->request.b_request && p_playlist->status.p_item &&
-         p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
-    {
-        msg_Dbg( p_playlist, "blocking item, stopping") ;
-        return NULL;
-    }
-
-    /* Random case. This is an exception: if request, but request is skip +- 1
-     * we don't go to next item but select a new random one. */
-    if( b_random )
-        msg_Err( p_playlist, "random unsupported" );
-#if 0
-            &&
-        ( !p_playlist->request.b_request ||
-        ( p_playlist->request.b_request && ( p_playlist->request.p_item == NULL ||
-          p_playlist->request.i_skip == 1 || p_playlist->request.i_skip == -1 ) ) ) )
+    if( !p_playlist->request.b_request && p_playlist->status.p_item )
     {
-        /* how many items to choose from ? */
-        i_count = 0;
-        for ( i = 0; i < p_playlist->i_size; i++ )
+        playlist_item_t *p_parent = p_playlist->status.p_item;
+        while( p_parent )
         {
-            if ( p_playlist->pp_items[i]->p_input->i_nb_played == 0 )
-                i_count++;
-        }
-        /* Nothing left? */
-        if ( i_count == 0 )
-        {
-            /* Don't loop? Exit! */
-            if( !b_loop )
-                return NULL;
-            /* Otherwise reset the counter */
-            for ( i = 0; i < p_playlist->i_size; i++ )
+            if( p_parent->i_flags & PLAYLIST_SKIP_FLAG )
             {
-                p_playlist->pp_items[i]->p_input->i_nb_played = 0;
+                msg_Dbg( p_playlist, "blocking item, stopping") ;
+                return NULL;
             }
-            i_count = p_playlist->i_size;
-        }
-        srand( (unsigned int)mdate() );
-        i = rand() % i_count + 1 ;
-        /* loop thru the list and count down the unplayed items to the selected one */
-        for ( i_new = 0; i_new < p_playlist->i_size && i > 0; i_new++ )
-        {
-            if ( p_playlist->pp_items[i_new]->p_input->i_nb_played == 0 )
-                i--;
+            p_parent = p_parent->p_parent;
         }
-        i_new--;
-
-        p_playlist->request.i_skip = 0;
-        p_playlist->request.b_request = VLC_FALSE;
-        return p_playlist->pp_items[i_new];
     }
-#endif
 
     /* Start the real work */
     if( p_playlist->request.b_request )
     {
-        PL_DEBUG( "processing request node %s item %s skip %i",
-                        PLI_NAME( p_playlist->request.p_item ),
-                        PLI_NAME( p_playlist->request.p_node ), i_skip );
         p_new = p_playlist->request.p_item;
         i_skip = p_playlist->request.i_skip;
+        PL_DEBUG( "processing request item %s node %s skip %i",
+                        PLI_NAME( p_playlist->request.p_item ),
+                        PLI_NAME( p_playlist->request.p_node ), i_skip );
 
-        if( p_playlist->request.p_node )
+        if( p_playlist->request.p_node &&
+            p_playlist->request.p_node != p_playlist->status.p_node )
+        {
             p_playlist->status.p_node = p_playlist->request.p_node;
+            p_playlist->b_reset_currently_playing = VLC_TRUE;
+        }
 
         /* If we are asked for a node, dont take it */
         if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
             i_skip++;
 
-        if( i_skip > 0 )
+        if( p_playlist->b_reset_currently_playing )
+            ResetCurrentlyPlaying( p_playlist, b_random, p_new );
+        else if( p_new )
+            ResyncCurrentIndex( p_playlist, p_new );
+        else
+            p_playlist->i_current_index = -1;
+
+        if( p_playlist->current.i_size && i_skip > 0 )
         {
             for( i = i_skip; i > 0 ; i-- )
             {
-                p_new = playlist_GetNextEnabledLeaf( p_playlist,
-                                                     p_playlist->request.p_node,
-                                                     p_new );
-                if( p_new == NULL )
+                p_playlist->i_current_index++;
+                if( p_playlist->i_current_index == p_playlist->current.i_size )
                 {
                     PL_DEBUG( "looping - restarting at beginning of node" );
-                    p_new = playlist_GetNextLeaf( p_playlist,
-                                                  p_playlist->request.p_node,
-                                                  NULL );
-                    if( p_new == NULL ) break;
+                    p_playlist->i_current_index = 0;
                 }
             }
+            p_new = ARRAY_VAL( p_playlist->current,
+                               p_playlist->i_current_index );
         }
-        else if( i_skip < 0 )
+        else if( p_playlist->current.i_size && i_skip < 0 )
         {
             for( i = i_skip; i < 0 ; i++ )
             {
-                p_new = playlist_GetPrevLeaf( p_playlist,
-                                              p_playlist->request.p_node,
-                                              p_new );
-                if( p_new == NULL )
+                p_playlist->i_current_index--;
+                if( p_playlist->i_current_index == -1 )
                 {
                     PL_DEBUG( "looping - restarting at end of node" );
-                    /** \bug This is needed because GetPrevLeaf does not loop
-                      * by itself */
-                    p_new = playlist_GetLastLeaf( p_playlist,
-                                                 p_playlist->request.p_node );
+                    p_playlist->i_current_index = p_playlist->current.i_size-1;
                 }
-                if( p_new == NULL ) break;
             }
+            p_new = ARRAY_VAL( p_playlist->current,
+                               p_playlist->i_current_index );
         }
         /* Clear the request */
         p_playlist->request.b_request = VLC_FALSE;
@@ -381,30 +429,31 @@ playlist_item_t * playlist_NextItem( playlist_t *p_playlist )
     /* "Automatic" item change ( next ) */
     else
     {
-        PL_DEBUG( "changing item without a request" );
+        PL_DEBUG( "changing item without a request (current %i/%i)",
+                  p_playlist->i_current_index, p_playlist->current.i_size );
         /* Cant go to next from current item */
         if( p_playlist->status.p_item &&
             p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
             return NULL;
 
-        p_new = playlist_GetNextLeaf( p_playlist,
-                                      p_playlist->status.p_node,
-                                      p_playlist->status.p_item );
-        if( p_new == NULL && b_loop )
+        if( p_playlist->b_reset_currently_playing )
+            ResetCurrentlyPlaying( p_playlist, b_random,
+                                   p_playlist->status.p_item );
+
+        p_playlist->i_current_index++;
+        if( p_playlist->i_current_index == p_playlist->current.i_size )
         {
-            PL_DEBUG( "looping" );
-            p_new = playlist_GetNextLeaf( p_playlist,
-                                          p_playlist->status.p_node,
-                                          NULL );
+            if( !b_loop || p_playlist->current.i_size == 0 ) return NULL;
+            p_playlist->i_current_index = 0;
         }
+        PL_DEBUG( "using item %i", p_playlist->i_current_index );
+        if ( p_playlist->current.i_size == 0 ) return NULL;
+
+        p_new = ARRAY_VAL( p_playlist->current, p_playlist->i_current_index );
         /* The new item can't be autoselected  */
         if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
             return NULL;
     }
-    if( p_new == NULL )
-    {
-        msg_Dbg( p_playlist, "did not find something to play" );
-    }
     return p_new;
 }