]> git.sesse.net Git - vlc/blobdiff - src/playlist/engine.c
Use 1400 rather than 1500 as default *application-layer* MTU to be on the
[vlc] / src / playlist / engine.c
index 86a3c146233c9dce3ab907fe073210b59849a4d8..b1ef6a23db6043dcf285abec590f76c4d71f12ac 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * engine.c : Run the playlist and handle its control
  *****************************************************************************
- * Copyright (C) 1999-2004 the VideoLAN team
+ * Copyright (C) 1999-2007 the VideoLAN team
  * $Id$
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
+
 #include <vlc/vlc.h>
-#include <vlc/vout.h>
-#include <vlc/sout.h>
-#include <vlc/input.h>
-#include "vlc_playlist.h"
-#include "vlc_interaction.h"
+#include <vlc_vout.h>
+#include <vlc_sout.h>
+#include <vlc_playlist.h>
+#include <vlc_interface.h>
 #include "playlist_internal.h"
+#include "stream_output/stream_output.h" /* sout_DeleteInstance */
 
 /*****************************************************************************
  * Local prototypes
@@ -37,6 +38,8 @@ static void VariablesInit( playlist_t *p_playlist );
 static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
                            vlc_value_t oldval, vlc_value_t newval, void *a )
 {
+    (void)psz_cmd; (void)oldval; (void)newval; (void)a;
+
     ((playlist_t*)p_this)->b_reset_currently_playing = VLC_TRUE;
     playlist_Signal( ((playlist_t*)p_this) );
     return VLC_SUCCESS;
@@ -52,6 +55,7 @@ static int RandomCallback( vlc_object_t *p_this, char const *psz_cmd,
 playlist_t * playlist_Create( vlc_object_t *p_parent )
 {
     playlist_t *p_playlist;
+    vlc_bool_t b_save;
     int i_tree;
 
     /* Allocate structure */
@@ -61,6 +65,10 @@ playlist_t * playlist_Create( vlc_object_t *p_parent )
         msg_Err( p_parent, "out of memory" );
         return NULL;
     }
+
+    p_playlist->p_internal = malloc(sizeof(struct playlist_internal_t));
+    memset( p_playlist->p_internal, 0, sizeof(struct playlist_internal_t) );
+
     p_parent->p_libvlc->p_playlist = p_playlist;
 
     VariablesInit( p_playlist );
@@ -89,17 +97,30 @@ playlist_t * playlist_Create( vlc_object_t *p_parent )
 
     p_playlist->b_doing_ml = VLC_FALSE;
 
-    p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL);
-    p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL);
+    p_playlist->b_auto_preparse =
+                        var_CreateGetBool( p_playlist, "auto-preparse") ;
+
+    p_playlist->p_root_category = playlist_NodeCreate( p_playlist, NULL, NULL,
+                                                       0 );
+    p_playlist->p_root_onelevel = playlist_NodeCreate( p_playlist, NULL, NULL,
+                                                       0 );
+
+    if( !p_playlist->p_root_category || !p_playlist->p_root_onelevel )
+        return NULL;
 
     /* Create playlist and media library */
     p_playlist->p_local_category = playlist_NodeCreate( p_playlist,
-                                 _( "Playlist" ),p_playlist->p_root_category );
+                              _( "Playlist" ),p_playlist->p_root_category, 0 );
     p_playlist->p_local_onelevel =  playlist_NodeCreate( p_playlist,
-                                _( "Playlist" ), p_playlist->p_root_onelevel );
+                              _( "Playlist" ), p_playlist->p_root_onelevel, 0 );
     p_playlist->p_local_category->i_flags |= PLAYLIST_RO_FLAG;
     p_playlist->p_local_onelevel->i_flags |= PLAYLIST_RO_FLAG;
 
+    if( !p_playlist->p_local_category || !p_playlist->p_local_onelevel ||
+        !p_playlist->p_local_category->p_input ||
+        !p_playlist->p_local_onelevel->p_input )
+        return NULL;
+
     /* Link the nodes together. Todo: actually create them from the same input*/
     p_playlist->p_local_onelevel->p_input->i_id =
         p_playlist->p_local_category->p_input->i_id;
@@ -107,9 +128,13 @@ playlist_t * playlist_Create( vlc_object_t *p_parent )
     if( config_GetInt( p_playlist, "media-library") )
     {
         p_playlist->p_ml_category =   playlist_NodeCreate( p_playlist,
-                           _( "Media Library" ), p_playlist->p_root_category );
+                         _( "Media Library" ), p_playlist->p_root_category, 0 );
         p_playlist->p_ml_onelevel =  playlist_NodeCreate( p_playlist,
-                           _( "Media Library" ), p_playlist->p_root_onelevel );
+                         _( "Media Library" ), p_playlist->p_root_onelevel, 0 );
+
+        if(!p_playlist->p_ml_category || !p_playlist->p_ml_onelevel)
+            return NULL;
+
         p_playlist->p_ml_category->i_flags |= PLAYLIST_RO_FLAG;
         p_playlist->p_ml_onelevel->i_flags |= PLAYLIST_RO_FLAG;
         p_playlist->p_ml_onelevel->p_input->i_id =
@@ -130,29 +155,15 @@ playlist_t * playlist_Create( vlc_object_t *p_parent )
     p_playlist->i_sort = SORT_ID;
     p_playlist->i_order = ORDER_NORMAL;
 
-    vlc_object_attach( p_playlist, p_parent );
-
+    b_save = p_playlist->b_auto_preparse;
+    p_playlist->b_auto_preparse = VLC_FALSE;
     playlist_MLLoad( p_playlist );
+    p_playlist->b_auto_preparse = VLC_TRUE;
     return p_playlist;
 }
 
 void playlist_Destroy( playlist_t *p_playlist )
 {
-    while( p_playlist->i_sds )
-    {
-        playlist_ServicesDiscoveryRemove( p_playlist,
-                                          p_playlist->pp_sds[0]->psz_module );
-    }
-
-    playlist_MLDump( p_playlist );
-
-    vlc_thread_join( p_playlist->p_preparse );
-    vlc_thread_join( p_playlist->p_secondary_preparse );
-    vlc_thread_join( p_playlist );
-
-    vlc_object_detach( p_playlist->p_preparse );
-    vlc_object_detach( p_playlist->p_secondary_preparse );
-
     var_Destroy( p_playlist, "intf-change" );
     var_Destroy( p_playlist, "item-change" );
     var_Destroy( p_playlist, "playlist-current" );
@@ -165,56 +176,33 @@ void playlist_Destroy( playlist_t *p_playlist )
     var_Destroy( p_playlist, "loop" );
     var_Destroy( p_playlist, "activity" );
 
-    PL_LOCK;
-    /* Go through all items, and simply free everything without caring
-     * about the tree structure. Do not decref, it will be done by doing
-     * the same thing on the input items array */
-    FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
-        free( p_del->pp_children );
-        free( p_del );
-    FOREACH_END();
-    ARRAY_RESET( p_playlist->all_items );
-
-    FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
-        input_ItemClean( p_del );
-        free( p_del );
-    FOREACH_END();
-    ARRAY_RESET( p_playlist->input_items );
-
-    ARRAY_RESET( p_playlist->items );
-    ARRAY_RESET( p_playlist->current );
-
-    PL_UNLOCK;
-
-    if( p_playlist->p_stats )
-        free( p_playlist->p_stats );
-
     vlc_mutex_destroy( &p_playlist->gc_lock );
-    vlc_object_destroy( p_playlist->p_preparse );
-    vlc_object_destroy( p_playlist->p_secondary_preparse );
     vlc_object_detach( p_playlist );
+    free( p_playlist->p_internal );
     vlc_object_destroy( p_playlist );
-
 }
 
 /* Destroy remaining objects */
-static void ObjectGarbageCollector( playlist_t *p_playlist )
+static void ObjectGarbageCollector( playlist_t *p_playlist, vlc_bool_t b_force )
 {
     vlc_object_t *p_obj;
 
-    if( mdate() - p_playlist->gc_date < 1000000 )
+    if( !b_force )
     {
-        p_playlist->b_cant_sleep = VLC_TRUE;
-        return;
+        if( mdate() - p_playlist->gc_date < 1000000 )
+        {
+            p_playlist->b_cant_sleep = VLC_TRUE;
+            return;
+        }
+        else if( p_playlist->gc_date == 0 )
+            return;
     }
-    else if( p_playlist->gc_date == 0 )
-        return;
 
     vlc_mutex_lock( &p_playlist->gc_lock );
     while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_VOUT,
                                                   FIND_CHILD ) ) )
     {
-        if( p_obj->p_parent != (vlc_object_t*)p_playlist )
+        if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
         {
             vlc_object_release( p_obj );
             break;
@@ -227,11 +215,13 @@ static void ObjectGarbageCollector( playlist_t *p_playlist )
     while( ( p_obj = vlc_object_find( p_playlist, VLC_OBJECT_SOUT,
                                                   FIND_CHILD ) ) )
     {
-        if( p_obj->p_parent != (vlc_object_t*)p_playlist )
+        if( p_obj->p_parent != VLC_OBJECT(p_playlist) )
         {
             vlc_object_release( p_obj );
             break;
         }
+        msg_Dbg( p_playlist, "garbage collector destroying 1 sout" );
+        vlc_object_detach( p_obj );
         vlc_object_release( p_obj );
         sout_DeleteInstance( (sout_instance_t*)p_obj );
     }
@@ -281,13 +271,6 @@ check_input:
             /* Destroy input */
             input_DestroyThread( p_input );
 
-            /* Unlink current input
-             * (_after_ input_DestroyThread for vout garbage collector) */
-            vlc_object_detach( p_input );
-
-            /* Destroy object */
-            vlc_object_destroy( p_input );
-
             PL_LOCK;
 
             p_playlist->gc_date = mdate();
@@ -313,7 +296,9 @@ check_input:
         else if( p_playlist->p_input->b_die )
         {
             PL_DEBUG( "dying input" );
+            PL_UNLOCK;
             msleep( 25000 ); // 25 ms
+            PL_LOCK;
             goto check_input;
         }
         /* This input has finished, ask it to die ! */
@@ -328,7 +313,7 @@ check_input:
         else if( p_playlist->p_input->i_state != INIT_S )
         {
             PL_UNLOCK;
-            ObjectGarbageCollector( p_playlist );
+            ObjectGarbageCollector( p_playlist, VLC_FALSE );
             PL_LOCK;
         }
     }
@@ -354,16 +339,20 @@ check_input:
                 if( b_playexit == VLC_TRUE )
                 {
                     msg_Info( p_playlist, "end of playlist, exiting" );
-                    p_playlist->p_libvlc->b_die = VLC_TRUE;
+                    vlc_object_kill( p_playlist->p_libvlc );
                 }
                 p_playlist->status.i_status = PLAYLIST_STOPPED;
                 PL_UNLOCK
+
+                ObjectGarbageCollector( p_playlist, VLC_TRUE );
                 return;
              }
              playlist_PlayItem( p_playlist, p_item );
          }
          else
          {
+            const vlc_bool_t b_gc_forced = p_playlist->status.i_status != PLAYLIST_STOPPED;
+
             p_playlist->status.i_status = PLAYLIST_STOPPED;
             if( p_playlist->status.p_item &&
                 p_playlist->status.p_item->i_flags & PLAYLIST_REMOVE_FLAG )
@@ -375,7 +364,7 @@ check_input:
 
             /* Collect garbage */
             PL_UNLOCK;
-            ObjectGarbageCollector( p_playlist );
+            ObjectGarbageCollector( p_playlist, b_gc_forced );
             PL_LOCK;
         }
     }
@@ -409,12 +398,6 @@ void playlist_LastLoop( playlist_t *p_playlist )
 
             /* Destroy input */
             input_DestroyThread( p_input );
-            /* Unlink current input (_after_ input_DestroyThread for vout
-             * garbage collector)*/
-            vlc_object_detach( p_input );
-
-            /* Destroy object */
-            vlc_object_destroy( p_input );
             continue;
         }
         else if( p_playlist->p_input->b_die )
@@ -442,6 +425,7 @@ void playlist_LastLoop( playlist_t *p_playlist )
     while( ( p_obj = vlc_object_find( p_playlist,
                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
     {
+        vlc_object_detach( p_obj );
         vlc_object_release( p_obj );
         sout_DeleteInstance( (sout_instance_t*)p_obj );
     }
@@ -454,6 +438,35 @@ void playlist_LastLoop( playlist_t *p_playlist )
         vlc_object_release( p_obj );
         vout_Destroy( (vout_thread_t *)p_obj );
     }
+
+    while( p_playlist->p_internal->i_asds )
+    {
+        playlist_ServicesDiscoveryRemove( p_playlist,
+                                          p_playlist->p_internal->pp_asds[0]->p_sd->psz_module );
+    }
+
+    playlist_MLDump( p_playlist );
+
+    PL_LOCK;
+    /* Go through all items, and simply free everything without caring
+     * about the tree structure. Do not decref, it will be done by doing
+     * the same thing on the input items array */
+    FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
+        free( p_del->pp_children );
+        free( p_del );
+    FOREACH_END();
+    ARRAY_RESET( p_playlist->all_items );
+
+    FOREACH_ARRAY( input_item_t *p_del, p_playlist->input_items )
+        input_ItemClean( p_del );
+        free( p_del );
+    FOREACH_END();
+    ARRAY_RESET( p_playlist->input_items );
+
+    ARRAY_RESET( p_playlist->items );
+    ARRAY_RESET( p_playlist->current );
+
+    PL_UNLOCK;
 }
 
 /** Main loop for preparser queue */
@@ -497,43 +510,66 @@ void playlist_PreparseLoop( playlist_preparse_t *p_obj )
                 b_preparsed = VLC_TRUE;
                 stats_TimerStart( p_playlist, "Preparse run",
                                   STATS_TIMER_PREPARSE );
-                PL_UNLOCK;
-                input_Preparse( p_playlist, p_current );
-                PL_LOCK;
+                /* Do not preparse if it is already done (like by playing it) */
+                if( !input_item_IsPreparsed( p_current ) )
+                {
+                    PL_UNLOCK;
+                    input_Preparse( p_playlist, p_current );
+                    PL_LOCK;
+                }
                 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
             }
             PL_UNLOCK;
             if( b_preparsed )
             {
-                p_current->p_meta->i_status |= ITEM_PREPARSED;
+                input_item_SetPreparsed( p_current, VLC_TRUE );
                 var_SetInteger( p_playlist, "item-change", p_current->i_id );
             }
             PL_LOCK;
 
             /* If we haven't retrieved enough meta, add to secondary queue
-             * which will run the "meta fetchers"
-             * TODO:
-             *  don't do this for things we won't get meta for, like
-             *  videos
+             * which will run the "meta fetchers".
+             * This only checks for meta, not for art
+             * \todo don't do this for things we won't get meta for, like vids
              */
-            if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o,
-                                      VLC_TRUE ) )
+            char *psz_arturl = input_item_GetArtURL( p_current );
+            char *psz_name = input_item_GetName( p_current );
+            if( !input_MetaSatisfied( p_playlist, p_current, &i_m, &i_o ) )
             {
                 preparse_item_t p;
+                PL_DEBUG("need to fetch meta for %s", p_current->psz_name );
                 p.p_item = p_current;
                 p.b_fetch_art = VLC_FALSE;
-                vlc_mutex_lock( &p_playlist->p_secondary_preparse->object_lock);
-                INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
-                             p_playlist->p_secondary_preparse->i_waiting,
-                             p_playlist->p_secondary_preparse->i_waiting,
-                             p );
-                vlc_mutex_unlock(
-                            &p_playlist->p_secondary_preparse->object_lock);
-                vlc_cond_signal(
-                            &p_playlist->p_secondary_preparse->object_wait );
+                vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
+                INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
+                             p_playlist->p_fetcher->i_waiting,
+                             p_playlist->p_fetcher->i_waiting, p);
+                vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
+                vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
+            }
+            /* We already have all needed meta, but we need art right now */
+            else if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL &&
+                     EMPTY_STR( psz_arturl ) )
+            {
+                preparse_item_t p;
+                PL_DEBUG("meta ok for %s, need to fetch art", psz_name );
+                p.p_item = p_current;
+                p.b_fetch_art = VLC_TRUE;
+                vlc_mutex_lock( &p_playlist->p_fetcher->object_lock );
+                INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
+                             p_playlist->p_fetcher->i_waiting,
+                             p_playlist->p_fetcher->i_waiting, p);
+                vlc_mutex_unlock( &p_playlist->p_fetcher->object_lock );
+                vlc_cond_signal( &p_playlist->p_fetcher->object_wait );
             }
             else
+            {
+                PL_DEBUG( "no fetch required for %s (art currently %s)",
+                          psz_name, psz_arturl );
                 vlc_gc_decref( p_current );
+            }
+            free( psz_name );
+            free( psz_arturl );
             PL_UNLOCK;
         }
         else
@@ -549,7 +585,7 @@ void playlist_PreparseLoop( playlist_preparse_t *p_obj )
 }
 
 /** Main loop for secondary preparser queue */
-void playlist_SecondaryPreparseLoop( playlist_secondary_preparse_t *p_obj )
+void playlist_FetcherLoop( playlist_fetcher_t *p_obj )
 {
     playlist_t *p_playlist = (playlist_t *)p_obj->p_parent;
     vlc_bool_t b_fetch_art;
@@ -578,27 +614,66 @@ void playlist_SecondaryPreparseLoop( playlist_secondary_preparse_t *p_obj )
             if( !b_fetch_art )
             {
                 input_MetaFetch( p_playlist, p_item );
-                p_item->p_meta->i_status |= ITEM_META_FETCHED;
                 var_SetInteger( p_playlist, "item-change", p_item->i_id );
                 /*  Fetch right now */
-                if( var_GetInteger( p_playlist, "album-art" ) == ALBUM_ART_ALL )
+                if( p_playlist->p_fetcher->i_art_policy == ALBUM_ART_ALL )
                 {
                     vlc_mutex_lock( &p_obj->object_lock );
                     preparse_item_t p;
                     p.p_item = p_item;
                     p.b_fetch_art = VLC_TRUE;
-                    INSERT_ELEM( p_playlist->p_secondary_preparse->p_waiting,
-                                 p_playlist->p_secondary_preparse->i_waiting,
+                    INSERT_ELEM( p_playlist->p_fetcher->p_waiting,
+                                 p_playlist->p_fetcher->i_waiting,
                                  0, p );
+                    PL_DEBUG("meta fetched for %s, get art", p_item->psz_name);
                     vlc_mutex_unlock( &p_obj->object_lock );
+                    continue;
                 }
                 else
                     vlc_gc_decref( p_item );
             }
             else
             {
-                input_ArtFetch( p_playlist, p_item );
-                p_item->p_meta->i_status |= ITEM_ART_FETCHED;
+                int i_ret;
+
+                /* Check if it is not yet preparsed and if so wait for it (at most 0.5s)
+                 * (This can happen if we fetch art on play)
+                 * FIXME this doesn't work if we need to fetch meta before art ... */
+                for( i_ret = 0; i_ret < 10 && !input_item_IsPreparsed( p_item ); i_ret++ )
+                {
+                    vlc_bool_t b_break;
+                    PL_LOCK;
+                    b_break = ( !p_playlist->p_input || input_GetItem(p_playlist->p_input) != p_item  ||
+                                p_playlist->p_input->b_die || p_playlist->p_input->b_eof || p_playlist->p_input->b_error );
+                    PL_UNLOCK;
+                    if( b_break )
+                        break;
+                    msleep( 50000 );
+                }
+                
+                i_ret = input_ArtFind( p_playlist, p_item );
+                if( i_ret == 1 )
+                {
+                    PL_DEBUG("downloading art for %s", p_item->psz_name );
+                    if( input_DownloadAndCacheArt( p_playlist, p_item ) )
+                        input_item_SetArtNotFound( p_item, VLC_TRUE );
+                    else {
+                        input_item_SetArtFetched( p_item, VLC_TRUE );
+                        var_SetInteger( p_playlist, "item-change",
+                                        p_item->i_id );
+                    }
+                }
+                else if( i_ret == 0 ) /* Was in cache */
+                {
+                    PL_DEBUG("found art for %s in cache", p_item->psz_name );
+                    input_item_SetArtFetched( p_item, VLC_TRUE );
+                    var_SetInteger( p_playlist, "item-change", p_item->i_id );
+                }
+                else
+                {
+                    PL_DEBUG("art not found for %s", p_item->psz_name );
+                    input_item_SetArtNotFound( p_item, VLC_TRUE );
+                }
                 vlc_gc_decref( p_item );
            }
         }