]> git.sesse.net Git - vlc/blob - src/playlist/playlist.c
(new in MAIN)
[vlc] / src / playlist / playlist.c
1 /*****************************************************************************
2  * playlist.c : Playlist management functions
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: playlist.c,v 1.5 2002/06/04 00:11:12 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     p_playlist->p_input = NULL;
61     p_playlist->i_status = PLAYLIST_RUNNING;
62     p_playlist->i_index = -1;
63     p_playlist->i_size = 0;
64     p_playlist->pp_items = NULL;
65     vlc_mutex_init( p_playlist, &p_playlist->change_lock );
66
67     vlc_object_attach( p_playlist, p_parent );
68
69     if( vlc_thread_create( p_playlist, "playlist", RunThread, 0 ) )
70     {
71         msg_Err( p_playlist, "cannot spawn playlist thread" );
72         vlc_object_detach_all( p_playlist );
73         vlc_mutex_destroy( &p_playlist->change_lock );
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( playlist_t *p_playlist, int i_pos, const char * psz_item )
103 {
104     msg_Warn( p_playlist, "adding playlist item « %s »", psz_item );
105
106     vlc_mutex_lock( &p_playlist->change_lock );
107
108     p_playlist->i_size++;
109     p_playlist->pp_items = realloc( p_playlist->pp_items,
110                                     p_playlist->i_size * sizeof(void*) );
111     if( p_playlist->pp_items == NULL )
112     {
113         msg_Err( p_playlist, "out of memory" );
114         vlc_mutex_unlock( &p_playlist->change_lock );
115         vlc_object_release( p_playlist );
116         return -1;
117     }
118
119     i_pos = p_playlist->i_size - 1; /* FIXME */
120     p_playlist->pp_items[i_pos] = malloc( sizeof( playlist_item_t ) );
121     p_playlist->pp_items[i_pos]->psz_name = strdup( psz_item );
122     p_playlist->pp_items[i_pos]->i_type = 0;
123     p_playlist->pp_items[i_pos]->i_status = 0;
124
125     p_playlist->i_status = PLAYLIST_RUNNING;
126
127     vlc_mutex_unlock( &p_playlist->change_lock );
128
129     return 0;
130 }
131
132 /*****************************************************************************
133  * playlist_Delete: delete an item from the playlist
134  *****************************************************************************
135  * Delete the item in the playlist with position i_pos.
136  *****************************************************************************/
137 int playlist_Delete( playlist_t * p_playlist, int i_pos )
138 {
139     vlc_mutex_lock( &p_playlist->change_lock );
140
141     vlc_mutex_unlock( &p_playlist->change_lock );
142
143     return 0;
144 }
145
146 /*****************************************************************************
147  * playlist_Command: do a playlist action
148  *****************************************************************************
149  * 
150  *****************************************************************************/
151 void playlist_Command( playlist_t * p_playlist, int i_command, int i_arg )
152 {   
153     vlc_mutex_lock( &p_playlist->change_lock );
154
155     switch( i_command )
156     {
157     case PLAYLIST_STOP:
158         msg_Dbg( p_playlist, "stopping" );
159         p_playlist->i_status = PLAYLIST_STOPPED;
160         break;
161     case PLAYLIST_PLAY:
162         msg_Dbg( p_playlist, "running" );
163         p_playlist->i_status = PLAYLIST_RUNNING;
164         break;
165     case PLAYLIST_SKIP:
166         msg_Dbg( p_playlist, "next" );
167         if( p_playlist->i_size )
168         {
169             p_playlist->i_index = 0;
170             p_playlist->i_status = PLAYLIST_RUNNING;
171         }
172         break;
173     default:
174         break;
175     }
176
177     vlc_mutex_unlock( &p_playlist->change_lock );
178
179     return;
180 }
181
182 /* Following functions are local */
183
184 /*****************************************************************************
185  * RunThread: main playlist thread
186  *****************************************************************************/
187 static void RunThread ( playlist_t *p_playlist )
188 {
189     while( !p_playlist->b_die )
190     {
191         /* If there is an input, check that it doesn't need to die. */
192         if( p_playlist->p_input )
193         {
194             /* This input is dead. Remove it ! */
195             if( p_playlist->p_input->b_dead )
196             {
197                 input_thread_t *p_input;
198
199                 /* Unlink current input */
200                 vlc_mutex_lock( &p_playlist->change_lock );
201                 p_input = p_playlist->p_input;
202                 p_playlist->p_input = NULL;
203                 vlc_object_detach_all( p_input );
204                 vlc_mutex_unlock( &p_playlist->change_lock );
205
206                 /* Destroy input */
207                 vlc_object_release( p_input );
208                 input_DestroyThread( p_input );
209                 vlc_object_destroy( p_input );
210             }
211             /* This input is dying, let him do */
212             else if( p_playlist->p_input->b_die )
213             {
214                 ;
215             }
216             /* This input has finished, ask him to die ! */
217             else if( p_playlist->p_input->b_error
218                       || p_playlist->p_input->b_eof )
219             {
220                 input_StopThread( p_playlist->p_input );
221             }
222         }
223         else if( p_playlist->i_status != PLAYLIST_STOPPED )
224         {
225             /* Select the next playlist item */
226             playlist_Next( p_playlist );
227
228             /* don't loop by default: stop at playlist end */
229             if( p_playlist->i_index == -1 )
230             {
231                 p_playlist->i_status = PLAYLIST_STOPPED;
232             }
233             else
234             {
235                 input_thread_t *p_input;
236
237                 //p_playlist->i_mode = PLAYLIST_FORWARD +
238                 //    config_GetInt( p_playlist, "loop-playlist" );
239                 msg_Dbg( p_playlist, "creating new input thread" );
240                 p_input = input_CreateThread( p_playlist,
241                             p_playlist->pp_items[p_playlist->i_index], NULL );
242                 if( p_input != NULL )
243                 {
244                     /* Link current input */
245                     vlc_mutex_lock( &p_playlist->change_lock );
246                     p_playlist->p_input = p_input;
247                     vlc_mutex_unlock( &p_playlist->change_lock );
248                 }
249             }
250         }
251
252         msleep( INTF_IDLE_SLEEP );
253     }
254
255     /* If there is an input, kill it */
256     while( p_playlist->p_input )
257     {
258         if( p_playlist->p_input->b_dead )
259         {
260             input_thread_t *p_input;
261
262             /* Unlink current input */
263             vlc_mutex_lock( &p_playlist->change_lock );
264             p_input = p_playlist->p_input;
265             p_playlist->p_input = NULL;
266             vlc_object_detach_all( p_input );
267             vlc_mutex_unlock( &p_playlist->change_lock );
268
269             /* Destroy input */
270             vlc_object_release( p_input );
271             input_DestroyThread( p_input );
272             vlc_object_destroy( p_input );
273         }
274         /* This input is dying, let him do */
275         else if( p_playlist->p_input->b_die )
276         {
277             ;
278         }
279         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
280         {
281             input_StopThread( p_playlist->p_input );
282         }
283         else
284         {
285             p_playlist->p_input->b_eof = 1;
286         }
287
288         msleep( INTF_IDLE_SLEEP );
289     }
290 }
291