]> git.sesse.net Git - vlc/blob - src/control/playlist.c
Yum, abusing private headers
[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 #define __LIBVLC__
32 #include "../playlist/playlist_internal.h"
33
34 #define PL (libvlc_priv (p_instance->p_libvlc_int)->p_playlist)
35
36 static inline int playlist_was_locked( libvlc_instance_t *p_instance )
37 {
38     int was_locked;
39     vlc_mutex_lock( &p_instance->instance_lock );
40     was_locked = p_instance->b_playlist_locked;
41     vlc_mutex_unlock( &p_instance->instance_lock );
42     return was_locked;
43 }
44
45 static inline void playlist_mark_locked( libvlc_instance_t *p_instance,
46                                          int locked )
47 {
48     vlc_mutex_lock( &p_instance->instance_lock );
49     p_instance->b_playlist_locked = locked;
50     vlc_mutex_unlock( &p_instance->instance_lock );
51 }
52
53 void libvlc_playlist_loop( libvlc_instance_t *p_instance, int loop,
54                            libvlc_exception_t *p_e)
55 {
56     VLC_UNUSED(p_e);
57
58     assert( PL );
59     var_SetBool( PL, "loop", loop );
60 }
61
62 void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
63                            int i_options, char **ppsz_options,
64                            libvlc_exception_t *p_e )
65 {
66     VLC_UNUSED(p_e); VLC_UNUSED(i_options); VLC_UNUSED(ppsz_options);
67
68     int did_lock = 0;
69     assert( PL );
70     ///\todo Handle additionnal options
71
72     if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" );
73     if( i_id > 0 )
74     {
75         playlist_item_t *p_item;
76         if (! playlist_was_locked( p_instance ) )
77         {
78             playlist_mark_locked( p_instance, 1 );
79             playlist_Lock( PL );
80             did_lock = 1;
81         }
82
83         p_item = playlist_ItemGetByInputId( PL, i_id,
84                                             PL->p_root_category );
85         if( p_item )
86             playlist_Control( PL, PLAYLIST_VIEWPLAY, pl_Locked,
87                               PL->p_root_category, p_item );
88        else
89             RAISEVOID( "Unable to find item" );
90
91         if( did_lock == 1 )
92         {
93             playlist_Unlock( PL );
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     VLC_UNUSED(p_e);
127
128     assert( PL );
129     playlist_Clear( PL, playlist_was_locked( p_instance ) );
130 }
131
132 void libvlc_playlist_next( libvlc_instance_t *p_instance,
133                            libvlc_exception_t *p_e )
134 {
135     assert( PL );
136     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
137                           1 ) != VLC_SUCCESS )
138         RAISEVOID( "Empty playlist" );
139 }
140
141 void libvlc_playlist_prev( libvlc_instance_t *p_instance,
142                            libvlc_exception_t *p_e )
143 {
144     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
145                           -1  ) != VLC_SUCCESS )
146         RAISEVOID( "Empty playlist" );
147 }
148
149 int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
150                          const char *psz_name, libvlc_exception_t *p_e )
151 {
152     return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
153                                          0, NULL, p_e );
154 }
155
156 static int PlaylistAddExtended( libvlc_instance_t *p_instance,
157                                 const char *psz_uri, const char *psz_name,
158                                 int i_options, const char **ppsz_options,
159                                 unsigned i_option_flags,
160                                 libvlc_exception_t *p_e )
161 {
162     assert( PL );
163     if( playlist_was_locked( p_instance ) )
164     {
165         libvlc_exception_raise( p_e, "You must unlock playlist before "
166                                "calling libvlc_playlist_add" );
167         return VLC_EGENERIC;
168     }
169     return playlist_AddExt( PL, psz_uri, psz_name,
170                             PLAYLIST_INSERT, PLAYLIST_END, -1,
171                             i_options, ppsz_options, i_option_flags,
172                             true, pl_Unlocked );
173 }
174 int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
175                                   const char *psz_uri, const char *psz_name,
176                                   int i_options, const char **ppsz_options,
177                                   libvlc_exception_t *p_e )
178 {
179     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
180                                 i_options, ppsz_options, VLC_INPUT_OPTION_TRUSTED,
181                                 p_e );
182 }
183 int libvlc_playlist_add_extended_untrusted( libvlc_instance_t *p_instance,
184                                             const char *psz_uri, const char *psz_name,
185                                             int i_options, const char **ppsz_options,
186                                             libvlc_exception_t *p_e )
187 {
188     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
189                                 i_options, ppsz_options, 0,
190                                 p_e );
191 }
192
193 int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
194                                  libvlc_exception_t *p_e )
195 {
196     assert( PL );
197
198     if( playlist_DeleteFromInput( PL, i_id,
199                                   playlist_was_locked( p_instance ) ) )
200     {
201         libvlc_exception_raise( p_e, "deletion failed" );
202         return VLC_ENOITEM;
203     }
204     return VLC_SUCCESS;
205 }
206
207 int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
208                                libvlc_exception_t *p_e )
209 {
210     VLC_UNUSED(p_e);
211
212     assert( PL );
213     return playlist_Status( PL ) == PLAYLIST_RUNNING;
214 }
215
216 int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
217                                  libvlc_exception_t *p_e )
218 {
219     VLC_UNUSED(p_e);
220
221     assert( PL );
222     return playlist_CurrentSize( PL );
223 }
224
225 int libvlc_playlist_get_current_index ( libvlc_instance_t *p_instance,
226                                         libvlc_exception_t *p_e )
227 {
228     VLC_UNUSED(p_e);
229
230     assert( PL );
231     playlist_item_t *p_item = playlist_CurrentPlayingItem( PL );
232     if( !p_item )
233         return -1;
234     return p_item->i_id;
235 }
236
237 void libvlc_playlist_lock( libvlc_instance_t *p_instance )
238 {
239     assert( PL );
240     playlist_Lock( PL );
241     p_instance->b_playlist_locked = 1;
242 }
243
244 void libvlc_playlist_unlock( libvlc_instance_t *p_instance )
245 {
246     assert( PL );
247     p_instance->b_playlist_locked = 0;
248     playlist_Unlock( PL );
249 }
250
251 libvlc_media_player_t * libvlc_playlist_get_media_player(
252                                 libvlc_instance_t *p_instance,
253                                 libvlc_exception_t *p_e )
254 {
255     libvlc_media_player_t *p_mi;
256     assert( PL );
257     input_thread_t * input = playlist_CurrentInput( PL );
258     if( input )
259     {
260         p_mi = libvlc_media_player_new_from_input_thread(
261                             p_instance, input, p_e );
262         vlc_object_release( input );
263     }
264     else
265     {
266         /* no active input */
267         p_mi = NULL;
268         libvlc_exception_raise( p_e, "No active input" );
269     }
270
271     return p_mi;
272 }
273