* ./src/video_output/video_output.c: fixed the "picture has invalid status"
[vlc.git] / src / playlist / playlist.c
1 /*****************************************************************************
2  * playlist.c : Playlist management functions
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: playlist.c,v 1.3 2002/06/02 09:03:54 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23 #include <stdlib.h>                                      /* free(), strtol() */
24 #include <stdio.h>                                              /* sprintf() */
25 #include <string.h>                                            /* strerror() */
26 #include <errno.h>                                                 /* ENOMEM */
27
28 #include <vlc/vlc.h>
29
30 #include "stream_control.h"
31 #include "input_ext-intf.h"
32
33 #include "playlist.h"
34
35 #define PLAYLIST_STOPPED 0
36 #define PLAYLIST_RUNNING 1
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static void RunThread ( playlist_t * );
42
43 /*****************************************************************************
44  * playlist_Create: create playlist
45  *****************************************************************************
46  * Create a playlist structure.
47  *****************************************************************************/
48 playlist_t * __playlist_Create ( vlc_object_t *p_parent )
49 {
50     playlist_t *p_playlist;
51
52     /* Allocate structure */
53     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
54     if( !p_playlist )
55     {
56         msg_Err( p_parent, "out of memory" );
57         return NULL;
58     }
59
60     vlc_object_attach( p_playlist, p_parent );
61
62     p_playlist->p_input = NULL;
63     p_playlist->i_status = PLAYLIST_RUNNING;
64     p_playlist->i_index = -1;
65     p_playlist->i_size = 0;
66     p_playlist->pp_items = NULL;
67     vlc_mutex_init( p_playlist, &p_playlist->change_lock );
68
69     if( vlc_thread_create( p_playlist, "playlist", RunThread, 0 ) )
70     {
71         msg_Err( p_playlist, "cannot spawn playlist thread" );
72         vlc_mutex_destroy( &p_playlist->change_lock );
73         vlc_object_detach_all( p_playlist );
74         vlc_object_destroy( p_playlist );
75         return NULL;
76     }
77
78     return p_playlist;
79 }
80
81 /*****************************************************************************
82  * playlist_Destroy: destroy the playlist
83  *****************************************************************************
84  * Delete all items in the playlist and free the playlist structure.
85  *****************************************************************************/
86 void playlist_Destroy( playlist_t * p_playlist )
87 {
88     p_playlist->b_die = 1;
89
90     vlc_thread_join( p_playlist );
91
92     vlc_mutex_destroy( &p_playlist->change_lock );
93     vlc_object_destroy( p_playlist );
94 }
95
96 /*****************************************************************************
97  * playlist_Add: add an item to the playlist
98  *****************************************************************************
99  * Add an item to the playlist at position i_pos. If i_pos is PLAYLIST_END,
100  * add it at the end regardless of the playlist current size.
101  *****************************************************************************/
102 int __playlist_Add( vlc_object_t *p_this, int i_pos, const char * psz_item )
103 {
104     playlist_t *p_playlist;
105
106     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
107
108     if( p_playlist == NULL )
109     {
110         msg_Dbg( p_this, "no playlist present, creating one" );
111         p_playlist = playlist_Create( p_this->p_vlc );
112
113         if( p_playlist == NULL )
114         {
115             return VLC_EGENERIC;
116         }
117
118         vlc_object_yield( p_playlist );
119     }
120
121     msg_Warn( p_this, "adding playlist item « %s »", psz_item );
122
123     vlc_mutex_lock( &p_playlist->change_lock );
124
125     p_playlist->i_size++;
126     p_playlist->pp_items = realloc( p_playlist->pp_items,
127                                     p_playlist->i_size * sizeof(void*) );
128     if( p_playlist->pp_items == NULL )
129     {
130         msg_Err( p_playlist, "out of memory" );
131         vlc_mutex_unlock( &p_playlist->change_lock );
132         vlc_object_release( p_playlist );
133         return -1;
134     }
135
136     i_pos = p_playlist->i_size - 1; /* FIXME */
137     p_playlist->pp_items[i_pos] = malloc( sizeof( playlist_item_t ) );
138     p_playlist->pp_items[i_pos]->psz_name = strdup( psz_item );
139     p_playlist->pp_items[i_pos]->i_type = 0;
140     p_playlist->pp_items[i_pos]->i_status = 0;
141
142     vlc_mutex_unlock( &p_playlist->change_lock );
143     vlc_object_release( p_playlist );
144
145     return 0;
146 }
147
148 /*****************************************************************************
149  * playlist_Delete: delete an item from the playlist
150  *****************************************************************************
151  * Delete the item in the playlist with position i_pos.
152  *****************************************************************************/
153 int playlist_Delete( playlist_t * p_playlist, int i_pos )
154 {
155     return 0;
156 }
157
158 /*****************************************************************************
159  * playlist_Command: do a playlist action
160  *****************************************************************************
161  * Delete the item in the playlist with position i_pos.
162  *****************************************************************************/
163 void playlist_Command( playlist_t * p_playlist, int i_command, int i_arg )
164 {   
165     switch( i_command )
166     {
167     case PLAYLIST_STOP:
168         msg_Dbg( p_playlist, "stopping" );
169         p_playlist->i_status = PLAYLIST_STOPPED;
170         break;
171     case PLAYLIST_PLAY:
172         msg_Dbg( p_playlist, "running" );
173         p_playlist->i_status = PLAYLIST_RUNNING;
174         break;
175     case PLAYLIST_SKIP:
176         msg_Dbg( p_playlist, "next" );
177         if( p_playlist->i_size )
178         {
179             p_playlist->i_index = 0;
180             p_playlist->i_status = PLAYLIST_RUNNING;
181         }
182         break;
183     default:
184         break;
185     }
186
187     return;
188 }
189
190 /* Following functions are local */
191
192 /*****************************************************************************
193  * RunThread: main playlist thread
194  *****************************************************************************/
195 static void RunThread ( playlist_t *p_playlist )
196 {
197     while( !p_playlist->b_die )
198     {
199         /* If there is an input, check that it doesn't need to die. */
200         if( p_playlist->p_input )
201         {
202             if( p_playlist->p_input->i_status == THREAD_OVER )
203             {
204                 input_thread_t *p_input;
205
206                 /* Unlink current input */
207                 vlc_mutex_lock( &p_playlist->change_lock );
208                 p_input = p_playlist->p_input;
209                 p_playlist->p_input = NULL;
210                 vlc_mutex_unlock( &p_playlist->change_lock );
211
212                 /* Destroy input */
213                 vlc_object_detach_all( p_input );
214                 vlc_object_release( p_input );
215                 input_DestroyThread( p_input );
216             }
217             else if(    ( p_playlist->p_input->i_status == THREAD_READY
218                            || p_playlist->p_input->i_status == THREAD_ERROR )
219                      && ( p_playlist->p_input->b_error
220                            || p_playlist->p_input->b_eof ) )
221             {
222                 input_StopThread( p_playlist->p_input, NULL );
223             }
224         }
225         else if( p_playlist->i_status != PLAYLIST_STOPPED )
226         {
227             /* Select the next playlist item */
228             playlist_Next( p_playlist );
229
230             /* don't loop by default: stop at playlist end */
231             if( p_playlist->i_index == -1 )
232             {
233                 p_playlist->i_status = PLAYLIST_STOPPED;
234             }
235             else
236             {
237                 input_thread_t *p_input;
238
239                 //p_playlist->i_mode = PLAYLIST_FORWARD +
240                 //    config_GetInt( p_playlist, "loop-playlist" );
241                 msg_Dbg( p_playlist, "creating new input thread" );
242                 p_input = input_CreateThread( p_playlist,
243                             p_playlist->pp_items[p_playlist->i_index], NULL );
244                 if( p_input != NULL )
245                 {
246                     /* Link current input */
247                     vlc_mutex_lock( &p_playlist->change_lock );
248                     p_playlist->p_input = p_input;
249                     vlc_mutex_unlock( &p_playlist->change_lock );
250                 }
251             }
252         }
253
254         msleep( INTF_IDLE_SLEEP );
255     }
256
257     /* If there is an input, kill it */
258     while( p_playlist->p_input )
259     {
260         if( p_playlist->p_input->i_status == THREAD_OVER )
261         {
262             input_thread_t *p_input;
263
264             /* Unlink current input */
265             vlc_mutex_lock( &p_playlist->change_lock );
266             p_input = p_playlist->p_input;
267             p_playlist->p_input = NULL;
268             vlc_mutex_unlock( &p_playlist->change_lock );
269
270             /* Destroy input */
271             vlc_object_detach_all( p_input );
272             vlc_object_release( p_input );
273             input_DestroyThread( p_input );
274         }
275         else if(    ( p_playlist->p_input->i_status == THREAD_READY
276                        || p_playlist->p_input->i_status == THREAD_ERROR )
277                  && ( p_playlist->p_input->b_error
278                        || p_playlist->p_input->b_eof ) )
279         {
280             input_StopThread( p_playlist->p_input, NULL );
281         }
282         else
283         {
284             p_playlist->p_input->b_eof = 1;
285         }
286
287         msleep( INTF_IDLE_SLEEP );
288     }
289 }
290