]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
Split creation/destruction and activation/deactivation of playlist.
[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   ( vlc_object_t * );
39
40 /*****************************************************************************
41  * Main functions for the global thread
42  *****************************************************************************/
43
44 /**
45  * Create the main playlist threads.
46  * Additionally to the playlist, this thread controls :
47  *    - Statistics
48  *    - VLM
49  * \param p_parent
50  * \return an object with a started thread
51  */
52 void playlist_Activate( playlist_t *p_playlist )
53 {
54     /* */
55     playlist_private_t *p_sys = pl_priv(p_playlist);
56
57     /* Fetcher */
58     p_sys->p_fetcher = playlist_fetcher_New( p_playlist );
59     if( !p_sys->p_fetcher )
60         msg_Err( p_playlist, "cannot create playlist fetcher" );
61
62     /* Preparse */
63     p_sys->p_preparser = playlist_preparser_New( p_playlist, p_sys->p_fetcher );
64     if( !p_sys->p_preparser )
65         msg_Err( p_playlist, "cannot create playlist preparser" );
66
67     /* Start the playlist thread */
68     if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
69                            VLC_THREAD_PRIORITY_LOW, false ) )
70     {
71         msg_Err( p_playlist, "cannot spawn playlist thread" );
72     }
73     msg_Err( p_playlist, "Activated" );
74 }
75
76 void playlist_Deactivate( playlist_t *p_playlist )
77 {
78     /* */
79     playlist_private_t *p_sys = pl_priv(p_playlist);
80
81     msg_Err( p_playlist, "Deactivate" );
82     vlc_object_kill( p_playlist );
83     vlc_thread_join( p_playlist );
84
85     if( p_sys->p_preparser )
86         playlist_preparser_Delete( p_sys->p_preparser );
87     if( p_sys->p_fetcher )
88         playlist_fetcher_Delete( p_sys->p_fetcher );
89
90     /* The NULL are there only to assert in playlist destructor */
91     p_sys->p_preparser = NULL;
92     p_sys->p_fetcher = NULL;
93     msg_Err( p_playlist, "Deactivated" );
94 }
95
96 /**
97  * Run the main control thread itself
98  */
99 static void* RunControlThread ( vlc_object_t *p_this )
100 {
101     playlist_t *p_playlist = (playlist_t*)p_this;
102
103     int canc = vlc_savecancel ();
104     vlc_object_lock( p_playlist );
105     while( vlc_object_alive( p_playlist ) )
106     {
107         playlist_MainLoop( p_playlist );
108
109         /* The playlist lock has been unlocked, so we can't tell if
110          * someone has killed us in the meantime. Check now. */
111         if( !vlc_object_alive( p_playlist ) )
112             break;
113
114         if( pl_priv(p_playlist)->b_cant_sleep )
115         {
116             /* 100 ms is an acceptable delay for playlist operations */
117             vlc_object_unlock( p_playlist );
118
119             msleep( INTF_IDLE_SLEEP*2 );
120
121             vlc_object_lock( p_playlist );
122         }
123         else
124         {
125             vlc_object_wait( p_playlist );
126         }
127     }
128     vlc_object_unlock( p_playlist );
129
130     playlist_LastLoop( p_playlist );
131     vlc_restorecancel (canc);
132     return NULL;
133 }