1 /*****************************************************************************
2 * thread.c : Playlist management functions
3 *****************************************************************************
4 * Copyright © 1999-2008 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
30 #include <vlc_input.h>
31 #include <vlc_interface.h>
32 #include <vlc_playlist.h>
33 #include "playlist_internal.h"
35 /*****************************************************************************
37 *****************************************************************************/
38 static void RunControlThread ( playlist_t * );
39 static void RunPreparse( playlist_preparse_t * );
40 static void RunFetcher( playlist_fetcher_t * );
41 static void PreparseDestructor( vlc_object_t * );
42 static void FetcherDestructor( vlc_object_t * );
44 /*****************************************************************************
45 * Main functions for the global thread
46 *****************************************************************************/
49 * Create the main playlist thread
50 * Additionally to the playlist, this thread controls :
54 * \return an object with a started thread
56 void __playlist_ThreadCreate( vlc_object_t *p_parent )
58 playlist_t *p_playlist = playlist_Create( p_parent );
59 if( !p_playlist ) return;
62 p_playlist->p_preparse = vlc_object_create( p_playlist,
63 sizeof( playlist_preparse_t ) );
64 p_playlist->p_preparse->psz_object_name = "preparser";
65 if( !p_playlist->p_preparse )
67 msg_Err( p_playlist, "unable to create preparser" );
68 vlc_object_release( p_playlist );
71 p_playlist->p_preparse->i_waiting = 0;
72 p_playlist->p_preparse->pp_waiting = NULL;
74 vlc_object_set_destructor( p_playlist->p_preparse, PreparseDestructor );
76 vlc_object_attach( p_playlist->p_preparse, p_playlist );
77 if( vlc_thread_create( p_playlist->p_preparse, "preparser",
78 RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
80 msg_Err( p_playlist, "cannot spawn preparse thread" );
81 vlc_object_release( p_playlist->p_preparse );
86 p_playlist->p_fetcher = vlc_object_create( p_playlist,
87 sizeof( playlist_fetcher_t ) );
88 p_playlist->p_fetcher->psz_object_name = "fetcher";
89 if( !p_playlist->p_fetcher )
91 msg_Err( p_playlist, "unable to create secondary preparser" );
92 vlc_object_release( p_playlist );
95 p_playlist->p_fetcher->i_waiting = 0;
96 p_playlist->p_fetcher->p_waiting = NULL;
97 p_playlist->p_fetcher->b_fetch_meta = var_CreateGetInteger( p_playlist,
99 p_playlist->p_fetcher->i_art_policy = var_CreateGetInteger( p_playlist,
102 vlc_object_set_destructor( p_playlist->p_fetcher, FetcherDestructor );
104 vlc_object_attach( p_playlist->p_fetcher, p_playlist );
105 if( vlc_thread_create( p_playlist->p_fetcher,
108 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
110 msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
111 vlc_object_release( p_playlist->p_fetcher );
116 if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
117 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
119 msg_Err( p_playlist, "cannot spawn playlist thread" );
120 vlc_object_release( p_playlist );
124 /* The object has been initialized, now attach it */
125 vlc_object_attach( p_playlist, p_parent );
131 * Destroy the playlist global thread.
133 * Deinits all things controlled by the playlist global thread
134 * \param p_playlist the playlist thread to destroy
135 * \return VLC_SUCCESS or an error
137 int playlist_ThreadDestroy( playlist_t * p_playlist )
139 playlist_Destroy( p_playlist );
144 * Run the main control thread itself
146 static void RunControlThread ( playlist_t *p_playlist )
148 /* Tell above that we're ready */
149 vlc_thread_ready( p_playlist );
151 while( !p_playlist->b_die )
153 playlist_MainLoop( p_playlist );
154 if( p_playlist->b_cant_sleep )
156 /* 100 ms is an acceptable delay for playlist operations */
157 msleep( INTF_IDLE_SLEEP*2 );
162 vlc_bool_t b_die = vlc_object_wait( p_playlist );
168 playlist_LastLoop( p_playlist );
171 /*****************************************************************************
172 * Preparse-specific functions
173 *****************************************************************************/
174 static void RunPreparse ( playlist_preparse_t *p_obj )
176 /* Tell above that we're ready */
177 vlc_thread_ready( p_obj );
178 playlist_PreparseLoop( p_obj );
181 static void RunFetcher( playlist_fetcher_t *p_obj )
183 /* Tell above that we're ready */
184 vlc_thread_ready( p_obj );
185 playlist_FetcherLoop( p_obj );
188 static void PreparseDestructor( vlc_object_t * p_this )
190 playlist_preparse_t * p_preparse = (playlist_preparse_t *)p_this;
191 free( p_preparse->pp_waiting );
194 static void FetcherDestructor( vlc_object_t * p_this )
196 playlist_fetcher_t * p_fetcher = (playlist_fetcher_t *)p_this;
197 free( p_fetcher->p_waiting );