]> git.sesse.net Git - vlc/blob - src/playlist/control.c
LGPL
[vlc] / src / playlist / control.c
1 /*****************************************************************************
2  * control.c : Handle control of the playlist & running through it
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 static vlc_mutex_t global_lock = VLC_STATIC_MUTEX;
43
44 #undef pl_Get
45 playlist_t *pl_Get (vlc_object_t *obj)
46 {
47     playlist_t *pl;
48     libvlc_int_t *p_libvlc = obj->p_libvlc;
49
50     vlc_mutex_lock (&global_lock);
51     pl = libvlc_priv (p_libvlc)->p_playlist;
52     assert (pl != NULL);
53
54     if (!libvlc_priv (p_libvlc)->playlist_active)
55     {
56          playlist_Activate (pl);
57          libvlc_priv (p_libvlc)->playlist_active = true;
58     }
59     vlc_mutex_unlock (&global_lock);
60     return pl;
61 }
62
63 void pl_Deactivate (libvlc_int_t *p_libvlc)
64 {
65     bool deactivate;
66
67     vlc_mutex_lock (&global_lock);
68     deactivate = libvlc_priv (p_libvlc)->playlist_active;
69     vlc_mutex_unlock (&global_lock);
70
71     if (deactivate)
72         playlist_Deactivate (libvlc_priv (p_libvlc)->p_playlist);
73 }
74
75 void playlist_Lock( playlist_t *pl )
76 {
77     vlc_mutex_lock( &pl_priv(pl)->lock );
78 }
79
80 void playlist_Unlock( playlist_t *pl )
81 {
82     vlc_mutex_unlock( &pl_priv(pl)->lock );
83 }
84
85 void playlist_AssertLocked( playlist_t *pl )
86 {
87     vlc_assert_locked( &pl_priv(pl)->lock );
88 }
89
90 int playlist_Control( playlist_t * p_playlist, int i_query,
91                       bool b_locked, ... )
92 {
93     va_list args;
94     int i_result;
95     PL_LOCK_IF( !b_locked );
96     va_start( args, b_locked );
97     i_result = PlaylistVAControl( p_playlist, i_query, args );
98     va_end( args );
99     PL_UNLOCK_IF( !b_locked );
100
101     return i_result;
102 }
103
104 static int PlaylistVAControl( playlist_t * p_playlist, int i_query, va_list args )
105 {
106     playlist_item_t *p_item, *p_node;
107
108     PL_ASSERT_LOCKED;
109
110     if( !vlc_object_alive( p_playlist ) )
111         return VLC_EGENERIC;
112
113     if( playlist_IsEmpty( p_playlist ) && i_query != PLAYLIST_STOP )
114         return VLC_EGENERIC;
115
116     switch( i_query )
117     {
118     case PLAYLIST_STOP:
119         pl_priv(p_playlist)->request.i_status = PLAYLIST_STOPPED;
120         pl_priv(p_playlist)->request.b_request = true;
121         pl_priv(p_playlist)->request.p_item = NULL;
122         break;
123
124     // Node can be null, it will keep the same. Use with care ...
125     // Item null = take the first child of node
126     case PLAYLIST_VIEWPLAY:
127         p_node = (playlist_item_t *)va_arg( args, playlist_item_t * );
128         p_item = (playlist_item_t *)va_arg( args, playlist_item_t * );
129         if ( p_node == NULL )
130         {
131             p_node = get_current_status_node( p_playlist );
132             assert( p_node );
133         }
134         pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
135         pl_priv(p_playlist)->request.i_skip = 0;
136         pl_priv(p_playlist)->request.b_request = true;
137         pl_priv(p_playlist)->request.p_node = p_node;
138         pl_priv(p_playlist)->request.p_item = p_item;
139         if( p_item && var_GetBool( p_playlist, "random" ) )
140             pl_priv(p_playlist)->b_reset_currently_playing = true;
141         break;
142
143     case PLAYLIST_PLAY:
144         if( pl_priv(p_playlist)->p_input )
145         {
146             var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
147             break;
148         }
149         else
150         {
151             pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
152             pl_priv(p_playlist)->request.b_request = true;
153             pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
154             pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
155             pl_priv(p_playlist)->request.i_skip = 0;
156         }
157         break;
158
159     case PLAYLIST_PAUSE:
160         if( !pl_priv(p_playlist)->p_input )
161         {   /* FIXME: is this really useful without input? */
162             pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
163             /* return without notifying the playlist thread as there is nothing to do */
164             return VLC_SUCCESS;
165         }
166
167         if( var_GetInteger( pl_priv(p_playlist)->p_input, "state" ) == PAUSE_S )
168         {
169             pl_priv(p_playlist)->status.i_status = PLAYLIST_RUNNING;
170             var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
171         }
172         else
173         {
174             pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
175             var_SetInteger( pl_priv(p_playlist)->p_input, "state", PAUSE_S );
176         }
177         break;
178
179     case PLAYLIST_SKIP:
180         pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
181         pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
182         pl_priv(p_playlist)->request.i_skip = (int) va_arg( args, int );
183         /* if already running, keep running */
184         if( pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED )
185             pl_priv(p_playlist)->request.i_status = pl_priv(p_playlist)->status.i_status;
186         pl_priv(p_playlist)->request.b_request = true;
187         break;
188
189     default:
190         msg_Err( p_playlist, "unknown playlist query" );
191         return VLC_EBADVAR;
192     }
193     vlc_cond_signal( &pl_priv(p_playlist)->signal );
194
195     return VLC_SUCCESS;
196 }
197
198 /*****************************************************************************
199  * Preparse control
200  *****************************************************************************/
201 /** Enqueue an item for preparsing */
202 int playlist_PreparseEnqueue( playlist_t *p_playlist, input_item_t *p_item )
203 {
204     playlist_private_t *p_sys = pl_priv(p_playlist);
205
206     if( unlikely(p_sys->p_preparser == NULL) )
207         return VLC_ENOMEM;
208     playlist_preparser_Push( p_sys->p_preparser, p_item );
209     return VLC_SUCCESS;
210 }
211
212 int playlist_AskForArtEnqueue( playlist_t *p_playlist, input_item_t *p_item )
213 {
214     playlist_private_t *p_sys = pl_priv(p_playlist);
215
216     if( unlikely(p_sys->p_fetcher == NULL) )
217         return VLC_ENOMEM;
218     playlist_fetcher_Push( p_sys->p_fetcher, p_item );
219     return VLC_SUCCESS;
220 }
221