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