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