]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
libvlc: use vlc_common.h (libvlccore) instead of vlc/vlc.h
[vlc] / src / playlist / thread.c
1 /*****************************************************************************
2  * thread.c : Playlist management functions
3  *****************************************************************************
4  * Copyright © 1999-2008 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_es.h>
30 #include <vlc_input.h>
31 #include <vlc_interface.h>
32 #include <vlc_playlist.h>
33 #include "playlist_internal.h"
34
35 /*****************************************************************************
36  * Local prototypes
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 * );
43
44 /*****************************************************************************
45  * Main functions for the global thread
46  *****************************************************************************/
47
48 /**
49  * Create the main playlist thread
50  * Additionally to the playlist, this thread controls :
51  *    - Statistics
52  *    - VLM
53  * \param p_parent
54  * \return an object with a started thread
55  */
56 void __playlist_ThreadCreate( vlc_object_t *p_parent )
57 {
58     playlist_t *p_playlist = playlist_Create( p_parent );
59     if( !p_playlist ) return;
60
61     // Preparse
62     p_playlist->p_preparse = vlc_object_create( p_playlist,
63                                   sizeof( playlist_preparse_t ) );
64     if( !p_playlist->p_preparse )
65     {
66         msg_Err( p_playlist, "unable to create preparser" );
67         vlc_object_release( p_playlist );
68         return;
69     }
70     p_playlist->p_preparse->psz_object_name = strdup( "preparser" );
71     p_playlist->p_preparse->i_waiting = 0;
72     p_playlist->p_preparse->pp_waiting = NULL;
73
74     vlc_object_set_destructor( p_playlist->p_preparse, PreparseDestructor );
75
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, true ) )
79     {
80         msg_Err( p_playlist, "cannot spawn preparse thread" );
81         vlc_object_release( p_playlist->p_preparse );
82         return;
83     }
84
85     // Secondary Preparse
86     p_playlist->p_fetcher = vlc_object_create( p_playlist,
87                               sizeof( playlist_fetcher_t ) );
88     if( !p_playlist->p_fetcher )
89     {
90         msg_Err( p_playlist, "unable to create secondary preparser" );
91         vlc_object_release( p_playlist );
92         return;
93     }
94     p_playlist->p_fetcher->psz_object_name = strdup( "fetcher" );
95     p_playlist->p_fetcher->i_waiting = 0;
96     p_playlist->p_fetcher->pp_waiting = NULL;
97     p_playlist->p_fetcher->i_art_policy = var_CreateGetInteger( p_playlist,
98                                                                 "album-art" );
99
100     vlc_object_set_destructor( p_playlist->p_fetcher, FetcherDestructor );
101
102     vlc_object_attach( p_playlist->p_fetcher, p_playlist );
103     if( vlc_thread_create( p_playlist->p_fetcher,
104                            "fetcher",
105                            RunFetcher,
106                            VLC_THREAD_PRIORITY_LOW, true ) )
107     {
108         msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
109         vlc_object_release( p_playlist->p_fetcher );
110         return;
111     }
112
113     // Start the thread
114     if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
115                            VLC_THREAD_PRIORITY_LOW, true ) )
116     {
117         msg_Err( p_playlist, "cannot spawn playlist thread" );
118         vlc_object_release( p_playlist );
119         return;
120     }
121
122     /* The object has been initialized, now attach it */
123     vlc_object_attach( p_playlist, p_parent );
124
125     return;
126 }
127
128 /**
129  * Run the main control thread itself
130  */
131 static void RunControlThread ( playlist_t *p_playlist )
132 {
133     /* Tell above that we're ready */
134     vlc_thread_ready( p_playlist );
135
136     vlc_object_lock( p_playlist );
137     while( vlc_object_alive( p_playlist ) )
138     {
139         PL_UNLOCK;
140         playlist_MainLoop( p_playlist );
141         PL_LOCK;
142
143         /* The playlist lock has been unlocked, so we can't tell if
144          * someone has killed us in the meantime. Check now. */
145         if( !vlc_object_alive( p_playlist ) )
146             break;
147
148         if( p_playlist->b_cant_sleep )
149         {
150             /* 100 ms is an acceptable delay for playlist operations */
151             PL_UNLOCK;
152             msleep( INTF_IDLE_SLEEP*2 );
153             PL_LOCK;
154         }
155         else
156         {
157             vlc_object_wait( p_playlist );
158         }
159     }
160     vlc_object_unlock( p_playlist );
161
162     playlist_LastLoop( p_playlist );
163 }
164
165 /*****************************************************************************
166  * Preparse-specific functions
167  *****************************************************************************/
168 static void RunPreparse ( playlist_preparse_t *p_obj )
169 {
170     /* Tell above that we're ready */
171     vlc_thread_ready( p_obj );
172     playlist_PreparseLoop( p_obj );
173 }
174
175 static void RunFetcher( playlist_fetcher_t *p_obj )
176 {
177     /* Tell above that we're ready */
178     vlc_thread_ready( p_obj );
179     playlist_FetcherLoop( p_obj );
180 }
181
182 static void PreparseDestructor( vlc_object_t * p_this )
183 {
184     playlist_preparse_t * p_preparse = (playlist_preparse_t *)p_this;
185     free( p_preparse->pp_waiting );
186 }
187
188 static void FetcherDestructor( vlc_object_t * p_this )
189 {
190     playlist_fetcher_t * p_fetcher = (playlist_fetcher_t *)p_this;
191     free( p_fetcher->pp_waiting );
192 }