Added libvlc_playlist_add_extended_untrusted and libvlc_media_add_option_untrusted.
[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 (libvlc_priv (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, int loop,
53                            libvlc_exception_t *p_e)
54 {
55     VLC_UNUSED(p_e);
56
57     assert( PL );
58     var_SetBool( PL, "loop", loop );
59 }
60
61 void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
62                            int i_options, char **ppsz_options,
63                            libvlc_exception_t *p_e )
64 {
65     VLC_UNUSED(p_e); VLC_UNUSED(i_options); VLC_UNUSED(ppsz_options);
66
67     int did_lock = 0;
68     assert( PL );
69     ///\todo Handle additionnal options
70
71     if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" );
72     if( i_id > 0 )
73     {
74         playlist_item_t *p_item;
75         if (! playlist_was_locked( p_instance ) )
76         {
77             playlist_mark_locked( p_instance, 1 );
78             vlc_object_lock( PL );
79             did_lock = 1;
80         }
81
82         p_item = playlist_ItemGetByInputId( PL, i_id,
83                                             PL->p_root_category );
84         if( !p_item )
85         {
86             if( did_lock == 1 )
87             {
88                 vlc_object_unlock( PL );
89                 playlist_mark_locked( p_instance, 0 );
90             }
91             RAISEVOID( "Unable to find item" );
92         }
93
94         playlist_Control( PL, PLAYLIST_VIEWPLAY, pl_Locked,
95                           PL->p_root_category, p_item );
96         if( did_lock == 1 )
97         {
98             vlc_object_unlock( PL );
99             playlist_mark_locked( p_instance, 0 );
100         }
101     }
102     else
103     {
104         playlist_Control( PL, PLAYLIST_PLAY,
105                           playlist_was_locked( p_instance ) );
106     }
107 }
108
109 void libvlc_playlist_pause( libvlc_instance_t *p_instance,
110                             libvlc_exception_t *p_e )
111 {
112     assert( PL );
113     if( playlist_Control( PL, PLAYLIST_PAUSE,
114                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
115         RAISEVOID( "Empty playlist" );
116 }
117
118
119 void libvlc_playlist_stop( libvlc_instance_t *p_instance,
120                            libvlc_exception_t *p_e )
121 {
122     assert( PL );
123     if( playlist_Control( PL, PLAYLIST_STOP,
124                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
125         RAISEVOID( "Empty playlist" );
126 }
127
128 void libvlc_playlist_clear( libvlc_instance_t *p_instance,
129                             libvlc_exception_t *p_e )
130 {
131     VLC_UNUSED(p_e);
132
133     assert( PL );
134     playlist_Clear( PL, playlist_was_locked( p_instance ) );
135 }
136
137 void libvlc_playlist_next( libvlc_instance_t *p_instance,
138                            libvlc_exception_t *p_e )
139 {
140     assert( PL );
141     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
142                           1 ) != VLC_SUCCESS )
143         RAISEVOID( "Empty playlist" );
144 }
145
146 void libvlc_playlist_prev( libvlc_instance_t *p_instance,
147                            libvlc_exception_t *p_e )
148 {
149     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
150                           -1  ) != VLC_SUCCESS )
151         RAISEVOID( "Empty playlist" );
152 }
153
154 int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
155                          const char *psz_name, libvlc_exception_t *p_e )
156 {
157     return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
158                                          0, NULL, p_e );
159 }
160
161 static int PlaylistAddExtended( libvlc_instance_t *p_instance,
162                                 const char *psz_uri, const char *psz_name,
163                                 int i_options, const char **ppsz_options,
164                                 unsigned i_option_flags,
165                                 libvlc_exception_t *p_e )
166 {
167     assert( PL );
168     if( playlist_was_locked( p_instance ) )
169     {
170         libvlc_exception_raise( p_e, "You must unlock playlist before "
171                                "calling libvlc_playlist_add" );
172         return VLC_EGENERIC;
173     }
174     return playlist_AddExt( PL, psz_uri, psz_name,
175                             PLAYLIST_INSERT, PLAYLIST_END, -1,
176                             i_options, ppsz_options, i_option_flags,
177                             true, pl_Unlocked );
178 }
179 int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
180                                   const char *psz_uri, const char *psz_name,
181                                   int i_options, const char **ppsz_options,
182                                   libvlc_exception_t *p_e )
183 {
184     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
185                                 i_options, ppsz_options, VLC_INPUT_OPTION_TRUSTED,
186                                 p_e );
187 }
188 int libvlc_playlist_add_extended_untrusted( libvlc_instance_t *p_instance,
189                                             const char *psz_uri, const char *psz_name,
190                                             int i_options, const char **ppsz_options,
191                                             libvlc_exception_t *p_e )
192 {
193     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
194                                 i_options, ppsz_options, 0,
195                                 p_e );
196 }
197
198 int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
199                                  libvlc_exception_t *p_e )
200 {
201     assert( PL );
202
203     if( playlist_DeleteFromInput( PL, i_id,
204                                   playlist_was_locked( p_instance ) ) )
205     {
206         libvlc_exception_raise( p_e, "deletion failed" );
207         return VLC_ENOITEM;
208     }
209     return VLC_SUCCESS;
210 }
211
212 int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
213                                libvlc_exception_t *p_e )
214 {
215     VLC_UNUSED(p_e);
216
217     assert( PL );
218     return playlist_IsPlaying( PL );
219 }
220
221 int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
222                                  libvlc_exception_t *p_e )
223 {
224     VLC_UNUSED(p_e);
225
226     assert( PL );
227     return playlist_CurrentSize( PL );
228 }
229
230 int libvlc_playlist_get_current_index ( libvlc_instance_t *p_instance,
231                                         libvlc_exception_t *p_e )
232 {
233     VLC_UNUSED(p_e);
234
235     assert( PL );
236     return playlist_CurrentId( PL );
237 }
238
239 void libvlc_playlist_lock( libvlc_instance_t *p_instance )
240 {
241     assert( PL );
242     vlc_object_lock( PL );
243     p_instance->b_playlist_locked = 1;
244 }
245
246 void libvlc_playlist_unlock( libvlc_instance_t *p_instance )
247 {
248     assert( PL );
249     p_instance->b_playlist_locked = 0;
250     vlc_object_unlock( PL );
251 }
252
253 libvlc_media_player_t * libvlc_playlist_get_media_player(
254                                 libvlc_instance_t *p_instance,
255                                 libvlc_exception_t *p_e )
256 {
257     libvlc_media_player_t *p_mi;
258     assert( PL );
259     input_thread_t * input = playlist_CurrentInput( PL );
260     if( input )
261     {
262         p_mi = libvlc_media_player_new_from_input_thread(
263                             p_instance, input, p_e );
264         vlc_object_release( input );
265     }
266     else
267     {
268         /* no active input */
269         p_mi = NULL;
270         libvlc_exception_raise( p_e, "No active input" );
271     }
272
273     return p_mi;
274 }
275