988721f68014da6ec23d868ebe2dc77573edc09e
[vlc.git] / src / control / playlist.c
1 /*****************************************************************************
2  * playlist.c: libvlc new API playlist handling functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <vlc_playlist.h>
27
28 #include <assert.h>
29
30 #include "../playlist/playlist_internal.h"
31
32 #define PL p_instance->p_libvlc_int->p_playlist
33
34 static inline int playlist_was_locked( libvlc_instance_t *p_instance )
35 {
36     int was_locked;
37     vlc_mutex_lock( &p_instance->instance_lock );
38     was_locked = p_instance->b_playlist_locked;
39     vlc_mutex_unlock( &p_instance->instance_lock );
40     return was_locked;
41 }
42
43 static inline void playlist_mark_locked( libvlc_instance_t *p_instance,
44                                          int locked )
45 {
46     vlc_mutex_lock( &p_instance->instance_lock );
47     p_instance->b_playlist_locked = locked;
48     vlc_mutex_unlock( &p_instance->instance_lock );
49 }
50
51 void libvlc_playlist_loop( libvlc_instance_t *p_instance, vlc_bool_t loop,
52                            libvlc_exception_t *p_e)
53 {
54     assert( PL );
55     var_SetBool( PL, "loop", loop );
56 }
57
58 void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
59                            int i_options, char **ppsz_options,
60                            libvlc_exception_t *p_e )
61 {
62     int did_lock = 0;
63     assert( PL );
64     ///\todo Handle additionnal options
65
66     if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" );
67     if( i_id > 0 )
68     {
69         playlist_item_t *p_item;
70         if (! playlist_was_locked( p_instance ) )
71         {
72             playlist_mark_locked( p_instance, 1 );
73             vlc_mutex_lock( &PL->object_lock );
74             did_lock = 1;
75         }
76
77         p_item = playlist_ItemGetByInputId( PL, i_id,
78                                             PL->status.p_node );
79         if( !p_item )
80         {
81             if( did_lock == 1 )
82             {
83                 vlc_mutex_unlock( &PL->object_lock );
84                 playlist_mark_locked( p_instance, 0 );
85             }
86             RAISEVOID( "Unable to find item" );
87         }
88
89         playlist_Control( PL, PLAYLIST_VIEWPLAY, VLC_TRUE,
90                           PL->status.p_node, p_item );
91         if( did_lock == 1 )
92         {
93             vlc_mutex_unlock( &PL->object_lock );
94             playlist_mark_locked( p_instance, 0 );
95         }
96     }
97     else
98     {
99         playlist_Control( PL, PLAYLIST_PLAY,
100                           playlist_was_locked( p_instance ) );
101     }
102 }
103
104 void libvlc_playlist_pause( libvlc_instance_t *p_instance,
105                             libvlc_exception_t *p_e )
106 {
107     assert( PL );
108     if( playlist_Control( PL, PLAYLIST_PAUSE,
109                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
110         RAISEVOID( "Empty playlist" );
111 }
112
113
114 void libvlc_playlist_stop( libvlc_instance_t *p_instance,
115                            libvlc_exception_t *p_e )
116 {
117     assert( PL );
118     if( playlist_Control( PL, PLAYLIST_STOP,
119                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
120         RAISEVOID( "Empty playlist" );
121 }
122
123 void libvlc_playlist_clear( libvlc_instance_t *p_instance,
124                             libvlc_exception_t *p_e )
125 {
126     assert( PL );
127     playlist_Clear( PL, playlist_was_locked( p_instance ) );
128 }
129
130 void libvlc_playlist_next( libvlc_instance_t *p_instance,
131                            libvlc_exception_t *p_e )
132 {
133     assert( PL );
134     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
135                           1 ) != VLC_SUCCESS )
136         RAISEVOID( "Empty playlist" );
137 }
138
139 void libvlc_playlist_prev( libvlc_instance_t *p_instance,
140                            libvlc_exception_t *p_e )
141 {
142     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
143                           -1  ) != VLC_SUCCESS )
144         RAISEVOID( "Empty playlist" );
145 }
146
147 int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
148                          const char *psz_name, libvlc_exception_t *p_e )
149 {
150     return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
151                                          0, NULL, p_e );
152 }
153
154 int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
155                                   const char *psz_uri, const char *psz_name,
156                                   int i_options, const char **ppsz_options,
157                                   libvlc_exception_t *p_e )
158 {
159     assert( PL );
160     if( playlist_was_locked( p_instance ) )
161     {
162         libvlc_exception_raise( p_e, "You must unlock playlist before "
163                                "calling libvlc_playlist_add" );
164         return VLC_EGENERIC;
165     }
166     return playlist_AddExt( PL, psz_uri, psz_name,
167                             PLAYLIST_INSERT, PLAYLIST_END, -1, ppsz_options,
168                             i_options, 1, VLC_FALSE );
169 }
170
171
172 int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
173                                  libvlc_exception_t *p_e )
174 {
175     assert( PL );
176
177     if( playlist_DeleteFromInput( PL, i_id,
178                                   playlist_was_locked( p_instance ) ) )
179     {
180         libvlc_exception_raise( p_e, "deletion failed" );
181         return VLC_ENOITEM;
182     }
183     return VLC_SUCCESS;
184 }
185
186 int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
187                                libvlc_exception_t *p_e )
188 {
189     assert( PL );
190     return playlist_IsPlaying( PL );
191 }
192
193 int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
194                                  libvlc_exception_t *p_e )
195 {
196     assert( PL );
197     return playlist_CurrentSize( PL );
198 }
199
200 void libvlc_playlist_lock( libvlc_instance_t *p_instance )
201 {
202     assert( PL );
203     vlc_mutex_lock( &PL->object_lock );
204     p_instance->b_playlist_locked = 1;
205 }
206
207 void libvlc_playlist_unlock( libvlc_instance_t *p_instance )
208 {
209     assert( PL );
210     p_instance->b_playlist_locked = 0;
211     vlc_mutex_unlock( &PL->object_lock );
212 }
213
214
215
216 libvlc_input_t * libvlc_playlist_get_input( libvlc_instance_t *p_instance,
217                                             libvlc_exception_t *p_e )
218 {
219     libvlc_input_t *p_input;
220     assert( PL );
221
222     vlc_mutex_lock( &PL->object_lock );
223     if( PL->p_input == NULL )
224     {
225         libvlc_exception_raise( p_e, "No active input" );
226         vlc_mutex_unlock( &PL->object_lock );
227         return NULL;
228     }
229     p_input = (libvlc_input_t *)malloc( sizeof( libvlc_input_t ) );
230
231     p_input->i_input_id = PL->p_input->i_object_id;
232     p_input->p_instance = p_instance;
233     vlc_mutex_unlock( &PL->object_lock );
234
235     return p_input;
236 }