]> git.sesse.net Git - vlc/blobdiff - src/playlist/item.c
avcodec: map LCL MSZH and ZLIB
[vlc] / src / playlist / item.c
index 8213308fbb247a92ebb153e62e678004e262fd93..b27389a160df7a2edc798a65807b0a1fe2ec9e7d 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * item.c : Playlist item creation/deletion/add/removal functions
  *****************************************************************************
- * Copyright (C) 1999-2007 the VideoLAN team
+ * Copyright (C) 1999-2007 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *          ClĂ©ment Stenac <zorglub@videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -28,6 +28,7 @@
 #include <vlc_common.h>
 #include <assert.h>
 #include <vlc_playlist.h>
+#include <vlc_rand.h>
 #include "playlist_internal.h"
 
 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
@@ -61,60 +62,111 @@ static void input_item_add_subitem_tree ( const vlc_event_t * p_event,
         playlist_ItemGetByInput( p_playlist, p_input );
 
     assert( p_item != NULL );
-    playlist_item_t *p_parent = p_item->p_parent;
-    assert( p_parent != NULL );
 
     bool b_current = get_current_status_item( p_playlist ) == p_item;
-    bool b_autostart = var_CreateGetBool( p_playlist, "playlist-autostart" );
+    bool b_autostart = var_GetBool( p_playlist, "playlist-autostart" );
     bool b_stop = p_item->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
+    bool b_flat = false;
+
     p_item->i_flags &= ~PLAYLIST_SUBITEM_STOP_FLAG;
 
-    int pos = 0;
-    for( int i = 0; i < p_parent->i_children; i++ )
-    {
-        if( p_parent->pp_children[i] == p_item )
+    /* We will have to flatten the tree out if we are in "the playlist" node and
+    the user setting demands flat playlist */
+
+    if( !pl_priv(p_playlist)->b_tree ) {
+        playlist_item_t *p_up = p_item;
+        while( p_up->p_parent )
         {
-            pos = i;
-            break;
+            if( p_up->p_parent == p_playlist->p_playing )
+            {
+                b_flat = true;
+                break;
+            }
+            p_up = p_up->p_parent;
         }
     }
 
-    bool b_flat = false;
-    playlist_item_t *p_up = p_item;
-    while( p_up->p_parent )
+    int pos = 0;
+
+    /* If we have to flatten out, then take the item's position in the parent as
+    insertion point and delete the item */
+
+    if( b_flat )
     {
-        if( p_up->p_parent == p_playlist->p_playing )
+        playlist_item_t *p_parent = p_item->p_parent;
+        assert( p_parent != NULL );
+
+        int i;
+        for( i = 0; i < p_parent->i_children; i++ )
         {
-            if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
-            break;
+            if( p_parent->pp_children[i] == p_item )
+            {
+                pos = i;
+                break;
+            }
         }
-        p_up = p_up->p_parent;
-    }
+        assert( i < p_parent->i_children );
 
-    if( b_flat )
         playlist_DeleteItem( p_playlist, p_item, true );
 
-    p_item = playlist_InsertInputItemTree( p_playlist,
-                                           b_flat ? p_parent : p_item,
-                                           p_new_root,
-                                           b_flat ? pos: PLAYLIST_END,
-                                           b_flat );
+        p_item = p_parent;
+    }
+    else
+    {
+        pos = p_item->i_children >= 0 ? p_item->i_children : 0;
+    }
+
+    /* At this point:
+    "p_item" is the node where sub-items should be inserted,
+    "pos" is the insertion position in that node */
+
+    int last_pos = playlist_InsertInputItemTree( p_playlist,
+                                                 p_item,
+                                                 p_new_root,
+                                                 pos,
+                                                 b_flat );
 
-    if( !b_flat ) var_SetAddress( p_playlist, "leaf-to-parent", p_input );
+    if( !b_flat ) var_SetInteger( p_playlist, "leaf-to-parent", p_item->i_id );
 
     //control playback only if it was the current playing item that got subitems
     if( b_current )
     {
-        if( !p_item || ( b_stop && !b_flat ) || !b_autostart )
+        if( last_pos == pos || ( b_stop && !b_flat ) || !b_autostart )
         {
+            /* We stop, either because no sub-item was actually created, or some
+            flags/settings want us to do so at this point */
             PL_UNLOCK;
             playlist_Stop( p_playlist );
             return;
         }
         else
         {
+            /* Continue to play, either random or the first new item */
+            playlist_item_t *p_play_item;
+
+            if( var_GetBool( p_playlist, "random" ) )
+            {
+                unsigned rand_pos =
+                    ((unsigned)vlc_mrand48()) % (last_pos - pos);
+                rand_pos += pos;
+                p_play_item = p_item->pp_children[rand_pos];
+            }
+            else
+            {
+                p_play_item = p_item->pp_children[pos];
+                /* NOTE: this is a work around the general bug:
+                if node-to-be-played contains sub-nodes, then second instead
+                of first leaf starts playing (only in case the leafs have just
+                been instered and playlist has not yet been rebuilt.)
+                */
+                while( p_play_item->i_children > 0 )
+                    p_play_item = p_play_item->pp_children[0];
+            }
+
             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
-                pl_Locked, get_current_status_node( p_playlist ), p_item );
+                                  pl_Locked,
+                                  get_current_status_node( p_playlist ),
+                                  p_play_item );
         }
     }
 
@@ -370,7 +422,7 @@ int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
     int i_ret;
     input_item_t *p_input;
 
-    p_input = input_item_NewExt( p_playlist, psz_uri, psz_name,
+    p_input = input_item_NewExt( psz_uri, psz_name,
                                  i_options, ppsz_options, i_option_flags,
                                  i_duration );
     if( p_input == NULL )
@@ -399,7 +451,7 @@ int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
                        bool b_locked )
 {
     playlist_item_t *p_item;
-    if( p_playlist->b_die ) return VLC_EGENERIC;
+
     if( !pl_priv(p_playlist)->b_doing_ml )
         PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
                                              p_input->psz_uri );
@@ -407,7 +459,11 @@ int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
     PL_LOCK_IF( !b_locked );
 
     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
-    if( p_item == NULL ) return VLC_ENOMEM;
+    if( p_item == NULL )
+    {
+        PL_UNLOCK_IF( !b_locked );
+        return VLC_ENOMEM;
+    }
     AddItem( p_playlist, p_item,
              b_playlist ? p_playlist->p_playing :
                           p_playlist->p_media_library , i_mode, i_pos );
@@ -441,16 +497,16 @@ playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
     assert( p_input );
     assert( p_parent && p_parent->i_children != -1 );
 
-    if( p_playlist->b_die )
-        return NULL;
     PL_LOCK_IF( !b_locked );
 
     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
-    if( p_item == NULL ) return NULL;
+    if( p_item == NULL )
+        goto end;
     AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
 
     GoAndPreparse( p_playlist, i_mode, p_item );
 
+end:
     PL_UNLOCK_IF( !b_locked );
 
     return p_item;
@@ -508,13 +564,12 @@ int playlist_NodeAddCopy (
  * \param b_flat TRUE if the new tree contents should be flattened into a list
  * \return the first new leaf inserted (in playing order)
  */
-playlist_item_t *playlist_InsertInputItemTree (
+int playlist_InsertInputItemTree (
     playlist_t *p_playlist, playlist_item_t *p_parent,
     input_item_node_t *p_node, int i_pos, bool b_flat )
 {
-  playlist_item_t *p_first_leaf = NULL;
-  RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
-  return p_first_leaf;
+    playlist_item_t *p_first_leaf = NULL;
+    return RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
 }
 
 
@@ -662,10 +717,25 @@ void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
     add.i_item = i_item_id;
     add.i_node = i_node_id;
 
-    vlc_value_t val;
-    val.p_address = &add;
+    var_SetAddress( p_playlist, "playlist-item-append", &add );
+}
+
+/**
+ * Get the duration of all items in a node.
+ */
+mtime_t playlist_GetNodeDuration( playlist_item_t* node )
+{
+    /* For the assert */
+    playlist_t *p_playlist = node->p_playlist;
+    PL_ASSERT_LOCKED;
+
+    mtime_t mt_duration = 0;
+
+    if( node->i_children != -1 )
+        for( int i = 0; i < node->i_children; i++ )
+            mt_duration += input_item_GetDuration( node->pp_children[i]->p_input );
 
-    var_Set( p_playlist, "playlist-item-append", val );
+    return mt_duration;
 }
 
 /***************************************************************************
@@ -696,7 +766,7 @@ static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
         input_item_IsPreparsed( p_item->p_input ) == false &&
             ( EMPTY_STR( psz_artist ) || ( EMPTY_STR( psz_album ) ) )
           )
-        playlist_PreparseEnqueue( p_playlist, p_item->p_input );
+        libvlc_MetaRequest( p_playlist->p_libvlc, p_item->p_input );
     free( psz_artist );
     free( psz_album );
 }
@@ -744,105 +814,60 @@ static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
 int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
                         bool b_stop )
 {
-    int i;
-    int i_id = p_item->i_id;
-    PL_ASSERT_LOCKED;
-
-    if( p_item->i_children > -1 )
-    {
-        return playlist_NodeDelete( p_playlist, p_item, true, false );
-    }
+    assert( b_stop );
+    return playlist_NodeDelete( p_playlist, p_item, true, false );
+}
 
-    pl_priv(p_playlist)->b_reset_currently_playing = true;
-    var_SetInteger( p_playlist, "playlist-item-deleted", i_id );
+static int RecursiveAddIntoParent (
+    playlist_t *p_playlist, playlist_item_t *p_parent,
+    input_item_node_t *p_node, int i_pos, bool b_flat,
+    playlist_item_t **pp_first_leaf )
+{
+    *pp_first_leaf = NULL;
 
-    /* Remove the item from the bank */
-    ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
-    if( i != -1 )
-        ARRAY_REMOVE( p_playlist->all_items, i );
+    if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
 
-    ARRAY_BSEARCH( p_playlist->items,->i_id, int, i_id, i );
-    if( i != -1 )
-        ARRAY_REMOVE( p_playlist->items, i );
+    if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
 
-    /* Check if it is the current item */
-    if( get_current_status_item( p_playlist ) == p_item )
+    for( int i = 0; i < p_node->i_children; i++ )
     {
-        /* Stop it if we have to */
-        if( b_stop )
-        {
-            playlist_Control( p_playlist, PLAYLIST_STOP, pl_Locked );
-            msg_Info( p_playlist, "stopping playback" );
-        }
-        /* In any case, this item can't be the next one to be played ! */
-        set_current_status_item( p_playlist, NULL );
-    }
+        input_item_node_t *p_child_node = p_node->pp_children[i];
 
-    ARRAY_BSEARCH( p_playlist->current,->i_id, int, i_id, i );
-    if( i != -1 )
-        ARRAY_REMOVE( p_playlist->current, i );
-
-    PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
-
-    /* Remove the item from its parent */
-    playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
-
-    playlist_ItemRelease( p_item );
+        playlist_item_t *p_new_item = NULL;
+        bool b_children = p_child_node->i_children > 0;
 
-    return VLC_SUCCESS;
-}
+        //Create the playlist item represented by input node, if allowed.
+        if( !(b_flat && b_children) )
+        {
+            p_new_item = playlist_NodeAddInput( p_playlist,
+                    p_child_node->p_item,
+                    p_parent,
+                    PLAYLIST_INSERT, i_pos,
+                    pl_Locked );
+            if( !p_new_item ) return i_pos;
+
+            i_pos++;
+        }
+        //Recurse if any children
+        if( b_children )
+        {
+            //Substitute p_new_item for first child leaf
+            //(If flat, continue counting from current position)
+            int i_last_pos = RecursiveAddIntoParent(
+                    p_playlist,
+                    p_new_item ? p_new_item : p_parent,
+                    p_child_node,
+                    ( b_flat ? i_pos : 0 ),
+                    b_flat,
+                    &p_new_item );
+            //If flat, take position after recursion as current position
+            if( b_flat ) i_pos = i_last_pos;
+        }
 
-static int RecursiveAddIntoParent (
-    playlist_t *p_playlist, playlist_item_t *p_parent,
-    input_item_node_t *p_node, int i_pos, bool b_flat,
-    playlist_item_t **pp_first_leaf )
-{
-  *pp_first_leaf = NULL;
-
-  if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
-
-  if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
-
-  for( int i = 0; i < p_node->i_children; i++ )
-  {
-      input_item_node_t *p_child_node = p_node->pp_children[i];
-
-      playlist_item_t *p_new_item = NULL;
-      bool b_children = p_child_node->i_children > 0;
-
-      //Create the playlist item represented by input node, if allowed.
-      if( !(b_flat && b_children) )
-      {
-          p_new_item = playlist_NodeAddInput( p_playlist,
-                                              p_child_node->p_item,
-                                              p_parent,
-                                              PLAYLIST_INSERT, i_pos,
-                                              pl_Locked );
-          if( !p_new_item ) return i_pos;
-
-          i_pos++;
-
-          //Swap parent for new item, if we will recurse
-          if( b_children ) p_parent = p_new_item;
-      }
-      //Recurse if any children
-      if( b_children )
-      {
-          //Substitute p_new_item for first child leaf
-          //(If flat, continue counting from current position)
-          int i_last_pos = RecursiveAddIntoParent(
-                                      p_playlist, p_parent, p_child_node,
-                                      ( b_flat ? i_pos : 0 ),
-                                      b_flat,
-                                      &p_new_item );
-          //If flat, take position after recursion as current position
-          if( b_flat ) i_pos = i_last_pos;
-      }
-
-      assert( p_new_item != NULL );
-      if( i == 0 ) *pp_first_leaf = p_new_item;
-  }
-  return i_pos;
+        assert( p_new_item != NULL );
+        if( i == 0 ) *pp_first_leaf = p_new_item;
+    }
+    return i_pos;
 }
 
 static int RecursiveInsertCopy (
@@ -858,8 +883,7 @@ static int RecursiveInsertCopy (
 
     if( !(p_item->i_children != -1 && b_flat) )
     {
-        input_item_t *p_new_input = input_item_Copy( VLC_OBJECT(p_playlist),
-                                                     p_input );
+        input_item_t *p_new_input = input_item_Copy( p_input );
         if( !p_new_input ) return i_pos;
 
         playlist_item_t *p_new_item = NULL;