]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
Fixed too soon playlist items destruction.
[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 "stream_output/stream_output.h"
34 #include "playlist_internal.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static void* RunControlThread   ( vlc_object_t * );
40
41 /*****************************************************************************
42  * Main functions for the global thread
43  *****************************************************************************/
44
45 /**
46  * Create the main playlist threads.
47  * Additionally to the playlist, this thread controls :
48  *    - Statistics
49  *    - VLM
50  * \param p_parent
51  * \return an object with a started thread
52  */
53 void playlist_Activate( playlist_t *p_playlist )
54 {
55     /* */
56     playlist_private_t *p_sys = pl_priv(p_playlist);
57
58     /* Fetcher */
59     p_sys->p_fetcher = playlist_fetcher_New( p_playlist );
60     if( !p_sys->p_fetcher )
61         msg_Err( p_playlist, "cannot create playlist fetcher" );
62
63     /* Preparse */
64     p_sys->p_preparser = playlist_preparser_New( p_playlist, p_sys->p_fetcher );
65     if( !p_sys->p_preparser )
66         msg_Err( p_playlist, "cannot create playlist preparser" );
67
68     /* Start the playlist thread */
69     if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
70                            VLC_THREAD_PRIORITY_LOW, false ) )
71     {
72         msg_Err( p_playlist, "cannot spawn playlist thread" );
73     }
74     msg_Err( p_playlist, "Activated" );
75 }
76
77 void playlist_Deactivate( playlist_t *p_playlist )
78 {
79     /* */
80     playlist_private_t *p_sys = pl_priv(p_playlist);
81
82     msg_Err( p_playlist, "Deactivate" );
83     vlc_object_kill( p_playlist );
84     vlc_thread_join( p_playlist );
85
86     if( p_sys->p_preparser )
87         playlist_preparser_Delete( p_sys->p_preparser );
88     if( p_sys->p_fetcher )
89         playlist_fetcher_Delete( p_sys->p_fetcher );
90
91     /* close the remaining sout-keep */
92     if( p_sys->p_sout )
93         sout_DeleteInstance( p_sys->p_sout );
94
95     /* */
96     playlist_MLDump( p_playlist );
97
98     PL_LOCK;
99
100     /* Release the current node */
101     set_current_status_node( p_playlist, NULL );
102
103     /* Release the current item */
104     set_current_status_item( p_playlist, NULL );
105
106     FOREACH_ARRAY( playlist_item_t *p_del, p_playlist->all_items )
107         free( p_del->pp_children );
108         vlc_gc_decref( p_del->p_input );
109         free( p_del );
110     FOREACH_END();
111     ARRAY_RESET( p_playlist->all_items );
112     FOREACH_ARRAY( playlist_item_t *p_del, pl_priv(p_playlist)->items_to_delete )
113         free( p_del->pp_children );
114         vlc_gc_decref( p_del->p_input );
115         free( p_del );
116     FOREACH_END();
117     ARRAY_RESET( pl_priv(p_playlist)->items_to_delete );
118
119     ARRAY_RESET( p_playlist->items );
120     ARRAY_RESET( p_playlist->current );
121
122     PL_UNLOCK;
123
124     /* The NULL are there only to assert in playlist destructor */
125     p_sys->p_sout = NULL;
126     p_sys->p_preparser = NULL;
127     p_sys->p_fetcher = NULL;
128     msg_Err( p_playlist, "Deactivated" );
129 }
130
131 /**
132  * Run the main control thread itself
133  */
134 static void* RunControlThread ( vlc_object_t *p_this )
135 {
136     playlist_t *p_playlist = (playlist_t*)p_this;
137
138     int canc = vlc_savecancel ();
139     vlc_object_lock( p_playlist );
140     while( vlc_object_alive( p_playlist ) )
141     {
142         playlist_MainLoop( p_playlist );
143
144         /* The playlist lock has been unlocked, so we can't tell if
145          * someone has killed us in the meantime. Check now. */
146         if( !vlc_object_alive( p_playlist ) )
147             break;
148
149         if( pl_priv(p_playlist)->b_cant_sleep )
150         {
151             /* 100 ms is an acceptable delay for playlist operations */
152             vlc_object_unlock( p_playlist );
153
154             msleep( INTF_IDLE_SLEEP*2 );
155
156             vlc_object_lock( p_playlist );
157         }
158         else
159         {
160             vlc_object_wait( p_playlist );
161         }
162     }
163     vlc_object_unlock( p_playlist );
164
165     playlist_LastLoop( p_playlist );
166     vlc_restorecancel (canc);
167     return NULL;
168 }