]> git.sesse.net Git - vlc/blob - src/playlist/preparser.c
Clean up preparser/fetcher code.
[vlc] / src / playlist / preparser.c
1 /*****************************************************************************
2  * preparse.c: Preparser thread.
3  *****************************************************************************
4  * Copyright © 1999-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_playlist.h>
30
31 #include "art.h"
32 #include "fetcher.h"
33 #include "preparser.h"
34 #include "../input/input_interface.h"
35
36 static void *Thread( void * );
37
38 playlist_preparser_t *playlist_preparser_New( playlist_t *p_playlist, playlist_fetcher_t *p_fetcher )
39 {
40     playlist_preparser_t *p_preparser = malloc( sizeof(*p_preparser) );
41     if( !p_preparser )
42         return NULL;
43
44     p_preparser->p_playlist = p_playlist;
45     p_preparser->p_fetcher = p_fetcher;
46     vlc_mutex_init( &p_preparser->lock );
47     vlc_cond_init( &p_preparser->wait );
48     p_preparser->i_waiting = 0;
49     p_preparser->pp_waiting = NULL;
50
51     if( vlc_clone( &p_preparser->thread, Thread, p_preparser,
52                    VLC_THREAD_PRIORITY_LOW ) )
53     {
54         msg_Err( p_playlist, "cannot spawn preparse thread" );
55         free( p_preparser );
56         return NULL;
57     }
58     return p_preparser;
59 }
60
61 void playlist_preparser_Push( playlist_preparser_t *p_preparser, input_item_t *p_item )
62 {
63     vlc_gc_incref( p_item );
64
65     vlc_mutex_lock( &p_preparser->lock );
66     INSERT_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting,
67                  p_preparser->i_waiting, p_item );
68     vlc_cond_signal( &p_preparser->wait );
69     vlc_mutex_unlock( &p_preparser->lock );
70 }
71
72 void playlist_preparser_Delete( playlist_preparser_t *p_preparser )
73 {
74     /* Destroy the item preparser */
75     vlc_cancel( p_preparser->thread );
76     vlc_join( p_preparser->thread, NULL );
77
78     while( p_preparser->i_waiting > 0 )
79     {   /* Any left-over unparsed item? */
80         vlc_gc_decref( p_preparser->pp_waiting[0] );
81         REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
82     }
83     vlc_cond_destroy( &p_preparser->wait );
84     vlc_mutex_destroy( &p_preparser->lock );
85 }
86
87 static void *Thread( void *data )
88 {
89     playlist_preparser_t *p_preparser = data;
90     playlist_t *p_playlist = p_preparser->p_playlist;
91     playlist_fetcher_t *p_fetcher = p_preparser->p_fetcher;
92
93     for( ;; )
94     {
95         input_item_t *p_current;
96
97         /* */
98         vlc_mutex_lock( &p_preparser->lock );
99         mutex_cleanup_push( &p_preparser->lock );
100
101         while( p_preparser->i_waiting == 0 )
102             vlc_cond_wait( &p_preparser->wait, &p_preparser->lock );
103
104         p_current = p_preparser->pp_waiting[0];
105         REMOVE_ELEM( p_preparser->pp_waiting, p_preparser->i_waiting, 0 );
106         vlc_cleanup_run( );
107
108         if( !p_current )
109             continue;
110
111         int canc = vlc_savecancel ();
112         {
113             PL_LOCK;
114             if( p_current->i_type == ITEM_TYPE_FILE )
115             {
116                 stats_TimerStart( p_playlist, "Preparse run",
117                                   STATS_TIMER_PREPARSE );
118                 /* Do not preparse if it is already done (like by playing it) */
119                 if( !input_item_IsPreparsed( p_current ) )
120                 {
121                     PL_UNLOCK;
122                     input_Preparse( p_playlist, p_current );
123                     PL_LOCK;
124                 }
125                 stats_TimerStop( p_playlist, STATS_TIMER_PREPARSE );
126                 PL_UNLOCK;
127                 input_item_SetPreparsed( p_current, true );
128                 var_SetInteger( p_playlist, "item-change", p_current->i_id );
129                 PL_LOCK;
130             }
131             /* If we haven't retrieved enough meta, add to secondary queue
132              * which will run the "meta fetchers".
133              * This only checks for meta, not for art
134              * \todo don't do this for things we won't get meta for, like vids
135              */
136             char *psz_arturl = input_item_GetArtURL( p_current );
137             char *psz_name = input_item_GetName( p_current );
138             if( p_fetcher && p_fetcher->i_art_policy == ALBUM_ART_ALL &&
139                 ( !psz_arturl || strncmp( psz_arturl, "file://", 7 ) ) )
140             {
141                 msg_Dbg( p_playlist, "meta ok for %s, need to fetch art", psz_name );
142                 playlist_fetcher_Push( p_fetcher, p_current );
143             }
144             else
145             {
146                 msg_Dbg( p_playlist, "no fetch required for %s (art currently %s)",
147                          psz_name, psz_arturl );
148             }
149             vlc_gc_decref( p_current );
150             free( psz_name );
151             free( psz_arturl );
152             PL_UNLOCK;
153         }
154         vlc_restorecancel( canc );
155
156         int i_activity = var_GetInteger( p_playlist, "activity" );
157         if( i_activity < 0 ) i_activity = 0;
158         /* Sleep at least 1ms */
159         msleep( (i_activity+1) * 1000 );
160     }
161     return NULL;
162 }
163