]> git.sesse.net Git - vlc/commitdiff
* playlist.c: better random algorithm: do not play an item if it has
authorCyril Deguet <asmax@videolan.org>
Sat, 3 Apr 2004 14:59:15 +0000 (14:59 +0000)
committerCyril Deguet <asmax@videolan.org>
Sat, 3 Apr 2004 14:59:15 +0000 (14:59 +0000)
  already been played (check the i_nb_played variable)
  * item.c: initialize i_nb_played to 0

src/playlist/item.c
src/playlist/playlist.c

index bd2a57b36d386970be1eb10aa26605667b64f8e0..27faf26688df3b87440ab13079ca92fcca47c700 100644 (file)
@@ -55,6 +55,7 @@ playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
 
     p_item->b_enabled = VLC_TRUE;
     p_item->i_group = PLAYLIST_TYPE_MANUAL;
+    p_item->i_nb_played = 0;
 
     p_item->input.i_duration = -1;
     p_item->input.ppsz_options = NULL;
index fea9148adca8574b86f8371ee40fba7a6c6bde53..6968d5f4e120a70c2a5764223c0f12d6800a9892 100644 (file)
@@ -510,20 +510,33 @@ static void SkipItem( playlist_t *p_playlist, int i_arg )
     if( b_random )
     {
         srand( (unsigned int)mdate() );
-
-        /* Simple random stuff - we cheat a bit to minimize the chances to
-         * get the same index again. */
-        i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
-        if( i_arg == 0 )
+        int i_count = 0;
+        while( i_count < p_playlist->i_size )
+        {
+            p_playlist->i_index =
+                (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
+            /* Check if the item has not already been played */
+            if( p_playlist->pp_items[p_playlist->i_index]->i_nb_played == 0 )
+                break;
+            i_count++;
+        }
+        if( i_count == p_playlist->i_size )
         {
-            i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
+            /* The whole playlist has been played: reset the counters */
+            while( i_count > 0 )
+            {
+                p_playlist->pp_items[--i_count]->i_nb_played = 0;
+            }
+            if( !b_loop )
+            {
+                p_playlist->i_status = PLAYLIST_STOPPED;
+            }
         }
     }
-    if( b_repeat )
+    else if( !b_repeat )
     {
-        i_arg = 0;
+        p_playlist->i_index += i_arg;
     }
-    p_playlist->i_index += i_arg;
 
     /* Boundary check */
     if( p_playlist->i_index >= p_playlist->i_size )