]> git.sesse.net Git - vlc/blob - src/control/playlist.c
Factor and fix locking
[vlc] / 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             playlist_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             playlist_Control( PL, PLAYLIST_VIEWPLAY, pl_Locked,
86                               PL->p_root_category, p_item );
87        else
88             RAISEVOID( "Unable to find item" );
89
90         if( did_lock == 1 )
91         {
92             playlist_Unlock( PL );
93             playlist_mark_locked( p_instance, 0 );
94         }
95     }
96     else
97     {
98         playlist_Control( PL, PLAYLIST_PLAY,
99                           playlist_was_locked( p_instance ) );
100     }
101 }
102
103 void libvlc_playlist_pause( libvlc_instance_t *p_instance,
104                             libvlc_exception_t *p_e )
105 {
106     assert( PL );
107     if( playlist_Control( PL, PLAYLIST_PAUSE,
108                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
109         RAISEVOID( "Empty playlist" );
110 }
111
112
113 void libvlc_playlist_stop( libvlc_instance_t *p_instance,
114                            libvlc_exception_t *p_e )
115 {
116     assert( PL );
117     if( playlist_Control( PL, PLAYLIST_STOP,
118                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
119         RAISEVOID( "Empty playlist" );
120 }
121
122 void libvlc_playlist_clear( libvlc_instance_t *p_instance,
123                             libvlc_exception_t *p_e )
124 {
125     VLC_UNUSED(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 static int PlaylistAddExtended( libvlc_instance_t *p_instance,
156                                 const char *psz_uri, const char *psz_name,
157                                 int i_options, const char **ppsz_options,
158                                 unsigned i_option_flags,
159                                 libvlc_exception_t *p_e )
160 {
161     assert( PL );
162     if( playlist_was_locked( p_instance ) )
163     {
164         libvlc_exception_raise( p_e, "You must unlock playlist before "
165                                "calling libvlc_playlist_add" );
166         return VLC_EGENERIC;
167     }
168     return playlist_AddExt( PL, psz_uri, psz_name,
169                             PLAYLIST_INSERT, PLAYLIST_END, -1,
170                             i_options, ppsz_options, i_option_flags,
171                             true, pl_Unlocked );
172 }
173 int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
174                                   const char *psz_uri, const char *psz_name,
175                                   int i_options, const char **ppsz_options,
176                                   libvlc_exception_t *p_e )
177 {
178     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
179                                 i_options, ppsz_options, VLC_INPUT_OPTION_TRUSTED,
180                                 p_e );
181 }
182 int libvlc_playlist_add_extended_untrusted( libvlc_instance_t *p_instance,
183                                             const char *psz_uri, const char *psz_name,
184                                             int i_options, const char **ppsz_options,
185                                             libvlc_exception_t *p_e )
186 {
187     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
188                                 i_options, ppsz_options, 0,
189                                 p_e );
190 }
191
192 int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
193                                  libvlc_exception_t *p_e )
194 {
195     assert( PL );
196
197     if( playlist_DeleteFromInput( PL, i_id,
198                                   playlist_was_locked( p_instance ) ) )
199     {
200         libvlc_exception_raise( p_e, "deletion failed" );
201         return VLC_ENOITEM;
202     }
203     return VLC_SUCCESS;
204 }
205
206 int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
207                                libvlc_exception_t *p_e )
208 {
209     VLC_UNUSED(p_e);
210
211     assert( PL );
212     return playlist_Status( PL ) == PLAYLIST_RUNNING;
213 }
214
215 int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
216                                  libvlc_exception_t *p_e )
217 {
218     VLC_UNUSED(p_e);
219
220     assert( PL );
221     return playlist_CurrentSize( PL );
222 }
223
224 int libvlc_playlist_get_current_index ( libvlc_instance_t *p_instance,
225                                         libvlc_exception_t *p_e )
226 {
227     VLC_UNUSED(p_e);
228
229     assert( PL );
230     playlist_item_t *p_item = playlist_CurrentPlayingItem( PL );
231     if( !p_item )
232         return -1;
233     return p_item->i_id;
234 }
235
236 void libvlc_playlist_lock( libvlc_instance_t *p_instance )
237 {
238     assert( PL );
239     playlist_Lock( PL );
240     p_instance->b_playlist_locked = 1;
241 }
242
243 void libvlc_playlist_unlock( libvlc_instance_t *p_instance )
244 {
245     assert( PL );
246     p_instance->b_playlist_locked = 0;
247     playlist_Unlock( PL );
248 }
249
250 libvlc_media_player_t * libvlc_playlist_get_media_player(
251                                 libvlc_instance_t *p_instance,
252                                 libvlc_exception_t *p_e )
253 {
254     libvlc_media_player_t *p_mi;
255     assert( PL );
256     input_thread_t * input = playlist_CurrentInput( PL );
257     if( input )
258     {
259         p_mi = libvlc_media_player_new_from_input_thread(
260                             p_instance, input, p_e );
261         vlc_object_release( input );
262     }
263     else
264     {
265         /* no active input */
266         p_mi = NULL;
267         libvlc_exception_raise( p_e, "No active input" );
268     }
269
270     return p_mi;
271 }
272