]> git.sesse.net Git - vlc/blobdiff - src/playlist/fetcher.c
macosx: fix recursive inclusion of the compatibility header
[vlc] / src / playlist / fetcher.c
index f227a412d964ae8681299ba5959cf5a4edbc8acf..b6a9ee9e88de2ae7808a22efe777a9f42d1eafb0 100644 (file)
@@ -1,51 +1,51 @@
 /*****************************************************************************
  * fetcher.c: Art fetcher thread.
  *****************************************************************************
- * Copyright © 1999-2009 the VideoLAN team
+ * Copyright © 1999-2009 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"
 #endif
 
+#include <limits.h>
 #include <assert.h>
 
 #include <vlc_common.h>
-#include <vlc_playlist.h>
 #include <vlc_stream.h>
-#include <limits.h>
 #include <vlc_art_finder.h>
 #include <vlc_memory.h>
 #include <vlc_demux.h>
+#include <vlc_modules.h>
 
+#include "libvlc.h"
 #include "art.h"
 #include "fetcher.h"
-#include "playlist_internal.h"
+#include "input/input_interface.h"
 
 /*****************************************************************************
  * Structures/definitions
  *****************************************************************************/
 struct playlist_fetcher_t
 {
-    playlist_t      *p_playlist;
-
+    vlc_object_t   *object;
     vlc_mutex_t     lock;
     vlc_cond_t      wait;
     bool            b_live;
@@ -62,19 +62,19 @@ static void *Thread( void * );
 /*****************************************************************************
  * Public functions
  *****************************************************************************/
-playlist_fetcher_t *playlist_fetcher_New( playlist_t *p_playlist )
+playlist_fetcher_t *playlist_fetcher_New( vlc_object_t *parent )
 {
     playlist_fetcher_t *p_fetcher = malloc( sizeof(*p_fetcher) );
     if( !p_fetcher )
         return NULL;
 
-    p_fetcher->p_playlist = p_playlist;
+    p_fetcher->object = parent;
     vlc_mutex_init( &p_fetcher->lock );
     vlc_cond_init( &p_fetcher->wait );
     p_fetcher->b_live = false;
     p_fetcher->i_waiting = 0;
     p_fetcher->pp_waiting = NULL;
-    p_fetcher->i_art_policy = var_GetInteger( p_playlist, "album-art" );
+    p_fetcher->i_art_policy = var_GetInteger( parent, "album-art" );
     ARRAY_INIT( p_fetcher->albums );
 
     return p_fetcher;
@@ -90,16 +90,12 @@ void playlist_fetcher_Push( playlist_fetcher_t *p_fetcher,
                  p_fetcher->i_waiting, p_item );
     if( !p_fetcher->b_live )
     {
-        vlc_thread_t th;
-
-        if( vlc_clone( &th, Thread, p_fetcher, VLC_THREAD_PRIORITY_LOW ) )
-            msg_Err( p_fetcher->p_playlist,
+        if( vlc_clone_detach( NULL, Thread, p_fetcher,
+                              VLC_THREAD_PRIORITY_LOW ) )
+            msg_Err( p_fetcher->object,
                      "cannot spawn secondary preparse thread" );
         else
-        {
-            vlc_detach( th );
             p_fetcher->b_live = true;
-        }
     }
     vlc_mutex_unlock( &p_fetcher->lock );
 }
@@ -156,7 +152,7 @@ static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
             if( !strcmp( album.psz_artist, psz_artist ) &&
                 !strcmp( album.psz_album, psz_album ) )
             {
-                msg_Dbg( p_fetcher->p_playlist,
+                msg_Dbg( p_fetcher->object,
                          " %s - %s has already been searched",
                          psz_artist, psz_album );
                 /* TODO-fenrir if we cache art filename too, we can go faster */
@@ -180,12 +176,15 @@ static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
     free( psz_artist );
     free( psz_album );
 
-    playlist_FindArtInCache( p_item );
+    if ( playlist_FindArtInCacheUsingItemUID( p_item ) != VLC_SUCCESS )
+        playlist_FindArtInCache( p_item );
+    else
+        msg_Dbg( p_fetcher->object, "successfully retrieved arturl by uid" );
 
     char *psz_arturl = input_item_GetArtURL( p_item );
     if( psz_arturl )
     {
-        /* We already have an URL */
+        /* We already have a URL */
         if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
         {
             free( psz_arturl );
@@ -203,8 +202,8 @@ static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
     psz_artist = input_item_GetArtist( p_item );
     if( psz_album && psz_artist )
     {
-        msg_Dbg( p_fetcher->p_playlist, "searching art for %s - %s",
-             psz_artist, psz_album );
+        msg_Dbg( p_fetcher->object, "searching art for %s - %s",
+                 psz_artist, psz_album );
     }
     else
     {
@@ -212,29 +211,31 @@ static int FindArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
         if( !psz_title )
             psz_title = input_item_GetName( p_item );
 
-        msg_Dbg( p_fetcher->p_playlist, "searching art for %s", psz_title );
+        msg_Dbg( p_fetcher->object, "searching art for %s", psz_title );
         free( psz_title );
     }
 
     /* Fetch the art url */
     i_ret = VLC_EGENERIC;
 
-    vlc_object_t *p_parent = VLC_OBJECT(p_fetcher->p_playlist);
+    vlc_object_t *p_parent = p_fetcher->object;
     art_finder_t *p_finder =
-        vlc_custom_create( p_parent, sizeof( *p_finder ), VLC_OBJECT_GENERIC,
-                           "art finder" );
+        vlc_custom_create( p_parent, sizeof( *p_finder ), "art finder" );
     if( p_finder != NULL)
     {
         module_t *p_module;
 
-        vlc_object_attach( p_finder, p_parent );
         p_finder->p_item = p_item;
 
         p_module = module_need( p_finder, "art finder", NULL, false );
         if( p_module )
         {
             module_unneed( p_finder, p_module );
-            i_ret = 1;
+            /* Try immediately if found in cache by download URL */
+            if( !playlist_FindArtInCache( p_item ) )
+                i_ret = 0;
+            else
+                i_ret = 1;
         }
         vlc_object_release( p_finder );
     }
@@ -269,7 +270,7 @@ static int DownloadArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
 
     if( !strncmp( psz_arturl , "file://", 7 ) )
     {
-        msg_Dbg( p_fetcher->p_playlist,
+        msg_Dbg( p_fetcher->object,
                  "Album art is local file, no need to cache" );
         free( psz_arturl );
         return VLC_SUCCESS;
@@ -277,11 +278,11 @@ static int DownloadArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
 
     if( !strncmp( psz_arturl , "APIC", 4 ) )
     {
-        msg_Warn( p_fetcher->p_playlist, "APIC fetch not supported yet" );
+        msg_Warn( p_fetcher->object, "APIC fetch not supported yet" );
         goto error;
     }
 
-    stream_t *p_stream = stream_UrlNew( p_fetcher->p_playlist, psz_arturl );
+    stream_t *p_stream = stream_UrlNew( p_fetcher->object, psz_arturl );
     if( !p_stream )
         goto error;
 
@@ -312,7 +313,8 @@ static int DownloadArt( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
         if( psz_type && strlen( psz_type ) > 5 )
             psz_type = NULL; /* remove extension if it's > to 4 characters */
 
-        playlist_SaveArt( p_fetcher->p_playlist, p_item, p_data, i_data, psz_type );
+        playlist_SaveArt( p_fetcher->object, p_item,
+                          p_data, i_data, psz_type );
     }
 
     free( p_data );
@@ -332,9 +334,8 @@ error:
  */
 static void FetchMeta( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
 {
-    demux_meta_t *p_demux_meta = vlc_custom_create(p_fetcher->p_playlist,
-                                       sizeof(*p_demux_meta),
-                                       VLC_OBJECT_GENERIC, "demux meta" );
+    demux_meta_t *p_demux_meta = vlc_custom_create(p_fetcher->object,
+                                         sizeof(*p_demux_meta), "demux meta" );
     if( !p_demux_meta )
         return;
 
@@ -347,66 +348,10 @@ static void FetchMeta( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
     vlc_object_release( p_demux_meta );
 }
 
-static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
-                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
-{
-    VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
-    vlc_cond_t *p_cond = p_data;
-
-    if( newval.i_int == INPUT_EVENT_ITEM_META ||
-        newval.i_int == INPUT_EVENT_DEAD )
-        vlc_cond_signal( p_cond );
-
-    return VLC_SUCCESS;
-}
-
-
-/* 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...
- */
-static void WaitPreparsed( playlist_fetcher_t *p_fetcher, input_item_t *p_item )
-{
-    if( input_item_IsPreparsed( p_item ) )
-        return;
-
-    input_thread_t *p_input = playlist_CurrentInput( p_fetcher->p_playlist );
-    if( !p_input )
-        return;
-
-    if( input_GetItem( p_input ) != p_item )
-        goto exit;
-
-    vlc_cond_t cond;
-    vlc_cond_init( &cond );
-    var_AddCallback( p_input, "intf-event", InputEvent, &cond );
-
-    const mtime_t i_deadline = mdate() + 500*1000;
-    bool b_timeout = false;
-
-    while( !p_input->b_eof && !p_input->b_error
-        && !input_item_IsPreparsed( p_item ) && !b_timeout )
-    {
-        /* A bit weird, but input_item_IsPreparsed holds the protected value */
-        /* FIXME: locking looks wrong here */
-        vlc_mutex_lock( &p_fetcher->lock );
-        if( vlc_cond_timedwait( &cond, &p_fetcher->lock, i_deadline ) )
-            b_timeout = true;
-        vlc_mutex_unlock( &p_fetcher->lock );
-    }
-
-    var_DelCallback( p_input, "intf-event", InputEvent, &cond );
-    vlc_cond_destroy( &cond );
-
-exit:
-    vlc_object_release( p_input );
-}
-
 static void *Thread( void *p_data )
 {
     playlist_fetcher_t *p_fetcher = p_data;
-    playlist_t *p_playlist = p_fetcher->p_playlist;
+    vlc_object_t *obj = p_fetcher->object;
 
     for( ;; )
     {
@@ -428,11 +373,6 @@ static void *Thread( void *p_data )
         if( !p_item )
             break;
 
-        /* */
-
-        /* Wait that the input item is preparsed if it is being played */
-        WaitPreparsed( p_fetcher, p_item );
-
         /* Triggers "meta fetcher", eventually fetch meta on the network.
          * They are identical to "meta reader" expect that may actually
          * takes time. That's why they are running here.
@@ -448,13 +388,13 @@ static void *Thread( void *p_data )
         char *psz_name = input_item_GetName( p_item );
         if( !i_ret ) /* Art is now in cache */
         {
-            PL_DEBUG( "found art for %s in cache", psz_name );
+            msg_Dbg( obj, "found art for %s in cache", psz_name );
             input_item_SetArtFetched( p_item, true );
-            var_SetAddress( p_playlist, "item-change", p_item );
+            var_SetAddress( obj, "item-change", p_item );
         }
         else
         {
-            PL_DEBUG( "art not found for %s", psz_name );
+            msg_Dbg( obj, "art not found for %s", psz_name );
             input_item_SetArtNotFound( p_item, true );
         }
         free( psz_name );