]> git.sesse.net Git - vlc/blob - src/playlist/control.c
playlist: add real functions for locking
[vlc] / src / playlist / control.c
1 /*****************************************************************************
2  * control.c : Handle control of the playlist & running through it
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include "vlc_playlist.h"
30 #include "playlist_internal.h"
31 #include <assert.h>
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args );
37
38 /*****************************************************************************
39  * Playlist control
40  *****************************************************************************/
41
42 playlist_t *__pl_Hold( vlc_object_t *p_this )
43 {
44     playlist_t *pl;
45
46     barrier();
47     pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
48
49     assert( VLC_OBJECT(pl) != p_this /* This does not make sense to hold the playlist
50     using pl_Hold. use vlc_object_hold in this case */ );
51
52     if (pl)
53         vlc_object_hold (pl);
54     return pl;
55 }
56
57 void __pl_Release( vlc_object_t *p_this )
58 {
59     playlist_t *pl = libvlc_priv (p_this->p_libvlc)->p_playlist;
60     assert( pl != NULL );
61
62     /* The rule is that pl_Release() should act on
63        the same object than pl_Hold() */
64     assert( VLC_OBJECT(pl) != p_this);
65
66     vlc_object_release( pl );
67 }
68
69 void playlist_Lock( playlist_t *pl )
70 {
71     vlc_object_lock( pl );
72 }
73
74 void playlist_Unlock( playlist_t *pl )
75 {
76     vlc_object_unlock( pl );
77 }
78
79 void playlist_AssertLocked( playlist_t *pl )
80 {
81     vlc_object_assert_locked( pl );
82 }
83
84 int playlist_Control( playlist_t * p_playlist, int i_query,
85                       bool b_locked, ... )
86 {
87     va_list args;
88     int i_result;
89     PL_LOCK_IF( !b_locked );
90     va_start( args, b_locked );
91     i_result = PlaylistVAControl( p_playlist, i_query, args );
92     va_end( args );
93     PL_UNLOCK_IF( !b_locked );
94
95     return i_result;
96 }
97
98 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
99 {
100     playlist_item_t *p_item, *p_node;
101     vlc_value_t val;
102
103     PL_ASSERT_LOCKED;
104
105     if( !vlc_object_alive( p_playlist ) )
106         return VLC_EGENERIC;
107
108     if( playlist_IsEmpty( p_playlist ) )
109         return VLC_EGENERIC;
110
111     switch( i_query )
112     {
113     case PLAYLIST_STOP:
114         pl_priv(p_playlist)->request.i_status = PLAYLIST_STOPPED;
115         pl_priv(p_playlist)->request.b_request = true;
116         pl_priv(p_playlist)->request.p_item = NULL;
117         break;
118
119     // Node can be null, it will keep the same. Use with care ...
120     // Item null = take the first child of node
121     case PLAYLIST_VIEWPLAY:
122         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
123         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
124         if ( p_node == NULL )
125         {
126             p_node = get_current_status_node( p_playlist );
127             assert( p_node );
128         }
129         pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
130         pl_priv(p_playlist)->request.i_skip = 0;
131         pl_priv(p_playlist)->request.b_request = true;
132         pl_priv(p_playlist)->request.p_node = p_node;
133         pl_priv(p_playlist)->request.p_item = p_item;
134         if( p_item && var_GetBool( p_playlist, "random" ) )
135             pl_priv(p_playlist)->b_reset_currently_playing = true;
136         break;
137
138     case PLAYLIST_PLAY:
139         if( pl_priv(p_playlist)->p_input )
140         {
141             val.i_int = PLAYING_S;
142             var_Set( pl_priv(p_playlist)->p_input, "state", val );
143             break;
144         }
145         else
146         {
147             pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
148             pl_priv(p_playlist)->request.b_request = true;
149             pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
150             pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
151             pl_priv(p_playlist)->request.i_skip = 0;
152         }
153         break;
154
155     case PLAYLIST_PAUSE:
156         val.i_int = 0;
157         if( pl_priv(p_playlist)->p_input )
158             var_Get( pl_priv(p_playlist)->p_input, "state", &val );
159
160         if( val.i_int == PAUSE_S )
161         {
162             pl_priv(p_playlist)->status.i_status = PLAYLIST_RUNNING;
163             if( pl_priv(p_playlist)->p_input )
164             {
165                 val.i_int = PLAYING_S;
166                 var_Set( pl_priv(p_playlist)->p_input, "state", val );
167             }
168         }
169         else
170         {
171             pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
172             if( pl_priv(p_playlist)->p_input )
173             {
174                 val.i_int = PAUSE_S;
175                 var_Set( pl_priv(p_playlist)->p_input, "state", val );
176             }
177         }
178         break;
179
180     case PLAYLIST_SKIP:
181         pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
182         pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
183         pl_priv(p_playlist)->request.i_skip = (int) va_arg( args, int );
184         /* if already running, keep running */
185         if( pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED )
186             pl_priv(p_playlist)->request.i_status = pl_priv(p_playlist)->status.i_status;
187         pl_priv(p_playlist)->request.b_request = true;
188         break;
189
190     default:
191         msg_Err( p_playlist, "unknown playlist query" );
192         return VLC_EBADVAR;
193     }
194     vlc_cond_signal( &pl_priv(p_playlist)->signal );
195
196     return VLC_SUCCESS;
197 }
198
199 /*****************************************************************************
200  * Preparse control
201  *****************************************************************************/
202 /** Enqueue an item for preparsing */
203 int playlist_PreparseEnqueue( playlist_t *p_playlist,
204                               input_item_t *p_item, bool b_locked )
205 {
206     playlist_private_t *p_sys = pl_priv(p_playlist);
207
208     PL_LOCK_IF( !b_locked );
209     if( p_sys->p_preparser )
210         playlist_preparser_Push( p_sys->p_preparser, p_item );
211     PL_UNLOCK_IF( !b_locked );
212
213     return VLC_SUCCESS;
214 }
215
216 int playlist_AskForArtEnqueue( playlist_t *p_playlist,
217                                input_item_t *p_item, bool b_locked )
218 {
219     playlist_private_t *p_sys = pl_priv(p_playlist);
220
221     PL_LOCK_IF( !b_locked );
222     if( p_sys->p_fetcher )
223         playlist_fetcher_Push( p_sys->p_fetcher, p_item );
224     PL_UNLOCK_IF( !b_locked );
225
226     return VLC_SUCCESS;
227 }
228